mlaumcherb/Млаумчерб.Клиент/Config.cs
2024-09-01 00:30:50 +05:00

46 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
namespace Млаумчерб.Клиент;
public class Config
{
public string username { get; set; } = "";
public int memory { get; set; } = 4096;
public bool fullscreen { get; set; }
[JsonIgnore] public static Config Instance { get; private set; } = new();
public const string FileName = "млаумчерб.конфиг";
public const string BackupFileName = "млаумчерб.конфиг.старый";
public static readonly Encoding UTF8WithoutBom = new UTF8Encoding(false);
public static void LoadFromFile()
{
//TODO: log
if(!File.Exists(FileName))
{
SaveToFile();
return;
}
string text = File.ReadAllText(FileName);
Config? c = JsonConvert.DeserializeObject<Config>(text);
if (c is not null)
Instance = c;
else
{
File.Move(FileName, BackupFileName, true);
SaveToFile();
Errors.ShowMessageBox($"Не удалось прочитать конфиг.\n" +
$"Сломанный конфиг переименован в '{BackupFileName}'.\n" +
$"Создан новый файл '{FileName}'.");
}
}
public static void SaveToFile()
{
//TODO: log
var text = JsonConvert.SerializeObject(Instance, Formatting.Indented);
File.WriteAllText(FileName, text, UTF8WithoutBom);
}
}