helper.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. local skynet = require "skynet"
  2. local lfsUtil = require "utils.lfsUtil"
  3. local hotfix = require "hotfix.hotfix"
  4. -- 全局对象不允许更新(math/string/table可以更新,因为luaext用到了)
  5. local global_objects = {
  6. arg,
  7. assert,
  8. bit32,
  9. collectgarbage,
  10. coroutine,
  11. debug,
  12. dofile,
  13. error,
  14. getmetatable,
  15. io,
  16. ipairs,
  17. lfs,
  18. load,
  19. loadfile,
  20. loadstring,
  21. -- math,
  22. module,
  23. next,
  24. os,
  25. package,
  26. pairs,
  27. pcall,
  28. print,
  29. rawequal,
  30. rawget,
  31. rawlen,
  32. rawset,
  33. require,
  34. select,
  35. setmetatable,
  36. -- string,
  37. -- table,
  38. tonumber,
  39. tostring,
  40. type,
  41. unpack,
  42. utf8,
  43. xpcall,
  44. skynet
  45. }
  46. local protoPath = skynet.getenv("proto_path")
  47. local pathConfig = skynet.getenv("config_path")
  48. -- 排除文件
  49. local function excludeFile(dir)
  50. -- 服务文件目录
  51. if dir:match("service") then
  52. return true
  53. end
  54. -- skynet目录
  55. if dir:match("skynet") then
  56. return true
  57. end
  58. -- 协议目录
  59. -- if dir:match("proto") then return true end
  60. -- 配置文件目录
  61. if dir:match(pathConfig) then
  62. return true
  63. end
  64. -- core 目录
  65. -- if dir:match("core") then return true end
  66. -- hotfix 目录
  67. if dir:match("hotfix") then
  68. return true
  69. end
  70. -- main 文件
  71. if dir:match("main") then
  72. return true
  73. end
  74. return false
  75. end
  76. -- 获取逻辑文件路径
  77. local function getLogicDirs()
  78. local res = {}
  79. local luaDirs = string.split(package.path, ";")
  80. for _, value in ipairs(luaDirs) do
  81. if not excludeFile(value) then
  82. table.insert(res, string.rtrim(value, "?.lua"))
  83. end
  84. end
  85. return res
  86. end
  87. local function getAllFileMod()
  88. local res = {}
  89. local logicDirs = getLogicDirs()
  90. local fileList = {}
  91. for _, logicDir in ipairs(logicDirs) do
  92. local files = lfsUtil:get_path_lua_files(logicDir)
  93. for _, file in ipairs(files) do
  94. local dir = string.format("%s%s", logicDir, file)
  95. if not excludeFile(dir) then
  96. fileList[dir] = file:gsub("/", ".")
  97. end
  98. end
  99. end
  100. for dir, file in pairs(fileList) do
  101. res[file] = lfsUtil:get_file_modification(dir .. ".lua") -- 文件修改时间
  102. end
  103. return res
  104. end
  105. local root = {}
  106. local fileTimes = {}
  107. -- 热更新文件
  108. function root.update(changeList)
  109. local errFiles = {}
  110. for _, file in ipairs(changeList) do
  111. skynet.error(string.format("Hot fix %s", file))
  112. hotfix.hotfix_module(file)
  113. end
  114. end
  115. -- 获取变更文件
  116. function root.getChanggeFiles()
  117. local changeList = {}
  118. local newFileTimes = getAllFileMod()
  119. for file, time in pairs(newFileTimes) do
  120. if time ~= fileTimes[file] then
  121. table.insert(changeList, file)
  122. end
  123. end
  124. fileTimes = newFileTimes
  125. return changeList
  126. end
  127. -- 初始化
  128. function root.init(needInit)
  129. -- hotfix.log_debug = function(s) skynet.error(s) end
  130. if needInit then
  131. fileTimes = getAllFileMod()
  132. local t = {}
  133. for key, value in pairs(fileTimes) do
  134. table.insert(t, {file = key, time = value})
  135. end
  136. table.sort(
  137. t,
  138. function(a, b)
  139. return a.file < b.file
  140. end
  141. )
  142. for index, value in ipairs(t) do
  143. -- log.print(string.format("%s",value.file ))
  144. end
  145. end
  146. hotfix.add_protect(global_objects)
  147. end
  148. return root