From 2ddf91ceabed303276b53d618a6811887641d1b0 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sun, 25 Oct 2026 15:25:05 +0800 Subject: [PATCH] fix(cheatenabler): plan belt-signal statistics outside the logic tick Signals can change inside the logic tick when drones finish a blueprint, so a cache miss now queues the belt and the production planner runs from the next frame update; the tick only applies cached rates. Document the recipe-based upstream statistics in the unreleased changelog. --- CheatEnabler/CHANGELOG.md | 2 ++ .../Patches/Factory/BeltSignalPatch.cs | 29 +++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CheatEnabler/CHANGELOG.md b/CheatEnabler/CHANGELOG.md index fc6d031..7706adf 100644 --- a/CheatEnabler/CHANGELOG.md +++ b/CheatEnabler/CHANGELOG.md @@ -4,6 +4,7 @@ ## Changlog * 2.4.8 - Unreleased + * `Belt signal item generation`: Calculate raw-material, intermediate, and proliferator statistics from the game's current recipe data, using each item's standard recipe and counting by-products and consumption separately. If an item's recipe data is incomplete, only its upstream statistics pause, with the reason logged; item generation is unaffected. * 2.4.7 * `Dev Shortcuts`: Work immediately when enabled after loading a save, and process each key press once per frame at accelerated game speeds. * Extend `Unlock Tech with Key-Modifiers` with a downgrade mode. @@ -203,6 +204,7 @@ ## 更新日志 * 2.4.8 - Unreleased + * `传送带信号物品生成`:原料、中间产物和增产剂的统计改为按游戏当前配方数据计算,采用各物品的标准配方,并分别计入副产物和消耗。某物品的配方数据不完整时,仅暂停该物品的上游统计并在日志中说明原因,不影响物品生成。 * 2.4.7 * `开发模式快捷键`:修复载入存档后启用时不生效的问题;加速游戏时每次按键仍只执行一次。 * `使用组合键点击解锁科技` 新增降级模式。 diff --git a/CheatEnabler/Patches/Factory/BeltSignalPatch.cs b/CheatEnabler/Patches/Factory/BeltSignalPatch.cs index 0d5211e..b28a923 100644 --- a/CheatEnabler/Patches/Factory/BeltSignalPatch.cs +++ b/CheatEnabler/Patches/Factory/BeltSignalPatch.cs @@ -23,6 +23,7 @@ internal class BeltSignalGenerator : PatchImpl private static readonly Dictionary<(int itemId, bool proliferated, bool sprayed), IReadOnlyList> SourceCache = []; private static readonly HashSet<(int itemId, bool proliferated, bool sprayed)> FailedSources = []; + private static readonly HashSet PendingSources = []; public static void Init() { @@ -36,6 +37,13 @@ internal class BeltSignalGenerator : PatchImpl ProductionCatalogService.Changed -= OnProductionCatalogChanged; } + public static void OnUpdate() + { + if (PendingSources.Count == 0) return; + foreach (var beltSignal in PendingSources) TryApplySources(beltSignal, true); + PendingSources.Clear(); + } + private static void ResetState() { _initialized = false; @@ -43,6 +51,7 @@ internal class BeltSignalGenerator : PatchImpl _portalFrom = null; _portalTo = null; _signalBeltsCapacity = 0; + PendingSources.Clear(); ClearSourceCache(); } @@ -76,6 +85,7 @@ internal class BeltSignalGenerator : PatchImpl _initialized = false; _signalBelts = null; _signalBeltsCapacity = 0; + PendingSources.Clear(); ClearSourceCache(); } @@ -302,8 +312,15 @@ internal class BeltSignalGenerator : PatchImpl private static void AddSourcesToBeltSignal(BeltSignal beltSignal) { beltSignal.SourceStats = null; + // Signals can change inside the logic tick, for example when drones finish a blueprint, so missing rates + // are planned in the next frame update and the tick only applies cached rates. + if (!TryApplySources(beltSignal, false)) PendingSources.Add(beltSignal); + } + + private static bool TryApplySources(BeltSignal beltSignal, bool calculate) + { var itemId = beltSignal.SignalId; - if (itemId < 1000 || beltSignal.Stack == 0) return; + if (itemId < 1000 || beltSignal.Stack == 0) return true; var catalog = ProductionCatalogService.Current; if (!ReferenceEquals(_sourceCatalog, catalog)) { @@ -319,12 +336,13 @@ internal class BeltSignalGenerator : PatchImpl if (catalog == null) { LogSourceFailure(key, "Production data is not loaded."); - return; + return true; } - if (FailedSources.Contains(key)) return; + if (FailedSources.Contains(key)) return true; if (!SourceCache.TryGetValue(key, out var rates)) { + if (!calculate) return false; try { var request = BeltSignalSourcePreset.Create(catalog, itemId, proliferated, sprayed); @@ -332,7 +350,7 @@ internal class BeltSignalGenerator : PatchImpl if (!report.MaterialComplete) { LogSourceFailure(key, string.Join("; ", report.Diagnostics.Select(diagnostic => diagnostic.Message))); - return; + return true; } rates = BeltSignalSourceStats.FromReport(report, itemId); @@ -340,13 +358,14 @@ internal class BeltSignalGenerator : PatchImpl catch (Exception exception) { LogSourceFailure(key, exception.ToString()); - return; + return true; } SourceCache[key] = rates; } if (rates.Count > 0) beltSignal.SourceStats = new BeltSignalSourceStats(rates); + return true; } private static void LogSourceFailure((int itemId, bool proliferated, bool sprayed) key, string reason)