Skip to content

Commit

Permalink
Seems to be finally functional, in debugging and normal modes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Friendly0Fire committed Sep 21, 2022
1 parent 14baeb3 commit 0b97121
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 68 deletions.
78 changes: 67 additions & 11 deletions d3d11/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
#include <windows.h>
#include <d3d11.h>

using D3D11CoreCreateDevice_t = HRESULT (WINAPI *)(
IDXGIFactory * pFactory,
IDXGIAdapter * pAdapter,
UINT Flags,
const D3D_FEATURE_LEVEL * pFeatureLevels,
UINT FeatureLevels,
ID3D11Device * *ppDevice);
D3D11CoreCreateDevice_t RealD3D11CoreCreateDevice = nullptr;

using D3D11CoreCreateLayeredDevice_t = HRESULT (WINAPI *)(
const void* unknown0,
DWORD unknown1,
const void* unknown2,
REFIID riid,
void** ppvObj);
D3D11CoreCreateLayeredDevice_t RealD3D11CoreCreateLayeredDevice = nullptr;

using D3D11CoreGetLayeredDeviceSize_t = SIZE_T (WINAPI *)(const void* unknown0, DWORD unknown1);
D3D11CoreGetLayeredDeviceSize_t RealD3D11CoreGetLayeredDeviceSize = nullptr;

using D3D11CoreRegisterLayers_t = HRESULT (WINAPI *)(const void* unknown0, DWORD unknown1);
D3D11CoreRegisterLayers_t RealD3D11CoreRegisterLayers = nullptr;

#define DX11_CREATE_PLIST pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext

typedef HRESULT(WINAPI* D3D11CreateDeviceAndSwapChainFunc)(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext);
Expand Down Expand Up @@ -39,9 +62,21 @@ HMODULE GetD3D11Module()
GetSystemDirectory(infoBuf, 4096);
lstrcatW(infoBuf, L"\\d3d11.dll");

auto existingModule = GetModuleHandle(infoBuf);
if(existingModule)
return existingModule;

return LoadLibrary(infoBuf);
}

void FreeD3D11Module() {
wchar_t infoBuf[4096];
GetSystemDirectory(infoBuf, 4096);
lstrcatW(infoBuf, L"\\d3d11.dll");

FreeLibrary(GetModuleHandle(infoBuf));
}

FARPROC GetD3D11Function(LPCSTR name)
{
static HMODULE hmod = GetD3D11Module();
Expand All @@ -50,6 +85,9 @@ FARPROC GetD3D11Function(LPCSTR name)

#define GET_D3D11_FUNC(name) (decltype(name)*)GetD3D11Function(#name)

bool ShouldTryLoading = true;
void LoadAllFunctions();

extern "C" HRESULT WINAPI D3D11CoreCreateDevice(
IDXGIFactory * pFactory,
IDXGIAdapter * pAdapter,
Expand All @@ -58,8 +96,9 @@ extern "C" HRESULT WINAPI D3D11CoreCreateDevice(
UINT FeatureLevels,
ID3D11Device * *ppDevice)
{
static auto func = GET_D3D11_FUNC(D3D11CoreCreateDevice);
return func(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice);
if(ShouldTryLoading)
LoadAllFunctions();
return RealD3D11CoreCreateDevice(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice);
}

extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice(
Expand All @@ -69,27 +108,44 @@ extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice(
REFIID riid,
void** ppvObj)
{
static auto func = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice);
return func(unknown0, unknown1, unknown2, riid, ppvObj);
if(ShouldTryLoading)
LoadAllFunctions();
return RealD3D11CoreCreateLayeredDevice(unknown0, unknown1, unknown2, riid, ppvObj);
}

extern "C" SIZE_T WINAPI D3D11CoreGetLayeredDeviceSize(const void* unknown0, DWORD unknown1)
{
static auto func = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize);
return func(unknown0, unknown1);
if(ShouldTryLoading)
LoadAllFunctions();
return RealD3D11CoreGetLayeredDeviceSize(unknown0, unknown1);
}

extern "C" HRESULT WINAPI D3D11CoreRegisterLayers(const void* unknown0, DWORD unknown1)
{
static auto func = GET_D3D11_FUNC(D3D11CoreRegisterLayers);
return func(unknown0, unknown1);
if(ShouldTryLoading)
LoadAllFunctions();
return RealD3D11CoreRegisterLayers(unknown0, unknown1);
}

void LoadAllFunctions() {
RealD3D11CoreCreateDevice = GET_D3D11_FUNC(D3D11CoreCreateDevice);
RealD3D11CoreCreateLayeredDevice = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice);
RealD3D11CoreGetLayeredDeviceSize = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize);
RealD3D11CoreRegisterLayers = GET_D3D11_FUNC(D3D11CoreRegisterLayers);

ShouldTryLoading = false;
}

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
DWORD fdwReason,
LPVOID lpReserved
)
{
return TRUE;
}
if(fdwReason == DLL_PROCESS_DETACH)
{
ShouldTryLoading = true;
FreeD3D11Module();
}

return true;
}
12 changes: 2 additions & 10 deletions d3d9/d3d9.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>deffile.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>IF DEFINED GW2_INSTALL_DIR (
xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
)</Command>
</PostBuildEvent>
<PostBuildEvent />
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down Expand Up @@ -142,11 +138,7 @@ xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>deffile.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>IF DEFINED GW2_INSTALL_DIR (
xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
)</Command>
</PostBuildEvent>
<PostBuildEvent />
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand Down
55 changes: 39 additions & 16 deletions dxgi/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

using Compat_t = void*(WINAPI *)();
Compat_t RealCompatValue = nullptr;
Compat_t RealCompatString = nullptr;
using CompatValue_t = BOOL(WINAPI *)(LPCSTR szName, UINT64 *pValue);
using CompatString_t = BOOL(WINAPI *)(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag);
CompatValue_t RealCompatValue = nullptr;
CompatString_t RealCompatString = nullptr;

typedef HRESULT(WINAPI* DXGIFactoryCreate0)(REFIID riid, void** ppFactory);
typedef HRESULT(WINAPI* DXGIFactoryCreate1)(REFIID riid, void** ppFactory);
Expand Down Expand Up @@ -53,9 +54,21 @@ HMODULE GetDXGIModule()
GetSystemDirectory(infoBuf, 4096);
lstrcatW(infoBuf, L"\\dxgi.dll");

auto existingModule = GetModuleHandle(infoBuf);
if(existingModule)
return existingModule;

return LoadLibrary(infoBuf);
}

void FreeDXGIModule() {
wchar_t infoBuf[4096];
GetSystemDirectory(infoBuf, 4096);
lstrcatW(infoBuf, L"\\dxgi.dll");

FreeLibrary(GetModuleHandle(infoBuf));
}

FARPROC GetDXGIFunction(LPCSTR name)
{
static HMODULE hmod = GetDXGIModule();
Expand All @@ -64,31 +77,41 @@ FARPROC GetDXGIFunction(LPCSTR name)

#define GET_DXGI_FUNC(name) (decltype(name)*)GetDXGIFunction(#name)

extern "C" void* WINAPI CompatValue()
bool ShouldTryLoading = true;
void LoadAllFunctions();

extern "C" BOOL WINAPI CompatValue(LPCSTR szName, UINT64 *pValue)
{
return RealCompatValue();
if(ShouldTryLoading)
LoadAllFunctions();
return RealCompatValue(szName, pValue);
}

extern "C" void* WINAPI CompatString()
extern "C" BOOL WINAPI CompatString(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag)
{
return RealCompatString();
if(ShouldTryLoading)
LoadAllFunctions();
return RealCompatString(szName, pSize, lpData, Flag);
}

void LoadAllFunctions() {
RealCompatValue = GET_DXGI_FUNC(CompatValue);
RealCompatString = GET_DXGI_FUNC(CompatString);

ShouldTryLoading = false;
}

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD fdwReason,
LPVOID lpReserved
)
{
switch (fdwReason)
if(fdwReason == DLL_PROCESS_DETACH)
{
case DLL_PROCESS_ATTACH:
RealCompatValue = GET_DXGI_FUNC(CompatValue);
RealCompatString = GET_DXGI_FUNC(CompatString);
break;
case DLL_PROCESS_DETACH:
RealCompatValue = nullptr;
RealCompatString = nullptr;
break;
ShouldTryLoading = true;
FreeDXGIModule();
}

return true;
}

8 changes: 6 additions & 2 deletions loader_core.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "loader_core", "loader_core\loader_core.vcxproj", "{024CCE36-7DE3-4ACC-80E6-EF25F9894C11}"
ProjectSection(ProjectDependencies) = postProject
{5A73591F-E0F2-400D-9C93-146CD068E18A} = {5A73591F-E0F2-400D-9C93-146CD068E18A}
{C5DD718A-8B5E-493D-97F3-A39F2812E782} = {C5DD718A-8B5E-493D-97F3-A39F2812E782}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fake_client", "fake_client\fake_client.vcxproj", "{33FC0BB7-E821-4B68-BB81-B16BD7F76C84}"
ProjectSection(ProjectDependencies) = postProject
Expand Down
12 changes: 0 additions & 12 deletions loader_core/deffile.def
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
LIBRARY addonLoader.dll
EXPORTS

; d3d9

Direct3DCreate9 @30
Direct3DCreate9Ex
D3DPERF_BeginEvent
D3DPERF_EndEvent
D3DPERF_SetMarker
D3DPERF_SetRegion
D3DPERF_QueryRepeatFrame
D3DPERF_SetOptions
D3DPERF_GetStatus

; d3d11

D3D11CreateDevice
Expand Down
26 changes: 9 additions & 17 deletions loader_core/loader_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ loader_core loader_core::instance;

loader_core::loader_core()
{
state = LDR_DLL_LOADED;
state = LDR_DLL_LOADED;
}

loader_core::~loader_core()
{
{
}

loader_state loader_core::GetCurrentState()
Expand Down Expand Up @@ -83,20 +83,16 @@ void loader_core::log_text_fmt(gw2al_log_level level, const wchar_t * source, co

va_list arg;
va_start(arg, fmt);

vswprintf(buf, 4096, fmt, arg);

va_end(arg);

gw2al_core__log_text(level, (wchar_t*)source, buf);
}

void loader_core::innerInit()
{
HMODULE directDraw = GetModuleHandleA("ddraw.dll");
if(directDraw)
return;

if (SwitchState(LDR_ADDON_LOAD))
{
bool isFirstLoad = gw2al_core__init();
Expand All @@ -116,9 +112,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)

IDirect3D9* ret = NULL;

HMODULE directDraw = GetModuleHandleA("ddraw.dll");

if (!directDraw && d3d9_create_hook)
if (d3d9_create_hook)
{
LOG_DEBUG(L"core", L"Calling D3D9Create, hook = 0x%016llX", d3d9_create_hook);
ret = d3d9_create_hook();
Expand All @@ -136,7 +130,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)
typedef IDirect3D9* (WINAPI* Direct3DCreate9Func)(UINT sdkver);

Direct3DCreate9Func origDirect3DCreate9 = (Direct3DCreate9Func)GetProcAddress(sys_d3d9, "Direct3DCreate9");
ret = origDirect3DCreate9(sdkVer);
ret = origDirect3DCreate9(sdkVer);
}

LOG_DEBUG(L"core", L"ID3D9 = 0x%016llX", ret);
Expand All @@ -155,9 +149,7 @@ HRESULT loader_core::RouteDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, v

HRESULT ret = NULL;

HMODULE directDraw = GetModuleHandleA("ddraw.dll");

if (!directDraw && dxgi_create_hook)
if (dxgi_create_hook)
{
LOG_DEBUG(L"core", L"Calling DXGICreate, hook = 0x%016llX", dxgi_create_hook);
ret = dxgi_create_hook(ver, Flags, riid, ppFactory);
Expand Down Expand Up @@ -202,7 +194,7 @@ HRESULT loader_core::OnDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, void


IDirect3D9 * loader_core::OnD3DCreate(UINT sdkVer)
{
{
innerInit();
return RouteD3DCreate(sdkVer);
}
Expand Down Expand Up @@ -285,7 +277,7 @@ void loader_core::SignalUnload()
}

BOOL loader_core::SwitchState(loader_state newState)
{
{
int doSwitch = 0;

switch (state)
Expand Down

0 comments on commit 0b97121

Please sign in to comment.