1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-08 22:13:30 +08:00
This commit is contained in:
2023-08-31 02:11:56 +08:00
parent 884700d896
commit 55610e991b
2 changed files with 48 additions and 5 deletions

View File

@@ -214,8 +214,7 @@ public class LabOptPatch : BaseUnityPlugin
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.FindLabFunctionsForBuild))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabResearchMode))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.SyncLabFunctions))]
// no need to patch this function, it just set everything to empty
// [HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.TakeBackItems_Lab))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.TakeBackItems_Lab))]
private static IEnumerable<CodeInstruction> FactorySystem_ReplaceLabSetFunction_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
var matcher = new CodeMatcher(instructions, generator);

View File

@@ -1,4 +1,5 @@
using BepInEx;
using System;
using BepInEx;
using HarmonyLib;
namespace PoolOpt;
@@ -19,6 +20,48 @@ public class PoolOptPatch : BaseUnityPlugin
private static void GameSave_LoadCurrentGame_Postfix()
{
DebugOutput();
foreach (var planet in GameMain.data.factories)
{
if (planet == null) continue;
var factorySystem = planet.factorySystem;
if (factorySystem != null)
{
OptimizePool((in MinerComponent n) => n.id, 256,
ref factorySystem.minerPool, ref factorySystem.minerCursor, ref factorySystem.minerCapacity,
ref factorySystem.minerRecycle, ref factorySystem.minerRecycleCursor);
}
}
DebugOutput();
}
private delegate int GetId<T>(in T s) where T : struct;
private static bool OptimizePool<T>(GetId<T> getter, int initCapacity, ref T[] pool, ref int cursor, ref int capacity, ref int[] recycle, ref int recycleCursor) where T : struct
{
if (cursor <= 1) return false;
var pos = cursor;
while (pos > 0)
{
if (getter(pool[pos]) == pos) break;
pos--;
}
if (pos == cursor) return false;
if (pos == 0)
{
cursor = 1;
capacity = initCapacity;
pool = new T[initCapacity];
recycle = new int[initCapacity];
recycleCursor = 0;
return true;
}
cursor = pos + 1;
Array.Sort(recycle);
var idx = Array.BinarySearch(recycle, 0, recycleCursor, pos);
recycleCursor = idx < 0 ? ~idx : idx + 1;
return true;
}
private static void DebugOutput()
@@ -44,6 +87,7 @@ public class PoolOptPatch : BaseUnityPlugin
Logger.LogDebug($" Storage: Storage=[{factoryStorage.storageCursor},{factoryStorage.storageCapacity},{factoryStorage.storageRecycleCursor}]");
Logger.LogDebug($" Tank=[{factoryStorage.tankCursor},{factoryStorage.tankCapacity},{factoryStorage.tankRecycleCursor}]");
}
var factorySystem = planet.factorySystem;
if (factorySystem != null)
{