mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 03:25:23 +09:00
Add Clusterio Plugins
This commit is contained in:
147
exp_util/module/include/color.lua
Normal file
147
exp_util/module/include/color.lua
Normal file
@@ -0,0 +1,147 @@
|
||||
-- source: https://www.rapidtables.com/web/color/RGB_Color.html
|
||||
return {
|
||||
maroon = {r = 128, g = 0, b = 0},
|
||||
dark_red = {r = 139, g = 0, b = 0},
|
||||
brown = {r = 165, g = 42, b = 42},
|
||||
firebrick = {r = 178, g = 34, b = 34},
|
||||
crimson = {r = 220, g = 20, b = 60},
|
||||
red = {r = 255, g = 0, b = 0},
|
||||
tomato = {r = 255, g = 99, b = 71},
|
||||
coral = {r = 255, g = 127, b = 80},
|
||||
indian_red = {r = 205, g = 92, b = 92},
|
||||
light_coral = {r = 240, g = 128, b = 128},
|
||||
dark_salmon = {r = 233, g = 150, b = 122},
|
||||
salmon = {r = 250, g = 128, b = 114},
|
||||
light_salmon = {r = 255, g = 160, b = 122},
|
||||
orange_red = {r = 255, g = 69, b = 0},
|
||||
dark_orange = {r = 255, g = 140, b = 0},
|
||||
orange = {r = 255, g = 165, b = 0},
|
||||
gold = {r = 255, g = 215, b = 0},
|
||||
dark_golden_rod = {r = 184, g = 134, b = 11},
|
||||
golden_rod = {r = 218, g = 165, b = 32},
|
||||
pale_golden_rod = {r = 238, g = 232, b = 170},
|
||||
dark_khaki = {r = 189, g = 183, b = 107},
|
||||
khaki = {r = 240, g = 230, b = 140},
|
||||
olive = {r = 128, g = 128, b = 0},
|
||||
yellow = {r = 255, g = 255, b = 0},
|
||||
yellow_green = {r = 154, g = 205, b = 50},
|
||||
dark_olive_green = {r = 85, g = 107, b = 47},
|
||||
olive_drab = {r = 107, g = 142, b = 35},
|
||||
lawn_green = {r = 124, g = 252, b = 0},
|
||||
chart_reuse = {r = 127, g = 255, b = 0},
|
||||
green_yellow = {r = 173, g = 255, b = 47},
|
||||
dark_green = {r = 0, g = 100, b = 0},
|
||||
green = {r = 0, g = 128, b = 0},
|
||||
forest_green = {r = 34, g = 139, b = 34},
|
||||
lime = {r = 0, g = 255, b = 0},
|
||||
lime_green = {r = 50, g = 205, b = 50},
|
||||
light_green = {r = 144, g = 238, b = 144},
|
||||
pale_green = {r = 152, g = 251, b = 152},
|
||||
dark_sea_green = {r = 143, g = 188, b = 143},
|
||||
medium_spring_green = {r = 0, g = 250, b = 154},
|
||||
spring_green = {r = 0, g = 255, b = 127},
|
||||
sea_green = {r = 46, g = 139, b = 87},
|
||||
medium_aqua_marine = {r = 102, g = 205, b = 170},
|
||||
medium_sea_green = {r = 60, g = 179, b = 113},
|
||||
light_sea_green = {r = 32, g = 178, b = 170},
|
||||
dark_slate_gray = {r = 47, g = 79, b = 79},
|
||||
teal = {r = 0, g = 128, b = 128},
|
||||
dark_cyan = {r = 0, g = 139, b = 139},
|
||||
aqua = {r = 0, g = 255, b = 255},
|
||||
cyan = {r = 0, g = 255, b = 255},
|
||||
light_cyan = {r = 224, g = 255, b = 255},
|
||||
dark_turquoise = {r = 0, g = 206, b = 209},
|
||||
turquoise = {r = 64, g = 224, b = 208},
|
||||
medium_turquoise = {r = 72, g = 209, b = 204},
|
||||
pale_turquoise = {r = 175, g = 238, b = 238},
|
||||
aqua_marine = {r = 127, g = 255, b = 212},
|
||||
powder_blue = {r = 176, g = 224, b = 230},
|
||||
cadet_blue = {r = 95, g = 158, b = 160},
|
||||
steel_blue = {r = 70, g = 130, b = 180},
|
||||
corn_flower_blue = {r = 100, g = 149, b = 237},
|
||||
deep_sky_blue = {r = 0, g = 191, b = 255},
|
||||
dodger_blue = {r = 30, g = 144, b = 255},
|
||||
light_blue = {r = 173, g = 216, b = 230},
|
||||
sky_blue = {r = 135, g = 206, b = 235},
|
||||
light_sky_blue = {r = 135, g = 206, b = 250},
|
||||
midnight_blue = {r = 25, g = 25, b = 112},
|
||||
navy = {r = 0, g = 0, b = 128},
|
||||
dark_blue = {r = 0, g = 0, b = 139},
|
||||
medium_blue = {r = 0, g = 0, b = 205},
|
||||
blue = {r = 0, g = 0, b = 255},
|
||||
royal_blue = {r = 65, g = 105, b = 225},
|
||||
blue_violet = {r = 138, g = 43, b = 226},
|
||||
indigo = {r = 75, g = 0, b = 130},
|
||||
dark_slate_blue = {r = 72, g = 61, b = 139},
|
||||
slate_blue = {r = 106, g = 90, b = 205},
|
||||
medium_slate_blue = {r = 123, g = 104, b = 238},
|
||||
medium_purple = {r = 147, g = 112, b = 219},
|
||||
dark_magenta = {r = 139, g = 0, b = 139},
|
||||
dark_violet = {r = 148, g = 0, b = 211},
|
||||
dark_orchid = {r = 153, g = 50, b = 204},
|
||||
medium_orchid = {r = 186, g = 85, b = 211},
|
||||
purple = {r = 128, g = 0, b = 128},
|
||||
thistle = {r = 216, g = 191, b = 216},
|
||||
plum = {r = 221, g = 160, b = 221},
|
||||
violet = {r = 238, g = 130, b = 238},
|
||||
magenta = {r = 255, g = 0, b = 255},
|
||||
fuchsia = {r = 255, g = 0, b = 255},
|
||||
orchid = {r = 218, g = 112, b = 214},
|
||||
medium_violet_red = {r = 199, g = 21, b = 133},
|
||||
pale_violet_red = {r = 219, g = 112, b = 147},
|
||||
deep_pink = {r = 255, g = 20, b = 147},
|
||||
hot_pink = {r = 255, g = 105, b = 180},
|
||||
light_pink = {r = 255, g = 182, b = 193},
|
||||
pink = {r = 255, g = 192, b = 203},
|
||||
antique_white = {r = 250, g = 235, b = 215},
|
||||
beige = {r = 245, g = 245, b = 220},
|
||||
bisque = {r = 255, g = 228, b = 196},
|
||||
blanched_almond = {r = 255, g = 235, b = 205},
|
||||
wheat = {r = 245, g = 222, b = 179},
|
||||
corn_silk = {r = 255, g = 248, b = 220},
|
||||
lemon_chiffon = {r = 255, g = 250, b = 205},
|
||||
light_golden_rod_yellow = {r = 250, g = 250, b = 210},
|
||||
light_yellow = {r = 255, g = 255, b = 224},
|
||||
saddle_brown = {r = 139, g = 69, b = 19},
|
||||
sienna = {r = 160, g = 82, b = 45},
|
||||
chocolate = {r = 210, g = 105, b = 30},
|
||||
peru = {r = 205, g = 133, b = 63},
|
||||
sandy_brown = {r = 244, g = 164, b = 96},
|
||||
burly_wood = {r = 222, g = 184, b = 135},
|
||||
tan = {r = 210, g = 180, b = 140},
|
||||
rosy_brown = {r = 188, g = 143, b = 143},
|
||||
moccasin = {r = 255, g = 228, b = 181},
|
||||
navajo_white = {r = 255, g = 222, b = 173},
|
||||
peach_puff = {r = 255, g = 218, b = 185},
|
||||
misty_rose = {r = 255, g = 228, b = 225},
|
||||
lavender_blush = {r = 255, g = 240, b = 245},
|
||||
linen = {r = 250, g = 240, b = 230},
|
||||
old_lace = {r = 253, g = 245, b = 230},
|
||||
papaya_whip = {r = 255, g = 239, b = 213},
|
||||
sea_shell = {r = 255, g = 245, b = 238},
|
||||
mint_cream = {r = 245, g = 255, b = 250},
|
||||
slate_gray = {r = 112, g = 128, b = 144},
|
||||
light_slate_gray = {r = 119, g = 136, b = 153},
|
||||
light_steel_blue = {r = 176, g = 196, b = 222},
|
||||
lavender = {r = 230, g = 230, b = 250},
|
||||
floral_white = {r = 255, g = 250, b = 240},
|
||||
alice_blue = {r = 240, g = 248, b = 255},
|
||||
ghost_white = {r = 248, g = 248, b = 255},
|
||||
honeydew = {r = 240, g = 255, b = 240},
|
||||
ivory = {r = 255, g = 255, b = 240},
|
||||
azure = {r = 240, g = 255, b = 255},
|
||||
snow = {r = 255, g = 250, b = 250},
|
||||
black = {r = 0, g = 0, b = 0},
|
||||
silver = {r = 192, g = 192, b = 192},
|
||||
dim_grey = {r = 105, g = 105, b = 105},
|
||||
grey = {r = 128, g = 128, b = 128},
|
||||
dark_grey = {r = 169, g = 169, b = 169},
|
||||
light_grey = {r = 211, g = 211, b = 211},
|
||||
gainsboro = {r = 220, g = 220, b = 220},
|
||||
white_smoke = {r = 245, g = 245, b = 245},
|
||||
white = {r = 255, g = 255, b = 255},
|
||||
success = {r = 0, g = 255, b = 0},
|
||||
warning = {r = 255, g = 255, b = 0},
|
||||
fail = {r = 255, g = 0, b = 0},
|
||||
info = {r = 255, g = 255, b = 255}
|
||||
}
|
||||
342
exp_util/module/include/inspect.lua
Normal file
342
exp_util/module/include/inspect.lua
Normal file
@@ -0,0 +1,342 @@
|
||||
local inspect = {
|
||||
_VERSION = 'inspect.lua 3.1.0',
|
||||
_URL = 'http://github.com/kikito/inspect.lua',
|
||||
_DESCRIPTION = 'human-readable representations of tables',
|
||||
_LICENSE = [[
|
||||
MIT LICENSE
|
||||
|
||||
Copyright (c) 2013 Enrique García Cota
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
]]
|
||||
}
|
||||
|
||||
local tostring = tostring
|
||||
|
||||
inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
|
||||
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
|
||||
|
||||
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
||||
-- Otherwise, it returns a regular quoted string
|
||||
local function smartQuote(str)
|
||||
if str:match('"') and not str:match("'") then
|
||||
return "'" .. str .. "'"
|
||||
end
|
||||
return '"' .. str:gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- \a => '\\a', \0 => '\\0', 31 => '\31'
|
||||
local shortControlCharEscapes = {
|
||||
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
|
||||
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v"
|
||||
}
|
||||
local longControlCharEscapes = {} -- \a => nil, \0 => \000, 31 => \031
|
||||
for i=0, 31 do
|
||||
local ch = string.char(i)
|
||||
if not shortControlCharEscapes[ch] then
|
||||
shortControlCharEscapes[ch] = "\\"..i
|
||||
longControlCharEscapes[ch] = string.format("\\%03d", i)
|
||||
end
|
||||
end
|
||||
|
||||
local function escape(str)
|
||||
return (str:gsub("\\", "\\\\")
|
||||
:gsub("(%c)%f[0-9]", longControlCharEscapes)
|
||||
:gsub("%c", shortControlCharEscapes))
|
||||
end
|
||||
|
||||
local function isIdentifier(str)
|
||||
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
|
||||
end
|
||||
|
||||
local function isSequenceKey(k, sequenceLength)
|
||||
return type(k) == 'number'
|
||||
and 1 <= k
|
||||
and k <= sequenceLength
|
||||
and math.floor(k) == k
|
||||
end
|
||||
|
||||
local defaultTypeOrders = {
|
||||
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
|
||||
['function'] = 5, ['userdata'] = 6, ['thread'] = 7
|
||||
}
|
||||
|
||||
local function sortKeys(a, b)
|
||||
local ta, tb = type(a), type(b)
|
||||
|
||||
-- strings and numbers are sorted numerically/alphabetically
|
||||
if ta == tb and (ta == 'string' or ta == 'number') then return a < b end
|
||||
|
||||
local dta, dtb = defaultTypeOrders[ta], defaultTypeOrders[tb]
|
||||
-- Two default types are compared according to the defaultTypeOrders table
|
||||
if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb]
|
||||
elseif dta then return true -- default types before custom ones
|
||||
elseif dtb then return false -- custom types after default ones
|
||||
end
|
||||
|
||||
-- custom types are sorted out alphabetically
|
||||
return ta < tb
|
||||
end
|
||||
|
||||
-- For implementation reasons, the behavior of rawlen & # is "undefined" when
|
||||
-- tables aren't pure sequences. So we implement our own # operator.
|
||||
local function getSequenceLength(t)
|
||||
local len = 1
|
||||
local v = rawget(t, len)
|
||||
while v ~= nil do
|
||||
len = len + 1
|
||||
v = rawget(t, len)
|
||||
end
|
||||
return len - 1
|
||||
end
|
||||
|
||||
local function getNonSequentialKeys(t)
|
||||
local keys = {}
|
||||
local sequenceLength = getSequenceLength(t)
|
||||
for k, _ in pairs(t) do
|
||||
if not isSequenceKey(k, sequenceLength) then table.insert(keys, k) end
|
||||
end
|
||||
table.sort(keys, sortKeys)
|
||||
return keys, sequenceLength
|
||||
end
|
||||
|
||||
local function getToStringResultSafely(t, mt)
|
||||
local __tostring = type(mt) == 'table' and rawget(mt, '__tostring')
|
||||
local str, ok
|
||||
if type(__tostring) == 'function' then
|
||||
ok, str = pcall(__tostring, t)
|
||||
str = ok and str or 'error: ' .. tostring(str)
|
||||
end
|
||||
if type(str) == 'string' and #str > 0 then return str end
|
||||
end
|
||||
|
||||
local function countTableAppearances(t, tableAppearances)
|
||||
tableAppearances = tableAppearances or {}
|
||||
|
||||
if type(t) == 'table' then
|
||||
if not tableAppearances[t] then
|
||||
tableAppearances[t] = 1
|
||||
for k, v in pairs(t) do
|
||||
countTableAppearances(k, tableAppearances)
|
||||
countTableAppearances(v, tableAppearances)
|
||||
end
|
||||
countTableAppearances(getmetatable(t), tableAppearances)
|
||||
else
|
||||
tableAppearances[t] = tableAppearances[t] + 1
|
||||
end
|
||||
end
|
||||
|
||||
return tableAppearances
|
||||
end
|
||||
|
||||
local copySequence = function(s)
|
||||
local copy, len = {}, #s
|
||||
for i=1, len do copy[i] = s[i] end
|
||||
return copy, len
|
||||
end
|
||||
|
||||
local function makePath(path, ...)
|
||||
local keys = {...}
|
||||
local newPath, len = copySequence(path)
|
||||
for i=1, #keys do
|
||||
newPath[len + i] = keys[i]
|
||||
end
|
||||
return newPath
|
||||
end
|
||||
|
||||
-- Cooldude2606: Modified this to respect the depth option
|
||||
local function processRecursive(process, item, path, visited, depth)
|
||||
|
||||
if item == nil then return nil end
|
||||
if visited[item] then return visited[item] end
|
||||
|
||||
local processed = process(item, path)
|
||||
if type(processed) == 'table' and (depth == nil or depth > 0) then
|
||||
local processedCopy = {}
|
||||
visited[item] = processedCopy
|
||||
local processedKey
|
||||
|
||||
for k, v in pairs(processed) do
|
||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited, depth and depth - 1)
|
||||
if processedKey ~= nil then
|
||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited, depth and depth - 1)
|
||||
end
|
||||
end
|
||||
|
||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited, depth and depth - 1)
|
||||
setmetatable(processedCopy, mt)
|
||||
processed = processedCopy
|
||||
end
|
||||
return processed
|
||||
end
|
||||
|
||||
|
||||
|
||||
-------------------------------------------------------------------
|
||||
|
||||
local Inspector = {}
|
||||
local Inspector_mt = {__index = Inspector}
|
||||
|
||||
function Inspector:puts(...)
|
||||
local args = {...}
|
||||
local buffer = self.buffer
|
||||
local len = #buffer
|
||||
for i=1, #args do
|
||||
len = len + 1
|
||||
buffer[len] = args[i]
|
||||
end
|
||||
end
|
||||
|
||||
function Inspector:down(f)
|
||||
self.level = self.level + 1
|
||||
f()
|
||||
self.level = self.level - 1
|
||||
end
|
||||
|
||||
function Inspector:tabify()
|
||||
self:puts(self.newline, string.rep(self.indent, self.level))
|
||||
end
|
||||
|
||||
function Inspector:alreadyVisited(v)
|
||||
return self.ids[v] ~= nil
|
||||
end
|
||||
|
||||
function Inspector:getId(v)
|
||||
local id = self.ids[v]
|
||||
if not id then
|
||||
local tv = type(v)
|
||||
id = (self.maxIds[tv] or 0) + 1
|
||||
self.maxIds[tv] = id
|
||||
self.ids[v] = id
|
||||
end
|
||||
return tostring(id)
|
||||
end
|
||||
|
||||
function Inspector:putKey(k)
|
||||
if isIdentifier(k) then return self:puts(k) end
|
||||
self:puts("[")
|
||||
self:putValue(k)
|
||||
self:puts("]")
|
||||
end
|
||||
|
||||
function Inspector:putTable(t)
|
||||
if t == inspect.KEY or t == inspect.METATABLE then
|
||||
self:puts(tostring(t))
|
||||
elseif self:alreadyVisited(t) then
|
||||
self:puts('<table ', self:getId(t), '>')
|
||||
elseif self.level >= self.depth then
|
||||
self:puts('{...}')
|
||||
else
|
||||
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
|
||||
|
||||
local nonSequentialKeys, sequenceLength = getNonSequentialKeys(t)
|
||||
local mt = getmetatable(t)
|
||||
local toStringResult = getToStringResultSafely(t, mt)
|
||||
|
||||
self:puts('{')
|
||||
self:down(function()
|
||||
if toStringResult then
|
||||
self:puts(' -- ', escape(toStringResult))
|
||||
if sequenceLength >= 1 then self:tabify() end
|
||||
end
|
||||
|
||||
local count = 0
|
||||
for i=1, sequenceLength do
|
||||
if count > 0 then self:puts(', ') end
|
||||
self:puts(' ')
|
||||
self:putValue(t[i])
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
for _, k in ipairs(nonSequentialKeys) do
|
||||
if count > 0 then self:puts(', ') end
|
||||
self:tabify()
|
||||
self:putKey(k)
|
||||
self:puts(' = ')
|
||||
self:putValue(t[k])
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
if mt then
|
||||
if count > 0 then self:puts(', ') end
|
||||
self:tabify()
|
||||
self:puts('<metatable> = ')
|
||||
self:putValue(mt)
|
||||
end
|
||||
end)
|
||||
|
||||
if #nonSequentialKeys > 0 or mt then -- result is multi-lined. Justify closing }
|
||||
self:tabify()
|
||||
elseif sequenceLength > 0 then -- array tables have one extra space before closing }
|
||||
self:puts(' ')
|
||||
end
|
||||
|
||||
self:puts('}')
|
||||
end
|
||||
end
|
||||
|
||||
function Inspector:putValue(v)
|
||||
local tv = type(v)
|
||||
|
||||
if tv == 'string' then
|
||||
self:puts(smartQuote(escape(v)))
|
||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
||||
tv == 'cdata' or tv == 'ctype' then
|
||||
self:puts(tostring(v))
|
||||
elseif tv == 'table' then
|
||||
self:putTable(v)
|
||||
else
|
||||
self:puts('<', tv, ' ', self:getId(v), '>')
|
||||
end
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------
|
||||
|
||||
function inspect.inspect(root, options)
|
||||
options = options or {}
|
||||
|
||||
local depth = options.depth or math.huge
|
||||
local newline = options.newline or '\n'
|
||||
local indent = options.indent or ' '
|
||||
local process = options.process
|
||||
|
||||
if process then
|
||||
root = processRecursive(process, root, {}, {})
|
||||
end
|
||||
|
||||
local inspector = setmetatable({
|
||||
depth = depth,
|
||||
level = 0,
|
||||
buffer = {},
|
||||
ids = {},
|
||||
maxIds = {},
|
||||
newline = newline,
|
||||
indent = indent,
|
||||
tableAppearances = countTableAppearances(root)
|
||||
}, Inspector_mt)
|
||||
|
||||
inspector:putValue(root)
|
||||
|
||||
return table.concat(inspector.buffer)
|
||||
end
|
||||
|
||||
setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end })
|
||||
|
||||
return inspect
|
||||
64
exp_util/module/include/math.lua
Normal file
64
exp_util/module/include/math.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
--luacheck:ignore global math
|
||||
|
||||
local floor = math.floor
|
||||
local abs = math.abs
|
||||
|
||||
--- Constant value representing the square root of 2
|
||||
math.sqrt2 = math.sqrt(2)
|
||||
|
||||
--- Constant value representing the reciprocal of square root of 2
|
||||
math.inv_sqrt2 = 1 / math.sqrt2
|
||||
|
||||
--- Constant value representing the value of Tau aka 2*Pi
|
||||
math.tau = 2 * math.pi
|
||||
|
||||
--- Rounds a number to certain number of decimal places, does not work on significant figures
|
||||
-- @tparam number num The number to be rounded
|
||||
-- @tparam[opt=0] number idp The number of decimal places to round to
|
||||
-- @treturn number The input number rounded to the given number of decimal places
|
||||
math.round = function(num, idp)
|
||||
local mult = 10 ^ (idp or 0)
|
||||
return floor(num * mult + 0.5) / mult
|
||||
end
|
||||
|
||||
--- Clamp a number better a minimum and maximum value, preserves NaN (not the same as nil)
|
||||
-- @tparam number num The number to be clamped
|
||||
-- @tparam number min The lower bound of the accepted range
|
||||
-- @tparam number max The upper bound of the accepted range
|
||||
-- @treturn number The input number clamped to the given range
|
||||
math.clamp = function(num, min, max)
|
||||
if num < min then
|
||||
return min
|
||||
elseif num > max then
|
||||
return max
|
||||
else
|
||||
return num
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns the slope / gradient of a line given two points on the line
|
||||
-- @tparam number x1 The X coordinate of the first point on the line
|
||||
-- @tparam number y1 The Y coordinate of the first point on the line
|
||||
-- @tparam number x2 The X coordinate of the second point on the line
|
||||
-- @tparam number y2 The Y coordinate of the second point on the line
|
||||
-- @treturn number The slope of the line
|
||||
math.slope = function(x1, y1, x2, y2)
|
||||
return abs((y2 - y1) / (x2 - x1))
|
||||
end
|
||||
|
||||
--- Returns the y-intercept of a line given ibe point on the line and its slope
|
||||
-- @tparam number x The X coordinate of point on the line
|
||||
-- @tparam number y The Y coordinate of point on the line
|
||||
-- @tparam number slope The slope / gradient of the line
|
||||
-- @treturn number The y-intercept of the line
|
||||
math.y_intercept = function(x, y, slope)
|
||||
return y - (slope * x)
|
||||
end
|
||||
|
||||
local deg_to_rad = math.tau / 360
|
||||
--- Returns the angle x (given in radians) in degrees
|
||||
math.degrees = function(x)
|
||||
return x * deg_to_rad
|
||||
end
|
||||
|
||||
return math
|
||||
33
exp_util/module/include/package.lua
Normal file
33
exp_util/module/include/package.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
--luacheck:ignore global package
|
||||
|
||||
local Clustorio = require("modules/clusterio/api")
|
||||
|
||||
--- Enum values for the different lifecycle stages within a factorio module
|
||||
-- Info on the data lifecycle and how we use it: https://lua-api.factorio.com/latest/auxiliary/data-lifecycle.html
|
||||
-- We start in control stage and so values 1 thorough 3 are only present for completeness
|
||||
package.lifecycle_stage = {
|
||||
settings = 1,
|
||||
data = 2,
|
||||
migration = 3,
|
||||
control = 4,
|
||||
init = 5,
|
||||
load = 6,
|
||||
config_change = 7,
|
||||
runtime = 8
|
||||
}
|
||||
|
||||
--- Stores the current lifecycle stage we are in, compare values against package.lifecycle_stage
|
||||
package.lifecycle = package.lifecycle_stage.control
|
||||
|
||||
return setmetatable({
|
||||
on_init = function() package.lifecycle = package.lifecycle_stage.init end,
|
||||
on_load = function() package.lifecycle = package.lifecycle_stage.load end,
|
||||
on_configuration_changed = function() package.lifecycle = package.lifecycle_stage.config_change end,
|
||||
events = {
|
||||
-- TODO find a reliable way to set to runtime because currently it will desync if accessed before player joined
|
||||
[defines.events.on_player_joined_game] = function() package.lifecycle = package.lifecycle_stage.runtime end,
|
||||
[Clustorio.events.on_server_startup] = function() package.lifecycle = package.lifecycle_stage.runtime end,
|
||||
}
|
||||
}, {
|
||||
__index = package
|
||||
})
|
||||
23
exp_util/module/include/require.lua
Normal file
23
exp_util/module/include/require.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
--luacheck:ignore global require
|
||||
|
||||
local package = require 'modules.exp_util.include.package'
|
||||
local loaded = package.loaded
|
||||
local _require = require
|
||||
|
||||
-- This replace function is used to avoid additional lines in stack traces during control stage
|
||||
local function replace()
|
||||
require = function(path)
|
||||
if package.lifecycle == package.lifecycle_stage.runtime then
|
||||
return loaded[path] or loaded[path:gsub(".", "/")] or error('Can only require files at runtime that have been required in the control stage.', 2)
|
||||
else
|
||||
return _require(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return setmetatable({
|
||||
on_init = replace,
|
||||
on_load = replace,
|
||||
}, {
|
||||
__call = _require
|
||||
})
|
||||
427
exp_util/module/include/table.lua
Normal file
427
exp_util/module/include/table.lua
Normal file
@@ -0,0 +1,427 @@
|
||||
--luacheck:ignore global table
|
||||
|
||||
local random = math.random
|
||||
local floor = math.floor
|
||||
local remove = table.remove
|
||||
local tonumber = tonumber
|
||||
local pairs = pairs
|
||||
local table_size = table_size
|
||||
|
||||
--- Adds all keys of the source table to destination table as a shallow copy
|
||||
-- @tparam table dst The table to insert into
|
||||
-- @tparam table src The table to insert from
|
||||
function table.merge(dst, src)
|
||||
local dst_len = #dst
|
||||
for k, v in pairs(src) do
|
||||
if tonumber(k) then
|
||||
dst_len = dst_len + 1
|
||||
dst[dst_len] = v
|
||||
else
|
||||
dst[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[-- Much faster method for inserting items into an array
|
||||
@tparam table tbl the table that will have the values added to it
|
||||
@tparam[opt] number start_index the index at which values will be added, nil means end of the array
|
||||
@tparam table values the new values that will be added to the table
|
||||
@treturn table the table that was passed as the first argument
|
||||
@usage-- Adding 1000 values into the middle of the array
|
||||
local tbl = {}
|
||||
local values = {}
|
||||
for i = 1, 1000 do tbl[i] = i values[i] = i end
|
||||
table.array_insert(tbl, 500, values) -- around 0.4ms
|
||||
]]
|
||||
function table.array_insert(tbl, start_index, values)
|
||||
if not values then
|
||||
values = start_index
|
||||
start_index = nil
|
||||
end
|
||||
|
||||
if start_index then
|
||||
local starting_length = #tbl
|
||||
local adding_length = #values
|
||||
local move_to = start_index+adding_length+1
|
||||
for offset = starting_length-start_index, 0, -1 do
|
||||
tbl[move_to+offset] = tbl[starting_length+offset]
|
||||
end
|
||||
start_index = start_index-1
|
||||
else
|
||||
start_index = #tbl
|
||||
end
|
||||
|
||||
for offset, item in ipairs(values) do
|
||||
tbl[start_index+offset] = item
|
||||
end
|
||||
|
||||
return tbl
|
||||
end
|
||||
|
||||
--[[-- Much faster method for inserting keys into a table
|
||||
@tparam table tbl the table that will have keys added to it
|
||||
@tparam[opt] number start_index the index at which values will be added, nil means end of the array, numbered indexs only
|
||||
@tparam table tbl2 the table that may contain both string and numbered keys
|
||||
@treturn table the table passed as the first argument
|
||||
@usage-- Merging two tables
|
||||
local tbl = {}
|
||||
local tbl2 = {}
|
||||
for i = 1, 100 do tbl[i] = i tbl['_'..i] = i tbl2[i] = i tbl2['__'..i] = i end
|
||||
table.table_insert(tbl, 50, tbl2)
|
||||
]]
|
||||
function table.table_insert(tbl, start_index, tbl2)
|
||||
if not tbl2 then
|
||||
tbl2 = start_index
|
||||
start_index = nil
|
||||
end
|
||||
|
||||
table.array_insert(tbl, start_index, tbl2)
|
||||
for key, value in pairs(tbl2) do
|
||||
if not tonumber(key) then
|
||||
tbl[key] = value
|
||||
end
|
||||
end
|
||||
|
||||
return tbl
|
||||
end
|
||||
|
||||
--- Searches an array to remove a specific element without an index
|
||||
-- @tparam table tbl The array to remove the element from
|
||||
-- @param element The element to search for
|
||||
function table.remove_element(tbl, element)
|
||||
for k, v in pairs(tbl) do
|
||||
if v == element then
|
||||
remove(tbl, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Removes an item from an array in O(1) time. Does not guarantee the order of elements.
|
||||
-- @tparam table tbl The array to remove the element from
|
||||
-- @tparam number index Must be >= 0. The case where index > #tbl is handled.
|
||||
function table.remove_index(tbl, index)
|
||||
local count = #tbl
|
||||
if index > count then
|
||||
return
|
||||
end
|
||||
|
||||
tbl[index] = tbl[count]
|
||||
tbl[count] = nil
|
||||
end
|
||||
|
||||
--- Removes an item from an array in O(1) time. Does not guarantee the order of elements.
|
||||
-- @tparam table tbl The array to remove the element from
|
||||
-- @tparam number index Must be >= 0. The case where index > #tbl is handled.
|
||||
table.fast_remove = table.remove_index
|
||||
|
||||
--- Return the key which holds this element element
|
||||
-- @tparam table tbl The table to search
|
||||
-- @param element The element to find
|
||||
-- @return The key of the element or nil
|
||||
function table.get_key(tbl, element)
|
||||
for k, v in pairs(tbl) do
|
||||
if v == element then
|
||||
return k
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Checks if the arrayed portion of a table contains an element
|
||||
-- @tparam table tbl The table to search
|
||||
-- @param element The element to find
|
||||
-- @treturn ?number The index of the element or nil
|
||||
function table.get_index(tbl, element)
|
||||
for i = 1, #tbl do
|
||||
if tbl[i] == element then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Checks if a table contains an element
|
||||
-- @tparam table tbl The table to search
|
||||
-- @param e The element to find
|
||||
-- @treturn boolean True if the element was found
|
||||
function table.contains(tbl, element)
|
||||
return table.get_key(tbl, element) and true or false
|
||||
end
|
||||
|
||||
--- Checks if the arrayed portion of a table contains an element
|
||||
-- @tparam table tbl The table to search
|
||||
-- @param e The element to find
|
||||
-- @treturn boolean True if the element was found
|
||||
function table.array_contains(tbl, element)
|
||||
return table.get_index(tbl, element) and true or false
|
||||
end
|
||||
|
||||
--[[-- Extracts certain keys from a table, similar to deconstruction in other languages
|
||||
@tparam table tbl table the which contains the keys
|
||||
@tparam string ... the names of the keys you want extracted
|
||||
@return the keys in the order given
|
||||
@usage -- Deconstruction of a required module
|
||||
local format_number, distance = table.deconstruct(require('util'), 'format_number', 'distance')
|
||||
]]
|
||||
function table.deconstruct(tbl, ...)
|
||||
local values = {}
|
||||
for _, key in pairs({...}) do
|
||||
table.insert(values, tbl[key])
|
||||
end
|
||||
return table.unpack(values)
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a table, can only be used during runtime
|
||||
-- @tparam table tbl The table to select from
|
||||
-- @tparam[opt=false] boolean key When true the key will be returned rather than the value
|
||||
-- @return The selected element from the table
|
||||
function table.get_random(tbl, key)
|
||||
local target_index = random(1, table_size(tbl))
|
||||
local count = 1
|
||||
for k, v in pairs(tbl) do
|
||||
if target_index == count then
|
||||
if key then
|
||||
return k
|
||||
else
|
||||
return v
|
||||
end
|
||||
end
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a weighted table, can only be used during runtime
|
||||
-- @tparam table weighted_table The table of items and their weights
|
||||
-- @param[opt=1] item_key The index / key of items within each element
|
||||
-- @param[opt=2] weight_key The index / key of the weights within each element
|
||||
-- @return The selected element from the table
|
||||
function table.get_random_weighted(weighted_table, item_key, weight_index)
|
||||
local total_weight = 0
|
||||
item_key = item_key or 1
|
||||
weight_index = weight_index or 2
|
||||
|
||||
for _, w in pairs(weighted_table) do
|
||||
total_weight = total_weight + w[weight_index]
|
||||
end
|
||||
|
||||
local index = random() * total_weight
|
||||
local weight_sum = 0
|
||||
for _, w in pairs(weighted_table) do
|
||||
weight_sum = weight_sum + w[weight_index]
|
||||
if weight_sum >= index then
|
||||
return w[item_key]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Clears all existing entries in a table
|
||||
-- @tparam table tbl The table to clear
|
||||
-- @tparam[opt=false] boolean array When true only the array portion of the table is cleared
|
||||
function table.clear(t, array)
|
||||
if array then
|
||||
for i = 1, #t do
|
||||
t[i] = nil
|
||||
end
|
||||
else
|
||||
for i in pairs(t) do
|
||||
t[i] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Creates a fisher-yates shuffle of a sequential number-indexed table
|
||||
-- because this uses math.random, it cannot be used outside of events if no rng is supplied
|
||||
-- from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items
|
||||
-- @tparam table tbl The table to shuffle
|
||||
-- @tparam[opt=math.random] function rng The function to provide random numbers
|
||||
function table.shuffle(t, rng)
|
||||
local rand = rng or math.random
|
||||
local iterations = #t
|
||||
if iterations == 0 then
|
||||
error('Not a sequential table')
|
||||
return
|
||||
end
|
||||
local j
|
||||
|
||||
for i = iterations, 2, -1 do
|
||||
j = rand(i)
|
||||
t[i], t[j] = t[j], t[i]
|
||||
end
|
||||
end
|
||||
|
||||
--- Default table comparator sort function.
|
||||
-- @local
|
||||
-- @param x one comparator operand
|
||||
-- @param y the other comparator operand
|
||||
-- @return true if x logically comes before y in a list, false otherwise
|
||||
local function sortFunc(x, y) --sorts tables with mixed index types.
|
||||
local tx = type(x)
|
||||
local ty = type(y)
|
||||
if tx == ty then
|
||||
if type(x) == 'string' then
|
||||
return string.lower(x) < string.lower(y)
|
||||
else
|
||||
return x < y
|
||||
end
|
||||
elseif tx == 'number' then
|
||||
return true --only x is a number and goes first
|
||||
else
|
||||
return false --only y is a number and goes first
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns a copy of all of the values in the table.
|
||||
-- @tparam table tbl the to copy the keys from, or an empty table if tbl is nil
|
||||
-- @tparam[opt] boolean sorted whether to sort the keys (slower) or keep the random order from pairs()
|
||||
-- @tparam[opt] boolean as_string whether to try and parse the values as strings, or leave them as their existing type
|
||||
-- @treturn array an array with a copy of all the values in the table
|
||||
function table.get_values(tbl, sorted, as_string)
|
||||
if not tbl then return {} end
|
||||
local valueset = {}
|
||||
local n = 0
|
||||
if as_string then --checking as_string /before/ looping is faster
|
||||
for _, v in pairs(tbl) do
|
||||
n = n + 1
|
||||
valueset[n] = tostring(v)
|
||||
end
|
||||
else
|
||||
for _, v in pairs(tbl) do
|
||||
n = n + 1
|
||||
valueset[n] = v
|
||||
end
|
||||
end
|
||||
if sorted then
|
||||
table.sort(valueset, sortFunc)
|
||||
end
|
||||
return valueset
|
||||
end
|
||||
|
||||
--- Returns a copy of all of the keys in the table.
|
||||
-- @tparam table tbl the to copy the keys from, or an empty table if tbl is nil
|
||||
-- @tparam[opt] boolean sorted whether to sort the keys (slower) or keep the random order from pairs()
|
||||
-- @tparam[opt] boolean as_string whether to try and parse the keys as strings, or leave them as their existing type
|
||||
-- @treturn array an array with a copy of all the keys in the table
|
||||
function table.get_keys(tbl, sorted, as_string)
|
||||
if not tbl then return {} end
|
||||
local keyset = {}
|
||||
local n = 0
|
||||
if as_string then --checking as_string /before/ looping is faster
|
||||
for k, _ in pairs(tbl) do
|
||||
n = n + 1
|
||||
keyset[n] = tostring(k)
|
||||
end
|
||||
else
|
||||
for k, _ in pairs(tbl) do
|
||||
n = n + 1
|
||||
keyset[n] = k
|
||||
end
|
||||
end
|
||||
if sorted then
|
||||
table.sort(keyset, sortFunc)
|
||||
end
|
||||
return keyset
|
||||
end
|
||||
|
||||
--- Returns the list is a sorted way that would be expected by people (this is by key)
|
||||
-- @tparam table tbl the table to be sorted
|
||||
-- @treturn table the sorted table
|
||||
function table.alphanum_sort(tbl)
|
||||
local o = table.get_keys(tbl)
|
||||
local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)")
|
||||
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end
|
||||
table.sort(o, function(a, b)
|
||||
return tostring(a):gsub("%.?%d+", padnum)..("%3d"):format(#b)
|
||||
< tostring(b):gsub("%.?%d+", padnum)..("%3d"):format(#a) end)
|
||||
local _tbl = {}
|
||||
for _, k in pairs(o) do _tbl[k] = tbl[k] end
|
||||
return _tbl
|
||||
end
|
||||
|
||||
--- Returns the list is a sorted way that would be expected by people (this is by key) (faster alternative than above)
|
||||
-- @tparam table tbl the table to be sorted
|
||||
-- @treturn table the sorted table
|
||||
function table.key_sort(tbl)
|
||||
local o = table.get_keys(tbl, true)
|
||||
local _tbl = {}
|
||||
for _, k in pairs(o) do _tbl[k] = tbl[k] end
|
||||
return _tbl
|
||||
end
|
||||
|
||||
--[[
|
||||
Returns the index where t[index] == target.
|
||||
If there is no such index, returns a negative value such that bit32.bnot(value) is
|
||||
the index that the value should be inserted to keep the list ordered.
|
||||
t must be a list in ascending order for the return value to be valid.
|
||||
|
||||
Usage example:
|
||||
local t = {1, 3,5, 7,9}
|
||||
local x = 5
|
||||
local index = table.binary_search(t, x)
|
||||
if index < 0 then
|
||||
game.print("value not found, smallest index where t[index] > x is: " .. bit32.bnot(index))
|
||||
else
|
||||
game.print("value found at index: " .. index)
|
||||
end
|
||||
]]
|
||||
function table.binary_search(t, target)
|
||||
--For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
|
||||
|
||||
local lower = 1
|
||||
local upper = #t
|
||||
|
||||
if upper == 0 then
|
||||
return -2 -- ~1
|
||||
end
|
||||
|
||||
repeat
|
||||
local mid = floor((lower + upper) * 0.5)
|
||||
local value = t[mid]
|
||||
if value == target then
|
||||
return mid
|
||||
elseif value < target then
|
||||
lower = mid + 1
|
||||
else
|
||||
upper = mid - 1
|
||||
end
|
||||
until lower > upper
|
||||
|
||||
return -1 - lower -- ~lower
|
||||
end
|
||||
|
||||
-- add table-related functions that exist in base factorio/util to the 'table' table
|
||||
require 'util'
|
||||
|
||||
--- Similar to serpent.block, returns a string with a pretty representation of a table.
|
||||
-- Notice: This method is not appropriate for saving/restoring tables. It is meant to be used by the programmer mainly while debugging a program.
|
||||
-- @tparam table root tTe table to serialize
|
||||
-- @tparam table options Options are depth, newline, indent, process
|
||||
-- depth sets the maximum depth that will be printed out. When the max depth is reached, inspect will stop parsing tables and just return {...}
|
||||
-- process is a function which allow altering the passed object before transforming it into a string.
|
||||
-- A typical way to use it would be to remove certain values so that they don't appear at all.
|
||||
-- return <string> the prettied table
|
||||
table.inspect = require 'modules.exp_util.include.inspect' --- @dep modules.exp_util.includes.inspect
|
||||
|
||||
--- Takes a table and returns the number of entries in the table. (Slower than #table, faster than iterating via pairs)
|
||||
table.size = table_size
|
||||
|
||||
--- Creates a deepcopy of a table. Metatables and LuaObjects inside the table are shallow copies.
|
||||
-- Shallow copies meaning it copies the reference to the object instead of the object itself.
|
||||
-- @tparam table object The object to copy
|
||||
-- @treturn table The copied object
|
||||
table.deep_copy = table.deepcopy -- added by util
|
||||
|
||||
--- Merges multiple tables. Tables later in the list will overwrite entries from tables earlier in the list.
|
||||
-- Ex. merge({{1, 2, 3}, {[2] = 0}, {[3] = 0}}) will return {1, 0, 0}
|
||||
-- @tparam table tables A table of tables to merge
|
||||
-- @treturn table The merged table
|
||||
table.deep_merge = util.merge
|
||||
|
||||
--- Determines if two tables are structurally equal.
|
||||
-- Notice: tables that are LuaObjects or contain LuaObjects won't be compared correctly, use == operator for LuaObjects
|
||||
-- @tparam table tbl1 The first table
|
||||
-- @tparam table tbl2 The second table
|
||||
-- @treturn boolean True if the tables are equal
|
||||
table.equals = table.compare -- added by util
|
||||
|
||||
return table
|
||||
Reference in New Issue
Block a user