2025-01-04 23:00:19 +08:00

209 lines
4.7 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

TableHelper = TableHelper or {}
---@param obj table
---@return table
function TableHelper.DeepCopy(obj)
if obj == nil then
return {};
end
local InTable = {};
local function Func(o)
if type(o) ~= "table" then --判断表中是否有表
return o;
end
local NewTable = {}; --定义一个新表
InTable[o] = NewTable; --若表中有表则先把表给InTable再用NewTable去接收内嵌的表
for k,v in pairs(o) do --把旧表的key和Value赋给新表
NewTable[Func(k)] = Func(v);
end
return setmetatable(NewTable, getmetatable(o))--赋值元表
end
return Func(obj) --若表中有表,则把内嵌的表也复制了
end
---@param InSelf table
---@param InTable table
function TableHelper.CopyToTable(InSelf, InTable)
if InSelf == nil or InTable == nil then return end
for i, v in pairs(InTable) do
if type(v) == 'table' then
TableHelper.CopyToTable(InSelf[i], v);
else
InSelf[i] = v;
end
end
end
---@param Tab table
---@return table
function TableHelper.CopyTable(Tab)
local NewTab = {}
for k, v in pairs(Tab) do NewTab[k] = v; end
return NewTab
end
---@param TableData table
---@return table
function TableHelper.DeepCopyTable(TableData)
if TableData == nil then
UGCLogSystem.LogError("表格为空,不能复制");
return {};
end
local TmpTab = {};
for k, v in pairs(TableData) do
if type(v) == "table" then
local SubTab = TableHelper.DeepCopyTable(v);
TmpTab[k] = SubTab;
-- elseif type(v) == "userdata" then
-- TmpTab[k] = v:Copy();
else
TmpTab[k] = v;
end
end
return TmpTab;
end
--- 返回值的下标,没有返回-1
---@param tab table
---@param val any
---@return bool, int32
function TableHelper.GetValueIndex(tab, val)
for k, v in pairs(tab) do
if v == val then return true, k; end
end
return false, -1;
end
---@param t table
---@param v any
function TableHelper.RemoveByValue(t, v)
local _i = nil;
for i, Val in pairs(t) do
if Val == v then
_i = i;
break;
end
end
table.remove(t, _i);
end
function TableHelper.Contains(tab, val)
for i, Val in pairs(tab) do
if Val == val then return true; end
end
return false;
end
function TableHelper.GetName(Actor)
if UE.IsValid(Actor) then
return UE.GetPathName(Actor);
elseif Actor ~= nil then
return type(Actor);
end
return "[NullActor]";
end
function TableHelper.Length(InTable)
if InTable == nil then return 0; end
local Count = 0
for _ in pairs(InTable) do Count = Count + 1; end
return Count;
end
function TableHelper.AppendToTable(OriginalTable, NewTable)
if OriginalTable == nil or NewTable == nil then return; end
for k, v in pairs(NewTable) do table.insert(OriginalTable, v); end
end
function TableHelper.ArrayToLuaTable(Array)
local Tab = {};
for k, v in pairs(Array) do Tab[k] = v; end
return Tab;
end
function class(c, s)
local superType = type(s);
local cls;
if superType ~= "function" and superType ~= "table" then
superType = nil
s = nil
end
if superType == "function" or (s and s.__ctype == 1) then
-- inherited from native C++ Object
cls = {}
if superType == "table" then
-- copy fields from s
for k, v in pairs(s) do cls[k] = v; end
cls.__create = s.__create
cls.s = s
else
cls.__create = s
cls.Ctor = function() end
end
cls.__cname = c
cls.__ctype = 1
function cls.New(...)
local i = cls.__create(...)
-- copy fields from class to native object
for k, v in pairs(cls) do
i[k] = v
end
i.class = cls
i:Ctor(...)
return i
end
else
-- inherited from Lua Object
if s then
cls = {}
setmetatable(cls, {
__index = s
})
cls.s = s
else
cls = {
Ctor = function() end
}
end
cls.__cname = c
cls.__ctype = 2 -- lua
cls.__index = cls
function cls.New(...)
local i = setmetatable({}, cls)
i.class = cls
i:Ctor(...)
return i
end
end
return cls
end
function TableHelper.printEnum(InEnum, InV)
for i, v in pairs(InEnum) do
if v == InV then return i end
end
return '';
end
return TableHelper;