Skip to content

Commit

Permalink
New: lua surport the module function hot update
Browse files Browse the repository at this point in the history
  • Loading branch information
i0gan committed Aug 11, 2024
1 parent 67d74e6 commit c8cc104
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/lua/common/module_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
-----------------------------------------------------------------------------

ModuleMgr = {
modules = {}
modules = {},
modules_info = {}
}

-- pre_path: 模块路径
Expand All @@ -27,14 +28,15 @@ function ModuleMgr:Register(pre_path, list)
Squick:LogInfo("lua start to load " .. name);

local old_module = ModuleMgr.modules[name]
local object = require(path);
if object then
local new_module = require(path);
if new_module then

if old_module ~= nil then
Squick:LogInfo("reload " .. path.. " succeed");
-- 只更新函数
Squick:LogInfo("Begin hot reload " .. path);
self:HotReload(old_module, new_module)
Squick:LogInfo("Hot reload " .. path.. " succeed");
else
ModuleMgr.modules[name] = object -- 加载全部
ModuleMgr.modules[name] = new_module -- 加载全部
Squick:LogInfo("load " .. path .. " succeed");
end
else
Expand All @@ -47,8 +49,13 @@ function ModuleMgr:Register(pre_path, list)
return true
end

function ModuleMgr:HotReload()

function ModuleMgr:HotReload(old_module, new_module)
for key, value in pairs(new_module) do
if type(value) == "function" then
Squick:LogInfo("reloading function " .. key)
old_module[key] = value
end
end
end

function ModuleMgr:Start()
Expand Down
1 change: 1 addition & 0 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Print(...)
end

function Require(file)
package.loaded[file] = nil
require(file)
end

Expand Down

0 comments on commit c8cc104

Please sign in to comment.