Skip to content

Commit

Permalink
Tweak DetouredQueryPerformanceCounter
Browse files Browse the repository at this point in the history
Add D3DCREATE_PUREDEVICE to BehaviorFlags (should be safe)
Remove some useless code
Properly apply Astyle -_-
Correctly find game window (no more GetActiveWindow())
  • Loading branch information
Robert Krawczyk committed Aug 3, 2015
1 parent 8bb013b commit ddfcdf4
Show file tree
Hide file tree
Showing 38 changed files with 17,026 additions and 16,688 deletions.
12 changes: 3 additions & 9 deletions Common/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Logger : NonCopyable
return m_file != INVALID_HANDLE_VALUE;
}

bool Console(const char* title, const char* console_notice)
bool Console(const char* title)
{
AllocConsole();

Expand All @@ -47,12 +47,6 @@ class Logger : NonCopyable
{
ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
if (title) SetConsoleTitleA(title);
if (console_notice)
{
size_t len = strlen(console_notice);
DWORD lenout = 0;
WriteConsoleA(m_console, console_notice, (DWORD)len, &lenout, NULL);
}
}
return m_console != INVALID_HANDLE_VALUE;
}
Expand Down Expand Up @@ -126,9 +120,9 @@ inline void LogFile(const std::string& logname)
Logger::Get().File(logname);
}

inline void LogConsole(const char* title = nullptr, const char* console_notice = nullptr)
inline void LogConsole(const char* title = nullptr)
{
Logger::Get().Console(title, console_notice);
Logger::Get().Console(title);
}

inline void PrintLog(const char* format, ...)
Expand Down
29,871 changes: 14,936 additions & 14,935 deletions DSFix/AreaTex.h

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions DSFix/Detouring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,25 @@ DWORD WINAPI DetouredTimeGetTime()
return TrueTimeGetTime() + timeIncrease;
}

static LARGE_INTEGER perfCountIncrease, countsPerSec;
static LARGE_INTEGER 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", GetCurrentThreadId(), captured, hash);
BOOL ret = TrueQueryPerformanceCounter(lpPerformanceCount);
if (timingIntroMode && captured >= 1) {
perfCountIncrease.QuadPart += countsPerSec.QuadPart / 50;
if (timingIntroMode)
{
lpPerformanceCount->QuadPart += countsPerSec.QuadPart / 50;
}
else
{
static size_t count;
if (count > 10)
MH_DisableHook(QueryPerformanceCounter);
++count;
}
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");
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)
Expand Down Expand Up @@ -105,14 +99,17 @@ void startDetour()
{
MH_Initialize();

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

if (Settings::get().getOverrideLanguage().length() >= 2 && Settings::get().getOverrideLanguage().find("none") != 0) {
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) {
if (lcid)
{
s_langid = LANGIDFROMLCID(lcid);

MH_CreateHook(GetUserDefaultLangID, DetouredGetUserDefaultLangID, reinterpret_cast<void**>(&TrueGetUserDefaultLangID));
Expand All @@ -123,9 +120,12 @@ void startDetour()
MH_CreateHook(D3DXCreateTextureFromFileInMemory, DetouredD3DXCreateTextureFromFileInMemory, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemory));
MH_CreateHook(D3DXCreateTextureFromFileInMemoryEx, DetouredD3DXCreateTextureFromFileInMemoryEx, reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemoryEx));

if (MH_EnableHook(MH_ALL_HOOKS) == MH_OK) {
if (MH_EnableHook(MH_ALL_HOOKS) == MH_OK)
{
SDLOG(0, "Detouring: Detoured successfully");
} else {
}
else
{
SDLOG(0, "Detouring: Error detouring");
}
}
Expand Down
3 changes: 2 additions & 1 deletion DSFix/Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "Effect.h"

const D3DVERTEXELEMENT9 Effect::vertexElements[3] = {
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()
Expand Down
18 changes: 11 additions & 7 deletions DSFix/Effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
#include "main.h"

// Base class for effects
class Effect {
class Effect
{
protected:
CComPtr<IDirect3DDevice9> device;
CComPtr<IDirect3DVertexDeclaration9> vertexDeclaration;
CComPtr<IDirect3DDevice9> device;
CComPtr<IDirect3DVertexDeclaration9> vertexDeclaration;

static const D3DVERTEXELEMENT9 vertexElements[3];
static const D3DVERTEXELEMENT9 vertexElements[3];

public:
Effect(IDirect3DDevice9* device) : device(device) {
Effect(IDirect3DDevice9* device) : device(device)
{
device->CreateVertexDeclaration(vertexElements , &vertexDeclaration);
}

void quad(int width, int height) {
void quad(int width, int height)
{
// Draw aligned fullscreen quad
D3DXVECTOR2 pixelSize = D3DXVECTOR2(1.0f / float(width), 1.0f / float(height));
float quad[4][5] = {
float quad[4][5] =
{
{ -1.0f - pixelSize.x, 1.0f + pixelSize.y, 0.5f, 0.0f, 0.0f },
{ 1.0f - pixelSize.x, 1.0f + pixelSize.y, 0.5f, 1.0f, 0.0f },
{ -1.0f - pixelSize.x, -1.0f + pixelSize.y, 0.5f, 0.0f, 1.0f },
Expand Down
40 changes: 28 additions & 12 deletions DSFix/FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ double getElapsedTime(void)
void _stdcall updateFramerate(unsigned int cmd)
{
// If rendering was performed, update animation step-time
if((cmd == 2) || (cmd == 5)) {
if((cmd == 2) || (cmd == 5))
{
// FPS regulation based on previous render
double maxFPS = (double)Settings::get().getCurrentFPSLimit();
double minFPS = 10.0f;
Expand All @@ -109,7 +110,8 @@ void _stdcall updateFramerate(unsigned int cmd)
// Hook
__declspec(naked) void getDrawThreadMsgCommand(void)
{
__asm {
__asm
{
MOV EAX, [ECX+0Ch] // Put msgCmd in EAX (Return value)
PUSHAD
PUSH EAX
Expand All @@ -133,7 +135,8 @@ void applyFPSPatch()
PIMAGE_NT_HEADERS ntHeader;
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", ImageBase);

Expand All @@ -144,38 +147,51 @@ void applyFPSPatch()
SDLOG(0, "Executable timestamp: 0x%08X, config: 0x%08X", TimeStamp, EXE_TIMESTAMP);

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

DWORD address;
address = GetMemoryAddressFromPattern(NULL, TS_PATTERN, TS_OFFSET);
if(address != NULL) {
if(address != NULL)
{
SDLOG(0, "ADDR_TS found at 0x%08X", address);
ADDR_TS = address;
} else {
}
else
{
SDLOG(0, "Could not match ADDR_TS pattern, FPS not unlocked");
return;
}
address = GetMemoryAddressFromPattern(NULL, PRESINT_PATTERN, PRESINT_OFFSET);
if(address != NULL) {
if(address != NULL)
{
SDLOG(0, "ADDR_PRESINT found at 0x%08X", address);
ADDR_PRESINT = address;
} else {
}
else
{
SDLOG(0, "Could not match ADDR_PRESINT pattern, FPS not unlocked");
return;
}
address = GetMemoryAddressFromPattern(NULL, GETCMD_PATTERN, GETCMD_OFFSET);
if(address != NULL) {
if(address != NULL)
{
SDLOG(0, "ADDR_GETCMD found at 0x%08X", address);
ADDR_GETCMD = address;
} else {
}
else
{
SDLOG(0, "Could not match ADDR_GETCMD pattern, FPS not unlocked");
return;
}
SDLOG(0, "Pattern matching successful");
} else
}
else
SDLOG(0, "Using configured addresses");
} else {
}
else
{
SDLOG(0, "GetModuleInformation failed, FPS not unlocked");
return;
}
Expand Down
3 changes: 2 additions & 1 deletion DSFix/FXAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ FXAA::FXAA(IDirect3DDevice9 *device, int width, int height, Quality quality)
D3DXMACRO pixelSizeMacro = { "PIXEL_SIZE", pixelSizeText.c_str() };
defines.push_back(pixelSizeMacro);

D3DXMACRO qualityMacros[] = {
D3DXMACRO qualityMacros[] =
{
{ "FXAA_QUALITY__PRESET", "10" },
{ "FXAA_QUALITY__PRESET", "20" },
{ "FXAA_QUALITY__PRESET", "28" },
Expand Down
17 changes: 9 additions & 8 deletions DSFix/FXAA.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@

#include "Effect.h"

class FXAA : public Effect {
class FXAA : public Effect
{
public:
enum Quality { QualityLow, QualityMedium, QualityHigh, QualityUltra };

FXAA(IDirect3DDevice9 *device, int width, int height, Quality quality);
virtual ~FXAA(){};
FXAA(IDirect3DDevice9 *device, int width, int height, Quality quality);
virtual ~FXAA() {};

void go(IDirect3DTexture9 *frame, IDirect3DSurface9 *dst);

private:
int width, height;

CComPtr<ID3DXEffect> effect;
CComPtr<IDirect3DTexture9> buffer1Tex;
CComPtr<IDirect3DSurface9> buffer1Surf;
CComPtr<ID3DXEffect> effect;

CComPtr<IDirect3DTexture9> buffer1Tex;
CComPtr<IDirect3DSurface9> buffer1Surf;

D3DXHANDLE frameTexHandle;

void lumaPass(IDirect3DTexture9 *frame, IDirect3DSurface9 *dst);
void fxaaPass(IDirect3DTexture9 *src, IDirect3DSurface9* dst);
};
13 changes: 7 additions & 6 deletions DSFix/GAUSS.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

#include "Effect.h"

class GAUSS : public Effect {
class GAUSS : public Effect
{
public:
GAUSS(IDirect3DDevice9 *device, int width, int height);
virtual ~GAUSS(){};
GAUSS(IDirect3DDevice9 *device, int width, int height);
virtual ~GAUSS() {};

void go(IDirect3DTexture9 *input, IDirect3DSurface9 *dst);

private:
int width, height;

CComPtr<ID3DXEffect> effect;
CComPtr<IDirect3DTexture9> buffer1Tex;
CComPtr<IDirect3DSurface9> buffer1Surf;

CComPtr<IDirect3DTexture9> buffer1Tex;
CComPtr<IDirect3DSurface9> buffer1Surf;

D3DXHANDLE frameTexHandle;
};
Loading

0 comments on commit ddfcdf4

Please sign in to comment.