Skip to content

Commit

Permalink
Refactor module reloading (FAForever#4283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hdt80bro authored Oct 21, 2022
1 parent f5c95e5 commit 2b0f441
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lua/sim/NavDebug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ end
local ScanningThread = ForkThread(Scan)

--- Called by the module manager when this module is dirty due to a disk change
function __OnDirtyModule()
function __moduleinfo.OnDirty()
if ScanningThread then
ScanningThread:Destroy()
end
Expand Down
2 changes: 1 addition & 1 deletion lua/sim/NavGenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ function Generate()
end

--- Called by the module manager when this module is dirty due to a disk change
function __OnDirtyModule()
function __moduleinfo.OnDirty()
end


28 changes: 25 additions & 3 deletions lua/system/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local error = error

--- Table of all loaded modules, indexed by name.
__modules = {}
-- temporary place to store reloading modules
local oldModules = {}

--- Common metatable used by all modules, which forwards global references to _G
---@class Module
Expand All @@ -27,6 +29,8 @@ __module_metatable = {
---@field name string
---@field used_by table<string, true>
---@field track_imports boolean
---@field OnDirty? fun()
---@field OnReload? fun(newmod: Module)

-- these values can be adjusted by hooking into this file
local informDevOfLoad = false
Expand Down Expand Up @@ -61,6 +65,11 @@ function import(name)
used_by = {},
track_imports = true,
}
-- make any old data available to the new one while it reloads
local oldMod = oldModules[name]
if oldMod then
moduleinfo.old = oldMod
end

-- set up an environment for the new module
---@type Module
Expand Down Expand Up @@ -89,6 +98,16 @@ function import(name)

-- try to add content to the environment
local ok, msg = pcall(doscript, name, module)
if oldMod then
-- now clear the old module
oldModules[name] = nil
moduleinfo.old = nil
-- let the old module load data into the new one, if they'd prefer to do it that way
local onReload = oldMod.__moduleinfo.OnReload
if onReload then
onReload(module)
end
end
if not ok then
-- we failed: report back
modules[name] = nil
Expand All @@ -113,16 +132,19 @@ function dirty_module(name, why)
if why then LOG("Module '", name, "' changed on disk") end
LOG(" marking '", name, "' for reload")

local moduleinfo = module.__moduleinfo
-- allow us to run code when a module is ejected
if rawget(module, '__OnDirtyModule') then
local ok, msg = pcall(module.__OnDirtyModule)
local onDirty = moduleinfo.OnDirty
if onDirty then
local ok, msg = pcall(onDirty)
if not ok then
WARN(msg)
end
end
oldModules[name] = module

modules[name] = nil
local deps = module.__moduleinfo.used_by
local deps = moduleinfo.used_by
if deps then
for k, _ in deps do
dirty_module(k)
Expand Down
2 changes: 1 addition & 1 deletion lua/ui/game/NavGenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ function CloseWindow()
end

--- Called by the module manager when this module is dirty due to a disk change
function __OnDirtyModule()
function __moduleinfo.OnDirty()
if Root then
Root:Destroy()
end
Expand Down

0 comments on commit 2b0f441

Please sign in to comment.