Mod & Require: add load function
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
---Luanti mod
|
||||
---@class Mod
|
||||
---@field name string Technical name of the mod.
|
||||
---@field shortName string
|
||||
---@field path string Path to the mod folder.
|
||||
---@field Class any A public class that provides a mod.
|
||||
---@field require fun(FileName:string):table
|
||||
Mod = {}
|
||||
|
||||
---@class Mod.Info
|
||||
---@field name string
|
||||
---@field shortName string
|
||||
---@field path string
|
||||
---@field require fun(fileName:string):table
|
||||
---@field load fun(class:table, fileName:string)
|
||||
|
||||
---Returns the mod name without prefixes
|
||||
---@param name string
|
||||
---@return string
|
||||
@@ -18,15 +20,24 @@ end
|
||||
|
||||
---Return table with mod name, path and require function.
|
||||
---@param modname? string Specify a name of mod to get its info
|
||||
---@return {name: string, shortName: string, path: string, require: fun(FileName : string): table}
|
||||
---@return Mod.Info
|
||||
function Mod.getInfo(modname)
|
||||
local name = modname or core.get_current_modname()
|
||||
local shortName = getShortModName(name)
|
||||
local path = core.get_modpath(name)
|
||||
assert(path ~= nil, ('A mod `%s` does not exist.'):format(name))
|
||||
local require = Require.getModRequire(name, path)
|
||||
local require = Require:getModRequire(name, path)
|
||||
local load = Require:getModLoad(name, path)
|
||||
|
||||
return {name = name, shortName = shortName, path = path, require = require}
|
||||
local info = {
|
||||
name = name,
|
||||
shortName = shortName,
|
||||
path = path,
|
||||
require = require,
|
||||
load = load
|
||||
}
|
||||
|
||||
return info
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -65,4 +76,4 @@ function Mod:getModClassExtended(ChildClass)
|
||||
return childClass
|
||||
end --]]
|
||||
|
||||
return Mod
|
||||
return Mod
|
||||
|
||||
@@ -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