2021-08-17 21:23:10 +03:00
|
|
|
-----------------------------------
|
|
|
|
|
-- Module helpers
|
|
|
|
|
-----------------------------------
|
2023-03-04 13:18:03 +00:00
|
|
|
xi = xi or {}
|
|
|
|
|
xi.module = xi.module or {}
|
|
|
|
|
|
2025-03-28 23:26:36 +00:00
|
|
|
--
|
|
|
|
|
-- applyOverride: Provides the "super" functionality for overriding functions
|
|
|
|
|
--
|
|
|
|
|
function applyOverride(base_table, name, func, fullname, filename)
|
|
|
|
|
local old = base_table[name]
|
|
|
|
|
|
|
|
|
|
local thisenv = getfenv(old)
|
|
|
|
|
|
|
|
|
|
local env = { super = old }
|
|
|
|
|
setmetatable(env, { __index = thisenv })
|
|
|
|
|
|
|
|
|
|
setfenv(func, env)
|
|
|
|
|
|
|
|
|
|
base_table[name] = func
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--
|
2023-03-04 13:18:03 +00:00
|
|
|
-- Helpers
|
2025-03-28 23:26:36 +00:00
|
|
|
--
|
2023-03-04 13:18:03 +00:00
|
|
|
|
|
|
|
|
-- Iterate through all the sections of a table-string, and instantiate them if they don't exist
|
2023-09-07 09:26:08 -04:00
|
|
|
-- Example: xi.module.ensureTable('xi.aName.anotherName') will ensure the table: xi.aName.anotherName
|
2023-03-04 13:18:03 +00:00
|
|
|
-- : is fully instantiated.
|
|
|
|
|
-- https://github.com/LandSandBoat/server/issues/3542#issuecomment-1407190523
|
|
|
|
|
xi.module.ensureTable = function(str)
|
|
|
|
|
local parts = utils.splitStr(str, '.')
|
2023-07-15 11:21:44 -04:00
|
|
|
local table = _G
|
2023-03-04 13:18:03 +00:00
|
|
|
for _, part in ipairs(parts) do
|
|
|
|
|
table[part] = table[part] or {}
|
|
|
|
|
table = table[part]
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-08-17 21:23:10 +03:00
|
|
|
|
2023-07-03 17:10:59 +01:00
|
|
|
xi.module.modifyInteractionEntry = function(filename, modifyFunc)
|
|
|
|
|
package.loaded[filename] = nil -- Clear out the pre-required resource (it might not be there, but it doesn't matter)
|
|
|
|
|
local res = utils.prequire(filename) -- Load the resource
|
|
|
|
|
InteractionGlobal.lookup:removeContainer(res) -- Remove the resource from the container
|
|
|
|
|
modifyFunc(res) -- Run function to modify resource
|
|
|
|
|
InteractionGlobal.lookup:addContainer(res) -- Re-add resource to container
|
|
|
|
|
end
|
|
|
|
|
|
2021-08-17 21:23:10 +03:00
|
|
|
-- Override Object
|
|
|
|
|
Override = {}
|
|
|
|
|
Override.__index = Override
|
|
|
|
|
|
|
|
|
|
function Override:new(target_func_str, new_func)
|
|
|
|
|
local obj = {}
|
|
|
|
|
setmetatable(obj, self)
|
|
|
|
|
|
2022-03-19 17:32:56 +02:00
|
|
|
obj.name = target_func_str
|
2021-08-17 21:23:10 +03:00
|
|
|
obj.func = new_func
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Module Object
|
|
|
|
|
Module = {}
|
|
|
|
|
Module.__index = Module
|
|
|
|
|
|
|
|
|
|
function Module:new(name)
|
|
|
|
|
if name == nil or string.len(name) < 5 then
|
2023-09-07 09:26:08 -04:00
|
|
|
printf('Invalid module name: %s', name)
|
2021-08-17 21:23:10 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local obj = {}
|
|
|
|
|
setmetatable(obj, self)
|
|
|
|
|
|
|
|
|
|
obj.name = name
|
|
|
|
|
obj.overrides = {}
|
|
|
|
|
obj.enabled = false
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Module:setEnabled(isEnabled)
|
|
|
|
|
self.enabled = isEnabled
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Module:addOverride(target_func_str, func)
|
|
|
|
|
local override = Override:new(target_func_str, func)
|
|
|
|
|
table.insert(self.overrides, override)
|
|
|
|
|
end
|