toast_mcl/Programs/Config/ConfigUtils.cs

60 lines
1.5 KiB
C#
Raw Normal View History

2024-07-15 17:02:53 +08:00
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using toast_mcl.Config;
namespace toast_mcl.Programs;
public static class ConfigUtils
{
public static ConfigSettings ?Settings { get; private set;}
private static readonly string ConfigFolderPath = Path.Combine(Base.AppDirectory, "toast_mc");
private static readonly string ConfigFilePath = Path.Combine(ConfigFolderPath, "config.json");
public static void Init()
{
if (!Directory.Exists(ConfigFolderPath))
{
Directory.CreateDirectory(ConfigFolderPath);
}
if (!File.Exists(ConfigFilePath))
{
CreateDefaultConfig();
}
}
private static void CreateDefaultConfig()
{
Settings = DefaultConfig.GetDefaultConfig();
SaveConfig();
}
public static void LoadConfig()
{
if (File.Exists(ConfigFilePath))
{
string configJson = File.ReadAllText(ConfigFilePath);
Settings = JsonConvert.DeserializeObject<ConfigSettings>(configJson);
}
else
{
CreateDefaultConfig();
}
var ConfigVersion = Settings?.AppSettings?.Version;
if (ConfigVersion != Base.Version)
{
var oldSettings = Settings;
UpdateConfig();
}
}
private static void UpdateConfig()
{
}
public static void SaveConfig()
{
string configJson = JsonConvert.SerializeObject(Settings, Formatting.Indented);
File.WriteAllText(ConfigFilePath, configJson);
}
}