Mod & Require: add load function

This commit is contained in:
Van
2026-11-11 21:01:59 +03:00
parent af0b7c8beb
commit 24932b5d2e
2 changed files with 78 additions and 23 deletions

View File

@@ -1,12 +1,14 @@
---Luanti mod ---Luanti mod
---@class 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 = {} 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 ---Returns the mod name without prefixes
---@param name string ---@param name string
---@return string ---@return string
@@ -18,15 +20,24 @@ end
---Return table with mod name, path and require function. ---Return table with mod name, path and require function.
---@param modname? string Specify a name of mod to get its info ---@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) function Mod.getInfo(modname)
local name = modname or core.get_current_modname() local name = modname or core.get_current_modname()
local shortName = getShortModName(name) local shortName = getShortModName(name)
local path = core.get_modpath(name) local path = core.get_modpath(name)
assert(path ~= nil, ('A mod `%s` does not exist.'):format(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 end
--[[ --[[

View File

@@ -2,26 +2,36 @@ Require = {}
local loadedModules = {} local loadedModules = {}
local DS = '/' local DS = '/'
--- Creates a scoped require function for a specific module ---A helper function that returns the path to a file.
--- This custom require handles module loading relative to a specific module path, ---@param modPath string
--- prevents duplicate loading, and caches results appropriately. ---@param fileName string
--- @param modName string Namespace identifier for the module (prevents global collisions) ---@return string
--- @param modPath string Absolute base path to the module's directory function Require:getFilePath(modPath, fileName)
--- @return function require Custom require function scoped to the specified module return modPath..DS..'src'..DS..fileName:gsub('%.', DS)..'.lua'
function Require.getModRequire(modName, modPath) end
--- Custom require function for module-specific loading
--- Resolves files relative to modPath/src, converts dot-notation to path separators ---Creates a scoped require function for a specific module.
--- @param fileName string Module path in dot-notation (e.g. "lib.utils") ---
--- @return any loadedModule The loaded module's return value or cached result ---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 function require(fileName)
local fullName = modName .. '..' .. fileName local fullName = modName..'..'..fileName
local module = loadedModules[fullName] local module = loadedModules[fullName]
if module ~= nil then if module ~= nil then
return module return module
end end
local result = dofile(modPath .. DS .. 'src' .. DS .. fileName:gsub('%.', DS) .. '.lua') local result = dofile(self:getFilePath(modPath, fileName))
if result ~= nil then if result ~= nil then
loadedModules[fullName] = result loadedModules[fullName] = result
@@ -35,4 +45,38 @@ function Require.getModRequire(modName, modPath)
return require return require
end 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: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 return Require