Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Detours with MinHook, rebased onto mrexodia's build fixes #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
*.sdf
*.user
*.opensdf
Release
*.suo
Release
Debug/
Release/
.vs/
*.VC.VC.opendb
*.VC.db
9 changes: 0 additions & 9 deletions DSfix.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DSfix", "DSfix.vcxproj", "{
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E004A114-54E0-B15C-BDA5-46681084E257}.Debug|Win32.ActiveCfg = Debug|Win32
{E004A114-54E0-B15C-BDA5-46681084E257}.Debug|Win32.Build.0 = Debug|Win32
{E004A114-54E0-B15C-BDA5-46681084E257}.Debug|x64.ActiveCfg = Debug|x64
{E004A114-54E0-B15C-BDA5-46681084E257}.Debug|x64.Build.0 = Debug|x64
{E004A114-54E0-B15C-BDA5-46681084E257}.Release|Win32.ActiveCfg = Release|Win32
{E004A114-54E0-B15C-BDA5-46681084E257}.Release|Win32.Build.0 = Release|Win32
{E004A114-54E0-B15C-BDA5-46681084E257}.Release|x64.ActiveCfg = Release|x64
{E004A114-54E0-B15C-BDA5-46681084E257}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Binary file removed DSfix.v11.suo
Binary file not shown.
205 changes: 8 additions & 197 deletions DSfix.vcxproj

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions DSfix.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="d3d9.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="d3d9dev.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="d3d9int.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="d3dutil.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Detouring.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="dinputWrapper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Effect.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FPS.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FXAA.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="GAUSS.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Hud.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="KeyActions.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="memory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RenderstateManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SaveManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SMAA.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SSAO.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WindowManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AreaTex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="d3d9.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="d3d9dev.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="d3d9int.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="d3dutil.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dinput.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Detouring.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dinputWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Effect.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FPS.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FXAA.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GAUSS.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Hash.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Hud.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="memory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SaveManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SearchTex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Settings.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SMAA.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SSAO.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WindowManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="KeyActions.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RenderstateManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Textures.def" />
</ItemGroup>
<ItemGroup>
<None Include="Actions.def" />
<None Include="dinput8.def" />
<None Include="Keys.def" />
<None Include="Settings.def" />
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{b7358087-4926-4614-a239-dcc17176b6d8}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{55ebbcd6-421b-458a-abd5-c9d7c082044d}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
93 changes: 56 additions & 37 deletions Detouring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,68 @@ HRESULT WINAPI DetouredD3DXCompileShader(_In_ LPCSTR pSrcData, _In_ UINT srcData
return res;
}

void hookFunction(const char* name, const char* dllname, void** ppTarget, void* const pDetour, void** ppOriginal) {
HMODULE dllHandle = GetModuleHandle(dllname);
*ppTarget = GetProcAddress(dllHandle, name);
MH_STATUS ret = MH_CreateHook(*ppTarget, pDetour, ppOriginal);
if (ret == MH_OK) {
SDLOG(2, "MH_CreateHook for %s in %s succeeded\n", name, dllname);
}
else {
SDLOG(0, "MH_CreateHook for %s in %s failed\n", name, dllname);
SDLOG(0, "dllHandle = %p\n", dllHandle);
SDLOG(0, "*ppTarget = %p\n", *ppTarget);
}
ret = MH_EnableHook(*ppTarget);
if (ret == MH_OK) {
SDLOG(2, "MH_EnableHook succeeded\n");
}
else {
SDLOG(0, "MH_EnableHook failed\n");
}
}

namespace {
void* TargetDirect3DCreate9;
void* TargetQueryPerformanceCounter;
void* TargetD3DXCreateTextureFromFileInMemory;
void* TargetD3DXCreateTextureFromFileInMemoryEx;
}

void earlyDetour() {
QueryPerformanceFrequency(&countsPerSec);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)oDirect3DCreate9, hkDirect3DCreate9);
DetourTransactionCommit();
MH_Initialize();
hookFunction("Direct3DCreate9",
"d3d9.dll",
&TargetDirect3DCreate9,
&hkDirect3DCreate9,
reinterpret_cast<void**>(&oDirect3DCreate9));
}

void startDetour() {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
//DetourAttach(&(PVOID&)TrueSleepEx, DetouredSleepEx);
//DetourAttach(&(PVOID&)TrueTimeGetTime, DetouredTimeGetTime);
if(Settings::get().getSkipIntro()) DetourAttach(&(PVOID&)TrueQueryPerformanceCounter, DetouredQueryPerformanceCounter);
//TrueD3DXCreateTexture = (D3DXCreateTexture_FNType)DetourFindFunction("d3dx9_43.dll", "D3DXCreateTexture");
TrueD3DXCreateTextureFromFileInMemory = (D3DXCreateTextureFromFileInMemory_FNType)DetourFindFunction("d3dx9_43.dll", "D3DXCreateTextureFromFileInMemory");
TrueD3DXCreateTextureFromFileInMemoryEx = (D3DXCreateTextureFromFileInMemoryEx_FNType)DetourFindFunction("d3dx9_43.dll", "D3DXCreateTextureFromFileInMemoryEx");
//DetourAttach(&(PVOID&)TrueD3DXCreateTexture, DetouredD3DXCreateTexture);
DetourAttach(&(PVOID&)TrueD3DXCreateTextureFromFileInMemory, DetouredD3DXCreateTextureFromFileInMemory);
DetourAttach(&(PVOID&)TrueD3DXCreateTextureFromFileInMemoryEx, DetouredD3DXCreateTextureFromFileInMemoryEx);
//TrueD3DXCompileShader = (D3DXCompileShader_FNType)DetourFindFunction("d3dx9_43.dll", "D3DXCompileShader");
//SDLOG(0, "Detouring: compile shader: %p\n", TrueD3DXCompileShader);
//DetourAttach(&(PVOID&)TrueD3DXCompileShader, DetouredD3DXCompileShader);

if(DetourTransactionCommit() == NO_ERROR) {
SDLOG(0, "Detouring: Detoured successfully\n");
} else {
SDLOG(0, "Detouring: Error detouring\n");
void startDetour() {
if (Settings::get().getSkipIntro()) {
hookFunction("QueryPerformanceCounter",
"kernel32.dll",
&TargetQueryPerformanceCounter,
&DetouredQueryPerformanceCounter,
reinterpret_cast<void**>(&TrueQueryPerformanceCounter));
}
hookFunction("D3DXCreateTextureFromFileInMemory",
"d3dx9_43.dll",
&TargetD3DXCreateTextureFromFileInMemory,
&DetouredD3DXCreateTextureFromFileInMemory,
reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemory));
hookFunction("D3DXCreateTextureFromFileInMemoryEx",
"d3dx9_43.dll",
&TargetD3DXCreateTextureFromFileInMemoryEx,
&DetouredD3DXCreateTextureFromFileInMemoryEx,
reinterpret_cast<void**>(&TrueD3DXCreateTextureFromFileInMemoryEx));
}

void endDetour() {
//if(Settings::get().getSkipIntro()) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
//DetourDetach(&(PVOID&)TrueSleepEx, DetouredSleepEx);
//DetourDetach(&(PVOID&)TrueTimeGetTime, DetouredTimeGetTime);
if(Settings::get().getSkipIntro()) DetourDetach(&(PVOID&)TrueQueryPerformanceCounter, DetouredQueryPerformanceCounter);
//DetourDetach(&(PVOID&)TrueD3DXCreateTexture, DetouredD3DXCreateTexture);
DetourDetach(&(PVOID&)TrueD3DXCreateTextureFromFileInMemory, DetouredD3DXCreateTextureFromFileInMemory);
DetourDetach(&(PVOID&)TrueD3DXCreateTextureFromFileInMemoryEx, DetouredD3DXCreateTextureFromFileInMemoryEx);
DetourDetach(&(PVOID&)oDirect3DCreate9, hkDirect3DCreate9);
//DetourDetach(&(PVOID&)TrueD3DXCompileShader, DetouredD3DXCompileShader);
DetourTransactionCommit();
//}
MH_RemoveHook(TargetDirect3DCreate9);
MH_RemoveHook(TargetQueryPerformanceCounter);
MH_RemoveHook(TargetD3DXCreateTextureFromFileInMemory);
MH_RemoveHook(TargetD3DXCreateTextureFromFileInMemoryEx);
MH_Uninitialize();
}
3 changes: 2 additions & 1 deletion Detouring.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "main.h"

#include <detours.h>
#include "minhook/MinHook.h"
#pragma comment(lib, "minhook\\libMinhook.x86.lib")

static DWORD (WINAPI * TrueSleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx;

Expand Down
4 changes: 2 additions & 2 deletions SaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void SaveManager::init() {
if(searchHandle != INVALID_HANDLE_VALUE) {
do {
std::string fn = userSaveFolderData.cFileName;
bool dir = userSaveFolderData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
bool dir = !!(userSaveFolderData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
bool saveFile = fn.substr(fn.find_last_of(".") + 1) == "sl2";
// newer versions don't contain an additional folder under NBGI\\DarkSouls
if (fn.size() > 2 && (dir || saveFile)) {
Expand Down Expand Up @@ -103,7 +103,7 @@ void SaveManager::backup(const time_t curTime) {
vector<string> saveFiles = getSaveFiles();
for(size_t i=0; i<saveFiles.size(); ++i) {
string fn = getFileNameFromPath(saveFiles[i]);
sprintf_s(buffer, "%s\\%0" TIMESTAMP_LENGTH_STR "lu_", userSaveFolder.c_str(), curTime);
sprintf_s(buffer, "%s\\%0" TIMESTAMP_LENGTH_STR "lu_", userSaveFolder.c_str(), static_cast<unsigned long>(curTime));
string newPath = string(buffer) + fn + ".bak";
if(CopyFile(saveFiles[i].c_str(), newPath.c_str(), false) == 0) {
SDLOG(0, "ERROR: SaveManager failed to back up file! (Copying %s to %s)\n", saveFiles[i].c_str(), buffer);
Expand Down
2 changes: 2 additions & 0 deletions d3d9.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

//Download & Install https://www.microsoft.com/en-us/download/details.aspx?id=6812

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "dxguid.lib")
Expand Down
2 changes: 1 addition & 1 deletion d3d9dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ HRESULT APIENTRY hkIDirect3DDevice9::SetPixelShaderConstantF(UINT StartRegister,

UINT width;
UINT height;
getDofRes(HALF_X_RESOLUTION, HALF_Y_RESOLUTION, width, height);
getDofRes(static_cast<UINT>(HALF_X_RESOLUTION), static_cast<UINT>(HALF_Y_RESOLUTION), width, height);
pBuffer[offset] = static_cast<float>(width);
pBuffer[offset + 1] = static_cast<float>(height);
pBuffer[offset + 2] = 1.f / width;
Expand Down
Loading