From f5eef66707869904443a19f59b661710c6fc140d Mon Sep 17 00:00:00 2001 From: amazingalek Date: Sat, 28 Dec 2019 14:03:02 +0100 Subject: [PATCH] cleanup and output (#32) --- OWML.Common/IModBehaviour.cs | 10 ------- OWML.Common/OWML.Common.csproj | 1 - OWML.ModHelper.Events/HarmonyHelper.cs | 7 ----- OWML.ModHelper.Events/TypeExtensions.cs | 1 - OWML.ModHelper/ModBehaviour.cs | 2 +- OWML.ModHelper/ModConsole.cs | 5 +++- OWML.ModLoader/ModLoader.cs | 38 +++++++------------------ OWML.ModLoader/Owo.cs | 18 ++++++++---- 8 files changed, 27 insertions(+), 55 deletions(-) delete mode 100644 OWML.Common/IModBehaviour.cs diff --git a/OWML.Common/IModBehaviour.cs b/OWML.Common/IModBehaviour.cs deleted file mode 100644 index 1fa0e9c0..00000000 --- a/OWML.Common/IModBehaviour.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Collections; -using UnityEngine; - -namespace OWML.Common -{ - public interface IModBehaviour - { - Coroutine StartCoroutine(IEnumerator routine); - } -} diff --git a/OWML.Common/OWML.Common.csproj b/OWML.Common/OWML.Common.csproj index 8ca67e60..5cea5c91 100644 --- a/OWML.Common/OWML.Common.csproj +++ b/OWML.Common/OWML.Common.csproj @@ -57,7 +57,6 @@ - diff --git a/OWML.ModHelper.Events/HarmonyHelper.cs b/OWML.ModHelper.Events/HarmonyHelper.cs index d70fcc73..353b608a 100644 --- a/OWML.ModHelper.Events/HarmonyHelper.cs +++ b/OWML.ModHelper.Events/HarmonyHelper.cs @@ -21,7 +21,6 @@ public void AddPrefix(string methodName, Type patchType, string patchMethodNa var prefix = patchType.GetAnyMethod(patchMethodName); if (prefix == null) { - _logger.Log("prefix is null"); _console.WriteLine("prefix is null"); return; } @@ -33,7 +32,6 @@ public void AddPostfix(string methodName, Type patchType, string patchMethodN var postfix = patchType.GetAnyMethod(patchMethodName); if (postfix == null) { - _logger.Log("postfix is null"); _console.WriteLine("postfix is null"); return; } @@ -50,7 +48,6 @@ public void Transpile(string methodName, Type patchType, string patchMethodNa var patchMethod = patchType.GetAnyMethod(patchMethodName); if (patchMethod == null) { - _logger.Log("patchMethod is null"); _console.WriteLine("patchMethod is null"); return; } @@ -70,7 +67,6 @@ private void Patch(string methodName, MethodInfo prefix, MethodInfo postfix, } catch (Exception ex) { - _logger.Log($"Exception while creating harmony instance: {ex}"); _console.WriteLine($"Exception while creating harmony instance: {ex}"); return; } @@ -83,13 +79,11 @@ private void Patch(string methodName, MethodInfo prefix, MethodInfo postfix, } catch (Exception ex) { - _logger.Log($"Exception while getting method {methodName} of {targetType.Name}: {ex}"); _console.WriteLine($"Exception while getting method {methodName} of {targetType.Name}: {ex}"); return; } if (original == null) { - _logger.Log("original is null"); _console.WriteLine("original is null"); return; } @@ -103,7 +97,6 @@ private void Patch(string methodName, MethodInfo prefix, MethodInfo postfix, } catch (Exception ex) { - _logger.Log($"Exception while patching {targetType.Name}: {ex}"); _console.WriteLine($"Exception while patching {targetType.Name}: {ex}"); } } diff --git a/OWML.ModHelper.Events/TypeExtensions.cs b/OWML.ModHelper.Events/TypeExtensions.cs index b80d921f..d75b0fdf 100644 --- a/OWML.ModHelper.Events/TypeExtensions.cs +++ b/OWML.ModHelper.Events/TypeExtensions.cs @@ -57,7 +57,6 @@ public static void SetValue(this object obj, string name, object value) if (property != null) { property.SetValue(obj, value, null); - return; } } diff --git a/OWML.ModHelper/ModBehaviour.cs b/OWML.ModHelper/ModBehaviour.cs index fa0c7e3f..37896c91 100644 --- a/OWML.ModHelper/ModBehaviour.cs +++ b/OWML.ModHelper/ModBehaviour.cs @@ -3,7 +3,7 @@ namespace OWML.ModHelper { - public class ModBehaviour : MonoBehaviour, IModBehaviour + public class ModBehaviour : MonoBehaviour { public static IModHelper ModHelper; diff --git a/OWML.ModHelper/ModConsole.cs b/OWML.ModHelper/ModConsole.cs index 8ae5285c..1e997cd2 100644 --- a/OWML.ModHelper/ModConsole.cs +++ b/OWML.ModHelper/ModConsole.cs @@ -8,14 +8,17 @@ namespace OWML.ModHelper public class ModConsole : IModConsole { private readonly FileStream _writer; + private readonly IModLogger _logger; - public ModConsole(IModConfig config) + public ModConsole(IModConfig config, IModLogger logger) { + _logger = logger; _writer = File.Open(config.OutputFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); } public void WriteLine(string s) { + _logger.Log(s); var bytes = Encoding.UTF8.GetBytes(s + Environment.NewLine); _writer.Write(bytes, 0, bytes.Length); _writer.Flush(); diff --git a/OWML.ModLoader/ModLoader.cs b/OWML.ModLoader/ModLoader.cs index 3ab2c2cf..353f51cc 100644 --- a/OWML.ModLoader/ModLoader.cs +++ b/OWML.ModLoader/ModLoader.cs @@ -11,35 +11,23 @@ namespace OWML.ModLoader public class ModLoader { private static readonly string ConfigPath = $"{Application.dataPath}/Managed/OWML.Config.json"; - private static readonly string SecondaryLogPath = $"{Application.dataPath}/Resources/OWML.Output.txt"; public static void LoadMods() { - SecondaryLog($"In {nameof(ModLoader)}.{nameof(LoadMods)}!"); - SecondaryLog("Getting config..."); var config = GetConfig(); if (config == null) { - SecondaryLog("Config is null"); return; } - SecondaryLog("Got config!"); - SecondaryLog("Loading mods..."); - try - { - var logger = new ModLogger(config); - var console = new ModConsole(config); - var harmonyHelper = new HarmonyHelper(logger, console); - var events = new ModEvents(harmonyHelper); - var modFinder = new ModFinder(config); - var owo = new Owo(modFinder, logger, console, config, harmonyHelper, events); - owo.LoadMods(); - SecondaryLog("Loaded mods"); - } - catch (Exception ex) - { - SecondaryLog("Error while loading mods: " + ex); - } + var logger = new ModLogger(config); + logger.Log("Got config!"); + var console = new ModConsole(config, logger); + console.WriteLine("Mod loader has been initialized."); + var harmonyHelper = new HarmonyHelper(logger, console); + var events = new ModEvents(harmonyHelper); + var modFinder = new ModFinder(config); + var owo = new Owo(modFinder, logger, console, config, harmonyHelper, events); + owo.LoadMods(); } private static IModConfig GetConfig() @@ -49,17 +37,11 @@ private static IModConfig GetConfig() var json = File.ReadAllText(ConfigPath); return JsonConvert.DeserializeObject(json); } - catch (Exception ex) + catch (Exception) { - SecondaryLog("Error while loading config: " + ex); return null; } } - private static void SecondaryLog(string s) - { - File.AppendAllText(SecondaryLogPath, $"{DateTime.Now}: {s}{Environment.NewLine}"); - } - } } diff --git a/OWML.ModLoader/Owo.cs b/OWML.ModLoader/Owo.cs index 742f18f5..ac63314c 100644 --- a/OWML.ModLoader/Owo.cs +++ b/OWML.ModLoader/Owo.cs @@ -29,9 +29,9 @@ public Owo(IModFinder modFinder, IModLogger logger, IModConsole console, IModCon public void LoadMods() { - _logger.Log($"{nameof(Owo)}: {nameof(LoadMods)}"); if (_config.Verbose) { + _console.WriteLine("Verbose mod is enabled"); Application.logMessageReceived += OnLogMessageReceived; } var manifests = _modFinder.GetManifests(); @@ -69,11 +69,18 @@ private void LoadMod(IModHelper helper) _logger.Log($"Loading {helper.Manifest.UniqueName} ({helper.Manifest.Version})..."); _logger.Log("Adding mod behaviour..."); var go = new GameObject(helper.Manifest.UniqueName); - var mod = (ModBehaviour)go.AddComponent(modType); - _logger.Log("Added! Initializing..."); - mod.Init(helper); + try + { + var mod = (ModBehaviour)go.AddComponent(modType); + _logger.Log("Added! Initializing..."); + mod.Init(helper); + } + catch (Exception ex) + { + _console.WriteLine($"Error while adding/initializing {helper.Manifest.UniqueName}: {ex}"); + return; + } _console.WriteLine($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version})."); - _logger.Log($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version})."); } private Type LoadModType(IModManifest manifest) @@ -92,7 +99,6 @@ private Type LoadModType(IModManifest manifest) } catch (Exception ex) { - _logger.Log($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}"); _console.WriteLine($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}"); return null; }