Files
MHR-Overlay/reframework/autorun/MHR_Overlay/Misc/table_helpers.lua
GreenComfyTea f0eb9673b0 Fix freeze_displayed_players on quest end
...if module visibility is disabled during the quest
2022-12-02 13:49:14 +02:00

94 lines
2.1 KiB
Lua

local table_helpers = {};
local next = next;
function table_helpers.deep_copy(original, copies)
copies = copies or {};
local original_type = type(original);
local copy;
if original_type == "table" then
if copies[original] then
copy = copies[original];
else
copy = {};
copies[original] = copy;
for original_key, original_value in next, original, nil do
copy[table_helpers.deep_copy(original_key, copies)] = table_helpers.deep_copy(original_value
,
copies);
end
setmetatable(copy,
table_helpers.deep_copy(getmetatable(original)
, copies));
end
else -- number, string, boolean, etc
copy = original;
end
return copy;
end
function table_helpers.find_index(table_, value, nullable)
for i = 1, #table_ do
if table_[i] == value then
return i;
end
end
if not nullable then
return 1;
end
return nil;
end
function table_helpers.merge(...)
local tables_to_merge = { ... };
assert(#tables_to_merge > 1, "There should be at least two tables to merge them");
for key, table_ in ipairs(tables_to_merge) do
assert(type(table_) == "table", string.format("Expected a table as function parameter %d", key));
end
local result = table_helpers.deep_copy(tables_to_merge[1]);
for i = 2, #tables_to_merge do
local from = tables_to_merge[i];
for key, value in pairs(from) do
if type(value) == "table" then
result[key] = result[key] or {};
assert(type(result[key]) == "table", string.format("Expected a table: '%s'", key));
result[key] = table_helpers.merge(result[key], value);
else
result[key] = value;
end
end
end
return result;
end
function table_helpers.tostring(table_)
if type(table_) == "table" then
local s = "{ \n";
for k, v in pairs(table_) do
if type(k) ~= "number" then
k = "\"" .. k .. "\"";
end
s = s .. "\t[" .. k .. "] = " .. table_helpers.tostring(v) .. ",\n";
end
return s .. "} \n";
else
return tostring(table_);
end
end
function table_helpers.is_empty(table_)
return next(table_) == nil;
end
function table_helpers.init_module()
end
return table_helpers;