using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace PJ64Savestater; public class InputProfile { public string? ProfileCode { get; set; } public Dictionary Controls { get; } = []; private byte[] _ControllerInstanceGuid = Array.Empty(); [JsonConstructor] public InputProfile() { } public InputProfile(string profileCode) { ProfileCode = profileCode; } public Guid ControllerInstanceGuid { get { if (_ControllerInstanceGuid.Length == 0) return default; return new Guid(_ControllerInstanceGuid); } set { _ControllerInstanceGuid = value.ToByteArray(); } } public InputControl this[string name] { get { if (!Controls.ContainsKey(name)) Controls.Add(name, new InputControl()); return Controls[name]; } set { if (!Controls.ContainsKey(name)) Controls.Add(name, value); else Controls[name] = value; } } public void ClearProfile() { Controls.Clear(); } public void Save(string fileName) { File.WriteAllText(fileName, JsonConvert.SerializeObject(this, GetJsonSerializer())); } public static InputProfile? Load(string fileName) { return JsonConvert.DeserializeObject(File.ReadAllText(fileName), GetJsonSerializer()); } private static JsonSerializerSettings GetJsonSerializer() { return new() { TypeNameHandling = TypeNameHandling.Auto }; } }