Rename API -> SYSTEM
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
local modInfo = Mod.getInfo('smc__api__api')
|
||||
local require = modInfo.require
|
||||
|
||||
require('Api')
|
||||
@@ -1,3 +0,0 @@
|
||||
name = smc__api__api
|
||||
description = Include this mod in your `depends` to ensure all necessary APIs are loaded for your mod
|
||||
depends = smc__api__ensure, smc__api__math, smc__api__mod, smc__api__require, smc__api__table, smc__api__string, smc__api__utf8, smc__api__class
|
||||
@@ -1,6 +0,0 @@
|
||||
Api = {}
|
||||
|
||||
local modInfo = Mod.getInfo('smc__api__api')
|
||||
local require = modInfo.require
|
||||
|
||||
require('Api.globalStep')
|
||||
@@ -1,75 +0,0 @@
|
||||
---@type {timeDelay:number, callbackFunc:fun(deltaTime:number), timer:number, lastCall:number}[]
|
||||
local callsWithDelay = {}
|
||||
|
||||
---@param timeDelay number
|
||||
---@param callbackFunc fun(deltaTime:number)
|
||||
function Api.registerGlobalStepWithDelay(timeDelay, callbackFunc)
|
||||
table.insert(callsWithDelay, {
|
||||
timeDelay = timeDelay,
|
||||
callbackFunc = callbackFunc,
|
||||
timer = 0.0,
|
||||
lastCall = 0.0
|
||||
})
|
||||
end
|
||||
|
||||
core.register_globalstep(function(deltaTime)
|
||||
---@type number
|
||||
local timer
|
||||
local lastCall
|
||||
|
||||
for _, callback in ipairs(callsWithDelay) do
|
||||
timer = callback.timer
|
||||
lastCall = callback.lastCall
|
||||
|
||||
timer = timer + deltaTime
|
||||
lastCall = lastCall + deltaTime
|
||||
|
||||
if timer >= callback.timeDelay then
|
||||
callback.callbackFunc(lastCall)
|
||||
callback.timer = timer - callback.timeDelay
|
||||
callback.lastCall = 0.0
|
||||
else
|
||||
callback.timer = timer
|
||||
callback.lastCall = lastCall
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
---@type {timeDelay:number, callbackFunc:fun(deltaTime:number, player), timer:number, lastCall:number}[]
|
||||
local callsForEachPlayer = {}
|
||||
|
||||
---@param timeDelay number
|
||||
---@param callbackFunc fun(deltaTime:number, player:Player)
|
||||
function Api.registerGlobalStepForEachPlayer(timeDelay, callbackFunc)
|
||||
table.insert(callsWithDelay, {
|
||||
timeDelay = timeDelay,
|
||||
callbackFunc = callbackFunc,
|
||||
timer = 0.0,
|
||||
lastCall = 0.0,
|
||||
})
|
||||
end
|
||||
|
||||
core.register_globalstep(function(deltaTime)
|
||||
---@type number
|
||||
local timer
|
||||
local lastCall
|
||||
|
||||
for _, player in ipairs(core.get_connected_players()) do
|
||||
for _, callback in ipairs(callsForEachPlayer) do
|
||||
timer = callback.timer
|
||||
lastCall = callback.lastCall
|
||||
|
||||
timer = timer + deltaTime
|
||||
lastCall = lastCall + deltaTime
|
||||
|
||||
if timer >= callback.timeDelay then
|
||||
callback.callbackFunc(lastCall, player)
|
||||
callback.timer = timer - callback.timeDelay
|
||||
callback.lastCall = 0.0
|
||||
else
|
||||
callback.timer = timer
|
||||
callback.lastCall = lastCall
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
@@ -1,9 +0,0 @@
|
||||
---@class Class
|
||||
Class = {}
|
||||
|
||||
---Returns a child class with inherited parameters from the parent class.
|
||||
---@param parent table
|
||||
---@param child table
|
||||
function Class.extend(parent ,child)
|
||||
return setmetatable(child or {}, { __index = parent })
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__class
|
||||
@@ -1,53 +0,0 @@
|
||||
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 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),
|
||||
functionName,
|
||||
expectedType,
|
||||
actualType))
|
||||
end
|
||||
|
||||
local ensureArgType = Ensure.argType
|
||||
|
||||
--- @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, functionName))
|
||||
end
|
||||
|
||||
--- @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 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
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__ensure
|
||||
@@ -1,33 +0,0 @@
|
||||
Events = {}
|
||||
|
||||
---@alias Events.callback fun(eventName, ...)
|
||||
|
||||
---@type table<string, Events.callback[]>
|
||||
local registeredEvents = {}
|
||||
|
||||
---@param eventName string
|
||||
function Events.registerEvent(eventName)
|
||||
assert(registeredEvents[eventName] == nil, ('Event %s is already registered.'):format(eventName))
|
||||
|
||||
registeredEvents[eventName] = {}
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback Events.callback
|
||||
function Events.subscribe(eventName, callback)
|
||||
local callbacks = registeredEvents[eventName]
|
||||
assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName))
|
||||
|
||||
table.insert(callbacks, callback)
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param ... any
|
||||
function Events.call(eventName, ...)
|
||||
local callbacks = registeredEvents[eventName]
|
||||
assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName))
|
||||
|
||||
for _, func in ipairs(callbacks) do
|
||||
func(eventName, ...)
|
||||
end
|
||||
end
|
||||
@@ -1,2 +0,0 @@
|
||||
name = smc__api__events
|
||||
depends = smc__api__table
|
||||
@@ -1,17 +0,0 @@
|
||||
Logger = {}
|
||||
|
||||
---@param text string
|
||||
---@param ... any
|
||||
function Logger.infoLog(text, ...)
|
||||
text = core.colorize('#00BFFF', '[SMC] '..text:format(...))
|
||||
|
||||
core.log('info', text)
|
||||
end
|
||||
|
||||
---@param text string
|
||||
---@param ... any
|
||||
function Logger.warningLog(text, ...)
|
||||
text = core.colorize('#00BFFF', '[SMC] '..text:format(...))
|
||||
|
||||
core.log('warning', text)
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__logger
|
||||
@@ -1,19 +0,0 @@
|
||||
local math = math
|
||||
|
||||
---Limiting the value by minimum and maximum value.
|
||||
---@param value number The value to be limited.
|
||||
---@param min number Minimum limit.
|
||||
---@param max number Maximum limit.
|
||||
math.clamp = function(value, min, max)
|
||||
return math.min(math.max(value, min), max)
|
||||
end
|
||||
|
||||
---Linear interpolation.
|
||||
---@param a number Minimum value.
|
||||
---@param b number Maximum value.
|
||||
---@param t number Number in range [0, 1].
|
||||
math.lerp = function (a, b, t)
|
||||
t = math.clamp(t, 0, 1)
|
||||
|
||||
return a + (b - a) * t
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__math
|
||||
@@ -1,67 +0,0 @@
|
||||
---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 = {}
|
||||
|
||||
---Returns the mod name without prefixes
|
||||
---@param name string
|
||||
---@return string
|
||||
local function getShortModName(name)
|
||||
local shortName = string.gsub(name, '%w*__', '')
|
||||
|
||||
return shortName
|
||||
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}
|
||||
function Mod.getInfo(modname)
|
||||
local name = modname or core.get_current_modname()
|
||||
local shortName = getShortModName(name)
|
||||
local path = core.get_modpath(name)
|
||||
local require = Require.getModRequire(name, path)
|
||||
|
||||
return {name = name, shortName = shortName, path = path, require = require}
|
||||
end
|
||||
|
||||
--[[
|
||||
function Mod:new()
|
||||
---@type Mod
|
||||
local instance = setmetatable(Mod.getInfo(), { __index = self })
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Get an instance of a public class provided by a mod
|
||||
---@param ... any?
|
||||
---@return table
|
||||
function Mod:getModClassInstance(...)
|
||||
local classInstance = self.Class:new(...)
|
||||
|
||||
return classInstance
|
||||
end
|
||||
|
||||
---Inherits a child class from a parent class
|
||||
---@param ParentClass table
|
||||
---@param ChildClass table?
|
||||
---@return table
|
||||
function Mod:getClassExtended(ParentClass, ChildClass)
|
||||
local childClass = setmetatable(ChildClass or {}, { __index = ParentClass })
|
||||
|
||||
return childClass
|
||||
end
|
||||
|
||||
---Inherits a child class from a parent public class provided by the mod.
|
||||
---@param ChildClass table?
|
||||
---@return table
|
||||
function Mod:getModClassExtended(ChildClass)
|
||||
local childClass = Mod:getClassExtended(self.Class, ChildClass or {})
|
||||
|
||||
return childClass
|
||||
end --]]
|
||||
|
||||
return Mod
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__mod
|
||||
@@ -1,38 +0,0 @@
|
||||
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
|
||||
local function require(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')
|
||||
|
||||
if result ~= nil then
|
||||
loadedModules[fullName] = result
|
||||
else
|
||||
loadedModules[fullName] = true
|
||||
end
|
||||
|
||||
return loadedModules[fullName]
|
||||
end
|
||||
|
||||
return require
|
||||
end
|
||||
|
||||
return Require
|
||||
@@ -1 +0,0 @@
|
||||
name = smc__api__require
|
||||
@@ -1,132 +0,0 @@
|
||||
local sub, upper, ensureArgType
|
||||
= string_utf8.sub, string_utf8.upper, Ensure.argType
|
||||
|
||||
--- Writes a string with a capital letter.
|
||||
--- @param str string Input string.
|
||||
--- @return string?
|
||||
function string.capitalize(str)
|
||||
ensureArgType(str, "string", 1, "string.capitalize")
|
||||
|
||||
local first_char = sub(str,1, 1)
|
||||
|
||||
if not first_char then
|
||||
return
|
||||
end
|
||||
|
||||
first_char = upper(first_char)
|
||||
|
||||
return first_char..sub(str,2, -1)
|
||||
end
|
||||
|
||||
--- Capitalizes each word in a line.
|
||||
--- @param str string Input string.
|
||||
--- @return string?
|
||||
function string.to_title(str)
|
||||
ensureArgType(str, "string", 1, "string.to_title")
|
||||
|
||||
local result = ""
|
||||
|
||||
for _, word in ipairs(str:split(" ")) do
|
||||
result = string.format("%s%s ", result, word:capitalize())
|
||||
end
|
||||
|
||||
return result:trim()
|
||||
end
|
||||
|
||||
--- Replaces underscores with spaces in a string.
|
||||
--- @param str string Input string.
|
||||
--- @return string
|
||||
function string.remove_underscores(str)
|
||||
ensureArgType(str, "string", 1, "string.remove_underscores")
|
||||
local output = str:gsub("_", " ")
|
||||
|
||||
return output
|
||||
end
|
||||
|
||||
--- Checks if a string starts with `prefix`.
|
||||
--- @param str string String to check.
|
||||
--- @param prefix string The string the `str` should start with.
|
||||
--- @return boolean
|
||||
function string.starts_with(str, prefix)
|
||||
ensureArgType(str, "string", 1, "string.starts_with")
|
||||
ensureArgType(prefix, "string", 2, "string.starts_with")
|
||||
|
||||
return str:sub(1, #prefix) == prefix
|
||||
end
|
||||
|
||||
|
||||
--- Checks if a string end with `prefix`.
|
||||
--- @param str string String to check.
|
||||
--- @param suffix string The string the `str` should end with.
|
||||
--- @return boolean
|
||||
function string.ends_with(str, suffix)
|
||||
ensureArgType(str, "string", 1, "string.ends_with")
|
||||
ensureArgType(suffix, "string", 2, "string.ends_with")
|
||||
|
||||
return str:sub(-#suffix, -1) == suffix
|
||||
end
|
||||
|
||||
--- Checks if the string contains a specific substring.
|
||||
--- @param str string String to check.
|
||||
--- @param sub_string string The text segment to search for within the string.
|
||||
--- @return boolean
|
||||
function string.contains(str, sub_string)
|
||||
ensureArgType(str, "string", 1, "string.contains")
|
||||
ensureArgType(sub_string, "string", 2, "string.contains")
|
||||
|
||||
return str:find(sub_string, 1, true) ~= nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- EMMYLUA ANNOTATIONS OVERWRITES --
|
||||
|
||||
--- Returns a copy of `s` in which all (or the first `n`, if given)
|
||||
--- occurrences of the `pattern` have been replaced by a replacement string
|
||||
--- specified by `repl`, which can be a string, a table, or a function. `gsub`
|
||||
--- also returns, as its second value, the total number of matches that
|
||||
--- occurred.
|
||||
---
|
||||
--- If `repl` is a string, then its value is used for replacement. The character
|
||||
--- `%` works as an escape character: any sequence in `repl` of the form `%n`,
|
||||
--- with *n* between 1 and 9, stands for the value of the *n*-th captured
|
||||
--- substring (see below). The sequence `%0` stands for the whole match. The
|
||||
--- sequence `%%` stands for a single `%`.
|
||||
---
|
||||
--- If `repl` is a table, then the table is queried for every match, using
|
||||
--- the first capture as the key; if the pattern specifies no captures, then
|
||||
--- the whole match is used as the key.
|
||||
---
|
||||
--- If `repl` is a function, then this function is called every time a match
|
||||
--- occurs, with all captured substrings passed as arguments, in order; if
|
||||
--- the pattern specifies no captures, then the whole match is passed as a
|
||||
--- sole argument.
|
||||
---
|
||||
--- If the value returned by the table query or by the function call is a
|
||||
--- string or a number, then it is used as the replacement string; otherwise,
|
||||
--- if it is false or nil, then there is no replacement (that is, the original
|
||||
--- match is kept in the string).
|
||||
---
|
||||
--- Here are some examples:
|
||||
--- `x = string.gsub("hello world", "(%w+)", "%1 %1")`
|
||||
--- `-- > x="hello hello world world"`
|
||||
--- `x = string.gsub("hello world", "%w+", "%0 %0", 1)`
|
||||
--- `-- > x="hello hello world"`
|
||||
--- `x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")`
|
||||
--- `-- > x="world hello Lua from"`
|
||||
--- `x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)`
|
||||
--- `-- > x="home = /home/roberto, user = roberto"`
|
||||
--- `x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)`
|
||||
--- >> return loadstring(s)()
|
||||
--- > end)
|
||||
--- `-- > x="4+5 = 9"`
|
||||
--- `local t = {name="lua", version="5.3"}`
|
||||
--- `x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)`
|
||||
--- > x="lua-5.3.tar.gz"
|
||||
-- -@overload fun(s:string, pattern:string, repl:string|table|fun()):string, number
|
||||
-- -@param s string
|
||||
-- -@param pattern string
|
||||
-- -@param repl string|table|fun(param:string)
|
||||
-- -@param n? number
|
||||
-- -@return string, number
|
||||
-- function string.gsub(s, pattern, repl, n) end
|
||||
@@ -1,2 +0,0 @@
|
||||
name = smc__api__string
|
||||
depends = smc__api__ensure
|
||||
@@ -1,360 +0,0 @@
|
||||
local tableCopy, tableKeyOf, pairs, ipairs, next, type, assert, ensureArgType, ensureArgNotNil
|
||||
= table.copy, table.keyof, pairs, ipairs, next, type, assert, Ensure.argType, Ensure.argNotNil
|
||||
|
||||
-- While extending Lua's table library, we maintain snake_case naming for standard library
|
||||
-- functions and arguments to match standard library conventions. The code that is not exposed
|
||||
-- to the outer scope continues to use our camelCase convention.
|
||||
|
||||
--- Returns the number of key-value pairs in the table `t`.
|
||||
--- For array-like tables, prefer the # operator.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @return number
|
||||
function table.get_size(t)
|
||||
ensureArgType(t, 'table', 1, 'table.get_size')
|
||||
|
||||
local size = 0
|
||||
for _ in pairs(t) do
|
||||
size = size + 1
|
||||
end
|
||||
|
||||
return size
|
||||
end
|
||||
|
||||
--- Returns all keys from the table `t` as an array.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @return string[] | number[]
|
||||
function table.get_keys(t)
|
||||
ensureArgType(t, 'table', 1, 'table.get_keys')
|
||||
|
||||
local keys = {}
|
||||
for key in pairs(t) do
|
||||
keys[#keys+1] = key
|
||||
end
|
||||
|
||||
return keys
|
||||
end
|
||||
|
||||
--- Returns an array of all `t` keys corresponding to the specified value.
|
||||
--- @param t table<string, any> | any[] Table to check
|
||||
--- @param value string | number Value to compare against
|
||||
--- @return any[]?
|
||||
function table.get_keys_by_value(t, value)
|
||||
ensureArgType(t, 'table', 1, 'table.get_keys_by_value')
|
||||
|
||||
local found = {}
|
||||
for k, v in pairs(t) do
|
||||
if v == value then
|
||||
found[#found+1] = k
|
||||
end
|
||||
end
|
||||
|
||||
return #found > 0 and found or nil
|
||||
end
|
||||
|
||||
--- Checks if the table `t` contains the specified value. Returns `true` if the key is found at least once, `false` otherwise.
|
||||
--- @param t table<string, any> | any[] Table to check
|
||||
--- @param value any Value to compare against
|
||||
--- @return boolean
|
||||
function table.contains(t, value)
|
||||
ensureArgType(t, 'table', 1, 'table.contains')
|
||||
return tableKeyOf(t, value) ~= nil
|
||||
end
|
||||
|
||||
local tableContains = table.contains
|
||||
|
||||
--- Checks if the table `t` contains the specified key. Returns `true` if the key is found at least once, `false` otherwise.
|
||||
--- Use when you need to check for key, avoiding metamethods intervention.
|
||||
--- @param t table<any, any> | any[] Table to check
|
||||
--- @param key any Value to compare against
|
||||
--- @return boolean
|
||||
function table.has_key(t, key)
|
||||
ensureArgType(t, 'table', 1, 'table.has_key')
|
||||
ensureArgNotNil(key, 2, 'table.has_key')
|
||||
|
||||
for k in pairs(t) do
|
||||
if k == key then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
--- Checks whether all `t` elements are equal to the specified value. Returns `true` if all elements match, `false` otherwise.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @param value any Value to compare against
|
||||
--- @return boolean
|
||||
function table.all_equal_to(t, value)
|
||||
ensureArgType(t, 'table', 1, 'table.all_equal_to')
|
||||
ensureArgNotNil(value, 2, 'table.all_equal_to')
|
||||
|
||||
for _, v in pairs(t) do
|
||||
if v ~= value then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Returns a table with only specified key-value pairs of the table `t`.
|
||||
--- @param t table<string, any> | any[] Original table
|
||||
--- @param keys string[] | number[] Array of keys
|
||||
--- @return table<string, any> | any[]
|
||||
function table.pick(t, keys)
|
||||
ensureArgType(t, 'table', 1, 'table.pick')
|
||||
ensureArgType(keys, 'table', 2, 'table.pick')
|
||||
|
||||
local result = {}
|
||||
for _, k in ipairs(keys) do
|
||||
result[k] = t[k]
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Returns a `t` table copy with specified key-value pairs omitted.
|
||||
--- @param t table<string, any> | any[] Original table
|
||||
--- @param keys string[] | number[] Array of keys
|
||||
--- @return table<string, any> | any[]
|
||||
function table.omit(t, keys)
|
||||
ensureArgType(t, 'table', 1, 'table.omit')
|
||||
ensureArgType(keys, 'table', 2, 'table.omit')
|
||||
|
||||
local result = tableCopy(t)
|
||||
for _, k in ipairs(keys) do
|
||||
result[k] = nil
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Helper function that merges 'source' into 'target' in-place
|
||||
--- Don't expose it because YOU SHOULD NEVER WRITE CODE WHICH MODIFIES TABLES IN-PLACE.
|
||||
--- In this case it's okay, because it's used only once and on the copy of a table.
|
||||
--- @param target table<string, any> | any[] Target table to merge into
|
||||
--- @param source table<string, any> | any[] Source table to merge from
|
||||
--- @param recursively? boolean Enable recursive merging (default: false)
|
||||
local function mergeInPlace(target, source, recursively)
|
||||
for key, value in pairs(source) do
|
||||
local targetValue = target[key]
|
||||
local valueType = type(value)
|
||||
|
||||
if targetValue == nil then
|
||||
-- Add new key with deep copy if needed
|
||||
target[key] = (valueType == 'table') and tableCopy(value) or value
|
||||
elseif recursively then
|
||||
local targetValueType = type(targetValue)
|
||||
if targetValueType == 'table' and valueType == 'table' then
|
||||
-- Recursively merge into existing table
|
||||
mergeInPlace(targetValue, value, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Merges entries from `t2` into a deep copy of `t1`, skipping existing keys.
|
||||
--- @param t1 table<string, any> | any[] Target table to merge into
|
||||
--- @param t2 table<string, any> | any[] Source table to merge from
|
||||
--- @param recursively? boolean Enable recursive merging (default: false)
|
||||
--- @return table<string, any> | any[]
|
||||
function table.merge(t1, t2, recursively)
|
||||
ensureArgType(t1, 'table', 1, 'table.merge')
|
||||
ensureArgType(t2, 'table', 2, 'table.merge')
|
||||
|
||||
local result = tableCopy(t1)
|
||||
mergeInPlace(result, t2, recursively == true)
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Returns an array containing only unique values of the table `t`.
|
||||
--- @param t any[] Array to check
|
||||
--- @return any[]
|
||||
function table.unique_values(t)
|
||||
ensureArgType(t, 'table', 1, 'table.unique_values')
|
||||
|
||||
local seen = {}
|
||||
local result = {}
|
||||
for i = 1, #t do
|
||||
local value = t[i]
|
||||
if not seen[value] then
|
||||
seen[value] = true
|
||||
result[#result + 1] = value
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Checks if table contains no elements. Returns `true` if table is empty, `false` otherwise.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @return boolean
|
||||
function table.is_empty(t)
|
||||
ensureArgType(t, 'table', 1, 'table.is_empty')
|
||||
|
||||
return next(t) == nil
|
||||
end
|
||||
|
||||
--- Checks if table contains any of specified keys. Returns `true` if any key exists, `false` otherwise.
|
||||
--- @param t table<string, any> Table to check
|
||||
--- @param keys string[] Array of keys to look for
|
||||
--- @return boolean
|
||||
function table.has_any_key_from(t, keys)
|
||||
ensureArgType(t, 'table', 1, 'table.has_any_key_from')
|
||||
ensureArgType(keys, 'table', 2, 'table.has_any_key_from')
|
||||
|
||||
for key in pairs(t) do
|
||||
if tableContains(keys, key) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
--- Deep comparison of two tables. Returns `true` if tables are deeply equal, `false` otherwise.
|
||||
--- @param t1 table<string, any> | any[] First table
|
||||
--- @param t2 table<string, any> | any[] Second table
|
||||
--- @return boolean
|
||||
function table.deep_equal(t1, t2)
|
||||
ensureArgType(t1, 'table', 1, 'table.deep_equal')
|
||||
ensureArgType(t2, 'table', 2, 'table.deep_equal')
|
||||
|
||||
-- Check t1's entries
|
||||
for k, v1 in pairs(t1) do
|
||||
local v2 = t2[k]
|
||||
if type(v1) == 'table' and type(v2) == 'table' then
|
||||
if not table.deep_equal(v1, v2) then
|
||||
return false
|
||||
end
|
||||
else
|
||||
if v2 ~= v1 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Check for extra keys in t2
|
||||
for k in pairs(t2) do
|
||||
if t1[k] == nil then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Transforms each value in `t` using a callback.
|
||||
--- @param t table<string, any> | any[] Input table
|
||||
--- @param callback fun(key:string | number, value:any):any Transformation callback
|
||||
--- @return table
|
||||
function table.map(t, callback)
|
||||
ensureArgType(t, 'table', 1, 'table.map')
|
||||
ensureArgType(callback, 'function', 2, 'table.map')
|
||||
|
||||
local result = tableCopy(t)
|
||||
for key, value in pairs(t) do
|
||||
result[key] = callback(key, value)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
local tableMap = table.map
|
||||
|
||||
--- Applies multipliers to corresponding values in the table `t`. Returns a table with resulting values.
|
||||
--- @param t table<string, number> Input values
|
||||
--- @param multiplier_table table<string, number> Multipliers
|
||||
--- @return table<string, number>
|
||||
function table.multiply_values_with(t, multiplier_table)
|
||||
ensureArgType(t, 'table', 1, 'table.multiply_values_with')
|
||||
ensureArgType(multiplier_table, 'table', 2, 'table.multiply_values_with')
|
||||
|
||||
return tableMap(t, function(key, value)
|
||||
return multiplier_table[key] and value * multiplier_table[key] or value
|
||||
end)
|
||||
end
|
||||
|
||||
--- Iterates over each key-value pair in `t`. For every entry calls `callback` with key and value as arguments.
|
||||
--- @param t table<string, any> | any[] Table to iterate through.
|
||||
--- @param callback fun(key:string | number, value:any) Callback applied to each key-value pair.
|
||||
function table.for_each(t, callback)
|
||||
ensureArgType(t, 'table', 1, 'table.for_each')
|
||||
ensureArgType(callback, 'function', 2, 'table.for_each')
|
||||
|
||||
for key, value in pairs(t) do
|
||||
callback(key, value)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Internal helper function for performing arithmetic operations between two tables. Returns resulting table with resulting values.
|
||||
--- Works almost like `table.map`, but while modyfying a copy of `t1` (which is the same in `table.map`),
|
||||
--- iterates through `t2` and therefore uses its key-value pairs.
|
||||
--- @param t1 table<string, number> | number[] Base table to modify
|
||||
--- @param t2 table<string, number> | number[] Table with values to operate with
|
||||
--- @param emptyValue number Default value for missing keys in `t1`
|
||||
--- @param operation fun(a:number, b:number):number Operation function to apply
|
||||
--- @return table<string, number> | number[]
|
||||
local function operateValues(t1, t2, emptyValue, operation)
|
||||
local result = tableCopy(t1)
|
||||
for key, value in pairs(t2) do
|
||||
local a = t1[key] or emptyValue
|
||||
result[key] = operation(a, value)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Adds values with identical keys. Uses `t2` keys. If `t1` lacks a key, uses `empty_value`.
|
||||
--- @param t1 table<string, number> | number[] Base table to modify
|
||||
--- @param t2 table<string, number> | number[] Table with values to add
|
||||
--- @param empty_value? number Default value for missing keys in t1 (default: 0)
|
||||
--- @return table<string, number> | number[]
|
||||
function table.add_values(t1, t2, empty_value)
|
||||
ensureArgType(t1, 'table', 1, 'table.add_values')
|
||||
ensureArgType(t2, 'table', 2, 'table.add_values')
|
||||
|
||||
return operateValues(t1, t2, empty_value or 0, function(a, b) return a + b end)
|
||||
end
|
||||
|
||||
--- Subtracts t2 values from t1 values with identical keys. Uses `t2` keys. If `t1` lacks a key, uses `empty_value`.
|
||||
--- @param t1 table<string, number> | number[] Base table to modify
|
||||
--- @param t2 table<string, number> | number[] Table with values to add
|
||||
--- @param empty_value? number Default value for missing keys in `t1` (default: 0)
|
||||
--- @return table<string, number> | number[]
|
||||
function table.subtract_values(t1, t2, empty_value)
|
||||
ensureArgType(t1, 'table', 1, 'table.subtract_values')
|
||||
ensureArgType(t2, 'table', 2, 'table.subtract_values')
|
||||
|
||||
return operateValues(t1, t2, empty_value or 0, function(a, b) return a - b end)
|
||||
end
|
||||
|
||||
--- Multiplies values with identical keys. Uses `t2` keys. If `t1` lacks a key, uses `empty_value`.
|
||||
--- @param t1 table<string, number> | number[] Base table to modify
|
||||
--- @param t2 table<string, number> | number[] Table with values to add
|
||||
--- @param empty_value? number Default value for missing keys in `t1` (default: 1)
|
||||
--- @return table<string, number> | number[]
|
||||
function table.multiply_values(t1, t2, empty_value)
|
||||
ensureArgType(t1, 'table', 1, 'table.multiply_values')
|
||||
ensureArgType(t2, 'table', 2, 'table.multiply_values')
|
||||
|
||||
return operateValues(t1, t2, empty_value or 1, function(a, b) return a * b end)
|
||||
end
|
||||
|
||||
--- Divides `t1` values by `t2` values with identical keys. Uses `t2` keys. If `t1` lacks a key, uses `empty_value`.
|
||||
--- @param t1 table<string, number> | number[] Base table to modify
|
||||
--- @param t2 table<string, number> | number[] Table with values to add
|
||||
--- @param empty_value? number Default value for missing keys in `t1` (default: 1)
|
||||
--- @return table<string, number> | number[]
|
||||
function table.divide_values(t1, t2, empty_value)
|
||||
ensureArgType(t1, 'table', 1, 'table.add_values')
|
||||
ensureArgType(t2, 'table', 2, 'table.divide_values')
|
||||
|
||||
local operation = function(a, b)
|
||||
assert(b ~= 0, 'Division by zero in table.div_values')
|
||||
return a / b
|
||||
end
|
||||
return operateValues(t1, t2, empty_value or 1, operation)
|
||||
end
|
||||
@@ -1,2 +0,0 @@
|
||||
name = smc__api__table
|
||||
depends = smc__api__ensure
|
||||
Submodule mods/API/Utf8 deleted from d612fc127f
0
mods/API/Utf8/init.lua
Normal file
0
mods/API/Utf8/init.lua
Normal file
@@ -1 +0,0 @@
|
||||
name = smc__api
|
||||
Reference in New Issue
Block a user