// IltSettingsModel.cs - RynthILT's copy of the Infinite Leaftide settings (the "ILT settings" view). // // Nothing here is decided by RynthILT; it shows and edits what others own: // * RynthCore owns the world verdict, this character's Auto / On / Off override and the ILT // world-name list. RynthILT reads the verdict from its server list (plugin API v86, // GetServerStateJson, parsed by Plugins/Shared/EngineServerState.cs): the plugin tick polls // it every 2 s and publishes the "ilt" entry here (ApplyServerState). The world names aren't // in that list; "/rc server names ilt" prints them. Edits go back as RynthCore commands: // "/rc server ilt auto|on|off" and "/rc server names ilt add|remove ". // * RynthILT's own Hub reads which ILT features the server has on; the plugin tick publishes // them (PublishFeatures). // Each is one immutable snapshot published with a single reference write, so the render thread // always reads a whole one. Pure (no host calls): RynthILT.Tests covers it. using System; using System.Collections.Generic; using System.Threading; using RynthCore.Plugin.Shared; namespace RynthCore.Plugin.RynthILT.IltSettingsView; /// RynthCore's verdict for Infinite Leaftide. internal sealed record IltServerView(string Name, bool Active, string Force, bool Detected, string Reason); /// What RynthCore's server list says: the ILT verdict (null if it lists no "ilt"). internal sealed record IltEngineView(IltServerView? Server); /// The Hub's server read: the world, its last check, and each feature (-1 unknown, 0 off, 1 on). internal sealed record IltFeatureView(string World, string Source, string LastCheck, bool Checking, bool IltLike, KeyValuePair[] Features); internal sealed class IltSettingsModel { /// RynthCore's id for Infinite Leaftide. public const string ServerId = EngineServerState.IltId; /// Longest pattern RynthCore keeps (EngineSettings.MaxIltServerNameLength). public const int MaxPattern = 64; /// The RynthCore command that prints the ILT world names (and hosts) in chat. public const string NamesListCommand = "/rc server names " + ServerId; private IltEngineView? _engine; private IltFeatureView? _features; /// The last server list from RynthCore (null until one arrives). Any thread. public IltEngineView? Engine => Volatile.Read(ref _engine); /// The last feature read (null until the Hub publishes one). Any thread. public IltFeatureView? Features => Volatile.Read(ref _features); /// RynthCore's server list (GetServerStateJson); null (no answer) keeps the last one. Any thread. public void ApplyServerState(EngineServerState? state) { IltEngineView? view = FromServerState(state); if (view != null) Volatile.Write(ref _engine, view); } /// /// Older "servermenu" text push (RynthPluginApplyRemoteCommand). No-op here: the verdict now /// comes from / IltEngineVerdict. Kept so ApplyRemoteCommand compiles. /// public void ApplyEngine(string? payload) { _ = payload; } /// Older "iltfeatures" text push. No-op; features come from the Hub's PublishFeatures. public void ApplyFeatures(string? payload) { _ = payload; } /// The Hub's own feature read (plugin tick); null clears it. Any thread. public void PublishFeatures(IltFeatureView? view) => Volatile.Write(ref _features, view); /// Logout: the next character's world and features come fresh. public void Clear() { Volatile.Write(ref _engine, null); Volatile.Write(ref _features, null); } /// The ILT verdict from RynthCore's server list; null for no list. public static IltEngineView? FromServerState(EngineServerState? state) { if (state == null) return null; EngineServer? s = state.Find(ServerId); return new IltEngineView(s == null ? null : new IltServerView(s.Name.Length > 0 ? s.Name : "Infinite Leaftide", s.Active, NormalizeForce(s.Force), s.Detected, s.Reason)); } /// "auto", "on" or "off" (anything else is shown as auto). public static string NormalizeForce(string? force) => (force ?? string.Empty).Trim().ToLowerInvariant() switch { "on" => "on", "off" => "off", _ => "auto", }; /// The RynthCore command that sets this character's override; "" for an unknown mode. public static string ForceCommand(string mode) => mode switch { "auto" or "on" or "off" => "/rc server " + ServerId + " " + mode, _ => string.Empty, }; /// /// The RynthCore command that adds or removes a world-name pattern, or "" (with /// ) when the pattern can't be sent: empty, longer than /// , or with a control character or '/' (it is spliced into a command). /// public static string NamesCommand(bool add, string? pattern, out string error) { error = string.Empty; string p = (pattern ?? string.Empty).Trim(); if (p.Length == 0) { error = "Enter a world name ('*' matches any text)."; return string.Empty; } if (p.Length > MaxPattern) { error = $"At most {MaxPattern} characters."; return string.Empty; } foreach (char c in p) if (char.IsControl(c) || c == '/' || c >= 128) { error = "World names are plain text (no '/', line breaks or special characters)."; return string.Empty; } return "/rc server names " + ServerId + (add ? " add " : " remove ") + p; } /// A feature's state as text. public static string StateText(int value) => value switch { 1 => "on", 0 => "off", _ => "unknown" }; }