Skip to content

Commit

Permalink
gathered all file reading/writing to one place (#157)
Browse files Browse the repository at this point in the history
* gathered all file reading/writing to one place
  • Loading branch information
amazingalek authored Jul 11, 2020
1 parent 89e4fb4 commit f1830ab
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 81 deletions.
9 changes: 1 addition & 8 deletions OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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" };
Expand Down
27 changes: 9 additions & 18 deletions OWML.Launcher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using Newtonsoft.Json;
using OWML.Common;
using OWML.GameFinder;
using OWML.ModHelper;
Expand All @@ -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);
Expand All @@ -29,32 +27,25 @@ static void Main(string[] args)

private static IOwmlConfig GetOwmlConfig()
{
return GetJsonObject<OwmlConfig>(Constants.OwmlConfigFileName);
return JsonHelper.LoadJsonObject<OwmlConfig>(Constants.OwmlConfigFileName);
}

private static IOwmlConfig CreateOwmlConfig()
{
var config = GetJsonObject<OwmlConfig>(Constants.OwmlDefaultConfigFileName);
var json = JsonConvert.SerializeObject(config, Formatting.Indented);
File.WriteAllText(Constants.OwmlConfigFileName, json);
var config = JsonHelper.LoadJsonObject<OwmlConfig>(Constants.OwmlDefaultConfigFileName);
JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, config);
return config;
}

private static IModManifest GetOwmlManifest()
private static void SaveOwmlPath(IOwmlConfig owmlConfig)
{
return GetJsonObject<ModManifest>(Constants.OwmlManifestFileName);
owmlConfig.OWMLPath = AppDomain.CurrentDomain.BaseDirectory;
JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, owmlConfig);
}

private static T GetJsonObject<T>(string filename)
private static IModManifest GetOwmlManifest()
{
if (!File.Exists(filename))
{
return default(T);
}
var json = File.ReadAllText(filename)
.Replace("\\\\", "/")
.Replace("\\", "/");
return JsonConvert.DeserializeObject<T>(json);
return JsonHelper.LoadJsonObject<ModManifest>(Constants.OwmlManifestFileName);
}

}
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper.Menus/ModConfigMenuBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions OWML.ModHelper/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.IO;
using Newtonsoft.Json;

namespace OWML.ModHelper
{
public static class JsonHelper
{
public static T LoadJsonObject<T>(string path)
{
if (!File.Exists(path))
{
return default;
}
var json = File.ReadAllText(path)
.Replace("\\\\", "/")
.Replace("\\", "/");
return JsonConvert.DeserializeObject<T>(json);
}

public static void SaveJsonObject<T>(string path, T obj)
{
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
File.WriteAllText(path, json);
}
}
}
34 changes: 4 additions & 30 deletions OWML.ModHelper/ModStorage.cs
Original file line number Diff line number Diff line change
@@ -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<T>(string filename)
{
var path = _manifest.ModFolderPath + filename;
if (!File.Exists(path))
{
return default;
}
try
{
var json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception ex)
{
_console.WriteLine($"Error while loading {path}: {ex}");
return default;
}
return JsonHelper.LoadJsonObject<T>(path);
}

public void Save<T>(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);
}

}
Expand Down
1 change: 1 addition & 0 deletions OWML.ModHelper/OWML.ModHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="CommandLineArguments.cs" />
<Compile Include="ControllerButton.cs" />
<Compile Include="DontDestroyOnLoad.cs" />
<Compile Include="JsonHelper.cs" />
<Compile Include="ModBehaviour.cs" />
<Compile Include="ModConfig.cs" />
<Compile Include="Logging\ModFileOutput.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModLoader/ModFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IList<IModData> GetMods()

private IModData GetModData(IModManifest manifest)
{
var storage = new ModStorage(_console, manifest);
var storage = new ModStorage(manifest);
var config = storage.Load<ModConfig>("config.json");
var defaultConfig = storage.Load<ModConfig>("default-config.json");
if (config == null && defaultConfig == null)
Expand Down
24 changes: 4 additions & 20 deletions OWML.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,9 +17,9 @@ public static void LoadMods()
{
var owmlGo = new GameObject();
owmlGo.AddComponent<OwmlBehaviour>();
var owmlConfig = GetJsonObject<OwmlConfig>(ConfigPath);
var owmlDefaultConfig = GetJsonObject<OwmlConfig>(DefaultConfigPath);
var owmlManifest = GetJsonObject<ModManifest>(ManifestPath);
var owmlConfig = JsonHelper.LoadJsonObject<OwmlConfig>(ConfigPath);
var owmlDefaultConfig = JsonHelper.LoadJsonObject<OwmlConfig>(DefaultConfigPath);
var owmlManifest = JsonHelper.LoadJsonObject<ModManifest>(ManifestPath);
if (owmlConfig == null || owmlManifest == null)
{
// Everything is wrong and can't write to console...
Expand All @@ -42,18 +39,5 @@ public static void LoadMods()
owo.LoadMods();
}

private static T GetJsonObject<T>(string path)
{
try
{
var json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception)
{
return default(T);
}
}

}
}
2 changes: 1 addition & 1 deletion OWML.ModLoader/Owo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions OWML.Patcher/OWPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using dnlib.DotNet.Emit;
using dnpatch;
using Newtonsoft.Json;
using OWML.Common;

namespace OWML.Patcher
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f1830ab

Please sign in to comment.