From 82e0e2ac435c3f16f7222576913c793ea0282823 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sun, 6 Apr 2025 17:40:07 +0800 Subject: [PATCH] UXAssist: Optimize implementation for `ProtectVeinsFromExhaustion` --- UXAssist/Patches/FactoryPatch.cs | 82 ++++++++++++++------------------ 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/UXAssist/Patches/FactoryPatch.cs b/UXAssist/Patches/FactoryPatch.cs index 8514ccf..0dc317f 100644 --- a/UXAssist/Patches/FactoryPatch.cs +++ b/UXAssist/Patches/FactoryPatch.cs @@ -134,7 +134,7 @@ public class FactoryPatch : PatchImpl NightLight.Enable(false); RemoveSomeConditionBuild.Enable(false); UnlimitInteractive.Enable(false); - + BeltSignalsForBuyOut.UninitPersist(); } @@ -994,12 +994,13 @@ public class FactoryPatch : PatchImpl switch (__instance.type) { case EMinerType.Vein: - if (__instance.veinCount <= 0) + var veinCount = __instance.veinCount; + if (veinCount <= 0) break; if (__instance.time <= __instance.period) { - __instance.time += (int)(power * __instance.speedDamper * __instance.speed * miningSpeed * __instance.veinCount); + __instance.time += (int)(power * __instance.speedDamper * __instance.speed * miningSpeed * veinCount); res = 1U; } @@ -1008,23 +1009,16 @@ public class FactoryPatch : PatchImpl break; } - veinId = __instance.veins[__instance.currentVeinIndex]; + var currentVeinIndex = __instance.currentVeinIndex; + veinId = __instance.veins[currentVeinIndex]; lock (veinPool) { if (veinPool[veinId].id == 0) { - __instance.RemoveVeinFromArray(__instance.currentVeinIndex); + __instance.RemoveVeinFromArray(currentVeinIndex); __instance.GetMinimumVeinAmount(factory, veinPool); - if (__instance.veinCount > 1) - { - __instance.currentVeinIndex %= __instance.veinCount; - } - else - { - __instance.currentVeinIndex = 0; - } - - __instance.time += (int)(power * __instance.speedDamper * __instance.speed * miningSpeed * __instance.veinCount); + veinCount = __instance.veinCount; + __instance.currentVeinIndex = veinCount > 1 ? currentVeinIndex % veinCount : 0; __result = 0U; return false; } @@ -1035,9 +1029,9 @@ public class FactoryPatch : PatchImpl times = __instance.time / __instance.period; var outputCount = 0; var amount = veinPool[veinId].amount; - if (amount > KeepVeinAmount) + if (miningRate > 0f) { - if (miningRate > 0f) + if (amount > KeepVeinAmount) { var usedCount = 0; var maxAllowed = amount - KeepVeinAmount; @@ -1078,45 +1072,43 @@ public class FactoryPatch : PatchImpl factory.RemoveVeinWithComponents(veinId); factory.RecalculateVeinGroup(groupIndex); factory.NotifyVeinExhausted(venType, pos); + veinCount = __instance.veinCount; } else { - __instance.currentVeinIndex++; + currentVeinIndex++; } } } else { - outputCount = times; + if (amount <= 0) + { + __instance.RemoveVeinFromArray(currentVeinIndex); + __instance.GetMinimumVeinAmount(factory, veinPool); + veinCount = __instance.veinCount; + } + else + { + currentVeinIndex++; + } + __instance.currentVeinIndex = veinCount > 1 ? currentVeinIndex % veinCount : 0; + break; } - - __instance.productCount += outputCount; - lock (productRegister) - { - productRegister[__instance.productId] += outputCount; - factory.AddMiningFlagUnsafe(veinPool[veinId].type); - factory.AddVeinMiningFlagUnsafe(veinPool[veinId].type); - } - } - else if (amount <= 0) - { - __instance.RemoveVeinFromArray(__instance.currentVeinIndex); - __instance.GetMinimumVeinAmount(factory, veinPool); } else { - __instance.currentVeinIndex++; + outputCount = times; + } + __instance.productCount += outputCount; + lock (productRegister) + { + productRegister[__instance.productId] += outputCount; + factory.AddMiningFlagUnsafe(veinPool[veinId].type); + factory.AddVeinMiningFlagUnsafe(veinPool[veinId].type); } - __instance.time -= __instance.period * outputCount; - if (__instance.veinCount > 1) - { - __instance.currentVeinIndex %= __instance.veinCount; - } - else - { - __instance.currentVeinIndex = 0; - } + __instance.currentVeinIndex = veinCount > 1 ? currentVeinIndex % veinCount : 0; } } @@ -1857,7 +1849,7 @@ public class FactoryPatch : PatchImpl new(OpCodes.Call, AccessTools.Method(typeof(Math), nameof(Math.Min), [typeof(int), typeof(int)])) ]; private static readonly CodeInstruction GetRealCount = new(OpCodes.Ldsfld, AccessTools.Field(typeof(FactoryPatch), nameof(_tankFastFillInAndTakeOutMultiplierRealValue))); - + [HarmonyTranspiler] [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.EntityFastFillIn))] private static IEnumerable PlanetFactory_EntityFastFillIn_Transpiler(IEnumerable instructions, ILGenerator generator) @@ -1877,7 +1869,7 @@ public class FactoryPatch : PatchImpl ).RemoveInstructions(5).Insert(MultiplierWithCountCheck); return matcher.InstructionEnumeration(); } - + [HarmonyTranspiler] [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.EntityFastTakeOut))] private static IEnumerable PlanetFactory_EntityFastTakeOut_Transpiler(IEnumerable instructions, ILGenerator generator) @@ -1898,7 +1890,7 @@ public class FactoryPatch : PatchImpl ).RemoveInstructions(5).Insert(MultiplierWithCountCheck); return matcher.InstructionEnumeration(); } - + [HarmonyTranspiler] [HarmonyPatch(typeof(UITankWindow), nameof(UITankWindow._OnUpdate))] private static IEnumerable UITankWindow__OnUpdate_Transpiler(IEnumerable instructions, ILGenerator generator)