debugger.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. if not DEVELOP then
  2. return
  3. end
  4. local fs = require 'bee.filesystem'
  5. local luaDebugs = {}
  6. local home = os.getenv 'USERPROFILE' or os.getenv 'HOME'
  7. if not home then
  8. log.error('Cannot find home directory')
  9. return
  10. end
  11. for _, vscodePath in ipairs { '.vscode', '.vscode-insiders', '.vscode-server-insiders' } do
  12. local extensionPath = fs.path(home) / vscodePath / 'extensions'
  13. log.debug('Search extensions at:', extensionPath:string())
  14. if fs.exists(extensionPath) then
  15. for path in fs.pairs(extensionPath) do
  16. if fs.is_directory(path) then
  17. local name = path:filename():string()
  18. if name:find('actboy168.lua-debug-', 1, true) then
  19. luaDebugs[#luaDebugs+1] = path:string()
  20. end
  21. end
  22. end
  23. end
  24. end
  25. if #luaDebugs == 0 then
  26. log.debug('Cant find "actboy168.lua-debug"')
  27. return
  28. end
  29. local function getVer(filename)
  30. local a, b, c = filename:match('actboy168%.lua%-debug%-(%d+)%.(%d+)%.(%d+)')
  31. if not a then
  32. return 0
  33. end
  34. return a * 1000000 + b * 1000 + c
  35. end
  36. table.sort(luaDebugs, function (a, b)
  37. return getVer(a) > getVer(b)
  38. end)
  39. local debugPath = luaDebugs[1]
  40. local cpath = "/runtime/win64/lua54/?.dll;/runtime/win64/lua54/?.so"
  41. local path = "/script/?.lua"
  42. local function tryDebugger()
  43. local entry = assert(package.searchpath('debugger', debugPath .. path))
  44. local root = debugPath
  45. local addr = ("127.0.0.1:%d"):format(DBGPORT)
  46. local dbg = loadfile(entry)(root)
  47. dbg:start {
  48. address = addr,
  49. }
  50. log.debug('Debugger startup, listen port:', DBGPORT)
  51. log.debug('Debugger args:', addr, root, path, cpath)
  52. if DBGWAIT then
  53. dbg:event('wait')
  54. end
  55. return dbg
  56. end
  57. xpcall(tryDebugger, log.debug)