mlaumcherb/Млаумчерб.Клиент/Настройки.cs

42 lines
1.7 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 Настройки
{
public string имя_пользователя { get; set; } = "";
public int выделенная_память_мб { get; set; } = 4096;
public bool открыватьаесь_экран { get; set; }
public static readonly Encoding UTF8WithoutBom = new UTF8Encoding(false);
public static Настройки ЗагрузитьИзФайла(string имяайла = "млаумчерб.настройки")
{
//TODO: лог
if(!File.Exists(имяайла))
{
return new Настройки();
}
string текст = File.ReadAllText(имяайла);
Настройки? н = JsonConvert.DeserializeObject<Настройки>(текст);
if (н == null)
{
File.Move(имяайла, имяайла + ".старые", true);
н = new Настройки();
н.СохранитьВФайл();
Ошибки.ПоказатьСообщение($"Не удалось прочитать настройки.\n" +
$"Сломанный файл настроек переименован в '{имя_файла}.старые'.\n" +
$"Создан новый файл '{имя_файла}'.");
}
return н;
}
public void СохранитьВФайл(string имяайла = "млаумчерб.настройки")
{
//TODO: log
var текст = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(имяайла, текст, UTF8WithoutBom);
}
}