From d1305795dec74edaad36e046a5941dc241490f8b Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 30 May 2019 19:28:28 +0200 Subject: [PATCH] Ensure tables are compared by value for 1 level instead of reference --- resources/setting_types.lua | 35 +++++++++++++++++++++++++++++++++++ utils/redmew_settings.lua | 5 +++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/resources/setting_types.lua b/resources/setting_types.lua index 3d6e4a3d..f5ced641 100644 --- a/resources/setting_types.lua +++ b/resources/setting_types.lua @@ -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) diff --git a/utils/redmew_settings.lua b/utils/redmew_settings.lua index 1ea83821..6723e446 100644 --- a/utils/redmew_settings.lua +++ b/utils/redmew_settings.lua @@ -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