diff --git a/LabOpt/Functions.cs b/LabOpt/Functions.cs index 7e866d3..4e8af34 100644 --- a/LabOpt/Functions.cs +++ b/LabOpt/Functions.cs @@ -19,7 +19,7 @@ public static class LabOptPatchFunctions do { RootLabIdField.SetValueDirect(__makeref(labPool[targetLabId]), rootId); - LabOptPatch.Logger.LogDebug($"Set rootLabId of lab {targetLabId} to {rootId}"); + // LabOptPatch.Logger.LogDebug($"Set rootLabId of lab {targetLabId} to {rootId}"); } while ((targetLabId = labPool[targetLabId].nextLabId) > 0); } @@ -61,7 +61,7 @@ public static class LabOptPatchFunctions var rootId = pair.Value; while (parentDict.TryGetValue(rootId, out var parentId)) rootId = parentId; RootLabIdField.SetValueDirect(__makeref(labPool[pair.Key]), rootId); - LabOptPatch.Logger.LogDebug($"Set rootLabId of lab {pair.Key} to {rootId}"); + // LabOptPatch.Logger.LogDebug($"Set rootLabId of lab {pair.Key} to {rootId}"); AssignRootLabValues(ref labPool[rootId], ref labPool[pair.Key]); } } @@ -350,7 +350,7 @@ public static class LabOptPatchFunctions public static void SetFunctionNew(ref LabComponent lab, bool researchMode, int recpId, int techId, SignData[] signPool, LabComponent[] labPool) { - LabOptPatch.Logger.LogDebug($"SetFunctionNew: {lab.id} {(int)RootLabIdField.GetValue(lab)} {researchMode} {recpId} {techId}"); + // LabOptPatch.Logger.LogDebug($"SetFunctionNew: {lab.id} {(int)RootLabIdField.GetValue(lab)} {researchMode} {recpId} {techId}"); lab.replicating = false; lab.time = 0; lab.hashBytes = 0; @@ -503,4 +503,39 @@ public static class LabOptPatchFunctions signPool[lab.entityId].iconId0 = (uint)lab.recipeId; signPool[lab.entityId].iconType = lab.recipeId == 0 ? 0U : 2U; } + + public static int InsertIntoLab(PlanetFactory factory, int labId, int itemId, byte itemCount, byte itemInc, ref byte remainInc, int[] needs) + { + var factorySystem = factory.factorySystem; + var served = factorySystem.labPool[labId].served; + var incServed = factorySystem.labPool[labId].incServed; + var matrixServed = factorySystem.labPool[labId].matrixServed; + var matrixIncServed = factorySystem.labPool[labId].matrixIncServed; + var len = needs.Length; + for (var i = 0; i < len; i++) + { + if (needs[i] != itemId) continue; + if (served != null) + { + lock (served) + { + served[i] += itemCount; + incServed[i] += itemInc; + } + remainInc = 0; + return itemCount; + } + if (matrixServed != null) + { + lock (matrixServed) + { + matrixServed[i] += 3600 * itemCount; + matrixIncServed[i] += 3600 * itemInc; + } + remainInc = 0; + return itemCount; + } + } + return 0; + } } diff --git a/LabOpt/LabOpt.cs b/LabOpt/LabOpt.cs index 68dd38d..c0e6a0d 100644 --- a/LabOpt/LabOpt.cs +++ b/LabOpt/LabOpt.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Reflection.Emit; +using System.Threading; using BepInEx; using HarmonyLib; @@ -267,5 +268,43 @@ public class LabOptPatch : BaseUnityPlugin ); return matcher.InstructionEnumeration(); } - + + // Change locks on PlanetFactory.InsertInto() + [HarmonyTranspiler] + [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.InsertInto))] + private static IEnumerable PlanetFactory_InsertInto_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + /* Remove following codes: + 465 0427 ldarg.0 + 466 0428 ldfld class Mutex[] PlanetFactory::entityMutexs + 467 042D ldarg.1 + 468 042E ldelem.ref + 469 042F stloc.s V_10 (10) + 470 0431 ldc.i4.0 + 471 0432 stloc.s V_11 (11) + 472 0434 ldloc.s V_10 (10) + 473 0436 ldloca.s V_11 (11) + 474 0438 call void [netstandard]System.Threading.Monitor::Enter(object, bool&) + */ + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(EntityData), nameof(EntityData.labId))) + ).Advance(1).MatchForward(false, + new CodeMatch(OpCodes.Ret) + ).Advance(1); + var labels = matcher.Instruction.labels; + matcher.InsertAndAdvance( + new CodeInstruction(OpCodes.Ldarg_0).WithLabels(labels), + new CodeInstruction(OpCodes.Ldloc_S, 5), + new CodeInstruction(OpCodes.Ldarg_3), + new CodeInstruction(OpCodes.Ldarg_S, 4), + new CodeInstruction(OpCodes.Ldarg_S, 5), + new CodeInstruction(OpCodes.Ldarg_S, 6), + new CodeInstruction(OpCodes.Ldloc_1), + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.InsertIntoLab))), + new CodeInstruction(OpCodes.Ret) + ); + matcher.Instruction.labels.Clear(); + return matcher.InstructionEnumeration(); + } } diff --git a/LabOpt/LabOpt.csproj b/LabOpt/LabOpt.csproj index cff1dc1..ea5ec4d 100644 --- a/LabOpt/LabOpt.csproj +++ b/LabOpt/LabOpt.csproj @@ -5,7 +5,7 @@ LabOpt org.soardev.labopt DSP MOD - LabOpt - 0.3.0 + 0.3.2 true latest diff --git a/LabOpt/package/manifest.json b/LabOpt/package/manifest.json index da89207..c89d504 100644 --- a/LabOpt/package/manifest.json +++ b/LabOpt/package/manifest.json @@ -1,6 +1,6 @@ { "name": "LabOpt", - "version_number": "0.3.0", + "version_number": "0.3.2", "website_url": "https://github.com/soarqin/DSP_Mods/tree/master/LabOpt", "description": "Optimize Lab performance / 优化研究站性能", "dependencies": [