162 lines
6.6 KiB
C#
162 lines
6.6 KiB
C#
// ============================================================================
|
|
// RynthSuite - Plugins/Shared/ServerQuestGuideClient.cs
|
|
// A quest reader's side of "RynthServer.QuestGuide" (RynthServerQuestGuideApi.cs):
|
|
// the active server's own quest guide, from the plugin RynthCore names as its
|
|
// owner (GetServerStateJson "activePlugin"). The reader never names a server:
|
|
// whatever guide the active server's plugin offers is the server guide, and a
|
|
// plugin that offers none means no server guide.
|
|
//
|
|
// The info JSON is polled (every 2 s, cheap); the guide itself is read only
|
|
// when the owner, its table or the info's revision changes. Revision goes up
|
|
// whenever the guide or its status line changes (the reader redraws).
|
|
// Needs, linked into the reader: RynthServerQuestGuideApi.cs, QuestGuideData.cs.
|
|
// Pump thread only.
|
|
// ============================================================================
|
|
|
|
using System;
|
|
using System.Text;
|
|
using RynthCore.PluginSdk;
|
|
|
|
namespace RynthCore.Plugin.Shared;
|
|
|
|
internal sealed unsafe class ServerQuestGuideClient
|
|
{
|
|
private const long PollMs = 2000;
|
|
private const long RequeryMs = 10_000;
|
|
|
|
private string _owner = string.Empty;
|
|
private RynthServerQuestGuideApiV1* _table;
|
|
private long _nextPollAt, _nextQueryAt;
|
|
private string _infoJson = string.Empty;
|
|
private long _readRevision = long.MinValue;
|
|
|
|
/// <summary>The server's guide as last read (empty when it offers none).</summary>
|
|
public QuestGuide Guide { get; private set; } = new();
|
|
|
|
/// <summary>The offered info (tag, status, commands); null without a server guide.</summary>
|
|
public ServerQuestGuideInfo? Info { get; private set; }
|
|
|
|
/// <summary>Goes up when <see cref="Guide"/> or <see cref="Info"/> changes.</summary>
|
|
public int Revision { get; private set; }
|
|
|
|
/// <summary>The active server's plugin offers a guide (it may still be empty while it reads).</summary>
|
|
public bool Available => Info != null;
|
|
|
|
/// <summary>What the reader calls the guide: the plugin's tag, else "Server".</summary>
|
|
public string Tag => Info is { Tag.Length: > 0 } i ? i.Tag : "Server";
|
|
|
|
/// <summary>Logout: forget everything; the next poll reads fresh.</summary>
|
|
public void Clear()
|
|
{
|
|
_table = null;
|
|
_owner = string.Empty;
|
|
_nextPollAt = _nextQueryAt = 0;
|
|
SetNone();
|
|
}
|
|
|
|
/// <summary>Polls <paramref name="owner"/>'s guide when due; <paramref name="force"/> polls now. Pump thread.</summary>
|
|
public void Poll(RynthCoreHost host, string? owner, long now, bool force = false)
|
|
{
|
|
if (!force && now < _nextPollAt) return;
|
|
_nextPollAt = now + PollMs;
|
|
owner = (owner ?? string.Empty).Trim();
|
|
if (!owner.Equals(_owner, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_table = null;
|
|
_owner = owner;
|
|
_nextQueryAt = 0;
|
|
SetNone();
|
|
}
|
|
if (owner.Length == 0) return;
|
|
if (_table != null && _table->Version == 0)
|
|
{
|
|
_table = null; // the plugin shut down (a reload): ask the new copy
|
|
_nextQueryAt = 0;
|
|
}
|
|
if (_table == null)
|
|
{
|
|
if (force) _nextQueryAt = 0;
|
|
if (now < _nextQueryAt || !host.HasGetPluginInterface) return;
|
|
_nextQueryAt = now + RequeryMs;
|
|
var p = (RynthServerQuestGuideApiV1*)host.GetPluginInterface(owner, RynthServerQuestGuideApiV1.InterfaceName, RynthServerQuestGuideApiV1.InterfaceVersion);
|
|
if (!Usable(p)) { SetNone(); return; }
|
|
_table = p;
|
|
}
|
|
ReadTable();
|
|
}
|
|
|
|
/// <summary>Reads a table directly (the tests hand one in; Poll uses it once the host gave one).</summary>
|
|
internal void Use(RynthServerQuestGuideApiV1* table, string owner)
|
|
{
|
|
if (!Usable(table)) { _table = null; SetNone(); return; }
|
|
_owner = owner;
|
|
_table = table;
|
|
ReadTable();
|
|
}
|
|
|
|
private static bool Usable(RynthServerQuestGuideApiV1* p) =>
|
|
p != null && p->Version >= 1 && p->StructSize >= (uint)sizeof(RynthServerQuestGuideApiV1)
|
|
&& p->GetGuideInfoJson != null && p->GetGuideJson != null && p->GuideCommand != null;
|
|
|
|
private void ReadTable()
|
|
{
|
|
string info = Read(_table->GetGuideInfoJson, 1024, RynthServerQuestGuide.MaxInfoBytes);
|
|
if (info == _infoJson) return;
|
|
_infoJson = info;
|
|
ServerQuestGuideInfo? parsed = ServerQuestGuideInfo.Parse(info);
|
|
if (parsed == null) { SetNone(); return; }
|
|
Info = parsed;
|
|
Revision++;
|
|
if (parsed.Revision == _readRevision) return;
|
|
if (parsed.Bytes <= 0 || parsed.Bytes > RynthServerQuestGuide.MaxGuideBytes)
|
|
{
|
|
// Not ready yet (or too big to take): no quests, the status line says why.
|
|
Guide = new QuestGuide();
|
|
_readRevision = parsed.Revision;
|
|
return;
|
|
}
|
|
string json = Read(_table->GetGuideJson, parsed.Bytes + 1, RynthServerQuestGuide.MaxGuideBytes + 1);
|
|
if (json.Length == 0)
|
|
{
|
|
_infoJson = string.Empty; // try again on the next poll
|
|
return;
|
|
}
|
|
Guide = QuestGuide.Parse(json);
|
|
_readRevision = parsed.Revision;
|
|
}
|
|
|
|
/// <summary>Asks the server's plugin to run a guide command ("refresh", "settings"); false when it doesn't take it.</summary>
|
|
public bool Command(string word)
|
|
{
|
|
if (_table == null || _table->Version == 0 || Info == null || !Info.Takes(word)) return false;
|
|
byte[] bytes = Encoding.UTF8.GetBytes(word.Trim().ToLowerInvariant() + "\0");
|
|
fixed (byte* b = bytes) return _table->GuideCommand(b) != 0;
|
|
}
|
|
|
|
/// <summary>Copies a string out, growing the buffer once when the plugin says it didn't fit ("" = none).</summary>
|
|
private static string Read(delegate* unmanaged[Cdecl]<byte*, int, int> fn, int first, int max)
|
|
{
|
|
if (fn == null) return string.Empty;
|
|
var buffer = new byte[Math.Max(16, Math.Min(first, max))];
|
|
for (int attempt = 0; attempt < 2; attempt++)
|
|
{
|
|
int len;
|
|
fixed (byte* b = buffer) len = fn(b, buffer.Length);
|
|
if (len <= 0) return string.Empty;
|
|
if (len < buffer.Length) return Encoding.UTF8.GetString(buffer, 0, len);
|
|
if (len + 1 > max) return string.Empty;
|
|
buffer = new byte[len + 1];
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
private void SetNone()
|
|
{
|
|
_infoJson = string.Empty;
|
|
_readRevision = long.MinValue;
|
|
if (Info == null && Guide.IsEmpty) return;
|
|
Info = null;
|
|
Guide = new QuestGuide();
|
|
Revision++;
|
|
}
|
|
}
|