From 3565da0040990b4a0ca58c3663f50b372ea8a518 Mon Sep 17 00:00:00 2001 From: Bottersnike Date: Fri, 14 Jul 2023 21:21:32 +0100 Subject: [PATCH] Restrict content of GrooveMaster --- src/micetools/dll/hooks/files.c | 14 ++++++++- src/micetools/dll/hooks/files.h | 1 + src/micetools/dll/hooks/system.c | 51 ++++++++++++++++++++++++++++++++ src/micetools/dll/util/_util.h | 3 ++ src/micetools/dll/util/misc.c | 5 ++-- src/micetools/lib/mice/da.c | 9 +++--- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/micetools/dll/hooks/files.c b/src/micetools/dll/hooks/files.c index 859e945..5717d76 100644 --- a/src/micetools/dll/hooks/files.c +++ b/src/micetools/dll/hooks/files.c @@ -348,7 +348,10 @@ BOOL WINAPI FakeReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRe } BOOL WINAPI FakeCloseHandle(HANDLE hObject) { - RemoveDataForHandle(hObject); + if ((DWORD_PTR)hObject & MICE_HANDLE_MASK) { + RemoveDataForHandle(hObject); + return TRUE; + } return TrueCloseHandle(hObject); } @@ -417,6 +420,14 @@ DWORD WINAPI FakeGetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffer) { return length; } +DWORD WINAPI FakeGetFileType(HANDLE hFile) { + if ((DWORD_PTR)hFile & MICE_HANDLE_MASK) { + // TODO: This is a lie + return FILE_TYPE_CHAR; + } + return TrueGetFileType(hFile); +} + void hook_io() { hook("Kernel32.dll", "DeviceIoControl", FakeDeviceIoControl, (void**)&TrueDeviceIoControl); @@ -429,6 +440,7 @@ void hook_io() { hook("Kernel32.dll", "WriteFile", FakeWriteFile, (void**)&TrueWriteFile); hook("Kernel32.dll", "ReadFile", FakeReadFile, (void**)&TrueReadFile); hook("Kernel32.dll", "GetFileSizeEx", FakeGetFileSizeEx, (void**)&TrueGetFileSizeEx); + hook("Kernel32.dll", "GetFileType", FakeGetFileType, (void**)&TrueGetFileType); // PathIsDirectory, PathFileExists, etc just call GetFileAttributes under the hood hook("Kernel32.dll", "DeleteFileA", FakeDeleteFileA, (void**)&TrueDeleteFileA); diff --git a/src/micetools/dll/hooks/files.h b/src/micetools/dll/hooks/files.h index 87c856a..5ef3b9a 100644 --- a/src/micetools/dll/hooks/files.h +++ b/src/micetools/dll/hooks/files.h @@ -46,6 +46,7 @@ BOOL(WINAPI* TrueWriteFile) _MICE_FILES BOOL(WINAPI* TrueReadFile)(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped); _MICE_FILES BOOL(WINAPI* TrueGetFileSizeEx)(HANDLE hFile, PLARGE_INTEGER lpFileSize); +_MICE_FILES DWORD(WINAPI* TrueGetFileType)(HANDLE hFile); _MICE_FILES BOOL(WINAPI* TrueCloseHandle)(HANDLE hObject); _MICE_FILES BOOL(WINAPI* TrueDeleteFileA)(LPCSTR lpFileName); _MICE_FILES BOOL(WINAPI* TrueDeleteFileW)(LPCWSTR lpFileName); diff --git a/src/micetools/dll/hooks/system.c b/src/micetools/dll/hooks/system.c index c430c0e..6c86df6 100644 --- a/src/micetools/dll/hooks/system.c +++ b/src/micetools/dll/hooks/system.c @@ -108,6 +108,57 @@ void hook_system() { const char* SystemVersion = "00691001\r\n"; const char* UpdateVersion = "0000\r\n"; + HANDLE hGM = CreateFileA("GrooveMaster.ini", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + BOOL bGmIs1P = FALSE; + if (hGM != INVALID_HANDLE_VALUE) { + CHAR line[256]; + DWORD_PTR lidx = 0; + DWORD nRead = 0; + while (1) { + // Literally not worth recovering; we're working with something stupid. + // Play stupid games, win stupid prizes. + if (lidx == _countof(line)) break; + + ReadFile(hGM, &(line[lidx]), 1, &nRead, NULL); + if (nRead != 1) break; + if (line[lidx] == '\n') { + if (strncmp(line, "1P_ONLY 1\r\n", lidx) == 0) { + bGmIs1P = TRUE; + break; + } + lidx = 0; + } else + lidx++; + } + CloseHandle(hGM); + } + + // You can have any configuration you want as long as it's black + if (bGmIs1P) { + const char* GrooveMaster1P = + ("# TODO: Get maimai_deliver not rebooting the cab\r\n" + "NO_DELIVER 1\r\n" + "# TODO: figure out how to do this without also needing a USB\r\n" + "USB_DL_DISABLE 1\r\n" + "# These are just easier to do via GM than a patch\r\n" + "1P_ONLY 1\r\n" + "NO_REBOOT 1\r\n" + "ROTATE 1\r\n"); + hook_file_with_buffer(L"GrooveMaster.ini", (LPBYTE)GrooveMaster1P, + strlen(GrooveMaster1P) + 1, GENERIC_READ); + } else { + const char* GrooveMaster = + ("# TODO: Get maimai_deliver not rebooting the cab\r\n" + "NO_DELIVER 1\r\n" + "# TODO: figure out how to do this without also needing a USB\r\n" + "USB_DL_DISABLE 1\r\n" + "# These are just easier to do via GM than a patch\r\n" + "NO_REBOOT 1\r\n" + "ROTATE 1\r\n"); + hook_file_with_buffer(L"GrooveMaster.ini", (LPBYTE)GrooveMaster, + strlen(GrooveMaster) + 1, GENERIC_READ); + } + const char* RingmasterPub = ("-----BEGIN PUBLIC KEY-----\r\n" "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH/y0LFuiVonnU+7fKLEOhfQoi\r\n" diff --git a/src/micetools/dll/util/_util.h b/src/micetools/dll/util/_util.h index 1530529..1b3068a 100644 --- a/src/micetools/dll/util/_util.h +++ b/src/micetools/dll/util/_util.h @@ -25,3 +25,6 @@ void* open_mapped_file(LPCSTR path, DWORD size, HANDLE* file, HANDLE* file_mappi #define char_lower(value) (('A' <= (value) && (value) <= 'Z') ? ((value) - 'A' + 'a') : (value)) #define char_upper(value) (('a' <= (value) && (value) <= 'z') ? ((value) - 'a' + 'A') : (value)) + +// Use the second tag bit to indicate a mice handle +#define MICE_HANDLE_MASK 0x00000002 diff --git a/src/micetools/dll/util/misc.c b/src/micetools/dll/util/misc.c index 6a2db10..221f80a 100644 --- a/src/micetools/dll/util/misc.c +++ b/src/micetools/dll/util/misc.c @@ -7,9 +7,6 @@ #include "../hooks/files.h" -// Use the second tag bit to indicate a mice handle -#define MICE_HANDLE_MASK 0x00000002 - void PrintStack(void) { unsigned int i; void* stack[100]; @@ -59,6 +56,7 @@ PVOID GetDataForHandle(HANDLE hObject) { DWORD_PTR dwIndex = (DWORD_PTR)hObject >> 2; PHANDLE_DATA found = MiceDAGet(g_handleData, dwIndex); if (found == NULL) return NULL; + return found->m_pData; } void SetDataForHandle(HANDLE hObject, PVOID pData, BOOL isHeap) { @@ -95,6 +93,7 @@ BOOL RemoveDataForHandle(HANDLE hObject) { found->m_pData = NULL; found->m_bOccupied = FALSE; } + return TRUE; } HANDLE GetDummyHandle() { diff --git a/src/micetools/lib/mice/da.c b/src/micetools/lib/mice/da.c index 1136ec7..af81ad4 100644 --- a/src/micetools/lib/mice/da.c +++ b/src/micetools/lib/mice/da.c @@ -6,11 +6,7 @@ static inline void _MiceDAGrow(PMICE_DA lpDa) { DWORD_PTR newSize = lpDa->m_Capacity * 2 + 1; if (lpDa->m_Array) { - LPVOID newArray = realloc(lpDa->m_Array, lpDa->m_Size * newSize); - if (newArray != lpDa->m_Array) { - memcpy(newArray, lpDa->m_Array, lpDa->m_Size * lpDa->m_Capacity); - } - lpDa->m_Array = newArray; + lpDa->m_Array = realloc(lpDa->m_Array, lpDa->m_Size * newSize); } else { lpDa->m_Array = malloc(lpDa->m_Size * newSize); } @@ -59,6 +55,9 @@ BOOL MiceDAGrowAndSet(PMICE_DA lpDa, DWORD_PTR dwIndex, LPVOID lpValue) { if (!lpDa || !lpValue) return FALSE; if (dwIndex >= lpDa->m_Length) { while (dwIndex >= lpDa->m_Capacity) _MiceDAGrow(lpDa); + if (lpDa->m_Length != dwIndex) + ZeroMemory(_MICE_DA_INDEX_P(lpDa, lpDa->m_Length), + lpDa->m_Size * (lpDa->m_Length - dwIndex)); lpDa->m_Length = dwIndex + 1; } _MICE_DA_COPY_INTO_DA(lpDa, dwIndex, lpValue);