DreamWeaveWebTools.cs wires the launcher into the ServerPack hooks: the card buttons (render thread, draw only, a click queues StartLaunch with RunOnTick), the /rserver market|suit|abilities verbs, the /weblogin reply read off chat (Code, Refused and Stale eat the line; Stale does nothing else), the 10 s timeout polled every tick, and Reset at login, logout and shutdown. Every launcher call is under lock (Gate); Send, OpenWeb, Reply and the log are always outside it. OpenWeb's answer replaces the "Opening X..." status. WebButtons.cs draws the buttons and is the only ImGui code in the web tools. A source-scan test pins the thread rules. Rebased onto release 2026.11.8.1, which ships its own DreamWeavePlugin.cs (bank check, Bank page) and DreamWeaveSkills.cs (Skills and Character panels, /rserver skills). Those files keep the one implementation of each shared hook and call the web tools' Web* method first; the web code lives in DreamWeaveWebTools.cs. The ImGui scan covers the web tools' own files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
318 lines
11 KiB
C#
318 lines
11 KiB
C#
// DreamWeaveWebTools.cs - RynthDreamWeave's website tools, through the shared server plugin's hooks
|
|
// (Plugins/Shared/ServerPack/ServerPackPlugin.cs): the card's Market, Suit Builder and Ability Planner
|
|
// buttons and "/rserver market | suit | abilities", which open the website logged in as this account.
|
|
// The hooks it shares with the bank and the Skills panel are implemented once, in DreamWeavePlugin.cs and
|
|
// DreamWeaveSkills.cs, and call the Web* methods here first.
|
|
//
|
|
// The flow: a button (render thread) queues StartLaunch for the tick; a verb runs it on the pump. It
|
|
// sends "/weblogin"; ACE answers "[WEB] Launch code: ..." and the chat hook eats that line and opens
|
|
// DreamWeaveWeb.LaunchUrl through OpenWeb (ServerPackWeb.cs). Only the code comes from chat; the host
|
|
// and the path are constants. The code is never logged, saved, chatted or put in a status line.
|
|
//
|
|
// Threads (WebLauncher's remark): every launcher call is made under lock (_web.Gate), and the lock is
|
|
// held around launcher calls only, never around Send, OpenWeb, Reply or the log. Times come from
|
|
// Environment.TickCount64. The card hook runs on the render thread and only draws (WebButtons); the
|
|
// chat, tick, command and login hooks run on the plugin pump; OpenWeb's `done` may run on its worker
|
|
// thread. No hook lets an exception out: this code runs inside acclient.
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using RynthCore.Plugin.RynthDreamWeave;
|
|
|
|
namespace RynthCore.Plugin.ServerPack;
|
|
|
|
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;
|
|
// The card's draw failure, logged once rather than every frame (render thread only).
|
|
private bool _webDrawFailed;
|
|
|
|
private enum WebBrowserWhy { PanelFellBack, NoPanel, NoAccount }
|
|
|
|
private void WebCommandVerbs(List<string> verbs)
|
|
{
|
|
try
|
|
{
|
|
foreach (WebDestination d in DreamWeaveWeb.Destinations) verbs.Add(d.Id);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// The constructor has no Host to log to yet; the verbs are constants, so this can't happen.
|
|
}
|
|
}
|
|
|
|
partial void ServerCardActions()
|
|
{
|
|
try
|
|
{
|
|
WebButtons.Draw(_web, Active, _webClick ??= d => RunOnTick(() => StartLaunch(d)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (!_webDrawFailed)
|
|
{
|
|
_webDrawFailed = true;
|
|
LogTagged("The web buttons failed to draw: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void WebChatCommand(string[] parts, ref bool handled)
|
|
{
|
|
try
|
|
{
|
|
if (parts.Length < 2) return;
|
|
WebDestination? d = DreamWeaveWeb.ById(parts[1]);
|
|
if (d == null) return;
|
|
handled = true;
|
|
StartLaunch(d);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("A web verb failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>Asks DreamWeave for a login code for <paramref name="d"/>. Pump thread (a verb, or a button's RunOnTick).</summary>
|
|
private void StartLaunch(WebDestination d)
|
|
{
|
|
try
|
|
{
|
|
if (!Active)
|
|
{
|
|
Reply.Fail(WebButtons.NotDreamWeave);
|
|
return;
|
|
}
|
|
|
|
LaunchBegin begun;
|
|
string status;
|
|
lock (_web.Gate)
|
|
{
|
|
begun = _web.Begin(d, Environment.TickCount64);
|
|
status = _web.Status;
|
|
}
|
|
if (begun == LaunchBegin.Busy)
|
|
{
|
|
Reply.Fail("Already asking DreamWeave for a login code; wait a moment.");
|
|
return;
|
|
}
|
|
if (begun == LaunchBegin.TooSoon)
|
|
{
|
|
Reply.Fail("Wait a few seconds between web opens.");
|
|
return;
|
|
}
|
|
|
|
if (!Send(LaunchWire.RequestFor(d)))
|
|
{
|
|
const string notSent = "Couldn't send /weblogin (not in the world).";
|
|
lock (_web.Gate) _web.Cancel(notSent);
|
|
Reply.Fail(notSent);
|
|
return;
|
|
}
|
|
Reply.Ok(status);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("Starting a web open failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
private void WebChatText(string text, int chatType, ref bool eat)
|
|
{
|
|
LaunchLine kind = LaunchLine.NotOurs;
|
|
try
|
|
{
|
|
if (!Active) return;
|
|
WebDestination? d;
|
|
string launchCode;
|
|
string status;
|
|
lock (_web.Gate)
|
|
{
|
|
kind = _web.OnChatLine(text, chatType, Environment.TickCount64, out d, out launchCode, out _);
|
|
status = _web.Status;
|
|
}
|
|
switch (kind)
|
|
{
|
|
case LaunchLine.NotOurs:
|
|
return;
|
|
case LaunchLine.Stale:
|
|
// A late or repeated code: keep it out of chat and open nothing. No reply, no status, no log.
|
|
eat = true;
|
|
return;
|
|
case LaunchLine.Refused:
|
|
eat = true;
|
|
Reply.Fail(status);
|
|
return;
|
|
case LaunchLine.Code:
|
|
eat = true;
|
|
if (d != null) OpenLaunch(d, DreamWeaveWeb.LaunchUrl(launchCode, d), status);
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// The line that answered our request never reaches chat, whatever failed after it was read.
|
|
if (kind != LaunchLine.NotOurs) eat = true;
|
|
LogTagged("Reading a web reply failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
/// <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.
|
|
/// </summary>
|
|
private void OpenLaunch(WebDestination d, string url, string opening)
|
|
{
|
|
try
|
|
{
|
|
string? account = Host.TryGetAccountName(out string name) ? name : null;
|
|
var request = new WebOpenRequest(url, DreamWeaveWeb.AllowedHosts,
|
|
WebProfile.Key(Info.Id, account) ?? "", DreamWeaveWeb.PanelTitle);
|
|
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));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("Opening a web page failed: " + ex.GetType().Name);
|
|
WebOpenFailed(d, "Couldn't open " + d.Label + ".", opening);
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
try
|
|
{
|
|
if (!outcome.Ok)
|
|
{
|
|
WebOpenFailed(d, "Couldn't open " + d.Label + ": " + outcome.Message, opening);
|
|
return;
|
|
}
|
|
|
|
string text;
|
|
string? note = null;
|
|
if (outcome.Route != WebOpenRoute.Browser) text = "Opened " + d.Label + ".";
|
|
else if (why == WebBrowserWhy.NoPanel)
|
|
{
|
|
text = "Opened " + d.Label + ".";
|
|
if (Interlocked.Exchange(ref _webNoPanelNoted, 1) == 0)
|
|
note = "This RynthCore has no in-game web panel, so " + d.Label + " opened in your browser.";
|
|
}
|
|
else if (why == WebBrowserWhy.NoAccount)
|
|
text = "Opened " + d.Label + " in your browser, not the web panel: RynthCore didn't report the account name.";
|
|
else
|
|
text = "Opened " + d.Label + " in your browser: the web panel didn't take it.";
|
|
|
|
ReplaceOpeningStatus(opening, text);
|
|
Reply.Ok(text);
|
|
if (note != null) Reply.Ok(note);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// OpenWeb's Finish swallows this too; nothing here may reach the engine.
|
|
}
|
|
}
|
|
|
|
private void WebOpenFailed(WebDestination d, string text, string opening)
|
|
{
|
|
try
|
|
{
|
|
ReplaceOpeningStatus(opening, text);
|
|
Reply.Fail(text);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
lock (_web.Gate)
|
|
{
|
|
if (_web.Pending == null && _web.Status == opening) _web.Cancel(text);
|
|
}
|
|
}
|
|
|
|
private void WebTick()
|
|
{
|
|
try
|
|
{
|
|
WebDestination? timedOut;
|
|
string status = "";
|
|
lock (_web.Gate)
|
|
{
|
|
timedOut = _web.TakeTimedOut(Environment.TickCount64);
|
|
if (timedOut != null) status = _web.Status;
|
|
}
|
|
if (timedOut != null) Reply.Fail(status);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("The web request timer failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
private void WebInfoLines(Action<string> line)
|
|
{
|
|
try
|
|
{
|
|
string status;
|
|
lock (_web.Gate) status = _web.Status;
|
|
line("Web tools (/rserver market | suit | abilities): " + (status.Length > 0 ? status : "none opened this login."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("The web tools' info line failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
private void WebLoginComplete()
|
|
{
|
|
try
|
|
{
|
|
lock (_web.Gate) _web.Reset();
|
|
Interlocked.Exchange(ref _webNoPanelNoted, 0);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("Resetting the web tools at login failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
private void WebLogout()
|
|
{
|
|
try
|
|
{
|
|
lock (_web.Gate) _web.Reset();
|
|
CloseWeb();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("Closing the web tools at logout failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
|
|
private void WebShutdown()
|
|
{
|
|
try
|
|
{
|
|
lock (_web.Gate) _web.Reset();
|
|
CloseWeb();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogTagged("Closing the web tools at shutdown failed: " + ex.GetType().Name);
|
|
}
|
|
}
|
|
}
|