Fix loading into singleplayer worlds (Issue with multiple harmony patches on one method)

This commit is contained in:
zznty
2024-11-03 00:08:12 +07:00
parent cda99844b2
commit 271e8a1dde

View File

@@ -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<MethodInfo> 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);
}
}
}