_0GameDebug.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. --- 辅助调试代码
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by 無心道(15388152619).
  4. --- DateTime: 2024/10/31 19:30
  5. gameDebug = {}
  6. ---当前lua运行环境路径
  7. function gameDebug.getCurrentDirectory()
  8. local platform = package.config:sub(1, 1)
  9. if platform == "\\" then
  10. -- Windows
  11. local handle = io.popen("cd")
  12. local result = handle:read("*a")
  13. handle:close()
  14. return tostring(string.gsub(result, "\n$", ""))
  15. else
  16. -- Linux/Mac
  17. local handle = io.popen("pwd")
  18. local result = handle:read("*a")
  19. handle:close()
  20. return tostring(string.gsub(result, "\n$", ""))
  21. end
  22. end
  23. ---获取函数所在的文件名
  24. function gameDebug.get_function_file(func)
  25. local info = debug.getinfo(func)
  26. return info.source or "匿名文件"
  27. end
  28. ---获取函数的名字
  29. function gameDebug.get_function_name(func)
  30. local info = debug.getinfo(func)
  31. return info.name or "匿名函数"
  32. end
  33. ---获取函数所在的文件名
  34. function gameDebug.getFunctionInfo(func)
  35. local debugInfo = debug.getinfo(func)
  36. --获取不到函数名字,只能又文件名和行数表达了
  37. return (debugInfo.source or "匿名文件") .. ":" .. tostring(debugInfo.linedefined)
  38. end
  39. --- 把 table 数据转化成json字符串
  40. --- @param tab table 数据类型
  41. ---@param appendYinhao boolean 是否添加引号
  42. ---@param appendType boolean 是否添加类型
  43. function gameDebug.toTableJson(tab, appendYinhao, appendType)
  44. local json = ""
  45. for k, v in pairs(tab) do
  46. if not (json == nil or json == "") then
  47. json = json .. ", "
  48. end
  49. json = json .. gameDebug.toString(k, appendYinhao, appendType) .. ":" .. gameDebug.toString(v, appendYinhao, appendType)
  50. end
  51. local var = "{" .. json .. "}"
  52. return var
  53. end
  54. --- 把数组转换成字符串
  55. --- @param arr any 数组数据类型
  56. ---@param appendYinhao boolean 是否添加引号
  57. ---@param appendType boolean 是否添加类型
  58. function gameDebug.toArrayJson(arr, appendYinhao, appendType)
  59. local json = ""
  60. local success, result = pcall(function()
  61. for i, v in ipairs(arr) do
  62. if not (json == nil or json == "") then
  63. json = json .. ", "
  64. end
  65. local var = type(v)
  66. if var == "function" then
  67. _LUA_Error("d")
  68. else
  69. json = json .. gameDebug.toString(v, appendYinhao, appendType)
  70. end
  71. end
  72. end)
  73. --_LUA_Print(type(arr), result)
  74. if not success then
  75. --_LUA_Print("gameDebug.toArrayJson error: " .. result)
  76. if (json == nil or json == "") then
  77. if type(arr) == "userdata" then
  78. -- 这里可能是luaj的数组
  79. local len = arr.length
  80. local len_type = type(len)
  81. if len_type == "number" then
  82. for i = 1, len, 1 do
  83. if not (json == nil or json == "") then
  84. json = json .. ", "
  85. end
  86. json = json .. gameDebug.toString(arr[i], appendYinhao, appendType)
  87. end
  88. elseif len_type == "function" then
  89. json = arr:toString()
  90. else
  91. json = tostring(arr)
  92. end
  93. end
  94. end
  95. end
  96. local var = "[" .. json .. "]"
  97. return var
  98. end
  99. --- 把对象转化成字符串
  100. --- @param obj any 参数
  101. --- @param appendYinhao boolean 是否添加引号
  102. --- @param appendType boolean 是否添加类型
  103. function gameDebug.toString(obj, appendYinhao, appendType)
  104. if obj == nil or obj == "nil" then
  105. if appendType then
  106. return "【nil】 nil";
  107. end
  108. return "nil";
  109. end
  110. local typeString = type(obj)
  111. --_LUA_Print("gameDebug.toString", typeString, obj)
  112. if typeString == "number" or typeString == "boolean" then
  113. if appendType then
  114. return "【" .. typeString .. "】 " .. tostring(obj)
  115. end
  116. return tostring(obj)
  117. elseif typeString == 'string' then
  118. local str = tostring(obj);
  119. if appendYinhao then
  120. str = "\"" .. tostring(obj) .. "\""
  121. end
  122. if appendType then
  123. str = "【string】 " .. str
  124. end
  125. return str
  126. elseif typeString == 'cdata' then
  127. local str = tostring(obj)
  128. -- 获取字符串的长度
  129. local len = string.len(str)
  130. -- 检查字符串最后一个字符是否是目标字符
  131. if string.sub(str, len - 2, len) == 'ULL' then
  132. -- 删除最后一个字符
  133. str = string.sub(str, 1, len - 3)
  134. elseif string.sub(str, len - 1, len) == 'LL' then
  135. -- 删除最后一个字符
  136. str = string.sub(str, 1, len - 2)
  137. end
  138. if appendType then
  139. str = "【long】 " .. str
  140. end
  141. return str
  142. elseif typeString == 'table' then
  143. local str = gameDebug.toTableJson(obj, true, appendType)
  144. if appendType then
  145. str = "【" .. typeString .. "】 " .. str
  146. end
  147. return str
  148. elseif typeString == "function" then
  149. local s, e = pcall(function()
  150. return obj:toString()
  151. end)
  152. if not s then
  153. s, e = pcall(function()
  154. return obj.toString()
  155. end)
  156. end
  157. if not s then
  158. s, e = pcall(function()
  159. return tostring(obj)
  160. end)
  161. end
  162. return e
  163. else
  164. local str = gameDebug.toArrayJson(obj, true, appendType)
  165. if appendType then
  166. str = "【" .. typeString .. "】 " .. str
  167. end
  168. return str
  169. end
  170. end
  171. --- 把对象转化成字符串
  172. ---@param split string 分隔符
  173. --- @param ... any 参数
  174. function gameDebug.toStrings(split, ...)
  175. return gameDebug.toStrings0(false, false, split, ...)
  176. end
  177. --- 把对象转化成字符串,保护数据类型
  178. ---@param split string 分隔符
  179. --- @param ... any 参数
  180. function gameDebug.toStringsType(split, ...)
  181. return gameDebug.toStrings0(false, true, split, ...)
  182. end
  183. --- 把对象转化成字符串
  184. ---@param split string 分隔符
  185. --- @param appendYinhao boolean 是否添加引号
  186. --- @param appendType boolean 是否添加类型
  187. --- @param ... any 参数
  188. function gameDebug.toStrings0(appendYinhao, appendType, split, ...)
  189. local printString = ""
  190. local tmp = { ... }
  191. local _, _ = pcall(function()
  192. for i, v in pairs(tmp) do
  193. if not (printString == nil or printString == "") then
  194. printString = printString .. split
  195. end
  196. printString = printString .. gameDebug.toString(v, appendYinhao, appendType)
  197. end
  198. end)
  199. return printString
  200. end
  201. --- 打印参数信息, 格式化输出变量在一行内,配合过滤使用调试更方便
  202. function gameDebug.printLine(...)
  203. local string = gameDebug.toStrings(" ", ...)
  204. _LUA_Print(string)
  205. end
  206. --- 打印参数信息,格式化变量在多行显示
  207. function gameDebug.print(...)
  208. gameDebug.print0(false, true, false, ...)
  209. end
  210. --- 打印参数信息, 输出变量类型,格式化变量在多行显示
  211. function gameDebug.printType(...)
  212. gameDebug.print0(false, true, true, ...)
  213. end
  214. --- 打印参数信息,并且打印调用堆栈,格式化变量在多行显示
  215. function gameDebug.printTraceback(...)
  216. gameDebug.print0(true, true, false, ...)
  217. end
  218. --- 打印参数信息,并且打印调用堆栈 输出变量类型,格式化变量在多行显示
  219. function gameDebug.printTracebackType(...)
  220. gameDebug.print0(true, true, true, ...)
  221. end
  222. --- 打印参数信息,并且打印调用堆栈,格式化变量在多行显示
  223. --- @param isTraceback boolean 是否打印堆栈
  224. --- @param appendYinhao boolean 是否添加引号
  225. --- @param appendType boolean 是否添加类型
  226. --- @param ... any 参数
  227. function gameDebug.print0(isTraceback, appendYinhao, appendType, ...)
  228. local printString = ""
  229. local tmp = { ... }
  230. local success, result = pcall(function()
  231. for i, v in pairs(tmp) do
  232. if not (printString == nil or printString == "") then
  233. printString = printString .. ",\n"
  234. end
  235. printString = printString .. " " .. gameDebug.toString(v, appendYinhao, appendType)
  236. end
  237. end)
  238. printString = "===================参数======================\n" .. "[\n" .. printString .. "\n]"
  239. if isTraceback then
  240. printString = printString .. "\n===================堆栈=======================\n" .. debug.traceback("")
  241. end
  242. printString = printString .. "\n===================结束=======================\n"
  243. _LUA_Print(printString)
  244. end
  245. --- 辅助调试 不会抛出异常,执行异常会返回 nil
  246. --- @param ... any 如果调用 函数 异常后打印你需要显示的参数
  247. function gameDebug.debug(fun, ...)
  248. local s, e = xpcall(fun, debug.traceback, ...)
  249. if not s then
  250. local params = gameDebug.toStrings(" ", ...)
  251. error("执行异常", params, "\n", e)--把异常信息反馈java里面
  252. return nil
  253. end
  254. return e;
  255. end
  256. --- 断言对象为nil
  257. ---@param b boolean false 会抛出异常导致程序终止运行
  258. function gameDebug.assertTrue(b, ...)
  259. if not b then
  260. gameDebug.error(...)
  261. end
  262. end
  263. --- 断言 当 o1 ~= o2 会抛出异常导致程序终止运行
  264. ---@param o1 any
  265. ---@param o2 any
  266. function gameDebug.assertEquals(o1, o2, ...)
  267. gameDebug.assertTrue(o1 ~= o2, ...)
  268. end
  269. --- 断言对象为 nil 触发异常
  270. function gameDebug.assertNil(obj, ...)
  271. if obj == nil then
  272. gameDebug.error(...)
  273. end
  274. end
  275. --- 断言对象不是nil 触发异常
  276. function gameDebug.assertNotNil(obj, ...)
  277. if obj ~= nil then
  278. gameDebug.error(...)
  279. end
  280. end
  281. --- 断言 仅仅只是 print 输出
  282. ---@param b boolean false 仅仅只是 print 输出
  283. function gameDebug.assertPrint(b, ...)
  284. if not b then
  285. local msg = gameDebug.toStrings0(false, false, " ", ...)
  286. error(msg)--反馈的java里面
  287. end
  288. end
  289. --- 断言 仅仅只是 print 输出堆栈
  290. ---@param b boolean false 仅仅只是 print 输出
  291. function gameDebug.assertPrintTrace(b, ...)
  292. if not b then
  293. local msg = gameDebug.toStrings0(false, false, " ", ...)
  294. local traceback = debug.traceback(msg)
  295. error(traceback)--反馈的java里面
  296. end
  297. end
  298. --- 会抛出异常导致程序终止运行
  299. function gameDebug.error(...)
  300. local var = gameDebug.toStrings0(false, false, " ", ...)
  301. var = debug.traceback(var)
  302. _LUA_Error(var)
  303. end