obj.drawをネイティブエンジンへ対応

このコミットが含まれているのは:
routersys committed 2026-06-29 08:16:51 +09:00
1 親 90fe288f66
コミット 3ba77aab18
9ファイル変更
+96 -16

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -31,6 +31,7 @@
<Compile Include="..\LuaScript\Engine\NativeProtocol.cs" Link="Linked\NativeProtocol.cs" />
<Compile Include="..\LuaScript\Engine\ProcessJobObject.cs" Link="Linked\ProcessJobObject.cs" />
<Compile Include="..\LuaScript\Engine\LuaJitWorker.cs" Link="Linked\LuaJitWorker.cs" />
<Compile Include="..\LuaScript\DrawCommand.cs" Link="Linked\DrawCommand.cs" />
<Compile Include="..\LuaScript\SceneObjectInfo.cs" Link="Linked\SceneObjectInfo.cs" />
<Compile Include="..\LuaScript\SceneObjectQuery.cs" Link="Linked\SceneObjectQuery.cs" />
<Compile Include="..\LuaScript\AnimTableRegistrar.cs" Link="Linked\AnimTableRegistrar.cs" />
+34 -12
ファイルの表示
@@ -11,6 +11,7 @@ namespace LuaScript.Tests
private static readonly Func<string, int, SceneObjectInfo?> NoResolver = (_, _) => null;
private static readonly Func<string, int, double, double, double, (byte[] buffer, int w, int h)> NoLoadFigure = (_, _, _, _, _) => ([], 1, 1);
private static readonly Action<string, System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>>> NoAddEffect = (_, _) => { };
private static readonly Action<DrawCommand> 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<DrawCommand>();
Action<DrawCommand> 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]);
}
+1 -1
ファイルの表示
@@ -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)
{
+8
ファイルの表示
@@ -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;
+22 -2
ファイルの表示
@@ -50,6 +50,7 @@ namespace LuaScript.Engine
Func<string, int, SceneObjectInfo?> resolveObject,
Func<string, int, double, double, double, (byte[] buffer, int w, int h)> loadFigure,
Action<string, IReadOnlyList<KeyValuePair<string, object>>> addEffect,
Action<DrawCommand> 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<string, int, SceneObjectInfo?> resolveObject,
Func<string, int, double, double, double, (byte[] buffer, int w, int h)> loadFigure,
Action<string, IReadOnlyList<KeyValuePair<string, object>>> addEffect)
Action<string, IReadOnlyList<KeyValuePair<string, object>>> addEffect,
Action<DrawCommand> 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<DrawCommand> 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<string, int, SceneObjectInfo?> resolveObject)
{
int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame);
+1
ファイルの表示
@@ -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;
-1
ファイルの表示
@@ -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));
+15
ファイルの表示
@@ -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;
}
+14
ファイルの表示
@@ -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