Mod & Require: add load function
This commit is contained in:
@@ -2,26 +2,36 @@ Require = {}
|
||||
local loadedModules = {}
|
||||
local DS = '/'
|
||||
|
||||
--- Creates a scoped require function for a specific module
|
||||
--- This custom require handles module loading relative to a specific module path,
|
||||
--- prevents duplicate loading, and caches results appropriately.
|
||||
--- @param modName string Namespace identifier for the module (prevents global collisions)
|
||||
--- @param modPath string Absolute base path to the module's directory
|
||||
--- @return function require Custom require function scoped to the specified module
|
||||
function Require.getModRequire(modName, modPath)
|
||||
--- Custom require function for module-specific loading
|
||||
--- Resolves files relative to modPath/src, converts dot-notation to path separators
|
||||
--- @param fileName string Module path in dot-notation (e.g. "lib.utils")
|
||||
--- @return any loadedModule The loaded module's return value or cached result
|
||||
---A helper function that returns the path to a file.
|
||||
---@param modPath string
|
||||
---@param fileName string
|
||||
---@return string
|
||||
function Require:getFilePath(modPath, fileName)
|
||||
return modPath..DS..'src'..DS..fileName:gsub('%.', DS)..'.lua'
|
||||
end
|
||||
|
||||
---Creates a scoped require function for a specific module.
|
||||
---
|
||||
---This custom require handles module loading relative to a specific module path,
|
||||
---prevents duplicate loading, and caches results appropriately.
|
||||
---@param modName string Namespace identifier for the module (prevents global collisions)
|
||||
---@param modPath string Absolute base path to the module's directory
|
||||
---@return function require Custom require function scoped to the specified module
|
||||
function Require:getModRequire(modName, modPath)
|
||||
---Custom require function for module-specific loading.
|
||||
---
|
||||
---Resolves files relative to modPath/src, converts dot-notation to path separators.
|
||||
---@param fileName string Module path in dot-notation (e.g. "lib.utils")
|
||||
---@return table loadedModule The loaded module's return value or cached result
|
||||
local function require(fileName)
|
||||
local fullName = modName .. '..' .. fileName
|
||||
local fullName = modName..'..'..fileName
|
||||
|
||||
local module = loadedModules[fullName]
|
||||
if module ~= nil then
|
||||
return module
|
||||
end
|
||||
|
||||
local result = dofile(modPath .. DS .. 'src' .. DS .. fileName:gsub('%.', DS) .. '.lua')
|
||||
local result = dofile(self:getFilePath(modPath, fileName))
|
||||
|
||||
if result ~= nil then
|
||||
loadedModules[fullName] = result
|
||||
@@ -35,4 +45,38 @@ function Require.getModRequire(modName, modPath)
|
||||
return require
|
||||
end
|
||||
|
||||
return Require
|
||||
---Creates a load function bound to a specific module.
|
||||
---
|
||||
---This specialized load function ensures that scripts are executed with the specified class passed to them,
|
||||
---and prevents scripts from being executed more than once by throwing an error.
|
||||
---@param modName string Namespace identifier for the module (prevents global collisions)
|
||||
---@param modPath string Absolute base path to the module's directory
|
||||
---@return function load Custom load function scoped to the specified module
|
||||
function Require:getModLoad(modName, modPath)
|
||||
---Custom load function for loading modules.
|
||||
---
|
||||
---Resolves file paths relative to modPath/src and converts dot notation into path separators.
|
||||
---@param class table A class within the scope of which a script is to be executed
|
||||
---@param fileName string Module path in dot-notation (e.g. "lib.utils")
|
||||
local function load(class, fileName)
|
||||
local fullName = modName..'..'..fileName
|
||||
|
||||
local module = loadedModules[fullName]
|
||||
if module ~= nil then
|
||||
error(('Repeated execute of script `%s` is not allowed.'):format(modPath))
|
||||
end
|
||||
|
||||
local result = loadfile(self:getFilePath(modPath, fileName))
|
||||
if result == nil then
|
||||
error()
|
||||
end
|
||||
|
||||
result(class)
|
||||
|
||||
loadedModules[fullName] = true
|
||||
end
|
||||
|
||||
return load
|
||||
end
|
||||
|
||||
return Require
|
||||
|
||||
Reference in New Issue
Block a user