From 271e8a1dde38015dd89e9fae65d5de0e8ed27281 Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:08:12 +0700 Subject: [PATCH] Fix loading into singleplayer worlds (Issue with multiple harmony patches on one method) --- .../Patches/ModAssemblyLoadContextPatches.cs | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/CringeLauncher/Patches/ModAssemblyLoadContextPatches.cs b/CringeLauncher/Patches/ModAssemblyLoadContextPatches.cs index 2222a32..05ed99b 100644 --- a/CringeLauncher/Patches/ModAssemblyLoadContextPatches.cs +++ b/CringeLauncher/Patches/ModAssemblyLoadContextPatches.cs @@ -44,19 +44,6 @@ public static class ModAssemblyLoadContextPatches return matcher.Instructions(); } - [HarmonyPatch(typeof(MySession), nameof(MySession.Load))] - [HarmonyPatch(typeof(MySession), "LoadMultiplayer")] - [HarmonyPrefix] - private static void LoadPrefix() - { - if (_currentSessionContext is not null) - throw new InvalidOperationException("Previous session context was not disposed"); - - if (AssemblyLoadContext.GetLoadContext(typeof(MySession).Assembly) is not ICoreLoadContext coreContext) - throw new NotSupportedException("Mod loading is not supported in this context"); - - _currentSessionContext = new ModAssemblyLoadContext(coreContext); - } [HarmonyPatch(typeof(MySession), nameof(MySession.Unload))] [HarmonyPostfix] @@ -67,4 +54,27 @@ public static class ModAssemblyLoadContextPatches _currentSessionContext.Unload(); _currentSessionContext = null; } + + [HarmonyPatch] + private static class LoadPrefixes + { + [HarmonyTargetMethods] + private static IEnumerable TargetMethods() + { + yield return AccessTools.Method(typeof(MySession), nameof(MySession.Load)); + yield return AccessTools.Method(typeof(MySession), "LoadMultiplayer"); + } + + [HarmonyPrefix] + private static void Prefix() + { + if (_currentSessionContext is not null) + throw new InvalidOperationException("Previous session context was not disposed"); + + if (AssemblyLoadContext.GetLoadContext(typeof(MySession).Assembly) is not ICoreLoadContext coreContext) + throw new NotSupportedException("Mod loading is not supported in this context"); + + _currentSessionContext = new ModAssemblyLoadContext(coreContext); + } + } } \ No newline at end of file