pkNX/pkNX.WinForms/MainEditor/EditUtil.cs
Kurt 11e5bdbb88 Add persistent rand settings
save to xml with version name
2018-11-24 23:50:40 -08:00

76 lines
2.3 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 PersonalRandSettings();
public SpeciesSettings Species { get; set; } = new SpeciesSettings();
public TrainerRandSettings Trainer { get; set; } = new TrainerRandSettings();
public MovesetRandSettings Move { get; set; } = new MovesetRandSettings();
public LearnSettings Learn { get; set; } = new LearnSettings();
}
public static class EditUtil
{
public static SharedSettings Settings { get; set; }
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);
}
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.ExecutablePath);
return Path.Combine(path, $"randsetting{game}.xml");
}
}
}