From 1c7a3184136006942fb60598bab0d38f2b470940 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Fri, 19 Jun 2020 14:34:03 +0200 Subject: [PATCH] patch manager: move try catch block to yaml.cpp --- Utilities/bin_patch.cpp | 41 ++++++++++++++++++++--------------------- rpcs3/util/yaml.cpp | 19 +++++++++++++++++++ rpcs3/util/yaml.hpp | 4 ++++ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Utilities/bin_patch.cpp b/Utilities/bin_patch.cpp index 76276d8d1e..eee63d6c34 100644 --- a/Utilities/bin_patch.cpp +++ b/Utilities/bin_patch.cpp @@ -363,30 +363,29 @@ bool patch_engine::add_patch_data(YAML::Node node, patch_info& info, u32 modifie p_data.offset = addr_node.as(0) + modifier; p_data.original_value = value_node.IsScalar() ? value_node.Scalar() : ""; - // Use try/catch instead of YAML::Node::as(fallback) in order to get an error message - try + std::string error_message; + + switch (p_data.type) { - switch (p_data.type) - { - case patch_type::bef32: - case patch_type::lef32: - case patch_type::bef64: - case patch_type::lef64: - { - p_data.value.double_value = value_node.as(); - break; - } - default: - { - p_data.value.long_value = value_node.as(); - break; - } - } + case patch_type::bef32: + case patch_type::lef32: + case patch_type::bef64: + case patch_type::lef64: + { + p_data.value.double_value = get_yaml_node_value(value_node, error_message); + break; } - catch (const std::exception& e) + default: { - const std::string error_message = fmt::format("Skipping patch data entry: [ %s, 0x%.8x, %s ] (key: %s) %s", - p_data.type, p_data.offset, p_data.original_value.empty() ? "?" : p_data.original_value, info.hash, e.what()); + p_data.value.long_value = get_yaml_node_value(value_node, error_message); + break; + } + } + + if (!error_message.empty()) + { + error_message = fmt::format("Skipping patch data entry: [ %s, 0x%.8x, %s ] (key: %s) %s", + p_data.type, p_data.offset, p_data.original_value.empty() ? "?" : p_data.original_value, info.hash, error_message); append_log_message(log_messages, error_message); patch_log.error("%s", error_message); return false; diff --git a/rpcs3/util/yaml.cpp b/rpcs3/util/yaml.cpp index 71cfde00b7..dcafcdf650 100644 --- a/rpcs3/util/yaml.cpp +++ b/rpcs3/util/yaml.cpp @@ -1,4 +1,5 @@ #include "util/yaml.hpp" +#include "Utilities/types.h" std::pair yaml_load(const std::string& from) { @@ -15,3 +16,21 @@ std::pair yaml_load(const std::string& from) return{result, ""}; } + +template +T get_yaml_node_value(YAML::Node node, std::string& error_message) +{ + try + { + return node.as(); + } + catch (const std::exception& e) + { + error_message = e.what(); + } + + return {}; +} + +template u64 get_yaml_node_value(YAML::Node, std::string&); +template f64 get_yaml_node_value(YAML::Node, std::string&); diff --git a/rpcs3/util/yaml.hpp b/rpcs3/util/yaml.hpp index e36a1744cd..b910c62560 100644 --- a/rpcs3/util/yaml.hpp +++ b/rpcs3/util/yaml.hpp @@ -18,3 +18,7 @@ // Load from string and consume exception std::pair yaml_load(const std::string& from); + +// Use try/catch in YAML::Node::as() instead of YAML::Node::as(fallback) in order to get an error message +template +T get_yaml_node_value(YAML::Node node, std::string& error_message);