diff --git a/LuaScript.Runtime/Engine/AviUtlScriptContext.cs b/LuaScript.Runtime/Engine/AviUtlScriptContext.cs index 22f3f17..9ab7217 100644 --- a/LuaScript.Runtime/Engine/AviUtlScriptContext.cs +++ b/LuaScript.Runtime/Engine/AviUtlScriptContext.cs @@ -74,7 +74,7 @@ namespace LuaScript public Func? SceneImageLoader { get; set; } - public Func? BrushImageLoader { get; set; } + public Func>, (byte[] Buffer, int Width, int Height)>? BrushImageLoader { get; set; } private readonly Dictionary _stringParameters = new(StringComparer.Ordinal); diff --git a/LuaScript.Runtime/Engine/LuaJitWorker.cs b/LuaScript.Runtime/Engine/LuaJitWorker.cs index 87da6db..4821a6a 100644 --- a/LuaScript.Runtime/Engine/LuaJitWorker.cs +++ b/LuaScript.Runtime/Engine/LuaJitWorker.cs @@ -72,7 +72,7 @@ namespace LuaScript.Engine Func loadImage, Func loadMovie, Func loadScene, - Func loadBrush, + Func>, (byte[] buffer, int w, int h)> loadBrush, Action>> addEffect, Action addDraw, Action setAnchor, @@ -324,7 +324,7 @@ namespace LuaScript.Engine Func loadImage, Func loadMovie, Func loadScene, - Func loadBrush, + Func>, (byte[] buffer, int w, int h)> loadBrush, Action>> addEffect, Action setAnchor, Func sceneGetValue, @@ -355,6 +355,9 @@ namespace LuaScript.Engine case NativeProtocol.CbKindLoadBrush: ResolveLoadBrushCallback(view, loadBrush); break; + case NativeProtocol.CbKindBrush: + ResolveBrushCallback(view, loadBrush); + break; case NativeProtocol.CbKindEffect: ResolveEffectCallback(view, addEffect); break; @@ -810,20 +813,50 @@ namespace LuaScript.Engine view.Write(NativeProtocol.OffCallbackFound, 1); } + private static readonly KeyValuePair[] s_noBrushArguments = []; + private void ResolveLoadBrushCallback( MemoryMappedViewAccessor view, - Func loadBrush) + Func>, (byte[] buffer, int w, int h)> loadBrush) { int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen); string name = Encoding.UTF8.GetString(_callbackTag, 0, tagLen); + WriteBrushResult(view, loadBrush, name, s_noBrushArguments); + } + + private void ResolveBrushCallback( + MemoryMappedViewAccessor view, + Func>, (byte[] buffer, int w, int h)> loadBrush) + { + int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); + view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen); + + var segments = Encoding.UTF8.GetString(_callbackTag, 0, tagLen).Split('\0'); + if (segments.Length == 0) + { + view.Write(NativeProtocol.OffLoadResultWidth, 0); + view.Write(NativeProtocol.OffLoadResultHeight, 0); + view.Write(NativeProtocol.OffCallbackFound, 0); + return; + } + + WriteBrushResult(view, loadBrush, segments[0], ParseNamedArguments(segments)); + } + + private void WriteBrushResult( + MemoryMappedViewAccessor view, + Func>, (byte[] buffer, int w, int h)> loadBrush, + string name, + IReadOnlyList> arguments) + { long rOff = NativeProtocol.CallbackResultOffset; double width = view.ReadDouble(rOff + 0 * 8); double height = view.ReadDouble(rOff + 1 * 8); (byte[] buffer, int w, int h) result; - try { result = loadBrush(name, width, height); } + try { result = loadBrush(name, width, height, arguments); } catch { result = ([], 0, 0); } long pixelSize = (long)result.w * result.h * 4; @@ -841,16 +874,11 @@ namespace LuaScript.Engine view.Write(NativeProtocol.OffCallbackFound, 1); } - private void ResolveEffectCallback(MemoryMappedViewAccessor view, Action>> addEffect) + private static IReadOnlyList> ParseNamedArguments(string[] segments) { - int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); - view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen); + if (segments.Length < 3) + return s_noBrushArguments; - var segments = Encoding.UTF8.GetString(_callbackTag, 0, tagLen).Split('\0'); - if (segments.Length == 0) - return; - - string effectName = segments[0]; var arguments = new List>(); for (int i = 1; i + 1 < segments.Length; i += 2) { @@ -867,8 +895,19 @@ namespace LuaScript.Engine value = raw; arguments.Add(new KeyValuePair(key, value)); } + return arguments; + } - try { addEffect(effectName, arguments); } + private void ResolveEffectCallback(MemoryMappedViewAccessor view, Action>> addEffect) + { + int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); + view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen); + + var segments = Encoding.UTF8.GetString(_callbackTag, 0, tagLen).Split('\0'); + if (segments.Length == 0) + return; + + try { addEffect(segments[0], ParseNamedArguments(segments)); } catch { } view.Write(NativeProtocol.OffCallbackFound, 1); } diff --git a/LuaScript.Runtime/Engine/LuaScriptEngine.cs b/LuaScript.Runtime/Engine/LuaScriptEngine.cs index 6e60243..95eeee7 100644 --- a/LuaScript.Runtime/Engine/LuaScriptEngine.cs +++ b/LuaScript.Runtime/Engine/LuaScriptEngine.cs @@ -557,7 +557,8 @@ namespace LuaScript LoadBrush( args[1].String, args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d, - args.Count > 3 ? args[3].CastToNumber() ?? 0d : 0d); + args.Count > 3 ? args[3].CastToNumber() ?? 0d : 0d, + s_noNamedArguments); break; } return DynValue.Void; @@ -873,9 +874,19 @@ namespace LuaScript if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) return DynValue.Void; - string name = args[0].String; + _activeContext.AddEffect(new AviUtlEffectRequest(args[0].String, CollectNamedArguments(args, 1))); + return DynValue.Void; + } + + private static readonly KeyValuePair[] s_noNamedArguments = []; + + private static IReadOnlyList> CollectNamedArguments(CallbackArguments args, int start) + { + if (start + 1 >= args.Count) + return s_noNamedArguments; + var arguments = new List>(); - for (int i = 1; i + 1 < args.Count; i += 2) + for (int i = start; i + 1 < args.Count; i += 2) { if (args[i].Type != DataType.String) continue; @@ -889,8 +900,27 @@ namespace LuaScript }; arguments.Add(new KeyValuePair(args[i].String, boxed)); } + return arguments; + } - _activeContext.AddEffect(new AviUtlEffectRequest(name, arguments)); + [LuaFunction("brush")] + private DynValue Brush(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + + int start = 1; + double width = 0d; + double height = 0d; + if (args.Count > 2 && args[1].Type == DataType.Number && args[2].Type == DataType.Number) + { + width = args[1].Number; + height = args[2].Number; + start = 3; + } + + LoadBrush(args[0].String, width, height, CollectNamedArguments(args, start)); return DynValue.Void; } @@ -971,7 +1001,7 @@ namespace LuaScript RefreshObjDimensions(); } - private void LoadBrush(string name, double width, double height) + private void LoadBrush(string name, double width, double height, IReadOnlyList> arguments) { var ctx = _activeContext!; var loader = ctx.BrushImageLoader; @@ -979,7 +1009,7 @@ namespace LuaScript return; double w = width > 0d ? width : ctx.ImageWidth; double h = height > 0d ? height : ctx.ImageHeight; - var (buffer, bw, bh) = loader(name, w, h); + var (buffer, bw, bh) = loader(name, w, h, arguments); if (bw <= 0 || bh <= 0) return; ctx.ReplaceBuffer(buffer, bw, bh); diff --git a/LuaScript/Drawing/BrushImageRenderer.cs b/LuaScript/Drawing/BrushImageRenderer.cs index 8a4119b..ed55095 100644 --- a/LuaScript/Drawing/BrushImageRenderer.cs +++ b/LuaScript/Drawing/BrushImageRenderer.cs @@ -8,11 +8,26 @@ namespace LuaScript { private const int MaxDimension = 8192; - private readonly Dictionary _sources = new(StringComparer.Ordinal); + private sealed class Entry(IBrushParameter parameter, IBrushSource source) + { + public IBrushParameter Parameter { get; } = parameter; + public IBrushSource Source { get; } = source; + public string[] AppliedKeys { get; set; } = []; + } + + private readonly Dictionary _sources = new(StringComparer.Ordinal); private GraphicsDevicesAndContext? _ctx; private PixelBufferManager? _pixels; - public bool TryRender(TimelineItemSourceDescription desc, string name, int width, int height, out byte[] pixels, out int outWidth, out int outHeight) + public bool TryRender( + TimelineItemSourceDescription desc, + string name, + int width, + int height, + IReadOnlyList> arguments, + out byte[] pixels, + out int outWidth, + out int outHeight) { pixels = []; outWidth = 0; @@ -21,24 +36,42 @@ namespace LuaScript if (width <= 0 || height <= 0 || width > MaxDimension || height > MaxDimension) return false; - if (!_sources.TryGetValue(name, out var source)) + if (!_sources.TryGetValue(name, out var entry)) { - source = CreateSource(name); - _sources[name] = source; + entry = CreateEntry(name); + _sources[name] = entry; } - if (source is null) + if (entry is null) return false; + if (!KeysMatch(entry.AppliedKeys, arguments)) + { + entry.Source.Dispose(); + entry = CreateEntry(name); + _sources[name] = entry; + if (entry is null) + return false; + } + try { - source.Update(desc); + if (arguments.Count > 0) + { + Ymm4PropertyBinder.ApplyArguments(entry.Parameter, arguments); + if (entry.AppliedKeys.Length != arguments.Count) + entry.AppliedKeys = new string[arguments.Count]; + for (int i = 0; i < arguments.Count; i++) + entry.AppliedKeys[i] = arguments[i].Key; + } + + entry.Source.Update(desc); _pixels ??= new PixelBufferManager(_ctx!); - pixels = _pixels.LoadBrushPixels(source.Brush, width, height); + pixels = _pixels.LoadBrushPixels(entry.Source.Brush, width, height); } catch { _sources.Remove(name); - source.Dispose(); + entry.Source.Dispose(); pixels = []; return false; } @@ -48,22 +81,35 @@ namespace LuaScript return true; } - private IBrushSource? CreateSource(string name) + private static bool KeysMatch(string[] appliedKeys, IReadOnlyList> arguments) + { + if (appliedKeys.Length != arguments.Count) + return false; + for (int i = 0; i < appliedKeys.Length; i++) + { + if (!string.Equals(appliedKeys[i], arguments[i].Key, StringComparison.Ordinal)) + return false; + } + return true; + } + + private Entry? CreateEntry(string name) { foreach (var plugin in BrushFactory.Plugins) { if (!string.Equals(plugin.Name, name, StringComparison.Ordinal)) continue; _ctx ??= new GraphicsDevicesAndContext(devices); - return plugin.CreateBrushParameter().CreateBrush(_ctx); + var parameter = plugin.CreateBrushParameter(); + return new Entry(parameter, parameter.CreateBrush(_ctx)); } return null; } public void Dispose() { - foreach (var source in _sources.Values) - source?.Dispose(); + foreach (var entry in _sources.Values) + entry?.Source.Dispose(); _sources.Clear(); _pixels?.Dispose(); _ctx?.Dispose(); diff --git a/LuaScript/Effects/LuaScriptEffectProcessor.cs b/LuaScript/Effects/LuaScriptEffectProcessor.cs index 3bfb5cf..2bffda0 100644 --- a/LuaScript/Effects/LuaScriptEffectProcessor.cs +++ b/LuaScript/Effects/LuaScriptEffectProcessor.cs @@ -139,7 +139,7 @@ namespace LuaScript private Func? _nativeLoadImage; private Func? _nativeLoadMovie; private Func? _loadSceneImage; - private Func? _loadBrushImage; + private Func>, (byte[] Buffer, int Width, int Height)>? _loadBrushImage; private SceneImageRenderer? _sceneImageRenderer; private BrushImageRenderer? _brushImageRenderer; private bool _sceneImageLoaded; @@ -1194,7 +1194,7 @@ namespace LuaScript } } - private (byte[] Buffer, int Width, int Height) LoadBrushImage(string name, double width, double height) + private (byte[] Buffer, int Width, int Height) LoadBrushImage(string name, double width, double height, IReadOnlyList> arguments) { var desc = _frameDesc; var devices = _hostDevices; @@ -1208,7 +1208,7 @@ namespace LuaScript try { _brushImageRenderer ??= new BrushImageRenderer(devices); - if (!_brushImageRenderer.TryRender(desc, name, w, h, out var pixels, out int outWidth, out int outHeight)) + if (!_brushImageRenderer.TryRender(desc, name, w, h, arguments, out var pixels, out int outWidth, out int outHeight)) return s_noLoadedImage; return (pixels, outWidth, outHeight); }