luadb_maker.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --[[
  2. luadb maker, for script.bin
  3. -- 使用说明
  4. 1. 需要打包的文件存放到 disk 目录
  5. 2. 用luatos.exe luadb_maker.lua 执行本脚本
  6. 3. 然后就会生成script.bin
  7. -- 工作流程
  8. 1. 遍历disk目录,得到列表
  9. 2. 对.lua文件进行luac编译
  10. 3. 按luadb格式合成文件
  11. 运行所需要的程序:
  12. 1. luatos.exe, 可通过bsp/win32编译, 也可以从github action获取现成的
  13. 2. luac536.exe 与原版luac.exe的区别是启用了 `#define LUA_32BITS`
  14. ]]
  15. -- 存放脚本和其他资源文件的目录,不能带子文件夹
  16. script_dir = "disk"
  17. -- 是否保留全部
  18. debug_all = false
  19. -- 遍历文件, io.lsdir应该也行
  20. local function lsdir(path, files, shortname)
  21. local exe = io.popen("dir /b " .. (shortname and " " or " /s ") .. path)
  22. if exe then
  23. for line in exe:lines() do
  24. table.insert(files, line)
  25. end
  26. exe:close()
  27. end
  28. end
  29. -- 封装一下调用本地程序的逻辑
  30. local function oscall(cmd, quite, cwd)
  31. if cwd and Base_CWD then
  32. lfs.chdir(cwd)
  33. end
  34. if tool_debug then
  35. log.info("cmd", cmd)
  36. end
  37. local exe = io.popen(cmd)
  38. if exe then
  39. for line in exe:lines() do
  40. if not quite then
  41. log.info("cmd", line)
  42. end
  43. end
  44. exe:close()
  45. end
  46. if cwd and Base_CWD then
  47. lfs.chdir(Base_CWD)
  48. end
  49. end
  50. -- TLD格式打包, Tag - Len - data
  51. function TLD(buff, T, D)
  52. buff:pack("bb", T, D:len())
  53. buff:write(D)
  54. end
  55. -----------------------
  56. --- 开始正式的逻辑
  57. -----------------------
  58. -- 获取disk目录下的全部文件列表
  59. local files = {}
  60. lsdir(script_dir, files, true)
  61. oscall("mkdir tmp")
  62. -- 创建所需的缓冲区
  63. local buff = zbuff.create(1024*1024)
  64. local magic = string.char(0x5A, 0xA5, 0X5A, 0xA5)
  65. -- 先写入magic
  66. --buff:pack("bbbbbb", 0x01, 0x5A, 0xA5, 0X5A, 0xA5)
  67. TLD(buff, 0x01, magic)
  68. -- 然后是版本号, 当前是2
  69. --buff:write(string.char(0x02, 0x02, 0x00, 0x02))
  70. TLD(buff, 0x02, string.char(0x00, 0x02))
  71. -- head长度,固定长度
  72. buff:write(string.char(0x03, 0x04))
  73. buff:pack("I", 0x12)
  74. -- 文件数量, 按实际情况的
  75. buff:write(string.char(0x04, 0x02))
  76. buff:pack("H", #files)
  77. -- CRC值, 虽然有,但实际不校验
  78. buff:write(string.char(0xFE, 0x02))
  79. buff:pack("H", 0xFFFF)
  80. -- 如果是lua文件,转luac,然后添加
  81. -- 如果是其他文件,直接添加
  82. for _, value in ipairs(files) do
  83. TLD(buff, 0x01, magic)
  84. if value:endsWith(".lua") then
  85. TLD(buff, 0x02, value .. "c")
  86. -- 内置的dump也能用. 不过得考虑size_t和32bit是否启用的问题
  87. -- local lf = loadfile(script_dir .. "\\" .. value)
  88. -- local data = string.dump(lf, value == "main.lua" or debug_all)
  89. io.popen("luac536.exe -s -o tmp.luac " .. script_dir .. "\\" .. value):read("*a")
  90. local data = io.readFile("tmp.luac")
  91. TLD(buff, 0x03, pack.pack("I", #data))
  92. TLD(buff, 0xFE, string.char(0xFF, 0xFF))
  93. buff:write(data)
  94. else
  95. TLD(buff, 0x02, value)
  96. TLD(buff, 0x03, io.fileSize(script_dir .. "\\" .. value))
  97. TLD(buff, 0xFE, string.char(0xFF, 0xFF))
  98. buff:write(io.readFile( script_dir .. "\\" .. value))
  99. end
  100. end
  101. -- 最后获取全部数据
  102. local data = buff:toStr(0, buff:seek(0, zbuff.SEEK_CUR))
  103. log.info("target size", #data)
  104. -- 写入目标文件
  105. io.writeFile("script.bin", data)
  106. -- 收工
  107. os.exit(0)