From cc469e1fd41ecf0031057c33df6ebb33b087480e Mon Sep 17 00:00:00 2001 From: BroGamer <64546358+BroGamer4256@users.noreply.github.com> Date: Sun, 18 Sep 2022 11:04:44 +1200 Subject: [PATCH] Add WaitTouch plugin --- PLUGINS.md | 26 +++++++++++++++++++ src/dllmain.c | 69 +++++++++++++-------------------------------------- src/helpers.h | 1 - 3 files changed, 43 insertions(+), 53 deletions(-) create mode 100644 PLUGINS.md diff --git a/PLUGINS.md b/PLUGINS.md new file mode 100644 index 0000000..5877b90 --- /dev/null +++ b/PLUGINS.md @@ -0,0 +1,26 @@ +### Guide for making additional plugins + +Plugins are just libraries with certain exported functions that go in the plugins folder. + +``` +void PreInit() +``` +Runs in dllmain, may run into loader locks. You can also just use your own dllmain for this but that involves writing out all the arguments. +``` +void Init() +``` +Runs on bngrw_Init, may be a bit late for some things but should generally allow functions which would cause loader locks to run fine if a bit late. +``` +void Exit() +``` +Runs on bnusio_Close, dispose of any data here. +``` +void WaitTouch(i32 (*callback) (i32, i32, u8[168], u64), u64 data) +``` +Runs on bngrw_reqWaitTouch. Call the callback like so when you wish to have a card scanned. +``` +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 }; +memcpy (cardData + 0x2C, chipId, 33); +memcpy (cardData + 0x50, accessCode, 21); +callback(0, 0, cardData, data); +``` \ No newline at end of file diff --git a/src/dllmain.c b/src/dllmain.c index b85e3c4..9e13a9a 100644 --- a/src/dllmain.c +++ b/src/dllmain.c @@ -11,16 +11,18 @@ char accessCode1[21] = "00000000000000000001"; char accessCode2[21] = "00000000000000000002"; char chipId1[33] = "00000000000000000000000000000001"; char chipId2[33] = "00000000000000000000000000000002"; - -char *server = "https://divamodarchive.com"; +char *server = "https://divamodarchive.com"; 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; +HMODULE plugins[255] = { 0 }; #define ON_HIT(bind) IsButtonTapped (bind) ? drumMax == drumMin ? drumMax : (u16)(rand () % drumMax + drumMin) : 0 @@ -136,61 +138,17 @@ u32 __stdcall bnusio_GetSwIn () { } i64 __stdcall bnusio_Close () { - wchar_t path[MAX_PATH]; - GetModuleFileNameW (NULL, path, MAX_PATH); - *wcsrchr (path, '\\') = '\0'; - SetCurrentDirectoryW (path); - - WIN32_FIND_DATAW fd; - HANDLE hFind = FindFirstFileW (L"plugins/*.dll", &fd); - if (hFind != INVALID_HANDLE_VALUE) { - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; - wchar_t filePath[MAX_PATH]; - wcscpy (filePath, path); - wcscat (filePath, L"/plugins/"); - wcscat (filePath, fd.cFileName); - HMODULE hModule = LoadLibraryW (filePath); - if (!hModule) { - wchar_t buf[128]; - swprintf (buf, 128, L"Failed to load plugin %d", GetLastError ()); - MessageBoxW (NULL, buf, fd.cFileName, MB_ICONERROR); - } else { - FARPROC exitEvent = GetProcAddress (hModule, "Exit"); - if (exitEvent) ((event *)exitEvent) (); - } - } while (FindNextFileW (hFind, &fd)); - FindClose (hFind); + for (int i = 0; plugins[i] != 0; i++) { + FARPROC exitEvent = GetProcAddress (plugins[i], "Exit"); + if (exitEvent) ((event *)exitEvent) (); } return 0; } HOOK_DYNAMIC (u64, __stdcall, bngrw_Init) { - wchar_t path[MAX_PATH]; - GetModuleFileNameW (NULL, path, MAX_PATH); - *wcsrchr (path, '\\') = '\0'; - SetCurrentDirectoryW (path); - - WIN32_FIND_DATAW fd; - HANDLE hFind = FindFirstFileW (L"plugins/*.dll", &fd); - if (hFind != INVALID_HANDLE_VALUE) { - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; - wchar_t filePath[MAX_PATH]; - wcscpy (filePath, path); - wcscat (filePath, L"/plugins/"); - wcscat (filePath, fd.cFileName); - HMODULE hModule = LoadLibraryW (filePath); - if (!hModule) { - wchar_t buf[128]; - swprintf (buf, 128, L"Failed to load plugin %d", GetLastError ()); - MessageBoxW (NULL, buf, fd.cFileName, MB_ICONERROR); - } else { - FARPROC initEvent = GetProcAddress (hModule, "Init"); - if (initEvent) ((event *)initEvent) (); - } - } while (FindNextFileW (hFind, &fd)); - FindClose (hFind); + for (int i = 0; plugins[i] != 0; i++) { + FARPROC initEvent = GetProcAddress (plugins[i], "Init"); + if (initEvent) ((event *)initEvent) (); } return 0; } @@ -206,6 +164,10 @@ HOOK_DYNAMIC (i32, __stdcall, bngrw_reqWaitTouch, u32 a1, i32 a2, u32 a3, callba waitingForTouch = true; touchCallback = callback; touchData = a5; + for (int i = 0; plugins[i] != 0; i++) { + FARPROC touchEvent = GetProcAddress (plugins[i], "WaitTouch"); + if (touchEvent) ((waitTouchEvent *)touchEvent) (callback, a5); + } return 1; } @@ -237,6 +199,7 @@ i32 __stdcall DllMain (HMODULE mod, DWORD cause, void *ctx) { SetCurrentDirectoryW (path); WIN32_FIND_DATAW fd; + int i = 0; HANDLE hFind = FindFirstFileW (L"plugins/*.dll", &fd); if (hFind != INVALID_HANDLE_VALUE) { do { @@ -251,6 +214,8 @@ i32 __stdcall DllMain (HMODULE mod, DWORD cause, void *ctx) { swprintf (buf, 128, L"Failed to load plugin %d", GetLastError ()); MessageBoxW (NULL, buf, fd.cFileName, MB_ICONERROR); } else { + plugins[i] = hModule; + i++; FARPROC preInitEvent = GetProcAddress (hModule, "PreInit"); if (preInitEvent) ((event *)preInitEvent) (); } diff --git a/src/helpers.h b/src/helpers.h index f52f289..ed402f4 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -14,7 +14,6 @@ typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; -typedef void event (); #ifdef DEFINE_GUID #undef DEFINE_GUID