2014-09-23 03:35:43 +06:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-04-22 04:00:36 +05:00
|
|
|
#define WINVER 0x0501
|
|
|
|
#define _WIN32_WINNT 0x0501
|
2014-09-23 03:35:43 +06:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <Shlwapi.h>
|
2015-04-22 04:00:36 +05:00
|
|
|
#include <ShlObj.h>
|
2019-06-29 02:14:32 +05:00
|
|
|
#include "Utils/MemoryMgr.h"
|
|
|
|
#include "Utils/Patterns.h"
|
2024-01-28 19:26:24 +05:00
|
|
|
#include "Utils/ScopedUnprotect.hpp"
|
2014-09-23 03:35:43 +06:00
|
|
|
|
2017-09-28 02:12:05 +05:00
|
|
|
#include "Common_ddraw.h"
|
2021-07-27 02:52:20 +05:00
|
|
|
#include "Desktop.h"
|
2017-09-19 01:13:02 +05:00
|
|
|
|
2017-09-18 16:46:14 +05:00
|
|
|
#pragma comment(lib, "shlwapi.lib")
|
|
|
|
|
2014-09-23 03:35:43 +06:00
|
|
|
extern "C" HRESULT WINAPI DirectDrawCreateEx(GUID FAR *lpGUID, LPVOID *lplpDD, REFIID iid, IUnknown FAR *pUnkOuter)
|
|
|
|
{
|
|
|
|
static HRESULT (WINAPI *pDirectDrawCreateEx)(GUID FAR*, LPVOID*, REFIID, IUnknown FAR*);
|
2015-05-02 03:46:46 +05:00
|
|
|
if ( pDirectDrawCreateEx == nullptr )
|
2014-09-23 03:35:43 +06:00
|
|
|
{
|
|
|
|
wchar_t wcSystemPath[MAX_PATH];
|
|
|
|
GetSystemDirectoryW(wcSystemPath, MAX_PATH);
|
|
|
|
PathAppendW(wcSystemPath, L"ddraw.dll");
|
|
|
|
|
|
|
|
HMODULE hLib = LoadLibraryW(wcSystemPath);
|
|
|
|
pDirectDrawCreateEx = (HRESULT(WINAPI*)(GUID FAR*, LPVOID*, REFIID, IUnknown FAR*))GetProcAddress(hLib, "DirectDrawCreateEx");
|
|
|
|
}
|
2014-10-12 06:55:16 +06:00
|
|
|
return pDirectDrawCreateEx(lpGUID, lplpDD, iid, pUnkOuter);
|
|
|
|
}
|
|
|
|
|
2015-04-22 04:00:36 +05:00
|
|
|
char** ppUserFilesDir;
|
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
void InjectHooks()
|
2014-10-12 06:55:16 +06:00
|
|
|
{
|
2015-06-25 04:19:46 +05:00
|
|
|
static char aNoDesktopMode[64];
|
|
|
|
|
2021-07-27 02:52:20 +05:00
|
|
|
const auto [width, height] = GetDesktopResolution();
|
|
|
|
sprintf_s(aNoDesktopMode, "Cannot find %ux%ux32 video mode", width, height);
|
2015-06-25 04:19:46 +05:00
|
|
|
|
2017-09-19 02:11:19 +05:00
|
|
|
std::unique_ptr<ScopedUnprotect::Unprotect> Protect = ScopedUnprotect::UnprotectSectionOrFullModule( GetModuleHandle( nullptr ), ".text" );
|
|
|
|
|
2024-04-06 18:14:58 +05:00
|
|
|
if (*(DWORD*)Memory::DynBaseAddress(0x5C1E75) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// III 1.0
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x580C16);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_III_10( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2024-04-06 18:14:58 +05:00
|
|
|
else if (*(DWORD*)Memory::DynBaseAddress(0x5C2135) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// III 1.1
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x580F66);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_III_11( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2024-04-06 18:14:58 +05:00
|
|
|
else if (*(DWORD*)Memory::DynBaseAddress(0x5C6FD5) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// III Steam
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x580E66);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_III_Steam( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:14:58 +05:00
|
|
|
else if (*(DWORD*)Memory::DynBaseAddress(0x667BF5) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// VC 1.0
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x6022AA);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_VC_10( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2024-04-06 18:14:58 +05:00
|
|
|
else if (*(DWORD*)Memory::DynBaseAddress(0x667C45) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// VC 1.1
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x60228A);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_VC_11( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2024-04-06 18:14:58 +05:00
|
|
|
else if (*(DWORD*)Memory::DynBaseAddress(0x666BA5) == 0xB85548EC)
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
// VC Steam
|
2024-04-06 18:14:58 +05:00
|
|
|
ppUserFilesDir = (char**)Memory::DynBaseAddress(0x601ECA);
|
2021-07-27 02:52:20 +05:00
|
|
|
Common::Patches::DDraw_VC_Steam( width, height, aNoDesktopMode );
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2017-09-19 02:11:19 +05:00
|
|
|
|
|
|
|
Common::Patches::DDraw_Common();
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
|
|
|
|
2017-09-18 20:19:56 +05:00
|
|
|
static bool rwcsegUnprotected = false;
|
|
|
|
|
|
|
|
static void ProcHook()
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
static bool bPatched = false;
|
|
|
|
if ( !bPatched )
|
2015-04-22 04:00:36 +05:00
|
|
|
{
|
2015-06-25 04:19:46 +05:00
|
|
|
bPatched = true;
|
2015-05-02 03:27:56 +05:00
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
InjectHooks();
|
2017-09-18 20:19:56 +05:00
|
|
|
|
|
|
|
if ( !rwcsegUnprotected )
|
|
|
|
{
|
2017-09-19 01:13:02 +05:00
|
|
|
rwcsegUnprotected = Common::Patches::FixRwcseg_Patterns();
|
2017-09-18 20:19:56 +05:00
|
|
|
}
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2017-09-18 20:19:56 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID (WINAPI* pOrgGetStartupInfoA)(LPSTARTUPINFOA);
|
|
|
|
VOID WINAPI GetStartupInfoA_Hook(LPSTARTUPINFOA lpStartupInfo)
|
|
|
|
{
|
|
|
|
ProcHook();
|
2015-06-25 04:19:46 +05:00
|
|
|
pOrgGetStartupInfoA(lpStartupInfo);
|
|
|
|
}
|
2015-05-02 03:27:56 +05:00
|
|
|
|
2017-09-18 20:19:56 +05:00
|
|
|
static uint8_t orgCode[5];
|
|
|
|
static decltype(SystemParametersInfoA)* pOrgSystemParametersInfoA;
|
|
|
|
BOOL WINAPI SystemParametersInfoA_OverwritingHook( UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni )
|
|
|
|
{
|
|
|
|
ProcHook();
|
|
|
|
Memory::VP::Patch( pOrgSystemParametersInfoA, { orgCode[0], orgCode[1], orgCode[2], orgCode[3], orgCode[4] } );
|
|
|
|
return pOrgSystemParametersInfoA( uiAction, uiParam, pvParam, fWinIni );
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FixRwcseg_Header()
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
2017-04-28 01:15:40 +05:00
|
|
|
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
2017-09-18 20:19:56 +05:00
|
|
|
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)((DWORD_PTR)hInstance + ((PIMAGE_DOS_HEADER)hInstance)->e_lfanew);
|
2015-05-02 03:27:56 +05:00
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
// Give _rwcseg proper access rights
|
2017-09-18 20:19:56 +05:00
|
|
|
PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(ntHeader);
|
2015-04-22 04:00:36 +05:00
|
|
|
|
2016-09-12 03:11:17 +05:00
|
|
|
for ( SIZE_T i = 0, j = ntHeader->FileHeader.NumberOfSections; i < j; i++, pSection++ )
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
|
|
|
if ( *(uint64_t*)(pSection->Name) == 0x006765736377725F ) // _rwcseg
|
2015-04-22 04:00:36 +05:00
|
|
|
{
|
2015-06-25 04:19:46 +05:00
|
|
|
DWORD dwProtect;
|
2017-09-18 20:19:56 +05:00
|
|
|
VirtualProtect((LPVOID)((DWORD_PTR)hInstance + pSection->VirtualAddress), pSection->Misc.VirtualSize, PAGE_EXECUTE_READ, &dwProtect);
|
2017-04-28 01:12:48 +05:00
|
|
|
|
2017-05-17 03:36:09 +05:00
|
|
|
DWORD Characteristics = pSection->Characteristics;
|
|
|
|
if ( (Characteristics & IMAGE_SCN_CNT_CODE) == 0 )
|
2017-04-28 01:12:48 +05:00
|
|
|
{
|
2017-05-17 03:36:09 +05:00
|
|
|
Characteristics |= IMAGE_SCN_CNT_CODE;
|
|
|
|
Memory::VP::Patch( &ntHeader->OptionalHeader.SizeOfCode, ntHeader->OptionalHeader.SizeOfCode + pSection->Misc.VirtualSize );
|
2017-04-28 01:12:48 +05:00
|
|
|
}
|
2017-05-17 03:36:09 +05:00
|
|
|
if ( (Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) != 0 )
|
2017-04-28 01:12:48 +05:00
|
|
|
{
|
2017-05-17 03:36:09 +05:00
|
|
|
Characteristics &= ~(IMAGE_SCN_CNT_INITIALIZED_DATA);
|
|
|
|
Memory::VP::Patch( &ntHeader->OptionalHeader.SizeOfInitializedData, ntHeader->OptionalHeader.SizeOfInitializedData - pSection->Misc.VirtualSize );
|
2017-04-28 01:12:48 +05:00
|
|
|
}
|
2017-05-17 03:36:09 +05:00
|
|
|
if ( (Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0 )
|
2017-04-28 01:12:48 +05:00
|
|
|
{
|
2017-05-17 03:36:09 +05:00
|
|
|
Characteristics &= ~(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
|
|
|
|
Memory::VP::Patch( &ntHeader->OptionalHeader.SizeOfUninitializedData, ntHeader->OptionalHeader.SizeOfUninitializedData - pSection->Misc.VirtualSize );
|
2017-04-28 01:12:48 +05:00
|
|
|
}
|
2017-05-17 03:36:09 +05:00
|
|
|
Memory::VP::Patch( &pSection->Characteristics, Characteristics );
|
2017-09-18 20:19:56 +05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PatchIAT()
|
|
|
|
{
|
|
|
|
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
|
|
|
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)((DWORD_PTR)hInstance + ((PIMAGE_DOS_HEADER)hInstance)->e_lfanew);
|
2015-04-22 04:00:36 +05:00
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
// Find IAT
|
2017-09-18 20:19:56 +05:00
|
|
|
PIMAGE_IMPORT_DESCRIPTOR pImports = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD_PTR)hInstance + ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
|
2015-05-02 03:27:56 +05:00
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
// Find kernel32.dll
|
|
|
|
for ( ; pImports->Name != 0; pImports++ )
|
|
|
|
{
|
2017-09-18 20:19:56 +05:00
|
|
|
if ( !_stricmp((const char*)((DWORD_PTR)hInstance + pImports->Name), "KERNEL32.DLL") )
|
2015-04-22 04:00:36 +05:00
|
|
|
{
|
2017-09-19 01:13:02 +05:00
|
|
|
if ( pImports->OriginalFirstThunk != 0 )
|
2015-06-25 04:19:46 +05:00
|
|
|
{
|
2017-09-19 01:13:02 +05:00
|
|
|
PIMAGE_IMPORT_BY_NAME* pFunctions = (PIMAGE_IMPORT_BY_NAME*)((DWORD_PTR)hInstance + pImports->OriginalFirstThunk);
|
2015-06-25 04:19:46 +05:00
|
|
|
|
2017-09-19 01:13:02 +05:00
|
|
|
// kernel32.dll found, find GetStartupInfoA
|
|
|
|
for ( ptrdiff_t j = 0; pFunctions[j] != nullptr; j++ )
|
|
|
|
{
|
|
|
|
if ( !strcmp((const char*)((DWORD_PTR)hInstance + pFunctions[j]->Name), "GetStartupInfoA") )
|
|
|
|
{
|
|
|
|
// Overwrite the address with the address to a custom GetStartupInfoA
|
|
|
|
DWORD dwProtect[2];
|
|
|
|
DWORD_PTR* pAddress = &((DWORD_PTR*)((DWORD_PTR)hInstance + pImports->FirstThunk))[j];
|
|
|
|
|
|
|
|
VirtualProtect(pAddress, sizeof(DWORD_PTR), PAGE_EXECUTE_READWRITE, &dwProtect[0]);
|
|
|
|
pOrgGetStartupInfoA = **(VOID(WINAPI**)(LPSTARTUPINFOA))pAddress;
|
|
|
|
*pAddress = (DWORD_PTR)GetStartupInfoA_Hook;
|
|
|
|
VirtualProtect(pAddress, sizeof(DWORD_PTR), dwProtect[0], &dwProtect[1]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 04:00:36 +05:00
|
|
|
}
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
2017-09-18 20:19:56 +05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PatchIAT_ByPointers()
|
|
|
|
{
|
2024-11-29 00:41:17 +05:00
|
|
|
using namespace Memory::VP;
|
|
|
|
|
2017-09-18 20:19:56 +05:00
|
|
|
pOrgSystemParametersInfoA = SystemParametersInfoA;
|
|
|
|
memcpy( orgCode, pOrgSystemParametersInfoA, sizeof(orgCode) );
|
2024-11-29 00:41:17 +05:00
|
|
|
InjectHook( pOrgSystemParametersInfoA, SystemParametersInfoA_OverwritingHook, HookType::Jump );
|
2017-09-18 20:19:56 +05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ApplyDDrawHooks()
|
|
|
|
{
|
|
|
|
rwcsegUnprotected = FixRwcseg_Header();
|
|
|
|
|
|
|
|
bool getStartupInfoHooked = PatchIAT();
|
|
|
|
if ( !getStartupInfoHooked )
|
|
|
|
{
|
|
|
|
PatchIAT_ByPointers();
|
|
|
|
}
|
2015-06-25 04:19:46 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(hinstDLL);
|
|
|
|
UNREFERENCED_PARAMETER(lpvReserved);
|
2015-04-22 04:00:36 +05:00
|
|
|
|
2015-06-25 04:19:46 +05:00
|
|
|
if ( fdwReason == DLL_PROCESS_ATTACH )
|
|
|
|
{
|
2017-09-18 20:19:56 +05:00
|
|
|
ApplyDDrawHooks();
|
2015-04-22 04:00:36 +05:00
|
|
|
}
|
|
|
|
|
2014-09-23 03:35:43 +06:00
|
|
|
return TRUE;
|
2018-04-20 23:33:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport)
|
|
|
|
uint32_t GetBuildNumber()
|
|
|
|
{
|
|
|
|
return (SILENTPATCH_REVISION_ID << 8) | SILENTPATCH_BUILD_ID;
|
2014-09-23 03:35:43 +06:00
|
|
|
}
|