improve diff-dol-files

This commit is contained in:
Martin Michelsen
2025-02-09 23:08:16 -08:00
parent 01e6c5a8fb
commit 405399682f
4 changed files with 32 additions and 13 deletions
+7 -1
View File
@@ -5,10 +5,16 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
struct DiffEntry {
uint32_t address;
std::string a_data;
std::string b_data;
};
inline void run_address_translator(const std::string&, const std::string&, const std::string&) { inline void run_address_translator(const std::string&, const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv"); throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
} }
inline std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string&, const std::string&) { inline std::vector<DiffEntry> diff_dol_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv"); throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
} }
+6 -5
View File
@@ -623,7 +623,7 @@ void run_address_translator(const std::string& directory, const std::string& use
} }
} }
vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const string& b_filename) { vector<DiffEntry> diff_dol_files(const string& a_filename, const string& b_filename) {
ResourceDASM::DOLFile a(a_filename.c_str()); ResourceDASM::DOLFile a(a_filename.c_str());
ResourceDASM::DOLFile b(b_filename.c_str()); ResourceDASM::DOLFile b(b_filename.c_str());
auto a_mem = make_shared<ResourceDASM::MemoryContext>(); auto a_mem = make_shared<ResourceDASM::MemoryContext>();
@@ -642,7 +642,7 @@ vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const st
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size()); max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
} }
vector<pair<uint32_t, string>> ret; vector<DiffEntry> ret;
for (uint32_t addr = min_addr; addr < max_addr; addr += 4) { for (uint32_t addr = min_addr; addr < max_addr; addr += 4) {
bool a_exists = a_mem->exists(addr, 4); bool a_exists = a_mem->exists(addr, 4);
bool b_exists = b_mem->exists(addr, 4); bool b_exists = b_mem->exists(addr, 4);
@@ -650,10 +650,11 @@ vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const st
string a_value = a_mem->read(addr, 4); string a_value = a_mem->read(addr, 4);
string b_value = b_mem->read(addr, 4); string b_value = b_mem->read(addr, 4);
if (a_value != b_value) { if (a_value != b_value) {
if (!ret.empty() && (ret.back().first + ret.back().second.size() == addr)) { if (!ret.empty() && (ret.back().address + ret.back().b_data.size() == addr)) {
ret.back().second += b_value; ret.back().a_data += a_value;
ret.back().b_data += b_value;
} else { } else {
ret.emplace_back(make_pair(addr, b_value)); ret.emplace_back(DiffEntry{.address = addr, .a_data = a_value, .b_data = b_value});
} }
} }
} }
+7 -1
View File
@@ -6,5 +6,11 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
struct DiffEntry {
uint32_t address;
std::string a_data;
std::string b_data;
};
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command); void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string& a_filename, const std::string& b_filename); std::vector<DiffEntry> diff_dol_files(const std::string& a_filename, const std::string& b_filename);
+8 -2
View File
@@ -2858,10 +2858,16 @@ Action a_diff_dol_files(
"diff-dol-files", nullptr, +[](phosg::Arguments& args) { "diff-dol-files", nullptr, +[](phosg::Arguments& args) {
const string& a_filename = args.get<string>(1); const string& a_filename = args.get<string>(1);
const string& b_filename = args.get<string>(2); const string& b_filename = args.get<string>(2);
bool show_pre = args.get<bool>("show-pre");
auto result = diff_dol_files(a_filename, b_filename); auto result = diff_dol_files(a_filename, b_filename);
for (const auto& it : result) { for (const auto& it : result) {
string data = phosg::format_data_string(it.second, nullptr, phosg::FormatDataFlags::HEX_ONLY); string b_str = phosg::format_data_string(it.b_data, nullptr, phosg::FormatDataFlags::HEX_ONLY);
fprintf(stdout, "%08" PRIX32 " %s\n", it.first, data.c_str()); if (show_pre) {
string a_str = phosg::format_data_string(it.a_data, nullptr, phosg::FormatDataFlags::HEX_ONLY);
fprintf(stdout, "%08" PRIX32 ": %s => %s\n", it.address, a_str.c_str(), b_str.c_str());
} else {
fprintf(stdout, "%08" PRIX32 " %s\n", it.address, b_str.c_str());
}
} }
}); });