From 70bd2bb1d3aec23fc052c9612f9e530c1a2de72d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 1 Oct 2018 17:17:08 -0400 Subject: [PATCH] ips_layer: Deduplicate resource usage --- src/common/hex_util.cpp | 2 +- src/common/hex_util.h | 2 +- src/core/file_sys/ips_layer.cpp | 48 +++++++++++++++-------------- src/core/file_sys/ips_layer.h | 14 +++++---- src/core/file_sys/patch_manager.cpp | 6 ++-- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index e516e5bbd..5b63f9e81 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -30,7 +30,7 @@ std::vector HexStringToVector(std::string_view str, bool little_endian) { return out; } -std::string HexVectorToString(std::vector vector, bool upper) { +std::string HexVectorToString(const std::vector& vector, bool upper) { std::string out; for (u8 c : vector) out += fmt::format(upper ? "{:02X}" : "{:02x}", c); diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 0b7d3592a..68f003cb6 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -30,7 +30,7 @@ std::array HexStringToArray(std::string_view str) { return out; } -std::string HexVectorToString(std::vector vector, bool upper = true); +std::string HexVectorToString(const std::vector& vector, bool upper = true); template std::string HexArrayToString(std::array array, bool upper = true) { diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index 6c5535f83..abe3dcf62 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -17,9 +17,10 @@ enum class IPSFileType { Error, }; -const std::map ESCAPE_CHARACTER_MAP{ - {"\\a", "\a"}, {"\\b", "\b"}, {"\\f", "\f"}, {"\\n", "\n"}, {"\\r", "\r"}, {"\\t", "\t"}, - {"\\v", "\v"}, {"\\\\", "\\"}, {"\\\'", "\'"}, {"\\\"", "\""}, {"\\\?", "\?"}, +constexpr std::array, 11> ESCAPE_CHARACTER_MAP{ + std::pair{"\\a", "\a"}, {"\\b", "\b"}, {"\\f", "\f"}, {"\\n", "\n"}, + {"\\r", "\r"}, {"\\t", "\t"}, {"\\v", "\v"}, {"\\\\", "\\"}, + {"\\\'", "\'"}, {"\\\"", "\""}, {"\\\?", "\?"}, }; static IPSFileType IdentifyMagic(const std::vector& magic) { @@ -92,12 +93,12 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { return std::make_shared(in_data, in->GetName(), in->GetContainingDirectory()); } -IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) - : valid(false), patch_text(std::move(patch_text_)), nso_build_id{}, is_little_endian(false), - offset_shift(0), print_values(false), last_comment("") { +IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) : patch_text(std::move(patch_text_)) { Parse(); } +IPSwitchCompiler::~IPSwitchCompiler() = default; + std::array IPSwitchCompiler::GetBuildID() const { return nso_build_id; } @@ -106,7 +107,7 @@ bool IPSwitchCompiler::IsValid() const { return valid; } -static bool StartsWith(const std::string& base, const std::string& check) { +static bool StartsWith(std::string_view base, std::string_view check) { return base.size() >= check.size() && base.substr(0, check.size()) == check; } @@ -128,21 +129,22 @@ void IPSwitchCompiler::Parse() { s.write(reinterpret_cast(bytes.data()), bytes.size()); std::vector lines; - std::string line; - while (std::getline(s, line)) { + std::string stream_line; + while (std::getline(s, stream_line)) { // Remove a trailing \r - if (!line.empty() && line[line.size() - 1] == '\r') - line = line.substr(0, line.size() - 1); - lines.push_back(line); + if (!stream_line.empty() && stream_line.back() == '\r') + stream_line.pop_back(); + lines.push_back(std::move(stream_line)); } for (std::size_t i = 0; i < lines.size(); ++i) { auto line = lines[i]; // Remove midline comments - if (!StartsWith(line, "//") && line.find("//") != std::string::npos) { - last_comment = line.substr(line.find("//") + 2); - line = line.substr(0, line.find("//")); + const auto comment_index = line.find("//"); + if (!StartsWith(line, "//") && comment_index != std::string::npos) { + last_comment = line.substr(comment_index + 2); + line = line.substr(0, comment_index); } if (StartsWith(line, "@stop")) { @@ -191,28 +193,28 @@ void IPSwitchCompiler::Parse() { while (true) { if (i + 1 >= lines.size()) break; - line = lines[++i]; + const auto patch_line = lines[++i]; // 11 - 8 hex digit offset + space + minimum two digit overwrite val - if (line.length() < 11) + if (patch_line.length() < 11) break; - auto offset = std::stoul(line.substr(0, 8), nullptr, 16); + auto offset = std::stoul(patch_line.substr(0, 8), nullptr, 16); offset += offset_shift; std::vector replace; // 9 - first char of replacement val - if (line[9] == '\"') { + if (patch_line[9] == '\"') { // string replacement - const auto end_index = line.find('\"', 10); + const auto end_index = patch_line.find('\"', 10); if (end_index == std::string::npos || end_index < 10) return; - auto value = line.substr(10, end_index - 10); + auto value = patch_line.substr(10, end_index - 10); value = EscapeStringSequences(value); replace.reserve(value.size()); std::copy(value.begin(), value.end(), std::back_inserter(replace)); } else { // hex replacement - const auto value = line.substr(9); + const auto value = patch_line.substr(9); replace.reserve(value.size() / 2); replace = Common::HexStringToVector(value, is_little_endian); } @@ -224,7 +226,7 @@ void IPSwitchCompiler::Parse() { patch_text->GetName(), offset, Common::HexVectorToString(replace)); } - patch.records.emplace(offset, replace); + patch.records.emplace(offset, std::move(replace)); } patches.push_back(std::move(patch)); diff --git a/src/core/file_sys/ips_layer.h b/src/core/file_sys/ips_layer.h index 847e9bf3c..b07cc5673 100644 --- a/src/core/file_sys/ips_layer.h +++ b/src/core/file_sys/ips_layer.h @@ -15,6 +15,8 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips); class IPSwitchCompiler { public: explicit IPSwitchCompiler(VirtualFile patch_text); + ~IPSwitchCompiler(); + std::array GetBuildID() const; bool IsValid() const; VirtualFile Apply(const VirtualFile& in) const; @@ -22,7 +24,7 @@ public: private: void Parse(); - bool valid; + bool valid = false; struct IPSwitchPatch { std::string name; @@ -32,11 +34,11 @@ private: VirtualFile patch_text; std::vector patches; - std::array nso_build_id; - bool is_little_endian; - s64 offset_shift; - bool print_values; - std::string last_comment; + std::array nso_build_id{}; + bool is_little_endian = false; + s64 offset_shift = 0; + bool print_values = false; + std::string last_comment = ""; }; } // namespace FileSys diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index f148e7719..03df24906 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -273,11 +273,13 @@ std::map> PatchManager::GetPatchVersionNam if (mod_dir != nullptr && mod_dir->GetSize() > 0) { for (const auto& mod : mod_dir->GetSubdirectories()) { std::string types; - if (IsDirValidAndNonEmpty(mod->GetSubdirectory("exefs"))) { + + const auto exefs_dir = mod->GetSubdirectory("exefs"); + if (IsDirValidAndNonEmpty(exefs_dir)) { bool ips = false; bool ipswitch = false; - for (const auto& file : mod->GetSubdirectory("exefs")->GetFiles()) { + for (const auto& file : exefs_dir->GetFiles()) { if (file->GetExtension() == "ips") ips = true; else if (file->GetExtension() == "pchtxt")