From f1830ab619ed2d3517ee3f02f1acd6af612a588f Mon Sep 17 00:00:00 2001 From: AmazingAlek Date: Sat, 11 Jul 2020 11:04:37 +0200 Subject: [PATCH] gathered all file reading/writing to one place (#157) * gathered all file reading/writing to one place --- OWML.Launcher/App.cs | 9 +----- OWML.Launcher/Program.cs | 27 ++++++------------ OWML.ModHelper.Menus/ModConfigMenuBase.cs | 2 +- OWML.ModHelper/JsonHelper.cs | 26 +++++++++++++++++ OWML.ModHelper/ModStorage.cs | 34 +++-------------------- OWML.ModHelper/OWML.ModHelper.csproj | 1 + OWML.ModLoader/ModFinder.cs | 2 +- OWML.ModLoader/ModLoader.cs | 24 +++------------- OWML.ModLoader/Owo.cs | 2 +- OWML.Patcher/OWPatcher.cs | 3 +- 10 files changed, 49 insertions(+), 81 deletions(-) create mode 100644 OWML.ModHelper/JsonHelper.cs diff --git a/OWML.Launcher/App.cs b/OWML.Launcher/App.cs index 4ed3a70a5..82d0240ea 100644 --- a/OWML.Launcher/App.cs +++ b/OWML.Launcher/App.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.IO; using System.Linq; -using Newtonsoft.Json; using OWML.Common; using OWML.GameFinder; using OWML.ModHelper; @@ -75,16 +74,10 @@ private void LocateGamePath() if (gamePath != _owmlConfig.GamePath) { _owmlConfig.GamePath = gamePath; - SaveConfig(); + JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, _owmlConfig); } } - private void SaveConfig() - { - var json = JsonConvert.SerializeObject(_owmlConfig); - File.WriteAllText(Constants.OwmlConfigFileName, json); - } - private void CopyGameFiles() { var filesToCopy = new[] { "UnityEngine.CoreModule.dll", "Assembly-CSharp.dll" }; diff --git a/OWML.Launcher/Program.cs b/OWML.Launcher/Program.cs index 292a258a8..1352c27fe 100644 --- a/OWML.Launcher/Program.cs +++ b/OWML.Launcher/Program.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using Newtonsoft.Json; using OWML.Common; using OWML.GameFinder; using OWML.ModHelper; @@ -14,7 +12,7 @@ public class Program static void Main(string[] args) { var owmlConfig = GetOwmlConfig() ?? CreateOwmlConfig(); - owmlConfig.OWMLPath = AppDomain.CurrentDomain.BaseDirectory; + SaveOwmlPath(owmlConfig); var owmlManifest = GetOwmlManifest(); var writer = OutputFactory.CreateOutput(owmlConfig, null, owmlManifest); var modFinder = new ModFinder(owmlConfig, writer); @@ -29,32 +27,25 @@ static void Main(string[] args) private static IOwmlConfig GetOwmlConfig() { - return GetJsonObject(Constants.OwmlConfigFileName); + return JsonHelper.LoadJsonObject(Constants.OwmlConfigFileName); } private static IOwmlConfig CreateOwmlConfig() { - var config = GetJsonObject(Constants.OwmlDefaultConfigFileName); - var json = JsonConvert.SerializeObject(config, Formatting.Indented); - File.WriteAllText(Constants.OwmlConfigFileName, json); + var config = JsonHelper.LoadJsonObject(Constants.OwmlDefaultConfigFileName); + JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, config); return config; } - private static IModManifest GetOwmlManifest() + private static void SaveOwmlPath(IOwmlConfig owmlConfig) { - return GetJsonObject(Constants.OwmlManifestFileName); + owmlConfig.OWMLPath = AppDomain.CurrentDomain.BaseDirectory; + JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, owmlConfig); } - private static T GetJsonObject(string filename) + private static IModManifest GetOwmlManifest() { - if (!File.Exists(filename)) - { - return default(T); - } - var json = File.ReadAllText(filename) - .Replace("\\\\", "/") - .Replace("\\", "/"); - return JsonConvert.DeserializeObject(json); + return JsonHelper.LoadJsonObject(Constants.OwmlManifestFileName); } } diff --git a/OWML.ModHelper.Menus/ModConfigMenuBase.cs b/OWML.ModHelper.Menus/ModConfigMenuBase.cs index e0bb9a72e..f305b30af 100644 --- a/OWML.ModHelper.Menus/ModConfigMenuBase.cs +++ b/OWML.ModHelper.Menus/ModConfigMenuBase.cs @@ -28,7 +28,7 @@ public abstract class ModConfigMenuBase : ModPopupMenu, IModConfigMenuBase protected ModConfigMenuBase(IModConsole console, IModManifest manifest) : base(console) { Manifest = manifest; - Storage = new ModStorage(console, manifest); + Storage = new ModStorage(manifest); } public void Initialize(Menu menu, IModToggleInput toggleTemplate, IModSliderInput sliderTemplate, diff --git a/OWML.ModHelper/JsonHelper.cs b/OWML.ModHelper/JsonHelper.cs new file mode 100644 index 000000000..71c62d524 --- /dev/null +++ b/OWML.ModHelper/JsonHelper.cs @@ -0,0 +1,26 @@ +using System.IO; +using Newtonsoft.Json; + +namespace OWML.ModHelper +{ + public static class JsonHelper + { + public static T LoadJsonObject(string path) + { + if (!File.Exists(path)) + { + return default; + } + var json = File.ReadAllText(path) + .Replace("\\\\", "/") + .Replace("\\", "/"); + return JsonConvert.DeserializeObject(json); + } + + public static void SaveJsonObject(string path, T obj) + { + var json = JsonConvert.SerializeObject(obj, Formatting.Indented); + File.WriteAllText(path, json); + } + } +} diff --git a/OWML.ModHelper/ModStorage.cs b/OWML.ModHelper/ModStorage.cs index d0b35a6bf..553a69164 100644 --- a/OWML.ModHelper/ModStorage.cs +++ b/OWML.ModHelper/ModStorage.cs @@ -1,52 +1,26 @@ -using System; -using System.IO; -using Newtonsoft.Json; -using OWML.Common; +using OWML.Common; namespace OWML.ModHelper { public class ModStorage : IModStorage { - private readonly IModConsole _console; private readonly IModManifest _manifest; - public ModStorage(IModConsole console, IModManifest manifest) + public ModStorage(IModManifest manifest) { - _console = console; _manifest = manifest; } public T Load(string filename) { var path = _manifest.ModFolderPath + filename; - if (!File.Exists(path)) - { - return default; - } - try - { - var json = File.ReadAllText(path); - return JsonConvert.DeserializeObject(json); - } - catch (Exception ex) - { - _console.WriteLine($"Error while loading {path}: {ex}"); - return default; - } + return JsonHelper.LoadJsonObject(path); } public void Save(T obj, string filename) { var path = _manifest.ModFolderPath + filename; - try - { - var json = JsonConvert.SerializeObject(obj, Formatting.Indented); - File.WriteAllText(path, json); - } - catch (Exception ex) - { - _console.WriteLine($"Error while saving {path}: {ex}"); - } + JsonHelper.SaveJsonObject(path, obj); } } diff --git a/OWML.ModHelper/OWML.ModHelper.csproj b/OWML.ModHelper/OWML.ModHelper.csproj index e2eba8591..01309fe2f 100644 --- a/OWML.ModHelper/OWML.ModHelper.csproj +++ b/OWML.ModHelper/OWML.ModHelper.csproj @@ -76,6 +76,7 @@ + diff --git a/OWML.ModLoader/ModFinder.cs b/OWML.ModLoader/ModFinder.cs index e625b455c..1072a9f33 100644 --- a/OWML.ModLoader/ModFinder.cs +++ b/OWML.ModLoader/ModFinder.cs @@ -39,7 +39,7 @@ public IList GetMods() private IModData GetModData(IModManifest manifest) { - var storage = new ModStorage(_console, manifest); + var storage = new ModStorage(manifest); var config = storage.Load("config.json"); var defaultConfig = storage.Load("default-config.json"); if (config == null && defaultConfig == null) diff --git a/OWML.ModLoader/ModLoader.cs b/OWML.ModLoader/ModLoader.cs index b1ae3efa3..73c993fca 100644 --- a/OWML.ModLoader/ModLoader.cs +++ b/OWML.ModLoader/ModLoader.cs @@ -1,7 +1,4 @@ -using System; -using System.IO; -using Newtonsoft.Json; -using OWML.Common; +using OWML.Common; using OWML.ModHelper; using OWML.ModHelper.Events; using OWML.ModHelper.Menus; @@ -20,9 +17,9 @@ public static void LoadMods() { var owmlGo = new GameObject(); owmlGo.AddComponent(); - var owmlConfig = GetJsonObject(ConfigPath); - var owmlDefaultConfig = GetJsonObject(DefaultConfigPath); - var owmlManifest = GetJsonObject(ManifestPath); + var owmlConfig = JsonHelper.LoadJsonObject(ConfigPath); + var owmlDefaultConfig = JsonHelper.LoadJsonObject(DefaultConfigPath); + var owmlManifest = JsonHelper.LoadJsonObject(ManifestPath); if (owmlConfig == null || owmlManifest == null) { // Everything is wrong and can't write to console... @@ -42,18 +39,5 @@ public static void LoadMods() owo.LoadMods(); } - private static T GetJsonObject(string path) - { - try - { - var json = File.ReadAllText(path); - return JsonConvert.DeserializeObject(json); - } - catch (Exception) - { - return default(T); - } - } - } } diff --git a/OWML.ModLoader/Owo.cs b/OWML.ModLoader/Owo.cs index 1fedaa6c6..0cf9268ae 100644 --- a/OWML.ModLoader/Owo.cs +++ b/OWML.ModLoader/Owo.cs @@ -111,7 +111,7 @@ private IModHelper CreateModHelper(IModData modData) var logger = new ModLogger(_owmlConfig, modData.Manifest); var console = OutputFactory.CreateOutput(_owmlConfig, _logger, modData.Manifest); var assets = new ModAssets(console, modData.Manifest); - var storage = new ModStorage(console, modData.Manifest); + var storage = new ModStorage(modData.Manifest); var events = new ModEvents(logger, console, _harmonyHelper); var interaction = new ModInteraction(_modList, new InterfaceProxyFactory(), modData.Manifest); return new ModHelper.ModHelper(logger, console, _harmonyHelper, diff --git a/OWML.Patcher/OWPatcher.cs b/OWML.Patcher/OWPatcher.cs index c30dd3884..29227898c 100644 --- a/OWML.Patcher/OWPatcher.cs +++ b/OWML.Patcher/OWPatcher.cs @@ -4,7 +4,6 @@ using System.Linq; using dnlib.DotNet.Emit; using dnpatch; -using Newtonsoft.Json; using OWML.Common; namespace OWML.Patcher @@ -45,13 +44,13 @@ private void CopyFiles() "NAudio-Unity.dll", "OWML.ModHelper.Interaction.dll", Constants.OwmlManifestFileName, + Constants.OwmlConfigFileName, Constants.OwmlDefaultConfigFileName }; foreach (var filename in filesToCopy) { File.Copy(filename, $"{_owmlConfig.ManagedPath}/{filename}", true); } - File.WriteAllText($"{_owmlConfig.ManagedPath}/{Constants.OwmlConfigFileName}", JsonConvert.SerializeObject(_owmlConfig)); } private void PatchAssembly()