Files
WT-Offline/native/wto-net/src/dll_main.c
T
N0VAN0M4LY 96077153c6 refactor(native): batch MinHook enable calls and log status codes
Switch ws2_32 and LocalMP hook installers to MH_QueueEnableHook with two MH_ApplyQueued passes (mandatory network hooks first, then all LocalMP modules), reducing DllMain thread-freeze cycles from 26 to 2. Roll back partial module hooks on error and log MH_StatusToString diagnostics to wto_ws.log.

Checked: native/wto-net/build.ps1 compiles cleanly with zero warnings in project sources, and PE inspection confirms WS2_32/KERNEL32 imports and 49 exports. In-game verification pending.
2026-12-02 07:24:27 +08:00

83 lines
2.7 KiB
C

/*
* wto_ws.dll: the network hook library of WTOffline.
*
* aces.exe imports its network functions from this DLL instead of WS2_32.dll
* (patches/exe/online/network-hook-library). Every export is forwarded to the system's
* ws2_32.dll (wto_ws.def); on load the DLL hooks the resolution, connection and datagram
* functions of the system's ws2_32.dll itself with MinHook, so the calls of every module in
* the process pass through the hooks, not only the executable's.
*
* If the hooks cannot be installed the DLL fails to load and the game does not start: running
* unhooked would let the game reach Gaijin's servers.
*/
#include "hooks.h"
#include "hook_config.h"
#include "MinHook.h"
static HMODULE g_ws2;
int hook_export(const char *name, void *replacement, void **original)
{
void *target = (void *)GetProcAddress(g_ws2, name);
MH_STATUS status;
if (target == NULL) {
log_line("hook %s: not exported by ws2_32.dll", name);
return 0;
}
status = MH_CreateHook(target, replacement, original);
if (status != MH_OK) {
log_line("hook %s: MH_CreateHook failed (%s)", name, MH_StatusToString(status));
return 0;
}
status = MH_QueueEnableHook(target);
if (status != MH_OK) {
log_line("hook %s: MH_QueueEnableHook failed (%s)", name, MH_StatusToString(status));
MH_RemoveHook(target);
return 0;
}
return 1;
}
static int install(void)
{
MH_STATUS status;
/* ws2_32.dll is already loaded: this DLL links against it. */
g_ws2 = GetModuleHandleW(L"ws2_32.dll");
if (g_ws2 == NULL) {
log_line("wto_ws: ws2_32.dll module handle unavailable");
return 0;
}
status = MH_Initialize();
if (status != MH_OK) {
log_line("wto_ws: MH_Initialize failed (%s)", MH_StatusToString(status));
return 0;
}
if (!install_resolver_hooks() || !install_connection_hooks())
return 0;
/* Enable all mandatory network hooks in one thread-freeze pass before touching LocalMP hooks. */
status = MH_ApplyQueued();
if (status != MH_OK) {
log_line("wto_ws: MH_ApplyQueued for ws2_32 failed (%s)", MH_StatusToString(status));
return 0;
}
install_local_mp_hooks();
return 1;
}
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
(void)reserved;
if (reason != DLL_PROCESS_ATTACH)
return TRUE;
DisableThreadLibraryCalls(instance);
config_load(instance);
if (!install()) {
/* Failing DllMain makes the loader refuse to start the game. */
log_line("wto_ws: hooks not installed, refusing to load so the game cannot go online");
return FALSE;
}
log_line("wto_ws: hooks installed");
return TRUE;
}