Skip to content

Commit

Permalink
Added more Comprehensive Exception Message when attempting to use an …
Browse files Browse the repository at this point in the history
…Instance Patch Method LavaGang#138
  • Loading branch information
HerpDerpinstine committed Nov 25, 2021
1 parent 55ec8cf commit a2cdb8e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
58. Switched Coroutine System for Il2Cpp to a Wrapper based Interface. (Credits to [HookedBehemoth](https://github.com/HookedBehemoth) :D)
59. Fixed occasional double-logging issue with MelonLogger. (Credits to [benaclejames](https://github.com/benaclejames) :D)
60. Removed Useless Logger Instance Constructor.
61. Added more Comprehensive Exception Message when attempting to use an Instance Patch Method.

---

Expand Down
1 change: 1 addition & 0 deletions MelonLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private static int Initialize()
HarmonyInstance = new HarmonyLib.Harmony(BuildInfo.Name);

Fixes.ForcedCultureInfo.Install();
Fixes.InstancePatchFix.Install();
PatchShield.Install();

MelonPreferences.Load();
Expand Down
35 changes: 35 additions & 0 deletions MelonLoader/Fixes/InstancePatchFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Reflection;
using HarmonyLib;
using MonoMod.RuntimeDetour;

namespace MelonLoader.Fixes
{
internal static class InstancePatchFix
{
internal static void Install()
{
Type instancePatchFixType = typeof(InstancePatchFix);
HarmonyMethod patchMethod = AccessTools.Method(instancePatchFixType, "PatchMethod").ToNewHarmonyMethod();

try
{
Core.HarmonyInstance.Patch(AccessTools.Method("HarmonyLib.PatchFunctions:ReversePatch"), patchMethod);
Core.HarmonyInstance.Patch(AccessTools.Method("HarmonyLib.HarmonyMethod:ImportMethod"), patchMethod);
}
catch (Exception ex) { MelonLogger.Warning($"PatchFix Exception: {ex}"); }

Hook.OnDetour += (detour, originalMethod, patchMethod, delegateTarget) => PatchMethod(patchMethod);
Detour.OnDetour += (detour, originalMethod, patchMethod) => PatchMethod(patchMethod);
}

private static bool PatchMethod(MethodBase __0)
{
if (__0 == null)
throw new NullReferenceException("Patch Method");
if ((__0 != null) && !__0.IsStatic)
throw new Exception("Patch Method can't be an Instance Method!");
return true;
}
}
}
1 change: 1 addition & 0 deletions MelonLoader/MelonLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Fixes\ForcedCultureInfo.cs" />
<Compile Include="Fixes\InstancePatchFix.cs" />
<Compile Include="GameVersionHandler.cs" />
<Compile Include="InternalUtils\MelonStartScreen.cs" />
<Compile Include="Lemons\LemonAction.cs" />
Expand Down

0 comments on commit a2cdb8e

Please sign in to comment.