jxlast/lua_probject/base_project/Core/functions.lua

304 lines
7.2 KiB
Lua
Raw Permalink Normal View History

2025-11-14 23:38:35 +08:00
local setmetatable = setmetatable
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
-- 刘海偏移
function get_offset(full_offset)
if full_offset then
local r = GRoot.inst.width / GRoot.inst.height
if r >= 2 then
local d = (Screen.dpi /160)
return d * 20
end
end
return 0
end
---lua table 浅拷贝
function membe_clone(orig)
local copy
if type(orig) == "table" then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
-- lua table 深拷贝
function membe_deep_clone(orig)
local copy
if type(orig) == "table" then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[membe_deep_clone(orig_key)] = membe_deep_clone(orig_value)
end
else
copy = orig
end
return copy
end
---字符串分割函数
function split(str, delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
function handler( obj,func)
return function(...)
return func(obj, ...)
end
end
--重新require一个lua文件替代系统文件。
function unimport(moduleName,currentModuleName)
local package = package
local currentModuleNameParts
local moduleFullName = moduleName
local offset = 1
while true do
if string.byte(moduleName, offset) ~= 46 then -- .
moduleFullName = string.sub(moduleName, offset)
if currentModuleNameParts and #currentModuleNameParts > 0 then
moduleFullName = table.concat(currentModuleNameParts, ".") .. "." .. moduleFullName
end
break
end
offset = offset + 1
if not currentModuleNameParts then
if not currentModuleName then
local n,v = debug.getlocal(3, 1)
currentModuleName = v
end
currentModuleNameParts = string.split(currentModuleName, ".")
end
table.remove(currentModuleNameParts, #currentModuleNameParts)
end
package.loaded[moduleFullName] = nil
package.preload[moduleFullName] = nil
end
function table.nums(t)
local count = 0
for k, v in pairs(t) do
count = count + 1
end
return count
end
-- @param t 要检查的表格(t表示是table)
-- @param table 返回指定表格的所有键key,它是一个键集合的表格
--]]
function table.keys( t )
local keys = {}
for k, _ in pairs( t ) do
keys[#keys + 1] = k
end
return keys
end
function list_remove(t,item)
for i,v in ipairs(t) do
if v == item then
table.remove(t,i)
break
end
end
end
function list_check(t,item)
for i,v in ipairs(t) do
if v == item then
return true
end
end
return false
end
function list_index(t,item)
for i,v in ipairs(t) do
if v == item then
return i
end
end
return 0
end
function list_concat(des,tag)
for i = 1, #tag do
table.insert(des, tag[i])
end
return des
end
function vardump(object, label)
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end
local function _vardump(object, label, indent, nest)
label = label or "<var>"
local postfix = ""
if nest > 1 then postfix = "," end
if type(object) ~= "table" then
-- if type(label) == "string" then
result[#result +1] = string.format("%s%s = %s%s", indent, label, _v(object), postfix)
-- else
-- result[#result +1] = string.format("%s%s%s", indent, _v(object), postfix)
-- end
elseif not lookupTable[object] then
lookupTable[object] = true
-- if type(label) == "string" then
result[#result +1 ] = string.format("%s%s = {", indent, label)
-- else
-- result[#result +1 ] = string.format("%s{", indent)
-- end
local indent2 = indent .. " "
local keys = {}
local values = {}
for k, v in pairs(object) do
keys[#keys + 1] = k
values[k] = v
end
table.sort(keys, function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_vardump(values[k], k, indent2, nest + 1)
end
result[#result +1] = string.format("%s}%s", indent, postfix)
end
end
_vardump(object, label, "", 1)
return table.concat(result, "\n")
end
function sysrandom(min,max)
local num = math.random(min,max)
return num
end
function IsHasDictionary(currentTarget,list)
if list and #list>0 then
for k,v in ipairs(list) do
if v==currentTarget then
return true
end
end
return false
else
return false
end
end
function CheckDictionaryFromContent(target,list)
if list and #list>0 then
for k,v in pairs(list) do
if v.card==target then
return true,k
end
end
else
return false
end
end
function CombineDictionaryAndRemoveSomeItem(list1,list2)
local tempList=membe_clone(list2)
for i=1,#list1 do
if IsHasDictionary(list1[i],list2)==false then
table.insert(tempList,list1[i])
end
end
return tempList
end