From dfa36bf030517212d38480517df8408e97c8f1bf Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 13:57:46 +0800 Subject: [PATCH] support for MultiQrCode Accounts --- dist/keyconfig.toml | 10 +++++++ src/bnusio.cpp | 4 +++ src/patches/qr.cpp | 19 ++++++++++++- src/poll.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++ src/poll.h | 7 +++++ 5 files changed, 106 insertions(+), 1 deletion(-) diff --git a/dist/keyconfig.toml b/dist/keyconfig.toml index aaa4205..1378855 100644 --- a/dist/keyconfig.toml +++ b/dist/keyconfig.toml @@ -21,6 +21,16 @@ P2_LEFT_RED = [] P2_RIGHT_RED = [] P2_RIGHT_BLUE = [] +# For First User's QRCode +[[QRCODE_CARD]] +READ_KEY = ["Q"] # Bind Keys +CARD = "BNTTCNID1" # Card Number + +# For Second User's QRCode +[[QRCODE_CARD]] +READ_KEY = ["W"] # Bind Keys +CARD = "BNTTCNID2" # Card Number + # F1 through F12 # NUM0 through NUM9 # QWERTYUIOPASDFGHJKLZXCVBNM diff --git a/src/bnusio.cpp b/src/bnusio.cpp index cab2448..984e77b 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -39,6 +39,8 @@ Keybindings P2_LEFT_BLUE = {}; Keybindings P2_LEFT_RED = {}; Keybindings P2_RIGHT_RED = {}; Keybindings P2_RIGHT_BLUE = {}; +CardKeybingings* QRCODE_CARDS = {{.keybindings.keycodes = {'W'}, .card = "BNTTCNID1"}, {.keybindings.keycodes = {'E'}, .card = "BNTTCNID2"}}; +size_t QRCODE_CARDS_LENG = 2; namespace bnusio { #define RETURN_FALSE(returnType, functionName, ...) \ @@ -342,6 +344,8 @@ Init () { SetConfigValue (keyconfig, "P2_RIGHT_RED", &P2_RIGHT_RED); SetConfigValue (keyconfig, "P2_RIGHT_BLUE", &P2_RIGHT_BLUE); + SetCardConfigValue (keyconfig, "QRCODE_CARD", &QRCODE_CARDS, &QRCODE_CARDS_LENG); + toml_free (keyconfig); } } diff --git a/src/patches/qr.cpp b/src/patches/qr.cpp index a42dedd..03c38c7 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -6,10 +6,13 @@ #include #include #include +#include extern GameVersion gameVersion; extern Keybindings QR_CARD_READ; extern Keybindings QR_DATA_READ; +extern CardKeybingings *QRCODE_CARDS; +extern size_t QRCODE_CARDS_LENG; namespace patches::Qr { @@ -17,6 +20,7 @@ enum class State { Ready, CopyWait, AfterCopy1, AfterCopy2 }; enum class Mode { Card, Data }; State gState = State::Ready; Mode gMode = Mode::Card; +std::string card_number = ""; HOOK_DYNAMIC (char, __fastcall, qrInit, i64) { return 1; } HOOK_DYNAMIC (char, __fastcall, qrRead, i64 a1) { @@ -73,6 +77,10 @@ HOOK_DYNAMIC (i64, __fastcall, copy_data, i64, void *dest, int length) { memcpy (dest, card.c_str (), card.size () + 1); gState = State::AfterCopy1; return card.size () + 1; + } else if (gMode == Mode::MultiCard) { + memcpy (dest, card_number.c_str (), card_number.size () + 1); + gState = State::AfterCopy1; + return card_number.size () + 1; } else { std::string serial = ""; u16 type = 0; @@ -145,7 +153,16 @@ Update () { std::cout << "Insert" << std::endl; gState = State::CopyWait; gMode = Mode::Data; - } + } else { + for (size_t i = 0; i < QRCODE_CARDS_LENG; i++) { + if (IsButtonTapped (QRCODE_CARDS[i].keybindings)) { + std::cout << "Insert" << std::endl; + gState = State::CopyWait; + gMode = Mode::MultiCard; + card_number = QRCODE_CARDS[i].card; + } + } + } } } diff --git a/src/poll.cpp b/src/poll.cpp index 925046f..2afb60b 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -184,6 +184,73 @@ SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { } } +void +SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **cards, size_t *leng) { + toml_array_t *array = toml_array_in (table, key); + if (!array) { + printWarning ("%s (%s): Cannot find array\n", __func__, key); + return; + } + + size_t length = toml_array_nelem(array); + *leng = length; + cards = memset(*cards, 0, length * (sizeof (*cards))); + + for (size_t i = 0; i < length; ++i) { + const toml_table_t* card_obj = toml_table_at(arr, i); + if (card_obj) { + memset (cards[i], 0, sizeof (**cards)); + cards[i]->card = toml_string_in(obj, "CARD") + + for (size_t i = 0; i < COUNTOFARR (keybind->buttons); i++) + cards[i]->keybindings.buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; + + for (size_t i = 0;; i++) { + toml_datum_t bind = toml_string_at (array, i); + if (!bind.ok) break; + ConfigValue value = StringToConfigEnum (bind.u.s); + free (bind.u.s); + + switch (value.type) { + case keycode: + for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.keycodes); i++) { + if (cards[i]->keybindings.keycodes[i] == 0) { + cards[i]->keybindings.keycodes[i] = value.keycode; + break; + } + } + break; + case button: + for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.buttons); i++) { + if (cards[i]->keybindings.buttons[i] == SDL_CONTROLLER_BUTTON_INVALID) { + cards[i]->keybindings.buttons[i] = value.button; + break; + } + } + break; + case axis: + for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.axis); i++) { + if (cards[i]->keybindings.axis[i] == 0) { + cards[i]->keybindings.axis[i] = value.axis; + break; + } + } + case scroll: + for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.scroll); i++) { + if (cards[i]->keybindings.scroll[i] == 0) { + cards[i]->keybindings.scroll[i] = value.scroll; + break; + } + } + break; + default: + break; + } + } + } + } +} + bool InitializePoll (HWND windowHandle) { bool hasRumble = true; diff --git a/src/poll.h b/src/poll.h index f7b63dd..3aa52f2 100644 --- a/src/poll.h +++ b/src/poll.h @@ -4,6 +4,7 @@ #include #include #include +#include enum SDLAxis { SDL_AXIS_NULL, @@ -42,6 +43,11 @@ struct Keybindings { Scroll scroll[2]; }; +struct CardKeybingings { + Keybindings keybindings; + std::string card; +} + enum EnumType { none, keycode, button, axis, scroll }; struct ConfigValue { @@ -65,6 +71,7 @@ void UpdatePoll (HWND windowHandle); void DisposePoll (); ConfigValue StringToConfigEnum (const char *value); void SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind); +void SetCardConfigValue (toml_table_t *table, const char *key, CardKeybingings **keybind, size_t *leng); InternalButtonState GetInternalButtonState (Keybindings bindings); void SetRumble (int left, int right, int length);