feat(entry): phase-0 proxy gameMain forwarding to the original

This commit is contained in:
ulw-execute committed 2026-11-28 04:53:34 -05:00
1 parent cb53dd7e94
commit 643b4724a4
1 file changed
+69 -2
+69 -2
View File
@@ -1,6 +1,73 @@
#include "gk/types.h"
// Phase-0 forwarding proxy for gamekdm.dll's single export, gameMain @1: at
// call time it LoadLibraryA's the original DLL, resolves its gameMain, and
// forwards hInstance, returning the original's result. Only KERNEL32 and
// msvcrt symbols are used so the rebuilt DLL's import table stays
// KERNEL32+msvcrt only, matching the original (IS-2); export/gamekdm.def pins
// the export table to gameMain @1 so nothing else leaks out.
#include <windows.h>
#include <cstdio>
static const char kOrigDllName[] = "gamekdm_orig.dll";
// cdecl with a single pointer arg, verified from the original binary
// (bare `ret`, caller cleans the arg; docs/re/gamemain-binding.md part (d)).
typedef int(GK_CDECL* gameMain_fn)(void* hInstance);
// stderr (visible under wine) + OutputDebugString so the forward path is
// observable from either a console or a debugger.
static void proxy_log(const char* msg) {
std::fprintf(stderr, "%s\n", msg);
std::fflush(stderr);
OutputDebugStringA(msg);
OutputDebugStringA("\n");
}
static void proxy_logf(const char* fmt, ...) {
char buf[256];
va_list ap;
va_start(ap, fmt);
std::vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
proxy_log(buf);
}
extern "C" int gameMain(void* hInstance) {
(void)hInstance;
return 0;
proxy_logf("[gamekdm-proxy] gameMain entered, hInstance=%p", hInstance);
HMODULE orig = LoadLibraryA(kOrigDllName);
if (orig == NULL) {
// Missing original or its own deps failed: clean error, never a crash.
proxy_logf("[gamekdm-proxy] ERROR: LoadLibraryA(\"%s\") failed (GetLastError=%lu)",
kOrigDllName, (unsigned long)GetLastError());
return -1;
}
proxy_logf("[gamekdm-proxy] LoadLibraryA(\"%s\") -> %p", kOrigDllName, (void*)orig);
// By name first (as arkkdm.dll's own resolver does), then ordinal 1.
gameMain_fn fn = (gameMain_fn)GetProcAddress(orig, "gameMain");
if (fn == NULL) {
fn = (gameMain_fn)GetProcAddress(orig, MAKEINTRESOURCEA(1));
if (fn != NULL) {
proxy_log("[gamekdm-proxy] gameMain resolved by ordinal 1 (name lookup missed)");
}
} else {
proxy_logf("[gamekdm-proxy] GetProcAddress(\"gameMain\") -> %p", (void*)fn);
}
if (fn == NULL) {
proxy_logf("[gamekdm-proxy] ERROR: gameMain not found in \"%s\" (GetLastError=%lu)",
kOrigDllName, (unsigned long)GetLastError());
FreeLibrary(orig);
return -2;
}
proxy_log("[gamekdm-proxy] forwarding to original gameMain ...");
int result = fn(hInstance);
proxy_logf("[gamekdm-proxy] original gameMain returned %d", result);
// Leave the original resident (it owns global game state); no FreeLibrary.
return result;
}