Files
pkNX/pkNX.WinForms/MainEditor/EditUtil.cs
Kurt 0936c08eb1 LZA 1.0.2
Cumulative changes from the team.

Co-Authored-By: Matt <17801814+sora10pls@users.noreply.github.com>
Co-Authored-By: SciresM <8676005+SciresM@users.noreply.github.com>
Co-Authored-By: Lusamine <30205550+Lusamine@users.noreply.github.com>
2025-11-16 15:56:12 -06:00

72 lines
1.9 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using pkNX.Randomization;
using pkNX.Structures;
namespace pkNX.WinForms;
[Serializable]
public class SharedSettings
{
public PersonalRandSettings Personal { get; set; } = new();
public SpeciesSettings Species { get; set; } = new();
public TrainerRandSettings Trainer { get; set; } = new();
public MovesetRandSettings Move { get; set; } = new();
public LearnSettings Learn { get; set; } = new();
}
public static class EditUtil
{
public static SharedSettings Settings { get; set; } = new();
public static void LoadSettings(GameVersion game)
{
string path = GetSettingsFileName(game);
if (!File.Exists(path))
{
Settings = new SharedSettings();
return;
}
using var file = File.OpenRead(path);
var reader = new XmlSerializer(typeof(SharedSettings));
try
{
Settings = (SharedSettings?) reader.Deserialize(file) ?? new SharedSettings();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
public static void SaveSettings(GameVersion game)
{
string path = GetSettingsFileName(game);
using var file = File.Create(path);
var writer = new XmlSerializer(typeof(SharedSettings));
try
{
writer.Serialize(file, Settings);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
if (!File.Exists(path))
return;
file.Close();
File.Delete(path);
}
}
private static string GetSettingsFileName(GameVersion game)
{
var path = Path.GetDirectoryName(Application.StartupPath);
ArgumentNullException.ThrowIfNull(path);
return Path.Combine(path, $"randsetting{game}.xml");
}
}