fix compiler injection patch matching
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 1m0s
Build / Build Nuget package (NuGet) (push) Successful in 1m4s
Build / Build Nuget package (SharedCringe) (push) Successful in 1m2s
Build / Build Nuget package (CringePlugins) (push) Successful in 1m15s
Build / Build Launcher (push) Successful in 1m45s

This commit is contained in:
zznty
2025-05-07 17:43:26 +07:00
parent a10d0adc2f
commit 2a1b648f02

View File

@@ -26,25 +26,24 @@ public static class ModAssemblyLoadContextPatches
var load1Method = AccessTools.DeclaredMethod(typeof(Assembly), nameof(Assembly.Load), [typeof(byte[]), typeof(byte[])]); var load1Method = AccessTools.DeclaredMethod(typeof(Assembly), nameof(Assembly.Load), [typeof(byte[]), typeof(byte[])]);
var load2Method = AccessTools.DeclaredMethod(typeof(Assembly), nameof(Assembly.Load), [typeof(byte[])]); var load2Method = AccessTools.DeclaredMethod(typeof(Assembly), nameof(Assembly.Load), [typeof(byte[])]);
matcher.SearchForward(i => i.Calls(load1Method)) return matcher.SearchForward(i => i.Calls(load1Method))
.InsertAndAdvance(new(OpCodes.Ldarg_0), CodeInstruction.LoadField(original.DeclaringType, "target")) .InsertAndAdvance(new(OpCodes.Ldarg_0), CodeInstruction.LoadField(original.DeclaringType, "target"))
.SetInstruction(CodeInstruction.CallClosure((byte[] assembly, byte[] symbols, MyApiTarget target) => .SetInstruction(CodeInstruction.CallClosure((byte[] assembly, byte[] symbols, MyApiTarget target) =>
{ {
if (target is not MyApiTarget.Mod) return Assembly.Load(assembly, symbols); if (target is not MyApiTarget.Mod) return Assembly.Load(assembly, symbols);
ArgumentNullException.ThrowIfNull(_currentSessionContext, "No session context"); ArgumentNullException.ThrowIfNull(_currentSessionContext, "No session context");
return _currentSessionContext.LoadFromStream(new MemoryStream(assembly), new MemoryStream(symbols)); return _currentSessionContext.LoadFromStream(new MemoryStream(assembly), new MemoryStream(symbols));
})); }))
.Start()
matcher.SearchForward(i => i.Calls(load2Method)) .SearchForward(i => i.Calls(load2Method))
.InsertAndAdvance(new(OpCodes.Ldarg_0), CodeInstruction.LoadField(original.DeclaringType, "target")) .InsertAndAdvance(new(OpCodes.Ldarg_0), CodeInstruction.LoadField(original.DeclaringType, "target"))
.SetInstruction(CodeInstruction.CallClosure((byte[] assembly, MyApiTarget target) => .SetInstruction(CodeInstruction.CallClosure((byte[] assembly, MyApiTarget target) =>
{ {
if (target is not MyApiTarget.Mod) return Assembly.Load(assembly); if (target is not MyApiTarget.Mod) return Assembly.Load(assembly);
ArgumentNullException.ThrowIfNull(_currentSessionContext, "No session context"); ArgumentNullException.ThrowIfNull(_currentSessionContext, "No session context");
return _currentSessionContext.LoadFromStream(new MemoryStream(assembly)); return _currentSessionContext.LoadFromStream(new MemoryStream(assembly));
})); }))
.Instructions();
return matcher.Instructions();
} }
[HarmonyPatch(typeof(MyScriptManager), "Compile")] [HarmonyPatch(typeof(MyScriptManager), "Compile")]