From 27e95ee343a9b8fcbcfa2508a265db8166d874a8 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Thu, 8 Jun 2023 21:00:19 -0700 Subject: [PATCH] detect specific_version without using a patch --- src/FunctionCompiler.cc | 32 ++++++++++++++++++++++++++++++++ src/FunctionCompiler.hh | 2 ++ src/SendCommands.cc | 10 ++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/FunctionCompiler.cc b/src/FunctionCompiler.cc index e45f6671..85ac51f3 100644 --- a/src/FunctionCompiler.cc +++ b/src/FunctionCompiler.cc @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -347,3 +348,34 @@ DOLFileIndex::DOLFileIndex(const string& directory, bool compress) } } } + +uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum) { + static unordered_map checksum_to_specific_version; + if (checksum_to_specific_version.empty()) { + struct { + char system_code = 'G'; + char game_code1 = 'P'; + char game_code2; + char region_code; + char developer_code1 = '8'; + char developer_code2 = 'P'; + uint8_t disc_number = 0; + uint8_t version_code; + } __attribute__((packed)) data; + for (const char* game_code2 = "OS"; *game_code2; game_code2++) { + data.game_code2 = *game_code2; + for (const char* region_code = "JEP"; *region_code; region_code++) { + data.region_code = *region_code; + for (uint8_t version_code = 0; version_code < 8; version_code++) { + data.version_code = version_code; + uint32_t checksum = crc32(&data, sizeof(data)); + uint32_t specific_version = 0x33000030 | (*game_code2 << 16) | (*region_code << 8) | version_code; + if (!checksum_to_specific_version.emplace(checksum, specific_version).second) { + throw logic_error("multiple specific_versions have same header checksum"); + } + } + } + } + } + return checksum_to_specific_version.at(header_checksum); +} diff --git a/src/FunctionCompiler.hh b/src/FunctionCompiler.hh index 8e265f71..8363b315 100644 --- a/src/FunctionCompiler.hh +++ b/src/FunctionCompiler.hh @@ -88,3 +88,5 @@ struct DOLFileIndex { return this->name_to_file.empty() && this->item_id_to_file.empty(); } }; + +uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum); diff --git a/src/SendCommands.cc b/src/SendCommands.cc index 53b890ad..cb750aff 100644 --- a/src/SendCommands.cc +++ b/src/SendCommands.cc @@ -333,12 +333,18 @@ void prepare_client_for_patches(shared_ptr s, shared_ptr c, }; if (!(c->flags & Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH)) { - send_function_call(c, s->function_code_index->name_to_function.at("CacheClearFix-Phase1"), {}, "", 0, 0, 0x7F2734EC); - c->function_call_response_queue.emplace_back([s, wc = weak_ptr(c), send_version_detect](uint32_t, uint32_t) -> void { + send_function_call(c, s->function_code_index->name_to_function.at("CacheClearFix-Phase1"), {}, "", 0x80000000, 8, 0x7F2734EC); + c->function_call_response_queue.emplace_back([s, wc = weak_ptr(c), send_version_detect](uint32_t, uint32_t header_checksum) -> void { auto c = wc.lock(); if (!c) { return; } + try { + c->specific_version = specific_version_for_gc_header_checksum(header_checksum); + c->log.info("Version detected as %08" PRIX32 " from header checksum %08" PRIX32, c->specific_version, header_checksum); + } catch (const out_of_range&) { + c->log.info("Could not detect specific version from header checksum %08" PRIX32, header_checksum); + } send_function_call(c, s->function_code_index->name_to_function.at("CacheClearFix-Phase2")); c->function_call_response_queue.emplace_back([s, wc = weak_ptr(c), send_version_detect](uint32_t, uint32_t) -> void { auto c = wc.lock();