2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

ips_layer: Deduplicate resource usage

This commit is contained in:
Zach Hilman 2018-10-01 17:17:08 -04:00
parent 9669cdb710
commit 70bd2bb1d3
5 changed files with 39 additions and 33 deletions

View file

@ -30,7 +30,7 @@ std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) {
return out; return out;
} }
std::string HexVectorToString(std::vector<u8> vector, bool upper) { std::string HexVectorToString(const std::vector<u8>& vector, bool upper) {
std::string out; std::string out;
for (u8 c : vector) for (u8 c : vector)
out += fmt::format(upper ? "{:02X}" : "{:02x}", c); out += fmt::format(upper ? "{:02X}" : "{:02x}", c);

View file

@ -30,7 +30,7 @@ std::array<u8, Size> HexStringToArray(std::string_view str) {
return out; return out;
} }
std::string HexVectorToString(std::vector<u8> vector, bool upper = true); std::string HexVectorToString(const std::vector<u8>& vector, bool upper = true);
template <std::size_t Size> template <std::size_t Size>
std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {

View file

@ -17,9 +17,10 @@ enum class IPSFileType {
Error, Error,
}; };
const std::map<const char*, const char*> ESCAPE_CHARACTER_MAP{ constexpr std::array<std::pair<const char*, const char*>, 11> ESCAPE_CHARACTER_MAP{
{"\\a", "\a"}, {"\\b", "\b"}, {"\\f", "\f"}, {"\\n", "\n"}, {"\\r", "\r"}, {"\\t", "\t"}, std::pair{"\\a", "\a"}, {"\\b", "\b"}, {"\\f", "\f"}, {"\\n", "\n"},
{"\\v", "\v"}, {"\\\\", "\\"}, {"\\\'", "\'"}, {"\\\"", "\""}, {"\\\?", "\?"}, {"\\r", "\r"}, {"\\t", "\t"}, {"\\v", "\v"}, {"\\\\", "\\"},
{"\\\'", "\'"}, {"\\\"", "\""}, {"\\\?", "\?"},
}; };
static IPSFileType IdentifyMagic(const std::vector<u8>& magic) { static IPSFileType IdentifyMagic(const std::vector<u8>& magic) {
@ -92,12 +93,12 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory()); return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory());
} }
IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) : patch_text(std::move(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("") {
Parse(); Parse();
} }
IPSwitchCompiler::~IPSwitchCompiler() = default;
std::array<u8, 32> IPSwitchCompiler::GetBuildID() const { std::array<u8, 32> IPSwitchCompiler::GetBuildID() const {
return nso_build_id; return nso_build_id;
} }
@ -106,7 +107,7 @@ bool IPSwitchCompiler::IsValid() const {
return valid; 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; return base.size() >= check.size() && base.substr(0, check.size()) == check;
} }
@ -128,21 +129,22 @@ void IPSwitchCompiler::Parse() {
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size()); s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
std::vector<std::string> lines; std::vector<std::string> lines;
std::string line; std::string stream_line;
while (std::getline(s, line)) { while (std::getline(s, stream_line)) {
// Remove a trailing \r // Remove a trailing \r
if (!line.empty() && line[line.size() - 1] == '\r') if (!stream_line.empty() && stream_line.back() == '\r')
line = line.substr(0, line.size() - 1); stream_line.pop_back();
lines.push_back(line); lines.push_back(std::move(stream_line));
} }
for (std::size_t i = 0; i < lines.size(); ++i) { for (std::size_t i = 0; i < lines.size(); ++i) {
auto line = lines[i]; auto line = lines[i];
// Remove midline comments // Remove midline comments
if (!StartsWith(line, "//") && line.find("//") != std::string::npos) { const auto comment_index = line.find("//");
last_comment = line.substr(line.find("//") + 2); if (!StartsWith(line, "//") && comment_index != std::string::npos) {
line = line.substr(0, line.find("//")); last_comment = line.substr(comment_index + 2);
line = line.substr(0, comment_index);
} }
if (StartsWith(line, "@stop")) { if (StartsWith(line, "@stop")) {
@ -191,28 +193,28 @@ void IPSwitchCompiler::Parse() {
while (true) { while (true) {
if (i + 1 >= lines.size()) if (i + 1 >= lines.size())
break; break;
line = lines[++i]; const auto patch_line = lines[++i];
// 11 - 8 hex digit offset + space + minimum two digit overwrite val // 11 - 8 hex digit offset + space + minimum two digit overwrite val
if (line.length() < 11) if (patch_line.length() < 11)
break; 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; offset += offset_shift;
std::vector<u8> replace; std::vector<u8> replace;
// 9 - first char of replacement val // 9 - first char of replacement val
if (line[9] == '\"') { if (patch_line[9] == '\"') {
// string replacement // 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) if (end_index == std::string::npos || end_index < 10)
return; return;
auto value = line.substr(10, end_index - 10); auto value = patch_line.substr(10, end_index - 10);
value = EscapeStringSequences(value); value = EscapeStringSequences(value);
replace.reserve(value.size()); replace.reserve(value.size());
std::copy(value.begin(), value.end(), std::back_inserter(replace)); std::copy(value.begin(), value.end(), std::back_inserter(replace));
} else { } else {
// hex replacement // hex replacement
const auto value = line.substr(9); const auto value = patch_line.substr(9);
replace.reserve(value.size() / 2); replace.reserve(value.size() / 2);
replace = Common::HexStringToVector(value, is_little_endian); replace = Common::HexStringToVector(value, is_little_endian);
} }
@ -224,7 +226,7 @@ void IPSwitchCompiler::Parse() {
patch_text->GetName(), offset, Common::HexVectorToString(replace)); 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)); patches.push_back(std::move(patch));

View file

@ -15,6 +15,8 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips);
class IPSwitchCompiler { class IPSwitchCompiler {
public: public:
explicit IPSwitchCompiler(VirtualFile patch_text); explicit IPSwitchCompiler(VirtualFile patch_text);
~IPSwitchCompiler();
std::array<u8, 0x20> GetBuildID() const; std::array<u8, 0x20> GetBuildID() const;
bool IsValid() const; bool IsValid() const;
VirtualFile Apply(const VirtualFile& in) const; VirtualFile Apply(const VirtualFile& in) const;
@ -22,7 +24,7 @@ public:
private: private:
void Parse(); void Parse();
bool valid; bool valid = false;
struct IPSwitchPatch { struct IPSwitchPatch {
std::string name; std::string name;
@ -32,11 +34,11 @@ private:
VirtualFile patch_text; VirtualFile patch_text;
std::vector<IPSwitchPatch> patches; std::vector<IPSwitchPatch> patches;
std::array<u8, 0x20> nso_build_id; std::array<u8, 0x20> nso_build_id{};
bool is_little_endian; bool is_little_endian = false;
s64 offset_shift; s64 offset_shift = 0;
bool print_values; bool print_values = false;
std::string last_comment; std::string last_comment = "";
}; };
} // namespace FileSys } // namespace FileSys

View file

@ -273,11 +273,13 @@ std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNam
if (mod_dir != nullptr && mod_dir->GetSize() > 0) { if (mod_dir != nullptr && mod_dir->GetSize() > 0) {
for (const auto& mod : mod_dir->GetSubdirectories()) { for (const auto& mod : mod_dir->GetSubdirectories()) {
std::string types; std::string types;
if (IsDirValidAndNonEmpty(mod->GetSubdirectory("exefs"))) {
const auto exefs_dir = mod->GetSubdirectory("exefs");
if (IsDirValidAndNonEmpty(exefs_dir)) {
bool ips = false; bool ips = false;
bool ipswitch = false; bool ipswitch = false;
for (const auto& file : mod->GetSubdirectory("exefs")->GetFiles()) { for (const auto& file : exefs_dir->GetFiles()) {
if (file->GetExtension() == "ips") if (file->GetExtension() == "ips")
ips = true; ips = true;
else if (file->GetExtension() == "pchtxt") else if (file->GetExtension() == "pchtxt")