Skip to content

Commit cdaea2c

Browse files
committed
test mwiii
1 parent a39597e commit cdaea2c

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

src/mw23-dll/main.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,45 @@
55
#define EXPORT extern "C" __declspec(dllexport)
66

77
namespace {
8+
hook::library::Detour GetSystemMetricsDetour;
9+
hook::library::Detour ExitProcessDetour;
10+
hook::library::Detour GetThreadContextDetour;
11+
12+
DECLSPEC_NORETURN void ExitProcessStub(UINT uExitCode) {
13+
if (uExitCode) {
14+
LOG_ERROR("ExitProcess with {}", uExitCode);
15+
hook::error::DumpStackTraceFrom(alogs::LVL_ERROR);
16+
}
17+
else {
18+
LOG_INFO("ExitProcess with {}", uExitCode);
19+
hook::error::DumpStackTraceFrom(alogs::LVL_INFO);
20+
}
21+
ExitProcessDetour.Call(uExitCode);
22+
}
23+
24+
BOOL WINAPI GetThreadContextStub(HANDLE hThread, LPCONTEXT lpContext) {
25+
LOG_INFO("GetThreadContext({}, {})", hThread, (void*)lpContext);
26+
return GetThreadContextDetour.Call<BOOL>(hThread, lpContext);
27+
}
28+
int GetSystemMetricsStub(int nIndex) {
29+
LOG_INFO("GetSystemMetrics({}) / {}", nIndex, GetCurrentThread());
30+
return GetSystemMetricsDetour.Call<int>(nIndex);
31+
}
832
void Main() {
933
alogs::setfile("acts-mwiii.log");
1034
alogs::setlevel(alogs::LVL_TRACE);
11-
LOG_INFO("Init fast inject dll");
35+
LOG_INFO("Init MWIII dll");
1236

13-
hook::error::InstallErrorHooks(false);
37+
hook::error::InstallErrorHooks(true);
1438
hook::error::EnableHeavyDump();
1539

40+
hook::library::Library kernel32 = "kernel32.dll";
41+
hook::library::Library user32 = "user32.dll";
42+
43+
GetSystemMetricsDetour.Create(user32["GetSystemMetrics"], GetSystemMetricsStub);
44+
ExitProcessDetour.Create(kernel32["ExitProcess"], ExitProcessStub);
45+
GetThreadContextDetour.Create(kernel32["GetThreadContext"], GetThreadContextStub);
46+
1647

1748
//hook::library::InitScanContainer("acts-mwiii.scan");
1849

@@ -28,29 +59,24 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) {
2859
return TRUE;
2960
}
3061

31-
32-
// hook dxgi.dll for auto injection
33-
EXPORT HRESULT CreateDXGIFactory1(void* riid, void* ppFactory) {
34-
static auto func = [] {
35-
hook::library::Library dxgi{ "dxgi.dll", true };
36-
37-
if (!dxgi) throw std::runtime_error(utils::va("can't find system dxgi.dll"));
38-
39-
return reinterpret_cast<decltype(&CreateDXGIFactory1)>(dxgi["CreateDXGIFactory1"]);
40-
}();
41-
42-
return func(riid, ppFactory);
62+
namespace {
63+
int64_t DiscordCreateFake() {
64+
LOG_WARNING("Tried to get discord_game_sdk_old.dll#DiscordCreate(), but failed, did the user renamed discord_game_sdk to discord_game_sdk_old?");
65+
return 0;
66+
}
4367
}
4468

45-
46-
EXPORT HRESULT CreateDXGIFactory(void* riid, void* ppFactory) {
69+
// 000000001368F14C DiscordCreate discord_game_sdk
70+
// hook discord_game_sdk.dll for auto injection
71+
EXPORT int64_t DiscordCreate() {
4772
static auto func = [] {
48-
hook::library::Library dxgi{ "dxgi.dll", true };
73+
hook::library::Library discord_game_sdk{ "discord_game_sdk_old.dll", true };
4974

50-
if (!dxgi) throw std::runtime_error(utils::va("can't find system dxgi.dll"));
75+
if (!discord_game_sdk) return DiscordCreateFake;
5176

52-
return reinterpret_cast<decltype(&CreateDXGIFactory)>(dxgi["CreateDXGIFactory"]);
77+
return reinterpret_cast<decltype(&DiscordCreate)>(discord_game_sdk["DiscordCreate"]);
5378
}();
5479

55-
return func(riid, ppFactory);
80+
return func();
5681
}
82+

src/shared/hook/error.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ namespace hook::error {
2727

2828
return true;
2929
}
30+
const char* PtrInfo(void* location) {
31+
uintptr_t relativeLocation;
32+
const char* moduleName;
33+
34+
if (GetLocInfo(location, relativeLocation, moduleName)) {
35+
return utils::va("%s 0x%llx (%p)", moduleName, relativeLocation, (PVOID)location);
36+
}
37+
else {
38+
return utils::va("%p", (PVOID)location);
39+
}
40+
}
3041
namespace {
3142
struct ErrorConfig {
3243
DWORD mainThread{};
@@ -194,15 +205,7 @@ namespace hook::error {
194205
return;
195206
}
196207

197-
uintptr_t relativeLocation;
198-
const char* moduleName;
199-
200-
if (!GetLocInfo(lpTopLevelExceptionFilter, relativeLocation, moduleName)) {
201-
LOG_TRACE("Tried to insert hook form {} 0x{} ({})", moduleName, relativeLocation, (PVOID)lpTopLevelExceptionFilter);
202-
}
203-
else {
204-
LOG_TRACE("Tried to insert hook form {}", (PVOID)lpTopLevelExceptionFilter);
205-
}
208+
LOG_TRACE("Tried to insert hook from {} / {}", PtrInfo(lpTopLevelExceptionFilter), PtrInfo(_ReturnAddress()));
206209
}
207210

208211
}

0 commit comments

Comments
 (0)