GtaVUsersInfo/Sources/WindowParameters.cs

78 lines
2.5 KiB
C#
Raw Permalink 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;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GtaVUsersInfo.Sources
{
[Serializable]
public class WindowParameters
{
public int width;
public int height;
public int leftPos;
public int rightPos;
public bool isFullScreen;
public string? prevFile;
public WindowParameters() { }
public WindowParameters(Size window, Size pos, bool isfull)
{
width = window.Width;
height = window.Height;
leftPos = pos.Width;
rightPos = pos.Height;
isFullScreen = isfull;
}
public WindowParameters(Size window, Size pos, bool isfull, string file)
{
width = window.Width;
height = window.Height;
leftPos = pos.Width;
rightPos = pos.Height;
isFullScreen = isfull;
prevFile = file;
}
public static void SaveWindowParameters(WindowParameters fontSettings)
{
string json = JsonConvert.SerializeObject(fontSettings, Formatting.Indented);
if (!Directory.Exists(Path.GetDirectoryName(Resources.windowParametersJsonPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(Resources.windowParametersJsonPath));
}
File.WriteAllText(Resources.windowParametersJsonPath, json);
}
public static WindowParameters LoadWindowParameters()
{
try
{
string json = File.ReadAllText(Resources.windowParametersJsonPath);
return JsonConvert.DeserializeObject<WindowParameters>(json);
}
catch (FileNotFoundException)
{
Console.WriteLine($"Файл с параметрами окна не найден: {Resources.windowParametersJsonPath}");
return null;
}
catch (JsonException ex)
{
Console.WriteLine($"Ошибка при десериализации JSON параметрами окна: {ex.Message}");
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Произошла неожиданная ошибка при открытии файла с параметрами окна: {ex.Message}");
return null;
}
}
}
}