using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MoonSharp.Interpreter;
namespace RynthCore.Plugin.RynthLua;
///
/// One script's Lua state (MoonSharp, Lua 5.2), run on the plugin pump thread.
///
/// Every piece of script code runs as a coroutine ("thread") resumed from
/// : the main chunk, each event handler call and each
/// spawn. sleep(ms) parks the thread until its wake time, await(action)
/// until the action's IsFinished is true, and MoonSharp's auto-yield pauses any
/// thread that runs too long without sleeping, so an endless loop slows the script down
/// instead of freezing the client. A script stays running while it has live threads,
/// handlers (v1 on, v2 events, commands) or a global OnBotTick; Stop clears them.
///
internal sealed class LuaScriptHost
{
/// C:\Games\RynthSuite\RynthLua\Scripts (see ).
public static string ScriptFolder => RynthLuaPaths.Scripts;
/// Where scripts lived while Lua was part of RynthAi (copied over once, then left alone).
public static string LegacyScriptFolder => RynthLuaPaths.LegacyScripts;
private const int MaxConsoleLines = 400;
private const int AutoYieldCounter = 5_000; // VM instructions before a forced pause
private sealed class LuaThread
{
public DynValue Co = DynValue.Nil;
public long WakeAt;
public Func? WaitFor; // await: resume only once this is true
public DynValue[] Args = Array.Empty();
public bool Started;
/// Called once when the thread ends: its return values, or the error message.
public Action? OnDone;
}
private readonly Action _log;
private readonly Action