Skip to content

Commit

Permalink
fix #460
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Mar 22, 2024
1 parent e6c615c commit 5eea197
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 5 deletions.
1 change: 1 addition & 0 deletions CSharp.lua/CoreSystem.Lua/All.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down
135 changes: 135 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Numerics/Vector.lua
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions CSharp.lua/System.xml
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,22 @@ limitations under the License.
<method name="op_Inequality" Template="{0} ~= {1}" />
<method name="op_Implicit" Name="ToComplex" />
</class>
<class name="Vector`1" Name="System.Numerics.Vector">
<property name="Count">
<get Template="{class}:Count()" />
</property>
<property name="One">
<get Template="{class}:One()" />
</property>
<property name="IsSupported">
<get Template="{class}:IsSupported()" />
</property>
<method name="op_Subtraction" Template="{0} - {1}" />
<method name="op_Multiply" Template="{0} * {1}" />
<method name="op_Division" Template="{0} / {1}" />
<method name="op_Equality" Template="{0} == {1}" />
<method name="op_Inequality" Template="{0} ~= {1}" />
</class>
</namespace>
<namespace name="System.Runtime.CompilerServices" Name="System" />
</assembly>
Expand Down

0 comments on commit 5eea197

Please sign in to comment.