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;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ internal static class WebSocketApiFeature
|
||||
};
|
||||
|
||||
static readonly object Gate = new object();
|
||||
static readonly ProductionExtraInfoRefresh ProductionRefresh = new ProductionExtraInfoRefresh();
|
||||
static object _ownerToken;
|
||||
static WebSocketApiServer _server;
|
||||
static MainThreadDispatcher _dispatcher;
|
||||
@@ -48,6 +49,7 @@ internal static class WebSocketApiFeature
|
||||
public static void Init()
|
||||
{
|
||||
_reader = new ReflectionReader();
|
||||
ProductionRefresh.Reset();
|
||||
ReflectionReader.Warn = msg => LiveStreamAssist.Logger.LogWarning(msg);
|
||||
_version = GameVersionSnapshot.Empty;
|
||||
_session = new SessionSnapshot(0, null);
|
||||
@@ -144,6 +146,7 @@ internal static class WebSocketApiFeature
|
||||
try { server?.Stop(); }
|
||||
catch (Exception ex) { LiveStreamAssist.Logger.LogWarning($"WebSocket API server stop failed: {ex.Message}"); }
|
||||
_reader?.ClearCache();
|
||||
ProductionRefresh.Reset();
|
||||
ReflectionReader.Warn = null;
|
||||
}
|
||||
|
||||
@@ -182,6 +185,7 @@ internal static class WebSocketApiFeature
|
||||
|
||||
static void BeginSession()
|
||||
{
|
||||
ProductionRefresh.Reset();
|
||||
MainThreadDispatcher dispatcher;
|
||||
int oldGeneration;
|
||||
lock (Gate)
|
||||
@@ -196,6 +200,7 @@ internal static class WebSocketApiFeature
|
||||
|
||||
static void EndSession()
|
||||
{
|
||||
ProductionRefresh.Reset();
|
||||
MainThreadDispatcher dispatcher;
|
||||
lock (Gate)
|
||||
{
|
||||
@@ -262,6 +267,17 @@ internal static class WebSocketApiFeature
|
||||
return walked.Error;
|
||||
if (_session.Generation != work.Generation)
|
||||
return ApiErrors.SessionChanged();
|
||||
if (call.Method == DataMethod.Read)
|
||||
{
|
||||
var production = GameMain.statistics?.production;
|
||||
var calculator = production?.extraInfoCalculator;
|
||||
if (calculator != null)
|
||||
{
|
||||
// Native GameTickCharts refreshes derived statistics without opening the statistics UI.
|
||||
ProductionRefresh.OnRead(call, GameMain.data.factoryCount, calculator.calculating,
|
||||
_server.NowMs, calculator.AddFactory);
|
||||
}
|
||||
}
|
||||
var tick = GameMain.gameTick.ToString(CultureInfo.InvariantCulture);
|
||||
if (call.Method == DataMethod.Describe)
|
||||
return BuildDescribe(walked, session.SessionId, tick);
|
||||
|
||||
Reference in new issue
Block a user