feat: Move class and require hooks into early loading

This commit is contained in:
Lucas Schwiderski 2023-03-01 00:23:41 +01:00
parent 33756cd6d5
commit 2d15647fb5
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
4 changed files with 9 additions and 79 deletions

View file

@ -1,26 +0,0 @@
local original_class = Mods.original_class or class
Mods.original_class = original_class
local _G = _G
local rawget = rawget
local rawset = rawset
-- The `__index` metamethod maps a proper identifier `CLASS.MyClassName` to the
-- stringified version of the key: `"MyClassName"`.
-- This allows using LuaCheck for the stringified class names in hook parameters.
_G.CLASS = _G.CLASS or setmetatable({}, {
__index = function(_, key)
return key
end
})
class = function(class_name, super_name, ...)
local result = original_class(class_name, super_name, ...)
if not rawget(_G, class_name) then
rawset(_G, class_name, result)
end
if not rawget(_G.CLASS, class_name) then
rawset(_G.CLASS, class_name, result)
end
return result
end

View file

@ -1,3 +1,6 @@
dofile("scripts/mods/dml/message")
dofile("scripts/mods/dml/hook")
local StateGame = require("scripts/game_states/state_game") local StateGame = require("scripts/game_states/state_game")
local StateSplash = require("scripts/game_states/game/state_splash") local StateSplash = require("scripts/game_states/game/state_splash")
local GameStateMachine = require("scripts/foundation/utilities/game_state_machine") local GameStateMachine = require("scripts/foundation/utilities/game_state_machine")
@ -6,20 +9,9 @@ local GameStateMachine = require("scripts/foundation/utilities/game_state_machin
-- to initialize the modding environment. -- to initialize the modding environment.
local loader = {} local loader = {}
Mods = {} function loader:init(mod_data, boot_gui)
function loader:init(mod_data, libs, boot_gui)
-- The metatable prevents overwriting these
self._libs = setmetatable({}, { __index = libs })
Mods.lua = self._libs
dofile("scripts/mods/dml/message")
dofile("scripts/mods/dml/require")
dofile("scripts/mods/dml/class")
dofile("scripts/mods/dml/hook")
local ModLoader = dofile("scripts/mods/dml/mod_loader") local ModLoader = dofile("scripts/mods/dml/mod_loader")
local mod_loader = ModLoader:new(mod_data, libs, boot_gui) local mod_loader = ModLoader:new(mod_data, boot_gui)
self._mod_loader = mod_loader self._mod_loader = mod_loader
Managers.mod = mod_loader Managers.mod = mod_loader

View file

@ -26,10 +26,10 @@ local BUTTON_INDEX_R = Keyboard.button_index("r")
local BUTTON_INDEX_LEFT_SHIFT = Keyboard.button_index("left shift") local BUTTON_INDEX_LEFT_SHIFT = Keyboard.button_index("left shift")
local BUTTON_INDEX_LEFT_CTRL = Keyboard.button_index("left ctrl") local BUTTON_INDEX_LEFT_CTRL = Keyboard.button_index("left ctrl")
ModLoader.init = function(self, mod_data, libs, boot_gui) ModLoader.init = function(self, mod_data, boot_gui)
table.dump(mod_data, nil, 5, function(...) Log.info("ModLoader", ...) end) table.dump(mod_data, nil, 5, function(...) Log.info("ModLoader", ...) end)
self._mod_data = mod_data self._mod_data = mod_data
self._libs = libs
self._gui = boot_gui self._gui = boot_gui
self._settings = Application.user_setting("mod_settings") or DEFAULT_SETTINGS self._settings = Application.user_setting("mod_settings") or DEFAULT_SETTINGS
@ -142,7 +142,7 @@ ModLoader.update = function(self, dt)
if next_index > #mod_data.packages then if next_index > #mod_data.packages then
mod.state = "running" mod.state = "running"
local ok, object = xpcall(mod_data.run, self._libs.debug.traceback) local ok, object = xpcall(mod_data.run, Script.callstack)
if not ok then if not ok then
Log.error("ModLoader", "Failed 'run' for %q: %s", mod.name, object) Log.error("ModLoader", "Failed 'run' for %q: %s", mod.name, object)
@ -198,7 +198,7 @@ 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, self._libs.debug.traceback) local success, val = xpcall(function() return cb(object, table_unpack(args)) end, Script.callstack)
if success then if success then
return val return val

View file

@ -1,36 +0,0 @@
local require_store = Mods.require_store or {}
Mods.require_store = require_store
local original_require = Mods.original_require or require
Mods.original_require = original_require
local can_insert = function(filepath, new_result)
local store = require_store[filepath]
if not store or #store then
return true
end
if store[#store] ~= new_result then
return true
end
end
require = function(filepath, ...)
local result = original_require(filepath, ...)
if result and type(result) == "table" then
if can_insert(filepath, result) then
require_store[filepath] = require_store[filepath] or {}
local store = require_store[filepath]
table.insert(store, result)
--print("[Require] #" .. tostring(#store) .. " of " .. filepath)
local Mods = Mods
if Mods.hook then
Mods.hook.enable_by_file(filepath, #store)
end
end
end
return result
end