46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
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);
|
||
}
|
||
} |