local skynet = require "skynet" local lfsUtil = require "utils.lfsUtil" local hotfix = require "hotfix.hotfix" -- 全局对象不允许更新(math/string/table可以更新,因为luaext用到了) local global_objects = { arg, assert, bit32, collectgarbage, coroutine, debug, dofile, error, getmetatable, io, ipairs, lfs, load, loadfile, loadstring, -- math, module, next, os, package, pairs, pcall, print, rawequal, rawget, rawlen, rawset, require, select, setmetatable, -- string, -- table, tonumber, tostring, type, unpack, utf8, xpcall, skynet } local protoPath = skynet.getenv("proto_path") local pathConfig = skynet.getenv("config_path") -- 排除文件 local function excludeFile(dir) -- 服务文件目录 if dir:match("service") then return true end -- skynet目录 if dir:match("skynet") then return true end -- 协议目录 -- if dir:match("proto") then return true end -- 配置文件目录 if dir:match(pathConfig) then return true end -- core 目录 -- if dir:match("core") then return true end -- hotfix 目录 if dir:match("hotfix") then return true end -- main 文件 if dir:match("main") then return true end return false end -- 获取逻辑文件路径 local function getLogicDirs() local res = {} local luaDirs = string.split(package.path, ";") for _, value in ipairs(luaDirs) do if not excludeFile(value) then table.insert(res, string.rtrim(value, "?.lua")) end end return res end local function getAllFileMod() local res = {} local logicDirs = getLogicDirs() local fileList = {} for _, logicDir in ipairs(logicDirs) do local files = lfsUtil:get_path_lua_files(logicDir) for _, file in ipairs(files) do local dir = string.format("%s%s", logicDir, file) if not excludeFile(dir) then fileList[dir] = file:gsub("/", ".") end end end for dir, file in pairs(fileList) do res[file] = lfsUtil:get_file_modification(dir .. ".lua") -- 文件修改时间 end return res end local root = {} local fileTimes = {} -- 热更新文件 function root.update(changeList) local errFiles = {} for _, file in ipairs(changeList) do skynet.error(string.format("Hot fix %s", file)) hotfix.hotfix_module(file) end end -- 获取变更文件 function root.getChanggeFiles() local changeList = {} local newFileTimes = getAllFileMod() for file, time in pairs(newFileTimes) do if time ~= fileTimes[file] then table.insert(changeList, file) end end fileTimes = newFileTimes return changeList end -- 初始化 function root.init(needInit) -- hotfix.log_debug = function(s) skynet.error(s) end if needInit then fileTimes = getAllFileMod() local t = {} for key, value in pairs(fileTimes) do table.insert(t, {file = key, time = value}) end table.sort( t, function(a, b) return a.file < b.file end ) for index, value in ipairs(t) do -- log.print(string.format("%s",value.file )) end end hotfix.add_protect(global_objects) end return root