From 9147c08e93df58d2542e093141f9ce6dba8e2641 Mon Sep 17 00:00:00 2001 From: sir-wilhelm Date: Fri, 14 Feb 2020 20:28:05 -0600 Subject: [PATCH] Added support for auto updating .json files. resolves #9 --- SmartHunter/Core/Config/ConfigContainer.cs | 33 ++++++++++++++++------ SmartHunter/Game/Config/MainConfig.cs | 3 +- SmartHunter/Ui/Windows/ConsoleWindow.xaml | 6 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/SmartHunter/Core/Config/ConfigContainer.cs b/SmartHunter/Core/Config/ConfigContainer.cs index 50dca03a..9f759cf7 100644 --- a/SmartHunter/Core/Config/ConfigContainer.cs +++ b/SmartHunter/Core/Config/ConfigContainer.cs @@ -1,9 +1,9 @@ using System; using System.ComponentModel; using System.IO; -using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Converters; +using SmartHunter.Game.Helpers; using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace SmartHunter.Core.Config @@ -36,16 +36,19 @@ override protected void OnChanged() void Load() { - if (File.Exists(FullPathFileName))// && FileName.Equals("Config.json")) + if (File.Exists(FullPathFileName)) { try { string contents = null; - using var stream = File.Open(FullPathFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - using var reader = new StreamReader(stream, System.Text.Encoding.UTF8); - contents = reader.ReadToEnd(); + using (var stream = File.Open(FullPathFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var reader = new StreamReader(stream, System.Text.Encoding.UTF8)) + { + contents = reader.ReadToEnd(); + } - if (contents != GetAutoGenerateedJson() && FileName != "Config.json") + var contentsEqualsAutoGen = contents == GetAutoGenerateedJson(); + if (!contentsEqualsAutoGen && FileName != "Config.json") { Log.WriteWarning($"Warning: {FileName} differs from autogenerated version."); } @@ -65,9 +68,21 @@ void Load() settings.Converters.Add(new StringEnumConverter()); settings.Converters.Add(new StringFloatConverter()); - JsonConvert.PopulateObject(contents, Values, settings); + if (FileName.Equals("Config.json") || contentsEqualsAutoGen + || (!FileName.Equals("Config.json") && ConfigHelper.Main.Values.UseCustomData)) + { + JsonConvert.PopulateObject(contents, Values, settings); + Log.WriteLine($"{FileName} loaded"); + } + else + { + Log.WriteLine($"{FileName} will be renamed to custom_{FileName} and recreated..."); + Log.WriteLine("\tThis can be disabled by setting [\"UseCustomData\": true,] in Config.json."); - Log.WriteLine($"{FileName} loaded"); + File.Delete($"custom_{FileName}"); + File.Move(FileName, $"custom_{FileName}"); + Save(); + } } catch (Exception ex) { @@ -108,7 +123,7 @@ private string GetAutoGenerateedJson() ContractResolver = new ContractResolver() }; - settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); + settings.Converters.Add(new StringEnumConverter()); settings.Converters.Add(new StringFloatConverter()); return JsonConvert.SerializeObject(Values, settings); diff --git a/SmartHunter/Game/Config/MainConfig.cs b/SmartHunter/Game/Config/MainConfig.cs index d9c7bd1a..8ae93106 100644 --- a/SmartHunter/Game/Config/MainConfig.cs +++ b/SmartHunter/Game/Config/MainConfig.cs @@ -13,7 +13,8 @@ public class MainConfig public string MemoryFileName = "Memory.json"; public bool ShutdownWhenProcessExits = false; - public bool AutomaticallyCheckAndDownloadUpdates = true; // TODO: Rimetti a true + public bool UseCustomData = false; + public bool AutomaticallyCheckAndDownloadUpdates = true; public OverlayConfig Overlay = new OverlayConfig(); diff --git a/SmartHunter/Ui/Windows/ConsoleWindow.xaml b/SmartHunter/Ui/Windows/ConsoleWindow.xaml index 6d5de63b..5493e270 100644 --- a/SmartHunter/Ui/Windows/ConsoleWindow.xaml +++ b/SmartHunter/Ui/Windows/ConsoleWindow.xaml @@ -1,4 +1,4 @@ -