Fix json file throwing except

This commit is contained in:
D1GQ committed 2026-02-07 19:41:16 -06:00
1 parent 958e1371a6
commit 688e2f2854
2 files changed
+31 -34

No files matched your search

+30 -33
View File
@@ -35,7 +35,7 @@ internal abstract class AbstractJsonFile
if (_hasInit) return;
_hasInit = true;
if (!CheckFile())
if (!IsFileValid())
{
Save();
return;
@@ -106,15 +106,6 @@ internal abstract class AbstractJsonFile
}
}
/// <summary>
/// Reads the content from the JSON file.
/// </summary>
/// <returns>The content of the JSON file.</returns>
protected virtual string ReadFromFile()
{
return File.ReadAllText(FilePath);
}
/// <summary>
/// Saves the current instance's data to the JSON file.
/// </summary>
@@ -136,19 +127,10 @@ internal abstract class AbstractJsonFile
}
/// <summary>
/// Writes the JSON string to the file.
/// Determines whether the file specified by the current FilePath exists and contains valid, non-empty JSON content.
/// </summary>
/// <param name="json">The JSON string to write.</param>
protected virtual void WriteToFile(string json)
{
File.WriteAllText(FilePath, json);
}
/// <summary>
/// Checks if the JSON file exists and contains valid data.
/// </summary>
/// <returns>True if the file exists and contains valid JSON data, false otherwise.</returns>
private bool CheckFile()
/// <returns>true if the file exists, is not empty, and contains valid, non-empty JSON content; otherwise, false.</returns>
private bool IsFileValid()
{
var directory = Path.GetDirectoryName(FilePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
@@ -163,26 +145,23 @@ internal abstract class AbstractJsonFile
var content = TryReadFromFile();
if (string.IsNullOrEmpty(content.Trim())) return false;
var jsonElement = JsonSerializer.Deserialize<JsonElement>(content);
var fileInfo = new FileInfo(FilePath);
if (fileInfo.Length == 0)
{
return false;
}
try
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>(content);
var fileInfo = new FileInfo(FilePath);
if (fileInfo.Length == 0)
{
return false;
}
if (jsonElement.ValueKind == JsonValueKind.Object && !jsonElement.EnumerateObject().Any() ||
jsonElement.ValueKind == JsonValueKind.Array && !jsonElement.EnumerateArray().Any())
{
return false;
}
}
catch (JsonException)
{
return false;
}
catch (Exception)
{
return false;
@@ -190,4 +169,22 @@ internal abstract class AbstractJsonFile
return true;
}
/// <summary>
/// Reads the content from the JSON file.
/// </summary>
/// <returns>The content of the JSON file.</returns>
protected virtual string ReadFromFile()
{
return File.ReadAllText(FilePath);
}
/// <summary>
/// Writes the JSON string to the file.
/// </summary>
/// <param name="json">The JSON string to write.</param>
protected virtual void WriteToFile(string json)
{
File.WriteAllText(FilePath, json);
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ internal static class ModInfo
/// <summary>
/// The hotfix number for hotfix releases.
/// </summary>
internal const string HOTFIX_NUM = "2";
internal const string HOTFIX_NUM = "3";
/// <summary>
/// Indicates whether this is a hotfix release.