diff --git a/dist/keyconfig.toml b/dist/keyconfig.toml index 71f0fdd..fc192d7 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 = ["W"] # Bind Keys +CARD = "BNTTCNID1" # Card Number + +# For Second User's QRCode +[[QRCODE_CARD]] +READ_KEY = ["E"] # Bind Keys +CARD = "BNTTCNID2" # Card Number + # ESCAPE F1 through F12 # ` 1 through 0 -= BACKSPACE # TAB QWERTYUIOP [ ] BACKSLASH diff --git a/src/bnusio.cpp b/src/bnusio.cpp index cab2448..7343042 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -39,6 +39,11 @@ Keybindings P2_LEFT_BLUE = {}; Keybindings P2_LEFT_RED = {}; Keybindings P2_RIGHT_RED = {}; Keybindings P2_RIGHT_BLUE = {}; +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, ...) \ @@ -342,6 +347,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..0780c2b 100644 --- a/src/patches/qr.cpp +++ b/src/patches/qr.cpp @@ -6,17 +6,21 @@ #include #include #include +#include extern GameVersion gameVersion; extern Keybindings QR_CARD_READ; extern Keybindings QR_DATA_READ; +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; +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) { @@ -73,6 +77,11 @@ 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) { + if (config) toml_free (config); + memcpy (dest, gCardNumber.c_str (), gCardNumber.size () + 1); + gState = State::AfterCopy1; + return gCardNumber.size () + 1; } else { std::string serial = ""; u16 type = 0; @@ -145,7 +154,17 @@ Update () { std::cout << "Insert" << std::endl; gState = State::CopyWait; gMode = Mode::Data; - } + } else if (QRCODE_CARDS != nullptr) { + for (size_t i = 0; i < QRCODE_CARDS_LENG; i++) { + if (IsButtonTapped (QRCODE_CARDS[i].keybindings)) { + std::cout << "Insert: " << QRCODE_CARDS[i].card << std::endl; + gState = State::CopyWait; + gMode = Mode::MultiCard; + gCardNumber = QRCODE_CARDS[i].card; + break; + } + } + } } } diff --git a/src/poll.cpp b/src/poll.cpp index 1c75896..b5efef1 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -247,6 +247,81 @@ 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 *top_array = toml_array_in (table, key); + if (!top_array) { + printWarning ("%s (%s): Cannot find array\n", __func__, key); + return; + } + + *leng = toml_array_nelem(top_array); + *cards = (CardKeybindings*) calloc (*leng, sizeof(CardKeybindings)); + + 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) { + 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()); + + 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; + } + + 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; + + 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 (keybind->keycodes); i++) { + if (keybind->keycodes[i] == 0) { + keybind->keycodes[i] = value.keycode; + break; + } + } + break; + case 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 (keybind->axis); i++) { + if (keybind->axis[i] == 0) { + keybind->axis[i] = value.axis; + break; + } + } + case scroll: + for (size_t i = 0; i < COUNTOFARR (keybind->scroll); i++) { + if (keybind->scroll[i] == 0) { + keybind->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..a2dbbcc 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 CardKeybindings { + Keybindings keybindings; + char* 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, CardKeybindings **keybind, size_t *leng); InternalButtonState GetInternalButtonState (Keybindings bindings); void SetRumble (int left, int right, int length);