feat: reloading modules

This commit is contained in:
MihailRis 2025-03-16 22:27:08 +03:00
parent 4761c520d5
commit 12aced92cc
2 changed files with 30 additions and 0 deletions

View File

@ -172,6 +172,7 @@ function run_current_file()
world = world.reload_script,
hud = hud.reload_script,
component = entities.reload_component,
module = reload_module,
}
func = funcs[script_type] or func
local output = core.capture_output(function() func(unit) end)

View File

@ -475,6 +475,35 @@ function on_deprecated_call(name, alternatives)
end
end
function reload_module(name)
local prefix, name = parse_path(name)
local path = prefix..":modules/"..name..".lua"
local previous = package.loaded[path]
if not previous then
debug.log("attempt to reload non-loaded module "..name.." ("..path..")")
return
end
local script, err = load(file.read(path), path)
if script == nil then
error(err)
end
local result = script()
if not result then
return
end
for i, value in ipairs(result) do
previous[i] = value
end
local copy = table.copy(result)
for key, value in pairs(result) do
result[key] = nil
end
for key, value in pairs(copy) do
previous[key] = value
end
end
-- Load script with caching
--
-- path - script path `contentpack:filename`.