mirror of
https://github.com/sebastian-heinz/mhf-server.git
synced 2025-04-03 13:28:30 +08:00
54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
using System.IO;
|
|
using Arrowgene.Services.Serialization;
|
|
using Mhf.Server.Common;
|
|
|
|
namespace Mhf.Server.Setting
|
|
{
|
|
public class SettingProvider
|
|
{
|
|
private readonly string _directory;
|
|
|
|
|
|
public SettingProvider(string directory = null)
|
|
{
|
|
if (Directory.Exists(directory))
|
|
{
|
|
_directory = directory;
|
|
}
|
|
else
|
|
{
|
|
_directory = Util.ExecutingDirectory();
|
|
}
|
|
}
|
|
|
|
public string GetSettingsPath(string file)
|
|
{
|
|
return Path.Combine(_directory, file);
|
|
}
|
|
|
|
public void Save<T>(T settings, string file)
|
|
{
|
|
string path = GetSettingsPath(file);
|
|
string json = JsonSerializer.Serialize(settings);
|
|
File.WriteAllText(path, json);
|
|
}
|
|
|
|
public T Load<T>(string file)
|
|
{
|
|
T settings;
|
|
string path = GetSettingsPath(file);
|
|
if (File.Exists(path))
|
|
{
|
|
string json = File.ReadAllText(path);
|
|
settings = JsonSerializer.Deserialize<T>(json);
|
|
}
|
|
else
|
|
{
|
|
settings = default(T);
|
|
}
|
|
|
|
return settings;
|
|
}
|
|
}
|
|
}
|