130 lines
6.8 KiB
C#
130 lines
6.8 KiB
C#
// ============================================================================
|
|
// RynthSuite - Plugins/Shared/RynthServerQuestGuideApi.cs
|
|
// The "RynthServer.QuestGuide" interface (2026-11-27): a server plugin's own
|
|
// quest guide (its wiki's quests with their steps, stamps, places and loot),
|
|
// for a quest reader such as RynthOracle's Guide tab, which shows it without
|
|
// knowing the server. Server-specific guide data (a server's own wiki, read
|
|
// however that wiki needs) stays in the server's plugin; the reader only takes
|
|
// the finished guide. Same idea as "RynthServer.QuestPack" (the quest list):
|
|
// RynthCore says which plugin owns the active server (plugin API v86,
|
|
// GetServerStateJson "activePlugin"), and the reader asks only that plugin
|
|
// (host GetPluginInterface), so a guide shows only while its server is active.
|
|
// No interface = no server guide.
|
|
//
|
|
// The idea and the first version came from SilentKelpie's backlog (Forgejo
|
|
// rynth PR #12, "RynthServer.QuestGuide" over a guide file); this one hands
|
|
// the guide over in a buffer instead of a file path and adds a command line.
|
|
// Compiled into the server plugins and their readers (linked source), so the
|
|
// layout can't drift. The guide's own format: QuestGuideData.cs.
|
|
//
|
|
// Rules (as the other shared interfaces):
|
|
// - Append-only: new functions go at the end, and Version goes up.
|
|
// - Strings are UTF-8, NUL-terminated. The info JSON is plain ASCII.
|
|
// - Every function returns safely (no exception crosses the boundary).
|
|
// - The table is allocated once per loaded copy of the plugin and never freed;
|
|
// shutdown sets Version to 0, and a reader re-queries when it sees that.
|
|
//
|
|
// GetGuideInfoJson payload (schema 1; read leniently, fields may be added):
|
|
// {
|
|
// "schema": 1,
|
|
// "tag": "My Server", // what the reader calls the guide ("My Server Wiki")
|
|
// "plugin": "RynthMyServer",
|
|
// "revision": 3, // goes up whenever the guide changes (read GetGuideJson again)
|
|
// "bytes": 123456, // GetGuideJson's length in UTF-8 bytes (0 = no guide yet)
|
|
// "quests": 317, // quests in the guide
|
|
// "running": false, // the plugin is reading its source right now
|
|
// "status": "317 quests ...",// one line for the reader to show
|
|
// "commands": ["refresh"] // GuideCommand words this plugin takes ("refresh", "settings")
|
|
// }
|
|
//
|
|
// GetGuideJson payload: the guide (QuestGuideData.cs, QuestGuide.ToJson).
|
|
// GuideCommand(line): a word from "commands" (UTF-8, NUL-terminated); 1 = taken
|
|
// (queued for the plugin's own tick), 0 = not a command it takes.
|
|
// ============================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.Json;
|
|
|
|
namespace RynthCore.Plugin.Shared;
|
|
|
|
/// <summary>Constants of the "RynthServer.QuestGuide" interface.</summary>
|
|
public static class RynthServerQuestGuide
|
|
{
|
|
/// <summary>The info JSON's schema.</summary>
|
|
public const int Schema = 1;
|
|
|
|
/// <summary>The biggest guide a reader takes (bytes); a bigger one is refused, not cut.</summary>
|
|
public const int MaxGuideBytes = 16 * 1024 * 1024;
|
|
|
|
/// <summary>The longest info JSON a reader takes.</summary>
|
|
public const int MaxInfoBytes = 64 * 1024;
|
|
|
|
/// <summary>Ask the plugin to read its source again.</summary>
|
|
public const string CommandRefresh = "refresh";
|
|
|
|
/// <summary>Ask the plugin to show its own settings for the guide (a login, a file to import).</summary>
|
|
public const string CommandSettings = "settings";
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public unsafe struct RynthServerQuestGuideApiV1
|
|
{
|
|
public const string InterfaceName = "RynthServer.QuestGuide";
|
|
public const uint InterfaceVersion = 1;
|
|
|
|
public uint Version; // InterfaceVersion of the table the plugin filled; 0 once it has shut down
|
|
public uint StructSize; // sizeof(RynthServerQuestGuideApiV1) as the plugin built it
|
|
|
|
/// <summary>
|
|
/// GetGuideInfoJson(buffer, capacity): writes the info JSON (UTF-8, NUL-terminated) into buffer when it
|
|
/// fits, and returns its length in bytes without the NUL. A return value of capacity or more means
|
|
/// nothing was written: call again with a bigger buffer. 0 = no guide (or shutting down). Any thread.
|
|
/// </summary>
|
|
public delegate* unmanaged[Cdecl]<byte*, int, int> GetGuideInfoJson;
|
|
|
|
/// <summary>GetGuideJson(buffer, capacity): the guide, with GetGuideInfoJson's buffer rules. 0 = none. Any thread.</summary>
|
|
public delegate* unmanaged[Cdecl]<byte*, int, int> GetGuideJson;
|
|
|
|
/// <summary>GuideCommand(line): 1 when the plugin takes the command (it runs it on its own tick), 0 when not. Any thread.</summary>
|
|
public delegate* unmanaged[Cdecl]<byte*, int> GuideCommand;
|
|
}
|
|
|
|
/// <summary>The "RynthServer.QuestGuide" info JSON, read leniently.</summary>
|
|
internal sealed record ServerQuestGuideInfo(string Tag, string Plugin, long Revision, int Bytes, int Quests,
|
|
bool Running, string Status, List<string> Commands)
|
|
{
|
|
/// <summary>The info, or null for junk or another schema. A guide not ready yet has Bytes 0.</summary>
|
|
public static ServerQuestGuideInfo? Parse(string? json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json)) return null;
|
|
try
|
|
{
|
|
using JsonDocument doc = JsonDocument.Parse(json);
|
|
JsonElement r = doc.RootElement;
|
|
if (r.ValueKind != JsonValueKind.Object) return null;
|
|
if (!r.TryGetProperty("schema", out JsonElement s) || s.ValueKind != JsonValueKind.Number
|
|
|| !s.TryGetInt32(out int schema) || schema != RynthServerQuestGuide.Schema) return null;
|
|
var commands = new List<string>();
|
|
if (r.TryGetProperty("commands", out JsonElement c) && c.ValueKind == JsonValueKind.Array)
|
|
foreach (JsonElement e in c.EnumerateArray())
|
|
if (e.ValueKind == JsonValueKind.String && e.GetString() is { Length: > 0 } word) commands.Add(word.Trim().ToLowerInvariant());
|
|
return new ServerQuestGuideInfo(Str(r, "tag"), Str(r, "plugin"), Long(r, "revision"), (int)Math.Max(0, Long(r, "bytes")),
|
|
(int)Math.Max(0, Long(r, "quests")), r.TryGetProperty("running", out JsonElement ru) && ru.ValueKind == JsonValueKind.True,
|
|
Str(r, "status"), commands);
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool Takes(string command) => Commands.Contains(command.Trim().ToLowerInvariant());
|
|
|
|
private static string Str(JsonElement r, string name) =>
|
|
r.TryGetProperty(name, out JsonElement e) && e.ValueKind == JsonValueKind.String ? (e.GetString() ?? string.Empty).Trim() : string.Empty;
|
|
|
|
private static long Long(JsonElement r, string name) =>
|
|
r.TryGetProperty(name, out JsonElement e) && e.ValueKind == JsonValueKind.Number && e.TryGetInt64(out long v) ? v : 0;
|
|
}
|