HelperLib: rename load() -> loadFile(), add doFile()

This commit is contained in:
Van
2026-11-13 17:06:08 +03:00
parent 2243ed7532
commit 1e91290b02
4 changed files with 52 additions and 26 deletions

View File

@@ -1,20 +1,24 @@
---SMC helper lib
---@class Smc
Smc = {}
---@type boolean
Smc.isAsync = false
local modInfo = Mod.getInfo('smc__system__helper_lib')
local load = modInfo.load
local doFile = modInfo.doFile
-- Independent of SMC methods
load(Smc, 'Lib.activeFormspecs')
load(Smc, 'Lib.globalStep')
load(Smc, 'Lib.inventoryHelpers')
load(Smc, 'Lib.color')
load(Smc, 'Lib.itemStackInventory')
load(Smc, 'Lib.nodeSuperClasses')
load(Smc, 'Lib.getObjectByGUID')
load(Smc, 'Lib.receiveFields')
load(Smc, 'Lib.registeredItems')
load(Smc, 'Lib.loads')
doFile('Lib.activeFormspecs')
doFile('Lib.globalStep')
doFile('Lib.inventoryHelpers')
doFile('Lib.color')
doFile('Lib.itemStackInventory')
doFile('Lib.nodeSuperClasses')
doFile('Lib.getObjectByGUID')
doFile('Lib.receiveFields')
doFile('Lib.registeredItems')
doFile('Lib.requires')
--- Dependent of SMC methods
load(Smc, 'Lib.matchItemDescription')
doFile('Lib.matchItemDescription')

View File

@@ -7,7 +7,8 @@ Mod = {}
---@field shortName string
---@field path string
---@field require fun(fileName:string):table
---@field load fun(class:table, fileName:string)
---@field loadFile fun(class:table, fileName:string)
---@field doFile fun(fileName:string)
---Returns the mod name without prefixes
---@param name string
@@ -27,14 +28,16 @@ function Mod.getInfo(modname)
local path = core.get_modpath(name)
assert(path ~= nil, ('A mod `%s` does not exist.'):format(name))
local require = Require:getModRequire(name, path)
local load = Require:getModLoad(name, path)
local loadFile = Require:getModLoadFile(name, path)
local doFile = Require:getModDoFile(path)
local info = {
name = name,
shortName = shortName,
path = path,
require = require,
load = load
loadFile = loadFile,
doFile = doFile
}
return info

View File

@@ -52,17 +52,16 @@ end
---@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)
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 load(class, fileName)
local function loadFile(class, fileName)
local fullName = modName..'..'..fileName
local module = loadedModules[fullName]
if module ~= nil then
if loadedModules[fullName] ~= nil then
error(('Repeated execute of script `%s` is not allowed.'):format(modPath))
end
@@ -76,7 +75,27 @@ function Require:getModLoad(modName, modPath)
loadedModules[fullName] = true
end
return load
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

View File

@@ -13,7 +13,7 @@ local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
local modInfo = Mod.getInfo('smc__ui__player_inventory')
local require = modInfo.require
local load = modInfo.load
local loadFile = modInfo.loadFile
---The distance at which a player can interact with blocks that have a node-tab.
---
@@ -434,10 +434,10 @@ end)
--- Inventory action handling ---
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.general')
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.itemsList')
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.crafts')
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.adminList')
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.equip')
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.general')
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.itemsList')
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.crafts')
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.adminList')
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.equip')
return PlayerInventoryUI