From c3052bbc2cfc244a52f8e38f515920611a1daca0 Mon Sep 17 00:00:00 2001 From: esuo1198 <54134.es@gmail.com> Date: Sat, 23 Mar 2024 05:55:20 +0900 Subject: [PATCH] Refactor toml processing --- src/dllmain.cpp | 34 ++++++++++++++++++---------------- src/helpers.cpp | 36 +++++++++++++++++++++++------------- src/helpers.h | 10 +++++----- src/patches/amauth.cpp | 35 +++++++++++++++++------------------ src/patches/cn_jun_2023.cpp | 4 ++-- 5 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/dllmain.cpp b/src/dllmain.cpp index ad64008..1fa81bb 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -7,12 +7,12 @@ GameVersion gameVersion = GameVersion::UNKNOWN; std::vector plugins; -const char *server = "127.0.0.1"; -const char *port = "54430"; -const char *chassisId = "284111080000"; -const char *shopId = "TAIKO ARCADE LOADER"; -const char *gameVerNum = "00.00"; -const char *countryCode = "JPN"; +std::string server = "127.0.0.1"; +std::string port = "54430"; +std::string chassisId = "284111080000"; +std::string shopId = "TAIKO ARCADE LOADER"; +std::string gameVerNum = "00.00"; +std::string countryCode = "JPN"; char fullAddress[256] = {'\0'}; char placeId[16] = {'\0'}; char accessCode1[21] = "00000000000000000001"; @@ -35,11 +35,13 @@ HOOK (i32, ssleay_Shutdown, PROC_ADDRESS ("ssleay32.dll", "SSL_shutdown")) { ret HOOK (i64, UsbFinderInitialize, PROC_ADDRESS ("nbamUsbFinder.dll", "nbamUsbFinderInitialize")) { return 0; } HOOK (i64, UsbFinderRelease, PROC_ADDRESS ("nbamUsbFinder.dll", "nbamUsbFinderRelease")) { return 0; } HOOK (i64, UsbFinderGetSerialNumber, PROC_ADDRESS ("nbamUsbFinder.dll", "nbamUsbFinderGetSerialNumber"), i32 a1, char *a2) { - strcpy (a2, chassisId); + strcpy (a2, chassisId.c_str ()); return 0; } -HOOK (i32, ws2_getaddrinfo, PROC_ADDRESS ("ws2_32.dll", "getaddrinfo"), const char *node, char *service, void *hints, void *out) { return originalws2_getaddrinfo (server, service, hints, out); } +HOOK (i32, ws2_getaddrinfo, PROC_ADDRESS ("ws2_32.dll", "getaddrinfo"), const char *node, char *service, void *hints, void *out) { + return originalws2_getaddrinfo (server.c_str (), service, hints, out); +} void GetGameVersion () { @@ -100,7 +102,7 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { // This is bad, dont do this // I/O in DllMain can easily cause a deadlock - const char *version = "auto"; + std::string version = "auto"; auto configPath = std::filesystem::current_path () / "config.toml"; std::unique_ptr config_ptr (openConfig (configPath), toml_free); toml_table_t *config = config_ptr.get (); @@ -114,24 +116,24 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { gameVerNum = readConfigString (amauth, "game_ver", gameVerNum); countryCode = readConfigString (amauth, "country_code", countryCode); - std::strcat (fullAddress, server); + std::strcat (fullAddress, server.c_str ()); std::strcat (fullAddress, ":"); - std::strcat (fullAddress, port); + std::strcat (fullAddress, port.c_str ()); - std::strcat (placeId, countryCode); + std::strcat (placeId, countryCode.c_str ()); std::strcat (placeId, "0FF0"); } auto patches = openConfigSection (config, "patches"); if (patches) version = readConfigString (patches, "version", version); } - if (!strcmp (version, "auto")) { + if (version == "auto") { GetGameVersion (); - } else if (!strcmp (version, "jp_nov_2020")) { + } else if (version == "jp_nov_2020") { gameVersion = GameVersion::JP_NOV_2020; - } else if (!strcmp (version, "cn_jun_2023")) { + } else if (version == "cn_jun_2023") { gameVersion = GameVersion::CN_JUN_2023; - } else if (!strcmp (version, "jp_apr_2023")) { + } else if (version == "jp_apr_2023") { gameVersion = GameVersion::JP_APR_2023; } else { MessageBoxA (0, "Unknown patch version", 0, MB_OK); diff --git a/src/helpers.cpp b/src/helpers.cpp index 27b9085..918951a 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -3,6 +3,14 @@ void *consoleHandle = 0; +static void +toml_myfree (void *p) { + if (p) { + char *pp = (char *)p; + delete[] pp; + } +} + toml_table_t * openConfig (std::filesystem::path path) { if (!std::filesystem::exists (path) || !path.has_filename ()) { @@ -37,10 +45,10 @@ openConfig (std::filesystem::path path) { } toml_table_t * -openConfigSection (toml_table_t *config, const char *sectionName) { - toml_table_t *section = toml_table_in (config, sectionName); +openConfigSection (toml_table_t *config, const std::string §ionName) { + toml_table_t *section = toml_table_in (config, sectionName.c_str ()); if (!section) { - printWarning ("%s (%s): cannot find section\n", __func__, sectionName); + printWarning ("%s (%s): cannot find section\n", __func__, sectionName.c_str ()); return 0; } @@ -48,32 +56,34 @@ openConfigSection (toml_table_t *config, const char *sectionName) { } bool -readConfigBool (toml_table_t *table, const char *key, bool notFoundValue) { - toml_datum_t data = toml_bool_in (table, key); +readConfigBool (toml_table_t *table, const std::string &key, bool notFoundValue) { + toml_datum_t data = toml_bool_in (table, key.c_str ()); if (!data.ok) return notFoundValue; return (bool)data.u.b; } int64_t -readConfigInt (toml_table_t *table, const char *key, int64_t notFoundValue) { - toml_datum_t data = toml_int_in (table, key); +readConfigInt (toml_table_t *table, const std::string &key, int64_t notFoundValue) { + toml_datum_t data = toml_int_in (table, key.c_str ()); if (!data.ok) return notFoundValue; return data.u.i; } -const char * -readConfigString (toml_table_t *table, const char *key, const char *notFoundValue) { - toml_datum_t data = toml_string_in (table, key); +const std::string +readConfigString (toml_table_t *table, const std::string &key, const std::string ¬FoundValue) { + toml_datum_t data = toml_string_in (table, key.c_str ()); if (!data.ok) return notFoundValue; + std::string str = data.u.s; + toml_myfree (data.u.s); - return data.u.s; + return str; } std::vector -readConfigIntArray (toml_table_t *table, const char *key, std::vector notFoundValue) { - toml_array_t *array = toml_array_in (table, key); +readConfigIntArray (toml_table_t *table, const std::string &key, std::vector notFoundValue) { + toml_array_t *array = toml_array_in (table, key.c_str ()); if (!array) return notFoundValue; std::vector datas; diff --git a/src/helpers.h b/src/helpers.h index a966bab..aece032 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -115,9 +115,9 @@ const HMODULE MODULE_HANDLE = GetModuleHandle (nullptr); #define printError(format, ...) printColour (ERROR_COLOUR, format, __VA_ARGS__) toml_table_t *openConfig (std::filesystem::path path); -toml_table_t *openConfigSection (toml_table_t *config, const char *sectionName); -bool readConfigBool (toml_table_t *table, const char *key, bool notFoundValue); -int64_t readConfigInt (toml_table_t *table, const char *key, int64_t notFoundValue); -const char *readConfigString (toml_table_t *table, const char *key, const char *notFoundValue); -std::vector readConfigIntArray (toml_table_t *table, const char *key, std::vector notFoundValue); +toml_table_t *openConfigSection (toml_table_t *config, const std::string §ionName); +bool readConfigBool (toml_table_t *table, const std::string &key, bool notFoundValue); +int64_t readConfigInt (toml_table_t *table, const std::string &key, int64_t notFoundValue); +const std::string readConfigString (toml_table_t *table, const std::string &key, const std::string ¬FoundValue); +std::vector readConfigIntArray (toml_table_t *table, const std::string &key, std::vector notFoundValue); void printColour (int colour, const char *format, ...); diff --git a/src/patches/amauth.cpp b/src/patches/amauth.cpp index 3ad9ca4..bbc12cd 100644 --- a/src/patches/amauth.cpp +++ b/src/patches/amauth.cpp @@ -16,12 +16,11 @@ * https://github.com/BroGamer4256/TaikoArcadeLoader/blob/master/plugins/amauth/dllmain.cpp */ -extern const char *server; -extern const char *port; -extern const char *chassisId; -extern const char *shopId; -extern const char *gameVerNum; -extern const char *countryCode; +extern std::string server; +extern std::string chassisId; +extern std::string shopId; +extern std::string gameVerNum; +extern std::string countryCode; extern char fullAddress[256]; extern char placeId[16]; @@ -321,7 +320,7 @@ public: // printf("IAuth_GetUpdaterState called\n"); memset (arr, 0, sizeof (*arr)); // Convert gameVerNum from string to double - double ver_d = std::stod (gameVerNum); + double ver_d = std::stod (gameVerNum.c_str ()); int ver_top = (int)ver_d; int ver_btm = (int)(ver_d * 100); @@ -379,8 +378,8 @@ public: memset (state, 0, sizeof (*state)); strcpy_s (state->mode, "STANDALONE"); strcpy_s (state->pcbid, "ABLN1080001"); - strcpy_s (state->dongle_serial, chassisId); - strcpy_s (state->auth_server_ip, server); + strcpy_s (state->dongle_serial, chassisId.c_str ()); + strcpy_s (state->auth_server_ip, server.c_str ()); strcpy_s (state->local_ip, "127.0.0.1"); strcpy_s (state->shop_router_ip, "127.0.0.1"); strcpy_s (state->subnet_mask, "***.***.***.***"); @@ -404,7 +403,7 @@ public: strcpy_s (version->game_id, "SBWY"); strcpy_s (version->game_ver, "12.20"); strcpy_s (version->game_cd, "S121"); - strcpy_s (version->cacfg_game_ver, gameVerNum); + strcpy_s (version->cacfg_game_ver, gameVerNum.c_str ()); strcpy_s (version->game_board_type, "0"); strcpy_s (version->game_board_id, "PCB"); strcpy_s (version->auth_url, fullAddress); @@ -427,8 +426,8 @@ public: strcpy_s (resp->uri, fullAddress); strcpy_s (resp->host, fullAddress); - strcpy_s (resp->shop_name, shopId); - strcpy_s (resp->shop_nickname, shopId); + strcpy_s (resp->shop_name, shopId.c_str ()); + strcpy_s (resp->shop_nickname, shopId.c_str ()); strcpy_s (resp->region0, "01035"); @@ -438,7 +437,7 @@ public: strcpy_s (resp->region_name3, "Z"); strcpy_s (resp->place_id, placeId); strcpy_s (resp->setting, ""); - strcpy_s (resp->country, countryCode); + strcpy_s (resp->country, countryCode.c_str ()); strcpy_s (resp->timezone, "+0900"); strcpy_s (resp->res_class, "PowerOnResponseVer3"); return 0; @@ -463,12 +462,12 @@ public: // printf("IAuth_GetMuchaAuthResponse called\n"); memset (arr, 0, sizeof (*arr)); - strcpy_s (arr->shop_name, sizeof (arr->shop_name), shopId); - strcpy_s (arr->shop_name_en, sizeof (arr->shop_name_en), shopId); - strcpy_s (arr->shop_nickname, sizeof (arr->shop_nickname), shopId); - strcpy_s (arr->shop_nickname_en, sizeof (arr->shop_nickname_en), shopId); + strcpy_s (arr->shop_name, sizeof (arr->shop_name), shopId.c_str ()); + strcpy_s (arr->shop_name_en, sizeof (arr->shop_name_en), shopId.c_str ()); + strcpy_s (arr->shop_nickname, sizeof (arr->shop_nickname), shopId.c_str ()); + strcpy_s (arr->shop_nickname_en, sizeof (arr->shop_nickname_en), shopId.c_str ()); strcpy_s (arr->place_id, sizeof (arr->place_id), placeId); - strcpy_s (arr->country_cd, sizeof (arr->country_cd), countryCode); + strcpy_s (arr->country_cd, sizeof (arr->country_cd), countryCode.c_str ()); strcpy_s (arr->area0, sizeof (arr->area0), "008"); strcpy_s (arr->area0_en, sizeof (arr->area0_en), "008"); diff --git a/src/patches/cn_jun_2023.cpp b/src/patches/cn_jun_2023.cpp index 84463cf..90f6162 100644 --- a/src/patches/cn_jun_2023.cpp +++ b/src/patches/cn_jun_2023.cpp @@ -2,7 +2,7 @@ #include "patches.h" #include -extern const char *chassisId; +extern std::string chassisId; namespace patches::CN_JUN_2023 { @@ -109,7 +109,7 @@ Init () { haspBuffer = (u8 *)malloc (0xD40); memset (haspBuffer, 0, 0xD40); - strcpy ((char *)(haspBuffer + 0xD00), chassisId); + strcpy ((char *)(haspBuffer + 0xD00), chassisId.c_str ()); u8 crc = 0; for (int i = 0; i < 62; i++) crc += haspBuffer[0xD00 + i];