1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 04:13:32 +08:00

Work in progress

This commit is contained in:
2023-07-28 03:24:12 +08:00
parent 15a30c85cd
commit 7813532e3f

View File

@@ -1,11 +1,9 @@
using System.Collections.Generic; using System;
using System.IO; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using BepInEx; using BepInEx;
using HarmonyLib; using HarmonyLib;
using UnityEngine.Yoga;
namespace LabOpt; namespace LabOpt;
@@ -199,6 +197,7 @@ public class LabOptPatch : BaseUnityPlugin
} }
// Add a parameter on calling LabComponent.InternalUpdateAssemble() // Add a parameter on calling LabComponent.InternalUpdateAssemble()
// Remove call to LabComponent.InternalUpdateAssemble()
[HarmonyTranspiler] [HarmonyTranspiler]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabProduceMode), typeof(long), typeof(bool))] [HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabProduceMode), typeof(long), typeof(bool))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabProduceMode), typeof(long), typeof(bool), typeof(int), typeof(int), typeof(int))] [HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabProduceMode), typeof(long), typeof(bool), typeof(int), typeof(int), typeof(int))]
@@ -212,10 +211,14 @@ public class LabOptPatch : BaseUnityPlugin
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.InternalUpdateAssembleNew))) new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.InternalUpdateAssembleNew)))
); );
matcher.Start().MatchForward(false,
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(LabComponent), nameof(LabComponent.UpdateNeedsAssemble)))
).Advance(-4).RemoveInstructions(5);
return matcher.InstructionEnumeration(); return matcher.InstructionEnumeration();
} }
// Add a parameter on calling LabComponent.InternalUpdateResearch() // Add a parameter on calling LabComponent.InternalUpdateResearch()
// Remove call to LabComponent.InternalUpdateResearch()
[HarmonyTranspiler] [HarmonyTranspiler]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabResearchMode), typeof(long), typeof(bool))] [HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTickLabResearchMode), typeof(long), typeof(bool))]
private static IEnumerable<CodeInstruction> FactorySystem_GameTickLabResearchMode_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) private static IEnumerable<CodeInstruction> FactorySystem_GameTickLabResearchMode_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
@@ -228,6 +231,9 @@ public class LabOptPatch : BaseUnityPlugin
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))), new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(FactorySystem), nameof(FactorySystem.labPool))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.InternalUpdateResearchNew))) new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(LabOptPatchFunctions), nameof(LabOptPatchFunctions.InternalUpdateResearchNew)))
); );
matcher.Start().MatchForward(false,
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(LabComponent), nameof(LabComponent.UpdateNeedsResearch)))
).Advance(-4).RemoveInstructions(5);
return matcher.InstructionEnumeration(); return matcher.InstructionEnumeration();
} }
@@ -349,6 +355,28 @@ public class LabOptPatchFunctions
{ {
if (labPool[id].id != id) continue; if (labPool[id].id != id) continue;
ref var lab = ref labPool[id]; ref var lab = ref labPool[id];
if (lab.researchMode)
{
var len = lab.matrixIncServed.Length;
for (var i = 0; i < len; i++)
{
if (lab.matrixIncServed[i] < 0)
{
lab.matrixIncServed[i] = 0;
}
}
}
else
{
var len = lab.incServed.Length;
for (var i = 0; i < len; i++)
{
if (lab.incServed[i] < 0)
{
lab.incServed[i] = 0;
}
}
}
if (lab.nextLabId != 0) parentDict[lab.nextLabId] = id; if (lab.nextLabId != 0) parentDict[lab.nextLabId] = id;
} }
@@ -363,43 +391,43 @@ public class LabOptPatchFunctions
int len; int len;
if (rootLab.researchMode) if (rootLab.researchMode)
{ {
len = rootLab.matrixServed.Length; len = Math.Min(rootLab.matrixServed.Length, thisLab.matrixServed.Length);
for (var i = 0; i < len; i++) for (var i = 0; i < len; i++)
{ {
if (thisLab.matrixServed[i] == 0) continue; if (thisLab.matrixServed[i] != 0)
rootLab.matrixServed[i] += thisLab.matrixServed[i]; {
thisLab.matrixServed[i] = 0; rootLab.matrixServed[i] += thisLab.matrixServed[i];
} thisLab.matrixServed[i] = 0;
len = rootLab.matrixIncServed.Length; }
for (var i = 0; i < len; i++) if (thisLab.matrixIncServed[i] != 0)
{ {
if (thisLab.matrixIncServed[i] == 0) continue; rootLab.matrixIncServed[i] += thisLab.matrixIncServed[i];
rootLab.matrixIncServed[i] += thisLab.matrixIncServed[i]; thisLab.matrixIncServed[i] = 0;
thisLab.matrixIncServed[i] = 0; }
} }
} }
else else
{ {
len = rootLab.produced.Length; len = Math.Min(rootLab.produced.Length, thisLab.produced.Length);
for (var i = 0; i < len; i++) for (var i = 0; i < len; i++)
{ {
if (thisLab.produced[i] == 0) continue; if (thisLab.produced[i] == 0) continue;
rootLab.produced[i] += thisLab.produced[i]; rootLab.produced[i] += thisLab.produced[i];
thisLab.produced[i] = 0; thisLab.produced[i] = 0;
} }
len = rootLab.served.Length; len = Math.Min(rootLab.served.Length, thisLab.served.Length);
for (var i = 0; i < len; i++) for (var i = 0; i < len; i++)
{ {
if (thisLab.served[i] == 0) continue; if (thisLab.served[i] != 0)
rootLab.served[i] += thisLab.served[i]; {
thisLab.served[i] = 0; rootLab.served[i] += thisLab.served[i];
} thisLab.served[i] = 0;
len = rootLab.incServed.Length; }
for (var i = 0; i < len; i++) if (thisLab.incServed[i] != 0)
{ {
if (thisLab.incServed[i] == 0) continue; rootLab.incServed[i] += thisLab.incServed[i];
rootLab.incServed[i] += thisLab.incServed[i]; thisLab.incServed[i] = 0;
thisLab.incServed[i] = 0; }
} }
} }
@@ -422,24 +450,26 @@ public class LabOptPatchFunctions
ref var rootLab = ref rootLabId > 0 ? ref labPool[rootLabId] : ref lab; ref var rootLab = ref rootLabId > 0 ? ref labPool[rootLabId] : ref lab;
if (extraPassed) if (extraPassed)
{ {
int num = lab.products.Length; int len = lab.products.Length;
if (num == 1) lock (rootLab.produced)
{ {
rootLab.produced[0] += lab.productCounts[0]; if (len == 1)
int[] array = productRegister;
lock (array)
{ {
productRegister[lab.products[0]] += lab.productCounts[0]; rootLab.produced[0] += lab.productCounts[0];
}
}
else
{
for (int i = 0; i < num; i++)
{
rootLab.produced[i] += lab.productCounts[i];
lock (productRegister) lock (productRegister)
{ {
productRegister[lab.products[i]] += lab.productCounts[i]; productRegister[lab.products[0]] += lab.productCounts[0];
}
}
else
{
for (int i = 0; i < len; i++)
{
rootLab.produced[i] += lab.productCounts[i];
lock (productRegister)
{
productRegister[lab.products[i]] += lab.productCounts[i];
}
} }
} }
} }
@@ -450,37 +480,39 @@ public class LabOptPatchFunctions
if (timePassed) if (timePassed)
{ {
lab.replicating = false; lab.replicating = false;
int num2 = lab.products.Length; int len = lab.products.Length;
if (num2 == 1) lock (rootLab.produced)
{ {
if (rootLab.produced[0] + lab.productCounts[0] > 10) if (len == 1)
{ {
return 0U; if (rootLab.produced[0] + lab.productCounts[0] > 30)
}
rootLab.produced[0] += lab.productCounts[0];
int[] array = productRegister;
lock (array)
{
productRegister[lab.products[0]] += lab.productCounts[0];
}
}
else
{
for (int j = 0; j < num2; j++)
{
if (rootLab.produced[j] + lab.productCounts[j] > 50)
{ {
return 0U; return 0U;
} }
}
for (int k = 0; k < num2; k++) rootLab.produced[0] += lab.productCounts[0];
{
rootLab.produced[k] += lab.productCounts[k];
lock (productRegister) lock (productRegister)
{ {
productRegister[lab.products[k]] += lab.productCounts[k]; productRegister[lab.products[0]] += lab.productCounts[0];
}
}
else
{
for (int j = 0; j < len; j++)
{
if (rootLab.produced[j] + lab.productCounts[j] > 30)
{
return 0U;
}
}
for (int k = 0; k < len; k++)
{
rootLab.produced[k] += lab.productCounts[k];
lock (productRegister)
{
productRegister[lab.products[k]] += lab.productCounts[k];
}
} }
} }
} }
@@ -493,54 +525,60 @@ public class LabOptPatchFunctions
if (!lab.replicating) if (!lab.replicating)
{ {
int num3 = lab.requireCounts.Length; int len = lab.requireCounts.Length;
for (int l = 0; l < num3; l++) int incLevel;
if (len > 0)
{ {
if (rootLab.incServed[l] <= 0) var served = rootLab.served;
lock (served)
{ {
rootLab.incServed[l] = 0; for (int l = 0; l < len; l++)
} {
if (served[l] < lab.requireCounts[l] || served[l] == 0)
{
lab.time = 0;
return 0U;
}
}
if (rootLab.served[l] < lab.requireCounts[l] || rootLab.served[l] == 0) incLevel = 10;
{ for (int m = 0; m < len; m++)
lab.time = 0; {
return 0U; int splittedIncLevel = lab.split_inc_level(ref served[m], ref rootLab.incServed[m], lab.requireCounts[m]);
if (splittedIncLevel < incLevel) incLevel = splittedIncLevel;
if (served[m] == 0)
{
rootLab.incServed[m] = 0;
}
rootLab.needs[m] = served[m] < 6 ? rootLab.requires[0] : 0;
lock (consumeRegister)
{
consumeRegister[lab.requires[m]] += lab.requireCounts[m];
}
}
if (incLevel < 0)
{
incLevel = 0;
}
} }
} }
else
int num4 = ((num3 > 0) ? 10 : 0);
for (int m = 0; m < num3; m++)
{ {
int num5 = lab.split_inc_level(ref rootLab.served[m], ref rootLab.incServed[m], lab.requireCounts[m]); incLevel = 0;
num4 = ((num4 < num5) ? num4 : num5);
if (rootLab.served[m] == 0)
{
rootLab.incServed[m] = 0;
}
int[] array = consumeRegister;
lock (array)
{
consumeRegister[lab.requires[m]] += lab.requireCounts[m];
}
}
if (num4 < 0)
{
num4 = 0;
} }
if (lab.productive && !lab.forceAccMode) if (lab.productive && !lab.forceAccMode)
{ {
lab.extraSpeed = (int)((double)lab.speed * Cargo.incTableMilli[num4] * 10.0 + 0.1); lab.extraSpeed = (int)((double)lab.speed * Cargo.incTableMilli[incLevel] * 10.0 + 0.1);
lab.speedOverride = lab.speed; lab.speedOverride = lab.speed;
lab.extraPowerRatio = Cargo.powerTable[num4]; lab.extraPowerRatio = Cargo.powerTable[incLevel];
} }
else else
{ {
lab.extraSpeed = 0; lab.extraSpeed = 0;
lab.speedOverride = (int)((double)lab.speed * (1.0 + Cargo.accTableMilli[num4]) + 0.1); lab.speedOverride = (int)((double)lab.speed * (1.0 + Cargo.accTableMilli[incLevel]) + 0.1);
lab.extraPowerRatio = Cargo.powerTable[num4]; lab.extraPowerRatio = Cargo.powerTable[incLevel];
} }
lab.replicating = true; lab.replicating = true;
@@ -570,15 +608,16 @@ public class LabOptPatchFunctions
var rootLabId = (int)RootLabIdField.GetValue(lab); var rootLabId = (int)RootLabIdField.GetValue(lab);
ref var rootLab = ref rootLabId > 0 ? ref labPool[rootLabId] : ref lab; ref var rootLab = ref rootLabId > 0 ? ref labPool[rootLabId] : ref lab;
int num = (int)(speed + 2f); int multiplier = (int)(speed + 2f);
var matrixServed = rootLab.matrixServed;
for (var i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
{ {
if (lab.matrixPoints[i] <= 0) continue; if (lab.matrixPoints[i] <= 0) continue;
int num2 = rootLab.matrixServed[i] / lab.matrixPoints[i]; int mult = matrixServed[i] / lab.matrixPoints[i];
if (num2 < num) if (mult < multiplier)
{ {
num = num2; multiplier = mult;
if (num == 0) if (multiplier == 0)
{ {
lab.replicating = false; lab.replicating = false;
return 0U; return 0U;
@@ -587,52 +626,49 @@ public class LabOptPatchFunctions
} }
lab.replicating = true; lab.replicating = true;
speed = ((speed < (float)num) ? speed : ((float)num)); if (multiplier < speed) speed = multiplier;
int num3 = (int)(power * 10000f * speed + 0.5f); int hashBytes = (int)(power * 10000f * speed + 0.5f);
lab.hashBytes += num3; lab.hashBytes += hashBytes;
long num4 = (long)(lab.hashBytes / 10000); long count = lab.hashBytes / 10000;
lab.hashBytes -= (int)num4 * 10000; lab.hashBytes -= (int)count * 10000;
long num5 = ts.hashNeeded - ts.hashUploaded; long maxNeeded = ts.hashNeeded - ts.hashUploaded;
num4 = ((num4 < num5) ? num4 : num5); if (maxNeeded < count) count = maxNeeded;
num4 = ((num4 < (long)num) ? num4 : ((long)num)); if (multiplier < count) count = multiplier;
int num6 = (int)num4; int icount = (int)count;
if (num6 > 0) if (icount > 0)
{ {
int num7 = rootLab.matrixServed.Length; int len = matrixServed.Length;
int num8 = ((num7 == 0) ? 0 : 10); int incLevel = ((len == 0) ? 0 : 10);
for (int i = 0; i < num7; i++) for (int i = 0; i < len; i++)
{ {
if (lab.matrixPoints[i] > 0) if (lab.matrixPoints[i] <= 0) continue;
int matrixBefore = matrixServed[i];
int splittedIncLevel = lab.split_inc_level(ref matrixServed[i], ref rootLab.matrixIncServed[i], lab.matrixPoints[i] * icount);
incLevel = incLevel < splittedIncLevel ? incLevel : splittedIncLevel;
if (matrixServed[i] <= 0)
{ {
int num9 = rootLab.matrixServed[i] / 3600; rootLab.matrixIncServed[i] = 0;
int num10 = lab.split_inc_level(ref rootLab.matrixServed[i], ref rootLab.matrixIncServed[i], lab.matrixPoints[i] * num6);
num8 = ((num8 < num10) ? num8 : num10);
int num11 = rootLab.matrixServed[i] / 3600;
if (rootLab.matrixServed[i] <= 0 || rootLab.matrixIncServed[i] < 0)
{
rootLab.matrixIncServed[i] = 0;
}
consumeRegister[LabComponent.matrixIds[i]] += num9 - num11;
} }
rootLab.needs[i] = matrixServed[i] < 54000 ? 6001 + i : 0;
consumeRegister[LabComponent.matrixIds[i]] += (matrixBefore - rootLab.matrixIncServed[i]) / 3600;
} }
if (num8 < 0) if (incLevel < 0)
{ {
num8 = 0; incLevel = 0;
} }
lab.extraSpeed = (int)(10000.0 * Cargo.incTableMilli[num8] * 10.0 + 0.1); lab.extraSpeed = (int)(10000.0 * Cargo.incTableMilli[incLevel] * 10.0 + 0.1);
lab.extraPowerRatio = Cargo.powerTable[num8]; lab.extraPowerRatio = Cargo.powerTable[incLevel];
lab.extraHashBytes += (int)(power * (float)lab.extraSpeed * speed + 0.5f); lab.extraHashBytes += (int)(power * lab.extraSpeed * speed + 0.5f);
long num12 = (long)(lab.extraHashBytes / 100000); long extraCount = lab.extraHashBytes / 100000;
lab.extraHashBytes -= (int)num12 * 100000; lab.extraHashBytes -= (int)extraCount * 100000;
num12 = ((num12 < 0L) ? 0L : num12); if (extraCount < 0L) extraCount = 0L;
int num13 = (int)num12; int iextraCount = (int)extraCount;
ts.hashUploaded += num4 + num12; ts.hashUploaded += count + extraCount;
hashRegister += num4 + num12; hashRegister += count + extraCount;
uMatrixPoint += (long)ts.uPointPerHash * num4; uMatrixPoint += ts.uPointPerHash * count;
techHashedThisFrame += num6 + num13; techHashedThisFrame += icount + iextraCount;
if (ts.hashUploaded >= ts.hashNeeded) if (ts.hashUploaded >= ts.hashNeeded)
{ {
TechProto techProto = LDB.techs.Select(lab.techId); TechProto techProto = LDB.techs.Select(lab.techId);