using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using MoonSharp.Interpreter;
namespace RynthCore.Plugin.RynthLua;
/// Optional script.json next to a folder script's index.lua.
public sealed class ScriptManifest
{
[JsonPropertyName("name")] public string? Name { get; set; }
[JsonPropertyName("description")] public string? Description { get; set; }
[JsonPropertyName("version")] public string? Version { get; set; }
[JsonPropertyName("author")] public string? Author { get; set; }
/// "none" (the default) | "global" | "character": seeds autostart once; the player can change it.
[JsonPropertyName("autostart")] public string? Autostart { get; set; }
}
///
/// One script: where it lives, its Lua state (), and everything
/// it registered through the v2 API (events, commands). Stopping the script clears those.
///
internal sealed class ScriptContext
{
public string Name { get; }
/// The script's folder (folder scripts) or null (single-file scripts).
public string? Folder { get; }
/// index.lua (folder scripts) or Name.lua.
public string EntryFile { get; }
public ScriptManifest Manifest { get; }
public LuaScriptHost Host { get; }
/// True when the script came from the panel editor or /lua exec, not a file.
public bool Transient { get; init; }
private readonly Dictionary _events = new(StringComparer.OrdinalIgnoreCase);
/// Chat commands this script registered ("/name" → handler).
public readonly Dictionary Commands = new(StringComparer.OrdinalIgnoreCase);
/// Anything else an API piece wants dropped when the script stops (actions, timers, ...).
public readonly List OnStop = new();
/// Counts of pending work that keeps the script alive (e.g. its queued actions).
public readonly List> KeepAlive = new();
public ScriptContext(string name, string? folder, string entryFile, ScriptManifest manifest, LuaScriptHost host)
{
Name = name;
Folder = folder;
EntryFile = entryFile;
Manifest = manifest;
Host = host;
host.Folder = folder;
host.ExtraHandlers = () =>
{
int n = Commands.Count;
foreach (var e in _events.Values) n += e.Count;
foreach (var k in KeepAlive)
{
try { n += k(); } catch { }
}
return n;
};
host.Stopped += _ => Clear();
}
/// The event registered under (e.g. "World.OnObjectCreated"), created on first use.
public ScriptEvent Event(string key)
{
if (!_events.TryGetValue(key, out var e)) _events[key] = e = new ScriptEvent(key);
return e;
}
public bool HasHandlers(string key) => _events.TryGetValue(key, out var e) && e.Count > 0;
/// Keys starting with that have handlers (per-object events).
public IEnumerable EventKeys(string prefix)
{
foreach (var kv in _events)
if (kv.Value.Count > 0 && kv.Key.StartsWith(prefix, StringComparison.Ordinal)) yield return kv.Key;
}
/// Forgets an event and its handlers (a per-object event whose object is gone).
public void DropEvent(string key)
{
if (_events.TryGetValue(key, out var e)) e.Clear();
}
/// Runs every handler of with an args table built for this script's Lua state.
public void Fire(string key, Func