From 85aefb4ba131521595e48fe1a25f4db9a69e71e6 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Sun, 27 Sep 2020 23:04:29 -0400 Subject: add arg checks in wrapper minor changes to settings shape, requires driver reinstall add error handling to writer grapher changes: add prettier serialization + comments add elements for scale and separated limit/exp reset irrelevant (invisible) arg input before checks/write --- grapher/Models/Serialized/RawAccelSettings.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'grapher/Models/Serialized/RawAccelSettings.cs') diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs index 3f5aebc..570a6c8 100644 --- a/grapher/Models/Serialized/RawAccelSettings.cs +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -1,6 +1,8 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.IO; +using System.Linq; namespace grapher.Models.Serialized { @@ -13,9 +15,8 @@ namespace grapher.Models.Serialized public static readonly string DefaultSettingsFile = Path.Combine(ExecutingDirectory, Constants.DefaultSettingsFileName); public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings { - MissingMemberHandling = MissingMemberHandling.Error, + MissingMemberHandling = MissingMemberHandling.Ignore, }; - #endregion Fields #region Constructors @@ -33,9 +34,10 @@ namespace grapher.Models.Serialized #endregion Constructors #region Properties - + [JsonProperty(Required = Required.Always)] public GUISettings GUISettings { get; set; } + [JsonProperty(DriverSettings.Key, Required = Required.Always)] public DriverSettings AccelerationSettings { get; set; } #endregion Properties @@ -51,15 +53,17 @@ namespace grapher.Models.Serialized { try { - return JsonConvert.DeserializeObject(File.ReadAllText(file), SerializerSettings); + var settings = JsonConvert.DeserializeObject(File.ReadAllText(file), SerializerSettings); + if (settings is null) throw new JsonException($"{file} contains invalid JSON"); + return settings; } catch (FileNotFoundException e) { throw new FileNotFoundException($"Settings file does not exist at {file}", e); } - catch (JsonSerializationException e) + catch (JsonException e) { - throw new JsonSerializationException($"Settings file at {file} does not contain valid Raw Accel Settings.", e); + throw new JsonException($"Settings file at {file} does not contain valid Raw Accel Settings.", e); } } @@ -80,7 +84,16 @@ namespace grapher.Models.Serialized public void Save(string file) { - File.WriteAllText(file, JsonConvert.SerializeObject(this, Formatting.Indented)); + JObject thisJO = JObject.FromObject(this); + AddComments(thisJO); + File.WriteAllText(file, thisJO.ToString(Formatting.Indented)); + } + + private void AddComments(JObject thisJO) + { + string modes = string.Join(" | ", Enum.GetNames(typeof(AccelMode))); + ((JObject)thisJO[DriverSettings.Key]) + .AddFirst(new JProperty("### Mode Types ###", modes)); } #endregion Methods -- cgit v1.2.3