1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

Ensure tables are compared by value for 1 level instead of reference

This commit is contained in:
Lynn 2019-05-30 19:28:28 +02:00
parent caf8898353
commit d1305795de
2 changed files with 38 additions and 2 deletions

View File

@ -36,6 +36,36 @@ local function raw(input)
return input
end
local function equals_by_value(a, b)
return a == b
end
local function equals_by_table_values(a, b)
if type(a) ~= 'table' or type(b) ~= 'table' then
return a == b
end
if size(a) ~= size(b) then
return false
end
for index, value in pairs(a) do
local value_b = b[index]
if value_b == nil or value ~= value_b then
return false
end
end
for index, value in pairs(b) do
local value_a = a[index]
if value_a == nil or value ~= value_a then
return false
end
end
return true
end
local function color_to_scalar(input)
if type(input) ~= 'table' then
return ''
@ -118,6 +148,7 @@ end
--- to_string = takes stored input and converts it to its string representation
return {
fraction = {
equals = equals_by_value,
toScalar = raw,
sanitizer = function(input)
input = tonumber(input)
@ -138,6 +169,7 @@ return {
end
},
string = {
equals = equals_by_value,
toScalar = raw,
sanitizer = function(input)
if input == nil then
@ -157,6 +189,7 @@ return {
end
},
boolean = {
equals = equals_by_value,
toScalar = raw,
sanitizer = function(input)
local input_type = type(input)
@ -184,10 +217,12 @@ return {
end
},
color = {
equals = equals_by_table_values,
toScalar = color_to_scalar,
sanitizer = color_sanitizer
},
chat_color = {
equals = equals_by_table_values,
toScalar = color_to_scalar,
sanitizer = function(input)
local suc, value = color_sanitizer(input)

View File

@ -121,7 +121,8 @@ function Public.set(player_index, name, value)
setting = missing_setting
end
local success, sanitized_value = setting.data_transformation.sanitizer(value)
local data_transformation = setting.data_transformation
local success, sanitized_value = data_transformation.sanitizer(value)
if not success then
error(format('Setting "%s" failed: %s', name, sanitized_value), 2)
@ -141,7 +142,7 @@ function Public.set(player_index, name, value)
old_value = old_value,
new_value = sanitized_value,
player_index = player_index,
value_changed = old_value ~= sanitized_value
value_changed = not data_transformation.equals(old_value, sanitized_value)
})
return sanitized_value