getobjectをネイティブ実行でも利用可能にした

このコミットが含まれているのは:
routersys committed 2026-06-27 17:55:47 +09:00
1 親 876f05d277
コミット 41defa1059
6ファイル変更
+163 -17

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

+2
ファイルの表示
@@ -28,7 +28,9 @@
<Compile Include="..\LuaScript\Compat\Bit32.cs" Link="Linked\Bit32.cs" />
<Compile Include="..\LuaScript\Engine\ScriptEngineKind.cs" Link="Linked\ScriptEngineKind.cs" />
<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\SceneObjectInfo.cs" Link="Linked\SceneObjectInfo.cs" />
<Compile Include="..\LuaScript\AnimTableRegistrar.cs" Link="Linked\AnimTableRegistrar.cs" />
</ItemGroup>
+57 -8
ファイルの表示
@@ -8,6 +8,8 @@ namespace LuaScript.Tests
{
private static string NativeDir => Path.Combine(AppContext.BaseDirectory, "native");
private static readonly Func<string, int, SceneObjectInfo?> NoResolver = (_, _) => null;
private readonly LuaJitWorker _worker = new(NativeDir);
public void Dispose() => _worker.Dispose();
@@ -32,7 +34,7 @@ namespace LuaScript.Tests
bool ok = _worker.Execute(
"obj.rz = obj.time * 90\nobj.alpha = 128",
fields, pixels, 4, 4, 5000, out bool dirty, out string? error);
fields, pixels, 4, 4, 5000, NoResolver, out bool dirty, out string? error);
Assert.True(ok, error);
Assert.False(dirty);
@@ -67,7 +69,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, out bool dirty, out string? error);
bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, out bool dirty, out string? error);
Assert.True(ok, error);
Assert.True(dirty);
@@ -102,7 +104,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, out bool dirty, out string? error);
bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, out bool dirty, out string? error);
Assert.True(ok, error);
Assert.True(dirty);
@@ -138,7 +140,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, out _, out string? error);
bool ok = _worker.Execute("obj.x = obj.time * 10", fields, pixels, 2, 2, 5000, NoResolver, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(k * 10d, fields[NativeProtocol.X]);
}
@@ -153,16 +155,63 @@ 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, out _, out string? error);
"local x=0 while true do x=x+1 end", fields, pixels, 2, 2, 1500, NoResolver, 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, out _, out error);
bool ok = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, out _, out error);
Assert.True(ok, error);
Assert.Equal(3d, fields2[NativeProtocol.X]);
}
[Fact]
public void GetObject_ResolvesThroughCallback()
{
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
var pixels = new byte[16];
var fields = Fields(2, 2, 0d);
var resolved = new SceneObjectInfo("a", true, 12d, 34d, 56d, 2d, 90d, 200d, 7);
Func<string, int, SceneObjectInfo?> resolver = (tag, frame) =>
tag == "a" && frame == 5 ? resolved : null;
const string script =
"local o = obj.getobject(\"a\", 5) " +
"obj.x = o.x obj.y = o.y obj.z = o.z " +
"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, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(12d, fields[NativeProtocol.X]);
Assert.Equal(34d, fields[NativeProtocol.Y]);
Assert.Equal(56d, fields[NativeProtocol.Z]);
Assert.Equal(2d, fields[NativeProtocol.Zoom]);
Assert.Equal(90d, fields[NativeProtocol.Rz]);
Assert.Equal(200d, fields[NativeProtocol.Alpha]);
Assert.Equal(2d, fields[NativeProtocol.Sx]);
Assert.Equal(90d * Math.PI / 180d, fields[NativeProtocol.Rzr]);
}
[Fact]
public void GetObject_ReturnsNil_WhenUnresolved()
{
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
var pixels = new byte[16];
var fields = Fields(2, 2, 0d);
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, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(1d, fields[NativeProtocol.X]);
}
[Fact]
public void RuntimeError_IsReported_AndWorkerSurvives()
{
@@ -171,12 +220,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, out _, out string? error);
bool ok = _worker.Execute("obj.x = missing.value", fields, pixels, 2, 2, 5000, NoResolver, 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, out _, out error);
bool ok2 = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, out _, out error);
Assert.True(ok2, error);
Assert.Equal(7d, fields2[NativeProtocol.X]);
}
+49 -5
ファイルの表示
@@ -41,6 +41,7 @@ namespace LuaScript.Engine
int width,
int height,
int timeoutMs,
Func<string, int, SceneObjectInfo?> resolveObject,
out bool pixelsDirty,
out string? error)
{
@@ -70,14 +71,26 @@ namespace LuaScript.Engine
_workEvent!.Set();
if (!_doneEvent!.WaitOne(timeoutMs))
var stopwatch = Stopwatch.StartNew();
int status;
while (true)
{
KillWorker();
error = "native script execution timed out";
return false;
long remaining = timeoutMs - stopwatch.ElapsedMilliseconds;
if (remaining <= 0 || !_doneEvent!.WaitOne((int)remaining))
{
KillWorker();
error = "native script execution timed out";
return false;
}
status = view.ReadInt32(NativeProtocol.OffStatus);
if (status != NativeProtocol.StatusCallback)
break;
ResolveCallback(view, resolveObject);
_workEvent.Set();
}
int status = view.ReadInt32(NativeProtocol.OffStatus);
if (status == NativeProtocol.StatusError)
{
int len = view.ReadInt32(NativeProtocol.OffErrorLen);
@@ -97,6 +110,37 @@ namespace LuaScript.Engine
return true;
}
private void ResolveCallback(MemoryMappedViewAccessor view, Func<string, int, SceneObjectInfo?> resolveObject)
{
int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame);
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
var tagBytes = new byte[tagLen];
view.ReadArray(NativeProtocol.CallbackTagOffset, tagBytes, 0, tagLen);
string tag = Encoding.UTF8.GetString(tagBytes);
SceneObjectInfo? info;
try { info = resolveObject(tag, frame); }
catch { info = null; }
if (info is { } value)
{
long result = NativeProtocol.CallbackResultOffset;
view.Write(result + NativeProtocol.CbExist * 8, value.Exist ? 1d : 0d);
view.Write(result + NativeProtocol.CbX * 8, value.X);
view.Write(result + NativeProtocol.CbY * 8, value.Y);
view.Write(result + NativeProtocol.CbZ * 8, value.Z);
view.Write(result + NativeProtocol.CbZoom * 8, value.Zoom);
view.Write(result + NativeProtocol.CbRz * 8, value.Rz);
view.Write(result + NativeProtocol.CbAlpha * 8, value.Alpha);
view.Write(result + NativeProtocol.CbLayer * 8, (double)value.Layer);
view.Write(NativeProtocol.OffCallbackFound, 1);
}
else
{
view.Write(NativeProtocol.OffCallbackFound, 0);
}
}
private void EnsureWorker(int width, int height)
{
if (_alive && _process is { HasExited: false } && _width == width && _height == height)
+19 -1
ファイルの表示
@@ -10,6 +10,7 @@ namespace LuaScript.Engine
public const int StatusIdle = 0;
public const int StatusOk = 1;
public const int StatusError = 2;
public const int StatusCallback = 3;
public const int OffMagic = 0;
public const int OffCommand = 4;
@@ -19,6 +20,9 @@ namespace LuaScript.Engine
public const int OffScriptLen = 20;
public const int OffPixelsDirty = 24;
public const int OffErrorLen = 28;
public const int OffCallbackFrame = 32;
public const int OffCallbackTagLen = 36;
public const int OffCallbackFound = 40;
public const int HeaderSize = 64;
@@ -31,7 +35,21 @@ namespace LuaScript.Engine
public const int ErrorOffset = ScriptOffset + ScriptMax;
public const int ErrorMax = 4 * 1024;
public const int PixelOffset = ErrorOffset + ErrorMax;
public const int CallbackTagOffset = ErrorOffset + ErrorMax;
public const int CallbackTagMax = 256;
public const int CallbackResultOffset = CallbackTagOffset + CallbackTagMax;
public const int CallbackResultCount = 8;
public const int CbExist = 0;
public const int CbX = 1;
public const int CbY = 2;
public const int CbZ = 3;
public const int CbZoom = 4;
public const int CbRz = 5;
public const int CbAlpha = 6;
public const int CbLayer = 7;
public const int PixelOffset = CallbackResultOffset + CallbackResultCount * 8;
public const int W = 0;
public const int H = 1;
+3 -1
ファイルの表示
@@ -365,7 +365,9 @@ namespace LuaScript
byte[] buffer = LoadInputPixels(bounds, imgW, imgH);
bool ok = _nativeWorker.Execute(
script, _nativeFields, buffer, imgW, imgH, NativeTimeoutMilliseconds, out bool dirty, out string? error);
script, _nativeFields, buffer, imgW, imgH, NativeTimeoutMilliseconds,
(tag, frame) => ctx.ResolveObject(tag, frame, out var info) ? info : null,
out bool dirty, out string? error);
if (!ok)
{
+33 -2
ファイルの表示
@@ -43,11 +43,18 @@ local OFF_HEIGHT = 4
local OFF_SCRIPTLEN = 5
local OFF_PIXELSDIRTY = 6
local OFF_ERRORLEN = 7
local OFF_CB_FRAME = 8
local OFF_CB_TAGLEN = 9
local OFF_CB_FOUND = 10
local SCRIPT_OFFSET = 64 + 64 * 8
local ERROR_OFFSET = SCRIPT_OFFSET + 128 * 1024
local PIXEL_OFFSET = ERROR_OFFSET + 4 * 1024
local CB_TAG_OFFSET = ERROR_OFFSET + 4 * 1024
local CB_TAG_MAX = 256
local CB_RESULT_OFFSET = CB_TAG_OFFSET + CB_TAG_MAX
local PIXEL_OFFSET = CB_RESULT_OFFSET + 8 * 8
local FIRST_WRITABLE = 8
local LAST_WRITABLE = 25
local STATUS_CALLBACK = 3
assert(loadfile(shimPath))()
@@ -59,6 +66,7 @@ end
local width, height = 0, 0
local pixels = ffi.cast("uint8_t*", base + PIXEL_OFFSET)
local cbResult = ffi.cast("double*", base + CB_RESULT_OFFSET)
local dirty = false
local obj = {}
@@ -137,7 +145,30 @@ function obj.getpixeldata()
end
function obj.putpixeldata() end
function obj.getobject() return nil end
function obj.getobject(tag, frame)
if type(tag) ~= "string" then return nil end
if frame then frame = math.floor(frame) else frame = math.floor(obj.timelineframe or 0) end
local len = #tag
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, tag, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_FRAME] = frame
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
if i32[OFF_CB_FOUND] == 0 then return nil end
local rz = cbResult[5]
local zoom = cbResult[4]
return {
exist = cbResult[0] ~= 0,
x = cbResult[1], y = cbResult[2], z = cbResult[3],
zoom = zoom, sx = zoom, sy = zoom,
rx = 0, ry = 0, rz = rz,
rxr = 0, ryr = 0, rzr = rz * math.pi / 180,
alpha = cbResult[6], layer = cbResult[7],
}
end
local sandbox = {
obj = obj, scene = scene, ymm4 = ymm4,