Skip to content

Commit

Permalink
New, simpler, pattern finding code
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfintel committed Nov 24, 2020
1 parent 858e999 commit 7cba9ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
78 changes: 28 additions & 50 deletions ME2KasumiCrashFix/main.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,47 @@
#include <windows.h>
#include <winuser.h>

#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
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 😏".

Expand Down

0 comments on commit 7cba9ca

Please sign in to comment.