HelperLib: rename load() -> loadFile(), add doFile()
This commit is contained in:
@@ -1,20 +1,24 @@
|
|||||||
|
---SMC helper lib
|
||||||
|
---@class Smc
|
||||||
|
Smc = {}
|
||||||
|
|
||||||
---@type boolean
|
---@type boolean
|
||||||
Smc.isAsync = false
|
Smc.isAsync = false
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__system__helper_lib')
|
local modInfo = Mod.getInfo('smc__system__helper_lib')
|
||||||
local load = modInfo.load
|
local doFile = modInfo.doFile
|
||||||
|
|
||||||
-- Independent of SMC methods
|
-- Independent of SMC methods
|
||||||
load(Smc, 'Lib.activeFormspecs')
|
doFile('Lib.activeFormspecs')
|
||||||
load(Smc, 'Lib.globalStep')
|
doFile('Lib.globalStep')
|
||||||
load(Smc, 'Lib.inventoryHelpers')
|
doFile('Lib.inventoryHelpers')
|
||||||
load(Smc, 'Lib.color')
|
doFile('Lib.color')
|
||||||
load(Smc, 'Lib.itemStackInventory')
|
doFile('Lib.itemStackInventory')
|
||||||
load(Smc, 'Lib.nodeSuperClasses')
|
doFile('Lib.nodeSuperClasses')
|
||||||
load(Smc, 'Lib.getObjectByGUID')
|
doFile('Lib.getObjectByGUID')
|
||||||
load(Smc, 'Lib.receiveFields')
|
doFile('Lib.receiveFields')
|
||||||
load(Smc, 'Lib.registeredItems')
|
doFile('Lib.registeredItems')
|
||||||
load(Smc, 'Lib.loads')
|
doFile('Lib.requires')
|
||||||
|
|
||||||
--- Dependent of SMC methods
|
--- Dependent of SMC methods
|
||||||
load(Smc, 'Lib.matchItemDescription')
|
doFile('Lib.matchItemDescription')
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ Mod = {}
|
|||||||
---@field shortName string
|
---@field shortName string
|
||||||
---@field path string
|
---@field path string
|
||||||
---@field require fun(fileName:string):table
|
---@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
|
---Returns the mod name without prefixes
|
||||||
---@param name string
|
---@param name string
|
||||||
@@ -27,14 +28,16 @@ function Mod.getInfo(modname)
|
|||||||
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)
|
local loadFile = Require:getModLoadFile(name, path)
|
||||||
|
local doFile = Require:getModDoFile(path)
|
||||||
|
|
||||||
local info = {
|
local info = {
|
||||||
name = name,
|
name = name,
|
||||||
shortName = shortName,
|
shortName = shortName,
|
||||||
path = path,
|
path = path,
|
||||||
require = require,
|
require = require,
|
||||||
load = load
|
loadFile = loadFile,
|
||||||
|
doFile = doFile
|
||||||
}
|
}
|
||||||
|
|
||||||
return info
|
return info
|
||||||
|
|||||||
@@ -52,17 +52,16 @@ end
|
|||||||
---@param modName string Namespace identifier for the module (prevents global collisions)
|
---@param modName string Namespace identifier for the module (prevents global collisions)
|
||||||
---@param modPath string Absolute base path to the module's directory
|
---@param modPath string Absolute base path to the module's directory
|
||||||
---@return function load Custom load function scoped to the specified module
|
---@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.
|
---Custom load function for loading modules.
|
||||||
---
|
---
|
||||||
---Resolves file paths relative to modPath/src and converts dot notation into path separators.
|
---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 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")
|
---@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 fullName = modName..'..'..fileName
|
||||||
|
|
||||||
local module = loadedModules[fullName]
|
if loadedModules[fullName] ~= nil then
|
||||||
if module ~= nil then
|
|
||||||
error(('Repeated execute of script `%s` is not allowed.'):format(modPath))
|
error(('Repeated execute of script `%s` is not allowed.'):format(modPath))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -76,7 +75,27 @@ function Require:getModLoad(modName, modPath)
|
|||||||
loadedModules[fullName] = true
|
loadedModules[fullName] = true
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return Require
|
return Require
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
|||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__ui__player_inventory')
|
local modInfo = Mod.getInfo('smc__ui__player_inventory')
|
||||||
local require = modInfo.require
|
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.
|
---The distance at which a player can interact with blocks that have a node-tab.
|
||||||
---
|
---
|
||||||
@@ -434,10 +434,10 @@ end)
|
|||||||
|
|
||||||
--- Inventory action handling ---
|
--- Inventory action handling ---
|
||||||
|
|
||||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.general')
|
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.general')
|
||||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.itemsList')
|
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.itemsList')
|
||||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.crafts')
|
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.crafts')
|
||||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.adminList')
|
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.adminList')
|
||||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.equip')
|
loadFile(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.equip')
|
||||||
|
|
||||||
return PlayerInventoryUI
|
return PlayerInventoryUI
|
||||||
|
|||||||
Reference in New Issue
Block a user