Merge pull request #2 from lty2008one/Refactor

Added support for multiple QR code cards
This commit is contained in:
esuo1198
2024-01-10 01:20:44 +09:00
committed by GitHub
5 changed files with 122 additions and 4 deletions
+10
View File
@@ -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
+7
View File
@@ -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);
}
}
+23 -4
View File
@@ -6,17 +6,21 @@
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
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;
}
}
}
}
}
+75
View File
@@ -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;
+7
View File
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <toml.h>
#include <windows.h>
#include <iostream>
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);