From 6d2a5cbe2495207759a2c7c7e3517bcae7c2ee1d Mon Sep 17 00:00:00 2001 From: routersys Date: Mon, 29 Jun 2026 08:31:53 +0900 Subject: [PATCH] =?UTF-8?q?obj.copybuffer=E3=81=AE=E3=83=90=E3=83=83?= =?UTF-8?q?=E3=83=95=E3=82=A1=E4=BF=9D=E5=AD=98=E5=BE=A9=E5=85=83=E3=82=92?= =?UTF-8?q?=E4=B8=A1=E3=82=A8=E3=83=B3=E3=82=B8=E3=83=B3=E3=81=A7=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeWorkerRoundTripTests.cs | 47 ++++++++++++ LuaScript/AviUtlScriptContext.cs | 74 +++++++++++++++++++ LuaScript/Engine/ScriptDirective.cs | 5 +- LuaScript/LuaScriptEngine.cs | 19 +++++ native/worker.lua | 45 +++++++++++ 5 files changed, 187 insertions(+), 3 deletions(-) diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index ffca592..db8ebbc 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -312,6 +312,53 @@ namespace LuaScript.Tests Assert.Equal(0.5d, commands[0].Alpha); } + [Fact] + public void CopyBuffer_TmpSaveRestore_RoundTripsPixels() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + const int w = 2, h = 2; + var pixels = new byte[w * h * 4]; + for (int i = 0; i < pixels.Length; i++) + pixels[i] = (byte)((i * 37 + 11) & 0xFF); + pixels[3] = 255; pixels[7] = 255; pixels[11] = 255; pixels[15] = 255; + var original = (byte[])pixels.Clone(); + + var fields = Fields(w, h, 0d); + const string script = + "obj.copybuffer('tmp','obj') " + + "for y=0,obj.h-1 do for x=0,obj.w-1 do obj.setpixel(x,y,0,0,0,0) end end " + + "obj.copybuffer('obj','tmp')"; + + bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.Equal(original, pixels); + } + + [Fact] + public void CopyBuffer_CachePersistsAcrossRuns() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + const int w = 2, h = 2; + var stored = new byte[w * h * 4]; + for (int i = 0; i < stored.Length; i++) + stored[i] = (byte)((i * 53 + 7) & 0xFF); + stored[3] = 255; stored[7] = 255; stored[11] = 255; stored[15] = 255; + + var save = (byte[])stored.Clone(); + bool ok = _worker.Execute("obj.copybuffer('cache:foo','obj')", Fields(w, h, 0d), save, w, h, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + Assert.True(ok, error); + + var blank = new byte[w * h * 4]; + ok = _worker.Execute("obj.copybuffer('obj','cache:foo')", Fields(w, h, 0d), blank, w, h, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out error); + Assert.True(ok, error); + Assert.True(dirty); + Assert.Equal(stored, blank); + } + [Fact] public void RuntimeError_IsReported_AndWorkerSurvives() { diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs index 6c491e0..461a247 100644 --- a/LuaScript/AviUtlScriptContext.cs +++ b/LuaScript/AviUtlScriptContext.cs @@ -74,6 +74,80 @@ namespace LuaScript internal void ClearDraws() => _drawCommands.Clear(); + private readonly Dictionary _buffers = new(StringComparer.Ordinal); + + internal bool CopyBuffer(string dst, string src) + { + if (!TryReadBuffer(src, out var data, out int w, out int h)) + return false; + return TryWriteBuffer(dst, data, w, h); + } + + private bool TryReadBuffer(string id, out byte[] data, out int width, out int height) + { + data = []; + width = 0; + height = 0; + switch (BufferKind(id, out string key)) + { + case 'o': + EnsurePixelBuffer(); + if (_pixelBuffer is null) + return false; + data = _pixelBuffer; + width = ImageWidth; + height = ImageHeight; + return true; + case 't': + case 'c': + if (_buffers.TryGetValue(key, out var stored)) + { + data = stored.Data; + width = stored.Width; + height = stored.Height; + return true; + } + return false; + default: + return false; + } + } + + private bool TryWriteBuffer(string id, byte[] data, int width, int height) + { + switch (BufferKind(id, out string key)) + { + case 'o': + ReplaceBuffer((byte[])data.Clone(), width, height); + return true; + case 't': + case 'c': + _buffers[key] = ((byte[])data.Clone(), width, height); + return false; + default: + return false; + } + } + + private static char BufferKind(string id, out string key) + { + key = string.Empty; + int i = 0; + while (i < id.Length && (id[i] == ' ' || id[i] == '\t')) + i++; + if (i >= id.Length) + return '\0'; + char kind = char.ToLowerInvariant(id[i]); + if (kind == 't') + key = "t"; + else if (kind == 'c') + { + int colon = id.IndexOf(':', i); + key = colon >= 0 ? "c:" + id[(colon + 1)..].Trim() : "c"; + } + return kind; + } + public double RxRad => Rx * Math.PI / 180d; public double RyRad => Ry * Math.PI / 180d; public double RzRad => Rz * Math.PI / 180d; diff --git a/LuaScript/Engine/ScriptDirective.cs b/LuaScript/Engine/ScriptDirective.cs index 80d7844..3e9fc84 100644 --- a/LuaScript/Engine/ScriptDirective.cs +++ b/LuaScript/Engine/ScriptDirective.cs @@ -20,9 +20,8 @@ namespace LuaScript.Engine script is not null && (script.Contains("getpixel", StringComparison.Ordinal) || script.Contains("setpixel", StringComparison.Ordinal)); public static bool UsesDrawingApi(string? script) => - script is not null && ( - script.Contains("obj.setoption", StringComparison.Ordinal) || - script.Contains("obj.copybuffer", StringComparison.Ordinal)); + script is not null && + script.Contains("obj.setoption", StringComparison.Ordinal); private static bool TryGetDirective(string? script, out ScriptEngineKind kind) { diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 763b4ce..941a87b 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -567,6 +567,18 @@ namespace LuaScript _activeContext.AddDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly)); return DynValue.Void; }); + + obj["copybuffer"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count < 2 || + args[0].Type != DataType.String || args[1].Type != DataType.String) + return DynValue.Void; + + if (_activeContext.CopyBuffer(args[0].String, args[1].String)) + RefreshObjDimensions(); + return DynValue.Void; + }); } private void LoadFigure(string name, int color, double size, double line, double aspect) @@ -580,6 +592,13 @@ namespace LuaScript var buffer = FigureRenderer.Render(name, w, h, color, line); _activeContext!.ReplaceBuffer(buffer, w, h); + RefreshObjDimensions(); + } + + private void RefreshObjDimensions() + { + int w = _activeContext!.ImageWidth; + int h = _activeContext.ImageHeight; var obj = _objTable!; obj["w"] = w; obj["h"] = h; diff --git a/native/worker.lua b/native/worker.lua index cb7973d..d97fb5e 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -76,8 +76,21 @@ local width, height = 0, 0 local pixels = ffi.cast("uint8_t*", base + PIXEL_OFFSET) local cbResult = ffi.cast("double*", base + CB_RESULT_OFFSET) local cbTagD = ffi.cast("double*", base + CB_TAG_OFFSET) +local pixelCapacity = mapSize - PIXEL_OFFSET +local buffers = {} local dirty = false +local function bufferKind(id) + local c = id:match("^%s*(.)") + if not c then return nil end + c = c:lower() + if c == "c" then + local name = id:match(":%s*(.-)%s*$") + return "c", name and ("c:" .. name) or "c" + end + return c, c +end + local obj = {} local scene = {} local ymm4 = {} @@ -269,6 +282,38 @@ function obj.drawpoly(...) k32.WaitForSingleObject(workEvent, INFINITE) end +function obj.copybuffer(dst, src) + if type(dst) ~= "string" or type(src) ~= "string" then return end + local sk, skey = bufferKind(src) + local data, w, h + if sk == "o" then + data = ffi.string(pixels, width * height * 4) + w = width; h = height + elseif sk == "t" or sk == "c" then + local b = buffers[skey] + if not b then return end + data, w, h = b.data, b.w, b.h + else + return + end + local dk, dkey = bufferKind(dst) + if dk == "o" then + local need = w * h * 4 + if need > pixelCapacity then return end + ffi.copy(pixels, data, need) + width = w; height = h + i32[OFF_WIDTH] = w; i32[OFF_HEIGHT] = h + dirty = true + obj.w = w; obj.h = h + obj.hw = w / 2; obj.hh = h / 2 + obj.cx = w / 2; obj.cy = h / 2 + obj.cz = 0 + obj.diagonal = math.sqrt(w * w + h * h) + elseif dk == "t" or dk == "c" then + buffers[dkey] = { data = data, w = w, h = h } + end +end + local cacheTag, cacheFrame, cacheFound local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer