Implement loading non-bundled mods

This commit is contained in:
Lucas Schwiderski 2023-11-10 08:20:41 +01:00
parent 7b8a8d1094
commit 6c5604cb1c
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
2 changed files with 80 additions and 58 deletions

View file

@ -7,13 +7,11 @@ local GameStateMachine = require("scripts/foundation/utilities/game_state_machin
-- The loader object that is used during game boot -- The loader object that is used during game boot
-- to initialize the modding environment. -- to initialize the modding environment.
local loader = {} local DML = {}
function loader:init(mod_data, boot_gui) function DML.create_loader(mod_data, boot_gui)
local ModLoader = dofile("scripts/mods/dml/mod_loader") local ModLoader = dofile("scripts/mods/dml/mod_loader")
local mod_loader = ModLoader:new(mod_data, boot_gui) local mod_loader = ModLoader:new(mod_data, boot_gui)
self._mod_loader = mod_loader
Managers.mod = mod_loader
-- The mod loader needs to remain active during game play, to -- The mod loader needs to remain active during game play, to
-- enable reloads -- enable reloads
@ -64,10 +62,11 @@ function loader:init(mod_data, boot_gui)
return func(self, ...) return func(self, ...)
end) end)
return mod_loader
end end
function loader:update(dt) function DML.update(mod_loader, dt)
local mod_loader = self._mod_loader
mod_loader:update(dt) mod_loader:update(dt)
local done = mod_loader:all_mods_loaded() local done = mod_loader:all_mods_loaded()
@ -78,8 +77,8 @@ function loader:update(dt)
return done return done
end end
function loader:done() function DML.done(mod_loader)
return self._mod_loader:all_mods_loaded() return mod_loader:all_mods_loaded()
end end
return loader return DML

View file

@ -59,13 +59,12 @@ ModLoader._draw_state_to_gui = function(self, gui, dt)
if state == "scanning" then if state == "scanning" then
status_str = "Scanning for mods" status_str = "Scanning for mods"
elseif state == "loading" then elseif state == "loading" or state == "initializing" then
local mod = self._mods[self._mod_load_index] local mod = self._mods[self._mod_load_index]
status_str = string.format("Loading mod %q", mod.name) status_str = string.format("Loading mod %q", mod.name)
end end
local msg = status_str .. string.rep(".", (2 * t) % 4) local msg = status_str .. string.rep(".", (2 * t) % 4)
Log.info("ModLoader", msg)
ScriptGui.text(gui, msg, FONT_MATERIAL, 25, Vector3(20, 30, 1), Color.white()) ScriptGui.text(gui, msg, FONT_MATERIAL, 25, Vector3(20, 30, 1), Color.white())
end end
@ -117,24 +116,19 @@ ModLoader.update = function(self, dt)
self._reload_requested = true self._reload_requested = true
end end
if self._reload_requested and self._state == "done" then if self._reload_requested and old_state == "done" then
self:_reload_mods() self:_reload_mods()
end end
if self._state == "done" then if old_state == "done" then
for i = 1, self._num_mods, 1 do self:_run_callbacks("update", dt)
local mod = self._mods[i] elseif old_state == "scanning" then
Log.info("ModLoader", "Scanning for mods")
if mod and not mod.callbacks_disabled then
self:_run_callback(mod, "update", dt)
end
end
elseif self._state == "scanning" then
self:_build_mod_table() self:_build_mod_table()
self._state = self:_load_mod(1) self._state = self:_load_mod(1)
self._ui_time = 0 self._ui_time = 0
elseif self._state == "loading" then elseif old_state == "loading" then
local handle = self._loading_resource_handle local handle = self._loading_resource_handle
if ResourcePackage.has_loaded(handle) then if ResourcePackage.has_loaded(handle) then
@ -144,11 +138,34 @@ ModLoader.update = function(self, dt)
local next_index = mod.package_index + 1 local next_index = mod.package_index + 1
local mod_data = mod.data local mod_data = mod.data
if next_index > #mod_data.packages then if next_index <= #mod_data.packages then
self:_load_package(mod, next_index)
else
self._state = "initializing"
end
end
elseif old_state == "initializing" then
local mod = self._mods[self._mod_load_index]
local mod_data = mod.data
Log.info("ModLoader", "Initializing mod %q", mod.name)
mod.state = "running" mod.state = "running"
local ok, object = xpcall(mod_data.run, Script.callstack) local ok, object = xpcall(mod_data.run, function(err)
if type(err) == "string" then
return err .. "\n" .. Script.callstack()
else
return err
end
end)
if not ok then if not ok then
if object.error then
object = string.format(
"%s\n<<Lua Stack>>\n%s\n<</Lua Stack>>\n<<Lua Locals>>\n%s\n<</Lua Locals>>\n<<Lua Self>>\n%s\n<</Lua Self>>",
object.error, object.traceback, object.locals, object.self)
end
Log.error("ModLoader", "Failed 'run' for %q: %s", mod.name, object) Log.error("ModLoader", "Failed 'run' for %q: %s", mod.name, object)
end end
@ -159,14 +176,9 @@ ModLoader.update = function(self, dt)
Log.info("ModLoader", "Finished loading %q", mod.name) Log.info("ModLoader", "Finished loading %q", mod.name)
self._state = self:_load_mod(self._mod_load_index + 1) self._state = self:_load_mod(self._mod_load_index + 1)
else
self:_load_package(mod, next_index)
end
end
end end
local gui = self._gui local gui = self._gui
if gui then if gui then
self:_draw_state_to_gui(gui, dt) self:_draw_state_to_gui(gui, dt)
end end
@ -181,15 +193,18 @@ ModLoader.all_mods_loaded = function(self)
end end
ModLoader.destroy = function(self) ModLoader.destroy = function(self)
self:_run_callbacks("on_destroy")
self:unload_all_mods()
end
ModLoader._run_callbacks = function(self, callback_name, ...)
for i = 1, self._num_mods, 1 do for i = 1, self._num_mods, 1 do
local mod = self._mods[i] local mod = self._mods[i]
if mod and not mod.callbacks_disabled then if mod and not mod.callbacks_disabled then
self:_run_callback(mod, "on_destroy") self:_run_callback(mod, callback_name, ...)
end end
end end
self:unload_all_mods()
end end
ModLoader._run_callback = function(self, mod, callback_name, ...) ModLoader._run_callback = function(self, mod, callback_name, ...)
@ -202,16 +217,25 @@ ModLoader._run_callback = function(self, mod, callback_name, ...)
local args = table_pack(...) local args = table_pack(...)
local success, val = xpcall(function() return cb(object, table_unpack(args)) end, Script.callstack) local success, val = xpcall(
function() return cb(object, table_unpack(args)) end,
function(err)
if type(err) == "string" then
return err .. "\n" .. Script.callstack()
else
return err
end
end
)
if success then if success then
return val return val
else else
Log.error("ModLoader", "Failed to run callback %q for mod %q with id %q. Disabling callbacks until reload.", Log.error("ModLoader", "Failed to run callback %q for mod %q with id %q. Disabling callbacks until reload.",
callback_name, mod.name, mod.id) callback_name, mod.name, mod.id)
if type(val) == "table" then if val.error then
Log.error("ModLoader", Log.error("ModLoader",
"<<Script Error>>%s<</Script Error>>\n<<Lua Stack>>\n%s\n<</Lua Stack>>\n<<Lua Locals>>\n%s\n<</Lua Locals>>\n<<Lua Self>>\n%s\n<</Lua Self>>", "Error: %s\n<<Lua Stack>>\n%s<</Lua Stack>>\n<<Lua Locals>>\n%s<</Lua Locals>>\n<<Lua Self>>\n%s<</Lua Self>>",
val.error, val.traceback, val.locals, val.self) val.error, val.traceback, val.locals, val.self)
else else
Log.error("ModLoader", "Error: %s", val or "[unknown error]") Log.error("ModLoader", "Error: %s", val or "[unknown error]")
@ -230,7 +254,8 @@ ModLoader._build_mod_table = function(self)
fassert(table.is_empty(self._mods), "Trying to add mods to non-empty mod table") fassert(table.is_empty(self._mods), "Trying to add mods to non-empty mod table")
for i, mod_data in ipairs(self._mod_data) do for i, mod_data in ipairs(self._mod_data) do
Log.info("ModLoader", "mods[%d] = id=%q | name=%q", i, mod_data.id, mod_data.name) Log.info("ModLoader", "mods[%d] = id=%q | name=%q | bundled=%s", i, mod_data.id, mod_data.name,
tostring(mod_data.bundled))
self._mods[i] = { self._mods[i] = {
id = mod_data.id, id = mod_data.id,
@ -240,12 +265,13 @@ ModLoader._build_mod_table = function(self)
loaded_packages = {}, loaded_packages = {},
packages = mod_data.packages, packages = mod_data.packages,
data = mod_data, data = mod_data,
bundled = mod_data.bundled or false,
} }
end end
self._num_mods = #self._mods self._num_mods = #self._mods
Log.info("ModLoader", "Found %i mods", #self._mods) Log.info("ModLoader", "Found %i mods", self._num_mods)
end end
ModLoader._load_mod = function(self, index) ModLoader._load_mod = function(self, index)
@ -267,9 +293,12 @@ ModLoader._load_mod = function(self, index)
self._mod_load_index = index self._mod_load_index = index
if mod.bundled and mod.packages[1] then
self:_load_package(mod, 1) self:_load_package(mod, 1)
return "loading" return "loading"
else
return "initializing"
end
end end
ModLoader._load_package = function(self, mod, index) ModLoader._load_package = function(self, mod, index)
@ -287,7 +316,7 @@ ModLoader._load_package = function(self, mod, index)
ResourcePackage.load(resource_handle) ResourcePackage.load(resource_handle)
mod.loaded_packages[#mod.loaded_packages + 1] = resource_handle table.insert(mod.loaded_packages, resource_handle)
end end
ModLoader.unload_all_mods = function(self) ModLoader.unload_all_mods = function(self)
@ -353,13 +382,7 @@ end
ModLoader.on_game_state_changed = function(self, status, state_name, state_object) ModLoader.on_game_state_changed = function(self, status, state_name, state_object)
if self._state == "done" then if self._state == "done" then
for i = 1, self._num_mods, 1 do self:_run_callbacks("on_game_state_changed", status, state_name, state_object)
local mod = self._mods[i]
if mod and not mod.callbacks_disabled then
self:_run_callback(mod, "on_game_state_changed", status, state_name, state_object)
end
end
else else
Log.warning("ModLoader", "Ignored on_game_state_changed call due to being in state %q", self._state) Log.warning("ModLoader", "Ignored on_game_state_changed call due to being in state %q", self._state)
end end