mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
fix(livestreamassist): refresh theoretical item rates
Queue the native production extra-info calculator on factory reads so production and consumption reference speeds update without the stats UI. Deduplicate and throttle refreshes, let active batches finish, and clear pending work at session boundaries. Add refresh scheduling regressions and document the initial cache warm-up.
This commit is contained in:
1 parent
f03d6c758e
commit
63a6a6ec6c
7 files changed
+155
-2
No files matched your search
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LiveStreamAssist.Api;
|
||||
|
||||
internal sealed class ProductionExtraInfoRefresh
|
||||
{
|
||||
const int RefreshIntervalMs = 2000;
|
||||
readonly Dictionary<int, long> _nextRefreshMs = new Dictionary<int, long>();
|
||||
readonly HashSet<int> _pendingFactories = new HashSet<int>();
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_nextRefreshMs.Clear();
|
||||
_pendingFactories.Clear();
|
||||
}
|
||||
|
||||
public void OnRead(DataCall call, int factoryCount, bool calculating, long nowMs, Action<int> addFactory)
|
||||
{
|
||||
var factoryIndex = GetFactoryIndex(call);
|
||||
if (factoryIndex < 0 || factoryIndex >= factoryCount) return;
|
||||
|
||||
if (!_nextRefreshMs.TryGetValue(factoryIndex, out var next) || nowMs >= next)
|
||||
_pendingFactories.Add(factoryIndex);
|
||||
|
||||
// Let the native batch finish before requeuing factories it has already processed.
|
||||
if (calculating) return;
|
||||
foreach (var index in _pendingFactories)
|
||||
{
|
||||
if (index >= factoryCount) continue;
|
||||
addFactory(index);
|
||||
_nextRefreshMs[index] = nowMs + RefreshIntervalMs;
|
||||
}
|
||||
_pendingFactories.Clear();
|
||||
}
|
||||
|
||||
static int GetFactoryIndex(DataCall call)
|
||||
{
|
||||
if (call.Method != DataMethod.Read || call.Path == null) return -1;
|
||||
var path = call.Path;
|
||||
var offset = 0;
|
||||
if (call.Root == "game")
|
||||
{
|
||||
if (path.Length == 0 || path[0].Kind != PathKind.Member || path[0].Name != "statistics")
|
||||
return -1;
|
||||
offset = 1;
|
||||
}
|
||||
else if (call.Root != "statistics")
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (path.Length < offset + 3 ||
|
||||
path[offset].Kind != PathKind.Member || path[offset].Name != "production" ||
|
||||
path[offset + 1].Kind != PathKind.Member || path[offset + 1].Name != "factoryStatPool" ||
|
||||
path[offset + 2].Kind != PathKind.Index)
|
||||
return -1;
|
||||
return path[offset + 2].Index;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user