Refactor: Reorganize project structure by moving core components into dedicated directories and introducing new configuration and API models.
This commit is contained in:
40
Configuration/ConfigManager.cs
Normal file
40
Configuration/ConfigManager.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Toak.Serialization;
|
||||
|
||||
namespace Toak.Configuration;
|
||||
public static class ConfigManager
|
||||
{
|
||||
private static readonly string ConfigDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", "toak");
|
||||
private static readonly string ConfigPath = Path.Combine(ConfigDir, "config.json");
|
||||
|
||||
public static ToakConfig LoadConfig()
|
||||
{
|
||||
if (!File.Exists(ConfigPath))
|
||||
{
|
||||
return new ToakConfig();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(ConfigPath);
|
||||
return JsonSerializer.Deserialize(json, AppJsonSerializerContext.Default.ToakConfig) ?? new ToakConfig();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return new ToakConfig();
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveConfig(ToakConfig config)
|
||||
{
|
||||
if (!Directory.Exists(ConfigDir))
|
||||
{
|
||||
Directory.CreateDirectory(ConfigDir);
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(config, AppJsonSerializerContext.Default.ToakConfig);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user