Browse Source

Change: methods -> property

AnnulusGames 11 months ago
parent
commit
86ae57af1d

+ 21 - 69
src/Lua.Unity/Assets/Lua.Unity/Runtime/ColorLibrary.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+using Lua.Runtime;
 using UnityEngine;
 
 namespace Lua.Unity
@@ -10,84 +11,35 @@ namespace Lua.Unity
         public static readonly ColorLibrary Instance = new();
 
         public readonly LuaFunction[] Functions;
+        public readonly LuaTable Metatable = new();
 
         public ColorLibrary()
         {
             Functions = new LuaFunction[]
             {
-                new("black", Black),
-                new("blue", Blue),
-                new("clear", Clear),
-                new("cyan", Cyan),
-                new("gray", Gray),
-                new("green", Green),
-                new("magenta", Magenta),
-                new("red", Red),
-                new("white", White),
-                new("yellow", Yellow),
                 new("hsv_to_rgb", HSVToRGB),
                 new("rgb_to_hsv", RGBToHSV),
             };
-        }
-
-        public static ValueTask<int> Black(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.black);
-            return new(1);
-        }
-
-        public static ValueTask<int> Blue(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.blue);
-            return new(1);
-        }
-
-        public static ValueTask<int> Clear(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.clear);
-            return new(1);
-        }
-
-        public static ValueTask<int> Cyan(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.cyan);
-            return new(1);
-        }
-
-        public static ValueTask<int> Gray(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.gray);
-            return new(1);
-        }
-
-        public static ValueTask<int> Green(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.green);
-            return new(1);
-        }
-
-        public static ValueTask<int> Magenta(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.magenta);
-            return new(1);
-        }
-
-        public static ValueTask<int> Red(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.red);
-            return new(1);
-        }
-
-        public static ValueTask<int> White(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.white);
-            return new(1);
-        }
 
-        public static ValueTask<int> Yellow(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaColor(Color.yellow);
-            return new(1);
+            Metatable[Metamethods.Index] = new LuaFunction((context, buffer, ct) =>
+            {
+                var name = context.GetArgument<string>(1);
+                buffer.Span[0] = name switch
+                {
+                    "black" => new LuaColor(Color.black),
+                    "blue" => new LuaColor(Color.blue),
+                    "clear" => new LuaColor(Color.clear),
+                    "cyan" => new LuaColor(Color.cyan),
+                    "gray" => new LuaColor(Color.gray),
+                    "green" => new LuaColor(Color.green),
+                    "magenta" => new LuaColor(Color.magenta),
+                    "red" => new LuaColor(Color.red),
+                    "white" => new LuaColor(Color.white),
+                    "yellow" => new LuaColor(Color.yellow),
+                    _ => LuaValue.Nil,
+                };
+                return new(1);
+            });
         }
 
         public static ValueTask<int> HSVToRGB(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)

+ 3 - 0
src/Lua.Unity/Assets/Lua.Unity/Runtime/LuaStateExtensions.cs

@@ -9,6 +9,7 @@ namespace Lua.Unity
             {
                 vector2[func.Name] = func;
             }
+            vector2.Metatable = Vector2Library.Instance.Metatable;
             state.Environment["vector2"] = vector2;
 
             var vector3 = new LuaTable(0, Vector3Library.Instance.Functions.Length);
@@ -16,6 +17,7 @@ namespace Lua.Unity
             {
                 vector3[func.Name] = func;
             }
+            vector3.Metatable = Vector3Library.Instance.Metatable;
             state.Environment["vector3"] = vector3;
 
             var color = new LuaTable(0, ColorLibrary.Instance.Functions.Length);
@@ -23,6 +25,7 @@ namespace Lua.Unity
             {
                 color[func.Name] = func;
             }
+            color.Metatable = ColorLibrary.Instance.Metatable;
             state.Environment["color"] = color;
         }
     }

+ 31 - 0
src/Lua.Unity/Assets/Lua.Unity/Runtime/TimeLibrary.cs

@@ -0,0 +1,31 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using UnityEngine;
+
+namespace Lua.Unity
+{
+    public sealed class TimeLibrary
+    {
+        public static readonly TimeLibrary Instance = new();
+
+        public readonly LuaFunction[] functions = new LuaFunction[]
+        {
+            new("time", GetTime),
+            new("delta_time", GetDeltaTime),
+        };
+
+        public static ValueTask<int> GetTime(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+        {
+            buffer.Span[0] = Time.timeAsDouble;
+            return new(1);
+        }
+
+        public static ValueTask<int> GetDeltaTime(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+        {
+            buffer.Span[0] = Time.deltaTime;
+            return new(1);
+        }
+
+    }
+}

+ 17 - 41
src/Lua.Unity/Assets/Lua.Unity/Runtime/Vector2Library.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+using Lua.Runtime;
 using UnityEngine;
 
 namespace Lua.Unity
@@ -10,17 +11,12 @@ namespace Lua.Unity
         public static readonly Vector2Library Instance = new();
 
         public readonly LuaFunction[] Functions;
+        public readonly LuaTable Metatable = new();
 
         public Vector2Library()
         {
             Functions = new LuaFunction[]
             {
-                new("zero", Zero),
-                new("one", One),
-                new("right", Right),
-                new("left", Left),
-                new("up", Up),
-                new("down", Down),
                 new("angle", Angle),
                 new("distance", Distance),
                 new("dot", Dot),
@@ -33,42 +29,22 @@ namespace Lua.Unity
                 new("scale", Scale),
                 new("signed_angle", SignedAngle),
             };
-        }
-
-        public ValueTask<int> Zero(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.zero);
-            return new(1);
-        }
-
-        public ValueTask<int> One(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.one);
-            return new(1);
-        }
-
-        public ValueTask<int> Right(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.right);
-            return new(1);
-        }
-
-        public ValueTask<int> Left(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.left);
-            return new(1);
-        }
-
-        public ValueTask<int> Up(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.up);
-            return new(1);
-        }
 
-        public ValueTask<int> Down(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector2(Vector2.down);
-            return new(1);
+            Metatable[Metamethods.Index] = new LuaFunction((context, buffer, ct) =>
+            {
+                var name = context.GetArgument<string>(1);
+                buffer.Span[0] = name switch
+                {
+                    "zero" => new LuaVector2(Vector2.zero),
+                    "one" => new LuaVector2(Vector2.one),
+                    "right" => new LuaVector2(Vector2.right),
+                    "left" => new LuaVector2(Vector2.left),
+                    "up" => new LuaVector2(Vector2.up),
+                    "down" => new LuaVector2(Vector2.down),
+                    _ => LuaValue.Nil,
+                };
+                return new(1);
+            });
         }
 
         public ValueTask<int> Angle(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)

+ 19 - 55
src/Lua.Unity/Assets/Lua.Unity/Runtime/Vector3Library.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+using Lua.Runtime;
 using UnityEngine;
 
 namespace Lua.Unity
@@ -10,19 +11,12 @@ namespace Lua.Unity
         public static readonly Vector3Library Instance = new();
 
         public readonly LuaFunction[] Functions;
+        public readonly LuaTable Metatable = new();
 
         public Vector3Library()
         {
             Functions = new LuaFunction[]
             {
-                new("zero", Zero),
-                new("one", One),
-                new("right", Right),
-                new("left", Left),
-                new("up", Up),
-                new("down", Down),
-                new("forward", Forward),
-                new("back", Back),
                 new("angle", Angle),
                 new("cross", Cross),
                 new("distance", Distance),
@@ -41,54 +35,24 @@ namespace Lua.Unity
                 new("slerp", Slerp),
                 new("slerp_unclamped", SlerpUnclamped),
             };
-        }
-
-        public ValueTask<int> Zero(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.zero);
-            return new(1);
-        }
-
-        public ValueTask<int> One(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.one);
-            return new(1);
-        }
-
-        public ValueTask<int> Right(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.right);
-            return new(1);
-        }
-
-        public ValueTask<int> Left(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.left);
-            return new(1);
-        }
-
-        public ValueTask<int> Up(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.up);
-            return new(1);
-        }
-
-        public ValueTask<int> Down(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.down);
-            return new(1);
-        }
-
-        public ValueTask<int> Forward(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.forward);
-            return new(1);
-        }
 
-        public ValueTask<int> Back(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = new LuaVector3(Vector3.back);
-            return new(1);
+            Metatable[Metamethods.Index] = new LuaFunction((context, buffer, ct) =>
+            {
+                var name = context.GetArgument<string>(1);
+                buffer.Span[0] = name switch
+                {
+                    "zero" => new LuaVector3(Vector3.zero),
+                    "one" => new LuaVector3(Vector3.one),
+                    "right" => new LuaVector3(Vector2.right),
+                    "left" => new LuaVector3(Vector3.left),
+                    "up" => new LuaVector3(Vector3.up),
+                    "down" => new LuaVector3(Vector3.down),
+                    "forward" => new LuaVector3(Vector3.forward),
+                    "back" => new LuaVector3(Vector3.back),
+                    _ => LuaValue.Nil,
+                };
+                return new(1);
+            });
         }
 
         public ValueTask<int> Angle(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)