From d25a3743f21cd784231f87147bff1f8f27fcefe0 Mon Sep 17 00:00:00 2001 From: esuo1198 <54134.es@gmail.com> Date: Thu, 21 Sep 2023 02:37:29 +0900 Subject: [PATCH] Add card emulation --- dist/keyconfig.toml | 4 +- src/bnusio.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++--- src/dllmain.cpp | 20 +++++++--- src/keyconfig.cpp | 8 +++- 4 files changed, 113 insertions(+), 15 deletions(-) diff --git a/dist/keyconfig.toml b/dist/keyconfig.toml index df29b1b..d09a890 100644 --- a/dist/keyconfig.toml +++ b/dist/keyconfig.toml @@ -7,7 +7,9 @@ DEBUG_DOWN = ["DOWNARROW"] DEBUG_ENTER = ["ENTER"] COIN_ADD = ["ENTER", "SDL_START"] -QR_CARD_READ = ["P"] +CARD_INSERT_1 = ["P"] +CARD_INSERT_2 = [] +QR_CARD_READ = ["O"] QR_DATA_READ = ["Q"] P1_LEFT_BLUE = ["D", "SDL_LTRIGGER"] diff --git a/src/bnusio.cpp b/src/bnusio.cpp index 34b959b..d365030 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -1,15 +1,26 @@ #include "helpers.h" #include "poll.h" #include "patches/patches.h" -#include #include "keyconfig.h" #include "constants.h" extern std::vector plugins; -extern char accessCode[21]; -extern char chipId[33]; +extern char accessCode1[21]; +extern char chipId1[33]; +extern char accessCode2[21]; +extern char chipId2[33]; extern GameVersion version; +typedef i32 (*callbackAttach) (i32, i32, i32 *); +typedef void (*callbackTouch) (i32, i32, u8[168], u64); +typedef void event (); +typedef void waitTouchEvent (callbackTouch, u64); +bool waitingForTouch = false; +callbackTouch touchCallback; +u64 touchData; +callbackAttach attachCallback; +i32 *attachData; + namespace bnusio { #define RETURN_FALSE(returnType, functionName, ...) \ returnType functionName (__VA_ARGS__) { return 0; } @@ -55,7 +66,6 @@ RETURN_FALSE (i64, bnusio_SramRead, i32 a1, u8 a2, i32 a3, u16 a4); RETURN_FALSE (i64, bnusio_SramWrite, i32 a1, u8 a2, i32 a3, u16 a4); RETURN_FALSE (i64, bnusio_ResetCoin); RETURN_FALSE (i64, bnusio_DecCoin, i32 a1, u16 a2); -RETURN_FALSE (i64, bnusio_Close); size_t bnusio_GetFirmwareVersion () { return 126; @@ -68,6 +78,8 @@ extern Keybindings DEBUG_UP; extern Keybindings DEBUG_DOWN; extern Keybindings DEBUG_ENTER; extern Keybindings COIN_ADD; +extern Keybindings CARD_INSERT_1; +extern Keybindings CARD_INSERT_2; extern Keybindings P1_LEFT_BLUE; extern Keybindings P1_LEFT_RED; extern Keybindings P1_RIGHT_RED; @@ -158,6 +170,53 @@ u16 __fastcall bnusio_GetCoin (i32 a1) { if (IsButtonTapped (COIN_ADD) && !testEnabled) coin_count++; 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; + if (IsButtonTapped (CARD_INSERT_1)) { + for (auto plugin : plugins) { + FARPROC insertEvent = GetProcAddress (plugin, "BeforeCard1Insert"); + if (insertEvent) ((event *)insertEvent) (); + } + for (auto plugin : plugins) { + FARPROC insertEvent = GetProcAddress (plugin, "Card1Insert"); + if (insertEvent) { + ((event *)insertEvent) (); + hasInserted = true; + } + } + if (!hasInserted) { + memcpy (cardData + 0x2C, chipId1, 33); + memcpy (cardData + 0x50, accessCode1, 21); + touchCallback (0, 0, cardData, touchData); + } + } else if (IsButtonTapped (CARD_INSERT_2)) { + for (auto plugin : plugins) { + FARPROC insertEvent = GetProcAddress (plugin, "BeforeCard2Insert"); + if (insertEvent) ((event *)insertEvent) (); + } + for (auto plugin : plugins) { + FARPROC insertEvent = GetProcAddress (plugin, "Card2Insert"); + if (insertEvent) { + ((event *)insertEvent) (); + hasInserted = true; + } + } + if (!hasInserted) { + memcpy (cardData + 0x2C, chipId2, 33); + memcpy (cardData + 0x50, accessCode2, 21); + touchCallback (0, 0, cardData, touchData); + } + } + } for (auto plugin : plugins) { auto updateEvent = GetProcAddress (plugin, "Update"); @@ -179,6 +238,15 @@ bnusio_GetSwIn () { sw |= (u32)IsButtonDown (SERVICE) << 14; return sw; } + +i64 +bnusio_Close () { + for (auto plugin : plugins) { + FARPROC exitEvent = GetProcAddress (plugin, "Exit"); + if (exitEvent) ((event *)exitEvent) (); + } + return 0; +} } HOOK (u64, bngrw_DevReset, PROC_ADDRESS ("bngrw.dll", "BngRwDevReset")) { return 1; } @@ -200,8 +268,22 @@ HOOK (i32, bngrw_ReqSendUrl, PROC_ADDRESS ("bngrw.dll", "BngRwReqSendUrlTo")) { HOOK (u64, bngrw_ReqSetLedPower, PROC_ADDRESS ("bngrw.dll", "BngRwReqSetLedPower")) { return 0; } HOOK (i32, bngrw_reqCancel, PROC_ADDRESS ("bngrw.dll", "BngRwReqCancel")) { return 1; } HOOK (u64, bngrw_Init, PROC_ADDRESS ("bngrw.dll", "BngRwInit")) { return 0; } -HOOK (u64, bngrw_attach, PROC_ADDRESS ("bngrw.dll", "BngRwAttach"), i32 a1, char *a2, i32 a3, i32 a4, i32 (*callback) (i32, i32, i32 *), i32 *a6) { return 1; } -HOOK (u64, bngrw_reqWaitTouch, PROC_ADDRESS ("bngrw.dll", "BngRwReqWaitTouch"), u32 a1, i32 a2, u32 a3, void (*callback) (i32, i32, u8[168], u64), u64 a5) { return 1; } +HOOK (u64, bngrw_attach, PROC_ADDRESS ("bngrw.dll", "BngRwAttach"), i32 a1, char *a2, i32 a3, i32 a4, i32 (*callback) (i32, i32, i32 *), i32 *a6) { + // This is way too fucking jank + attachCallback = callback; + attachData = a6; + return 1; +} +HOOK (u64, bngrw_reqWaitTouch, PROC_ADDRESS ("bngrw.dll", "BngRwReqWaitTouch"), u32 a1, i32 a2, u32 a3, void (*callback) (i32, i32, u8[168], u64), u64 a5) { + waitingForTouch = true; + touchCallback = callback; + touchData = a5; + for (auto plugin : plugins) { + FARPROC touchEvent = GetProcAddress (plugin, "WaitTouch"); + if (touchEvent) ((waitTouchEvent *)touchEvent) (callback, a5); + } + return 1; +} void Init () { @@ -224,6 +306,8 @@ Init () { INSTALL_HOOK (bngrw_ReqSetLedPower); INSTALL_HOOK (bngrw_reqCancel); INSTALL_HOOK (bngrw_Init) + INSTALL_HOOK (bngrw_attach); + INSTALL_HOOK (bngrw_reqWaitTouch); KeyConfig::Init(); } diff --git a/src/dllmain.cpp b/src/dllmain.cpp index 346fd21..24185e9 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -8,8 +8,10 @@ GameVersion version = GameVersion::UNKNOWN; std::vector plugins; const char *server = "127.0.0.1"; -char accessCode[21] = "00000000000000000000"; -char chipId[33] = "00000000000000000000000000000000"; +char accessCode1[21] = "00000000000000000001"; +char accessCode2[21] = "00000000000000000002"; +char chipId1[33] = "00000000000000000000000000000001"; +char chipId2[33] = "00000000000000000000000000000002"; HOOK (i32, ShowMouse, PROC_ADDRESS ("user32.dll", "ShowCursor"), bool) { return originalShowMouse (true); } HOOK (i32, ExitWindows, PROC_ADDRESS ("user32.dll", "ExitWindowsEx")) { @@ -75,9 +77,13 @@ createCard () { srand (time (0)); std::generate (buf, buf + 20, [&] () { return hexCharacterTable[rand () % 10]; }); - WritePrivateProfileStringA ("card", "accessCode", buf, ".\\card.ini"); + WritePrivateProfileStringA ("card", "accessCode1", buf, ".\\card.ini"); std::generate (buf, buf + 32, [&] () { return hexCharacterTable[rand () % 16]; }); - WritePrivateProfileStringA ("card", "chipId", buf, ".\\card.ini"); + WritePrivateProfileStringA ("card", "chipId1", buf, ".\\card.ini"); + std::generate (buf, buf + 20, [&] () { return hexCharacterTable[rand () % 10]; }); + WritePrivateProfileStringA ("card", "accessCode2", buf, ".\\card.ini"); + std::generate (buf, buf + 32, [&] () { return hexCharacterTable[rand () % 16]; }); + WritePrivateProfileStringA ("card", "chipId2", buf, ".\\card.ini"); } BOOL @@ -113,8 +119,10 @@ DllMain (HMODULE module, DWORD reason, LPVOID reserved) { } if (!std::filesystem::exists (".\\card.ini")) createCard (); - GetPrivateProfileStringA ("card", "accessCode", accessCode, accessCode, 21, ".\\card.ini"); - GetPrivateProfileStringA ("card", "chipId", chipId, chipId, 33, ".\\card.ini"); + GetPrivateProfileStringA ("card", "accessCode1", accessCode1, accessCode1, 21, ".\\card.ini"); + GetPrivateProfileStringA ("card", "chipId1", chipId1, chipId1, 33, ".\\card.ini"); + GetPrivateProfileStringA ("card", "accessCode2", accessCode2, accessCode2, 21, ".\\card.ini"); + GetPrivateProfileStringA ("card", "chipId2", chipId2, chipId2, 33, ".\\card.ini"); INSTALL_HOOK (ShowMouse); INSTALL_HOOK (ExitWindows); diff --git a/src/keyconfig.cpp b/src/keyconfig.cpp index 12d54ad..915b5bb 100644 --- a/src/keyconfig.cpp +++ b/src/keyconfig.cpp @@ -9,8 +9,10 @@ Keybindings DEBUG_UP = {.keycodes = {VK_UP}}; Keybindings DEBUG_DOWN = {.keycodes = {VK_DOWN}}; Keybindings DEBUG_ENTER = {.keycodes = {VK_RETURN}}; Keybindings COIN_ADD = {.keycodes = {VK_RETURN}, .buttons = {SDL_CONTROLLER_BUTTON_START}}; -Keybindings QR_CARD_READ = {.keycodes = {'P'}}; -Keybindings QR_DATA_READ = {.keycodes = {'Q'}}; +Keybindings CARD_INSERT_1 = {.keycodes = {'P'}}; +Keybindings CARD_INSERT_2 = {}; +Keybindings QR_CARD_READ = {.keycodes = {'O'}}; +Keybindings QR_DATA_READ = {.keycodes = {'Q'}}; Keybindings P1_LEFT_BLUE = {.keycodes = {'D'}, .axis = {SDL_AXIS_LTRIGGER_DOWN}}; Keybindings P1_LEFT_RED = {.keycodes = {'F'}, .buttons = {SDL_CONTROLLER_BUTTON_LEFTSTICK}}; Keybindings P1_RIGHT_RED = {.keycodes = {'J'}, .buttons = {SDL_CONTROLLER_BUTTON_RIGHTSTICK}}; @@ -36,6 +38,8 @@ Init() { SetConfigValue (config, "DEBUG_ENTER", &DEBUG_ENTER); SetConfigValue (config, "COIN_ADD", &COIN_ADD); + SetConfigValue (config, "CARD_INSERT_1", &CARD_INSERT_1); + SetConfigValue (config, "CARD_INSERT_2", &CARD_INSERT_2); SetConfigValue (config, "QR_CARD_READ", &QR_CARD_READ); SetConfigValue (config, "QR_DATA_READ", &QR_DATA_READ);