From 15a30c85cdd59ef1998803a63694baf4d57501dc Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Thu, 27 Jul 2023 21:20:59 +0800 Subject: [PATCH] Finished test version of LabOpt --- LabOpt/LabOpt.cs | 412 ++++++++++++++++++++++++++--- LabOptPreloader/LabOptPreloader.cs | 7 +- 2 files changed, 383 insertions(+), 36 deletions(-) diff --git a/LabOpt/LabOpt.cs b/LabOpt/LabOpt.cs index af4704a..8e93cb4 100644 --- a/LabOpt/LabOpt.cs +++ b/LabOpt/LabOpt.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Reflection.Emit; using BepInEx; using HarmonyLib; +using UnityEngine.Yoga; namespace LabOpt; @@ -20,7 +22,7 @@ public class LabOptPatch : BaseUnityPlugin // Remove use of LabComponent.UpdateOutputToNext() for single-thread mode [HarmonyTranspiler] - [HarmonyPatch(typeof(PlanetFactory), "GameTick")] + [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.GameTick))] private static IEnumerable RemoveLabUpdateOutputToNextForSingleThread( IEnumerable instructions, ILGenerator generator) @@ -81,34 +83,42 @@ public class LabOptPatch : BaseUnityPlugin var label1 = generator.DefineLabel(); matcher.MatchForward(false, new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(EntityData), nameof(EntityData.labId)))) .MatchForward(false, new CodeMatch(OpCodes.Ret)) - .Advance(1) - .Insert( - // rootLabId = this.factorySystem.labPool[labId].rootLabId; - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlanetFactory), nameof(PlanetFactory.factorySystem))), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), - new CodeInstruction(OpCodes.Ldloc_S, 5), - new CodeInstruction(OpCodes.Ldelema, typeof(LabComponent)), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), "rootLabId")), - new CodeInstruction(OpCodes.Stloc_S, 6), - // if (rootLabId <= 0) goto label1; - new CodeInstruction(OpCodes.Ldloc_S, 6), - new CodeInstruction(OpCodes.Ldc_I4_0), - new CodeInstruction(OpCodes.Ble, label1), - // labId = rootLabId; - new CodeInstruction(OpCodes.Ldloc_S, 6), - new CodeInstruction(OpCodes.Stloc_S, 5), - // entityId = this.factorySystem.labPool[labId].entityId; - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlanetFactory), nameof(PlanetFactory.factorySystem))), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), - new CodeInstruction(OpCodes.Ldloc_S, 5), - new CodeInstruction(OpCodes.Ldelema, typeof(LabComponent)), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), nameof(LabComponent.entityId))), - new CodeInstruction(OpCodes.Starg_S, 1) - // lable1: - ); - matcher.Instruction.labels.Add(label1); + .Advance(1); + var labels = matcher.Instruction.labels; + matcher.InsertAndAdvance( + // rootLabId = this.factorySystem.labPool[labId].rootLabId; + new CodeInstruction(OpCodes.Ldarg_0).WithLabels(labels), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlanetFactory), nameof(PlanetFactory.factorySystem))), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), + new CodeInstruction(OpCodes.Ldloc_S, 5), + new CodeInstruction(OpCodes.Ldelema, typeof(LabComponent)), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), "rootLabId")), + new CodeInstruction(OpCodes.Stloc_S, 6), + // if (rootLabId <= 0) goto label1; + new CodeInstruction(OpCodes.Ldloc_S, 6), + new CodeInstruction(OpCodes.Ldc_I4_0), + new CodeInstruction(OpCodes.Ble, label1), + // labId = rootLabId; + new CodeInstruction(OpCodes.Ldloc_S, 6), + new CodeInstruction(OpCodes.Stloc_S, 5), + // entityId = this.factorySystem.labPool[labId].entityId; + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlanetFactory), nameof(PlanetFactory.factorySystem))), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), + new CodeInstruction(OpCodes.Ldloc_S, 5), + new CodeInstruction(OpCodes.Ldelema, typeof(LabComponent)), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(LabComponent), nameof(LabComponent.entityId))), + new CodeInstruction(OpCodes.Starg_S, 1), + new CodeInstruction(OpCodes.Ldarg_S, 1), + // array = this.entityNeeds[entityId]; + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlanetFactory), nameof(PlanetFactory.entityNeeds))), + new CodeInstruction(OpCodes.Ldarg_1), + new CodeInstruction(OpCodes.Ldelem_Ref), + new CodeInstruction(OpCodes.Stloc_1) + // lable1: + ); + matcher.Instruction.labels = new List