50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Text.Json;
|
||
using System.Text.Json.Serialization;
|
||
|
||
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 = JsonSerializer.Deserialize<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 = JsonSerializer.Serialize(Instance, new JsonSerializerOptions
|
||
{
|
||
WriteIndented = true
|
||
});
|
||
File.WriteAllText(FileName, text, UTF8WithoutBom);
|
||
}
|
||
} |