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

fixed rest issues

This commit is contained in:
2025-09-21 23:57:03 +08:00
parent 6b423225fe
commit 2a4008deac
3 changed files with 120 additions and 103 deletions

View File

@@ -1125,20 +1125,13 @@ public class FactoryPatch : PatchImpl<FactoryPatch>
DeepProfiler.EndSample(DPEntry.Belt);
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.LogicFrame))]
public static IEnumerable<CodeInstruction> GameLogic_LogicFrame_Transpiler(IEnumerable<CodeInstruction> instructions)
[HarmonyPostfix]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.OnFactoryFrameBegin))]
public static void GameLogic_OnFactoryFrameBegin_Postfix()
{
var matcher = new CodeMatcher(instructions);
matcher.MatchForward(false,
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(DeepProfiler), nameof(DeepProfiler.EndSample), [typeof(int), typeof(long)]))
).Advance(1).Insert(
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(BeltSignalGenerator), nameof(ProcessBeltSignals)))
);
return matcher.InstructionEnumeration();
ProcessBeltSignals();
}
/* BEGIN: Item sources calculation */
private static readonly Dictionary<int, ItemSource> ItemSources = [];
private static bool _itemSourcesInitialized;

View File

@@ -31,10 +31,10 @@ public static class ResourcePatch
private class InfiniteResource : PatchImpl<InfiniteResource>
{
static private readonly float InfiniteResourceRate = 0f;
[HarmonyTranspiler]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTick), typeof(long), typeof(bool))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTick), typeof(long), typeof(bool), typeof(int), typeof(int), typeof(int))]
[HarmonyPatch(typeof(ItemProto), nameof(ItemProto.GetPropValue))]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic._miner_parallel))]
[HarmonyPatch(typeof(PlanetTransport), nameof(PlanetTransport.GameTick))]
[HarmonyPatch(typeof(UIChartAstroResource), nameof(UIChartAstroResource.CalculateMaxAmount))]
[HarmonyPatch(typeof(UIChartVeinGroup), nameof(UIChartVeinGroup.CalculateMaxAmount))]
@@ -42,18 +42,29 @@ public static class ResourcePatch
[HarmonyPatch(typeof(UIControlPanelVeinCollectorPanel), nameof(UIControlPanelVeinCollectorPanel._OnUpdate))]
[HarmonyPatch(typeof(UIMinerWindow), nameof(UIMinerWindow._OnUpdate))]
[HarmonyPatch(typeof(UIMiningUpgradeLabel), nameof(UIMiningUpgradeLabel.Update))]
[HarmonyPatch(typeof(UIStationStorage), nameof(UIStationStorage.RefreshValues))]
[HarmonyPatch(typeof(UIVeinCollectorPanel), nameof(UIVeinCollectorPanel._OnUpdate))]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
var matcher = new CodeMatcher(instructions, generator);
matcher.MatchForward(false,
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(GameHistoryData), nameof(GameHistoryData.miningCostRate)))
new CodeMatch(ci => (ci.opcode == OpCodes.Ldfld || ci.opcode == OpCodes.Ldflda) && ci.OperandIs(AccessTools.Field(typeof(GameHistoryData), nameof(GameHistoryData.miningCostRate))))
).Repeat(codeMatcher =>
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldc_R4, 0f)
)
{
if (codeMatcher.Instruction.opcode == OpCodes.Ldfld)
{
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldc_R4, InfiniteResourceRate)
);
}
else
{
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldsflda, AccessTools.Field(typeof(InfiniteResource), nameof(InfiniteResourceRate)))
);
}
}
);
return matcher.InstructionEnumeration();
}
@@ -61,15 +72,25 @@ public static class ResourcePatch
private class FastMining : PatchImpl<FastMining>
{
static private readonly float FastMiningSpeed = 2400f;
[HarmonyTranspiler]
[HarmonyPatch(typeof(AstroResourceStatPlan), nameof(AstroResourceStatPlan.AddPlanetResources))]
[HarmonyPatch(typeof(BuildingGizmo), nameof(BuildingGizmo.Update))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTick), typeof(long), typeof(bool))]
[HarmonyPatch(typeof(FactorySystem), nameof(FactorySystem.GameTick), typeof(long), typeof(bool), typeof(int), typeof(int), typeof(int))]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic._miner_parallel))]
[HarmonyPatch(typeof(ItemProto), nameof(ItemProto.GetPropValue))]
[HarmonyPatch(typeof(PlanetTransport), nameof(PlanetTransport.GameTick))]
[HarmonyPatch(typeof(ProductionExtraInfoCalculator), nameof(ProductionExtraInfoCalculator.CalculateFactory))]
[HarmonyPatch(typeof(UIChartAstroResource), nameof(UIChartAstroResource.CalculateMaxAmount))]
[HarmonyPatch(typeof(UIChartVeinGroup), nameof(UIChartVeinGroup.CalculateMaxAmount))]
[HarmonyPatch(typeof(UIControlPanelStationStorage), nameof(UIControlPanelStationStorage.RefreshValues))]
[HarmonyPatch(typeof(UIControlPanelVeinCollectorPanel), nameof(UIControlPanelVeinCollectorPanel._OnUpdate))]
[HarmonyPatch(typeof(UIMinerWindow), nameof(UIMinerWindow._OnUpdate))]
[HarmonyPatch(typeof(UIMiningUpgradeLabel), nameof(UIMiningUpgradeLabel.Update))]
[HarmonyPatch(typeof(UIPlanetDetail), nameof(UIPlanetDetail.OnPlanetDataSet))]
[HarmonyPatch(typeof(UIPlanetDetail), nameof(UIPlanetDetail.RefreshDynamicProperties))]
[HarmonyPatch(typeof(UIReferenceSpeedTip), nameof(UIReferenceSpeedTip.AddEntryDataWithFactory))]
[HarmonyPatch(typeof(UIStarDetail), nameof(UIStarDetail.OnStarDataSet))]
[HarmonyPatch(typeof(UIStarDetail), nameof(UIStarDetail.RefreshDynamicProperties))]
[HarmonyPatch(typeof(UIStationStorage), nameof(UIStationStorage.RefreshValues))]
@@ -78,12 +99,24 @@ public static class ResourcePatch
{
var matcher = new CodeMatcher(instructions, generator);
matcher.MatchForward(false,
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(GameHistoryData), nameof(GameHistoryData.miningSpeedScale)))
new CodeMatch(ci => (ci.opcode == OpCodes.Ldfld || ci.opcode == OpCodes.Ldflda) && ci.OperandIs(AccessTools.Field(typeof(GameHistoryData), nameof(GameHistoryData.miningSpeedScale))))
).Repeat(codeMatcher =>
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldc_R4, 2400f)
)
{
if (codeMatcher.Instruction.opcode == OpCodes.Ldfld)
{
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldc_R4, FastMiningSpeed)
);
}
else
{
codeMatcher.RemoveInstruction().InsertAndAdvance(
new CodeInstruction(OpCodes.Pop),
new CodeInstruction(OpCodes.Ldsflda, AccessTools.Field(typeof(FastMining), nameof(FastMiningSpeed)))
);
}
}
);
return matcher.InstructionEnumeration();
}

View File

@@ -1832,91 +1832,82 @@ public class FactoryPatch : PatchImpl<FactoryPatch>
}
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.LogicFrame))]
public static IEnumerable<CodeInstruction> GameLogic_LogicFrame_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
[HarmonyPostfix]
[HarmonyPatch(typeof(GameLogic), nameof(GameLogic.OnFactoryFrameBegin))]
public static void GameLogic_OnFactoryFrameBegin_Postfix()
{
var matcher = new CodeMatcher(instructions, generator);
matcher.MatchForward(false,
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(DeepProfiler), nameof(DeepProfiler.EndSample), [typeof(int), typeof(long)]))
).Advance(1).Insert(
Transpilers.EmitDelegate(() =>
var factories = GameMain.data?.factories;
if (factories == null) return;
var factoriesCount = factories.Length;
var propertySystem = DSPGame.propertySystem;
List<int> factoriesToRemove = null;
foreach (var factoryIndex in SignalBeltFactoryIndices)
{
if (factoryIndex >= factoriesCount)
{
var factories = GameMain.data?.factories;
if (factories == null) return;
var factoriesCount = factories.Length;
var propertySystem = DSPGame.propertySystem;
List<int> factoriesToRemove = null;
foreach (var factoryIndex in SignalBeltFactoryIndices)
if (factoriesToRemove == null)
factoriesToRemove = [factoryIndex];
else
factoriesToRemove.Add(factoryIndex);
continue;
}
var signalBelts = GetSignalBelts(factoryIndex);
if (signalBelts == null) continue;
var factory = factories[factoryIndex];
if (factory == null) continue;
var cargoTraffic = factory.cargoTraffic;
var beltCount = cargoTraffic.beltCursor;
List<int> beltsToRemove = null;
foreach (var kvp in signalBelts)
{
if (kvp.Key >= beltCount)
{
if (factoryIndex >= factoriesCount)
{
if (factoriesToRemove == null)
factoriesToRemove = [factoryIndex];
else
factoriesToRemove.Add(factoryIndex);
continue;
}
var signalBelts = GetSignalBelts(factoryIndex);
if (signalBelts == null) continue;
var factory = factories[factoryIndex];
if (factory == null) continue;
var cargoTraffic = factory.cargoTraffic;
var beltCount = cargoTraffic.beltCursor;
List<int> beltsToRemove = null;
foreach (var kvp in signalBelts)
{
if (kvp.Key >= beltCount)
{
if (beltsToRemove == null)
beltsToRemove = [kvp.Key];
else
beltsToRemove.Add(kvp.Key);
continue;
}
ref var belt = ref cargoTraffic.beltPool[kvp.Key];
var cargoPath = cargoTraffic.GetCargoPath(belt.segPathId);
var itemIdx = kvp.Value;
if (cargoPath == null) continue;
var itemId = DarkFogItemIds[itemIdx];
var consume = (byte)Math.Min(DarkFogItemsInVoid[itemIdx], 4);
if (consume < 4)
{
var metaverse = propertySystem.GetItemAvaliableProperty(_clusterSeedKey, 6006);
if (metaverse > 0)
{
if (metaverse > 10)
metaverse = 10;
propertySystem.AddItemConsumption(_clusterSeedKey, 6006, metaverse);
var mainPlayer = GameMain.mainPlayer;
GameMain.history.AddPropertyItemConsumption(6006, metaverse, true);
var count = DarkFogItemExchangeRate[itemIdx] * metaverse;
DarkFogItemsInVoid[itemIdx] += count;
consume = (byte)Math.Min(DarkFogItemsInVoid[itemIdx], 4);
mainPlayer.mecha.AddProductionStat(itemId, count, mainPlayer.nearestFactory);
}
}
if (consume > 0 && cargoPath.TryInsertItem(belt.segIndex + belt.segPivotOffset, itemId, consume, 0))
DarkFogItemsInVoid[itemIdx] -= consume;
}
if (beltsToRemove == null) continue;
foreach (var beltId in beltsToRemove)
signalBelts.Remove(beltId);
if (signalBelts.Count > 0) continue;
if (factoriesToRemove == null)
factoriesToRemove = [factoryIndex];
if (beltsToRemove == null)
beltsToRemove = [kvp.Key];
else
factoriesToRemove.Add(factoryIndex);
beltsToRemove.Add(kvp.Key);
continue;
}
if (factoriesToRemove == null) return;
foreach (var factoryIndex in factoriesToRemove)
ref var belt = ref cargoTraffic.beltPool[kvp.Key];
var cargoPath = cargoTraffic.GetCargoPath(belt.segPathId);
var itemIdx = kvp.Value;
if (cargoPath == null) continue;
var itemId = DarkFogItemIds[itemIdx];
var consume = (byte)Math.Min(DarkFogItemsInVoid[itemIdx], 4);
if (consume < 4)
{
RemovePlanetSignalBelts(factoryIndex);
var metaverse = propertySystem.GetItemAvaliableProperty(_clusterSeedKey, 6006);
if (metaverse > 0)
{
if (metaverse > 10)
metaverse = 10;
propertySystem.AddItemConsumption(_clusterSeedKey, 6006, metaverse);
var mainPlayer = GameMain.mainPlayer;
GameMain.history.AddPropertyItemConsumption(6006, metaverse, true);
var count = DarkFogItemExchangeRate[itemIdx] * metaverse;
DarkFogItemsInVoid[itemIdx] += count;
consume = (byte)Math.Min(DarkFogItemsInVoid[itemIdx], 4);
mainPlayer.mecha.AddProductionStat(itemId, count, mainPlayer.nearestFactory);
}
}
})
);
return matcher.InstructionEnumeration();
if (consume > 0 && cargoPath.TryInsertItem(belt.segIndex + belt.segPivotOffset, itemId, consume, 0))
DarkFogItemsInVoid[itemIdx] -= consume;
}
if (beltsToRemove == null) continue;
foreach (var beltId in beltsToRemove)
signalBelts.Remove(beltId);
if (signalBelts.Count > 0) continue;
if (factoriesToRemove == null)
factoriesToRemove = [factoryIndex];
else
factoriesToRemove.Add(factoryIndex);
}
if (factoriesToRemove == null) return;
foreach (var factoryIndex in factoriesToRemove)
{
RemovePlanetSignalBelts(factoryIndex);
}
}
}