_0GameDebug_1.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. if typeString == 'userdata' then
  112. local s, e = pcall(function()
  113. return obj:toString()
  114. end)
  115. if s then
  116. return "【actor】" .. e
  117. end
  118. end
  119. --_LUA_Print("gameDebug.toString", typeString, obj)
  120. if typeString == "number" or typeString == "boolean" then
  121. if appendType then
  122. return "【" .. typeString .. "】 " .. tostring(obj)
  123. end
  124. return tostring(obj)
  125. elseif typeString == 'string' then
  126. local str = tostring(obj);
  127. if appendYinhao then
  128. str = "\"" .. tostring(obj) .. "\""
  129. end
  130. if appendType then
  131. str = "【string】 " .. str
  132. end
  133. return str
  134. elseif typeString == 'cdata' then
  135. local str = tostring(obj)
  136. -- 获取字符串的长度
  137. local len = string.len(str)
  138. -- 检查字符串最后一个字符是否是目标字符
  139. if string.sub(str, len - 2, len) == 'ULL' then
  140. -- 删除最后一个字符
  141. str = string.sub(str, 1, len - 3)
  142. elseif string.sub(str, len - 1, len) == 'LL' then
  143. -- 删除最后一个字符
  144. str = string.sub(str, 1, len - 2)
  145. end
  146. if appendType then
  147. str = "【long】 " .. str
  148. end
  149. return str
  150. elseif typeString == 'table' then
  151. local str = gameDebug.toTableJson(obj, true, appendType)
  152. if appendType then
  153. str = "【" .. typeString .. "】 " .. str
  154. end
  155. return str
  156. elseif typeString == "function" then
  157. local s, e = pcall(function()
  158. return obj:toString()
  159. end)
  160. if not s then
  161. s, e = pcall(function()
  162. return obj.toString()
  163. end)
  164. end
  165. if not s then
  166. s, e = pcall(function()
  167. return tostring(obj)
  168. end)
  169. end
  170. return e
  171. else
  172. local str = gameDebug.toArrayJson(obj, true, appendType)
  173. if appendType then
  174. str = "【" .. typeString .. "】 " .. str
  175. end
  176. return str
  177. end
  178. end
  179. --- 把对象转化成字符串
  180. ---@param split string 分隔符
  181. --- @param ... any 参数
  182. function gameDebug.toStrings(split, ...)
  183. return gameDebug.toStrings0(false, false, split, ...)
  184. end
  185. --- 把对象转化成字符串,保护数据类型
  186. ---@param split string 分隔符
  187. --- @param ... any 参数
  188. function gameDebug.toStringsType(split, ...)
  189. return gameDebug.toStrings0(false, true, split, ...)
  190. end
  191. --- 把对象转化成字符串
  192. ---@param split string 分隔符
  193. --- @param appendYinhao boolean 是否添加引号
  194. --- @param appendType boolean 是否添加类型
  195. --- @param ... any 参数
  196. function gameDebug.toStrings0(appendYinhao, appendType, split, ...)
  197. local printString = ""
  198. local tmp = { ... }
  199. local _, _ = pcall(function()
  200. for i, v in pairs(tmp) do
  201. if not (printString == nil or printString == "") then
  202. printString = printString .. split
  203. end
  204. printString = printString .. gameDebug.toString(v, appendYinhao, appendType)
  205. end
  206. end)
  207. return printString
  208. end
  209. --- 打印参数信息, 格式化输出变量在一行内,配合过滤使用调试更方便
  210. function gameDebug.printLine(...)
  211. local string = gameDebug.toStrings(" ", ...)
  212. _LUA_Print(string)
  213. end
  214. --- 打印参数信息,格式化变量在多行显示
  215. function gameDebug.print(...)
  216. gameDebug.print0(false, true, false, ...)
  217. end
  218. --- 打印参数信息, 输出变量类型,格式化变量在多行显示
  219. function gameDebug.printType(...)
  220. gameDebug.print0(false, true, true, ...)
  221. end
  222. --- 打印参数信息,并且打印调用堆栈,格式化变量在多行显示
  223. function gameDebug.printTraceback(...)
  224. gameDebug.print0(true, true, false, ...)
  225. end
  226. --- 打印参数信息,并且打印调用堆栈 输出变量类型,格式化变量在多行显示
  227. function gameDebug.printTracebackType(...)
  228. gameDebug.print0(true, true, true, ...)
  229. end
  230. --- 打印参数信息,并且打印调用堆栈,格式化变量在多行显示
  231. --- @param isTraceback boolean 是否打印堆栈
  232. --- @param appendYinhao boolean 是否添加引号
  233. --- @param appendType boolean 是否添加类型
  234. --- @param ... any 参数
  235. function gameDebug.print0(isTraceback, appendYinhao, appendType, ...)
  236. local printString = ""
  237. local tmp = { ... }
  238. local success, result = pcall(function()
  239. for i, v in pairs(tmp) do
  240. if not (printString == nil or printString == "") then
  241. printString = printString .. ",\n"
  242. end
  243. printString = printString .. " " .. gameDebug.toString(v, appendYinhao, appendType)
  244. end
  245. end)
  246. printString = "===================参数======================\n" .. "[\n" .. printString .. "\n]"
  247. if isTraceback then
  248. printString = printString .. "\n===================堆栈=======================\n" .. debug.traceback("")
  249. end
  250. printString = printString .. "\n===================结束=======================\n"
  251. _LUA_Print(printString)
  252. end
  253. --- 辅助调试 不会抛出异常,执行异常会返回 nil
  254. --- @param ... any 如果调用 函数 异常后打印你需要显示的参数
  255. function gameDebug.debug(fun, ...)
  256. local s, e = xpcall(fun, debug.traceback, ...)
  257. if not s then
  258. local params = gameDebug.toStrings(" ", ...)
  259. error("执行异常", params, "\n", e)--把异常信息反馈java里面
  260. return nil
  261. end
  262. return e;
  263. end
  264. --- 断言对象为nil
  265. ---@param b boolean false 会抛出异常导致程序终止运行
  266. function gameDebug.assertTrue(b, ...)
  267. if not b then
  268. gameDebug.error(...)
  269. end
  270. end
  271. --- 断言 当 o1 ~= o2 会抛出异常导致程序终止运行
  272. ---@param o1 any
  273. ---@param o2 any
  274. function gameDebug.assertEquals(o1, o2, ...)
  275. gameDebug.assertTrue(o1 ~= o2, ...)
  276. end
  277. --- 断言对象为 nil 触发异常
  278. function gameDebug.assertNil(obj, ...)
  279. if obj == nil then
  280. gameDebug.error(...)
  281. end
  282. end
  283. --- 断言对象不是nil 触发异常
  284. function gameDebug.assertNotNil(obj, ...)
  285. if obj ~= nil then
  286. gameDebug.error(...)
  287. end
  288. end
  289. --- 断言 仅仅只是 print 输出
  290. ---@param b boolean false 仅仅只是 print 输出
  291. function gameDebug.assertPrint(b, ...)
  292. if not b then
  293. local msg = gameDebug.toStrings0(false, false, " ", ...)
  294. error(msg)--反馈的java里面
  295. end
  296. end
  297. --- 断言 仅仅只是 print 输出堆栈
  298. ---@param b boolean false 仅仅只是 print 输出
  299. function gameDebug.assertPrintTrace(b, ...)
  300. if not b then
  301. local msg = gameDebug.toStrings0(false, false, " ", ...)
  302. local traceback = debug.traceback(msg)
  303. error(traceback)--反馈的java里面
  304. end
  305. end
  306. --- 会抛出异常导致程序终止运行
  307. function gameDebug.error(...)
  308. local var = gameDebug.toStrings0(false, false, " ", ...)
  309. var = debug.traceback(var)
  310. _LUA_Error(var)
  311. end