1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-02-05 03:02:20 +08:00

improve performance of LabOpt by replacing use of FieldInfo with self CIL-patches

This commit is contained in:
2023-09-12 02:59:56 +08:00
parent 6361ab21c6
commit e12d20980a
2 changed files with 40 additions and 14 deletions

View File

@@ -332,4 +332,35 @@ public class LabOptPatch : BaseUnityPlugin
{
LabOptPatchFunctions.SetRootId(ref __instance, 0);
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.SetRootId))]
[HarmonyPatch(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.SetRootLabIdForStacking))]
[HarmonyPatch(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.SetRootLabIdOnLoading))]
[HarmonyPatch(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.SetFunctionManually))]
[HarmonyPatch(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.SetFunctionInternal))]
private static IEnumerable<CodeInstruction> LabOptPatchFunctions_PatchRootId_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
var matcher = new CodeMatcher(instructions, generator);
matcher.Start().MatchForward(false,
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), nameof(LabComponent.pcId)))
);
if (matcher.IsValid)
{
matcher.Repeat(codeMatcher => codeMatcher.SetInstructionAndAdvance(
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), "rootLabId"))
));
}
matcher.Start().MatchForward(false,
new CodeMatch(OpCodes.Stfld, AccessTools.Field(typeof(LabComponent), nameof(LabComponent.pcId)))
);
if (matcher.IsValid)
{
matcher.Repeat(codeMatcher => codeMatcher.SetInstructionAndAdvance(
new CodeInstruction(OpCodes.Stfld, AccessTools.Field(typeof(LabComponent), "rootLabId"))
));
}
return matcher.InstructionEnumeration();
}
}