diff --git a/dist/config.toml b/dist/config.toml index d102d16..2446f35 100644 --- a/dist/config.toml +++ b/dist/config.toml @@ -2,6 +2,7 @@ server = "127.0.0.1" [patches] +version = "auto" res = { x = 1920, y = 1080 } unlock_songs = true shared_audio = true diff --git a/src/bnusio.cpp b/src/bnusio.cpp index ec879b4..cab2448 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -1,7 +1,7 @@ -#include "helpers.h" -#include "poll.h" -#include "patches/patches.h" #include "constants.h" +#include "helpers.h" +#include "patches/patches.h" +#include "poll.h" extern std::vector plugins; extern char accessCode1[21]; @@ -172,16 +172,14 @@ u16 __fastcall bnusio_GetCoin (i32 a1) { if (IsButtonTapped (TEST)) testEnabled = !testEnabled; if (IsButtonTapped (EXIT)) ExitProcess (0); if (waitingForTouch) { - static u8 cardData[168] - = { 0x01, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x92, 0x2E, 0x58, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x5C, 0x97, 0x44, 0xF0, 0x88, 0x04, 0x00, 0x43, 0x26, 0x2C, 0x33, 0x00, 0x04, - 0x06, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x42, 0x47, 0x49, 0x43, 0x36, - 0x00, 0x00, 0xFA, 0xE9, 0x69, 0x00, 0xF6, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - bool hasInserted = false; + static u8 cardData[168] = {0x01, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x92, 0x2E, 0x58, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x7F, 0x5C, 0x97, 0x44, 0xF0, 0x88, 0x04, 0x00, 0x43, 0x26, 0x2C, 0x33, 0x00, 0x04, 0x06, 0x10, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x4E, 0x42, 0x47, 0x49, 0x43, 0x36, 0x00, 0x00, 0xFA, 0xE9, 0x69, 0x00, 0xF6, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + bool hasInserted = false; if (IsButtonTapped (CARD_INSERT_1)) { for (auto plugin : plugins) { FARPROC insertEvent = GetProcAddress (plugin, "BeforeCard1Insert"); @@ -224,7 +222,7 @@ u16 __fastcall bnusio_GetCoin (i32 a1) { if (updateEvent) updateEvent (); } - patches::Qr::Update(); + patches::Qr::Update (); return coin_count; } @@ -314,9 +312,7 @@ Init () { toml_table_t *config = openConfig (configPath); if (config) { auto drum = openConfigSection (config, "drum"); - if (drum) { - drumWaitPeriod = readConfigInt (drum, "wait_period", drumWaitPeriod); - } + if (drum) drumWaitPeriod = readConfigInt (drum, "wait_period", drumWaitPeriod); toml_free (config); } diff --git a/src/dllmain.cpp b/src/dllmain.cpp index d69bc4d..e52732d 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -1,13 +1,13 @@ #include "bnusio.h" +#include "constants.h" #include "helpers.h" #include "patches/patches.h" #include "poll.h" -#include "constants.h" -GameVersion version = GameVersion::UNKNOWN; +GameVersion gameVersion = GameVersion::UNKNOWN; std::vector plugins; -const char *server = "127.0.0.1"; +const char *server = "127.0.0.1"; char accessCode1[21] = "00000000000000000001"; char accessCode2[21] = "00000000000000000002"; char chipId1[33] = "00000000000000000000000000000001"; @@ -58,12 +58,12 @@ GetGameVersion () { char *buf = (char *)calloc (length + 1, sizeof (char)); stream.read (buf, length); - version = (GameVersion)XXH64 (buf, length, 0); + gameVersion = (GameVersion)XXH64 (buf, length, 0); stream.close (); free (buf); - switch (version) { + switch (gameVersion) { case GameVersion::JP_NOV_2020: case GameVersion::CN_JUN_2023: break; default: MessageBoxA (0, "Unknown game version", 0, MB_OK); ExitProcess (0); @@ -91,7 +91,28 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { if (reason == DLL_PROCESS_ATTACH) { // This is bad, dont do this // I/O in DllMain can easily cause a deadlock - GetGameVersion (); + + const char *version = "auto"; + auto configPath = std::filesystem::current_path () / "config.toml"; + toml_table_t *config = openConfig (configPath); + if (config) { + auto amauth = openConfigSection (config, "amauth"); + if (amauth) server = readConfigString (amauth, "server", server); + auto patches = openConfigSection (config, "patches"); + if (patches) version = readConfigString (patches, "version", version); + toml_free (config); + } + + if (!strcmp (version, "auto")) { + GetGameVersion (); + } else if (!strcmp (version, "jp_nov_2020")) { + gameVersion = GameVersion::JP_NOV_2020; + } else if (!strcmp (version, "cn_jun_2023")) { + gameVersion = GameVersion::CN_JUN_2023; + } else { + MessageBoxA (0, "Unknown patch version", 0, MB_OK); + ExitProcess (0); + } auto pluginPath = std::filesystem::current_path () / "plugins"; @@ -111,16 +132,6 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { } } - auto configPath = std::filesystem::current_path () / "config.toml"; - toml_table_t *config = openConfig (configPath); - if (config) { - auto amauth = openConfigSection (config, "amauth"); - if (amauth) { - server = readConfigString (amauth, "server", server); - } - toml_free (config); - } - if (!std::filesystem::exists (".\\card.ini")) createCard (); GetPrivateProfileStringA ("card", "accessCode1", accessCode1, accessCode1, 21, ".\\card.ini"); GetPrivateProfileStringA ("card", "chipId1", chipId1, chipId1, 33, ".\\card.ini"); @@ -144,7 +155,7 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { bnusio::Init (); - switch (version) { + switch (gameVersion) { case GameVersion::UNKNOWN: break; case GameVersion::JP_NOV_2020: patches::JP_NOV_2020::Init (); break; case GameVersion::CN_JUN_2023: patches::CN_JUN_2023::Init (); break; diff --git a/src/helpers.cpp b/src/helpers.cpp index eb51f32..27b9085 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -77,11 +77,11 @@ readConfigIntArray (toml_table_t *table, const char *key, std::vector n if (!array) return notFoundValue; std::vector datas; - for (int i = 0; ; i++) { - toml_datum_t data = toml_int_at (array, i); - if (!data.ok) break; + for (int i = 0;; i++) { + toml_datum_t data = toml_int_at (array, i); + if (!data.ok) break; datas.push_back (data.u.i); - } + } return datas; } diff --git a/src/helpers.h b/src/helpers.h index e3261b4..a966bab 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -37,10 +37,10 @@ const HMODULE MODULE_HANDLE = GetModuleHandle (nullptr); void *where##functionName = (void *)(location); \ returnType implOf##functionName (__VA_ARGS__) -#define HOOK_DYNAMIC(returnType, callingConvention, functionName, ...) \ - typedef returnType callingConvention (*functionName) (__VA_ARGS__); \ - functionName original##functionName = NULL; \ - void *where##functionName = NULL; \ +#define HOOK_DYNAMIC(returnType, callingConvention, functionName, ...) \ + typedef returnType callingConvention (*functionName) (__VA_ARGS__); \ + functionName original##functionName = NULL; \ + void *where##functionName = NULL; \ returnType callingConvention implOf##functionName (__VA_ARGS__) #define VTABLE_HOOK(returnType, className, functionName, ...) \ @@ -56,10 +56,10 @@ const HMODULE MODULE_HANDLE = GetModuleHandle (nullptr); MH_EnableHook ((void *)where##functionName); \ } -#define INSTALL_HOOK_DYNAMIC(functionName, location) \ - { \ - where##functionName = (void *)location; \ - INSTALL_HOOK (functionName); \ +#define INSTALL_HOOK_DYNAMIC(functionName, location) \ + { \ + where##functionName = (void *)location; \ + INSTALL_HOOK (functionName); \ } #define INSTALL_VTABLE_HOOK(className, object, functionName, functionIndex) \ diff --git a/src/patches/amauth.cpp b/src/patches/amauth.cpp index 3791748..fe20470 100644 --- a/src/patches/amauth.cpp +++ b/src/patches/amauth.cpp @@ -190,7 +190,7 @@ CoCreateInstanceHook (const IID *const rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsC void Init () { MH_Initialize (); - MH_CreateHookApi (L"ole32.dll", "CoCreateInstance", reinterpret_cast (CoCreateInstanceHook), reinterpret_cast (&gOriCoCreateInstance)); // NOLINT(clang-diagnostic-microsoft-cast) + MH_CreateHookApi (L"ole32.dll", "CoCreateInstance", (LPVOID)CoCreateInstanceHook, (void **)&gOriCoCreateInstance); MH_EnableHook (nullptr); } } // namespace patches::AmAuth diff --git a/src/patches/jp_nov_2020.cpp b/src/patches/jp_nov_2020.cpp index df603ac..fff390b 100644 --- a/src/patches/jp_nov_2020.cpp +++ b/src/patches/jp_nov_2020.cpp @@ -119,7 +119,7 @@ Init () { } // Move various files to current directory - auto amHandle = (u64) (GetModuleHandle ("AMFrameWork.dll")); + auto amHandle = (u64)GetModuleHandle ("AMFrameWork.dll"); WRITE_MEMORY (amHandle + 0x33EF7, u8, 0xEB); WRITE_MEMORY (amHandle + 0x3404A, u8, 0xEB); WRITE_MEMORY (amHandle + 0x34429, u8, 0xEB); diff --git a/src/patches/qr.cpp b/src/patches/qr.cpp index c31bbf2..a42dedd 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -7,36 +7,26 @@ #include #include -extern GameVersion version; +extern GameVersion gameVersion; extern Keybindings QR_CARD_READ; extern Keybindings QR_DATA_READ; namespace patches::Qr { -enum class State { - Ready, - CopyWait, - AfterCopy1, - AfterCopy2 -}; - -enum class Mode { - Card, - Data -}; - +enum class State { Ready, CopyWait, AfterCopy1, AfterCopy2 }; +enum class Mode { Card, Data }; State gState = State::Ready; Mode gMode = Mode::Card; -HOOK_DYNAMIC (char, __fastcall, qrInit, int64_t a1) { return 1; } -HOOK_DYNAMIC (char, __fastcall, qrRead, int64_t a1) { - *reinterpret_cast (a1 + 40) = 1; - *reinterpret_cast (a1 + 16) = 1; - *reinterpret_cast (a1 + 112) = 0; +HOOK_DYNAMIC (char, __fastcall, qrInit, i64) { return 1; } +HOOK_DYNAMIC (char, __fastcall, qrRead, i64 a1) { + *(DWORD *)(a1 + 40) = 1; + *(DWORD *)(a1 + 16) = 1; + *(BYTE *)(a1 + 112) = 0; return 1; } -HOOK_DYNAMIC (char, __fastcall, qrClose, int64_t a1) { return 1; } -HOOK_DYNAMIC (int64_t, __fastcall, callQrUnknown, int64_t a1) { +HOOK_DYNAMIC (char, __fastcall, qrClose, i64) { return 1; } +HOOK_DYNAMIC (i64, __fastcall, callQrUnknown, i64) { switch (gState) { case State::Ready: case State::CopyWait: { @@ -52,33 +42,31 @@ HOOK_DYNAMIC (int64_t, __fastcall, callQrUnknown, int64_t a1) { default: return 0; } } -HOOK_DYNAMIC (bool, __fastcall, Send1, int64_t, const void *, int64_t) { return true; } -HOOK_DYNAMIC (bool, __fastcall, Send2, int64_t a1, char a2) { return true; } -HOOK_DYNAMIC (bool, __fastcall, Send3, int64_t a1) { +HOOK_DYNAMIC (bool, __fastcall, Send1, i64, const void *, i64) { return true; } +HOOK_DYNAMIC (bool, __fastcall, Send2, i64, char) { return true; } +HOOK_DYNAMIC (bool, __fastcall, Send3, i64 a1) { *(WORD *)(a1 + 88) = 0; *(BYTE *)(a1 + 90) = 0; return true; } -HOOK_DYNAMIC (bool, __fastcall, Send4, int64_t a1) { - *(BYTE *)(a1 + 88) = 1; - *(int64_t *)(a1 + 32) = *(int64_t *)(a1 + 24); - *(WORD *)(a1 + 89) = 0; +HOOK_DYNAMIC (bool, __fastcall, Send4, i64 a1) { + *(BYTE *)(a1 + 88) = 1; + *(i64 *)(a1 + 32) = *(i64 *)(a1 + 24); + *(WORD *)(a1 + 89) = 0; return true; } -HOOK_DYNAMIC (int64_t, __fastcall, copy_data, int64_t this_, void *dest, int length) { +HOOK_DYNAMIC (i64, __fastcall, copy_data, i64, void *dest, int length) { if (gState == State::CopyWait) { std::cout << "Copy data, length: " << length << std::endl; auto configPath = std::filesystem::current_path () / "config.toml"; toml_table_t *config = openConfig (configPath); - + if (gMode == Mode::Card) { std::string card = ""; if (config) { auto qr = openConfigSection (config, "qr"); - if (qr) { - card = readConfigString (qr, "card", ""); - } + if (qr) card = readConfigString (qr, "card", ""); toml_free (config); } @@ -87,7 +75,7 @@ HOOK_DYNAMIC (int64_t, __fastcall, copy_data, int64_t this_, void *dest, int len return card.size () + 1; } else { std::string serial = ""; - u16 type = 0; + u16 type = 0; std::vector songNoes; if (config) { @@ -95,36 +83,35 @@ HOOK_DYNAMIC (int64_t, __fastcall, copy_data, int64_t this_, void *dest, int len if (qr) { auto data = openConfigSection (qr, "data"); if (data) { - serial = readConfigString (data, "serial", ""); - type = readConfigInt (data, "type", 0); + serial = readConfigString (data, "serial", ""); + type = readConfigInt (data, "type", 0); songNoes = readConfigIntArray (data, "song_no", songNoes); } } toml_free (config); } - BYTE serial_length = static_cast (serial.size ()); + BYTE serial_length = (BYTE)serial.size (); std::vector byteBuffer = {0x53, 0x31, 0x32, 0x00, 0x00, 0xFF, 0xFF, serial_length, 0x01, 0x00}; for (char c : serial) - byteBuffer.push_back (static_cast (c)); + byteBuffer.push_back ((BYTE)c); if (type == 5) { std::vector folderData = {0xFF, 0xFF}; - folderData.push_back (songNoes.size() * 2); + folderData.push_back (songNoes.size () * 2); - folderData.push_back (static_cast (type & 0xFF)); - folderData.push_back (static_cast ((type >> 8) & 0xFF)); + folderData.push_back ((u8)(type & 0xFF)); + folderData.push_back ((u8)((type >> 8) & 0xFF)); - for (u16 songNo:songNoes) { - folderData.push_back (static_cast (songNo & 0xFF)); - folderData.push_back (static_cast ((songNo >> 8) & 0xFF)); + for (u16 songNo : songNoes) { + folderData.push_back ((u8)(songNo & 0xFF)); + folderData.push_back ((u8)((songNo >> 8) & 0xFF)); } - - for (auto c:folderData) { + + for (auto c : folderData) byteBuffer.push_back (c); - } } byteBuffer.push_back (0xEE); @@ -164,35 +151,35 @@ Update () { void Init () { - auto amHandle = reinterpret_cast (GetModuleHandle ("AMFrameWork.dll")); - switch (version) { + auto amHandle = (u64)GetModuleHandle ("AMFrameWork.dll"); + switch (gameVersion) { case GameVersion::JP_NOV_2020: { - INSTALL_HOOK_DYNAMIC (qrInit, reinterpret_cast (amHandle + 0x1BA00)); - INSTALL_HOOK_DYNAMIC (qrRead, reinterpret_cast (amHandle + 0x1BC20)); - INSTALL_HOOK_DYNAMIC (qrClose, reinterpret_cast (amHandle + 0x1BBD0)); - INSTALL_HOOK_DYNAMIC (callQrUnknown, reinterpret_cast (amHandle + 0xFD40)); + INSTALL_HOOK_DYNAMIC (qrInit, (LPVOID)(amHandle + 0x1BA00)); + INSTALL_HOOK_DYNAMIC (qrRead, (LPVOID)(amHandle + 0x1BC20)); + INSTALL_HOOK_DYNAMIC (qrClose, (LPVOID)(amHandle + 0x1BBD0)); + INSTALL_HOOK_DYNAMIC (callQrUnknown, (LPVOID)(amHandle + 0xFD40)); // 08.18 has no Send1 - INSTALL_HOOK_DYNAMIC (Send2, reinterpret_cast (amHandle + 0x1C2D0)); - INSTALL_HOOK_DYNAMIC (Send3, reinterpret_cast (amHandle + 0x1C260)); - INSTALL_HOOK_DYNAMIC (Send4, reinterpret_cast (amHandle + 0x1C220)); - INSTALL_HOOK_DYNAMIC (copy_data, reinterpret_cast (amHandle + 0x1C2A0)); - break; + INSTALL_HOOK_DYNAMIC (Send2, (LPVOID)(amHandle + 0x1C2D0)); + INSTALL_HOOK_DYNAMIC (Send3, (LPVOID)(amHandle + 0x1C260)); + INSTALL_HOOK_DYNAMIC (Send4, (LPVOID)(amHandle + 0x1C220)); + INSTALL_HOOK_DYNAMIC (copy_data, (LPVOID)(amHandle + 0x1C2A0)); + break; } case GameVersion::CN_JUN_2023: { - INSTALL_HOOK_DYNAMIC (qrInit, reinterpret_cast (amHandle + 0x161B0)); - INSTALL_HOOK_DYNAMIC (qrRead, reinterpret_cast (amHandle + 0x163A0)); - INSTALL_HOOK_DYNAMIC (qrClose, reinterpret_cast (amHandle + 0x16350)); - INSTALL_HOOK_DYNAMIC (callQrUnknown, reinterpret_cast (amHandle + 0x8F60)); - INSTALL_HOOK_DYNAMIC (Send1, reinterpret_cast (amHandle + 0x16A30)); - INSTALL_HOOK_DYNAMIC (Send2, reinterpret_cast (amHandle + 0x16A00)); - INSTALL_HOOK_DYNAMIC (Send3, reinterpret_cast (amHandle + 0x16990)); - INSTALL_HOOK_DYNAMIC (Send4, reinterpret_cast (amHandle + 0x16940)); - INSTALL_HOOK_DYNAMIC (copy_data, reinterpret_cast (amHandle + 0x169D0)); - break; + INSTALL_HOOK_DYNAMIC (qrInit, (LPVOID)(amHandle + 0x161B0)); + INSTALL_HOOK_DYNAMIC (qrRead, (LPVOID)(amHandle + 0x163A0)); + INSTALL_HOOK_DYNAMIC (qrClose, (LPVOID)(amHandle + 0x16350)); + INSTALL_HOOK_DYNAMIC (callQrUnknown, (LPVOID)(amHandle + 0x8F60)); + INSTALL_HOOK_DYNAMIC (Send1, (LPVOID)(amHandle + 0x16A30)); + INSTALL_HOOK_DYNAMIC (Send2, (LPVOID)(amHandle + 0x16A00)); + INSTALL_HOOK_DYNAMIC (Send3, (LPVOID)(amHandle + 0x16990)); + INSTALL_HOOK_DYNAMIC (Send4, (LPVOID)(amHandle + 0x16940)); + INSTALL_HOOK_DYNAMIC (copy_data, (LPVOID)(amHandle + 0x169D0)); + break; + } + default: { + break; } - default: { - break; - } } } } // namespace patches::Qr