Skip to content

Commit

Permalink
Late patch for the stupid unity methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Jul 15, 2024
1 parent b58746d commit 5ed1041
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
14 changes: 13 additions & 1 deletion Winch/Core/AssetLoaderObject.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions Winch/Core/WinchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Winch.Core
{
public class WinchCore
{
internal static Harmony Harmony;

public static Logger Log = new Logger();

public static Dictionary<string, object> WinchModConfig = new();
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Winch/Patches/API/EncyclopediaPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Winch.Patches.API
{
[HarmonyPatch(typeof(Encyclopedia))]
class EncyclopediaPatcher
internal static class EncyclopediaPatcher
{
[HarmonyPrefix]
[HarmonyPatch(nameof(Encyclopedia.OnZoneButtonClicked))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
/// <summary>
/// See <see cref="LatePatcher.Initialize"/> for details on why this doesn't have attributes
/// </summary>
public static void Awake(LanguageSelectorDropdown __instance)
{
// Size it to 8 so it doesn't go off screen.
Expand Down
27 changes: 27 additions & 0 deletions Winch/Patches/LatePatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using HarmonyLib;
using Winch.Patches.API;
using Winch.Patches.API.Localization;

namespace Winch.Patches
{
internal static class LatePatcher
{
/// <summary>
/// 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.
/// </summary>
/*
System.MissingMethodException: assembly:<unknown assembly> type:<unknown 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))));
}
}
}
10 changes: 1 addition & 9 deletions Winch/Util/ItemUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,8 @@ public static void AddModdedItemData()
}

/// <summary>
/// Cannot patch <see cref="Encyclopedia.Awake"/> 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 <see cref="Encyclopedia.Awake"/> until it is opened so we just search for it with Resources and add the fish
/// </summary>
/*
System.MissingMethodException: assembly:<unknown assembly> type:<unknown 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");
Expand Down

0 comments on commit 5ed1041

Please sign in to comment.