From dfa36bf030517212d38480517df8408e97c8f1bf Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 13:57:46 +0800 Subject: [PATCH 01/12] 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); From 013556bd08c03072aa23d2658af364df70e15182 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 14:18:09 +0800 Subject: [PATCH 02/12] support for MultiQrCode Accounts --- src/patches/qr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/patches/qr.cpp b/src/patches/qr.cpp index 03c38c7..3ae269c 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -160,6 +160,7 @@ Update () { gState = State::CopyWait; gMode = Mode::MultiCard; card_number = QRCODE_CARDS[i].card; + break; } } } From c50ab88ce1f4ed511a4b504a7d3f7ccb199f784c Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 14:35:54 +0800 Subject: [PATCH 03/12] support for MultiQrCode Accounts --- src/patches/qr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/patches/qr.cpp b/src/patches/qr.cpp index 3ae269c..8fe45e3 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -78,6 +78,7 @@ HOOK_DYNAMIC (i64, __fastcall, copy_data, i64, void *dest, int length) { gState = State::AfterCopy1; return card.size () + 1; } else if (gMode == Mode::MultiCard) { + if (config) toml_free (config); memcpy (dest, card_number.c_str (), card_number.size () + 1); gState = State::AfterCopy1; return card_number.size () + 1; From b510c1e53551902991df2406d01190c24984d449 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 15:24:47 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bnusio.cpp | 4 ++-- src/patches/qr.cpp | 22 +++++++++++----------- src/poll.cpp | 20 +++++++++++++------- src/poll.h | 6 +++--- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/bnusio.cpp b/src/bnusio.cpp index 984e77b..74cb17b 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -39,8 +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; +CardKeybindings *QRCODE_CARDS = {}; +size_t QRCODE_CARDS_LENG = 0; namespace bnusio { #define RETURN_FALSE(returnType, functionName, ...) \ diff --git a/src/patches/qr.cpp b/src/patches/qr.cpp index 8fe45e3..7ec8da2 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -11,16 +11,16 @@ extern GameVersion gameVersion; extern Keybindings QR_CARD_READ; extern Keybindings QR_DATA_READ; -extern CardKeybingings *QRCODE_CARDS; +extern CardKeybindings *QRCODE_CARDS; extern size_t QRCODE_CARDS_LENG; namespace patches::Qr { enum class State { Ready, CopyWait, AfterCopy1, AfterCopy2 }; -enum class Mode { Card, Data }; -State gState = State::Ready; -Mode gMode = Mode::Card; -std::string card_number = ""; +enum class Mode { Card, Data, MultiCard }; +State gState = State::Ready; +Mode gMode = Mode::Card; +std::string gCardNumber = ""; HOOK_DYNAMIC (char, __fastcall, qrInit, i64) { return 1; } HOOK_DYNAMIC (char, __fastcall, qrRead, i64 a1) { @@ -79,9 +79,9 @@ HOOK_DYNAMIC (i64, __fastcall, copy_data, i64, void *dest, int length) { return card.size () + 1; } else if (gMode == Mode::MultiCard) { if (config) toml_free (config); - memcpy (dest, card_number.c_str (), card_number.size () + 1); + memcpy (dest, gCardNumber.c_str (), gCardNumber.size () + 1); gState = State::AfterCopy1; - return card_number.size () + 1; + return gCardNumber.size () + 1; } else { std::string serial = ""; u16 type = 0; @@ -154,13 +154,13 @@ Update () { std::cout << "Insert" << std::endl; gState = State::CopyWait; gMode = Mode::Data; - } else { + } else if (QRCODE_CARDS != nullptr) { 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; + gState = State::CopyWait; + gMode = Mode::MultiCard; + gCardNumber = QRCODE_CARDS[i].card; break; } } diff --git a/src/poll.cpp b/src/poll.cpp index 2afb60b..5fc2e2b 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -186,23 +186,29 @@ 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) { + toml_array_t *top_array = toml_array_in (table, key); + if (!top_array) { printWarning ("%s (%s): Cannot find array\n", __func__, key); return; } - size_t length = toml_array_nelem(array); + size_t length = toml_array_nelem(top_array); *leng = length; - cards = memset(*cards, 0, length * (sizeof (*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); + toml_table_t* card_obj = toml_table_at(top_array, i); if (card_obj) { memset (cards[i], 0, sizeof (**cards)); - cards[i]->card = toml_string_in(obj, "CARD") + cards[i]->card = readConfigString(card_obj, "CARD", ""); - for (size_t i = 0; i < COUNTOFARR (keybind->buttons); i++) + toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); + if (!array) { + printWarning ("%s (%s): Cannot find READ_KEY in CARD_INFO\n", __func__, key); + return; + } + + for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.buttons); i++) cards[i]->keybindings.buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; for (size_t i = 0;; i++) { diff --git a/src/poll.h b/src/poll.h index 3aa52f2..826d04d 100644 --- a/src/poll.h +++ b/src/poll.h @@ -43,10 +43,10 @@ struct Keybindings { Scroll scroll[2]; }; -struct CardKeybingings { +struct CardKeybindings { Keybindings keybindings; std::string card; -} +}; enum EnumType { none, keycode, button, axis, scroll }; @@ -71,7 +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); +void SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **keybind, size_t *leng); InternalButtonState GetInternalButtonState (Keybindings bindings); void SetRumble (int left, int right, int length); From 732ecb555f4e5a87bae60b67e0b39cf36f0afe9a Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 15:44:45 +0800 Subject: [PATCH 05/12] fix MultiQrCode --- src/bnusio.cpp | 4 ++-- src/poll.cpp | 36 ++++++++++++++++++------------------ src/poll.h | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/bnusio.cpp b/src/bnusio.cpp index 74cb17b..5fb34e3 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -39,7 +39,7 @@ Keybindings P2_LEFT_BLUE = {}; Keybindings P2_LEFT_RED = {}; Keybindings P2_RIGHT_RED = {}; Keybindings P2_RIGHT_BLUE = {}; -CardKeybindings *QRCODE_CARDS = {}; +CardKeybindings *QRCODE_CARDS = new CardKeybindings[0]; size_t QRCODE_CARDS_LENG = 0; namespace bnusio { @@ -344,7 +344,7 @@ 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); + SetCardConfigValue (keyconfig, "QRCODE_CARD", QRCODE_CARDS, &QRCODE_CARDS_LENG); toml_free (keyconfig); } diff --git a/src/poll.cpp b/src/poll.cpp index 5fc2e2b..d3d7beb 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -185,7 +185,7 @@ SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { } void -SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **cards, size_t *leng) { +SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards, size_t *leng) { toml_array_t *top_array = toml_array_in (table, key); if (!top_array) { printWarning ("%s (%s): Cannot find array\n", __func__, key); @@ -194,13 +194,13 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **card size_t length = toml_array_nelem(top_array); *leng = length; - memset(cards, 0, length * (sizeof (*cards))); + cards = new CardKeybindings[length]; for (size_t i = 0; i < length; ++i) { toml_table_t* card_obj = toml_table_at(top_array, i); if (card_obj) { - memset (cards[i], 0, sizeof (**cards)); - cards[i]->card = readConfigString(card_obj, "CARD", ""); + CardKeybindings cardInfo = cards[i] = {}; + cardInfo.card = readConfigString(card_obj, "CARD", ""); toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); if (!array) { @@ -208,8 +208,8 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **card return; } - for (size_t i = 0; i < COUNTOFARR (cards[i]->keybindings.buttons); i++) - cards[i]->keybindings.buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; + for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.buttons); i++) + cardInfo.keybindings.buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); @@ -219,32 +219,32 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **card 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; + for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.keycodes); i++) { + if (cardInfo.keybindings.keycodes[i] == 0) { + cardInfo.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; + for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.buttons); i++) { + if (cardInfo.keybindings.buttons[i] == SDL_CONTROLLER_BUTTON_INVALID) { + cardInfo.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; + for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.axis); i++) { + if (cardInfo.keybindings.axis[i] == 0) { + cardInfo.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; + for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.scroll); i++) { + if (cardInfo.keybindings.scroll[i] == 0) { + cardInfo.keybindings.scroll[i] = value.scroll; break; } } diff --git a/src/poll.h b/src/poll.h index 826d04d..0415ab7 100644 --- a/src/poll.h +++ b/src/poll.h @@ -71,7 +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, CardKeybindings **keybind, size_t *leng); +void SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *keybind, size_t *leng); InternalButtonState GetInternalButtonState (Keybindings bindings); void SetRumble (int left, int right, int length); From 3a6c16d1169c01e7696e33ca582ed7969db30b40 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 15:49:37 +0800 Subject: [PATCH 06/12] =?UTF-8?q?QRCode=E9=BB=98=E8=AE=A4=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=AE=A9=E5=BC=80=E9=94=AE=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/keyconfig.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/keyconfig.toml b/dist/keyconfig.toml index 1378855..5bf4114 100644 --- a/dist/keyconfig.toml +++ b/dist/keyconfig.toml @@ -23,12 +23,12 @@ P2_RIGHT_BLUE = [] # For First User's QRCode [[QRCODE_CARD]] -READ_KEY = ["Q"] # Bind Keys +READ_KEY = ["W"] # Bind Keys CARD = "BNTTCNID1" # Card Number # For Second User's QRCode [[QRCODE_CARD]] -READ_KEY = ["W"] # Bind Keys +READ_KEY = ["E"] # Bind Keys CARD = "BNTTCNID2" # Card Number # F1 through F12 From df389c72fc1439fa2e97b7f42613729996f22226 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 18:57:29 +0800 Subject: [PATCH 07/12] Fix Keybindings initial --- src/poll.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/poll.cpp b/src/poll.cpp index d3d7beb..d6a19dc 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -200,6 +200,7 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards toml_table_t* card_obj = toml_table_at(top_array, i); if (card_obj) { CardKeybindings cardInfo = cards[i] = {}; + memset(&cardInfo.keybindings, 0, sizeof(cardInfo.keybindings)); cardInfo.card = readConfigString(card_obj, "CARD", ""); toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); From 9e0f973e8523a5dfabcc13c2f19b13ed517b085f Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 20:12:57 +0800 Subject: [PATCH 08/12] test --- src/poll.cpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/poll.cpp b/src/poll.cpp index d6a19dc..8b52693 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -196,11 +196,10 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards *leng = length; cards = new CardKeybindings[length]; - for (size_t i = 0; i < length; ++i) { + for (size_t top_i = 0; top_i < length; ++top_i) { toml_table_t* card_obj = toml_table_at(top_array, i); if (card_obj) { CardKeybindings cardInfo = cards[i] = {}; - memset(&cardInfo.keybindings, 0, sizeof(cardInfo.keybindings)); cardInfo.card = readConfigString(card_obj, "CARD", ""); toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); @@ -209,8 +208,10 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards return; } - for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.buttons); i++) - cardInfo.keybindings.buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; + Keybindings *keybind = &(cardInfo.keybindings); + memset (keybind, 0, sizeof (*keybind)); + for (size_t i = 0; i < COUNTOFARR (keybind->buttons); i++) + keybind->buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); @@ -220,32 +221,32 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards switch (value.type) { case keycode: - for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.keycodes); i++) { - if (cardInfo.keybindings.keycodes[i] == 0) { - cardInfo.keybindings.keycodes[i] = value.keycode; + for (size_t i = 0; i < COUNTOFARR (keybind->keycodes); i++) { + if (keybind->keycodes[i] == 0) { + keybind->keycodes[i] = value.keycode; break; } } break; case button: - for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.buttons); i++) { - if (cardInfo.keybindings.buttons[i] == SDL_CONTROLLER_BUTTON_INVALID) { - cardInfo.keybindings.buttons[i] = value.button; + for (size_t i = 0; i < COUNTOFARR (keybind->buttons); i++) { + if (keybind->buttons[i] == SDL_CONTROLLER_BUTTON_INVALID) { + keybind->buttons[i] = value.button; break; } } break; case axis: - for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.axis); i++) { - if (cardInfo.keybindings.axis[i] == 0) { - cardInfo.keybindings.axis[i] = value.axis; + for (size_t i = 0; i < COUNTOFARR (keybind->axis); i++) { + if (keybind->axis[i] == 0) { + keybind->axis[i] = value.axis; break; } } case scroll: - for (size_t i = 0; i < COUNTOFARR (cardInfo.keybindings.scroll); i++) { - if (cardInfo.keybindings.scroll[i] == 0) { - cardInfo.keybindings.scroll[i] = value.scroll; + for (size_t i = 0; i < COUNTOFARR (keybind->scroll); i++) { + if (keybind->scroll[i] == 0) { + keybind->scroll[i] = value.scroll; break; } } From ff9e5ec3ed218ca2becdde36854bb1d9db40dc63 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 20:18:08 +0800 Subject: [PATCH 09/12] test --- src/poll.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poll.cpp b/src/poll.cpp index 8b52693..7b25f47 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -197,7 +197,7 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards cards = new CardKeybindings[length]; for (size_t top_i = 0; top_i < length; ++top_i) { - toml_table_t* card_obj = toml_table_at(top_array, i); + toml_table_t* card_obj = toml_table_at(top_array, top_i); if (card_obj) { CardKeybindings cardInfo = cards[i] = {}; cardInfo.card = readConfigString(card_obj, "CARD", ""); From dab7a7abfe57abf3464cf23c556f05261701a3d7 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 20:21:43 +0800 Subject: [PATCH 10/12] test --- src/poll.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poll.cpp b/src/poll.cpp index 7b25f47..fffd1f8 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -199,7 +199,7 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards for (size_t top_i = 0; top_i < length; ++top_i) { toml_table_t* card_obj = toml_table_at(top_array, top_i); if (card_obj) { - CardKeybindings cardInfo = cards[i] = {}; + CardKeybindings cardInfo = cards[top_i] = {}; cardInfo.card = readConfigString(card_obj, "CARD", ""); toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); From 9e4ce5a59aa0b411553b162abf863d2ee249b0b8 Mon Sep 17 00:00:00 2001 From: RyuMiya Date: Mon, 25 Dec 2023 23:04:51 +0800 Subject: [PATCH 11/12] Fix --- src/bnusio.cpp | 9 ++++++--- src/patches/qr.cpp | 2 +- src/poll.cpp | 21 +++++++++++++-------- src/poll.h | 4 ++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/bnusio.cpp b/src/bnusio.cpp index 5fb34e3..7343042 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -39,8 +39,11 @@ Keybindings P2_LEFT_BLUE = {}; Keybindings P2_LEFT_RED = {}; Keybindings P2_RIGHT_RED = {}; Keybindings P2_RIGHT_BLUE = {}; -CardKeybindings *QRCODE_CARDS = new CardKeybindings[0]; -size_t QRCODE_CARDS_LENG = 0; +CardKeybindings *QRCODE_CARDS = new CardKeybindings[] { + {.keybindings = {.keycodes = {'W'}}, .card = "BNTTCNID1"}, + {.keybindings = {.keycodes = {'E'}}, .card = "BNTTCNID2"}, +}; +size_t QRCODE_CARDS_LENG = 2; namespace bnusio { #define RETURN_FALSE(returnType, functionName, ...) \ @@ -344,7 +347,7 @@ 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); + 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 7ec8da2..0780c2b 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -157,7 +157,7 @@ Update () { } else if (QRCODE_CARDS != nullptr) { for (size_t i = 0; i < QRCODE_CARDS_LENG; i++) { if (IsButtonTapped (QRCODE_CARDS[i].keybindings)) { - std::cout << "Insert" << std::endl; + std::cout << "Insert: " << QRCODE_CARDS[i].card << std::endl; gState = State::CopyWait; gMode = Mode::MultiCard; gCardNumber = QRCODE_CARDS[i].card; diff --git a/src/poll.cpp b/src/poll.cpp index fffd1f8..b9fb738 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -130,6 +130,7 @@ SDL_GameController *controllers[255]; void SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { + std::cout << "Name: " << key << std::endl; toml_array_t *array = toml_array_in (table, key); if (!array) { printWarning ("%s (%s): Cannot find array\n", __func__, key); @@ -143,6 +144,7 @@ SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); if (!bind.ok) break; + std::cout << "Key: " << bind.u.s << std::endl; ConfigValue value = StringToConfigEnum (bind.u.s); free (bind.u.s); @@ -185,22 +187,24 @@ SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { } void -SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards, size_t *leng) { +SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **cards, size_t *leng) { toml_array_t *top_array = toml_array_in (table, key); if (!top_array) { printWarning ("%s (%s): Cannot find array\n", __func__, key); return; } - size_t length = toml_array_nelem(top_array); - *leng = length; - cards = new CardKeybindings[length]; + *leng = toml_array_nelem(top_array); + *cards = (CardKeybindings*) calloc (*leng, sizeof(CardKeybindings)); - for (size_t top_i = 0; top_i < length; ++top_i) { + for (size_t top_i = 0; top_i < *leng; ++top_i) { toml_table_t* card_obj = toml_table_at(top_array, top_i); if (card_obj) { - CardKeybindings cardInfo = cards[top_i] = {}; - cardInfo.card = readConfigString(card_obj, "CARD", ""); + std::string read_card = readConfigString (card_obj, "CARD", ""); + (*cards)[top_i].card = (char*) calloc (read_card.size() + 1, sizeof(char)); + strcpy((*cards)[top_i].card, read_card.c_str()); + + std::cout << "Card: " << (*cards)[top_i].card << std::endl; toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); if (!array) { @@ -208,7 +212,7 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards return; } - Keybindings *keybind = &(cardInfo.keybindings); + Keybindings *keybind = &((*cards)[top_i].keybindings); memset (keybind, 0, sizeof (*keybind)); for (size_t i = 0; i < COUNTOFARR (keybind->buttons); i++) keybind->buttons[i] = SDL_CONTROLLER_BUTTON_INVALID; @@ -216,6 +220,7 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings *cards for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); if (!bind.ok) break; + std::cout << "Key: " << bind.u.s << std::endl; ConfigValue value = StringToConfigEnum (bind.u.s); free (bind.u.s); diff --git a/src/poll.h b/src/poll.h index 0415ab7..a2dbbcc 100644 --- a/src/poll.h +++ b/src/poll.h @@ -45,7 +45,7 @@ struct Keybindings { struct CardKeybindings { Keybindings keybindings; - std::string card; + char* card; }; enum EnumType { none, keycode, button, axis, scroll }; @@ -71,7 +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, CardKeybindings *keybind, size_t *leng); +void SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **keybind, size_t *leng); InternalButtonState GetInternalButtonState (Keybindings bindings); void SetRumble (int left, int right, int length); From ffcdade891823c2af5b35d70a12224023dc54c03 Mon Sep 17 00:00:00 2001 From: ptmaster <52694802+lty2008one@users.noreply.github.com> Date: Tue, 26 Dec 2023 08:29:15 +0800 Subject: [PATCH 12/12] Remove extra std::cout --- src/poll.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/poll.cpp b/src/poll.cpp index a492b53..b5efef1 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -193,7 +193,6 @@ SDL_GameController *controllers[255]; void SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { - std::cout << "Name: " << key << std::endl; toml_array_t *array = toml_array_in (table, key); if (!array) { printWarning ("%s (%s): Cannot find array\n", __func__, key); @@ -207,7 +206,6 @@ SetConfigValue (toml_table_t *table, const char *key, Keybindings *keybind) { for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); if (!bind.ok) break; - std::cout << "Key: " << bind.u.s << std::endl; ConfigValue value = StringToConfigEnum (bind.u.s); free (bind.u.s); @@ -267,8 +265,6 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **card (*cards)[top_i].card = (char*) calloc (read_card.size() + 1, sizeof(char)); strcpy((*cards)[top_i].card, read_card.c_str()); - std::cout << "Card: " << (*cards)[top_i].card << std::endl; - toml_array_t *array = toml_array_in (card_obj, "READ_KEY"); if (!array) { printWarning ("%s (%s): Cannot find READ_KEY in CARD_INFO\n", __func__, key); @@ -283,7 +279,6 @@ SetCardConfigValue (toml_table_t *table, const char *key, CardKeybindings **card for (size_t i = 0;; i++) { toml_datum_t bind = toml_string_at (array, i); if (!bind.ok) break; - std::cout << "Key: " << bind.u.s << std::endl; ConfigValue value = StringToConfigEnum (bind.u.s); free (bind.u.s);