From dcd8e631dea49f43f092b7bf182bc446a2ba0d0d Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:39:13 +0300 Subject: [PATCH] [CFG] Add GetCountryCodeID --- CMakeLists.txt | 2 +- include/result/result.hpp | 1 + include/result/result_cfg.hpp | 8 +++++++ include/services/cfg.hpp | 1 + src/core/services/cfg.cpp | 45 ++++++++++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 include/result/result_cfg.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 20b6a4ca..b6079a1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,7 +174,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/termcolor.hpp include/crypto/aes_engine.hpp include/metaprogramming.hpp include/PICA/pica_vertex.hpp include/config.hpp include/services/ir_user.hpp include/http_server.hpp include/cheats.hpp include/action_replay.hpp include/renderer_sw/renderer_sw.hpp include/compiler_builtins.hpp - include/fs/romfs.hpp include/fs/ivfc.hpp include/discord_rpc.hpp include/services/http.hpp + include/fs/romfs.hpp include/fs/ivfc.hpp include/discord_rpc.hpp include/services/http.hpp include/result/result_cfg.hpp ) set(THIRD_PARTY_SOURCE_FILES third_party/imgui/imgui.cpp diff --git a/include/result/result.hpp b/include/result/result.hpp index 00a2436e..53bb725d 100644 --- a/include/result/result.hpp +++ b/include/result/result.hpp @@ -1,5 +1,6 @@ #pragma once +#include "result_cfg.hpp" #include "result_common.hpp" #include "result_kernel.hpp" #include "result_os.hpp" diff --git a/include/result/result_cfg.hpp b/include/result/result_cfg.hpp new file mode 100644 index 00000000..19b200cb --- /dev/null +++ b/include/result/result_cfg.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "result_common.hpp" + +DEFINE_HORIZON_RESULT_MODULE(Result::CFG, Config); + +namespace Result::CFG { + DEFINE_HORIZON_RESULT(NotFound, 1018, WrongArgument, Permanent); +}; diff --git a/include/services/cfg.hpp b/include/services/cfg.hpp index 1250a0c7..666e8b23 100644 --- a/include/services/cfg.hpp +++ b/include/services/cfg.hpp @@ -16,6 +16,7 @@ class CFGService { // Service functions void getConfigInfoBlk2(u32 messagePointer); + void getCountryCodeID(u32 messagePointer); void getRegionCanadaUSA(u32 messagePointer); void getSystemModel(u32 messagePointer); void genUniqueConsoleHash(u32 messagePointer); diff --git a/src/core/services/cfg.cpp b/src/core/services/cfg.cpp index d7861a61..ea853361 100644 --- a/src/core/services/cfg.cpp +++ b/src/core/services/cfg.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include namespace CFGCommands { enum : u32 { @@ -12,7 +14,8 @@ namespace CFGCommands { SecureInfoGetRegion = 0x00020000, GenHashConsoleUnique = 0x00030040, GetRegionCanadaUSA = 0x00040000, - GetSystemModel = 0x00050000 + GetSystemModel = 0x00050000, + GetCountryCodeID = 0x000A0040, }; } @@ -22,6 +25,7 @@ void CFGService::handleSyncRequest(u32 messagePointer) { const u32 command = mem.read32(messagePointer); switch (command) { case CFGCommands::GetConfigInfoBlk2: [[likely]] getConfigInfoBlk2(messagePointer); break; + case CFGCommands::GetCountryCodeID: getCountryCodeID(messagePointer); break; case CFGCommands::GetRegionCanadaUSA: getRegionCanadaUSA(messagePointer); break; case CFGCommands::GetSystemModel: getSystemModel(messagePointer); break; case CFGCommands::GenHashConsoleUnique: genUniqueConsoleHash(messagePointer); break; @@ -154,4 +158,43 @@ void CFGService::getRegionCanadaUSA(u32 messagePointer) { mem.write32(messagePointer, IPC::responseHeader(0x4, 2, 0)); mem.write32(messagePointer + 4, Result::Success); mem.write8(messagePointer + 8, ret); +} + +constexpr u16 C(const char name[3]) { return name[0] | (name[1] << 8); } +static std::unordered_map countryCodeToTableIDMap = { + {C("JP"), 1}, {C("AI"), 8}, {C("AG"), 9}, {C("AR"), 10}, {C("AW"), 11}, {C("BS"), 12}, {C("BB"), 13}, {C("BZ"), 14}, {C("BO"), 15}, + {C("BR"), 16}, {C("VG"), 17}, {C("CA"), 18}, {C("KY"), 19}, {C("CL"), 20}, {C("CO"), 21}, {C("CR"), 22}, {C("DM"), 23}, {C("DO"), 24}, + {C("EC"), 25}, {C("SV"), 26}, {C("GF"), 27}, {C("GD"), 28}, {C("GP"), 29}, {C("GT"), 30}, {C("GY"), 31}, {C("HT"), 32}, {C("HN"), 33}, + {C("JM"), 34}, {C("MQ"), 35}, {C("MX"), 36}, {C("MS"), 37}, {C("AN"), 38}, {C("NI"), 39}, {C("PA"), 40}, {C("PY"), 41}, {C("PE"), 42}, + {C("KN"), 43}, {C("LC"), 44}, {C("VC"), 45}, {C("SR"), 46}, {C("TT"), 47}, {C("TC"), 48}, {C("US"), 49}, {C("UY"), 50}, {C("VI"), 51}, + {C("VE"), 52}, {C("AL"), 64}, {C("AU"), 65}, {C("AT"), 66}, {C("BE"), 67}, {C("BA"), 68}, {C("BW"), 69}, {C("BG"), 70}, {C("HR"), 71}, + {C("CY"), 72}, {C("CZ"), 73}, {C("DK"), 74}, {C("EE"), 75}, {C("FI"), 76}, {C("FR"), 77}, {C("DE"), 78}, {C("GR"), 79}, {C("HU"), 80}, + {C("IS"), 81}, {C("IE"), 82}, {C("IT"), 83}, {C("LV"), 84}, {C("LS"), 85}, {C("LI"), 86}, {C("LT"), 87}, {C("LU"), 88}, {C("MK"), 89}, + {C("MT"), 90}, {C("ME"), 91}, {C("MZ"), 92}, {C("NA"), 93}, {C("NL"), 94}, {C("NZ"), 95}, {C("NO"), 96}, {C("PL"), 97}, {C("PT"), 98}, + {C("RO"), 99}, {C("RU"), 100}, {C("RS"), 101}, {C("SK"), 102}, {C("SI"), 103}, {C("ZA"), 104}, {C("ES"), 105}, {C("SZ"), 106}, {C("SE"), 107}, + {C("CH"), 108}, {C("TR"), 109}, {C("GB"), 110}, {C("ZM"), 111}, {C("ZW"), 112}, {C("AZ"), 113}, {C("MR"), 114}, {C("ML"), 115}, {C("NE"), 116}, + {C("TD"), 117}, {C("SD"), 118}, {C("ER"), 119}, {C("DJ"), 120}, {C("SO"), 121}, {C("AD"), 122}, {C("GI"), 123}, {C("GG"), 124}, {C("IM"), 125}, + {C("JE"), 126}, {C("MC"), 127}, {C("TW"), 128}, {C("KR"), 136}, {C("HK"), 144}, {C("MO"), 145}, {C("ID"), 152}, {C("SG"), 153}, {C("TH"), 154}, + {C("PH"), 155}, {C("MY"), 156}, {C("CN"), 160}, {C("AE"), 168}, {C("IN"), 169}, {C("EG"), 170}, {C("OM"), 171}, {C("QA"), 172}, {C("KW"), 173}, + {C("SA"), 174}, {C("SY"), 175}, {C("BH"), 176}, {C("JO"), 177}, {C("SM"), 184}, {C("VA"), 185}, {C("BM"), 186}, +}; + +void CFGService::getCountryCodeID(u32 messagePointer) { + // Read the character code as a u16 instead of as ASCII, and use it to index the unordered_map above and get the result + const u16 characterCode = mem.read16(messagePointer + 4); + log("CFG::GetCountryCodeID (code = %04X)\n", characterCode); + + mem.write32(messagePointer, IPC::responseHeader(0x0A, 2, 0)); + + // If the character code is valid, return its table ID and a success code + if (auto search = countryCodeToTableIDMap.find(characterCode); search != countryCodeToTableIDMap.end()) { + mem.write32(messagePointer + 4, Result::Success); + mem.write16(messagePointer + 8, search->second); + } + + else { + Helpers::warn("CFG::GetCountryCodeID: Invalid country code %X", characterCode); + mem.write32(messagePointer + 4, Result::CFG::NotFound); + mem.write16(messagePointer + 8, 0xFF); + } } \ No newline at end of file