_0GameDebug.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.print(...)
  203. gameDebug.print0(false, true, false, ...)
  204. end
  205. --- 打印参数信息, 输出变量类型
  206. function gameDebug.printType(...)
  207. gameDebug.print0(false, true, true, ...)
  208. end
  209. --- 打印参数信息,并且打印调用堆栈
  210. function gameDebug.printTraceback(...)
  211. gameDebug.print0(true, true, false, ...)
  212. end
  213. --- 打印参数信息,并且打印调用堆栈 输出变量类型
  214. function gameDebug.printTracebackType(...)
  215. gameDebug.print0(true, true, true, ...)
  216. end
  217. --- 打印参数信息,并且打印调用堆栈
  218. --- @param isTraceback boolean 是否打印堆栈
  219. --- @param appendYinhao boolean 是否添加引号
  220. --- @param appendType boolean 是否添加类型
  221. --- @param ... any 参数
  222. function gameDebug.print0(isTraceback, appendYinhao, appendType, ...)
  223. local printString = ""
  224. local tmp = { ... }
  225. local success, result = pcall(function()
  226. for i, v in pairs(tmp) do
  227. if not (printString == nil or printString == "") then
  228. printString = printString .. ",\n"
  229. end
  230. printString = printString .. " " .. gameDebug.toString(v, appendYinhao, appendType)
  231. end
  232. end)
  233. printString = "===================参数======================\n" .. "[\n" .. printString .. "\n]"
  234. if isTraceback then
  235. printString = printString .. "\n===================堆栈=======================\n" .. debug.traceback("")
  236. end
  237. printString = printString .. "\n===================结束=======================\n"
  238. _LUA_Print(printString)
  239. end
  240. --- 辅助调试 不会抛出异常,执行异常会返回 nil
  241. --- @param ... any 如果调用 函数 异常后打印你需要显示的参数
  242. function gameDebug.debug(fun, ...)
  243. local s, e = xpcall(fun, debug.traceback, ...)
  244. if not s then
  245. local params = gameDebug.toStrings(" ", ...)
  246. error("执行异常", params, "\n", e)--把异常信息反馈java里面
  247. return nil
  248. end
  249. return e;
  250. end
  251. --- 断言对象为nil
  252. ---@param b boolean false 会抛出异常导致程序终止运行
  253. function gameDebug.assertTrue(b, ...)
  254. if not b then
  255. gameDebug.error(...)
  256. end
  257. end
  258. --- 断言 当 o1 ~= o2 会抛出异常导致程序终止运行
  259. ---@param o1 any
  260. ---@param o2 any
  261. function gameDebug.assertEquals(o1, o2, ...)
  262. gameDebug.assertTrue(o1 ~= o2, ...)
  263. end
  264. --- 断言对象为 nil 触发异常
  265. function gameDebug.assertNil(obj, ...)
  266. if obj == nil then
  267. gameDebug.error(...)
  268. end
  269. end
  270. --- 断言对象不是nil 触发异常
  271. function gameDebug.assertNotNil(obj, ...)
  272. if obj ~= nil then
  273. gameDebug.error(...)
  274. end
  275. end
  276. --- 断言 仅仅只是 print 输出
  277. ---@param b boolean false 仅仅只是 print 输出
  278. function gameDebug.assertPrint(b, ...)
  279. if not b then
  280. local msg = gameDebug.toStrings0(false, false, " ", ...)
  281. error(msg)--反馈的java里面
  282. end
  283. end
  284. --- 断言 仅仅只是 print 输出堆栈
  285. ---@param b boolean false 仅仅只是 print 输出
  286. function gameDebug.assertPrintTrace(b, ...)
  287. if not b then
  288. local msg = gameDebug.toStrings0(false, false, " ", ...)
  289. local traceback = debug.traceback(msg)
  290. error(traceback)--反馈的java里面
  291. end
  292. end
  293. --- 会抛出异常导致程序终止运行
  294. function gameDebug.error(...)
  295. local var = gameDebug.toStrings0(false, false, " ", ...)
  296. var = debug.traceback(var)
  297. _LUA_Error(var)
  298. end