Skip to content

Commit

Permalink
Fix binary patching and bump version.
Browse files Browse the repository at this point in the history
No idea what trick I am missing, but trying to patch
in DLL_ATTACH just crashes for me.
Instead use the existing runtime patching code.
Also add a little bit of sanity checking before patching.
  • Loading branch information
rdoeffinger committed May 8, 2020
1 parent 3d41d4c commit 4ae0608
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
37 changes: 36 additions & 1 deletion impl11/ddraw/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <cctype>

#include "joystick.h"
#include "XwaDrawTextHook.h"
#include "XwaDrawRadarHook.h"
#include "XwaDrawBracketHook.h"

// Wrapper to avoid undefined behaviour due to
// sign-extending char to int when passing to isspace.
Expand Down Expand Up @@ -221,7 +224,7 @@ Config::Config()
GetModuleFileNameA(NULL, name, sizeof(name));
int len = strlen(name);

bool isXWA = len >= 17 && _stricmp(name + len - 17, "xwingalliance.exe") == 0;
isXWA = len >= 17 && _stricmp(name + len - 17, "xwingalliance.exe") == 0;
isXWing = len >= 11 && _stricmp(name + len - 11, "xwing95.exe") == 0;
isTIE = len >= 9 && _stricmp(name + len - 9, "tie95.exe") == 0;
isXvT = len >= 11 && _stricmp(name + len - 11, "z_xvt__.exe") == 0 &&
Expand Down Expand Up @@ -334,6 +337,15 @@ Config::Config()
DisableProcessWindowsGhosting();
}

static bool patchCall(int address, void *dst) {
if (*(unsigned char*)(address) != 0xE8) return false;
DWORD old, dummy;
VirtualProtect((void *)address, 5, PAGE_READWRITE, &old);
*(int*)(address + 0x01) = (int)dst - (address + 0x05);
VirtualProtect((void *)address, 5, old, &dummy);
return true;
}

void Config::runAutopatch()
{
if (RuntimeAutoPatchDone) return;
Expand Down Expand Up @@ -379,5 +391,28 @@ void Config::runAutopatch()
*(unsigned *)0x4f2a8c = 0x3d000000u;
VirtualProtect((void *)0x4f2a8c, 4, old, &dummy);
}
if (AutoPatch >= 2 && isXWA && g_config.XWAMode) {
// RenderCharHook
patchCall(0x00450A47, RenderCharHook);

// ComputeMetricsHook
patchCall(0x00510385, ComputeMetricsHook);

// DrawRadarHook
DWORD old, dummy;
VirtualProtect((void *)(0x00434977 + 0x6), 0x00434995 - 0x00434977 + 4, PAGE_READWRITE, &old);
*(int*)(0x00434977 + 0x06) = (int)DrawRadarHook;
*(int*)(0x00434995 + 0x06) = (int)DrawRadarSelectedHook;
VirtualProtect((void *)(0x00434977 + 0x6), 0x00434995 - 0x00434977 + 4, old, &dummy);

// DrawBracketInFlightHook
patchCall(0x00503D46, DrawBracketInFlightHook);

// DrawBracketInFlightHook CMD
patchCall(0x00478E44, DrawBracketInFlightHook);

// DrawBracketMapHook
patchCall(0x00503CFE, DrawBracketMapHook);
}
FlushInstructionCache(GetCurrentProcess(), NULL, 0);
}
1 change: 1 addition & 0 deletions impl11/ddraw/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Config
float MouseSensitivity;
float KbdSensitivity;
bool XWAMode;
bool isXWA;
bool isTIE;
bool isXWing;
bool isXvT;
Expand Down
8 changes: 4 additions & 4 deletions impl11/ddraw/ddraw.rc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ END
//

1 VERSIONINFO
FILEVERSION 1,5,11
PRODUCTVERSION 1,5,11
FILEVERSION 1,5,12
PRODUCTVERSION 1,5,12
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -66,12 +66,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Jérémy Ansel"
VALUE "FileDescription", "Direct3D 11 implementation for X-Wing series"
VALUE "FileVersion", "1.5.11"
VALUE "FileVersion", "1.5.12"
VALUE "InternalName", "ddraw.dll"
VALUE "LegalCopyright", "Copyright (C) Jérémy Ansel 2014, Reimar Döffinger 2015-2020"
VALUE "OriginalFilename", "ddraw.dll"
VALUE "ProductName", "xwa_ddraw_d3d11"
VALUE "ProductVersion", "1.5.11"
VALUE "ProductVersion", "1.5.12"
END
END
BLOCK "VarFileInfo"
Expand Down
52 changes: 0 additions & 52 deletions impl11/ddraw/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,11 @@
#define STRICT
#include <windows.h>

#include "XwaDrawTextHook.h"
#include "XwaDrawRadarHook.h"
#include "XwaDrawBracketHook.h"

bool IsXwaExe()
{
char filename[4096];

if (GetModuleFileName(nullptr, filename, sizeof(filename)) == 0)
{
return false;
}

int length = strlen(filename);

if (length < 17)
{
return false;
}

return _stricmp(filename + length - 17, "xwingalliance.exe") == 0;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (IsXwaExe())
{
// RenderCharHook
*(unsigned char*)(0x00450A47 + 0x00) = 0xE8;
*(int*)(0x00450A47 + 0x01) = (int)RenderCharHook - (0x00450A47 + 0x05);

// ComputeMetricsHook
*(unsigned char*)(0x00510385 + 0x00) = 0xE8;
*(int*)(0x00510385 + 0x01) = (int)ComputeMetricsHook - (0x00510385 + 0x05);

// DrawRadarHook
*(int*)(0x00434977 + 0x06) = (int)DrawRadarHook;
*(int*)(0x00434995 + 0x06) = (int)DrawRadarSelectedHook;

// DrawBracketInFlightHook
*(unsigned char*)(0x00503D46 + 0x00) = 0xE8;
*(int*)(0x00503D46 + 0x01) = (int)DrawBracketInFlightHook - (0x00503D46 + 0x05);

// DrawBracketInFlightHook CMD
*(unsigned char*)(0x00478E44 + 0x00) = 0xE8;
*(int*)(0x00478E44 + 0x01) = (int)DrawBracketInFlightHook - (0x00478E44 + 0x05);

// DrawBracketMapHook
*(unsigned char*)(0x00503CFE + 0x00) = 0xE8;
*(int*)(0x00503CFE + 0x01) = (int)DrawBracketMapHook - (0x00503CFE + 0x05);
}

break;

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
Expand Down

0 comments on commit 4ae0608

Please sign in to comment.