Update from 22.06.2025: add MapGen mod outline, fix some require() issues
This commit is contained in:
@@ -3,40 +3,51 @@ local tostring = tostring
|
||||
Ensure = {}
|
||||
|
||||
--- Validates a function argument's type and throws a descriptive error on mismatch.
|
||||
--- @param arg any The argument value to type-check
|
||||
--- @param expectedType string Expected Lua type name (e.g., "number", "table", "string")
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param function_name string Name of the parent function for error reporting
|
||||
function Ensure.argType(arg, expectedType, n, function_name)
|
||||
--- @param arg any The argument value to type-check
|
||||
--- @param expectedType string Expected Lua type name (e.g., "number", "table", "string")
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param functionName string Name of the parent function for error reporting
|
||||
function Ensure.argType(arg, expectedType, n, functionName)
|
||||
local actualType = type(arg)
|
||||
assert(actualType == expectedType, string.format(
|
||||
'bad argument #%s to \'%s\' (%s expected, got %s)',
|
||||
tostring(n),
|
||||
function_name,
|
||||
functionName,
|
||||
expectedType,
|
||||
actualType))
|
||||
end
|
||||
|
||||
local ensureArgType = Ensure.argType
|
||||
|
||||
--- @param arg string The string to check
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param function_name string Name of the parent function for error reporting
|
||||
function Ensure.stringArgNotEmpty(arg, n, function_name)
|
||||
--- @param arg any The argument value to type-check
|
||||
--- @param allowedOptions table Lookup table where keys represent valid options (with true values)
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param functionName string Name of the parent function for error reporting
|
||||
function Ensure.argIsAllowed(arg, allowedOptions , n, functionName)
|
||||
assert(allowedOptions[arg] == true, string.format(
|
||||
'bad argument #%s to \'%s\' (the value is not allowed)',
|
||||
tostring(n),
|
||||
functionName))
|
||||
end
|
||||
|
||||
--- @param arg string The string to check
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param functionName string Name of the parent function for error reporting
|
||||
function Ensure.stringArgNotEmpty(arg, n, functionName)
|
||||
ensureArgType(arg, 'string', 1, 'Ensure.stringArgNotEmpty')
|
||||
assert(#arg ~= 0, string.format('bad argument #%s to \'%s\' (string cannot be empty)', n, function_name))
|
||||
assert(#arg ~= 0, string.format('bad argument #%s to \'%s\' (string cannot be empty)', n, functionName))
|
||||
end
|
||||
|
||||
--- @param arg any The argument value to check
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param function_name string Name of the parent function for error reporting
|
||||
function Ensure.argNotNil(arg, n, function_name)
|
||||
assert(arg ~= nil, string.format('bad argument #%s to \'%s\' (key cannot be nil)', n, function_name))
|
||||
--- @param arg any The argument value to check
|
||||
--- @param n number Argument position in function call (1-based index)
|
||||
--- @param functionName string Name of the parent function for error reporting
|
||||
function Ensure.argNotNil(arg, n, functionName)
|
||||
assert(arg ~= nil, string.format('bad argument #%s to \'%s\' (key cannot be nil)', n, functionName))
|
||||
end
|
||||
|
||||
--- @param divider number The number to check
|
||||
--- @param function_name string Name of the parent function for error reporting
|
||||
function Ensure.zeroDivision(divider, function_name)
|
||||
assert(divider ~= 0, string.format('Division by zero in \'%s\'', function_name))
|
||||
--- @param divider number The number to check
|
||||
--- @param functionName string Name of the parent function for error reporting
|
||||
function Ensure.zeroDivision(divider, functionName)
|
||||
assert(divider ~= 0, string.format('Division by zero in \'%s\'', functionName))
|
||||
end
|
||||
|
||||
|
||||
9
mods/API/Math/init.lua
Normal file
9
mods/API/Math/init.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
local math = math
|
||||
|
||||
math.lerp = function (a, b, t)
|
||||
return a + (b - a) * t
|
||||
end
|
||||
|
||||
math.clamp = function(value, min, max)
|
||||
return math.min(math.max(value, min), max)
|
||||
end
|
||||
1
mods/API/Math/mod.conf
Normal file
1
mods/API/Math/mod.conf
Normal file
@@ -0,0 +1 @@
|
||||
name = api__math
|
||||
@@ -1,12 +1,18 @@
|
||||
_G.Mod = {}
|
||||
Mod = {}
|
||||
|
||||
function Mod:new(addToGlobal)
|
||||
for key, value in pairs(addToGlobal) do
|
||||
_G[key] = value
|
||||
end
|
||||
local modName = core.get_current_modname()
|
||||
local modPath = core.get_modpath(modName)
|
||||
|
||||
local modTable = ModTable:new(modName, modPath)
|
||||
modTable.require = ModTable:getRequire()
|
||||
function Mod.getInfo()
|
||||
local name = core.get_current_modname()
|
||||
local path = core.get_modpath(name)
|
||||
local require = Require.getModRequire(name, path)
|
||||
|
||||
return {name = name, path = path, require = require}
|
||||
end
|
||||
|
||||
function Mod:new()
|
||||
local mod = Mod.getInfo()
|
||||
|
||||
setmetatable(mod, self)
|
||||
self.__index = self
|
||||
|
||||
return mod
|
||||
end
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
name = api__mod
|
||||
depends = api__mod_table, api__require,
|
||||
name = api__mod_table
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
ModTable = {}
|
||||
|
||||
local modLoggers = {}
|
||||
local loadedModules = {}
|
||||
local DS = '/'
|
||||
|
||||
function ModTable:getRequire()
|
||||
return function(fileName)
|
||||
local modName = self.name
|
||||
local modPath = self.path
|
||||
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')
|
||||
|
||||
if result ~= nil then
|
||||
loadedModules[fullName] = result
|
||||
else
|
||||
loadedModules[fullName] = true
|
||||
end
|
||||
|
||||
return loadedModules[fullName]
|
||||
end
|
||||
end
|
||||
|
||||
function ModTable:new(name, path)
|
||||
local modTable = {
|
||||
name = name,
|
||||
path = path,
|
||||
}
|
||||
|
||||
setmetatable(modTable, self)
|
||||
self.__index = self
|
||||
|
||||
return modTable
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
name = api__mod_table
|
||||
@@ -1,6 +0,0 @@
|
||||
Path = {}
|
||||
|
||||
|
||||
function Path:new()
|
||||
return
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
name = api__path
|
||||
@@ -1,11 +1,19 @@
|
||||
_G.Require = {}
|
||||
Require = {}
|
||||
local loadedModules = {}
|
||||
local DS = '/'
|
||||
|
||||
function Require.getModRequire()
|
||||
return function(fileName)
|
||||
local modName = self.name
|
||||
local modPath = self.path
|
||||
--- 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
|
||||
local function require(fileName)
|
||||
local fullName = modName .. '..' .. fileName
|
||||
|
||||
local module = loadedModules[fullName]
|
||||
@@ -23,4 +31,6 @@ function Require.getModRequire()
|
||||
|
||||
return loadedModules[fullName]
|
||||
end
|
||||
|
||||
return require
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ local sub, upper, ensureArgType
|
||||
|
||||
--- Writes a string with a capital letter.
|
||||
--- @param str string Input string.
|
||||
--- @return string
|
||||
--- @return string?
|
||||
function string.capitalize(str)
|
||||
ensureArgType(str, "string", 1, "string.capitalize")
|
||||
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
name = api__string
|
||||
depends = api__table
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
Api = {}
|
||||
Core = {}
|
||||
Environment = {}
|
||||
Game = {}
|
||||
Game.Items = {}
|
||||
Game.Mobs = {}
|
||||
Game.Structures = {}
|
||||
Ui = {}
|
||||
|
||||
local allowedDomains = {
|
||||
Api = true,
|
||||
Core = true,
|
||||
Environment = true,
|
||||
Game = true,
|
||||
Ui = true
|
||||
}
|
||||
|
||||
--- Adds a mod to the global space under specified domain structure
|
||||
---@param mod table The mod table containing functionality to expose -- TODO: !!!Make a Mod class!!!
|
||||
---@param location string Dot-separated path within domain structure (e.g. "Items.Log")
|
||||
---@param force boolean? Whether to overwrite the target location.
|
||||
Api.addModToGlobalSpace = function(mod, location, force)
|
||||
-- Ensure `mod` type and that it's not empty
|
||||
Ensure.argType(mod, 'table', 1, 'Api.addModToGlobalSpace')
|
||||
|
||||
-- Ensure `location` type and that it's not empty
|
||||
Ensure.argType(location, 'string', 2, 'Api.addModToGlobalSpace')
|
||||
Ensure.stringArgNotEmpty(location, 2, 'Api.addModToGlobalSpace')
|
||||
|
||||
-- Ensure `location` matches this format: 'field.subfield.subsubfield.<...>'
|
||||
if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then
|
||||
error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)')
|
||||
end
|
||||
|
||||
-- Split `location` into fields -- TODO: !!!Make a String function!!!
|
||||
local fields = {}
|
||||
local domain = nil
|
||||
for field in string.gmatch(location, '[^.]+') do
|
||||
if domain ~= nil then
|
||||
table.insert(fields, field)
|
||||
else
|
||||
domain = field
|
||||
end
|
||||
end
|
||||
|
||||
-- Ensure the domain is allowed
|
||||
Ensure.argIsAllowed(domain, allowedDomains , 2, 'Api.addModToGlobalSpace')
|
||||
|
||||
-- Handle case where we're modifying the domain root directly
|
||||
if #fields == 0 then
|
||||
error('Not allowed to directly write into the \''.. domain ..'\' domain')
|
||||
end
|
||||
|
||||
-- Start traversing from the domain's table
|
||||
local current = _G[domain]
|
||||
assert(type(current) == 'table', 'The global domain is not a table')
|
||||
|
||||
-- Traverse each field of the location
|
||||
local num_fields = #fields
|
||||
for i = 1, num_fields - 1 do
|
||||
local field = fields[i]
|
||||
if current[field] == nil then
|
||||
current[field] = {}
|
||||
end
|
||||
current = current[field]
|
||||
assert(type(current) == 'table', 'intermediate field is not a table')
|
||||
end
|
||||
|
||||
-- Assign mod to the final field
|
||||
local last_field = fields[num_fields]
|
||||
if force and current[last_field] ~= nil then
|
||||
error(string.format('field \'%s\' already exists in \'%s\'', last_field, location))
|
||||
end
|
||||
current[last_field] = mod
|
||||
end
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
name = api__system
|
||||
depends = api__mod, api__string,
|
||||
description = Include this mod in your `depends` to ensure all necessary APIs are loaded for your mod
|
||||
depends = api__ensure, api__math, api__mod, api__require, api__table, api__string, api__utf8,
|
||||
|
||||
@@ -22,7 +22,7 @@ end
|
||||
|
||||
--- Returns all keys from the table `t` as an array.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @return any[]
|
||||
--- @return string[] | number[]
|
||||
function table.get_keys(t)
|
||||
ensureArgType(t, 'table', 1, 'table.get_keys')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user