feat: reloading modules
This commit is contained in:
parent
4761c520d5
commit
12aced92cc
@ -172,6 +172,7 @@ function run_current_file()
|
|||||||
world = world.reload_script,
|
world = world.reload_script,
|
||||||
hud = hud.reload_script,
|
hud = hud.reload_script,
|
||||||
component = entities.reload_component,
|
component = entities.reload_component,
|
||||||
|
module = reload_module,
|
||||||
}
|
}
|
||||||
func = funcs[script_type] or func
|
func = funcs[script_type] or func
|
||||||
local output = core.capture_output(function() func(unit) end)
|
local output = core.capture_output(function() func(unit) end)
|
||||||
|
|||||||
@ -475,6 +475,35 @@ function on_deprecated_call(name, alternatives)
|
|||||||
end
|
end
|
||||||
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
|
-- Load script with caching
|
||||||
--
|
--
|
||||||
-- path - script path `contentpack:filename`.
|
-- path - script path `contentpack:filename`.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user