mirror of
https://github.com/wxd-gaming/test-all.git
synced 2026-05-08 06:28:01 +08:00
359 lines
11 KiB
Lua
359 lines
11 KiB
Lua
--- 辅助调试代码
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
--- Created by 無心道(15388152619).
|
|
--- DateTime: 2024/10/31 19:30
|
|
|
|
require("LuaProfiler")
|
|
|
|
gameDebug = {}
|
|
gameDebug.__index = gameDebug
|
|
|
|
---当前lua运行环境路径
|
|
function gameDebug.getCurrentDirectory()
|
|
local platform = package.config:sub(1, 1)
|
|
if platform == "\\" then
|
|
-- Windows
|
|
local handle = io.popen("cd")
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
return tostring(string.gsub(result, "\n$", ""))
|
|
else
|
|
-- Linux/Mac
|
|
local handle = io.popen("pwd")
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
return tostring(string.gsub(result, "\n$", ""))
|
|
end
|
|
end
|
|
|
|
---获取函数所在的文件名
|
|
function gameDebug.get_function_file(func)
|
|
local info = debug.getinfo(func, "S")
|
|
return info.short_src or "匿名文件"
|
|
end
|
|
|
|
---获取函数的名字
|
|
function gameDebug.get_function_name(func)
|
|
local info = debug.getinfo(func)
|
|
gameDebug.print(info)
|
|
return info.name or "匿名函数"
|
|
end
|
|
|
|
---获取函数所在的文件名
|
|
function gameDebug.getFunctionInfo(func)
|
|
local debugInfo = debug.getinfo(func)
|
|
--获取不到函数名字,只能又文件名和行数表达了
|
|
return (debugInfo.short_src or "匿名文件") .. ":" .. tostring(debugInfo.linedefined)
|
|
end
|
|
|
|
---获取函数所在文件行号
|
|
function gameDebug.getFunctionLineNumber(func)
|
|
local debugInfo = debug.getinfo(func)
|
|
--获取不到函数名字,只能又文件名和行数表达了
|
|
return debugInfo.linedefined or -1
|
|
end
|
|
|
|
--- 把 table 数据转化成json字符串
|
|
--- @param tab table 数据类型
|
|
---@param appendYinhao boolean 是否添加引号
|
|
---@param appendType boolean 是否添加类型
|
|
function gameDebug.toTableJson(tab, appendYinhao, appendType)
|
|
local json = ""
|
|
for k, v in pairs(tab) do
|
|
if not (json == nil or json == "") then
|
|
json = json .. ", "
|
|
end
|
|
json = json .. gameDebug.toString(k, appendYinhao, appendType) .. ":" .. gameDebug.toString(v, appendYinhao, appendType)
|
|
end
|
|
local var = "{" .. json .. "}"
|
|
return var
|
|
end
|
|
|
|
--- 把数组转换成字符串
|
|
--- @param arr any 数组数据类型
|
|
---@param appendYinhao boolean 是否添加引号
|
|
---@param appendType boolean 是否添加类型
|
|
function gameDebug.toArrayJson(arr, appendYinhao, appendType)
|
|
local json = ""
|
|
local success, result = pcall(function()
|
|
for i, v in ipairs(arr) do
|
|
if not (json == nil or json == "") then
|
|
json = json .. ", "
|
|
end
|
|
local var = type(v)
|
|
if var == "function" then
|
|
_LUA_Error("d")
|
|
else
|
|
json = json .. gameDebug.toString(v, appendYinhao, appendType)
|
|
end
|
|
end
|
|
end)
|
|
--_LUA_Print(type(arr), result)
|
|
if not success then
|
|
--_LUA_Print("gameDebug.toArrayJson error: " .. result)
|
|
if (json == nil or json == "") then
|
|
if type(arr) == "userdata" then
|
|
-- 这里可能是luaj的数组
|
|
local len = arr.length
|
|
local len_type = type(len)
|
|
if len_type == "number" then
|
|
for i = 1, len, 1 do
|
|
if not (json == nil or json == "") then
|
|
json = json .. ", "
|
|
end
|
|
json = json .. gameDebug.toString(arr[i], appendYinhao, appendType)
|
|
end
|
|
elseif len_type == "function" then
|
|
json = arr:toString()
|
|
else
|
|
json = tostring(arr)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local var = "[" .. json .. "]"
|
|
return var
|
|
end
|
|
|
|
--- 把对象转化成字符串
|
|
--- @param obj any 参数
|
|
--- @param appendYinhao boolean 是否添加引号
|
|
--- @param appendType boolean 是否添加类型
|
|
function gameDebug.toString(obj, appendYinhao, appendType)
|
|
if obj == nil or obj == "nil" then
|
|
if appendType then
|
|
return "【nil】 nil";
|
|
end
|
|
return "nil";
|
|
end
|
|
local typeString = type(obj)
|
|
--_LUA_Print("gameDebug.toString", typeString, obj)
|
|
if typeString == "number" or typeString == "boolean" then
|
|
if appendType then
|
|
return "【" .. typeString .. "】 " .. tostring(obj)
|
|
end
|
|
return tostring(obj)
|
|
elseif typeString == 'string' then
|
|
local str = tostring(obj);
|
|
if appendYinhao then
|
|
str = "\"" .. tostring(obj) .. "\""
|
|
end
|
|
if appendType then
|
|
str = "【string】 " .. str
|
|
end
|
|
return str
|
|
elseif typeString == 'cdata' then
|
|
local str = tostring(obj)
|
|
-- 获取字符串的长度
|
|
local len = string.len(str)
|
|
-- 检查字符串最后一个字符是否是目标字符
|
|
if string.sub(str, len - 2, len) == 'ULL' then
|
|
-- 删除最后一个字符
|
|
str = string.sub(str, 1, len - 3)
|
|
elseif string.sub(str, len - 1, len) == 'LL' then
|
|
-- 删除最后一个字符
|
|
str = string.sub(str, 1, len - 2)
|
|
end
|
|
if appendType then
|
|
str = "【long】 " .. str
|
|
end
|
|
return str
|
|
elseif typeString == 'table' then
|
|
local str = gameDebug.toTableJson(obj, true, appendType)
|
|
if appendType then
|
|
str = "【" .. typeString .. "】 " .. str
|
|
end
|
|
return str
|
|
elseif typeString == "function" then
|
|
local s, e = pcall(function()
|
|
return obj:toString()
|
|
end)
|
|
if not s then
|
|
s, e = pcall(function()
|
|
return obj.toString()
|
|
end)
|
|
end
|
|
if not s then
|
|
s, e = pcall(function()
|
|
return tostring(obj)
|
|
end)
|
|
end
|
|
return e
|
|
else
|
|
local str = gameDebug.toArrayJson(obj, true, appendType)
|
|
if appendType then
|
|
str = "【" .. typeString .. "】 " .. str
|
|
end
|
|
return str
|
|
end
|
|
end
|
|
|
|
--- 把对象转化成字符串
|
|
---@param split string 分隔符
|
|
--- @param ... any 参数
|
|
function gameDebug.toStrings(split, ...)
|
|
return gameDebug.toStrings0(false, false, split, ...)
|
|
end
|
|
|
|
--- 把对象转化成字符串,保护数据类型
|
|
---@param split string 分隔符
|
|
--- @param ... any 参数
|
|
function gameDebug.toStringsType(split, ...)
|
|
return gameDebug.toStrings0(false, true, split, ...)
|
|
end
|
|
|
|
--- 把对象转化成字符串
|
|
---@param split string 分隔符
|
|
--- @param appendYinhao boolean 是否添加引号
|
|
--- @param appendType boolean 是否添加类型
|
|
--- @param ... any 参数
|
|
function gameDebug.toStrings0(appendYinhao, appendType, split, ...)
|
|
local printString = ""
|
|
local tmp = { ... }
|
|
local _, _ = pcall(function()
|
|
for i, v in pairs(tmp) do
|
|
if not (printString == nil or printString == "") then
|
|
printString = printString .. split
|
|
end
|
|
printString = printString .. gameDebug.toString(v, appendYinhao, appendType)
|
|
end
|
|
end)
|
|
return printString
|
|
end
|
|
|
|
--- 打印参数信息
|
|
function gameDebug.print(...)
|
|
gameDebug.print0(false, true, false, ...)
|
|
end
|
|
|
|
--- 打印参数信息, 输出变量类型
|
|
function gameDebug.printType(...)
|
|
gameDebug.print0(false, true, true, ...)
|
|
end
|
|
|
|
--- 打印参数信息,并且打印调用堆栈
|
|
function gameDebug.printTraceback(...)
|
|
gameDebug.print0(true, true, false, ...)
|
|
end
|
|
|
|
--- 打印参数信息,并且打印调用堆栈 输出变量类型
|
|
function gameDebug.printTracebackType(...)
|
|
gameDebug.print0(true, true, true, ...)
|
|
end
|
|
|
|
--- 打印参数信息,并且打印调用堆栈
|
|
--- @param isTraceback boolean 是否打印堆栈
|
|
--- @param appendYinhao boolean 是否添加引号
|
|
--- @param appendType boolean 是否添加类型
|
|
--- @param ... any 参数
|
|
function gameDebug.print0(isTraceback, appendYinhao, appendType, ...)
|
|
local printString = ""
|
|
local tmp = { ... }
|
|
local success, result = pcall(function()
|
|
for i, v in pairs(tmp) do
|
|
if not (printString == nil or printString == "") then
|
|
printString = printString .. ",\n"
|
|
end
|
|
printString = printString .. " " .. gameDebug.toString(v, appendYinhao, appendType)
|
|
end
|
|
end)
|
|
printString = "===================参数======================\n" .. "[\n" .. printString .. "\n]"
|
|
if isTraceback then
|
|
printString = printString .. "\n===================堆栈=======================\n" .. debug.traceback("")
|
|
end
|
|
printString = printString .. "\n===================结束=======================\n"
|
|
print(printString)
|
|
end
|
|
|
|
--- 辅助调试
|
|
--- @param fun function 函数
|
|
--- @param ... any 如果调用 函数 异常后打印你需要显示的参数
|
|
function gameDebug.debug(fun, ...)
|
|
gameDebug.debug0(fun, true, false, ...)
|
|
end
|
|
|
|
--- 辅助调试 输出变量类型
|
|
--- @param fun function 函数
|
|
--- @param ... any 如果调用 函数 异常后打印你需要显示的参数
|
|
function gameDebug.debugType(fun, ...)
|
|
gameDebug.debug0(fun, true, true, ...)
|
|
end
|
|
|
|
function gameDebug.debug0(fun, appendYinhao, appendType, ...)
|
|
local f_success, f_error = xpcall(fun, debug.traceback, ...)
|
|
if not f_success then
|
|
local printString = ""
|
|
local tmp = { ... }
|
|
local s, e = pcall(function()
|
|
for i, v in pairs(tmp) do
|
|
if not (printString == nil or printString == "") then
|
|
printString = printString .. ",\n"
|
|
end
|
|
printString = printString .. " " .. gameDebug.toString(v, appendYinhao, appendType)
|
|
end
|
|
end)
|
|
print("===================参数======================\n"
|
|
.. "[\n" .. printString .. "\n]" ..
|
|
"\n===================堆栈=======================\n"
|
|
.. f_error ..
|
|
"\n===================结束=======================\n"
|
|
)
|
|
end
|
|
end
|
|
|
|
--- 断言对象为nil
|
|
---@param b boolean false 会抛出异常导致程序终止运行
|
|
function gameDebug.assertTrue(b, ...)
|
|
if not b then
|
|
gameDebug.error(...)
|
|
end
|
|
end
|
|
|
|
--- 断言 当 o1 ~= o2 会抛出异常导致程序终止运行
|
|
---@param o1 any
|
|
---@param o2 any
|
|
function gameDebug.assertEquals(o1, o2, ...)
|
|
gameDebug.assertTrue(o1 ~= o2, ...)
|
|
end
|
|
|
|
--- 断言对象为nil
|
|
function gameDebug.assertNil(obj, ...)
|
|
if obj == nil then
|
|
gameDebug.error(...)
|
|
end
|
|
end
|
|
|
|
--- 断言对象为nil
|
|
function gameDebug.assertNotNil(obj, ...)
|
|
if obj ~= nil then
|
|
gameDebug.error(...)
|
|
end
|
|
end
|
|
|
|
--- 断言 仅仅只是 print 输出
|
|
---@param b boolean false 仅仅只是 print 输出
|
|
function gameDebug.assertPrint(b, ...)
|
|
if not b then
|
|
local msg = gameDebug.toStrings0(false, false, " ", ...)
|
|
_LUA_Print(msg)
|
|
end
|
|
end
|
|
|
|
--- 断言 仅仅只是 print 输出堆栈
|
|
---@param b boolean false 仅仅只是 print 输出
|
|
function gameDebug.assertPrintTrace(b, ...)
|
|
if not b then
|
|
local msg = gameDebug.toStrings0(false, false, " ", ...)
|
|
local traceback = debug.traceback(msg)
|
|
_LUA_Print(traceback)
|
|
end
|
|
end
|
|
|
|
--- 会抛出异常导致程序终止运行
|
|
function gameDebug.error(...)
|
|
local var = gameDebug.toStrings0(false, false, " ", ...)
|
|
var = debug.traceback(var)
|
|
_LUA_Error(var)
|
|
end
|
|
|
|
return gameDebug |