diff --git a/LuaScript.Tests/LuaScript.Tests.csproj b/LuaScript.Tests/LuaScript.Tests.csproj
index e3b5821..d8d70a9 100644
--- a/LuaScript.Tests/LuaScript.Tests.csproj
+++ b/LuaScript.Tests/LuaScript.Tests.csproj
@@ -31,6 +31,7 @@
+
diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs
index fb6e0af..87f13a2 100644
--- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs
+++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs
@@ -11,6 +11,7 @@ namespace LuaScript.Tests
private static readonly Func NoResolver = (_, _) => null;
private static readonly Func NoLoadFigure = (_, _, _, _, _) => ([], 1, 1);
private static readonly Action>> NoAddEffect = (_, _) => { };
+ private static readonly Action NoAddDraw = _ => { };
private readonly LuaJitWorker _worker = new(NativeDir);
@@ -36,7 +37,7 @@ namespace LuaScript.Tests
bool ok = _worker.Execute(
"obj.rz = obj.time * 90\nobj.alpha = 128",
- fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoAddEffect, out bool dirty, out _, out _, out _, out _, out string? error);
+ fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.False(dirty);
@@ -71,7 +72,7 @@ namespace LuaScript.Tests
"local gray = r*0.299 + g*0.587 + b*0.114 " +
"obj.setpixel(x,y,gray,gray,gray,a) end end";
- bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoAddEffect, out bool dirty, out _, out _, out _, out _, out string? error);
+ 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);
@@ -106,7 +107,7 @@ namespace LuaScript.Tests
"local gray=r*0.299+g*0.587+b*0.114 " +
"pd:set(base+1,gray) pd:set(base+2,gray) pd:set(base+3,gray) end end";
- bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoAddEffect, out bool dirty, out _, out _, out _, out _, out string? error);
+ 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);
@@ -142,7 +143,7 @@ namespace LuaScript.Tests
for (int k = 1; k <= 5; k++)
{
var fields = Fields(2, 2, k);
- bool ok = _worker.Execute("obj.x = obj.time * 10", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ bool ok = _worker.Execute("obj.x = obj.time * 10", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(k * 10d, fields[NativeProtocol.X]);
}
@@ -157,12 +158,12 @@ namespace LuaScript.Tests
var fields = Fields(2, 2, 0d);
bool timedOut = _worker.Execute(
- "local x=0 while true do x=x+1 end", fields, pixels, 2, 2, 1500, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ "local x=0 while true do x=x+1 end", fields, pixels, 2, 2, 1500, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.False(timedOut);
Assert.Contains("timed out", error);
var fields2 = Fields(2, 2, 3d);
- bool ok = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out error);
+ bool ok = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error);
Assert.True(ok, error);
Assert.Equal(3d, fields2[NativeProtocol.X]);
}
@@ -184,7 +185,7 @@ namespace LuaScript.Tests
"obj.zoom = o.zoom obj.rz = o.rz obj.alpha = o.alpha " +
"obj.sx = o.sy obj.rzr = o.rzr";
- bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(12d, fields[NativeProtocol.X]);
@@ -216,13 +217,13 @@ namespace LuaScript.Tests
"local b = obj.getobject(\"a\") " +
"obj.x = a.x + b.x";
- bool ok = _worker.Execute(script, Fields(2, 2, 0d), pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ bool ok = _worker.Execute(script, Fields(2, 2, 0d), pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(1, calls);
var fields = Fields(2, 2, 0d);
x = 20d;
- ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out error);
+ ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error);
Assert.True(ok, error);
Assert.Equal(2, calls);
Assert.Equal(40d, fields[NativeProtocol.X]);
@@ -239,12 +240,33 @@ namespace LuaScript.Tests
const string script =
"local o = obj.getobject(\"missing\") obj.x = o == nil and 1 or 0";
- bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(1d, fields[NativeProtocol.X]);
}
+ [Fact]
+ public void Draw_RecordsCommandsThroughCallback()
+ {
+ Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
+
+ var pixels = new byte[16];
+ var fields = Fields(2, 2, 0d);
+ var commands = new System.Collections.Generic.List();
+ Action addDraw = commands.Add;
+
+ bool ok = _worker.Execute(
+ "obj.draw() obj.draw(10, 20, 30, 2, 0.5, 0.25)",
+ fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, addDraw,
+ out _, out _, out _, out _, out _, out string? error);
+
+ Assert.True(ok, error);
+ Assert.Equal(2, commands.Count);
+ Assert.Equal(new DrawCommand(0d, 0d, 0d, 1d, 1d, 0d), commands[0]);
+ Assert.Equal(new DrawCommand(10d, 20d, 30d, 2d, 0.5d, 0.25d), commands[1]);
+ }
+
[Fact]
public void RuntimeError_IsReported_AndWorkerSurvives()
{
@@ -253,12 +275,12 @@ namespace LuaScript.Tests
var pixels = new byte[16];
var fields = Fields(2, 2, 0d);
- bool ok = _worker.Execute("obj.x = missing.value", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out string? error);
+ bool ok = _worker.Execute("obj.x = missing.value", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.False(ok);
Assert.False(string.IsNullOrEmpty(error));
var fields2 = Fields(2, 2, 7d);
- bool ok2 = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, out _, out _, out _, out _, out _, out error);
+ bool ok2 = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error);
Assert.True(ok2, error);
Assert.Equal(7d, fields2[NativeProtocol.X]);
}
diff --git a/LuaScript.Tests/ScriptDirectiveTests.cs b/LuaScript.Tests/ScriptDirectiveTests.cs
index 616000c..d812e01 100644
--- a/LuaScript.Tests/ScriptDirectiveTests.cs
+++ b/LuaScript.Tests/ScriptDirectiveTests.cs
@@ -40,7 +40,7 @@ namespace LuaScript.Tests
[InlineData("obj.load(\"figure\", \"円\", 0xff0000, 100)", "MoonSharp")]
[InlineData("obj.load(\"figure\", \"円\", 0xff0000, 100)\nlocal r = obj.getpixel(0,0)", "Native")]
[InlineData("obj.draw()", "MoonSharp")]
- [InlineData("obj.draw()\nlocal r = obj.getpixel(0,0)", "MoonSharp")]
+ [InlineData("obj.draw()\nlocal r = obj.getpixel(0,0)", "Native")]
[InlineData("--!native\nobj.load(\"figure\", \"円\", 0xff0000, 100)", "Native")]
public void ResolveAuto_RoutesPixelScriptsToNative(string script, string expected)
{
diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs
index 974dd58..6c491e0 100644
--- a/LuaScript/AviUtlScriptContext.cs
+++ b/LuaScript/AviUtlScriptContext.cs
@@ -124,6 +124,14 @@ namespace LuaScript
_pixelBufferLoaded = true;
}
+ internal void SetResolvedBuffer(byte[] buffer, int width, int height)
+ {
+ _pixelBuffer = buffer;
+ _pixelBufferLoaded = true;
+ ImageWidth = width;
+ ImageHeight = height;
+ }
+
internal byte[]? GetPixelBuffer() => _pixelBuffer;
internal void MarkPixelsDirty() => _isPixelsDirty = true;
diff --git a/LuaScript/Engine/LuaJitWorker.cs b/LuaScript/Engine/LuaJitWorker.cs
index e25a121..7dfcca7 100644
--- a/LuaScript/Engine/LuaJitWorker.cs
+++ b/LuaScript/Engine/LuaJitWorker.cs
@@ -50,6 +50,7 @@ namespace LuaScript.Engine
Func resolveObject,
Func loadFigure,
Action>> addEffect,
+ Action addDraw,
out bool pixelsDirty,
out bool bufferReplaced,
out byte[]? newPixels,
@@ -103,7 +104,7 @@ namespace LuaScript.Engine
if (status != NativeProtocol.StatusCallback)
break;
- DispatchCallback(view, resolveObject, loadFigure, addEffect);
+ DispatchCallback(view, resolveObject, loadFigure, addEffect, addDraw);
_workEvent.Set();
}
@@ -147,7 +148,8 @@ namespace LuaScript.Engine
MemoryMappedViewAccessor view,
Func resolveObject,
Func loadFigure,
- Action>> addEffect)
+ Action>> addEffect,
+ Action addDraw)
{
int kind = view.ReadInt32(NativeProtocol.OffCallbackKind);
switch (kind)
@@ -161,9 +163,27 @@ namespace LuaScript.Engine
case NativeProtocol.CbKindEffect:
ResolveEffectCallback(view, addEffect);
break;
+ case NativeProtocol.CbKindDraw:
+ ResolveDrawCallback(view, addDraw);
+ break;
}
}
+ private static void ResolveDrawCallback(MemoryMappedViewAccessor view, Action addDraw)
+ {
+ long rOff = NativeProtocol.CallbackResultOffset;
+ double ox = view.ReadDouble(rOff + 0 * 8);
+ double oy = view.ReadDouble(rOff + 1 * 8);
+ double oz = view.ReadDouble(rOff + 2 * 8);
+ double zoom = view.ReadDouble(rOff + 3 * 8);
+ double alpha = view.ReadDouble(rOff + 4 * 8);
+ double aspect = view.ReadDouble(rOff + 5 * 8);
+
+ try { addDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect)); }
+ catch { }
+ view.Write(NativeProtocol.OffCallbackFound, 1);
+ }
+
private void ResolveGetObjectCallback(MemoryMappedViewAccessor view, Func resolveObject)
{
int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame);
diff --git a/LuaScript/Engine/NativeProtocol.cs b/LuaScript/Engine/NativeProtocol.cs
index e9d09e2..aae5c5c 100644
--- a/LuaScript/Engine/NativeProtocol.cs
+++ b/LuaScript/Engine/NativeProtocol.cs
@@ -46,6 +46,7 @@ namespace LuaScript.Engine
public const int CbKindGetObject = 0;
public const int CbKindLoadFigure = 1;
public const int CbKindEffect = 2;
+ public const int CbKindDraw = 3;
public const int CbExist = 0;
public const int CbX = 1;
diff --git a/LuaScript/Engine/ScriptDirective.cs b/LuaScript/Engine/ScriptDirective.cs
index fbadb09..80d7844 100644
--- a/LuaScript/Engine/ScriptDirective.cs
+++ b/LuaScript/Engine/ScriptDirective.cs
@@ -21,7 +21,6 @@ namespace LuaScript.Engine
public static bool UsesDrawingApi(string? script) =>
script is not null && (
- script.Contains("obj.draw", StringComparison.Ordinal) ||
script.Contains("obj.setoption", StringComparison.Ordinal) ||
script.Contains("obj.copybuffer", StringComparison.Ordinal));
diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs
index 6e65ce1..fb0241b 100644
--- a/LuaScript/LuaScriptEffectProcessor.cs
+++ b/LuaScript/LuaScriptEffectProcessor.cs
@@ -447,6 +447,7 @@ namespace LuaScript
(tag, frame) => ctx.ResolveObject(tag, frame, out var info) ? info : null,
NativeLoadFigure,
(name, args) => ctx.AddEffect(new AviUtlEffectRequest(name, args)),
+ ctx.AddDraw,
out bool dirty, out bool bufferReplaced, out byte[]? newPixels,
out int resultW, out int resultH, out string? error);
@@ -459,6 +460,8 @@ namespace LuaScript
NativeFieldMap.FromFields(_nativeFields, ctx);
+ bool hasDraws = ctx.DrawCommands.Count > 0;
+
if (dirty)
{
int outW = bufferReplaced ? resultW : imgW;
@@ -471,6 +474,12 @@ namespace LuaScript
ctx.ReplaceBuffer(outPixels, outW, outH);
}
+ if (hasDraws)
+ {
+ ctx.SetResolvedBuffer(outPixels, outW, outH);
+ return true;
+ }
+
_pixelManager!.WritePixelsToOutput(outPixels, outW, outH);
if (bufferReplaced)
effectOutput = _pixelManager.GetTransformOutput(-outW / 2f, -outH / 2f);
@@ -479,6 +488,12 @@ namespace LuaScript
return true;
}
+ if (hasDraws)
+ {
+ ctx.SetResolvedBuffer(buffer, imgW, imgH);
+ return false;
+ }
+
effectOutput = null;
return false;
}
diff --git a/native/worker.lua b/native/worker.lua
index 3c2f415..ffc6b7f 100644
--- a/native/worker.lua
+++ b/native/worker.lua
@@ -61,6 +61,7 @@ local STATUS_CALLBACK = 3
local CB_KIND_GETOBJECT = 0
local CB_KIND_LOADFIGURE = 1
local CB_KIND_EFFECT = 2
+local CB_KIND_DRAW = 3
assert(loadfile(shimPath))()
@@ -227,6 +228,19 @@ function obj.effect(name, ...)
k32.WaitForSingleObject(workEvent, INFINITE)
end
+function obj.draw(ox, oy, oz, zoom, alpha, aspect)
+ cbResult[0] = ox or 0
+ cbResult[1] = oy or 0
+ cbResult[2] = oz or 0
+ cbResult[3] = zoom or 1
+ cbResult[4] = alpha or 1
+ cbResult[5] = aspect or 0
+ i32[OFF_CB_KIND] = CB_KIND_DRAW
+ i32[OFF_STATUS] = STATUS_CALLBACK
+ k32.SetEvent(doneEvent)
+ k32.WaitForSingleObject(workEvent, INFINITE)
+end
+
local cacheTag, cacheFrame, cacheFound
local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer