From f0c8f6d47b3d3a5a24ca36bc6e8f31502eff59f8 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sun, 19 Feb 2023 20:34:21 +0100 Subject: [PATCH] patch_manager: check config values before applying them --- Utilities/bin_patch.cpp | 32 +++++++++++++++++++++++++- Utilities/bin_patch.h | 2 ++ rpcs3/rpcs3qt/patch_manager_dialog.cpp | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Utilities/bin_patch.cpp b/Utilities/bin_patch.cpp index d3a7b9810f..9a0e66d2b8 100644 --- a/Utilities/bin_patch.cpp +++ b/Utilities/bin_patch.cpp @@ -82,6 +82,35 @@ void fmt_class_string::format(std::string& out, u64 arg) }); } +void patch_engine::patch_config_value::set_and_check_value(f64 new_value, const std::string& name) +{ + switch (type) + { + case patch_configurable_type::double_enum: + case patch_configurable_type::long_enum: + { + if (std::none_of(allowed_values.begin(), allowed_values.end(), [&new_value](const patch_allowed_value& allowed_value){ return allowed_value.value == new_value; })) + { + patch_log.error("Can't set configurable enumerated value '%s' to %f. Using default value %f", name, new_value, value); + return; + } + break; + } + case patch_configurable_type::double_range: + case patch_configurable_type::long_range: + { + if (new_value < min || new_value > max) + { + patch_log.error("Can't set configurable range value '%s' to %f. Using default value %f", name, new_value, value); + return; + } + break; + } + } + + value = new_value; +} + patch_engine::patch_engine() { } @@ -1249,7 +1278,8 @@ std::basic_string patch_engine::apply(const std::string& name, u8* dst, u32 { if (p_ptr->actual_config_values.contains(key)) { - ::at32(p_ptr->actual_config_values, key).value = config_value.value; + patch_config_value& actual_config_value = ::at32(p_ptr->actual_config_values, key); + actual_config_value.set_and_check_value(config_value.value, key); } } diff --git a/Utilities/bin_patch.h b/Utilities/bin_patch.h index 854a36e5c4..07a97a54c7 100644 --- a/Utilities/bin_patch.h +++ b/Utilities/bin_patch.h @@ -101,6 +101,8 @@ public: { return value == other.value && min == other.min && max == other.max && type == other.type && allowed_values == other.allowed_values; } + + void set_and_check_value(f64 new_value, const std::string& name); }; struct patch_config_values diff --git a/rpcs3/rpcs3qt/patch_manager_dialog.cpp b/rpcs3/rpcs3qt/patch_manager_dialog.cpp index da6c6b48eb..66abe2a2f0 100644 --- a/rpcs3/rpcs3qt/patch_manager_dialog.cpp +++ b/rpcs3/rpcs3qt/patch_manager_dialog.cpp @@ -340,7 +340,7 @@ void patch_manager_dialog::populate_tree() if (config_values.config_values.contains(key)) { - config_value.value = config_values.config_values.at(key).value; + config_value.set_and_check_value(config_values.config_values.at(key).value, key); } q_config_values[QString::fromStdString(key)] = QVariant::fromValue(config_value);