Files
2024-10-27 08:45:49 +01:00

77 lines
1.7 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PJ64Savestater;
public class InputProfile
{
public string? ProfileCode { get; set; }
public Dictionary<string, InputControl> Controls { get; } = [];
private byte[] _ControllerInstanceGuid = Array.Empty<byte>();
[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<InputProfile>(File.ReadAllText(fileName), GetJsonSerializer());
}
private static JsonSerializerSettings GetJsonSerializer()
{
return new()
{
TypeNameHandling = TypeNameHandling.Auto
};
}
}