From 27f04c75604c7c7e85170b4dfc5a392c7c9334d6 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 16:17:35 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB=E7=99=BB?= =?UTF-8?q?=E9=8C=B2=E3=82=92=20LuaScope=20=E3=81=A7=E6=B1=8E=E7=94=A8?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Runtime/Engine/LuaScope.cs | 81 ++++++++ LuaScript.Runtime/Engine/LuaScriptEngine.cs | 212 +++++--------------- 2 files changed, 128 insertions(+), 165 deletions(-) create mode 100644 LuaScript.Runtime/Engine/LuaScope.cs diff --git a/LuaScript.Runtime/Engine/LuaScope.cs b/LuaScript.Runtime/Engine/LuaScope.cs new file mode 100644 index 0000000..74d50ce --- /dev/null +++ b/LuaScript.Runtime/Engine/LuaScope.cs @@ -0,0 +1,81 @@ +using MoonSharp.Interpreter; + +namespace LuaScript +{ + internal sealed class LuaScope + { + private readonly string? _globalName; + private readonly Action? _register; + private readonly Action? _update; + private readonly List _removalBuffer = []; + + private Table? _table; + private HashSet? _snapshot; + + internal LuaScope(string? globalName, Action
? register, Action? update) + { + _globalName = globalName; + _register = register; + _update = update; + } + + internal Table Table => _table!; + + internal void Create(Script script) + { + _snapshot = null; + if (_globalName is null) + { + _table = script.Globals; + return; + } + _table = Build(script); + } + + internal void Reconcile(Script script) + { + if (_globalName is null) + return; + if (ReferenceEquals(script.Globals.Get(_globalName).Table, _table)) + return; + _table = Build(script); + _snapshot = null; + } + + internal void ResetUserKeys() + { + if (_snapshot is null) + return; + var buffer = _removalBuffer; + buffer.Clear(); + foreach (var key in _table!.Keys) + { + if (key.Type == DataType.String && !_snapshot.Contains(key.String)) + buffer.Add(key.String); + } + foreach (var key in buffer) + _table[key] = DynValue.Nil; + } + + internal void Update(AviUtlScriptContext ctx) => _update?.Invoke(_table!, ctx); + + internal void CaptureSnapshot() + { + var keys = new HashSet(StringComparer.Ordinal); + foreach (var key in _table!.Keys) + { + if (key.Type == DataType.String) + keys.Add(key.String); + } + _snapshot = keys; + } + + private Table Build(Script script) + { + var table = new Table(script); + _register?.Invoke(table); + script.Globals[_globalName!] = table; + return table; + } + } +} diff --git a/LuaScript.Runtime/Engine/LuaScriptEngine.cs b/LuaScript.Runtime/Engine/LuaScriptEngine.cs index c8af60f..4125840 100644 --- a/LuaScript.Runtime/Engine/LuaScriptEngine.cs +++ b/LuaScript.Runtime/Engine/LuaScriptEngine.cs @@ -54,8 +54,6 @@ namespace LuaScript private LuaScriptException? _jobException; private volatile bool _disposeRequested; - private readonly List _removalBuffer = []; - private readonly Dictionary _options = new(StringComparer.Ordinal); private readonly Dictionary _pixelOptions = new(StringComparer.Ordinal); @@ -66,26 +64,30 @@ namespace LuaScript private Script? _script; private DynValue? _compiledChunk; private string _lastCompiledCode = string.Empty; - private Table? _objTable; - private Table? _sceneTable; - private Table? _animTable; - private Table? _ymm4Table; - private HashSet? _builtinGlobalSnapshot; - private HashSet? _objTableSnapshot; - private HashSet? _sceneTableSnapshot; - private HashSet? _animTableSnapshot; - private HashSet? _ymm4TableSnapshot; private CancellationToken _activeCancellation; private AviUtlScriptContext? _activeContext; private AviUtlGlobalRegistrar? _globalRegistrar; private SceneTableRegistrar? _sceneRegistrar; + private readonly LuaScope _objScope; + private readonly LuaScope[] _scopes; + private bool _scopesInitialized; + internal ExecutionThread(Func mediaLoaderFactory) { _mediaLoaderFactory = mediaLoaderFactory; _activeCancellation = _cts.Token; _debugger.UpdateToken(_cts.Token); + _objScope = new LuaScope("obj", RegisterLuaMembers, UpdateLuaMembers); + _scopes = + [ + new LuaScope(null, null, (table, ctx) => _globalRegistrar!.UpdateLuaMembers(table, ctx)), + new LuaScope("scene", RegisterSceneCallbacks, (table, ctx) => _sceneRegistrar!.UpdateLuaMembers(table, ctx)), + _objScope, + new LuaScope("anim", AnimTableRegistrar.RegisterFunctions, null), + new LuaScope("ymm4", null, Ymm4TableRegistrar.UpdateLuaMembers), + ]; _thread = new Thread(WorkerLoop) { IsBackground = true, @@ -184,15 +186,7 @@ namespace LuaScript _script = null; _compiledChunk = null; _lastCompiledCode = string.Empty; - _objTable = null; - _sceneTable = null; - _animTable = null; - _ymm4Table = null; - _builtinGlobalSnapshot = null; - _objTableSnapshot = null; - _sceneTableSnapshot = null; - _animTableSnapshot = null; - _ymm4TableSnapshot = null; + _scopesInitialized = false; EnsureScript(); } @@ -217,7 +211,7 @@ namespace LuaScript private void SetupGlobals(AviUtlScriptContext ctx) { var script = _script!; - bool isFirstSetup = _objTable is null; + bool isFirstSetup = !_scopesInitialized; _options.Clear(); _pixelOptions.Clear(); @@ -225,83 +219,25 @@ namespace LuaScript if (isFirstSetup) { - var sceneTable = new Table(script); - RegisterSceneCallbacks(sceneTable); - script.Globals["scene"] = sceneTable; - - var objTable = new Table(script); - RegisterLuaMembers(objTable); - script.Globals["obj"] = objTable; - - var animTable = new Table(script); - AnimTableRegistrar.RegisterFunctions(animTable); - script.Globals["anim"] = animTable; - - var ymm4Table = new Table(script); - script.Globals["ymm4"] = ymm4Table; - - _sceneTable = sceneTable; - _animTable = animTable; - _ymm4Table = ymm4Table; - _objTable = objTable; + foreach (var scope in _scopes) + scope.Create(script); + _scopesInitialized = true; } else { - ResetUserGlobals(script); - - if (!ReferenceEquals(script.Globals.Get("scene").Table, _sceneTable)) - { - var sceneTable = new Table(script); - RegisterSceneCallbacks(sceneTable); - script.Globals["scene"] = sceneTable; - _sceneTable = sceneTable; - _sceneTableSnapshot = null; - } - - if (!ReferenceEquals(script.Globals.Get("obj").Table, _objTable)) - { - var objTable = new Table(script); - RegisterLuaMembers(objTable); - script.Globals["obj"] = objTable; - _objTable = objTable; - _objTableSnapshot = null; - } - - if (!ReferenceEquals(script.Globals.Get("anim").Table, _animTable)) - { - var animTable = new Table(script); - AnimTableRegistrar.RegisterFunctions(animTable); - script.Globals["anim"] = animTable; - _animTable = animTable; - _animTableSnapshot = null; - } - - if (!ReferenceEquals(script.Globals.Get("ymm4").Table, _ymm4Table)) - { - var ymm4Table = new Table(script); - script.Globals["ymm4"] = ymm4Table; - _ymm4Table = ymm4Table; - _ymm4TableSnapshot = null; - } - - ResetUserTableKeys(_objTable!, _objTableSnapshot); - ResetUserTableKeys(_sceneTable!, _sceneTableSnapshot); - ResetUserTableKeys(_animTable!, _animTableSnapshot); - ResetUserTableKeys(_ymm4Table!, _ymm4TableSnapshot); + foreach (var scope in _scopes) + scope.Reconcile(script); + foreach (var scope in _scopes) + scope.ResetUserKeys(); } - _globalRegistrar!.UpdateLuaMembers(script.Globals, ctx); - _sceneRegistrar!.UpdateLuaMembers(_sceneTable!, ctx); - Ymm4TableRegistrar.UpdateLuaMembers(_ymm4Table!, ctx); - UpdateLuaMembers(_objTable!, ctx); + foreach (var scope in _scopes) + scope.Update(ctx); if (isFirstSetup) { - _builtinGlobalSnapshot = CaptureGlobalKeys(script); - _objTableSnapshot = CaptureTableKeys(_objTable!); - _sceneTableSnapshot = CaptureTableKeys(_sceneTable!); - _animTableSnapshot = CaptureTableKeys(_animTable!); - _ymm4TableSnapshot = CaptureTableKeys(_ymm4Table!); + foreach (var scope in _scopes) + scope.CaptureSnapshot(); } script.Call( @@ -309,61 +245,6 @@ namespace LuaScript DynValue.NewNumber(ctx.Frame)); } - private static HashSet CaptureGlobalKeys(Script script) - { - var keys = new HashSet(StringComparer.Ordinal); - foreach (var key in script.Globals.Keys) - { - if (key.Type == DataType.String) - keys.Add(key.String); - } - return keys; - } - - private static HashSet CaptureTableKeys(Table table) - { - var keys = new HashSet(StringComparer.Ordinal); - foreach (var key in table.Keys) - { - if (key.Type == DataType.String) - keys.Add(key.String); - } - return keys; - } - - private void ResetUserTableKeys(Table table, HashSet? snapshot) - { - if (snapshot is null) return; - var buffer = _removalBuffer; - buffer.Clear(); - foreach (var key in table.Keys) - { - if (key.Type == DataType.String && - !snapshot.Contains(key.String)) - { - buffer.Add(key.String); - } - } - foreach (var key in buffer) - table[key] = DynValue.Nil; - } - - private void ResetUserGlobals(Script script) - { - var buffer = _removalBuffer; - buffer.Clear(); - foreach (var key in script.Globals.Keys) - { - if (key.Type == DataType.String && - !_builtinGlobalSnapshot!.Contains(key.String)) - { - buffer.Add(key.String); - } - } - foreach (var key in buffer) - script.Globals[key] = DynValue.Nil; - } - private double CurrentTimeRatio() => _activeContext is { TotalTime: > 0d } context ? context.Time / context.TotalTime : 0d; @@ -752,7 +633,7 @@ namespace LuaScript _activeCancellation.ThrowIfCancellationRequested(); if (args.Count == 0 || args[0].Type != DataType.String) return DynValue.NewNumber(0d); - var value = _objTable!.Get(args[0].String); + var value = _objScope.Table.Get(args[0].String); return value.Type == DataType.Number ? value : DynValue.NewNumber(0d); } @@ -924,7 +805,7 @@ namespace LuaScript { int w = _activeContext!.ImageWidth; int h = _activeContext.ImageHeight; - var obj = _objTable!; + var obj = _objScope.Table; obj["w"] = w; obj["h"] = h; obj["hw"] = w / 2d; @@ -960,27 +841,28 @@ namespace LuaScript private void ReadBackGlobals(AviUtlScriptContext ctx) { - if (_objTable is null) return; + if (!_scopesInitialized) return; - ctx.X = _objTable.Get("x").CastToNumber() ?? ctx.X; - ctx.Y = _objTable.Get("y").CastToNumber() ?? ctx.Y; - ctx.Z = _objTable.Get("z").CastToNumber() ?? ctx.Z; - ctx.Ox = _objTable.Get("ox").CastToNumber() ?? ctx.Ox; - ctx.Oy = _objTable.Get("oy").CastToNumber() ?? ctx.Oy; - ctx.Oz = _objTable.Get("oz").CastToNumber() ?? ctx.Oz; - ctx.Alpha = _objTable.Get("alpha").CastToNumber() ?? ctx.Alpha; + var obj = _objScope.Table; + ctx.X = obj.Get("x").CastToNumber() ?? ctx.X; + ctx.Y = obj.Get("y").CastToNumber() ?? ctx.Y; + ctx.Z = obj.Get("z").CastToNumber() ?? ctx.Z; + ctx.Ox = obj.Get("ox").CastToNumber() ?? ctx.Ox; + ctx.Oy = obj.Get("oy").CastToNumber() ?? ctx.Oy; + ctx.Oz = obj.Get("oz").CastToNumber() ?? ctx.Oz; + ctx.Alpha = obj.Get("alpha").CastToNumber() ?? ctx.Alpha; ctx.ApplyWriteBack( - _objTable.Get("sx").CastToNumber() ?? ctx.Sx, - _objTable.Get("sy").CastToNumber() ?? ctx.Sy, - _objTable.Get("zoom").CastToNumber() ?? ctx.Zoom, - _objTable.Get("aspect").CastToNumber() ?? ctx.Aspect, - _objTable.Get("rx").CastToNumber() ?? ctx.Rx, - _objTable.Get("ry").CastToNumber() ?? ctx.Ry, - _objTable.Get("rz").CastToNumber() ?? ctx.Rz, - _objTable.Get("rxr").CastToNumber() ?? ctx.RxRad, - _objTable.Get("ryr").CastToNumber() ?? ctx.RyRad, - _objTable.Get("rzr").CastToNumber() ?? ctx.RzRad); + obj.Get("sx").CastToNumber() ?? ctx.Sx, + obj.Get("sy").CastToNumber() ?? ctx.Sy, + obj.Get("zoom").CastToNumber() ?? ctx.Zoom, + obj.Get("aspect").CastToNumber() ?? ctx.Aspect, + obj.Get("rx").CastToNumber() ?? ctx.Rx, + obj.Get("ry").CastToNumber() ?? ctx.Ry, + obj.Get("rz").CastToNumber() ?? ctx.Rz, + obj.Get("rxr").CastToNumber() ?? ctx.RxRad, + obj.Get("ryr").CastToNumber() ?? ctx.RyRad, + obj.Get("rzr").CastToNumber() ?? ctx.RzRad); } public void Dispose()