diff options
| author | Jacob Palecki <[email protected]> | 2020-08-20 15:46:04 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-08-20 15:46:04 -0700 |
| commit | 55b739c50db217e6a61678c1eb1412e8884e3462 (patch) | |
| tree | a1924dce2e90d09dd3075c0ec1aca2283efc41d2 /grapher/Models/Serialized/RawAccelSettings.cs | |
| parent | Add accel type to active values and tweak color (diff) | |
| download | rawaccel-55b739c50db217e6a61678c1eb1412e8884e3462.tar.xz rawaccel-55b739c50db217e6a61678c1eb1412e8884e3462.zip | |
Serialization mostly working
Diffstat (limited to 'grapher/Models/Serialized/RawAccelSettings.cs')
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 81 |
1 files changed, 81 insertions, 0 deletions
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)); + } + } +} |