diff --git a/LuaScript.Runtime/AnimTableRegistrar.cs b/LuaScript.Runtime/AnimTableRegistrar.cs index c7fa9e5..d874700 100644 --- a/LuaScript.Runtime/AnimTableRegistrar.cs +++ b/LuaScript.Runtime/AnimTableRegistrar.cs @@ -1,412 +1,346 @@ +using LuaScript.Api; using MoonSharp.Interpreter; namespace LuaScript { - internal static class AnimTableRegistrar + [LuaTable("anim")] + internal static partial class AnimTableRegistrar { + [LuaConstant("tau")] private static readonly double s_tau = Math.PI * 2d; + + [LuaConstant("e")] private static readonly double s_e = Math.E; + + [LuaConstant("phi")] private static readonly double s_phi = (1d + Math.Sqrt(5d)) / 2d; + + [LuaConstant("sqrt2")] private static readonly double s_sqrt2 = Math.Sqrt(2d); - internal static void RegisterFunctions(Table anim) + internal static void RegisterFunctions(Table anim) => RegisterLuaMembers(anim); + + [LuaFunction("lerp")] + private static double Lerp(double a, double b, double t) => a + (b - a) * t; + + [LuaFunction("smoothstep")] + private static double Smoothstep(double edge0, double edge1 = 1d, double x = 0d) { - anim["tau"] = s_tau; - anim["e"] = s_e; - anim["phi"] = s_phi; - anim["sqrt2"] = s_sqrt2; + double span = edge1 - edge0; + double t = span == 0d ? 0d : Math.Clamp((x - edge0) / span, 0d, 1d); + return t * t * (3d - 2d * t); + } - anim["lerp"] = DynValue.NewCallback((_, args) => + [LuaFunction("smootherstep")] + private static double Smootherstep(double edge0, double edge1 = 1d, double x = 0d) + { + double span = edge1 - edge0; + double t = span == 0d ? 0d : Math.Clamp((x - edge0) / span, 0d, 1d); + return t * t * t * (t * (6d * t - 15d) + 10d); + } + + [LuaFunction("clamp")] + private static double Clamp(double v, double lo = 0d, double hi = 1d) => Math.Clamp(v, lo, hi); + + [LuaFunction("map")] + private static double Map(double v, double a1 = 0d, double b1 = 1d, double a2 = 0d, double b2 = 1d) + { + double range = b1 - a1; + if (range == 0d) return a2; + return a2 + (b2 - a2) * (v - a1) / range; + } + + [LuaFunction("norm")] + private static double Norm(double v, double lo = 0d, double hi = 1d) + { + double span = hi - lo; + if (span == 0d) return 0d; + return (v - lo) / span; + } + + [LuaFunction("wrap")] + private static double Wrap(double v, double lo = 0d, double hi = 1d) + { + double range = hi - lo; + if (range <= 0d) return lo; + double result = (v - lo) % range; + if (result < 0d) result += range; + return lo + result; + } + + [LuaFunction("pingpong")] + private static double Pingpong(double t, double length = 1d) + { + if (length <= 0d) return 0d; + double v = t % (2d * length); + if (v < 0d) v += 2d * length; + return v > length ? 2d * length - v : v; + } + + [LuaFunction("sign")] + private static double Sign(double v) => Math.Sign(v); + + [LuaFunction("oscillate")] + private static double Oscillate(double t, double lo = 0d, double hi = 1d, double freq = 1d) + { + double wave = (Math.Sin(t * freq * s_tau) + 1d) / 2d; + return lo + (hi - lo) * wave; + } + + [LuaFunction("triangle")] + private static double Triangle(double t, double freq = 1d) + { + double f = t * freq; + f -= Math.Floor(f); + return 1d - 2d * Math.Abs(f - 0.5d); + } + + [LuaFunction("square")] + private static double Square(double t, double freq = 1d) + { + double f = t * freq; + f -= Math.Floor(f); + return f >= 0.5d ? 1d : 0d; + } + + [LuaFunction("duration")] + private static double Duration(double t, double dur = 1d) + { + if (dur <= 0d) return 1d; + return Math.Clamp(t / dur, 0d, 1d); + } + + [LuaFunction("delay")] + private static double Delay(double t, double d) => Math.Max(0d, t - d); + + [LuaFunction("ease_in")] + private static double EaseIn(double t) => t * t; + + [LuaFunction("ease_out")] + private static double EaseOut(double t) + { + double inv = 1d - t; + return 1d - inv * inv; + } + + [LuaFunction("ease_in_out")] + private static double EaseInOut(double t) + { + if (t < 0.5d) + return 2d * t * t; + double inv = -2d * t + 2d; + return 1d - inv * inv / 2d; + } + + [LuaFunction("elastic")] + private static double Elastic(double t) + { + t = Math.Clamp(t, 0d, 1d); + if (t == 0d) return 0d; + if (t == 1d) return 1d; + return Math.Pow(2d, -10d * t) * Math.Sin((t * 10d - 0.75d) * (s_tau / 3d)) + 1d; + } + + [LuaFunction("back")] + private static double Back(double t) + { + const double c1 = 1.70158d; + const double c3 = c1 + 1d; + double inv = t - 1d; + return 1d + c3 * inv * inv * inv + c1 * inv * inv; + } + + [LuaFunction("step")] + private static double Step(double edge, double x) => x >= edge ? 1d : 0d; + + [LuaFunction("fract")] + private static double Fract(double v) => v - Math.Floor(v); + + [LuaFunction("bounce")] + private static double Bounce(double t) + { + const double n1 = 7.5625d; + const double d1 = 2.75d; + t = Math.Clamp(t, 0d, 1d); + if (t < 1d / d1) + return n1 * t * t; + if (t < 2d / d1) { - double a = args[0].CastToNumber() ?? 0d; - double b = args[1].CastToNumber() ?? 0d; - double t = args[2].CastToNumber() ?? 0d; - return DynValue.NewNumber(a + (b - a) * t); - }); - - anim["smoothstep"] = DynValue.NewCallback((_, args) => + t -= 1.5d / d1; + return n1 * t * t + 0.75d; + } + if (t < 2.5d / d1) { - double edge0 = args[0].CastToNumber() ?? 0d; - double edge1 = args[1].CastToNumber() ?? 1d; - double x = args[2].CastToNumber() ?? 0d; - double span = edge1 - edge0; - double t = span == 0d ? 0d : Math.Clamp((x - edge0) / span, 0d, 1d); - return DynValue.NewNumber(t * t * (3d - 2d * t)); - }); + t -= 2.25d / d1; + return n1 * t * t + 0.9375d; + } + t -= 2.625d / d1; + return n1 * t * t + 0.984375d; + } - anim["smootherstep"] = DynValue.NewCallback((_, args) => + [LuaFunction("hsv_to_rgb")] + private static DynValue HsvToRgb(double h, double s, double v) + { + double c = v * s; + double x = c * (1d - Math.Abs((h / 60d) % 2d - 1d)); + double m = v - c; + int sector = ((int)Math.Floor(h / 60d) % 6 + 6) % 6; + (double r, double g, double b) = sector switch { - double edge0 = args[0].CastToNumber() ?? 0d; - double edge1 = args[1].CastToNumber() ?? 1d; - double x = args[2].CastToNumber() ?? 0d; - double span = edge1 - edge0; - double t = span == 0d ? 0d : Math.Clamp((x - edge0) / span, 0d, 1d); - return DynValue.NewNumber(t * t * t * (t * (6d * t - 15d) + 10d)); - }); + 0 => (c, x, 0d), + 1 => (x, c, 0d), + 2 => (0d, c, x), + 3 => (0d, x, c), + 4 => (x, 0d, c), + _ => (c, 0d, x), + }; + return DynValue.NewTuple( + DynValue.NewNumber((r + m) * 255d), + DynValue.NewNumber((g + m) * 255d), + DynValue.NewNumber((b + m) * 255d)); + } - anim["clamp"] = DynValue.NewCallback((_, args) => + [LuaFunction("rgb_to_hsv")] + private static DynValue RgbToHsv(double r, double g, double b) + { + r /= 255d; + g /= 255d; + b /= 255d; + double max = Math.Max(r, Math.Max(g, b)); + double min = Math.Min(r, Math.Min(g, b)); + double delta = max - min; + double h = 0d; + if (delta > 0d) { - double v = args[0].CastToNumber() ?? 0d; - double lo = args[1].CastToNumber() ?? 0d; - double hi = args[2].CastToNumber() ?? 1d; - return DynValue.NewNumber(Math.Clamp(v, lo, hi)); - }); + if (max == r) h = 60d * ((((g - b) / delta) % 6d + 6d) % 6d); + else if (max == g) h = 60d * ((b - r) / delta + 2d); + else h = 60d * ((r - g) / delta + 4d); + } + return DynValue.NewTuple( + DynValue.NewNumber(h), + DynValue.NewNumber(max > 0d ? delta / max : 0d), + DynValue.NewNumber(max)); + } - anim["map"] = DynValue.NewCallback((_, args) => + [LuaFunction("len")] + private static double Len(double x, double y) => Math.Sqrt(x * x + y * y); + + [LuaFunction("dist")] + private static double Dist(double x1, double y1, double x2, double y2) + { + double dx = x2 - x1; + double dy = y2 - y1; + return Math.Sqrt(dx * dx + dy * dy); + } + + [LuaFunction("dot")] + private static double Dot(double x1, double y1, double x2, double y2) => x1 * x2 + y1 * y2; + + [LuaFunction("normalize")] + private static DynValue Normalize(double x, double y) + { + double magnitude = Math.Sqrt(x * x + y * y); + if (magnitude == 0d) + return DynValue.NewTuple(DynValue.NewNumber(0d), DynValue.NewNumber(0d)); + return DynValue.NewTuple( + DynValue.NewNumber(x / magnitude), + DynValue.NewNumber(y / magnitude)); + } + + [LuaFunction("noise")] + private static double Noise(double x, double y, double z) + { + int xi = (int)Math.Floor(x); + int yi = (int)Math.Floor(y); + int zi = (int)Math.Floor(z); + + double xf = x - xi; + double yf = y - yi; + double zf = z - zi; + + double u = xf * xf * (3d - 2d * xf); + double v = yf * yf * (3d - 2d * yf); + double w = zf * zf * (3d - 2d * zf); + + double c000 = GetNoiseHash(xi, yi, zi); + double c100 = GetNoiseHash(xi + 1, yi, zi); + double c010 = GetNoiseHash(xi, yi + 1, zi); + double c110 = GetNoiseHash(xi + 1, yi + 1, zi); + double c001 = GetNoiseHash(xi, yi, zi + 1); + double c101 = GetNoiseHash(xi + 1, yi, zi + 1); + double c011 = GetNoiseHash(xi, yi + 1, zi + 1); + double c111 = GetNoiseHash(xi + 1, yi + 1, zi + 1); + + double x00 = c000 + u * (c100 - c000); + double x10 = c010 + u * (c110 - c010); + double x01 = c001 + u * (c101 - c001); + double x11 = c011 + u * (c111 - c011); + + double y0 = x00 + v * (x10 - x00); + double y1 = x01 + v * (x11 - x01); + + return y0 + w * (y1 - y0); + } + + [LuaFunction("rand", "min", "max", "seed")] + private static DynValue Rand(CallbackArguments args) + { + if (args.Count == 0) return DynValue.NewNumber(0d); + double seed, min = 0d, max = 1d; + if (args.Count == 1) { - double v = args[0].CastToNumber() ?? 0d; - double a1 = args[1].CastToNumber() ?? 0d; - double b1 = args[2].CastToNumber() ?? 1d; - double a2 = args[3].CastToNumber() ?? 0d; - double b2 = args[4].CastToNumber() ?? 1d; - double range = b1 - a1; - if (range == 0d) return DynValue.NewNumber(a2); - return DynValue.NewNumber(a2 + (b2 - a2) * (v - a1) / range); - }); - - anim["norm"] = DynValue.NewCallback((_, args) => + seed = args[0].CastToNumber() ?? 0d; + } + else if (args.Count == 2) { - double v = args[0].CastToNumber() ?? 0d; - double lo = args[1].CastToNumber() ?? 0d; - double hi = args[2].CastToNumber() ?? 1d; - double span = hi - lo; - if (span == 0d) return DynValue.NewNumber(0d); - return DynValue.NewNumber((v - lo) / span); - }); - - anim["wrap"] = DynValue.NewCallback((_, args) => + max = args[0].CastToNumber() ?? 1d; + seed = args[1].CastToNumber() ?? 0d; + } + else { - double v = args[0].CastToNumber() ?? 0d; - double lo = args[1].CastToNumber() ?? 0d; - double hi = args[2].CastToNumber() ?? 1d; - double range = hi - lo; - if (range <= 0d) return DynValue.NewNumber(lo); - double result = (v - lo) % range; - if (result < 0d) result += range; - return DynValue.NewNumber(lo + result); - }); + min = args[0].CastToNumber() ?? 0d; + max = args[1].CastToNumber() ?? 1d; + seed = args[2].CastToNumber() ?? 0d; + } + long bits = BitConverter.DoubleToInt64Bits(seed); + uint h = (uint)((bits ^ (bits >> 32)) * 374761393); + h = (h ^ (h >> 13)) * 1274126177; + double r = (double)(h ^ (h >> 16)) / 4294967295.0; + return DynValue.NewNumber(min + r * (max - min)); + } - anim["pingpong"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double length = args[1].CastToNumber() ?? 1d; - if (length <= 0d) return DynValue.NewNumber(0d); - double v = t % (2d * length); - if (v < 0d) v += 2d * length; - return DynValue.NewNumber(v > length ? 2d * length - v : v); - }); + [LuaFunction("polar")] + private static DynValue Polar(double r, double a) + { + double rad = a * Math.PI / 180d; + return DynValue.NewTuple( + DynValue.NewNumber(r * Math.Cos(rad)), + DynValue.NewNumber(r * Math.Sin(rad)) + ); + } - anim["sign"] = DynValue.NewCallback((_, args) => - { - double v = args[0].CastToNumber() ?? 0d; - return DynValue.NewNumber(Math.Sign(v)); - }); + [LuaFunction("rotate")] + private static DynValue Rotate(double x, double y, double a) + { + double rad = a * Math.PI / 180d; + double c = Math.Cos(rad); + double s = Math.Sin(rad); + return DynValue.NewTuple( + DynValue.NewNumber(x * c - y * s), + DynValue.NewNumber(x * s + y * c) + ); + } - anim["oscillate"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double lo = args[1].CastToNumber() ?? 0d; - double hi = args[2].CastToNumber() ?? 1d; - double freq = args[3].CastToNumber() ?? 1d; - double wave = (Math.Sin(t * freq * s_tau) + 1d) / 2d; - return DynValue.NewNumber(lo + (hi - lo) * wave); - }); - - anim["triangle"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double freq = args[1].CastToNumber() ?? 1d; - double f = t * freq; - f -= Math.Floor(f); - return DynValue.NewNumber(1d - 2d * Math.Abs(f - 0.5d)); - }); - - anim["square"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double freq = args[1].CastToNumber() ?? 1d; - double f = t * freq; - f -= Math.Floor(f); - return DynValue.NewNumber(f >= 0.5d ? 1d : 0d); - }); - - anim["duration"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double dur = args[1].CastToNumber() ?? 1d; - if (dur <= 0d) return DynValue.NewNumber(1d); - return DynValue.NewNumber(Math.Clamp(t / dur, 0d, 1d)); - }); - - anim["delay"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double d = args[1].CastToNumber() ?? 0d; - return DynValue.NewNumber(Math.Max(0d, t - d)); - }); - - anim["ease_in"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - return DynValue.NewNumber(t * t); - }); - - anim["ease_out"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - double inv = 1d - t; - return DynValue.NewNumber(1d - inv * inv); - }); - - anim["ease_in_out"] = DynValue.NewCallback((_, args) => - { - double t = args[0].CastToNumber() ?? 0d; - if (t < 0.5d) - return DynValue.NewNumber(2d * t * t); - double inv = -2d * t + 2d; - return DynValue.NewNumber(1d - inv * inv / 2d); - }); - - anim["elastic"] = DynValue.NewCallback((_, args) => - { - double t = Math.Clamp(args[0].CastToNumber() ?? 0d, 0d, 1d); - if (t == 0d) return DynValue.NewNumber(0d); - if (t == 1d) return DynValue.NewNumber(1d); - return DynValue.NewNumber(Math.Pow(2d, -10d * t) * Math.Sin((t * 10d - 0.75d) * (s_tau / 3d)) + 1d); - }); - - anim["back"] = DynValue.NewCallback((_, args) => - { - const double c1 = 1.70158d; - const double c3 = c1 + 1d; - double t = args[0].CastToNumber() ?? 0d; - double inv = t - 1d; - return DynValue.NewNumber(1d + c3 * inv * inv * inv + c1 * inv * inv); - }); - - anim["step"] = DynValue.NewCallback((_, args) => - { - double edge = args[0].CastToNumber() ?? 0d; - double x = args[1].CastToNumber() ?? 0d; - return DynValue.NewNumber(x >= edge ? 1d : 0d); - }); - - anim["fract"] = DynValue.NewCallback((_, args) => - { - double v = args[0].CastToNumber() ?? 0d; - return DynValue.NewNumber(v - Math.Floor(v)); - }); - - anim["bounce"] = DynValue.NewCallback((_, args) => - { - const double n1 = 7.5625d; - const double d1 = 2.75d; - double t = Math.Clamp(args[0].CastToNumber() ?? 0d, 0d, 1d); - if (t < 1d / d1) - return DynValue.NewNumber(n1 * t * t); - if (t < 2d / d1) - { - t -= 1.5d / d1; - return DynValue.NewNumber(n1 * t * t + 0.75d); - } - if (t < 2.5d / d1) - { - t -= 2.25d / d1; - return DynValue.NewNumber(n1 * t * t + 0.9375d); - } - t -= 2.625d / d1; - return DynValue.NewNumber(n1 * t * t + 0.984375d); - }); - - anim["hsv_to_rgb"] = DynValue.NewCallback((_, args) => - { - double h = args[0].CastToNumber() ?? 0d; - double s = args[1].CastToNumber() ?? 0d; - double v = args[2].CastToNumber() ?? 0d; - double c = v * s; - double x = c * (1d - Math.Abs((h / 60d) % 2d - 1d)); - double m = v - c; - int sector = ((int)Math.Floor(h / 60d) % 6 + 6) % 6; - (double r, double g, double b) = sector switch - { - 0 => (c, x, 0d), - 1 => (x, c, 0d), - 2 => (0d, c, x), - 3 => (0d, x, c), - 4 => (x, 0d, c), - _ => (c, 0d, x), - }; - return DynValue.NewTuple( - DynValue.NewNumber((r + m) * 255d), - DynValue.NewNumber((g + m) * 255d), - DynValue.NewNumber((b + m) * 255d)); - }); - - anim["rgb_to_hsv"] = DynValue.NewCallback((_, args) => - { - double r = (args[0].CastToNumber() ?? 0d) / 255d; - double g = (args[1].CastToNumber() ?? 0d) / 255d; - double b = (args[2].CastToNumber() ?? 0d) / 255d; - double max = Math.Max(r, Math.Max(g, b)); - double min = Math.Min(r, Math.Min(g, b)); - double delta = max - min; - double h = 0d; - if (delta > 0d) - { - if (max == r) h = 60d * ((((g - b) / delta) % 6d + 6d) % 6d); - else if (max == g) h = 60d * ((b - r) / delta + 2d); - else h = 60d * ((r - g) / delta + 4d); - } - return DynValue.NewTuple( - DynValue.NewNumber(h), - DynValue.NewNumber(max > 0d ? delta / max : 0d), - DynValue.NewNumber(max)); - }); - - anim["len"] = DynValue.NewCallback((_, args) => - { - double x = args[0].CastToNumber() ?? 0d; - double y = args[1].CastToNumber() ?? 0d; - return DynValue.NewNumber(Math.Sqrt(x * x + y * y)); - }); - - anim["dist"] = DynValue.NewCallback((_, args) => - { - double x1 = args[0].CastToNumber() ?? 0d; - double y1 = args[1].CastToNumber() ?? 0d; - double x2 = args[2].CastToNumber() ?? 0d; - double y2 = args[3].CastToNumber() ?? 0d; - double dx = x2 - x1; - double dy = y2 - y1; - return DynValue.NewNumber(Math.Sqrt(dx * dx + dy * dy)); - }); - - anim["dot"] = DynValue.NewCallback((_, args) => - { - double x1 = args[0].CastToNumber() ?? 0d; - double y1 = args[1].CastToNumber() ?? 0d; - double x2 = args[2].CastToNumber() ?? 0d; - double y2 = args[3].CastToNumber() ?? 0d; - return DynValue.NewNumber(x1 * x2 + y1 * y2); - }); - - anim["normalize"] = DynValue.NewCallback((_, args) => - { - double x = args[0].CastToNumber() ?? 0d; - double y = args[1].CastToNumber() ?? 0d; - double magnitude = Math.Sqrt(x * x + y * y); - if (magnitude == 0d) - return DynValue.NewTuple(DynValue.NewNumber(0d), DynValue.NewNumber(0d)); - return DynValue.NewTuple( - DynValue.NewNumber(x / magnitude), - DynValue.NewNumber(y / magnitude)); - }); - - anim["polar"] = DynValue.NewCallback((_, args) => - { - double r = args[0].CastToNumber() ?? 0d; - double a = args[1].CastToNumber() ?? 0d; - double rad = a * Math.PI / 180d; - return DynValue.NewTuple( - DynValue.NewNumber(r * Math.Cos(rad)), - DynValue.NewNumber(r * Math.Sin(rad)) - ); - }); - - anim["rotate"] = DynValue.NewCallback((_, args) => - { - double x = args[0].CastToNumber() ?? 0d; - double y = args[1].CastToNumber() ?? 0d; - double a = args[2].CastToNumber() ?? 0d; - double rad = a * Math.PI / 180d; - double c = Math.Cos(rad); - double s = Math.Sin(rad); - return DynValue.NewTuple( - DynValue.NewNumber(x * c - y * s), - DynValue.NewNumber(x * s + y * c) - ); - }); - - anim["bezier"] = DynValue.NewCallback((_, args) => - { - double t = Math.Clamp(args[0].CastToNumber() ?? 0d, 0d, 1d); - double p0 = args[1].CastToNumber() ?? 0d; - double p1 = args[2].CastToNumber() ?? 0d; - double p2 = args[3].CastToNumber() ?? 1d; - double p3 = args[4].CastToNumber() ?? 1d; - - double inv = 1d - t; - double b0 = inv * inv * inv; - double b1 = 3d * inv * inv * t; - double b2 = 3d * inv * t * t; - double b3 = t * t * t; - - return DynValue.NewNumber(b0 * p0 + b1 * p1 + b2 * p2 + b3 * p3); - }); - - anim["rand"] = DynValue.NewCallback((_, args) => - { - if (args.Count == 0) return DynValue.NewNumber(0d); - double seed, min = 0d, max = 1d; - if (args.Count == 1) - { - seed = args[0].CastToNumber() ?? 0d; - } - else if (args.Count == 2) - { - max = args[0].CastToNumber() ?? 1d; - seed = args[1].CastToNumber() ?? 0d; - } - else - { - min = args[0].CastToNumber() ?? 0d; - max = args[1].CastToNumber() ?? 1d; - seed = args[2].CastToNumber() ?? 0d; - } - long bits = BitConverter.DoubleToInt64Bits(seed); - uint h = (uint)((bits ^ (bits >> 32)) * 374761393); - h = (h ^ (h >> 13)) * 1274126177; - double r = (double)(h ^ (h >> 16)) / 4294967295.0; - return DynValue.NewNumber(min + r * (max - min)); - }); - - anim["noise"] = DynValue.NewCallback((_, args) => - { - double x = args[0].CastToNumber() ?? 0d; - double y = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; - double z = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; - - int xi = (int)Math.Floor(x); - int yi = (int)Math.Floor(y); - int zi = (int)Math.Floor(z); - - double xf = x - xi; - double yf = y - yi; - double zf = z - zi; - - double u = xf * xf * (3d - 2d * xf); - double v = yf * yf * (3d - 2d * yf); - double w = zf * zf * (3d - 2d * zf); - - double c000 = GetNoiseHash(xi, yi, zi); - double c100 = GetNoiseHash(xi + 1, yi, zi); - double c010 = GetNoiseHash(xi, yi + 1, zi); - double c110 = GetNoiseHash(xi + 1, yi + 1, zi); - double c001 = GetNoiseHash(xi, yi, zi + 1); - double c101 = GetNoiseHash(xi + 1, yi, zi + 1); - double c011 = GetNoiseHash(xi, yi + 1, zi + 1); - double c111 = GetNoiseHash(xi + 1, yi + 1, zi + 1); - - double x00 = c000 + u * (c100 - c000); - double x10 = c010 + u * (c110 - c010); - double x01 = c001 + u * (c101 - c001); - double x11 = c011 + u * (c111 - c011); - - double y0 = x00 + v * (x10 - x00); - double y1 = x01 + v * (x11 - x01); - - return DynValue.NewNumber(y0 + w * (y1 - y0)); - }); + [LuaFunction("bezier")] + private static double Bezier(double t, double p0 = 0d, double p1 = 0d, double p2 = 1d, double p3 = 1d) + { + t = Math.Clamp(t, 0d, 1d); + double inv = 1d - t; + double b0 = inv * inv * inv; + double b1 = 3d * inv * inv * t; + double b2 = 3d * inv * t * t; + double b3 = t * t * t; + return b0 * p0 + b1 * p1 + b2 * p2 + b3 * p3; } private static double GetNoiseHash(int x, int y, int z) diff --git a/LuaScript.Runtime/AviUtlGlobalRegistrar.cs b/LuaScript.Runtime/AviUtlGlobalRegistrar.cs index 67c46b8..96d50a5 100644 --- a/LuaScript.Runtime/AviUtlGlobalRegistrar.cs +++ b/LuaScript.Runtime/AviUtlGlobalRegistrar.cs @@ -1,80 +1,113 @@ +using LuaScript.Api; using LuaScript.Compat; using LuaScript.Diagnostics; using MoonSharp.Interpreter; namespace LuaScript { - internal static class AviUtlGlobalRegistrar + [LuaTable("")] + internal sealed partial class AviUtlGlobalRegistrar(Func timeRatio) { - internal static void RegisterFunctions(Table globals, Func timeRatio) + internal static void RegisterFunctions(Table globals, Func timeRatio) => + new AviUtlGlobalRegistrar(timeRatio).RegisterLuaMembers(globals); + + [LuaVariable("time")] + private double Time(AviUtlScriptContext ctx) => ctx.Time; + + [LuaVariable("frame")] + private int Frame(AviUtlScriptContext ctx) => ctx.Frame; + + [LuaVariable("totalframe")] + private int TotalFrame(AviUtlScriptContext ctx) => ctx.TotalFrame; + + [LuaVariable("framerate")] + private int Framerate(AviUtlScriptContext ctx) => ctx.Framerate; + + [LuaVariable("timelineframe")] + private int TimelineFrame(AviUtlScriptContext ctx) => ctx.TimelineFrame; + + [LuaVariable("timelinetime")] + private double TimelineTime(AviUtlScriptContext ctx) => ctx.TimelineTime; + + [LuaVariable("layer")] + private int Layer(AviUtlScriptContext ctx) => ctx.Layer; + + [LuaVariable("color")] + private double? Color(AviUtlScriptContext ctx) => ctx.HasColor ? ctx.ColorValue : (double?)null; + + [LuaFunction("debug_print", "value")] + private DynValue DebugPrint(CallbackArguments args) { - globals["debug_print"] = DynValue.NewCallback((_, args) => + if (args.Count > 0) { - if (args.Count > 0) - { - var value = args[0]; - DebugOutput.Write(value.Type == DataType.String ? value.String : value.ToPrintString()); - } - return DynValue.Void; - }); - - globals["RGB"] = DynValue.NewCallback((_, args) => - { - int count = args.Count; - if (count >= 6) - return DynValue.NewNumber(AviUtlGlobalFunctions.RgbInterpolate( - Number(args, 0), Number(args, 1), Number(args, 2), - Number(args, 3), Number(args, 4), Number(args, 5), - timeRatio())); - if (count >= 3) - return DynValue.NewNumber(AviUtlGlobalFunctions.RgbCompose( - Number(args, 0), Number(args, 1), Number(args, 2))); - if (count >= 1) - { - AviUtlGlobalFunctions.RgbComponents(Number(args, 0), out int r, out int g, out int b); - return DynValue.NewTuple( - DynValue.NewNumber(r), - DynValue.NewNumber(g), - DynValue.NewNumber(b)); - } - return DynValue.Nil; - }); - - globals["HSV"] = DynValue.NewCallback((_, args) => - { - int count = args.Count; - if (count >= 6) - return DynValue.NewNumber(AviUtlGlobalFunctions.HsvInterpolate( - Number(args, 0), Number(args, 1), Number(args, 2), - Number(args, 3), Number(args, 4), Number(args, 5), - timeRatio())); - if (count >= 3) - return DynValue.NewNumber(AviUtlGlobalFunctions.HsvCompose( - Number(args, 0), Number(args, 1), Number(args, 2))); - if (count >= 1) - { - AviUtlGlobalFunctions.HsvComponents(Number(args, 0), out int h, out int s, out int v); - return DynValue.NewTuple( - DynValue.NewNumber(h), - DynValue.NewNumber(s), - DynValue.NewNumber(v)); - } - return DynValue.Nil; - }); - - globals["OR"] = DynValue.NewCallback((_, args) => - DynValue.NewNumber(AviUtlGlobalFunctions.BitOr(Number(args, 0), Number(args, 1)))); - - globals["AND"] = DynValue.NewCallback((_, args) => - DynValue.NewNumber(AviUtlGlobalFunctions.BitAnd(Number(args, 0), Number(args, 1)))); - - globals["XOR"] = DynValue.NewCallback((_, args) => - DynValue.NewNumber(AviUtlGlobalFunctions.BitXor(Number(args, 0), Number(args, 1)))); - - globals["SHIFT"] = DynValue.NewCallback((_, args) => - DynValue.NewNumber(AviUtlGlobalFunctions.BitShift(Number(args, 0), Number(args, 1)))); + var value = args[0]; + DebugOutput.Write(value.Type == DataType.String ? value.String : value.ToPrintString()); + } + return DynValue.Void; } + [LuaFunction("RGB")] + private DynValue Rgb(CallbackArguments args) + { + int count = args.Count; + if (count >= 6) + return DynValue.NewNumber(AviUtlGlobalFunctions.RgbInterpolate( + Number(args, 0), Number(args, 1), Number(args, 2), + Number(args, 3), Number(args, 4), Number(args, 5), + timeRatio())); + if (count >= 3) + return DynValue.NewNumber(AviUtlGlobalFunctions.RgbCompose( + Number(args, 0), Number(args, 1), Number(args, 2))); + if (count >= 1) + { + AviUtlGlobalFunctions.RgbComponents(Number(args, 0), out int r, out int g, out int b); + return DynValue.NewTuple( + DynValue.NewNumber(r), + DynValue.NewNumber(g), + DynValue.NewNumber(b)); + } + return DynValue.Nil; + } + + [LuaFunction("HSV")] + private DynValue Hsv(CallbackArguments args) + { + int count = args.Count; + if (count >= 6) + return DynValue.NewNumber(AviUtlGlobalFunctions.HsvInterpolate( + Number(args, 0), Number(args, 1), Number(args, 2), + Number(args, 3), Number(args, 4), Number(args, 5), + timeRatio())); + if (count >= 3) + return DynValue.NewNumber(AviUtlGlobalFunctions.HsvCompose( + Number(args, 0), Number(args, 1), Number(args, 2))); + if (count >= 1) + { + AviUtlGlobalFunctions.HsvComponents(Number(args, 0), out int h, out int s, out int v); + return DynValue.NewTuple( + DynValue.NewNumber(h), + DynValue.NewNumber(s), + DynValue.NewNumber(v)); + } + return DynValue.Nil; + } + + [LuaFunction("OR", "a", "b")] + private DynValue Or(CallbackArguments args) => + DynValue.NewNumber(AviUtlGlobalFunctions.BitOr(Number(args, 0), Number(args, 1))); + + [LuaFunction("AND", "a", "b")] + private DynValue And(CallbackArguments args) => + DynValue.NewNumber(AviUtlGlobalFunctions.BitAnd(Number(args, 0), Number(args, 1))); + + [LuaFunction("XOR", "a", "b")] + private DynValue Xor(CallbackArguments args) => + DynValue.NewNumber(AviUtlGlobalFunctions.BitXor(Number(args, 0), Number(args, 1))); + + [LuaFunction("SHIFT", "a", "b")] + private DynValue Shift(CallbackArguments args) => + DynValue.NewNumber(AviUtlGlobalFunctions.BitShift(Number(args, 0), Number(args, 1))); + private static double Number(CallbackArguments args, int index) => args.Count > index ? args[index].CastToNumber() ?? 0d : 0d; } diff --git a/LuaScript.Runtime/LuaScriptEngine.cs b/LuaScript.Runtime/LuaScriptEngine.cs index d67a440..c8af60f 100644 --- a/LuaScript.Runtime/LuaScriptEngine.cs +++ b/LuaScript.Runtime/LuaScriptEngine.cs @@ -1,4 +1,5 @@ using LuaScript.Anchor; +using LuaScript.Api; using LuaScript.Compat; using MoonSharp.Interpreter; using MoonSharp.Interpreter.Debugging; @@ -6,13 +7,14 @@ using MoonSharp.Interpreter.Loaders; namespace LuaScript { - internal sealed class LuaScriptEngine : IDisposable + internal sealed partial class LuaScriptEngine : IDisposable { private readonly record struct ExecutionResult( LuaScriptException? Exception, bool TimedOut); - private sealed class ExecutionThread : IDisposable + [LuaTable("obj")] + private sealed partial class ExecutionThread : IDisposable { private sealed class CancellationDebugger : IDebugger { @@ -76,6 +78,9 @@ namespace LuaScript private CancellationToken _activeCancellation; private AviUtlScriptContext? _activeContext; + private AviUtlGlobalRegistrar? _globalRegistrar; + private SceneTableRegistrar? _sceneRegistrar; + internal ExecutionThread(Func mediaLoaderFactory) { _mediaLoaderFactory = mediaLoaderFactory; @@ -167,7 +172,8 @@ namespace LuaScript CoreModules.ErrorHandling); script.Options.ScriptLoader = new DisabledFileScriptLoader(); script.AttachDebugger(_debugger); - AviUtlGlobalRegistrar.RegisterFunctions(script.Globals, CurrentTimeRatio); + _globalRegistrar = new AviUtlGlobalRegistrar(CurrentTimeRatio); + _globalRegistrar.RegisterLuaMembers(script.Globals); _script = script; } @@ -224,7 +230,7 @@ namespace LuaScript script.Globals["scene"] = sceneTable; var objTable = new Table(script); - RegisterObjectCallbacks(objTable); + RegisterLuaMembers(objTable); script.Globals["obj"] = objTable; var animTable = new Table(script); @@ -255,7 +261,7 @@ namespace LuaScript if (!ReferenceEquals(script.Globals.Get("obj").Table, _objTable)) { var objTable = new Table(script); - RegisterObjectCallbacks(objTable); + RegisterLuaMembers(objTable); script.Globals["obj"] = objTable; _objTable = objTable; _objTableSnapshot = null; @@ -284,72 +290,10 @@ namespace LuaScript ResetUserTableKeys(_ymm4Table!, _ymm4TableSnapshot); } - script.Globals["time"] = ctx.Time; - script.Globals["frame"] = ctx.Frame; - script.Globals["totalframe"] = ctx.TotalFrame; - script.Globals["framerate"] = ctx.Framerate; - script.Globals["timelineframe"] = ctx.TimelineFrame; - script.Globals["timelinetime"] = ctx.TimelineTime; - script.Globals["layer"] = ctx.Layer; - - _sceneTable!["width"] = ctx.SceneWidth; - _sceneTable!["height"] = ctx.SceneHeight; - _sceneTable!["cx"] = ctx.SceneWidth / 2d; - _sceneTable!["cy"] = ctx.SceneHeight / 2d; - - Ymm4TableRegistrar.UpdateVariables(_ymm4Table!, ctx); - - _objTable!["w"] = ctx.ImageWidth; - _objTable!["h"] = ctx.ImageHeight; - _objTable!["hw"] = ctx.ImageWidth / 2d; - _objTable!["hh"] = ctx.ImageHeight / 2d; - _objTable!["cx"] = ctx.ImageWidth / 2d; - _objTable!["cy"] = ctx.ImageHeight / 2d; - _objTable!["cz"] = 0d; - _objTable!["diagonal"] = Math.Sqrt((double)ctx.ImageWidth * ctx.ImageWidth + (double)ctx.ImageHeight * ctx.ImageHeight); - _objTable!["x"] = ctx.X; - _objTable!["y"] = ctx.Y; - _objTable!["z"] = ctx.Z; - _objTable!["ox"] = ctx.Ox; - _objTable!["oy"] = ctx.Oy; - _objTable!["oz"] = ctx.Oz; - _objTable!["sx"] = ctx.Sx; - _objTable!["sy"] = ctx.Sy; - _objTable!["sz"] = 1d; - _objTable!["zoom"] = ctx.Zoom; - _objTable!["aspect"] = ctx.Aspect; - _objTable!["alpha"] = ctx.Alpha; - _objTable!["rx"] = ctx.Rx; - _objTable!["ry"] = ctx.Ry; - _objTable!["rz"] = ctx.Rz; - _objTable!["rxr"] = ctx.RxRad; - _objTable!["ryr"] = ctx.RyRad; - _objTable!["rzr"] = ctx.RzRad; - _objTable!["track0"] = ctx.Track0; - _objTable!["track1"] = ctx.Track1; - _objTable!["track2"] = ctx.Track2; - _objTable!["track3"] = ctx.Track3; - _objTable!["slider0"] = ctx.Slider0; - _objTable!["slider1"] = ctx.Slider1; - _objTable!["slider2"] = ctx.Slider2; - _objTable!["slider3"] = ctx.Slider3; - foreach (var parameter in ctx.StringParameters) - _objTable![parameter.Key] = parameter.Value; - _objTable!["check0"] = ctx.Check0; - _objTable!["check1"] = ctx.Check1; - _objTable!["check2"] = ctx.Check2; - _objTable!["check3"] = ctx.Check3; - if (ctx.HasColor) - script.Globals["color"] = ctx.ColorValue; - _objTable!["time"] = ctx.Time; - _objTable!["frame"] = ctx.Frame; - _objTable!["totalframe"] = ctx.TotalFrame; - _objTable!["totaltime"] = ctx.TotalTime; - _objTable!["t"] = ctx.TotalFrame > 0 ? ctx.Frame / (double)ctx.TotalFrame : 0d; - _objTable!["framerate"] = ctx.Framerate; - _objTable!["layer"] = ctx.Layer; - _objTable!["index"] = ctx.Index; - _objTable!["num"] = ctx.Num; + _globalRegistrar!.UpdateLuaMembers(script.Globals, ctx); + _sceneRegistrar!.UpdateLuaMembers(_sceneTable!, ctx); + Ymm4TableRegistrar.UpdateLuaMembers(_ymm4Table!, ctx); + UpdateLuaMembers(_objTable!, ctx); if (isFirstSetup) { @@ -423,8 +367,11 @@ namespace LuaScript private double CurrentTimeRatio() => _activeContext is { TotalTime: > 0d } context ? context.Time / context.TotalTime : 0d; - private void RegisterSceneCallbacks(Table scene) => - SceneTableRegistrar.RegisterFunctions(scene, GetSceneValue, SetSceneValue); + private void RegisterSceneCallbacks(Table scene) + { + _sceneRegistrar = new SceneTableRegistrar(GetSceneValue, SetSceneValue); + _sceneRegistrar.RegisterLuaMembers(scene); + } private SceneValue GetSceneValue(string name) { @@ -438,302 +385,489 @@ namespace LuaScript _activeContext?.SetSceneValue(name, value); } - private void RegisterObjectCallbacks(Table obj) + [LuaVariable("w")] + private int W(AviUtlScriptContext ctx) => ctx.ImageWidth; + + [LuaVariable("h")] + private int H(AviUtlScriptContext ctx) => ctx.ImageHeight; + + [LuaVariable("hw")] + private double Hw(AviUtlScriptContext ctx) => ctx.ImageWidth / 2d; + + [LuaVariable("hh")] + private double Hh(AviUtlScriptContext ctx) => ctx.ImageHeight / 2d; + + [LuaVariable("diagonal")] + private double Diagonal(AviUtlScriptContext ctx) => Math.Sqrt((double)ctx.ImageWidth * ctx.ImageWidth + (double)ctx.ImageHeight * ctx.ImageHeight); + + [LuaVariable("cx")] + private double Cx(AviUtlScriptContext ctx) => ctx.ImageWidth / 2d; + + [LuaVariable("cy")] + private double Cy(AviUtlScriptContext ctx) => ctx.ImageHeight / 2d; + + [LuaVariable("cz")] + private double Cz(AviUtlScriptContext ctx) => 0d; + + [LuaVariable("x")] + private double X(AviUtlScriptContext ctx) => ctx.X; + + [LuaVariable("y")] + private double Y(AviUtlScriptContext ctx) => ctx.Y; + + [LuaVariable("z")] + private double Z(AviUtlScriptContext ctx) => ctx.Z; + + [LuaVariable("ox")] + private double Ox(AviUtlScriptContext ctx) => ctx.Ox; + + [LuaVariable("oy")] + private double Oy(AviUtlScriptContext ctx) => ctx.Oy; + + [LuaVariable("oz")] + private double Oz(AviUtlScriptContext ctx) => ctx.Oz; + + [LuaVariable("sx")] + private double Sx(AviUtlScriptContext ctx) => ctx.Sx; + + [LuaVariable("sy")] + private double Sy(AviUtlScriptContext ctx) => ctx.Sy; + + [LuaVariable("sz", InCatalog = false)] + private double Sz(AviUtlScriptContext ctx) => 1d; + + [LuaVariable("zoom")] + private double Zoom(AviUtlScriptContext ctx) => ctx.Zoom; + + [LuaVariable("alpha")] + private double Alpha(AviUtlScriptContext ctx) => ctx.Alpha; + + [LuaVariable("aspect")] + private double Aspect(AviUtlScriptContext ctx) => ctx.Aspect; + + [LuaVariable("rx")] + private double Rx(AviUtlScriptContext ctx) => ctx.Rx; + + [LuaVariable("ry")] + private double Ry(AviUtlScriptContext ctx) => ctx.Ry; + + [LuaVariable("rz")] + private double Rz(AviUtlScriptContext ctx) => ctx.Rz; + + [LuaVariable("rxr")] + private double Rxr(AviUtlScriptContext ctx) => ctx.RxRad; + + [LuaVariable("ryr")] + private double Ryr(AviUtlScriptContext ctx) => ctx.RyRad; + + [LuaVariable("rzr")] + private double Rzr(AviUtlScriptContext ctx) => ctx.RzRad; + + [LuaVariable("track0")] + private double Track0(AviUtlScriptContext ctx) => ctx.Track0; + + [LuaVariable("track1")] + private double Track1(AviUtlScriptContext ctx) => ctx.Track1; + + [LuaVariable("track2")] + private double Track2(AviUtlScriptContext ctx) => ctx.Track2; + + [LuaVariable("track3")] + private double Track3(AviUtlScriptContext ctx) => ctx.Track3; + + [LuaVariable("slider0")] + private double Slider0(AviUtlScriptContext ctx) => ctx.Slider0; + + [LuaVariable("slider1")] + private double Slider1(AviUtlScriptContext ctx) => ctx.Slider1; + + [LuaVariable("slider2")] + private double Slider2(AviUtlScriptContext ctx) => ctx.Slider2; + + [LuaVariable("slider3")] + private double Slider3(AviUtlScriptContext ctx) => ctx.Slider3; + + [LuaVariable("check0")] + private bool Check0(AviUtlScriptContext ctx) => ctx.Check0; + + [LuaVariable("check1")] + private bool Check1(AviUtlScriptContext ctx) => ctx.Check1; + + [LuaVariable("check2")] + private bool Check2(AviUtlScriptContext ctx) => ctx.Check2; + + [LuaVariable("check3")] + private bool Check3(AviUtlScriptContext ctx) => ctx.Check3; + + [LuaVariable("text")] + private string? Text(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("text", out var value) ? value : null; + + [LuaVariable("font")] + private string? Font(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("font", out var value) ? value : null; + + [LuaVariable("dir")] + private string? Dir(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("dir", out var value) ? value : null; + + [LuaVariable("file_video")] + private string? FileVideo(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_video", out var value) ? value : null; + + [LuaVariable("file_audio")] + private string? FileAudio(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_audio", out var value) ? value : null; + + [LuaVariable("file_image")] + private string? FileImage(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_image", out var value) ? value : null; + + [LuaVariable("file_project")] + private string? FileProject(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_project", out var value) ? value : null; + + [LuaVariable("file_mp4")] + private string? FileMp4(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_mp4", out var value) ? value : null; + + [LuaVariable("file_exo")] + private string? FileExo(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_exo", out var value) ? value : null; + + [LuaVariable("file_subtitle")] + private string? FileSubtitle(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_subtitle", out var value) ? value : null; + + [LuaVariable("file_shader")] + private string? FileShader(AviUtlScriptContext ctx) => ctx.StringParameters.TryGetValue("file_shader", out var value) ? value : null; + + [LuaVariable("time")] + private double Time(AviUtlScriptContext ctx) => ctx.Time; + + [LuaVariable("totaltime")] + private double Totaltime(AviUtlScriptContext ctx) => ctx.TotalTime; + + [LuaVariable("t")] + private double T(AviUtlScriptContext ctx) => ctx.TotalFrame > 0 ? ctx.Frame / (double)ctx.TotalFrame : 0d; + + [LuaVariable("frame")] + private int Frame(AviUtlScriptContext ctx) => ctx.Frame; + + [LuaVariable("totalframe")] + private int Totalframe(AviUtlScriptContext ctx) => ctx.TotalFrame; + + [LuaVariable("framerate")] + private int Framerate(AviUtlScriptContext ctx) => ctx.Framerate; + + [LuaVariable("layer")] + private int Layer(AviUtlScriptContext ctx) => ctx.Layer; + + [LuaVariable("index")] + private int Index(AviUtlScriptContext ctx) => ctx.Index; + + [LuaVariable("num")] + private int Num(AviUtlScriptContext ctx) => ctx.Num; + [LuaFunction("getobject")] + private DynValue GetObject(ScriptExecutionContext execCtx, CallbackArguments args) { - obj["getobject"] = DynValue.NewCallback((execCtx, args) => + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count == 0) + return DynValue.Nil; + var tag = args[0]; + if (tag.Type != DataType.String) + return DynValue.Nil; + int frame = _activeContext.TimelineFrame; + if (args.Count >= 2) { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null || args.Count == 0) + var frameArg = args[1]; + if (frameArg.Type != DataType.Number) return DynValue.Nil; - var tag = args[0]; - if (tag.Type != DataType.String) - return DynValue.Nil; - int frame = _activeContext.TimelineFrame; - if (args.Count >= 2) + frame = (int)frameArg.Number; + } + if (!_activeContext.ResolveObject(tag.String, frame, out var info)) + return DynValue.Nil; + return BuildObjectTable(execCtx.GetScript(), info); + } + + [LuaFunction("getpixel")] + private DynValue GetPixel(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null) return DynValue.Nil; + int x = (int)(args[0].CastToNumber() ?? 0d); + int y = (int)(args[1].CastToNumber() ?? 0d); + var (r, g, b, a) = _activeContext.GetPixel(x, y); + return DynValue.NewTuple( + DynValue.NewNumber(r), + DynValue.NewNumber(g), + DynValue.NewNumber(b), + DynValue.NewNumber(a)); + } + + [LuaFunction("setpixel")] + private DynValue SetPixel(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null) return DynValue.Void; + int x = (int)(args[0].CastToNumber() ?? 0d); + int y = (int)(args[1].CastToNumber() ?? 0d); + double r = args[2].CastToNumber() ?? 0d; + double g = args[3].CastToNumber() ?? 0d; + double b = args[4].CastToNumber() ?? 0d; + double a = args.Count > 5 ? args[5].CastToNumber() ?? 255d : 255d; + _activeContext.SetPixel(x, y, r, g, b, a); + return DynValue.Void; + } + + [LuaFunction("getpixeldata")] + private DynValue GetPixelData() + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null) return DynValue.Nil; + _activeContext.EnsurePixelBuffer(); + return UserData.Create(new PixelDataProxy(_activeContext)); + } + + [LuaFunction("putpixeldata")] + private DynValue PutPixelData() => DynValue.Void; + + [LuaFunction("rand")] + private DynValue Rand(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + double frameDefault = _activeContext?.Frame ?? 0d; + double a = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; + double b = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; + double seed = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; + double frame = args.Count > 3 ? args[3].CastToNumber() ?? frameDefault : frameDefault; + return DynValue.NewNumber(AviUtlRandom.Next(a, b, seed, frame)); + } + + [LuaFunction("load")] + private DynValue Load(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + + switch (args[0].String) + { + case "figure" when args.Count >= 4: + LoadFigure( + args[1].Type == DataType.String ? args[1].String : string.Empty, + (int)(args[2].CastToNumber() ?? 0d), + args[3].CastToNumber() ?? 0d, + args.Count > 4 ? args[4].CastToNumber() ?? 0d : 0d, + args.Count > 5 ? Math.Clamp(args[5].CastToNumber() ?? 0d, -1d, 1d) : 0d); + break; + case "text": + LoadText(args.Count > 1 && args[1].Type == DataType.String ? args[1].String : string.Empty); + break; + case "image" when args.Count > 1 && args[1].Type == DataType.String: + LoadImage(args[1].String); + break; + case "movie" when args.Count > 1 && args[1].Type == DataType.String: + LoadMovie( + args[1].String, + args.Count > 2 ? args[2].CastToNumber() ?? _activeContext.Time : _activeContext.Time); + break; + } + return DynValue.Void; + } + + [LuaFunction("setfont")] + private DynValue SetFont(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + _fontState.Apply( + args.Count > 0 ? args[0] : DynValue.Nil, + args.Count > 1 ? args[1] : DynValue.Nil, + args.Count > 2 ? args[2] : DynValue.Nil, + args.Count > 3 ? args[3] : DynValue.Nil); + return DynValue.Void; + } + + [LuaFunction("draw")] + private DynValue Draw(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null) + return DynValue.Void; + + double ox = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; + double oy = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; + double oz = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; + double zoom = args.Count > 3 ? args[3].CastToNumber() ?? 1d : 1d; + double alpha = args.Count > 4 ? args[4].CastToNumber() ?? 1d : 1d; + double aspect = args.Count > 5 ? args[5].CastToNumber() ?? 0d : 0d; + + _activeContext.SubmitDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect, null, CurrentAntialias(), CurrentBlend())); + return DynValue.Void; + } + + [LuaFunction("drawpoly")] + private DynValue DrawPoly(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count < 12) + return DynValue.Void; + + var poly = new double[DrawPolyMath.Length]; + for (int i = 0; i < 12; i++) + poly[i] = args[i].CastToNumber() ?? 0d; + + if (args.Count >= 20) + { + for (int i = 0; i < 8; i++) + poly[12 + i] = args[12 + i].CastToNumber() ?? 0d; + } + else + { + double w = _activeContext.ImageWidth; + double h = _activeContext.ImageHeight; + poly[12] = 0d; poly[13] = 0d; + poly[14] = w; poly[15] = 0d; + poly[16] = w; poly[17] = h; + poly[18] = 0d; poly[19] = h; + } + + poly[20] = args.Count switch + { + 13 => args[12].CastToNumber() ?? 1d, + >= 21 => args[20].CastToNumber() ?? 1d, + _ => 1d, + }; + + _activeContext.SubmitDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly, CurrentAntialias(), CurrentBlend())); + return DynValue.Void; + } + + [LuaFunction("copybuffer")] + private DynValue CopyBuffer(CallbackArguments 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; + } + + [LuaFunction("getvalue")] + private DynValue GetValue(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.NewNumber(0d); + var value = _objTable!.Get(args[0].String); + return value.Type == DataType.Number ? value : DynValue.NewNumber(0d); + } + + [LuaFunction("setoption")] + private DynValue SetOption(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + var name = args[0].String; + var value = args.Count > 1 ? args[1] : DynValue.True; + _options[name] = value; + if (name == "draw_state" && _activeContext is not null) + _activeContext.DrawStateOverride = value.Type == DataType.Boolean + ? value.Boolean + : (value.CastToNumber() ?? 0d) != 0d; + if (name == "drawtarget" && _activeContext is not null) + { + if (value.Type == DataType.String && value.String == "tempbuffer") { - var frameArg = args[1]; - if (frameArg.Type != DataType.Number) - return DynValue.Nil; - frame = (int)frameArg.Number; - } - if (!_activeContext.ResolveObject(tag.String, frame, out var info)) - return DynValue.Nil; - return BuildObjectTable(execCtx.GetScript(), info); - }); - - obj["getpixel"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null) return DynValue.Nil; - int x = (int)(args[0].CastToNumber() ?? 0d); - int y = (int)(args[1].CastToNumber() ?? 0d); - var (r, g, b, a) = _activeContext.GetPixel(x, y); - return DynValue.NewTuple( - DynValue.NewNumber(r), - DynValue.NewNumber(g), - DynValue.NewNumber(b), - DynValue.NewNumber(a)); - }); - - obj["setpixel"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null) return DynValue.Void; - int x = (int)(args[0].CastToNumber() ?? 0d); - int y = (int)(args[1].CastToNumber() ?? 0d); - double r = args[2].CastToNumber() ?? 0d; - double g = args[3].CastToNumber() ?? 0d; - double b = args[4].CastToNumber() ?? 0d; - double a = args.Count > 5 ? args[5].CastToNumber() ?? 255d : 255d; - _activeContext.SetPixel(x, y, r, g, b, a); - return DynValue.Void; - }); - - obj["getpixeldata"] = DynValue.NewCallback((_, _) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null) return DynValue.Nil; - _activeContext.EnsurePixelBuffer(); - return UserData.Create(new PixelDataProxy(_activeContext)); - }); - - obj["putpixeldata"] = DynValue.NewCallback((_, _) => DynValue.Void); - - obj["rand"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - double frameDefault = _activeContext?.Frame ?? 0d; - double a = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; - double b = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; - double seed = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; - double frame = args.Count > 3 ? args[3].CastToNumber() ?? frameDefault : frameDefault; - return DynValue.NewNumber(AviUtlRandom.Next(a, b, seed, frame)); - }); - - obj["load"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Void; - - switch (args[0].String) - { - case "figure" when args.Count >= 4: - LoadFigure( - args[1].Type == DataType.String ? args[1].String : string.Empty, - (int)(args[2].CastToNumber() ?? 0d), - args[3].CastToNumber() ?? 0d, - args.Count > 4 ? args[4].CastToNumber() ?? 0d : 0d, - args.Count > 5 ? Math.Clamp(args[5].CastToNumber() ?? 0d, -1d, 1d) : 0d); - break; - case "text": - LoadText(args.Count > 1 && args[1].Type == DataType.String ? args[1].String : string.Empty); - break; - case "image" when args.Count > 1 && args[1].Type == DataType.String: - LoadImage(args[1].String); - break; - case "movie" when args.Count > 1 && args[1].Type == DataType.String: - LoadMovie( - args[1].String, - args.Count > 2 ? args[2].CastToNumber() ?? _activeContext.Time : _activeContext.Time); - break; - } - return DynValue.Void; - }); - - obj["setfont"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - _fontState.Apply( - args.Count > 0 ? args[0] : DynValue.Nil, - args.Count > 1 ? args[1] : DynValue.Nil, - args.Count > 2 ? args[2] : DynValue.Nil, - args.Count > 3 ? args[3] : DynValue.Nil); - return DynValue.Void; - }); - - obj["effect"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Void; - - string name = args[0].String; - var arguments = new List>(); - for (int i = 1; i + 1 < args.Count; i += 2) - { - if (args[i].Type != DataType.String) - continue; - var value = args[i + 1]; - object boxed = value.Type switch - { - DataType.Number => value.Number, - DataType.Boolean => value.Boolean, - DataType.String => value.String, - _ => value.CastToNumber() ?? 0d, - }; - arguments.Add(new KeyValuePair(args[i].String, boxed)); - } - - _activeContext.AddEffect(new AviUtlEffectRequest(name, arguments)); - return DynValue.Void; - }); - - obj["draw"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null) - return DynValue.Void; - - double ox = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; - double oy = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; - double oz = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; - double zoom = args.Count > 3 ? args[3].CastToNumber() ?? 1d : 1d; - double alpha = args.Count > 4 ? args[4].CastToNumber() ?? 1d : 1d; - double aspect = args.Count > 5 ? args[5].CastToNumber() ?? 0d : 0d; - - _activeContext.SubmitDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect, null, CurrentAntialias(), CurrentBlend())); - return DynValue.Void; - }); - - obj["drawpoly"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null || args.Count < 12) - return DynValue.Void; - - var poly = new double[DrawPolyMath.Length]; - for (int i = 0; i < 12; i++) - poly[i] = args[i].CastToNumber() ?? 0d; - - if (args.Count >= 20) - { - for (int i = 0; i < 8; i++) - poly[12 + i] = args[12 + i].CastToNumber() ?? 0d; + bool hasSize = args.Count > 3; + int w = hasSize ? (int)(args[2].CastToNumber() ?? 0d) : 0; + int h = hasSize ? (int)(args[3].CastToNumber() ?? 0d) : 0; + _activeContext.SetDrawTarget(true, w, h, hasSize); } else { - double w = _activeContext.ImageWidth; - double h = _activeContext.ImageHeight; - poly[12] = 0d; poly[13] = 0d; - poly[14] = w; poly[15] = 0d; - poly[16] = w; poly[17] = h; - poly[18] = 0d; poly[19] = h; + _activeContext.SetDrawTarget(false, 0, 0, false); } + } + return DynValue.Void; + } - poly[20] = args.Count switch + [LuaFunction("getoption")] + private DynValue GetOption(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.NewNumber(0d); + return _options.TryGetValue(args[0].String, out var value) ? value : DynValue.NewNumber(0d); + } + + [LuaFunction("pixeloption")] + private DynValue PixelOption(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + _pixelOptions[args[0].String] = args.Count > 1 ? args[1] : DynValue.True; + return DynValue.Void; + } + + [LuaFunction("setanchor")] + private DynValue SetAnchor(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || _script is null || args.Count < 2 || args[0].Type != DataType.String) + return DynValue.NewNumber(0d); + + var anchorName = args[0].String; + int count = AnchorSupport.ClampCount((int)(args[1].CastToNumber() ?? 0d)); + if (string.Equals(anchorName, AnchorSupport.TrackGroup, StringComparison.Ordinal)) + return DynValue.NewNumber(0d); + + var connection = AnchorConnection.None; + bool is3D = false; + for (int i = 2; i < args.Count; i++) + { + if (args[i].Type == DataType.String) + AnchorSupport.ApplyOption(args[i].String, ref connection, ref is3D); + } + + int stride = is3D ? 3 : 2; + var table = new Table(_script); + var source = _activeContext.AnchorSource; + for (int i = 0; i < count; i++) + { + AnchorSupport.ResolvePosition(source, anchorName, i, out double x, out double y, out double z); + table.Set(i * stride + 1, DynValue.NewNumber(x)); + table.Set(i * stride + 2, DynValue.NewNumber(y)); + if (is3D) + table.Set(i * stride + 3, DynValue.NewNumber(z)); + } + _script.Globals.Set(anchorName, DynValue.NewTable(table)); + + _activeContext.AddAnchorRequest(new AnchorRequestData(anchorName, count, connection, is3D)); + return DynValue.NewNumber(count); + } + + [LuaFunction("effect")] + private DynValue Effect(CallbackArguments args) + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + + string name = args[0].String; + var arguments = new List>(); + for (int i = 1; i + 1 < args.Count; i += 2) + { + if (args[i].Type != DataType.String) + continue; + var value = args[i + 1]; + object boxed = value.Type switch { - 13 => args[12].CastToNumber() ?? 1d, - >= 21 => args[20].CastToNumber() ?? 1d, - _ => 1d, + DataType.Number => value.Number, + DataType.Boolean => value.Boolean, + DataType.String => value.String, + _ => value.CastToNumber() ?? 0d, }; + arguments.Add(new KeyValuePair(args[i].String, boxed)); + } - _activeContext.SubmitDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly, CurrentAntialias(), CurrentBlend())); - 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; - }); - - obj["getvalue"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.NewNumber(0d); - var value = _objTable!.Get(args[0].String); - return value.Type == DataType.Number ? value : DynValue.NewNumber(0d); - }); - - obj["setanchor"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (_activeContext is null || _script is null || args.Count < 2 || args[0].Type != DataType.String) - return DynValue.NewNumber(0d); - - var anchorName = args[0].String; - int count = AnchorSupport.ClampCount((int)(args[1].CastToNumber() ?? 0d)); - if (string.Equals(anchorName, AnchorSupport.TrackGroup, StringComparison.Ordinal)) - return DynValue.NewNumber(0d); - - var connection = AnchorConnection.None; - bool is3D = false; - for (int i = 2; i < args.Count; i++) - { - if (args[i].Type == DataType.String) - AnchorSupport.ApplyOption(args[i].String, ref connection, ref is3D); - } - - int stride = is3D ? 3 : 2; - var table = new Table(_script); - var source = _activeContext.AnchorSource; - for (int i = 0; i < count; i++) - { - AnchorSupport.ResolvePosition(source, anchorName, i, out double x, out double y, out double z); - table.Set(i * stride + 1, DynValue.NewNumber(x)); - table.Set(i * stride + 2, DynValue.NewNumber(y)); - if (is3D) - table.Set(i * stride + 3, DynValue.NewNumber(z)); - } - _script.Globals.Set(anchorName, DynValue.NewTable(table)); - - _activeContext.AddAnchorRequest(new AnchorRequestData(anchorName, count, connection, is3D)); - return DynValue.NewNumber(count); - }); - - obj["setoption"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Void; - var name = args[0].String; - var value = args.Count > 1 ? args[1] : DynValue.True; - _options[name] = value; - if (name == "draw_state" && _activeContext is not null) - _activeContext.DrawStateOverride = value.Type == DataType.Boolean - ? value.Boolean - : (value.CastToNumber() ?? 0d) != 0d; - if (name == "drawtarget" && _activeContext is not null) - { - if (value.Type == DataType.String && value.String == "tempbuffer") - { - bool hasSize = args.Count > 3; - int w = hasSize ? (int)(args[2].CastToNumber() ?? 0d) : 0; - int h = hasSize ? (int)(args[3].CastToNumber() ?? 0d) : 0; - _activeContext.SetDrawTarget(true, w, h, hasSize); - } - else - { - _activeContext.SetDrawTarget(false, 0, 0, false); - } - } - return DynValue.Void; - }); - - obj["getoption"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.NewNumber(0d); - return _options.TryGetValue(args[0].String, out var value) ? value : DynValue.NewNumber(0d); - }); - - obj["pixeloption"] = DynValue.NewCallback((_, args) => - { - _activeCancellation.ThrowIfCancellationRequested(); - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Void; - _pixelOptions[args[0].String] = args.Count > 1 ? args[1] : DynValue.True; - return DynValue.Void; - }); + _activeContext.AddEffect(new AviUtlEffectRequest(name, arguments)); + return DynValue.Void; } private double CurrentAntialias() => diff --git a/LuaScript.Runtime/SceneTableRegistrar.cs b/LuaScript.Runtime/SceneTableRegistrar.cs index a7481d8..ba603e2 100644 --- a/LuaScript.Runtime/SceneTableRegistrar.cs +++ b/LuaScript.Runtime/SceneTableRegistrar.cs @@ -1,54 +1,70 @@ +using LuaScript.Api; using MoonSharp.Interpreter; namespace LuaScript { - internal static class SceneTableRegistrar + [LuaTable("scene")] + internal sealed partial class SceneTableRegistrar(Func getValue, Action setValue) { - internal static void RegisterFunctions(Table scene, Func getValue, Action setValue) + internal static void RegisterFunctions(Table scene, Func getValue, Action setValue) => + new SceneTableRegistrar(getValue, setValue).RegisterLuaMembers(scene); + + [LuaVariable("width")] + private int Width(AviUtlScriptContext ctx) => ctx.SceneWidth; + + [LuaVariable("height")] + private int Height(AviUtlScriptContext ctx) => ctx.SceneHeight; + + [LuaVariable("cx")] + private double Cx(AviUtlScriptContext ctx) => ctx.SceneWidth / 2d; + + [LuaVariable("cy")] + private double Cy(AviUtlScriptContext ctx) => ctx.SceneHeight / 2d; + + [LuaFunction("set", "name", "value")] + private DynValue Set(CallbackArguments args) { - scene["set"] = DynValue.NewCallback((_, args) => - { - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Void; - - var value = args.Count > 1 ? args[1] : DynValue.Nil; - SceneValue stored; - switch (value.Type) - { - case DataType.Number: - stored = SceneValue.FromNumber(value.Number); - break; - case DataType.String: - stored = SceneValue.FromString(value.String); - break; - case DataType.Boolean: - stored = SceneValue.FromBoolean(value.Boolean); - break; - case DataType.Nil: - case DataType.Void: - stored = SceneValue.Nil; - break; - default: - return DynValue.Void; - } - setValue(args[0].String, stored); + if (args.Count == 0 || args[0].Type != DataType.String) return DynValue.Void; - }); - scene["get"] = DynValue.NewCallback((_, args) => + var value = args.Count > 1 ? args[1] : DynValue.Nil; + SceneValue stored; + switch (value.Type) { - if (args.Count == 0 || args[0].Type != DataType.String) - return DynValue.Nil; + case DataType.Number: + stored = SceneValue.FromNumber(value.Number); + break; + case DataType.String: + stored = SceneValue.FromString(value.String); + break; + case DataType.Boolean: + stored = SceneValue.FromBoolean(value.Boolean); + break; + case DataType.Nil: + case DataType.Void: + stored = SceneValue.Nil; + break; + default: + return DynValue.Void; + } + setValue(args[0].String, stored); + return DynValue.Void; + } - var value = getValue(args[0].String); - return value.Kind switch - { - SceneValueKind.Number => DynValue.NewNumber(value.Number), - SceneValueKind.String => DynValue.NewString(value.Text ?? string.Empty), - SceneValueKind.Boolean => value.Number != 0d ? DynValue.True : DynValue.False, - _ => DynValue.Nil, - }; - }); + [LuaFunction("get", "name")] + private DynValue Get(CallbackArguments args) + { + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Nil; + + var value = getValue(args[0].String); + return value.Kind switch + { + SceneValueKind.Number => DynValue.NewNumber(value.Number), + SceneValueKind.String => DynValue.NewString(value.Text ?? string.Empty), + SceneValueKind.Boolean => value.Number != 0d ? DynValue.True : DynValue.False, + _ => DynValue.Nil, + }; } } } diff --git a/LuaScript.Runtime/Ymm4TableRegistrar.cs b/LuaScript.Runtime/Ymm4TableRegistrar.cs index 9cbc40c..390fc7c 100644 --- a/LuaScript.Runtime/Ymm4TableRegistrar.cs +++ b/LuaScript.Runtime/Ymm4TableRegistrar.cs @@ -1,21 +1,39 @@ -using MoonSharp.Interpreter; +using LuaScript.Api; namespace LuaScript { - internal static class Ymm4TableRegistrar + [LuaTable("ymm4")] + internal static partial class Ymm4TableRegistrar { - internal static void UpdateVariables(Table ymm4, AviUtlScriptContext ctx) - { - ymm4["group_index"] = ctx.GroupIndex; - ymm4["group_count"] = ctx.GroupCount; - ymm4["group_ratio"] = ctx.GroupCount > 0 ? ctx.GroupIndex / (double)ctx.GroupCount : 0d; - ymm4["timeline_totalframe"] = ctx.TimelineTotalFrame; - ymm4["timeline_totaltime"] = ctx.TimelineTotalTime; - ymm4["is_saving"] = ctx.IsSaving; - ymm4["time_ratio"] = ctx.TimeRatio; - ymm4["is_playing"] = ctx.IsPlaying; - ymm4["is_paused"] = ctx.IsPaused; - ymm4["scene_id"] = ctx.SceneId; - } + [LuaVariable("group_index")] + private static int GroupIndex(AviUtlScriptContext ctx) => ctx.GroupIndex; + + [LuaVariable("group_count")] + private static int GroupCount(AviUtlScriptContext ctx) => ctx.GroupCount; + + [LuaVariable("group_ratio")] + private static double GroupRatio(AviUtlScriptContext ctx) => + ctx.GroupCount > 0 ? ctx.GroupIndex / (double)ctx.GroupCount : 0d; + + [LuaVariable("timeline_totalframe")] + private static int TimelineTotalFrame(AviUtlScriptContext ctx) => ctx.TimelineTotalFrame; + + [LuaVariable("timeline_totaltime")] + private static double TimelineTotalTime(AviUtlScriptContext ctx) => ctx.TimelineTotalTime; + + [LuaVariable("is_saving")] + private static bool IsSaving(AviUtlScriptContext ctx) => ctx.IsSaving; + + [LuaVariable("time_ratio")] + private static double TimeRatio(AviUtlScriptContext ctx) => ctx.TimeRatio; + + [LuaVariable("is_playing")] + private static bool IsPlaying(AviUtlScriptContext ctx) => ctx.IsPlaying; + + [LuaVariable("is_paused")] + private static bool IsPaused(AviUtlScriptContext ctx) => ctx.IsPaused; + + [LuaVariable("scene_id")] + private static string SceneId(AviUtlScriptContext ctx) => ctx.SceneId; } }