RynthDreamWeave: Task 6 review fixes (code never to chat, allow-list scan, result generation)

- ServerChatText reads every line through the launcher, with no Active
  gate first: a code answering our request is eaten whatever the world is
  now, and opens nothing when the world is no longer DreamWeave.
- WebLauncher: a generation token (Begin when Started, Reset) and
  SetResult(generation, status), which replaces the status compare and
  the use of Cancel as a status setter. The no-panel note is once per
  login epoch, so an answer from an earlier login can't spend it.
- An open failure reads "Market: Couldn't open your browser (code N)."
- StartLaunch's catch answers "Couldn't start the web open."
- ServerUsageLines: an optional shared hook for a server's own usage
  lines; DreamWeave lists market | suit | abilities. Hookless servers
  print the same two lines as before (pinned on Coldeve).
- The source scan is allow-lists: every lock is on the launcher's Gate
  and calls only its members, the Stale arm is exactly eat = true, Draw
  calls only ImGui, SafeImGui and click. Comments are stripped without
  touching string literals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 15:22:09 -08:00
commit 5fbbf4e654
5 changed files with 297 additions and 80 deletions

View file

@ -32,6 +32,16 @@ internal static class DreamWeaveWeb
return null;
}
/// <summary>The "/rserver" usage line for the destinations' verbs (ServerUsageLines).</summary>
public static string UsageLine()
{
var ids = new string[Destinations.Length];
var labels = new string[Destinations.Length];
for (int i = 0; i < Destinations.Length; i++) { ids[i] = Destinations[i].Id; labels[i] = Destinations[i].Label; }
return "/rserver " + string.Join(" | ", ids) + " - open the website's " + string.Join(", ", labels)
+ ", logged in as this account.";
}
/// <summary>The sign-in URL for a destination. The caller has already checked the code with LaunchWire.</summary>
public static string LaunchUrl(string code, WebDestination d)
=> SiteBase + "/launch?code=" + code + "&to=" + Uri.EscapeDataString(d.Path);

View file

@ -14,7 +14,10 @@ internal enum LaunchLine { NotOurs, Code, Refused, Stale }
/// <c>lock (Gate)</c>. The callers are the chat pump, the tick thread and the render thread. Hold
/// the lock only around calls into this class, never around Send or ShellExecute. nowMs must come
/// from one monotonic clock (Environment.TickCount64). After a Code reply, Status reads
/// "Opening X..."; if the open then fails, the caller must overwrite Status.
/// "Opening X..."; the caller then reports the open's answer with SetResult, never by setting Status
/// another way. Generation names the request a Code or Refused reply resolved: read it under the same
/// lock, right after OnChatLine, and pass it to SetResult. Begin (when Started) and Reset move it on,
/// so an answer for an older request, or one from before a relog, never overwrites a newer status.
/// </summary>
internal sealed class WebLauncher
{
@ -30,8 +33,12 @@ internal sealed class WebLauncher
// Nothing lowers it (not Begin, Cancel, Reset, Code, Refused or Expire), so a late code from any
// earlier request is still eaten as Stale, whatever happened to the requests since.
private long _staleUntilMs = long.MinValue;
private long _generation;
public WebDestination? Pending => _pending;
/// <summary>The current request's generation: after OnChatLine returns Code or Refused, the one it resolved.</summary>
public long Generation => _generation;
public string Status { get; private set; } = "";
/// <summary>
@ -44,6 +51,7 @@ internal sealed class WebLauncher
if (_pending != null) return LaunchBegin.Busy;
if (_everBegun && nowMs - _lastBeginMs < LaunchWire.MinIntervalMs) return LaunchBegin.TooSoon;
_pending = d;
_generation++;
_everBegun = true;
_lastBeginMs = nowMs;
_deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs;
@ -102,9 +110,19 @@ internal sealed class WebLauncher
public void Reset()
{
_pending = null;
_generation++;
Status = "";
}
/// <summary>
/// The answer to the request of <paramref name="generation"/> (the open's result): set as Status only
/// while nothing is pending and no Begin or Reset has happened since; otherwise ignored.
/// </summary>
public void SetResult(long generation, string status)
{
if (_pending == null && generation == _generation) Status = status;
}
private void Expire()
{
_pending = null;

View file

@ -26,8 +26,10 @@ public sealed partial class ServerPackPlugin
private readonly WebLauncher _web = new();
// The card's click handler, made once (render thread only).
private Action<WebDestination>? _webClick;
// The "no in-game web panel" line, once per login (set from OpenWeb's done, any thread).
private int _webNoPanelNoted;
// Counts logins, so an answer from before a relog never spends this login's "no web panel" line.
private int _webLoginEpoch;
// The login that has had the "no in-game web panel" line (set from OpenWeb's done, any thread).
private int _webNoPanelNotedEpoch;
// The card's draw failure, logged once rather than every frame (render thread only).
private bool _webDrawFailed;
@ -45,6 +47,18 @@ public sealed partial class ServerPackPlugin
}
}
partial void ServerUsageLines(Action<string> add)
{
try
{
add(DreamWeaveWeb.UsageLine());
}
catch (Exception)
{
// The constructor has no Host to log to yet; the destinations are constants.
}
}
partial void ServerCardActions()
{
try
@ -117,23 +131,33 @@ public sealed partial class ServerPackPlugin
}
catch (Exception ex)
{
LogTagged("Starting a web open failed: " + ex.GetType().Name);
try
{
LogTagged("Starting a web open failed: " + ex.GetType().Name);
Reply.Fail("Couldn't start the web open.");
}
catch (Exception)
{
}
}
}
private void WebChatText(string text, int chatType, ref bool eat)
{
// No Active check before the launcher: only a request it began (or its stale window) is ever
// not NotOurs, and a code that answers one must be eaten whatever the world is now.
LaunchLine kind = LaunchLine.NotOurs;
try
{
if (!Active) return;
WebDestination? d;
string launchCode;
string status;
long generation;
lock (_web.Gate)
{
kind = _web.OnChatLine(text, chatType, Environment.TickCount64, out d, out launchCode, out _);
status = _web.Status;
generation = _web.Generation;
}
switch (kind)
{
@ -149,7 +173,13 @@ public sealed partial class ServerPackPlugin
return;
case LaunchLine.Code:
eat = true;
if (d != null) OpenLaunch(d, DreamWeaveWeb.LaunchUrl(launchCode, d), status);
if (d == null) return;
if (!Active)
{
WebOpenFailed(d, generation, WebButtons.NotDreamWeave);
return;
}
OpenLaunch(d, DreamWeaveWeb.LaunchUrl(launchCode, d), generation);
return;
}
}
@ -162,11 +192,12 @@ public sealed partial class ServerPackPlugin
}
/// <summary>
/// Opens the launch URL (pump thread). <paramref name="opening"/> is the status the launcher set on the
/// code ("Opening X..."); the answer replaces it, so the card never stays on it.
/// Opens the launch URL (pump thread). <paramref name="generation"/> is the request the code answered;
/// the open's answer replaces its "Opening X..." status (SetResult), so the card never stays on it.
/// </summary>
private void OpenLaunch(WebDestination d, string url, string opening)
private void OpenLaunch(WebDestination d, string url, long generation)
{
int epoch = Volatile.Read(ref _webLoginEpoch);
try
{
string? account = Host.TryGetAccountName(out string name) ? name : null;
@ -175,23 +206,23 @@ public sealed partial class ServerPackPlugin
bool panel = PanelAvailable;
WebBrowserWhy why = ServerWebOpen.Route(request, panel) != WebOpenRoute.Browser ? WebBrowserWhy.PanelFellBack
: panel ? WebBrowserWhy.NoAccount : WebBrowserWhy.NoPanel;
OpenWeb(request, outcome => WebOpened(d, outcome, why, opening));
OpenWeb(request, outcome => WebOpened(d, outcome, why, generation, epoch));
}
catch (Exception ex)
{
LogTagged("Opening a web page failed: " + ex.GetType().Name);
WebOpenFailed(d, "Couldn't open " + d.Label + ".", opening);
WebOpenFailed(d, generation, "Couldn't open " + d.Label + ".");
}
}
/// <summary>OpenWeb's answer. Any thread (the browser opens on a worker thread).</summary>
private void WebOpened(WebDestination d, WebOpenOutcome outcome, WebBrowserWhy why, string opening)
private void WebOpened(WebDestination d, WebOpenOutcome outcome, WebBrowserWhy why, long generation, int epoch)
{
try
{
if (!outcome.Ok)
{
WebOpenFailed(d, "Couldn't open " + d.Label + ": " + outcome.Message, opening);
WebOpenFailed(d, generation, d.Label + ": " + outcome.Message);
return;
}
@ -201,7 +232,8 @@ public sealed partial class ServerPackPlugin
else if (why == WebBrowserWhy.NoPanel)
{
text = "Opened " + d.Label + ".";
if (Interlocked.Exchange(ref _webNoPanelNoted, 1) == 0)
// Once per login, and never spent by an answer from an earlier login.
if (epoch == Volatile.Read(ref _webLoginEpoch) && Interlocked.Exchange(ref _webNoPanelNotedEpoch, epoch) != epoch)
note = "This RynthCore has no in-game web panel, so " + d.Label + " opened in your browser.";
}
else if (why == WebBrowserWhy.NoAccount)
@ -209,7 +241,7 @@ public sealed partial class ServerPackPlugin
else
text = "Opened " + d.Label + " in your browser: the web panel didn't take it.";
ReplaceOpeningStatus(opening, text);
SetWebResult(generation, text);
Reply.Ok(text);
if (note != null) Reply.Ok(note);
}
@ -219,11 +251,11 @@ public sealed partial class ServerPackPlugin
}
}
private void WebOpenFailed(WebDestination d, string text, string opening)
private void WebOpenFailed(WebDestination d, long generation, string text)
{
try
{
ReplaceOpeningStatus(opening, text);
SetWebResult(generation, text);
Reply.Fail(text);
}
catch (Exception)
@ -231,17 +263,10 @@ public sealed partial class ServerPackPlugin
}
}
/// <summary>
/// Puts the answer in the card's status, unless a newer request has taken over the launcher since the
/// code came in (then its own status stands). Cancel is the launcher's status setter; with nothing
/// pending it only sets the status.
/// </summary>
private void ReplaceOpeningStatus(string opening, string text)
/// <summary>The open's answer as the card's status, unless a newer request (or a relog) has taken over since.</summary>
private void SetWebResult(long generation, string text)
{
lock (_web.Gate)
{
if (_web.Pending == null && _web.Status == opening) _web.Cancel(text);
}
lock (_web.Gate) _web.SetResult(generation, text);
}
private void WebTick()
@ -249,11 +274,11 @@ public sealed partial class ServerPackPlugin
try
{
WebDestination? timedOut;
string status = "";
string status;
lock (_web.Gate)
{
timedOut = _web.TakeTimedOut(Environment.TickCount64);
if (timedOut != null) status = _web.Status;
status = _web.Status;
}
if (timedOut != null) Reply.Fail(status);
}
@ -282,7 +307,7 @@ public sealed partial class ServerPackPlugin
try
{
lock (_web.Gate) _web.Reset();
Interlocked.Exchange(ref _webNoPanelNoted, 0);
Interlocked.Increment(ref _webLoginEpoch);
}
catch (Exception ex)
{