102 lines
3.4 KiB
Lua
102 lines
3.4 KiB
Lua
Require = {}
|
|
local loadedModules = {}
|
|
local DS = '/'
|
|
|
|
---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 module = loadedModules[fullName]
|
|
if module ~= nil then
|
|
return module
|
|
end
|
|
|
|
local result = dofile(self:getFilePath(modPath, fileName))
|
|
|
|
if result ~= nil then
|
|
loadedModules[fullName] = result
|
|
else
|
|
loadedModules[fullName] = true
|
|
end
|
|
|
|
return loadedModules[fullName]
|
|
end
|
|
|
|
return require
|
|
end
|
|
|
|
---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:getModLoadFile(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 loadFile(class, fileName)
|
|
local fullName = modName..'..'..fileName
|
|
|
|
if loadedModules[fullName] ~= 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 loadFile
|
|
end
|
|
|
|
---Creates dofile function for loading files.
|
|
---
|
|
---This loading function will execute a standard `dofile` for the specified script.
|
|
---@param modPath string Absolute base path to the module's directory
|
|
---@return function doFile Custom dofile function scoped to the specified file
|
|
function Require:getModDoFile(modPath)
|
|
---Custom `dofile` function for loading files.
|
|
---
|
|
---Resolves file paths relative to modPath/src and converts dot notation into path separators.
|
|
---@param fileName string File path in dot-notation (e.g. "lib.utils")
|
|
---@return any?
|
|
local function doFile(fileName)
|
|
local result = dofile(self:getFilePath(modPath, fileName))
|
|
|
|
return result
|
|
end
|
|
|
|
return doFile
|
|
end
|
|
|
|
return Require
|