From 5eea1976ab6c5945936d0da43da85fee88e9e92f Mon Sep 17 00:00:00 2001 From: yanghuan Date: Fri, 22 Mar 2024 15:01:50 +0800 Subject: [PATCH] fix #460 --- CSharp.lua/CoreSystem.Lua/All.lua | 1 + .../CoreSystem.Lua/CoreSystem/DateTime.lua | 10 +- .../CoreSystem/Numerics/Vector.lua | 135 ++++++++++++++++++ CSharp.lua/System.xml | 16 +++ 4 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 CSharp.lua/CoreSystem.Lua/CoreSystem/Numerics/Vector.lua diff --git a/CSharp.lua/CoreSystem.Lua/All.lua b/CSharp.lua/CoreSystem.Lua/All.lua index 2fedc8bb..85bf7542 100644 --- a/CSharp.lua/CoreSystem.Lua/All.lua +++ b/CSharp.lua/CoreSystem.Lua/All.lua @@ -65,6 +65,7 @@ return function(dir, conf) load("Numerics.Matrix4x4") load("Numerics.Plane") load("Numerics.Quaternion") + load("Numerics.Vector") load("Numerics.Vector2") load("Numerics.Vector3") load("Numerics.Vector4") diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua index 0953b2ba..6a8ff25b 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua @@ -514,7 +514,7 @@ DateTime = System.defStc("System.DateTime", { AddHours = function (this, hours) return add(this, hours, 3600000) end, - AddMinutes = function (this, minutes) + AddMinutes = function (this, minutes) return add(this, minutes, 60000); end, AddSeconds = function (this, seconds) @@ -527,7 +527,7 @@ DateTime = System.defStc("System.DateTime", { AddMonths = addMonths, AddYears = function (this, years) if years < - 10000 or years > 10000 then - throw(ArgumentOutOfRangeException("years")) + throw(ArgumentOutOfRangeException("years")) end return addMonths(this, years * 12) end, @@ -578,7 +578,7 @@ DateTime = System.defStc("System.DateTime", { return now():getDate() end, ToLocalTime = function (this) - if this.kind == 2 then + if this.kind == 2 then return this end local ticks = this.ticks + timeZoneTicks + dstTicks @@ -595,8 +595,8 @@ DateTime = System.defStc("System.DateTime", { return this.kind == 2 and dstTicks > 0 end, ToString = function (this, format) - if format then - return toStringWithFormat(this, format) + if format then + return toStringWithFormat(this, format) end local year, month, day = getDatePart(this.ticks) return sformat("%d/%d/%d %02d:%02d:%02d", year, month, day, this:getHour(), this:getMinute(), this:getSecond()) diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Numerics/Vector.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Numerics/Vector.lua new file mode 100644 index 00000000..56061ddd --- /dev/null +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Numerics/Vector.lua @@ -0,0 +1,135 @@ +local System = System +local throw = System.throw + +local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException +local ArgumentNullException = System.ArgumentNullException +local NotSupportedException = System.NotSupportedException + +local type = type +local getmetatable = getmetatable +local setmetatable = setmetatable + +local counts = { + [System.Byte] = 32, + [System.Int16] = 16, + [System.Int32] = 8, + [System.Single] = 8, + [System.Double] = 4, +} + +local function getCount(T) + local count = counts[T] + if not count then + throw(NotSupportedException("Specified type is not supported")) + end + return count +end + +local function default(T) + return T(0) +end + +local Vector = { + __ctor__ = function (this, values, index) + local count = this:Count() + if index == nil then + if type(values) == "number" then + for i = 1, count do + this[i] = values + end + return + else + index = 0 + end + end + if values == nil then + throw(ArgumentNullException()) + end + if (index < 0) or (#values - index < count) then + throw(ArgumentOutOfRangeException("IndexMustBeLessOrEqual")) + end + for i = 1, count do + this[i] = values[index + i] + end + end, + get = function (this, index) + local count = this:Count() + if (index < 0) or (index >= count) then + throw(ArgumentOutOfRangeException("index")) + end + return this[index + 1] + end, + Count = function (T) + return getCount(T.__genericT__) + end, + One = function (T) + return T(1) + end, + IsSupported = function (T) + return counts[T.__genericT__] ~= nil + end, + Zero = default, + default = default, + ToString = function (this) + return '<' .. table.concat(this, ', ') .. '>' + end, + __eq = function (a, b) + local n = #a + if n ~= #b then + return false + end + for i = 1, n do + if a[i] ~= b[i] then + return false + end + end + return true + end, + __sub = function (a, b) + local t, n = {}, #a + for i = 1, n do + t[i] = a[i] - b[i] + end + return setmetatable(t, getmetatable(a)) + end, + __mul = function (a, b) + if type(a) == "number" then + a, b = b, a + end + local t, n = {}, #a + if type(b) == "number" then + for i = 1, n do + t[i] = a[i] * b + end + else + for i = 1, n do + t[i] = a[i] * b[i] + end + end + return setmetatable(t, getmetatable(a)) + end, + __div = function (a, b) + local t, n = {}, #a + if type(b) == "number" then + for i = 1, n do + t[i] = a[i] / b + end + else + for i = 1, n do + t[i] = a[i] / b[i] + end + end + return setmetatable(t, getmetatable(a)) + end, +} + +System.defStc("System.Numerics.Vector", function(T) + return { + __genericT__ = T, + base = { System.IEquatable_1(T) }, + __eq = Vector.__eq, + __sub = Vector.__sub, + __mul = Vector.__mul, + __div = Vector.__div, + } +end, Vector, 1) diff --git a/CSharp.lua/System.xml b/CSharp.lua/System.xml index 7ce06b3f..32edd32a 100644 --- a/CSharp.lua/System.xml +++ b/CSharp.lua/System.xml @@ -829,6 +829,22 @@ limitations under the License. + + + + + + + + + + + + + + + +