From 5ed1041d1671c6b7bb271753da5738c18b597c48 Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Mon, 15 Jul 2024 05:57:36 -0400 Subject: [PATCH] Late patch for the stupid unity methods --- Winch/Core/AssetLoaderObject.cs | 14 +++++++++- Winch/Core/WinchCore.cs | 10 ++++--- Winch/Patches/API/EncyclopediaPatcher.cs | 2 +- .../LanguageSelectorDropdownPatcher.cs | 8 +++--- Winch/Patches/LatePatcher.cs | 27 +++++++++++++++++++ Winch/Util/ItemUtil.cs | 10 +------ 6 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 Winch/Patches/LatePatcher.cs diff --git a/Winch/Core/AssetLoaderObject.cs b/Winch/Core/AssetLoaderObject.cs index 8d13eefb..2217c617 100644 --- a/Winch/Core/AssetLoaderObject.cs +++ b/Winch/Core/AssetLoaderObject.cs @@ -1,6 +1,8 @@ -using System; +using HarmonyLib; +using System; using UnityEngine; using Winch.Core.API; +using Winch.Patches; using Winch.Util; namespace Winch.Core @@ -12,6 +14,16 @@ private void Awake() WinchCore.Log.Debug("[AssetLoaderObject] Awake()"); GameManager.Instance.OnGameStarted += OnGameStarted; GameManager.Instance.OnGameEnded += OnGameEnded; + + try + { + LatePatcher.Initialize(WinchCore.Harmony); + } + catch (Exception ex) + { + WinchCore.Log.Error($"Failed to apply late winch patches: {ex}"); + } + WinchCore.Log.Debug("Late Harmony Patching complete."); } private void OnDisable() diff --git a/Winch/Core/WinchCore.cs b/Winch/Core/WinchCore.cs index 8c23185b..7e1a6994 100644 --- a/Winch/Core/WinchCore.cs +++ b/Winch/Core/WinchCore.cs @@ -11,6 +11,8 @@ namespace Winch.Core { public class WinchCore { + internal static Harmony Harmony; + public static Logger Log = new Logger(); public static Dictionary WinchModConfig = new(); @@ -41,12 +43,12 @@ public static void Main() ModAssemblyLoader.LoadModAssemblies(); + Harmony = new Harmony("com.dredge.winch"); + Log.Debug("Created Harmony Instance 'com.dredge.winch'. Patching..."); try { - var harmony = new Harmony("com.dredge.winch"); - Log.Debug("Created Harmony Instance 'com.dredge.winch'. Patching..."); - harmony.PatchAll(); - EnumUtil.Initialize(harmony); + Harmony.PatchAll(); + EnumUtil.Initialize(Harmony); } catch (Exception ex) { diff --git a/Winch/Patches/API/EncyclopediaPatcher.cs b/Winch/Patches/API/EncyclopediaPatcher.cs index e8fa5c24..021d5f5b 100644 --- a/Winch/Patches/API/EncyclopediaPatcher.cs +++ b/Winch/Patches/API/EncyclopediaPatcher.cs @@ -9,7 +9,7 @@ namespace Winch.Patches.API { [HarmonyPatch(typeof(Encyclopedia))] - class EncyclopediaPatcher + internal static class EncyclopediaPatcher { [HarmonyPrefix] [HarmonyPatch(nameof(Encyclopedia.OnZoneButtonClicked))] diff --git a/Winch/Patches/API/Localization/LanguageSelectorDropdownPatcher.cs b/Winch/Patches/API/Localization/LanguageSelectorDropdownPatcher.cs index f28775af..c461c6c1 100644 --- a/Winch/Patches/API/Localization/LanguageSelectorDropdownPatcher.cs +++ b/Winch/Patches/API/Localization/LanguageSelectorDropdownPatcher.cs @@ -4,11 +4,11 @@ namespace Winch.Patches.API.Localization { - [HarmonyPatch(typeof(LanguageSelectorDropdown))] - public static class LanguageSelectorDropdownPatcher + internal static class LanguageSelectorDropdownPatcher { - [HarmonyPatch(nameof(LanguageSelectorDropdown.Awake))] - [HarmonyPostfix] + /// + /// See for details on why this doesn't have attributes + /// public static void Awake(LanguageSelectorDropdown __instance) { // Size it to 8 so it doesn't go off screen. diff --git a/Winch/Patches/LatePatcher.cs b/Winch/Patches/LatePatcher.cs new file mode 100644 index 00000000..aa4242e0 --- /dev/null +++ b/Winch/Patches/LatePatcher.cs @@ -0,0 +1,27 @@ +using HarmonyLib; +using Winch.Patches.API; +using Winch.Patches.API.Localization; + +namespace Winch.Patches +{ + internal static class LatePatcher + { + /// + /// Any unity methods like "Awake" and etc. require patching later or else game explodes for whatever reason. Even just touching the method slightly makes the loading screen go black and spam the error below. + /// I assume it is because where we originally patch is before unity native dlls load or something. + /// + /* + System.MissingMethodException: assembly: type: member:(null) + at (wrapper managed-to-native) UnityEngine.Component.get_gameObject() + at UnityEngine.UI.Graphic.CacheCanvas()[0x00006] + at UnityEngine.UI.Graphic.get_canvas() [0x0000e] + at Coffee.UIExtensions.UIParticleUpdater.Refresh(Coffee.UIExtensions.UIParticle particle) [0x00015] + at Coffee.UIExtensions.UIParticleUpdater.Refresh() [0x00027] + */ + public static void Initialize(Harmony harmony) + { + harmony.Patch(AccessTools.Method(typeof(LanguageSelectorDropdown), nameof(LanguageSelectorDropdown.Awake)), + postfix: new HarmonyMethod(AccessTools.Method(typeof(LanguageSelectorDropdownPatcher), nameof(LanguageSelectorDropdownPatcher.Awake)))); + } + } +} diff --git a/Winch/Util/ItemUtil.cs b/Winch/Util/ItemUtil.cs index 977feda1..81993402 100644 --- a/Winch/Util/ItemUtil.cs +++ b/Winch/Util/ItemUtil.cs @@ -56,16 +56,8 @@ public static void AddModdedItemData() } /// - /// Cannot patch or else game explodes for whatever reason (even just touching the method slightly makes the loading screen go black and spam the error below) + /// Encyclopedia doesn't run until it is opened so we just search for it with Resources and add the fish /// - /* - System.MissingMethodException: assembly: type: member:(null) - at (wrapper managed-to-native) UnityEngine.Component.get_gameObject() - at UnityEngine.UI.Graphic.CacheCanvas()[0x00006] - at UnityEngine.UI.Graphic.get_canvas() [0x0000e] - at Coffee.UIExtensions.UIParticleUpdater.Refresh(Coffee.UIExtensions.UIParticle particle) [0x00015] - at Coffee.UIExtensions.UIParticleUpdater.Refresh() [0x00027] - */ public static void Encyclopedia() { WinchCore.Log.Info("[Encyclopedia] AddModdedFishItemData");