Skip to content

Commit

Permalink
Apply AStyle.
Browse files Browse the repository at this point in the history
Fix bug in resources re-creation (RSManager::releaseResources).
Make use of smart pointers.
Fix logger double new line (removed '\n' from all log calls).
Remove GFWL stuff.
  • Loading branch information
Robert Krawczyk committed Aug 2, 2015
1 parent b915690 commit 8bb013b
Show file tree
Hide file tree
Showing 29 changed files with 16,884 additions and 16,632 deletions.
29,910 changes: 14,955 additions & 14,955 deletions DSFix/AreaTex.h

Large diffs are not rendered by default.

136 changes: 66 additions & 70 deletions DSFix/Detouring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ bool timingIntroMode = false;

DWORD WINAPI DetouredSleepEx(DWORD dwMilliseconds, BOOL bAlertable)
{
//SDLOG(12, "T %6lu: Detouring: Sleep for %lu ms\n", GetCurrentThreadId(), dwMilliseconds);
//return TrueSleepEx(dwMilliseconds, bAlertable);
return 0;
//SDLOG(12, "T %6lu: Detouring: Sleep for %lu ms", GetCurrentThreadId(), dwMilliseconds);
//return TrueSleepEx(dwMilliseconds, bAlertable);
return 0;
}

#include <mmsystem.h>
Expand All @@ -19,122 +19,118 @@ static DWORD timeIncrease;
DWORD(WINAPI * TrueTimeGetTime)(void) = timeGetTime;
DWORD WINAPI DetouredTimeGetTime()
{
SDLOG(13, "T %6lu: Detouring: TimeGetTime - real: %10lu, returned: %10lu\n", GetCurrentThreadId(), TrueTimeGetTime(), TrueTimeGetTime() + timeIncrease);
//timeIncrease += 16;
return TrueTimeGetTime() + timeIncrease;
SDLOG(13, "T %6lu: Detouring: TimeGetTime - real: %10lu, returned: %10lu", GetCurrentThreadId(), TrueTimeGetTime(), TrueTimeGetTime() + timeIncrease);
//timeIncrease += 16;
return TrueTimeGetTime() + timeIncrease;
}

static LARGE_INTEGER perfCountIncrease, countsPerSec;
BOOL(WINAPI * TrueQueryPerformanceCounter)(_Out_ LARGE_INTEGER *lpPerformanceCount) = QueryPerformanceCounter;
BOOL WINAPI DetouredQueryPerformanceCounter(_Out_ LARGE_INTEGER *lpPerformanceCount)
{
void *traces[128];
DWORD hash;
int captured = CaptureStackBackTrace(0, 128, traces, &hash);
SDLOG(14, "T %6lu: Detouring: QueryPerformanceCounter, stack depth %3d, hash %20ul\n", GetCurrentThreadId(), captured, hash);
BOOL ret = TrueQueryPerformanceCounter(lpPerformanceCount);
if (timingIntroMode && captured >= 1)
{
perfCountIncrease.QuadPart += countsPerSec.QuadPart / 50;
}
lpPerformanceCount->QuadPart += perfCountIncrease.QuadPart;
return ret;
void *traces[128];
DWORD hash;
int captured = CaptureStackBackTrace(0, 128, traces, &hash);
SDLOG(14, "T %6lu: Detouring: QueryPerformanceCounter, stack depth %3d, hash %20ul", GetCurrentThreadId(), captured, hash);
BOOL ret = TrueQueryPerformanceCounter(lpPerformanceCount);
if (timingIntroMode && captured >= 1) {
perfCountIncrease.QuadPart += countsPerSec.QuadPart / 50;
}
lpPerformanceCount->QuadPart += perfCountIncrease.QuadPart;
return ret;
}

typedef HRESULT(WINAPI * D3DXCreateTexture_FNType)(_In_ LPDIRECT3DDEVICE9 pDevice, _In_ UINT Width, _In_ UINT Height, _In_ UINT MipLevels, _In_ DWORD Usage, _In_ D3DFORMAT Format, _In_ D3DPOOL Pool, _Out_ LPDIRECT3DTEXTURE9 *ppTexture);
D3DXCreateTexture_FNType TrueD3DXCreateTexture = D3DXCreateTexture;
HRESULT WINAPI DetouredD3DXCreateTexture(_In_ LPDIRECT3DDEVICE9 pDevice, _In_ UINT Width, _In_ UINT Height, _In_ UINT MipLevels, _In_ DWORD Usage, _In_ D3DFORMAT Format, _In_ D3DPOOL Pool, _Out_ LPDIRECT3DTEXTURE9 *ppTexture)
{
SDLOG(4, "DetouredD3DXCreateTexture\n");
HRESULT res = TrueD3DXCreateTexture(pDevice, Width, Height, MipLevels, Usage, Format, Pool, ppTexture);
return res;
SDLOG(4, "DetouredD3DXCreateTexture");
HRESULT res = TrueD3DXCreateTexture(pDevice, Width, Height, MipLevels, Usage, Format, Pool, ppTexture);
return res;
}

typedef HRESULT(WINAPI * D3DXCreateTextureFromFileInMemory_FNType)(_In_ LPDIRECT3DDEVICE9 pDevice, _In_ LPCVOID pSrcData, _In_ UINT SrcDataSize, _Out_ LPDIRECT3DTEXTURE9 *ppTexture);
D3DXCreateTextureFromFileInMemory_FNType TrueD3DXCreateTextureFromFileInMemory = D3DXCreateTextureFromFileInMemory;
HRESULT WINAPI DetouredD3DXCreateTextureFromFileInMemory(_In_ LPDIRECT3DDEVICE9 pDevice, _In_ LPCVOID pSrcData, _In_ UINT SrcDataSize, _Out_ LPDIRECT3DTEXTURE9 *ppTexture)
{
SDLOG(4, "DetouredD3DXCreateTextureFromFileInMemory\n");
HRESULT res = TrueD3DXCreateTextureFromFileInMemory(pDevice, pSrcData, SrcDataSize, ppTexture);
RSManager::get().registerD3DXCreateTextureFromFileInMemory(pSrcData, SrcDataSize, *ppTexture);
return res;
SDLOG(4, "DetouredD3DXCreateTextureFromFileInMemory");
HRESULT res = TrueD3DXCreateTextureFromFileInMemory(pDevice, pSrcData, SrcDataSize, ppTexture);
RSManager::get().registerD3DXCreateTextureFromFileInMemory(pSrcData, SrcDataSize, *ppTexture);
return res;
}

typedef HRESULT(WINAPI * D3DXCreateTextureFromFileInMemoryEx_FNType)(LPDIRECT3DDEVICE9 pDevice, LPCVOID pSrcData, UINT SrcDataSize, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture);
D3DXCreateTextureFromFileInMemoryEx_FNType TrueD3DXCreateTextureFromFileInMemoryEx = D3DXCreateTextureFromFileInMemoryEx;
HRESULT WINAPI DetouredD3DXCreateTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 pDevice, LPCVOID pSrcData, UINT SrcDataSize, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter,
DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture)
DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture)
{
SDLOG(4, "DetouredD3DXCreateTextureFromFileInMemoryEx\n");
HRESULT res = RSManager::get().redirectD3DXCreateTextureFromFileInMemoryEx(pDevice, pSrcData, SrcDataSize, Width, Height, MipLevels, Usage, Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppTexture);
RSManager::get().registerD3DXCreateTextureFromFileInMemory(pSrcData, SrcDataSize, *ppTexture);
return res;
SDLOG(4, "DetouredD3DXCreateTextureFromFileInMemoryEx");
HRESULT res = RSManager::get().redirectD3DXCreateTextureFromFileInMemoryEx(pDevice, pSrcData, SrcDataSize, Width, Height, MipLevels, Usage, Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppTexture);
RSManager::get().registerD3DXCreateTextureFromFileInMemory(pSrcData, SrcDataSize, *ppTexture);
return res;
}

typedef HRESULT(WINAPI * D3DXCompileShader_FNType)(_In_ LPCSTR pSrcData, _In_ UINT srcDataLen, _In_ const D3DXMACRO *pDefines, _In_ LPD3DXINCLUDE pInclude, _In_ LPCSTR pFunctionName, _In_ LPCSTR pProfile, _In_ DWORD Flags, _Out_ LPD3DXBUFFER *ppShader, _Out_ LPD3DXBUFFER *ppErrorMsgs, _Out_ LPD3DXCONSTANTTABLE *ppConstantTable);
D3DXCompileShader_FNType TrueD3DXCompileShader = D3DXCompileShader;
HRESULT WINAPI DetouredD3DXCompileShader(_In_ LPCSTR pSrcData, _In_ UINT srcDataLen, _In_ const D3DXMACRO *pDefines, _In_ LPD3DXINCLUDE pInclude, _In_ LPCSTR pFunctionName, _In_ LPCSTR pProfile,
_In_ DWORD Flags, _Out_ LPD3DXBUFFER *ppShader, _Out_ LPD3DXBUFFER *ppErrorMsgs, _Out_ LPD3DXCONSTANTTABLE *ppConstantTable)
_In_ DWORD Flags, _Out_ LPD3DXBUFFER *ppShader, _Out_ LPD3DXBUFFER *ppErrorMsgs, _Out_ LPD3DXCONSTANTTABLE *ppConstantTable)
{
HRESULT res = TrueD3DXCompileShader(pSrcData, srcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
RSManager::get().registerD3DXCompileShader(pSrcData, srcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
return res;
HRESULT res = TrueD3DXCompileShader(pSrcData, srcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
RSManager::get().registerD3DXCompileShader(pSrcData, srcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
return res;
}

static LANGID s_langid;

LANGID(WINAPI* TrueGetUserDefaultLangID)(void);
LANGID WINAPI DetouredGetUserDefaultLangID(void)
{
if (s_langid)
return s_langid;
else
return TrueGetUserDefaultLangID();
if (s_langid)
return s_langid;
else
return TrueGetUserDefaultLangID();
}

void earlyDetour()
{
QueryPerformanceFrequency(&countsPerSec);
QueryPerformanceFrequency(&countsPerSec);

MH_Initialize();
MH_Initialize();

MH_CreateHook(Direct3DCreate9, hkDirect3DCreate9, reinterpret_cast<void**>(&oDirect3DCreate9));
MH_EnableHook(Direct3DCreate9);
MH_CreateHook(Direct3DCreate9, hkDirect3DCreate9, reinterpret_cast<void**>(&oDirect3DCreate9));
MH_EnableHook(Direct3DCreate9);
}

void startDetour()
{
MH_Initialize();

if (Settings::get().getSkipIntro()) {
MH_CreateHook(QueryPerformanceCounter, DetouredQueryPerformanceCounter, reinterpret_cast<void**>(&TrueQueryPerformanceCounter));
}

if (Settings::get().getOverrideLanguage().length() >= 2 && Settings::get().getOverrideLanguage().find("none") != 0) {
std::wstring langName = UTF8ToUTF16(Settings::get().getOverrideLanguage());
LCID lcid = LocaleNameToLCID(langName.c_str(), 0); // Vista+ only, but I do not care about XP
if (lcid) {
s_langid = LANGIDFROMLCID(lcid);

MH_CreateHook(GetUserDefaultLangID, DetouredGetUserDefaultLangID, reinterpret_cast<void**>(&TrueGetUserDefaultLangID));
MH_EnableHook(GetUserDefaultLangID);
}
}

MH_CreateHook(D3DXCreateTextureFromFileInMemory, DetouredD3DXCreateTextureFromFileInMemory, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemory));
MH_CreateHook(D3DXCreateTextureFromFileInMemoryEx, DetouredD3DXCreateTextureFromFileInMemoryEx, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemoryEx));

if (MH_EnableHook(MH_ALL_HOOKS) == MH_OK)
{
SDLOG(0, "Detouring: Detoured successfully\n");
}
else
{
SDLOG(0, "Detouring: Error detouring\n");
}
MH_Initialize();

if (Settings::get().getSkipIntro()) {
MH_CreateHook(QueryPerformanceCounter, DetouredQueryPerformanceCounter, reinterpret_cast<void**>(&TrueQueryPerformanceCounter));
}

if (Settings::get().getOverrideLanguage().length() >= 2 && Settings::get().getOverrideLanguage().find("none") != 0) {
std::wstring langName = UTF8ToUTF16(Settings::get().getOverrideLanguage());
LCID lcid = LocaleNameToLCID(langName.c_str(), 0); // Vista+ only, but I do not care about XP
if (lcid) {
s_langid = LANGIDFROMLCID(lcid);

MH_CreateHook(GetUserDefaultLangID, DetouredGetUserDefaultLangID, reinterpret_cast<void**>(&TrueGetUserDefaultLangID));
MH_EnableHook(GetUserDefaultLangID);
}
}

MH_CreateHook(D3DXCreateTextureFromFileInMemory, DetouredD3DXCreateTextureFromFileInMemory, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemory));
MH_CreateHook(D3DXCreateTextureFromFileInMemoryEx, DetouredD3DXCreateTextureFromFileInMemoryEx, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemoryEx));

if (MH_EnableHook(MH_ALL_HOOKS) == MH_OK) {
SDLOG(0, "Detouring: Detoured successfully");
} else {
SDLOG(0, "Detouring: Error detouring");
}
}

void endDetour()
{
MH_Uninitialize();
MH_Uninitialize();
}
8 changes: 4 additions & 4 deletions DSFix/Effect.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "stdafx.h"

#include "Effect.h"

const D3DVERTEXELEMENT9 Effect::vertexElements[3] = {
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
82 changes: 37 additions & 45 deletions DSFix/FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#include "RenderstateManager.h"
#include "memory.h"

#ifndef WITHOUT_GFWL_LIB
void enableGFWLCompatibility(void);
#endif

// Globals
static DWORD OriginalBase = 0x0400000;
static DWORD ImageBase = NULL;
Expand Down Expand Up @@ -51,27 +47,30 @@ DWORD GETCMD_OFFSET = 0x0000000D;

// Misc
//------------------------------------
DWORD getAbsoluteAddress(DWORD offset) {
DWORD getAbsoluteAddress(DWORD offset)
{
if (ImageBase)
return ImageBase + offset;
else
return NULL;
}

DWORD convertAddress(DWORD Address) {
DWORD convertAddress(DWORD Address)
{
return getAbsoluteAddress(Address - OriginalBase);
}

// Memory
//------------------------------------
void updateAnimationStepTime(float stepTime, float minFPS, float maxFPS) {
void updateAnimationStepTime(float stepTime, float minFPS, float maxFPS)
{
float FPS = 1.0f/(stepTime/1000);

if (FPS < minFPS)
FPS = minFPS;
else if (FPS > maxFPS)
FPS = maxFPS;

float cappedStep = 1/(float)FPS;
if(RSManager::get().isPaused()) cappedStep = 0.000000000000000001f;
DWORD data = *(DWORD*)&cappedStep;
Expand All @@ -80,7 +79,8 @@ void updateAnimationStepTime(float stepTime, float minFPS, float maxFPS) {
}

// Timer
double getElapsedTime(void) {
double getElapsedTime(void)
{
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return (double)( (c.QuadPart - counterAtStart.QuadPart) * 1000.0 / (double)timerFreq.QuadPart );
Expand All @@ -90,7 +90,8 @@ double getElapsedTime(void) {
// Hook functions
//----------------------------------------------------------------------------------------

void _stdcall updateFramerate(unsigned int cmd) {
void _stdcall updateFramerate(unsigned int cmd)
{
// If rendering was performed, update animation step-time
if((cmd == 2) || (cmd == 5)) {
// FPS regulation based on previous render
Expand All @@ -106,7 +107,8 @@ void _stdcall updateFramerate(unsigned int cmd) {
}

// Hook
__declspec(naked) void getDrawThreadMsgCommand(void) {
__declspec(naked) void getDrawThreadMsgCommand(void)
{
__asm {
MOV EAX, [ECX+0Ch] // Put msgCmd in EAX (Return value)
PUSHAD
Expand All @@ -120,71 +122,61 @@ __declspec(naked) void getDrawThreadMsgCommand(void) {
//----------------------------------------------------------------------------------------
// Game Patches
//----------------------------------------------------------------------------------------
void applyFPSPatch() {
void applyFPSPatch()
{

SDLOG(0, "Starting FPS unlock...\n");
#ifndef WITHOUT_GFWL_LIB
SDLOG(0, "Applying GFWL compatibility\n");
enableGFWLCompatibility();
#endif
SDLOG(0, "Starting FPS unlock...");

// Get image info
MODULEINFO moduleInfo;
PIMAGE_DOS_HEADER dosHeader;
PIMAGE_NT_HEADERS ntHeader;
IMAGE_FILE_HEADER header;
IMAGE_FILE_HEADER header;

if(GetModuleInformation(GetCurrentProcess(), GetModuleHandle(NULL), &moduleInfo, sizeof(moduleInfo)))
{
if(GetModuleInformation(GetCurrentProcess(), GetModuleHandle(NULL), &moduleInfo, sizeof(moduleInfo))) {
ImageBase = (DWORD)moduleInfo.lpBaseOfDll;
SDLOG(0, "ImageBase at 0x%08X\n", ImageBase);
SDLOG(0, "ImageBase at 0x%08X", ImageBase);

dosHeader = (PIMAGE_DOS_HEADER)ImageBase;
ntHeader = (PIMAGE_NT_HEADERS)((DWORD)(dosHeader) + (dosHeader->e_lfanew));
header = ntHeader->FileHeader;
DWORD TimeStamp = header.TimeDateStamp;
SDLOG(0, "Executable timestamp: 0x%08X, config: 0x%08X\n", TimeStamp, EXE_TIMESTAMP);
SDLOG(0, "Executable timestamp: 0x%08X, config: 0x%08X", TimeStamp, EXE_TIMESTAMP);

// Perform pattern matching if timestamp differs
if (TimeStamp != EXE_TIMESTAMP) {
SDLOG(0, "Trying pattern matching...\n");
SDLOG(0, "Trying pattern matching...");

DWORD address;
address = GetMemoryAddressFromPattern(NULL, TS_PATTERN, TS_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_TS found at 0x%08X\n", address);
SDLOG(0, "ADDR_TS found at 0x%08X", address);
ADDR_TS = address;
}
else {
SDLOG(0, "Could not match ADDR_TS pattern, FPS not unlocked\n");
} else {
SDLOG(0, "Could not match ADDR_TS pattern, FPS not unlocked");
return;
}
address = GetMemoryAddressFromPattern(NULL, PRESINT_PATTERN, PRESINT_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_PRESINT found at 0x%08X\n", address);
SDLOG(0, "ADDR_PRESINT found at 0x%08X", address);
ADDR_PRESINT = address;
}
else {
SDLOG(0, "Could not match ADDR_PRESINT pattern, FPS not unlocked\n");
} else {
SDLOG(0, "Could not match ADDR_PRESINT pattern, FPS not unlocked");
return;
}
address = GetMemoryAddressFromPattern(NULL, GETCMD_PATTERN, GETCMD_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_GETCMD found at 0x%08X\n", address);
SDLOG(0, "ADDR_GETCMD found at 0x%08X", address);
ADDR_GETCMD = address;
}
else {
SDLOG(0, "Could not match ADDR_GETCMD pattern, FPS not unlocked\n");
} else {
SDLOG(0, "Could not match ADDR_GETCMD pattern, FPS not unlocked");
return;
}
SDLOG(0, "Pattern matching successful\n");
}
else
SDLOG(0, "Using configured addresses\n");
}
else
{
SDLOG(0, "GetModuleInformation failed, FPS not unlocked\n");
SDLOG(0, "Pattern matching successful");
} else
SDLOG(0, "Using configured addresses");
} else {
SDLOG(0, "GetModuleInformation failed, FPS not unlocked");
return;
}

Expand All @@ -206,6 +198,6 @@ void applyFPSPatch() {
// Detour call to getDrawThreadMsgCommand
address = convertAddress(ADDR_GETCMD);
DetourApply((BYTE*)address, (BYTE*)getDrawThreadMsgCommand, 5, CALLOP);
SDLOG(0, "FPS unlocked\n");

SDLOG(0, "FPS unlocked");
}
Loading

0 comments on commit 8bb013b

Please sign in to comment.