LuaScript.Runtime へ実行コアを分離し依存を逆転
このコミットが含まれているのは:
41ファイル変更
+64
-19
検索条件に一致するファイルはありません
@@ -1,76 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace LuaScript.Engine.Kernel
|
||||
{
|
||||
internal static class KernelUniformBinding
|
||||
{
|
||||
public static double[] Create() => new double[KernelUniforms.Count];
|
||||
|
||||
public static void Fill(AviUtlScriptContext ctx, double[] values)
|
||||
{
|
||||
values[(int)KernelUniform.Width] = ctx.ImageWidth;
|
||||
values[(int)KernelUniform.Height] = ctx.ImageHeight;
|
||||
values[(int)KernelUniform.HalfWidth] = ctx.ImageWidth / 2d;
|
||||
values[(int)KernelUniform.HalfHeight] = ctx.ImageHeight / 2d;
|
||||
values[(int)KernelUniform.CenterX] = ctx.ImageWidth / 2d;
|
||||
values[(int)KernelUniform.CenterY] = ctx.ImageHeight / 2d;
|
||||
values[(int)KernelUniform.CenterZ] = 0d;
|
||||
values[(int)KernelUniform.Diagonal] = Math.Sqrt((double)ctx.ImageWidth * ctx.ImageWidth + (double)ctx.ImageHeight * ctx.ImageHeight);
|
||||
values[(int)KernelUniform.X] = ctx.X;
|
||||
values[(int)KernelUniform.Y] = ctx.Y;
|
||||
values[(int)KernelUniform.Z] = ctx.Z;
|
||||
values[(int)KernelUniform.Ox] = ctx.Ox;
|
||||
values[(int)KernelUniform.Oy] = ctx.Oy;
|
||||
values[(int)KernelUniform.Oz] = ctx.Oz;
|
||||
values[(int)KernelUniform.Sx] = ctx.Sx;
|
||||
values[(int)KernelUniform.Sy] = ctx.Sy;
|
||||
values[(int)KernelUniform.Sz] = 1d;
|
||||
values[(int)KernelUniform.Zoom] = ctx.Zoom;
|
||||
values[(int)KernelUniform.Aspect] = ctx.Aspect;
|
||||
values[(int)KernelUniform.Alpha] = ctx.Alpha;
|
||||
values[(int)KernelUniform.Rx] = ctx.Rx;
|
||||
values[(int)KernelUniform.Ry] = ctx.Ry;
|
||||
values[(int)KernelUniform.Rz] = ctx.Rz;
|
||||
values[(int)KernelUniform.Rxr] = ctx.RxRad;
|
||||
values[(int)KernelUniform.Ryr] = ctx.RyRad;
|
||||
values[(int)KernelUniform.Rzr] = ctx.RzRad;
|
||||
values[(int)KernelUniform.Track0] = ctx.Track0;
|
||||
values[(int)KernelUniform.Track1] = ctx.Track1;
|
||||
values[(int)KernelUniform.Track2] = ctx.Track2;
|
||||
values[(int)KernelUniform.Track3] = ctx.Track3;
|
||||
values[(int)KernelUniform.Slider0] = ctx.Slider0;
|
||||
values[(int)KernelUniform.Slider1] = ctx.Slider1;
|
||||
values[(int)KernelUniform.Slider2] = ctx.Slider2;
|
||||
values[(int)KernelUniform.Slider3] = ctx.Slider3;
|
||||
values[(int)KernelUniform.Check0] = ctx.Check0 ? 1d : 0d;
|
||||
values[(int)KernelUniform.Check1] = ctx.Check1 ? 1d : 0d;
|
||||
values[(int)KernelUniform.Check2] = ctx.Check2 ? 1d : 0d;
|
||||
values[(int)KernelUniform.Check3] = ctx.Check3 ? 1d : 0d;
|
||||
values[(int)KernelUniform.Color] = ctx.HasColor ? ctx.ColorValue : -1d;
|
||||
values[(int)KernelUniform.Time] = ctx.Time;
|
||||
values[(int)KernelUniform.Frame] = ctx.Frame;
|
||||
values[(int)KernelUniform.TotalFrame] = ctx.TotalFrame;
|
||||
values[(int)KernelUniform.TotalTime] = ctx.TotalTime;
|
||||
values[(int)KernelUniform.T] = ctx.TotalFrame > 0 ? ctx.Frame / (double)ctx.TotalFrame : 0d;
|
||||
values[(int)KernelUniform.Framerate] = ctx.Framerate;
|
||||
values[(int)KernelUniform.Layer] = ctx.Layer;
|
||||
values[(int)KernelUniform.Index] = ctx.Index;
|
||||
values[(int)KernelUniform.Num] = ctx.Num;
|
||||
values[(int)KernelUniform.SceneWidth] = ctx.SceneWidth;
|
||||
values[(int)KernelUniform.SceneHeight] = ctx.SceneHeight;
|
||||
values[(int)KernelUniform.SceneCenterX] = ctx.SceneWidth / 2d;
|
||||
values[(int)KernelUniform.SceneCenterY] = ctx.SceneHeight / 2d;
|
||||
values[(int)KernelUniform.TimelineFrame] = ctx.TimelineFrame;
|
||||
values[(int)KernelUniform.TimelineTime] = ctx.TimelineTime;
|
||||
}
|
||||
|
||||
public static void Pack(double[] values, float[] packed)
|
||||
{
|
||||
int count = Math.Min(values.Length, packed.Length);
|
||||
for (int i = 0; i < count; i++)
|
||||
packed[i] = (float)values[i];
|
||||
for (int i = count; i < packed.Length; i++)
|
||||
packed[i] = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,728 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal sealed class LuaJitWorker : IDisposable
|
||||
{
|
||||
private readonly string _luajitPath;
|
||||
private readonly string _workerScript;
|
||||
private readonly string _shimScript;
|
||||
private readonly string _nativeDir;
|
||||
|
||||
private MemoryMappedFile? _mmf;
|
||||
private MemoryMappedViewAccessor? _view;
|
||||
private EventWaitHandle? _workEvent;
|
||||
private EventWaitHandle? _doneEvent;
|
||||
private Process? _process;
|
||||
private int _stringParamsCapacity = NativeProtocol.MinStringParamsCapacity;
|
||||
private long _pixelOffset = NativeProtocol.PixelOffset(NativeProtocol.MinStringParamsCapacity);
|
||||
private long _allocatedSize;
|
||||
private bool _alive;
|
||||
|
||||
private readonly byte[] _callbackTag = new byte[NativeProtocol.CallbackTagMax];
|
||||
private readonly byte[] _lastCallbackTag = new byte[NativeProtocol.CallbackTagMax];
|
||||
private readonly double[] _anchorBuffer = new double[Anchor.AnchorSupport.MaxAnchors * 3];
|
||||
private int _lastCallbackTagLength = -1;
|
||||
private string _lastCallbackTagText = string.Empty;
|
||||
private int _scriptVersion;
|
||||
private string? _lastScript;
|
||||
private byte[] _lastScriptBytes = Array.Empty<byte>();
|
||||
private bool _scriptWritten;
|
||||
private readonly Dictionary<string, byte[]> _stringNameBytes = new(StringComparer.Ordinal);
|
||||
private byte[] _stringValueBytes = new byte[256];
|
||||
private byte[] _sceneValueBytes = new byte[256];
|
||||
|
||||
public LuaJitWorker(string nativeDir)
|
||||
{
|
||||
_nativeDir = nativeDir;
|
||||
_luajitPath = Path.Combine(nativeDir, "luajit.exe");
|
||||
_workerScript = Path.Combine(nativeDir, "worker.lua");
|
||||
_shimScript = Path.Combine(nativeDir, "shim.lua");
|
||||
}
|
||||
|
||||
public static bool IsAvailable(string nativeDir) =>
|
||||
File.Exists(Path.Combine(nativeDir, "luajit.exe"));
|
||||
|
||||
public bool Execute(
|
||||
string script,
|
||||
double[] fields,
|
||||
IReadOnlyDictionary<string, string> stringParameters,
|
||||
PixelRegionAccess loadPixels,
|
||||
int width,
|
||||
int height,
|
||||
int timeoutMs,
|
||||
Func<string, int, SceneObjectInfo?> resolveObject,
|
||||
Func<string, int, double, double, double, (byte[] buffer, int w, int h)> loadFigure,
|
||||
Func<string, string, double, bool, bool, int, (byte[] buffer, int w, int h)> loadText,
|
||||
Func<string, (byte[] buffer, int w, int h)> loadImage,
|
||||
Func<string, double, (byte[] buffer, int w, int h)> loadMovie,
|
||||
Action<string, IReadOnlyList<KeyValuePair<string, object>>> addEffect,
|
||||
Action<DrawCommand> addDraw,
|
||||
Action<string, int, bool, int, double[]> setAnchor,
|
||||
Func<string, SceneValue> sceneGetValue,
|
||||
Action<string, SceneValue> sceneSetValue,
|
||||
out bool pixelsDirty,
|
||||
out bool bufferReplaced,
|
||||
out int resultWidth,
|
||||
out int resultHeight,
|
||||
out string? error)
|
||||
{
|
||||
pixelsDirty = false;
|
||||
bufferReplaced = false;
|
||||
resultWidth = width;
|
||||
resultHeight = height;
|
||||
error = null;
|
||||
|
||||
bool scriptChanged = !ReferenceEquals(script, _lastScript);
|
||||
if (scriptChanged)
|
||||
{
|
||||
byte[] scriptBytes = Encoding.UTF8.GetBytes(script);
|
||||
if (scriptBytes.Length > NativeProtocol.ScriptMax)
|
||||
{
|
||||
error = "script too large for native lane";
|
||||
return false;
|
||||
}
|
||||
_lastScript = script;
|
||||
_lastScriptBytes = scriptBytes;
|
||||
_scriptVersion++;
|
||||
}
|
||||
|
||||
int stringCapacity = ResolveStringCapacity(_stringParamsCapacity, MeasureStringParameters(stringParameters));
|
||||
EnsureWorker(width, height, stringCapacity);
|
||||
var view = _view!;
|
||||
|
||||
view.WriteArray(NativeProtocol.FieldsOffset, fields, 0, NativeProtocol.FieldCount);
|
||||
WriteStringParameters(view, stringParameters);
|
||||
view.Write(NativeProtocol.OffWidth, width);
|
||||
view.Write(NativeProtocol.OffHeight, height);
|
||||
if (scriptChanged || !_scriptWritten)
|
||||
{
|
||||
view.Write(NativeProtocol.OffScriptLen, _lastScriptBytes.Length);
|
||||
view.WriteArray(NativeProtocol.ScriptOffset, _lastScriptBytes, 0, _lastScriptBytes.Length);
|
||||
_scriptWritten = true;
|
||||
}
|
||||
view.Write(NativeProtocol.OffScriptVersion, _scriptVersion);
|
||||
view.Write(NativeProtocol.OffPixelsDirty, 0);
|
||||
view.Write(NativeProtocol.OffErrorLen, 0);
|
||||
view.Write(NativeProtocol.OffStatus, NativeProtocol.StatusIdle);
|
||||
view.Write(NativeProtocol.OffCommand, NativeProtocol.CmdRun);
|
||||
|
||||
_workEvent!.Set();
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
int status;
|
||||
while (true)
|
||||
{
|
||||
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;
|
||||
|
||||
int callbackKind = view.ReadInt32(NativeProtocol.OffCallbackKind);
|
||||
if (callbackKind == NativeProtocol.CbKindRequestPixels)
|
||||
{
|
||||
try
|
||||
{
|
||||
AccessPixelRegion(loadPixels);
|
||||
}
|
||||
catch
|
||||
{
|
||||
KillWorker();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
else if (callbackKind == NativeProtocol.CbKindFlushDraws)
|
||||
{
|
||||
DrainDrawRing(view, addDraw);
|
||||
}
|
||||
else
|
||||
{
|
||||
DispatchCallback(view, resolveObject, loadFigure, loadText, loadImage, loadMovie, addEffect, setAnchor, sceneGetValue, sceneSetValue);
|
||||
}
|
||||
_workEvent.Set();
|
||||
}
|
||||
|
||||
if (status == NativeProtocol.StatusError)
|
||||
{
|
||||
int len = view.ReadInt32(NativeProtocol.OffErrorLen);
|
||||
var buffer = new byte[Math.Clamp(len, 0, NativeProtocol.ErrorMax)];
|
||||
view.ReadArray(NativeProtocol.ErrorOffset, buffer, 0, buffer.Length);
|
||||
error = Encoding.UTF8.GetString(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
DrainDrawRing(view, addDraw);
|
||||
|
||||
for (int i = NativeProtocol.FirstWritableField; i <= NativeProtocol.LastWritableField; i++)
|
||||
fields[i] = view.ReadDouble(NativeProtocol.FieldsOffset + i * 8);
|
||||
|
||||
fields[NativeProtocol.DrawState] = view.ReadDouble(NativeProtocol.FieldsOffset + NativeProtocol.DrawState * 8);
|
||||
|
||||
resultWidth = view.ReadInt32(NativeProtocol.OffWidth);
|
||||
resultHeight = view.ReadInt32(NativeProtocol.OffHeight);
|
||||
bufferReplaced = resultWidth != width || resultHeight != height;
|
||||
|
||||
pixelsDirty = view.ReadInt32(NativeProtocol.OffPixelsDirty) != 0;
|
||||
if (pixelsDirty && (long)resultWidth * resultHeight * 4 > _allocatedSize - _pixelOffset)
|
||||
{
|
||||
error = "native worker reported a pixel buffer beyond capacity";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ReadPixels(PixelRegionAccess reader) => AccessPixelRegion(reader);
|
||||
|
||||
private unsafe void AccessPixelRegion(PixelRegionAccess access)
|
||||
{
|
||||
var handle = _view!.SafeMemoryMappedViewHandle;
|
||||
byte* ptr = null;
|
||||
handle.AcquirePointer(ref ptr);
|
||||
try
|
||||
{
|
||||
long offset = _view.PointerOffset + _pixelOffset;
|
||||
access((nint)(ptr + offset), (long)handle.ByteLength - offset);
|
||||
}
|
||||
finally
|
||||
{
|
||||
handle.ReleasePointer();
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void WritePixelRegion(byte[] source, int length)
|
||||
{
|
||||
var handle = _view!.SafeMemoryMappedViewHandle;
|
||||
byte* ptr = null;
|
||||
handle.AcquirePointer(ref ptr);
|
||||
try
|
||||
{
|
||||
fixed (byte* src = source)
|
||||
Buffer.MemoryCopy(src, ptr + _view.PointerOffset + _pixelOffset, handle.ByteLength - (ulong)(_view.PointerOffset + _pixelOffset), (ulong)length);
|
||||
}
|
||||
finally
|
||||
{
|
||||
handle.ReleasePointer();
|
||||
}
|
||||
}
|
||||
|
||||
private static int MeasureStringParameters(IReadOnlyDictionary<string, string> stringParameters)
|
||||
{
|
||||
int total = 4;
|
||||
foreach (var pair in stringParameters)
|
||||
total += 8 + Encoding.UTF8.GetByteCount(pair.Key) + Encoding.UTF8.GetByteCount(pair.Value ?? string.Empty);
|
||||
return total;
|
||||
}
|
||||
|
||||
private static int ResolveStringCapacity(int current, int required)
|
||||
{
|
||||
int capacity = current;
|
||||
while (capacity < required)
|
||||
{
|
||||
if (capacity > int.MaxValue / 2)
|
||||
{
|
||||
capacity = required;
|
||||
break;
|
||||
}
|
||||
capacity *= 2;
|
||||
}
|
||||
return capacity;
|
||||
}
|
||||
|
||||
private void WriteStringParameters(MemoryMappedViewAccessor view, IReadOnlyDictionary<string, string> stringParameters)
|
||||
{
|
||||
long basePos = NativeProtocol.StringParamsOffset;
|
||||
long limit = basePos + _stringParamsCapacity;
|
||||
long pos = basePos + 4;
|
||||
int count = 0;
|
||||
foreach (var pair in stringParameters)
|
||||
{
|
||||
if (!_stringNameBytes.TryGetValue(pair.Key, out var name))
|
||||
{
|
||||
name = Encoding.UTF8.GetBytes(pair.Key);
|
||||
_stringNameBytes[pair.Key] = name;
|
||||
}
|
||||
string value = pair.Value ?? string.Empty;
|
||||
int maxValueBytes = Encoding.UTF8.GetMaxByteCount(value.Length);
|
||||
if (_stringValueBytes.Length < maxValueBytes)
|
||||
_stringValueBytes = new byte[Math.Max(maxValueBytes, _stringValueBytes.Length * 2)];
|
||||
int valueLen = Encoding.UTF8.GetBytes(value, 0, value.Length, _stringValueBytes, 0);
|
||||
if (pos + 8 + name.Length + valueLen > limit)
|
||||
break;
|
||||
view.Write(pos, name.Length); pos += 4;
|
||||
view.WriteArray(pos, name, 0, name.Length); pos += name.Length;
|
||||
view.Write(pos, valueLen); pos += 4;
|
||||
view.WriteArray(pos, _stringValueBytes, 0, valueLen); pos += valueLen;
|
||||
count++;
|
||||
}
|
||||
view.Write(basePos, count);
|
||||
view.Write(NativeProtocol.OffStringParamsLen, (int)(pos - basePos));
|
||||
}
|
||||
|
||||
private void DispatchCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, int, SceneObjectInfo?> resolveObject,
|
||||
Func<string, int, double, double, double, (byte[] buffer, int w, int h)> loadFigure,
|
||||
Func<string, string, double, bool, bool, int, (byte[] buffer, int w, int h)> loadText,
|
||||
Func<string, (byte[] buffer, int w, int h)> loadImage,
|
||||
Func<string, double, (byte[] buffer, int w, int h)> loadMovie,
|
||||
Action<string, IReadOnlyList<KeyValuePair<string, object>>> addEffect,
|
||||
Action<string, int, bool, int, double[]> setAnchor,
|
||||
Func<string, SceneValue> sceneGetValue,
|
||||
Action<string, SceneValue> sceneSetValue)
|
||||
{
|
||||
int kind = view.ReadInt32(NativeProtocol.OffCallbackKind);
|
||||
switch (kind)
|
||||
{
|
||||
case NativeProtocol.CbKindGetObject:
|
||||
ResolveGetObjectCallback(view, resolveObject);
|
||||
break;
|
||||
case NativeProtocol.CbKindLoadFigure:
|
||||
ResolveLoadFigureCallback(view, loadFigure);
|
||||
break;
|
||||
case NativeProtocol.CbKindLoadText:
|
||||
ResolveLoadTextCallback(view, loadText);
|
||||
break;
|
||||
case NativeProtocol.CbKindLoadImage:
|
||||
ResolveLoadImageCallback(view, loadImage);
|
||||
break;
|
||||
case NativeProtocol.CbKindLoadMovie:
|
||||
ResolveLoadMovieCallback(view, loadMovie);
|
||||
break;
|
||||
case NativeProtocol.CbKindEffect:
|
||||
ResolveEffectCallback(view, addEffect);
|
||||
break;
|
||||
case NativeProtocol.CbKindSetAnchor:
|
||||
ResolveSetAnchorCallback(view, setAnchor);
|
||||
break;
|
||||
case NativeProtocol.CbKindSceneGet:
|
||||
ResolveSceneGetCallback(view, sceneGetValue);
|
||||
break;
|
||||
case NativeProtocol.CbKindSceneSet:
|
||||
ResolveSceneSetCallback(view, sceneSetValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveSceneGetCallback(MemoryMappedViewAccessor view, Func<string, SceneValue> getValue)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string name = ResolveTag(tagLen);
|
||||
|
||||
SceneValue value;
|
||||
try { value = getValue(name); }
|
||||
catch { value = SceneValue.Nil; }
|
||||
|
||||
long rOff = NativeProtocol.CallbackResultOffset;
|
||||
view.Write(rOff + 0 * 8, (double)value.Kind);
|
||||
view.Write(rOff + 1 * 8, value.Number);
|
||||
if (value.Kind == SceneValueKind.String)
|
||||
{
|
||||
string text = value.Text ?? string.Empty;
|
||||
int maxBytes = Encoding.UTF8.GetMaxByteCount(text.Length);
|
||||
if (_sceneValueBytes.Length < maxBytes)
|
||||
_sceneValueBytes = new byte[Math.Max(maxBytes, _sceneValueBytes.Length * 2)];
|
||||
int length = Encoding.UTF8.GetBytes(text, 0, text.Length, _sceneValueBytes, 0);
|
||||
if (length > NativeProtocol.CallbackTagMax)
|
||||
length = NativeProtocol.CallbackTagMax;
|
||||
view.WriteArray(NativeProtocol.CallbackTagOffset, _sceneValueBytes, 0, length);
|
||||
view.Write(NativeProtocol.OffCallbackTagLen, length);
|
||||
}
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveSceneSetCallback(MemoryMappedViewAccessor view, Action<string, SceneValue> setValue)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
|
||||
long rOff = NativeProtocol.CallbackResultOffset;
|
||||
var kind = (SceneValueKind)(int)view.ReadDouble(rOff + 0 * 8);
|
||||
double number = view.ReadDouble(rOff + 1 * 8);
|
||||
|
||||
string name;
|
||||
SceneValue value;
|
||||
if (kind == SceneValueKind.String)
|
||||
{
|
||||
int separator = Array.IndexOf(_callbackTag, (byte)0, 0, tagLen);
|
||||
int nameLength = separator >= 0 ? separator : tagLen;
|
||||
name = Encoding.UTF8.GetString(_callbackTag, 0, nameLength);
|
||||
value = SceneValue.FromString(separator >= 0
|
||||
? Encoding.UTF8.GetString(_callbackTag, separator + 1, tagLen - separator - 1)
|
||||
: string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
name = ResolveTag(tagLen);
|
||||
value = kind switch
|
||||
{
|
||||
SceneValueKind.Number => SceneValue.FromNumber(number),
|
||||
SceneValueKind.Boolean => SceneValue.FromBoolean(number != 0d),
|
||||
_ => SceneValue.Nil,
|
||||
};
|
||||
}
|
||||
|
||||
try { setValue(name, value); }
|
||||
catch { }
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveSetAnchorCallback(MemoryMappedViewAccessor view, Action<string, int, bool, int, double[]> setAnchor)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string group = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
|
||||
|
||||
long rOff = NativeProtocol.CallbackResultOffset;
|
||||
int count = Math.Clamp((int)view.ReadDouble(rOff + 0 * 8), 0, Anchor.AnchorSupport.MaxAnchors);
|
||||
bool is3D = view.ReadDouble(rOff + 1 * 8) != 0d;
|
||||
int connection = (int)view.ReadDouble(rOff + 2 * 8);
|
||||
|
||||
Array.Clear(_anchorBuffer, 0, _anchorBuffer.Length);
|
||||
try { setAnchor(group, count, is3D, connection, _anchorBuffer); }
|
||||
catch { }
|
||||
|
||||
int stride = is3D ? 3 : 2;
|
||||
for (int i = 0; i < count * stride; i++)
|
||||
view.Write(NativeProtocol.CallbackTagOffset + i * 8, _anchorBuffer[i]);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void DrainDrawRing(MemoryMappedViewAccessor view, Action<DrawCommand> addDraw)
|
||||
{
|
||||
long ringBase = NativeProtocol.DrawRingOffset(_stringParamsCapacity);
|
||||
int count = Math.Clamp((int)view.ReadDouble(ringBase), 0, NativeProtocol.DrawRingCapacity);
|
||||
for (int k = 0; k < count; k++)
|
||||
{
|
||||
long entry = ringBase + (1 + (long)k * NativeProtocol.DrawEntryDoubles) * 8;
|
||||
int kind = (int)view.ReadDouble(entry);
|
||||
long payload = entry + 8;
|
||||
if (kind == NativeProtocol.CbKindDrawPoly)
|
||||
DispatchRingDrawPoly(view, payload, addDraw);
|
||||
else
|
||||
DispatchRingDraw(view, payload, addDraw);
|
||||
}
|
||||
view.Write(ringBase, 0d);
|
||||
}
|
||||
|
||||
private static void DispatchRingDraw(MemoryMappedViewAccessor view, long payload, Action<DrawCommand> addDraw)
|
||||
{
|
||||
double ox = view.ReadDouble(payload + 0 * 8);
|
||||
double oy = view.ReadDouble(payload + 1 * 8);
|
||||
double oz = view.ReadDouble(payload + 2 * 8);
|
||||
double zoom = view.ReadDouble(payload + 3 * 8);
|
||||
double alpha = view.ReadDouble(payload + 4 * 8);
|
||||
double aspect = view.ReadDouble(payload + 5 * 8);
|
||||
double antialias = view.ReadDouble(payload + 6 * 8);
|
||||
double blend = view.ReadDouble(payload + 7 * 8);
|
||||
|
||||
try { addDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect, null, antialias, blend)); }
|
||||
catch { }
|
||||
}
|
||||
|
||||
private static void DispatchRingDrawPoly(MemoryMappedViewAccessor view, long payload, Action<DrawCommand> addDraw)
|
||||
{
|
||||
var poly = new double[DrawPolyMath.Length];
|
||||
for (int i = 0; i < DrawPolyMath.Length; i++)
|
||||
poly[i] = view.ReadDouble(payload + i * 8);
|
||||
double antialias = view.ReadDouble(payload + DrawPolyMath.Length * 8);
|
||||
double blend = view.ReadDouble(payload + (DrawPolyMath.Length + 1) * 8);
|
||||
|
||||
try { addDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly, antialias, blend)); }
|
||||
catch { }
|
||||
}
|
||||
|
||||
private void ResolveGetObjectCallback(MemoryMappedViewAccessor view, Func<string, int, SceneObjectInfo?> resolveObject)
|
||||
{
|
||||
int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame);
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string tag = ResolveTag(tagLen);
|
||||
|
||||
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 ResolveLoadFigureCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, int, double, double, double, (byte[] buffer, int w, int h)> loadFigure)
|
||||
{
|
||||
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);
|
||||
|
||||
long rOff = NativeProtocol.CallbackResultOffset;
|
||||
int color = (int)view.ReadDouble(rOff + 0 * 8);
|
||||
double size = view.ReadDouble(rOff + 1 * 8);
|
||||
double lineWidth = view.ReadDouble(rOff + 2 * 8);
|
||||
double aspect = view.ReadDouble(rOff + 3 * 8);
|
||||
|
||||
(byte[] buffer, int w, int h) result;
|
||||
try { result = loadFigure(name, color, size, lineWidth, aspect); }
|
||||
catch { result = (new byte[4], 1, 1); }
|
||||
|
||||
int pixelSize = result.w * result.h * 4;
|
||||
long capacity = view.Capacity - _pixelOffset;
|
||||
if (pixelSize > capacity)
|
||||
{
|
||||
result = (new byte[4], 1, 1);
|
||||
pixelSize = 4;
|
||||
}
|
||||
|
||||
view.Write(NativeProtocol.OffLoadResultWidth, result.w);
|
||||
view.Write(NativeProtocol.OffLoadResultHeight, result.h);
|
||||
WritePixelRegion(result.buffer, pixelSize);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveLoadTextCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, string, double, bool, bool, int, (byte[] buffer, int w, int h)> loadText)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string payload = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
|
||||
int separator = payload.IndexOf('\0');
|
||||
string family = separator >= 0 ? payload[..separator] : string.Empty;
|
||||
string text = separator >= 0 ? payload[(separator + 1)..] : payload;
|
||||
|
||||
long rOff = NativeProtocol.CallbackResultOffset;
|
||||
double size = view.ReadDouble(rOff + 0 * 8);
|
||||
bool bold = view.ReadDouble(rOff + 1 * 8) != 0;
|
||||
bool italic = view.ReadDouble(rOff + 2 * 8) != 0;
|
||||
int color = (int)view.ReadDouble(rOff + 3 * 8);
|
||||
|
||||
(byte[] buffer, int w, int h) result;
|
||||
try { result = loadText(family, text, size, bold, italic, color); }
|
||||
catch { result = (new byte[4], 1, 1); }
|
||||
|
||||
int pixelSize = result.w * result.h * 4;
|
||||
long capacity = view.Capacity - _pixelOffset;
|
||||
if (pixelSize > capacity)
|
||||
{
|
||||
result = (new byte[4], 1, 1);
|
||||
pixelSize = 4;
|
||||
}
|
||||
|
||||
view.Write(NativeProtocol.OffLoadResultWidth, result.w);
|
||||
view.Write(NativeProtocol.OffLoadResultHeight, result.h);
|
||||
WritePixelRegion(result.buffer, pixelSize);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveLoadImageCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, (byte[] buffer, int w, int h)> loadImage)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string path = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
|
||||
|
||||
(byte[] buffer, int w, int h) result;
|
||||
try { result = loadImage(path); }
|
||||
catch { result = (new byte[4], 1, 1); }
|
||||
|
||||
int pixelSize = result.w * result.h * 4;
|
||||
long capacity = view.Capacity - _pixelOffset;
|
||||
if (pixelSize > capacity)
|
||||
{
|
||||
result = (new byte[4], 1, 1);
|
||||
pixelSize = 4;
|
||||
}
|
||||
|
||||
view.Write(NativeProtocol.OffLoadResultWidth, result.w);
|
||||
view.Write(NativeProtocol.OffLoadResultHeight, result.h);
|
||||
WritePixelRegion(result.buffer, pixelSize);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveLoadMovieCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, double, (byte[] buffer, int w, int h)> loadMovie)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
string path = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
|
||||
double time = view.ReadDouble(NativeProtocol.CallbackResultOffset);
|
||||
|
||||
(byte[] buffer, int w, int h) result;
|
||||
try { result = loadMovie(path, time); }
|
||||
catch { result = (new byte[4], 1, 1); }
|
||||
|
||||
int pixelSize = result.w * result.h * 4;
|
||||
long capacity = view.Capacity - _pixelOffset;
|
||||
if (pixelSize > capacity)
|
||||
{
|
||||
result = (new byte[4], 1, 1);
|
||||
pixelSize = 4;
|
||||
}
|
||||
|
||||
view.Write(NativeProtocol.OffLoadResultWidth, result.w);
|
||||
view.Write(NativeProtocol.OffLoadResultHeight, result.h);
|
||||
WritePixelRegion(result.buffer, pixelSize);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveEffectCallback(MemoryMappedViewAccessor view, Action<string, IReadOnlyList<KeyValuePair<string, object>>> 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;
|
||||
|
||||
string effectName = segments[0];
|
||||
var arguments = new List<KeyValuePair<string, object>>();
|
||||
for (int i = 1; i + 1 < segments.Length; i += 2)
|
||||
{
|
||||
string key = segments[i];
|
||||
string raw = segments[i + 1];
|
||||
object value;
|
||||
if (raw.Length > 0 && raw[0] == 'n')
|
||||
value = double.TryParse(raw.AsSpan(1), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var d) ? d : 0d;
|
||||
else if (raw.Length > 0 && raw[0] == 'b')
|
||||
value = raw.Length > 1 && raw[1] == '1';
|
||||
else if (raw.Length > 0 && raw[0] == 's')
|
||||
value = raw.Substring(1);
|
||||
else
|
||||
value = raw;
|
||||
arguments.Add(new KeyValuePair<string, object>(key, value));
|
||||
}
|
||||
|
||||
try { addEffect(effectName, arguments); }
|
||||
catch { }
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private string ResolveTag(int tagLen)
|
||||
{
|
||||
if (tagLen == _lastCallbackTagLength &&
|
||||
_callbackTag.AsSpan(0, tagLen).SequenceEqual(_lastCallbackTag.AsSpan(0, tagLen)))
|
||||
return _lastCallbackTagText;
|
||||
|
||||
_lastCallbackTagText = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
|
||||
_callbackTag.AsSpan(0, tagLen).CopyTo(_lastCallbackTag);
|
||||
_lastCallbackTagLength = tagLen;
|
||||
return _lastCallbackTagText;
|
||||
}
|
||||
|
||||
private void EnsureWorker(int width, int height, int stringCapacity)
|
||||
{
|
||||
long required = NativeProtocol.PixelOffset(stringCapacity) + (long)width * height * 4;
|
||||
if (_alive && _process is { HasExited: false } && _stringParamsCapacity == stringCapacity && required <= _allocatedSize)
|
||||
return;
|
||||
|
||||
KillWorker();
|
||||
StartWorker(width, height, stringCapacity);
|
||||
}
|
||||
|
||||
private void StartWorker(int width, int height, int stringCapacity)
|
||||
{
|
||||
_stringParamsCapacity = stringCapacity;
|
||||
_scriptWritten = false;
|
||||
_pixelOffset = NativeProtocol.PixelOffset(stringCapacity);
|
||||
long size = Math.Max(NativeProtocol.BufferSize(width, height, stringCapacity), _allocatedSize);
|
||||
_allocatedSize = size;
|
||||
|
||||
string suffix = Guid.NewGuid().ToString("N");
|
||||
string mapName = "LuaScriptMap_" + suffix;
|
||||
string workName = "LuaScriptWork_" + suffix;
|
||||
string doneName = "LuaScriptDone_" + suffix;
|
||||
|
||||
_mmf = MemoryMappedFile.CreateNew(mapName, size);
|
||||
_view = _mmf.CreateViewAccessor();
|
||||
_view.Write(NativeProtocol.OffMagic, NativeProtocol.Magic);
|
||||
_workEvent = new EventWaitHandle(false, EventResetMode.AutoReset, workName);
|
||||
_doneEvent = new EventWaitHandle(false, EventResetMode.AutoReset, doneName);
|
||||
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
FileName = _luajitPath,
|
||||
WorkingDirectory = _nativeDir,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
psi.ArgumentList.Add(_workerScript);
|
||||
psi.ArgumentList.Add(mapName);
|
||||
psi.ArgumentList.Add(size.ToString());
|
||||
psi.ArgumentList.Add(workName);
|
||||
psi.ArgumentList.Add(doneName);
|
||||
psi.ArgumentList.Add(_shimScript);
|
||||
psi.ArgumentList.Add(stringCapacity.ToString());
|
||||
|
||||
_process = Process.Start(psi);
|
||||
if (_process is not null)
|
||||
ProcessJobObject.Assign(_process.Handle);
|
||||
_alive = _process is not null;
|
||||
}
|
||||
|
||||
private void KillWorker()
|
||||
{
|
||||
if (_process is { HasExited: false })
|
||||
{
|
||||
try { _process.Kill(entireProcessTree: true); }
|
||||
catch { }
|
||||
}
|
||||
_process?.Dispose();
|
||||
_process = null;
|
||||
_view?.Dispose();
|
||||
_view = null;
|
||||
_mmf?.Dispose();
|
||||
_mmf = null;
|
||||
_workEvent?.Dispose();
|
||||
_workEvent = null;
|
||||
_doneEvent?.Dispose();
|
||||
_doneEvent = null;
|
||||
_alive = false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_alive && _process is { HasExited: false } && _view is not null && _workEvent is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_view.Write(NativeProtocol.OffCommand, NativeProtocol.CmdShutdown);
|
||||
_workEvent.Set();
|
||||
_process.WaitForExit(500);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
KillWorker();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal static class NativeFieldMap
|
||||
{
|
||||
public static void ToFields(AviUtlScriptContext ctx, double[] f)
|
||||
{
|
||||
f[NativeProtocol.W] = ctx.ImageWidth;
|
||||
f[NativeProtocol.H] = ctx.ImageHeight;
|
||||
f[NativeProtocol.Hw] = ctx.ImageWidth / 2d;
|
||||
f[NativeProtocol.Hh] = ctx.ImageHeight / 2d;
|
||||
f[NativeProtocol.Cx] = ctx.ImageWidth / 2d;
|
||||
f[NativeProtocol.Cy] = ctx.ImageHeight / 2d;
|
||||
f[NativeProtocol.Cz] = 0d;
|
||||
f[NativeProtocol.Diagonal] = Math.Sqrt((double)ctx.ImageWidth * ctx.ImageWidth + (double)ctx.ImageHeight * ctx.ImageHeight);
|
||||
f[NativeProtocol.X] = ctx.X;
|
||||
f[NativeProtocol.Y] = ctx.Y;
|
||||
f[NativeProtocol.Z] = ctx.Z;
|
||||
f[NativeProtocol.Ox] = ctx.Ox;
|
||||
f[NativeProtocol.Oy] = ctx.Oy;
|
||||
f[NativeProtocol.Oz] = ctx.Oz;
|
||||
f[NativeProtocol.Sx] = ctx.Sx;
|
||||
f[NativeProtocol.Sy] = ctx.Sy;
|
||||
f[NativeProtocol.Sz] = 1d;
|
||||
f[NativeProtocol.Zoom] = ctx.Zoom;
|
||||
f[NativeProtocol.Aspect] = ctx.Aspect;
|
||||
f[NativeProtocol.Alpha] = ctx.Alpha;
|
||||
f[NativeProtocol.Rx] = ctx.Rx;
|
||||
f[NativeProtocol.Ry] = ctx.Ry;
|
||||
f[NativeProtocol.Rz] = ctx.Rz;
|
||||
f[NativeProtocol.Rxr] = ctx.RxRad;
|
||||
f[NativeProtocol.Ryr] = ctx.RyRad;
|
||||
f[NativeProtocol.Rzr] = ctx.RzRad;
|
||||
f[NativeProtocol.Track0] = ctx.Track0;
|
||||
f[NativeProtocol.Track1] = ctx.Track1;
|
||||
f[NativeProtocol.Track2] = ctx.Track2;
|
||||
f[NativeProtocol.Track3] = ctx.Track3;
|
||||
f[NativeProtocol.Slider0] = ctx.Slider0;
|
||||
f[NativeProtocol.Slider1] = ctx.Slider1;
|
||||
f[NativeProtocol.Slider2] = ctx.Slider2;
|
||||
f[NativeProtocol.Slider3] = ctx.Slider3;
|
||||
f[NativeProtocol.Check0] = ctx.Check0 ? 1d : 0d;
|
||||
f[NativeProtocol.Check1] = ctx.Check1 ? 1d : 0d;
|
||||
f[NativeProtocol.Check2] = ctx.Check2 ? 1d : 0d;
|
||||
f[NativeProtocol.Check3] = ctx.Check3 ? 1d : 0d;
|
||||
f[NativeProtocol.Color] = ctx.HasColor ? ctx.ColorValue : -1d;
|
||||
f[NativeProtocol.Time] = ctx.Time;
|
||||
f[NativeProtocol.Frame] = ctx.Frame;
|
||||
f[NativeProtocol.TotalFrame] = ctx.TotalFrame;
|
||||
f[NativeProtocol.TotalTime] = ctx.TotalTime;
|
||||
f[NativeProtocol.T] = ctx.TotalFrame > 0 ? ctx.Frame / (double)ctx.TotalFrame : 0d;
|
||||
f[NativeProtocol.Framerate] = ctx.Framerate;
|
||||
f[NativeProtocol.Layer] = ctx.Layer;
|
||||
f[NativeProtocol.Index] = ctx.Index;
|
||||
f[NativeProtocol.Num] = ctx.Num;
|
||||
f[NativeProtocol.SceneWidth] = ctx.SceneWidth;
|
||||
f[NativeProtocol.SceneHeight] = ctx.SceneHeight;
|
||||
f[NativeProtocol.SceneCx] = ctx.SceneWidth / 2d;
|
||||
f[NativeProtocol.SceneCy] = ctx.SceneHeight / 2d;
|
||||
f[NativeProtocol.GroupIndex] = ctx.GroupIndex;
|
||||
f[NativeProtocol.GroupCount] = ctx.GroupCount;
|
||||
f[NativeProtocol.TimelineTotalFrame] = ctx.TimelineTotalFrame;
|
||||
f[NativeProtocol.TimelineTotalTime] = ctx.TimelineTotalTime;
|
||||
f[NativeProtocol.TimeRatio] = ctx.TimeRatio;
|
||||
f[NativeProtocol.IsSaving] = ctx.IsSaving ? 1d : 0d;
|
||||
f[NativeProtocol.IsPlaying] = ctx.IsPlaying ? 1d : 0d;
|
||||
f[NativeProtocol.IsPaused] = ctx.IsPaused ? 1d : 0d;
|
||||
f[NativeProtocol.TimelineFrame] = ctx.TimelineFrame;
|
||||
f[NativeProtocol.TimelineTime] = ctx.TimelineTime;
|
||||
}
|
||||
|
||||
public static void FromFields(double[] f, AviUtlScriptContext ctx)
|
||||
{
|
||||
ctx.X = f[NativeProtocol.X];
|
||||
ctx.Y = f[NativeProtocol.Y];
|
||||
ctx.Z = f[NativeProtocol.Z];
|
||||
ctx.Ox = f[NativeProtocol.Ox];
|
||||
ctx.Oy = f[NativeProtocol.Oy];
|
||||
ctx.Oz = f[NativeProtocol.Oz];
|
||||
ctx.Alpha = f[NativeProtocol.Alpha];
|
||||
|
||||
ctx.ApplyWriteBack(
|
||||
f[NativeProtocol.Sx],
|
||||
f[NativeProtocol.Sy],
|
||||
f[NativeProtocol.Zoom],
|
||||
f[NativeProtocol.Aspect],
|
||||
f[NativeProtocol.Rx],
|
||||
f[NativeProtocol.Ry],
|
||||
f[NativeProtocol.Rz],
|
||||
f[NativeProtocol.Rxr],
|
||||
f[NativeProtocol.Ryr],
|
||||
f[NativeProtocol.Rzr]);
|
||||
|
||||
ctx.DrawStateOverride = f[NativeProtocol.DrawState] switch
|
||||
{
|
||||
1d => true,
|
||||
2d => false,
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal static class NativeProtocol
|
||||
{
|
||||
public const int Magic = 0x4C4A4954;
|
||||
|
||||
public const int CmdRun = 0;
|
||||
public const int CmdShutdown = 1;
|
||||
|
||||
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;
|
||||
public const int OffStatus = 8;
|
||||
public const int OffWidth = 12;
|
||||
public const int OffHeight = 16;
|
||||
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 OffCallbackKind = 44;
|
||||
public const int OffLoadResultWidth = 48;
|
||||
public const int OffLoadResultHeight = 52;
|
||||
public const int OffStringParamsLen = 56;
|
||||
public const int OffScriptVersion = 60;
|
||||
|
||||
public const int HeaderSize = 64;
|
||||
|
||||
public const int FieldCount = 64;
|
||||
public const int FieldsOffset = HeaderSize;
|
||||
|
||||
public const int ScriptOffset = FieldsOffset + FieldCount * 8;
|
||||
public const int ScriptMax = 128 * 1024;
|
||||
|
||||
public const int ErrorOffset = ScriptOffset + ScriptMax;
|
||||
public const int ErrorMax = 4 * 1024;
|
||||
|
||||
public const int CallbackTagOffset = ErrorOffset + ErrorMax;
|
||||
public const int CallbackTagMax = 8192;
|
||||
public const int CallbackResultOffset = CallbackTagOffset + CallbackTagMax;
|
||||
public const int CallbackResultCount = 8;
|
||||
|
||||
public const int StringParamsOffset = CallbackResultOffset + CallbackResultCount * 8;
|
||||
public const int MinStringParamsCapacity = 64 * 1024;
|
||||
|
||||
public const int CbKindGetObject = 0;
|
||||
public const int CbKindLoadFigure = 1;
|
||||
public const int CbKindEffect = 2;
|
||||
public const int CbKindDraw = 3;
|
||||
public const int CbKindDrawPoly = 4;
|
||||
public const int CbKindLoadText = 5;
|
||||
public const int CbKindLoadImage = 6;
|
||||
public const int CbKindLoadMovie = 7;
|
||||
public const int CbKindSetAnchor = 8;
|
||||
public const int CbKindRequestPixels = 9;
|
||||
public const int CbKindFlushDraws = 10;
|
||||
public const int CbKindSceneGet = 11;
|
||||
public const int CbKindSceneSet = 12;
|
||||
|
||||
public const int DrawRingCapacity = 4096;
|
||||
public const int DrawEntryDoubles = 24;
|
||||
public const int DrawRingDoubles = 1 + DrawRingCapacity * DrawEntryDoubles;
|
||||
public const long DrawRingBytes = (long)DrawRingDoubles * 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 static long DrawRingOffset(int stringCapacity) => StringParamsOffset + stringCapacity;
|
||||
|
||||
public static long PixelOffset(int stringCapacity) => DrawRingOffset(stringCapacity) + DrawRingBytes;
|
||||
|
||||
public const int W = 0;
|
||||
public const int H = 1;
|
||||
public const int Hw = 2;
|
||||
public const int Hh = 3;
|
||||
public const int Cx = 4;
|
||||
public const int Cy = 5;
|
||||
public const int Cz = 6;
|
||||
public const int Diagonal = 7;
|
||||
public const int X = 8;
|
||||
public const int Y = 9;
|
||||
public const int Z = 10;
|
||||
public const int Ox = 11;
|
||||
public const int Oy = 12;
|
||||
public const int Oz = 13;
|
||||
public const int Sx = 14;
|
||||
public const int Sy = 15;
|
||||
public const int Sz = 16;
|
||||
public const int Zoom = 17;
|
||||
public const int Aspect = 18;
|
||||
public const int Alpha = 19;
|
||||
public const int Rx = 20;
|
||||
public const int Ry = 21;
|
||||
public const int Rz = 22;
|
||||
public const int Rxr = 23;
|
||||
public const int Ryr = 24;
|
||||
public const int Rzr = 25;
|
||||
public const int Track0 = 26;
|
||||
public const int Track1 = 27;
|
||||
public const int Track2 = 28;
|
||||
public const int Track3 = 29;
|
||||
public const int Time = 30;
|
||||
public const int Frame = 31;
|
||||
public const int TotalFrame = 32;
|
||||
public const int TotalTime = 33;
|
||||
public const int T = 34;
|
||||
public const int Framerate = 35;
|
||||
public const int Layer = 36;
|
||||
public const int Index = 37;
|
||||
public const int Num = 38;
|
||||
public const int SceneWidth = 39;
|
||||
public const int SceneHeight = 40;
|
||||
public const int SceneCx = 41;
|
||||
public const int SceneCy = 42;
|
||||
public const int GroupIndex = 43;
|
||||
public const int GroupCount = 44;
|
||||
public const int TimelineTotalFrame = 45;
|
||||
public const int TimelineTotalTime = 46;
|
||||
public const int TimeRatio = 47;
|
||||
public const int IsSaving = 48;
|
||||
public const int IsPlaying = 49;
|
||||
public const int IsPaused = 50;
|
||||
public const int TimelineFrame = 51;
|
||||
public const int TimelineTime = 52;
|
||||
public const int Check0 = 53;
|
||||
public const int Check1 = 54;
|
||||
public const int Check2 = 55;
|
||||
public const int Check3 = 56;
|
||||
public const int Color = 57;
|
||||
public const int Slider0 = 58;
|
||||
public const int Slider1 = 59;
|
||||
public const int Slider2 = 60;
|
||||
public const int Slider3 = 61;
|
||||
public const int DrawState = 62;
|
||||
|
||||
public const int FirstWritableField = X;
|
||||
public const int LastWritableField = Rzr;
|
||||
|
||||
public const int MaxPixelBufferSize = 3840 * 2160 * 4;
|
||||
|
||||
public static long BufferSize(int width, int height, int stringCapacity) =>
|
||||
PixelOffset(stringCapacity) + Math.Max((long)width * height * 4, MaxPixelBufferSize);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal delegate void PixelRegionAccess(nint address, long capacity);
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal static class ProcessJobObject
|
||||
{
|
||||
private const uint JobObjectExtendedLimitInformation = 9;
|
||||
private const uint JobObjectLimitKillOnJobClose = 0x2000;
|
||||
|
||||
private static readonly nint Handle = Create();
|
||||
|
||||
public static void Assign(nint processHandle)
|
||||
{
|
||||
if (Handle != nint.Zero && processHandle != nint.Zero)
|
||||
AssignProcessToJobObject(Handle, processHandle);
|
||||
}
|
||||
|
||||
private static nint Create()
|
||||
{
|
||||
nint job = CreateJobObject(nint.Zero, null);
|
||||
if (job == nint.Zero)
|
||||
return nint.Zero;
|
||||
|
||||
var information = new JobObjectExtendedLimitInformationData
|
||||
{
|
||||
BasicLimitInformation = new JobObjectBasicLimitInformationData
|
||||
{
|
||||
LimitFlags = JobObjectLimitKillOnJobClose
|
||||
}
|
||||
};
|
||||
|
||||
int length = Marshal.SizeOf<JobObjectExtendedLimitInformationData>();
|
||||
nint buffer = Marshal.AllocHGlobal(length);
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(information, buffer, false);
|
||||
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, buffer, (uint)length))
|
||||
{
|
||||
CloseHandle(job);
|
||||
return nint.Zero;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
}
|
||||
|
||||
return job;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct JobObjectBasicLimitInformationData
|
||||
{
|
||||
public long PerProcessUserTimeLimit;
|
||||
public long PerJobUserTimeLimit;
|
||||
public uint LimitFlags;
|
||||
public nuint MinimumWorkingSetSize;
|
||||
public nuint MaximumWorkingSetSize;
|
||||
public uint ActiveProcessLimit;
|
||||
public nuint Affinity;
|
||||
public uint PriorityClass;
|
||||
public uint SchedulingClass;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct IoCountersData
|
||||
{
|
||||
public ulong ReadOperationCount;
|
||||
public ulong WriteOperationCount;
|
||||
public ulong OtherOperationCount;
|
||||
public ulong ReadTransferCount;
|
||||
public ulong WriteTransferCount;
|
||||
public ulong OtherTransferCount;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct JobObjectExtendedLimitInformationData
|
||||
{
|
||||
public JobObjectBasicLimitInformationData BasicLimitInformation;
|
||||
public IoCountersData IoInfo;
|
||||
public nuint ProcessMemoryLimit;
|
||||
public nuint JobMemoryLimit;
|
||||
public nuint PeakProcessMemoryUsed;
|
||||
public nuint PeakJobMemoryUsed;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
private static extern nint CreateJobObject(nint lpJobAttributes, string? lpName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool SetInformationJobObject(nint hJob, uint jobObjectInformationClass, nint lpJobObjectInformation, uint cbJobObjectInformationLength);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool AssignProcessToJobObject(nint hJob, nint hProcess);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CloseHandle(nint hObject);
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal static class ScriptDirective
|
||||
{
|
||||
public static ScriptEngineKind Resolve(string? script) =>
|
||||
TryGetDirective(script, out var kind) ? kind : ScriptEngineKind.MoonSharp;
|
||||
|
||||
public static ScriptEngineKind ResolveAuto(string? script)
|
||||
{
|
||||
if (string.IsNullOrEmpty(script))
|
||||
return ScriptEngineKind.MoonSharp;
|
||||
return TryGetDirective(script, out var kind) ? kind : ScriptEngineKind.Native;
|
||||
}
|
||||
|
||||
public static bool TryResolveExplicit(string? script, out ScriptEngineKind kind) =>
|
||||
TryGetDirective(script, out kind);
|
||||
|
||||
private static bool TryGetDirective(string? script, out ScriptEngineKind kind)
|
||||
{
|
||||
kind = ScriptEngineKind.MoonSharp;
|
||||
if (string.IsNullOrEmpty(script))
|
||||
return false;
|
||||
|
||||
int index = 0;
|
||||
int length = script.Length;
|
||||
while (index < length)
|
||||
{
|
||||
int lineStart = index;
|
||||
while (index < length && script[index] != '\n')
|
||||
index++;
|
||||
int lineEnd = index;
|
||||
if (lineEnd > lineStart && script[lineEnd - 1] == '\r')
|
||||
lineEnd--;
|
||||
|
||||
if (TryParseDirectiveLine(script, lineStart, lineEnd, out kind))
|
||||
return true;
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TryParseDirectiveLine(string script, int start, int end, out ScriptEngineKind kind)
|
||||
{
|
||||
kind = ScriptEngineKind.MoonSharp;
|
||||
|
||||
int i = start;
|
||||
while (i < end && (script[i] == ' ' || script[i] == '\t'))
|
||||
i++;
|
||||
|
||||
if (i + 3 > end || script[i] != '-' || script[i + 1] != '-' || script[i + 2] != '!')
|
||||
return false;
|
||||
i += 3;
|
||||
|
||||
while (i < end && (script[i] == ' ' || script[i] == '\t'))
|
||||
i++;
|
||||
|
||||
int tokenStart = i;
|
||||
while (i < end && IsTokenChar(script[i]))
|
||||
i++;
|
||||
int tokenEnd = i;
|
||||
|
||||
while (i < end && (script[i] == ' ' || script[i] == '\t'))
|
||||
i++;
|
||||
if (i != end)
|
||||
return false;
|
||||
|
||||
var token = script.AsSpan(tokenStart, tokenEnd - tokenStart);
|
||||
if (token.Equals("native", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
kind = ScriptEngineKind.Native;
|
||||
return true;
|
||||
}
|
||||
if (token.Equals("gpu", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
kind = ScriptEngineKind.Gpu;
|
||||
return true;
|
||||
}
|
||||
if (token.Equals("cpu", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
kind = ScriptEngineKind.Cpu;
|
||||
return true;
|
||||
}
|
||||
if (token.Equals("moonsharp", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
kind = ScriptEngineKind.MoonSharp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsTokenChar(char c) =>
|
||||
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace LuaScript.Engine
|
||||
{
|
||||
internal enum ScriptEngineKind
|
||||
{
|
||||
MoonSharp,
|
||||
Native,
|
||||
Gpu,
|
||||
Cpu,
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする