From 7cba9cab9e76a7351b9ef299faa1cf611820af19 Mon Sep 17 00:00:00 2001 From: dosse91 Date: Tue, 24 Nov 2020 20:56:58 +0100 Subject: [PATCH] New, simpler, pattern finding code --- ME2KasumiCrashFix/main.cpp | 78 ++++++++++++++------------------------ README.md | 4 +- 2 files changed, 29 insertions(+), 53 deletions(-) diff --git a/ME2KasumiCrashFix/main.cpp b/ME2KasumiCrashFix/main.cpp index 025630b..232b090 100644 --- a/ME2KasumiCrashFix/main.cpp +++ b/ME2KasumiCrashFix/main.cpp @@ -1,69 +1,47 @@ #include #include -#define patternLength 32 +#define PATTERN_LENGTH 32 BYTE pattern[] = {0x83,0x3D,0xAC,0xA2,0x26,0x01,0x00,0x56,0x74,0x76,0xA1,0xBC,0xA7,0x26,0x01,0x83,0x78,0x54,0x00,0x74,0x6B,0x8B,0x40,0x54,0x83,0x78,0x40,0x00,0x74,0x62,0x8B,0x40}; -bool DataCompare(const BYTE* OpCodes, const BYTE* Mask, const char* StrMask) -{ - while (*StrMask) - { - if (*StrMask == 'x' && *OpCodes != *Mask) - return false; - ++StrMask; - ++OpCodes; - ++Mask; - } - return true; -} - -DWORD FindPattern(DWORD StartAddress, DWORD CodeLen, BYTE* Mask, char* StrMask, unsigned short ignore) -{ - unsigned short Ign = 0; - DWORD i = 0; - while (Ign <= ignore) - { - if (DataCompare((BYTE*)(StartAddress + i++), Mask, StrMask)) - ++Ign; - else if (i >= CodeLen) - return 0; +DWORD FindPattern(DWORD start, DWORD codeLength, BYTE* pattern, unsigned int patternLength) { + unsigned int matchLength = 0; + for (DWORD ptr = start; ptr <= start + codeLength - patternLength; ptr++) { + while (((BYTE*)ptr)[matchLength] == pattern[matchLength]) { + matchLength++; + if (matchLength == patternLength) return ptr; + } + matchLength = 0; } - return StartAddress + i - 1; + return NULL; } DWORD WINAPI Start(LPVOID lpParam) { - char patternMask[patternLength + 1]; - for (int i = 0; i < patternLength; i++) patternMask[i] = 'x'; - patternMask[patternLength] = 0; DWORD target; for (int count = 0; count < 10; count++) { - target = FindPattern(0x401000, 0xE52000, pattern, patternMask, 0); - if (target) - break; - else - Sleep(300); //pattern not found, try again in 300ms, max 10 times + target = FindPattern(0x401000, 0xE52000, pattern, PATTERN_LENGTH); + if(!target) Sleep(300); //pattern not found, try again in 300ms, max 10 times } if (target) { //pattern found, remove write protection and overwrite target code DWORD originalProtection; - VirtualProtect((void*)target, patternLength, PAGE_EXECUTE_READWRITE, &originalProtection); - BYTE* ptr = (BYTE*)target; - ptr[0x0F] = 0x85; - ptr[0x10] = 0xC0; - ptr[0x11] = 0x74; - ptr[0x12] = 0x6D; - ptr[0x13] = 0xEB; - ptr[0x14] = 0x6F; - ptr[0x84] = 0x83; - ptr[0x85] = 0x78; - ptr[0x86] = 0x54; - ptr[0x87] = 0x00; - ptr[0x88] = 0x74; - ptr[0x89] = 0xF6; - ptr[0x8A] = 0xEB; - ptr[0x8B] = 0x89; - VirtualProtect((void*)target, patternLength, originalProtection, NULL); + VirtualProtect((void*)target, PATTERN_LENGTH, PAGE_EXECUTE_READWRITE, &originalProtection); + ((BYTE*)target)[0x0F] = 0x85; + ((BYTE*)target)[0x10] = 0xC0; + ((BYTE*)target)[0x11] = 0x74; + ((BYTE*)target)[0x12] = 0x6D; + ((BYTE*)target)[0x13] = 0xEB; + ((BYTE*)target)[0x14] = 0x6F; + ((BYTE*)target)[0x84] = 0x83; + ((BYTE*)target)[0x85] = 0x78; + ((BYTE*)target)[0x86] = 0x54; + ((BYTE*)target)[0x87] = 0x00; + ((BYTE*)target)[0x88] = 0x74; + ((BYTE*)target)[0x89] = 0xF6; + ((BYTE*)target)[0x8A] = 0xEB; + ((BYTE*)target)[0x8B] = 0x89; + VirtualProtect((void*)target, PATTERN_LENGTH, originalProtection, NULL); return 0; } else { //pattern not found, display error diff --git a/README.md b/README.md index e2ead7a..78ad7a7 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,7 @@ This ASI mod fixes a crash that occurs on modern systems at the end of Kasumi's loyalty mission, when returning to Normandy after defeating the boss. -For use with Erik-JS's [Binkw32 proxy DLL](https://github.com/Erik-JS/masseffect-binkw32) - -__Consider this an EXPERIMENTAL fix: I'm currently doing a complete run of the game on Windows 10 to see if it causes problems elsewhere__ +__For use with Erik-JS's [Binkw32 proxy DLL](https://github.com/Erik-JS/masseffect-binkw32)__ It should work with all versions of the game: Steam, Origin, Disc and "DRM-free 😏".