From 55b739c50db217e6a61678c1eb1412e8884e3462 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 20 Aug 2020 15:46:04 -0700 Subject: Serialization mostly working --- grapher/Models/Serialized/RawAccelSettings.cs | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 grapher/Models/Serialized/RawAccelSettings.cs (limited to 'grapher/Models/Serialized/RawAccelSettings.cs') diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs new file mode 100644 index 0000000..d8896b6 --- /dev/null +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -0,0 +1,81 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Serialized +{ + [Serializable] + public class RawAccelSettings + { + public const string DefaultSettingsFile = @".\settings.json"; + + public RawAccelSettings( + ManagedAccel managedAccel, + GUISettings guiSettings) + { + ManagedAccel = managedAccel; + GUISettings = guiSettings; + } + + public ManagedAccel ManagedAccel { get; set; } + + public GUISettings GUISettings { get; set; } + + public static RawAccelSettings Load() + { + return Load(DefaultSettingsFile); + } + + public static RawAccelSettings Load(string file) + { + if (!Exists(file)) + { + throw new Exception($"Settings file does not exist at {file}"); + } + + object deserializedObject; + try + { + deserializedObject = JsonConvert.DeserializeObject(File.ReadAllText(file)); + } + catch + { + throw new Exception($"Settings file at {file} does not contain valid JSON."); + } + + RawAccelSettings deserializedSettings = (RawAccelSettings)deserializedObject; + + if (deserializedSettings == null) + { + throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings."); + } + + return deserializedSettings; + } + + public static bool Exists() + { + return Exists(DefaultSettingsFile); + } + + public static bool Exists(string file) + { + return File.Exists(file); + } + + public void Save() + { + Save(DefaultSettingsFile); + } + + public void Save(string file) + { + File.WriteAllText(file, JsonConvert.SerializeObject(this, Formatting.Indented)); + } + } +} -- cgit v1.2.3 From b874058d82a60a39163e91a26f370ff308b8af32 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Sat, 22 Aug 2020 02:46:45 -0700 Subject: Saving and loading fully works --- grapher/Models/Serialized/RawAccelSettings.cs | 31 +++++++++++++++------------ 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'grapher/Models/Serialized/RawAccelSettings.cs') diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs index d8896b6..21a7f0c 100644 --- a/grapher/Models/Serialized/RawAccelSettings.cs +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,20 +13,29 @@ namespace grapher.Models.Serialized [Serializable] public class RawAccelSettings { - public const string DefaultSettingsFile = @".\settings.json"; + public const string DefaultSettingsFileName = @"settings.json"; + public static readonly string ExecutingDirectory = AppDomain.CurrentDomain.BaseDirectory; + public static readonly string DefaultSettingsFile = Path.Combine(ExecutingDirectory, DefaultSettingsFileName); + public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings + { + MissingMemberHandling = MissingMemberHandling.Error, + }; + + public RawAccelSettings() { } public RawAccelSettings( ManagedAccel managedAccel, GUISettings guiSettings) { - ManagedAccel = managedAccel; + AccelerationSettings = new modifier_args(managedAccel); GUISettings = guiSettings; } - public ManagedAccel ManagedAccel { get; set; } public GUISettings GUISettings { get; set; } + public modifier_args AccelerationSettings { get; set; } + public static RawAccelSettings Load() { return Load(DefaultSettingsFile); @@ -38,21 +48,14 @@ namespace grapher.Models.Serialized throw new Exception($"Settings file does not exist at {file}"); } - object deserializedObject; + RawAccelSettings deserializedSettings; try { - deserializedObject = JsonConvert.DeserializeObject(File.ReadAllText(file)); + deserializedSettings = JsonConvert.DeserializeObject(File.ReadAllText(file), SerializerSettings); } - catch - { - throw new Exception($"Settings file at {file} does not contain valid JSON."); - } - - RawAccelSettings deserializedSettings = (RawAccelSettings)deserializedObject; - - if (deserializedSettings == null) + catch(Exception e) { - throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings."); + throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings.", e); } return deserializedSettings; -- cgit v1.2.3