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

198 lines
4.6 KiB
Lua
Raw 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(obj)
if type(obj) ~= "table" then --判断表中是否有表
return obj;
end
local NewTable = {}; --定义一个新表
InTable[obj] = NewTable; --若表中有表则先把表给InTable再用NewTable去接收内嵌的表
for k,v in pairs(obj) do --把旧表的key和Value赋给新表
NewTable[Func(k)] = Func(v);
end
return setmetatable(NewTable, getmetatable(obj))--赋值元表
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 tab table
---@param val any
function TableHelper.RemoveByValue(tab, val)
for i, Val in pairs(tab) do
if Val == val then
table.remove(tab, i);
break;
end
end
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(classname, super)
local superType = type(super)
local cls
if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end
if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {}
if superType == "table" then
-- copy fields from super
for k, v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.Ctor = function()
end
end
cls.__cname = classname
cls.__ctype = 1
function cls.New(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k, v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:Ctor(...)
return instance
end
else
-- inherited from Lua Object
if super then
cls = {}
setmetatable(cls, { __index = super })
cls.super = super
else
cls = {
Ctor = function() end
}
end
cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls
function cls.New(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:Ctor(...)
return instance
end
end
return cls
end
return TableHelper;