TableHelper = TableHelper or {} ---@param obj table ---@param NoCopyList table | string ---@return table function TableHelper.DeepCopy(obj, NoCopyList) 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赋给新表 if NoCopyList ~= nil then local NoCopys = false; if type(NoCopyList) == 'table' then for c, d in pairs(NoCopyList) do if type(v) == d then NoCopys = true break end end elseif type(NoCopyList) == 'string' then if type(v) == NoCopyList then NoCopys = true; end end if not NoCopys then NewTable[Func(k)] = Func(v); end else NewTable[Func(k)] = Func(v); end 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 -- 拷贝table function table.DeepCopy(object) -- 已经复制过的table,key为复制源table,value为复制后的table -- 为了防止table中的某个属性为自身时出现死循环 -- 避免本该是同一个table的属性,在复制时变成2个不同的table(内容同,但是地址关系和原来的不一样了) local lookup_table = {} local function _copy(object) if type(object) ~= 'table' then -- 非table类型都直接返回 return object elseif lookup_table[object] then return lookup_table[object] end local new_table = {} lookup_table[object] = new_table for k, v in pairs(object) do new_table[_copy(k)] = _copy(v) end -- 这里直接拿mt来用是因为一般对table操作不会很粗暴的修改mt的相关内容 return setmetatable(new_table, getmetatable(object)) end return _copy(object) end ---获取表元素个数 function table.getCount(t) if type(t) ~= "table" then return -1 end local Length = 0 for i, v in pairs(t) do Length = Length + 1 end return Length end ---获取表中所有的Key function table.getKeys(t) if type(t) ~= "table" then return {} end local keys = {} for k,v in pairs(t) do keys[#keys + 1] = k end return keys end ---获取对应元素的Index function table.getIndex(t, v) if type(t) ~= "table" then return nil end for index, value in pairs(t) do if value == v then return index end end return nil end ---获取表中是否有对应Key function table.hasKey(t, k) if type(t) ~= "table" then return false end for key, value in pairs(t) do if k == key then return true end end return false end ---按Key删除表中元素 function table.removeKey(t, k) if t == nil then return nil end local v = t[k] t[k] = nil return v end ---获取表中是否有对应的Value function table.hasValue(t, value) if t == nil then return false end for k, v in pairs(t) do if v == value then return true end end return false end ---按Value删除表中元素 function table.removeValue(t, value, removeAll) local deleteNum = 0 local i = 1 local max = table.getCount(t) while i <= max do if t[i] == value then table.remove(t,i) deleteNum = deleteNum + 1 i = i - 1 max = max - 1 if not removeAll then break end end i = i + 1 end return deleteNum end ---返回是否是空表 function table.isEmpty(t) if type(t) ~= "table" then return true end return next(t) == nil end function table.Rand(t) if type(t) ~= "table" or #t <= 0 then return nil end return t[math.random(1, #t)] end function table.GetMaxValueResKey(t, fun) if fun == nil then fun = function(p1, p2) return p1 < p2 end end if type(t) ~= "table" or type(fun) ~= "function" or table.getCount(t) <= 0 then return end local res = nil for k, v in pairs(t) do if not res or fun(t[res], v) then res = k end end return res end function table.FindKey(t, value) if type(t) ~= "table" then return nil end for k, v in pairs(t) do if v == value then return k end end return nil end function table.GetMaxValue(t) if type(t) ~= "table" then return nil end local Res = nil for k, v in pairs(t) do if Res == nil then Res = v elseif Res < v then Res = v end end return Res end function table.Swap(t, i, k) if type(t) ~= "table" then return false end local temp = t[i] t[i] = t[k] t[k] = temp end --- 打乱整个table 洗牌算法 function table.Shuffle(t) if type(t) ~= "table" then return false end for i = 1, #t - 1 do local SwapIndex = math.random(i + 1, #t) table.Swap(t, i, SwapIndex) end return true end function table.addTableNum(t, k, v) if t[k] == nil then t[k] = v else t[k] = t[k] + v end return t[k] end ---@param f fun(...:any): any function table.func(f, ...) if f ~= nil and type(f) == 'function' then return f(...) end end function TableHelper.PrintEnumName(EnumTable, InIndex) for i, v in pairs(EnumTable) do if v == InIndex then return i; end end return ''; end function TableHelper.CompareTables(t1, t2) if t1 == nil or t2 == nil then return false; end if type(t1) ~= type(t2) then return false; end for i, v in pairs(t1) do if type(v) == 'table' then if t2[1] ~= nil then return TableHelper.CompareTables(v, t2[i]); end return false; else if v ~= t2[i] then return false; end end end return true; end ---------------------------------------------------------table print--------------------------------------------------------- -- log输出格式化 local function logPrint(str) str = os.date("\nLog output date: %Y-%m-%d %H:%M:%S \n", os.time()) .. str print(str) end -- key值格式化 local function formatKey(key) local t = type(key) if t == "number" then return "[" .. key .. "]" elseif t == "string" then local n = tonumber(key) if n then return "[" .. key .. "]" end end return key end return TableHelper;