mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 17:18:38 +00:00
One squashed commit of a fork that was developed on a Linux/Proton machine and tested on two native Windows machines. On all of them the retail 1.0.2.1 package now launches, shows its launch movie, first-run pages and landing prompt, runs the story load to its end with the loading slideshow drawing its artwork, and enters the world phase. A first world frame is not verified yet. docs/linux_proton.md has the setup, a healthy boot's timeline, the machines and where it stops. Runtime: frames in the title's own window; DirectStorage pinned reads, page-list scatter/gather, BCPack and unshuffle with a locally generated predictor file; pooled, device-local and deferred image backing for streamed and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table that degrades instead of refusing a submission; a descriptor hash finalizer (off until a library carries it); directly indexed heap offsets for compute dispatches and VS/PS/GS draws; title buffer windows outside a fragmented arena; opt-in device-local copies of streamed buffer views; write-fault retries under the tracker lock, shared trackers and write-watched title memory for native Windows; device-loss, compute-input and memory probes. Translator: loop bounds at structured exits, the eight FidelityFX programs that use a packed-dot form the disassembler skips, buffer bounds modes per view kind, a compaction option, the hash finalizer option. Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows boot script, the paired compute translation script. No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted by people on the machines named in docs/linux_proton.md. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
145 lines
8.4 KiB
C++
145 lines
8.4 KiB
C++
// A mapped program that reaches the GDK runtime the way xgameruntime.lib's thunks do: it has no import of
|
|
// xgameruntime.dll, loads it with LoadLibraryW and looks the five run-time entry points and XErrorReport up with
|
|
// GetProcAddress (measured in the package's libHttpClient.GDK.dll and XCurl.dll, docs/runtime_imports.md "Run-time
|
|
// loaded modules"). Run under `gta6rt --run`, its own kernel32 imports are bound by the loader, so LoadLibraryW and
|
|
// GetProcAddress must reach runtime/shims/library_loader.cpp and the offline xgameruntime shim. Run on its own,
|
|
// LoadLibraryW goes to Windows and either fails or loads the PC's Gaming Services runtime, and this never prints
|
|
// PASS. Ordinary test code; no game data.
|
|
#include <windows.h>
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <cwchar>
|
|
#include <initializer_list>
|
|
|
|
namespace {
|
|
|
|
struct Options { // InitializeApiImplEx2's fourth argument (xodus-gaming/xgameruntime-docs)
|
|
UINT32 unknown; // documented as always 1
|
|
BOOL is_inline;
|
|
const char* game_config;
|
|
};
|
|
|
|
using InitEx2 = HRESULT(WINAPI*)(ULONG, ULONG, char, const Options*);
|
|
using Query = HRESULT(WINAPI*)(const GUID*, REFIID, void**);
|
|
using Uninit = HRESULT(WINAPI*)();
|
|
using Report = HRESULT(WINAPI*)(HRESULT, const char*);
|
|
|
|
HRESULT g_reported = S_OK; // what the XErrorSetCallback callback last received
|
|
|
|
int fail(const char* stage, unsigned long value) {
|
|
std::printf("tiny_gdk_load: FAIL %s (0x%08lx)\n", stage, value);
|
|
return 1;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
for (const wchar_t* dll : {L"kernel32.dll", L"kernelbase.dll"}) {
|
|
HMODULE host = GetModuleHandleW(dll);
|
|
if (!host) return fail("host module handle", GetLastError());
|
|
auto command_a = reinterpret_cast<decltype(&GetCommandLineA)>(GetProcAddress(host, "GetCommandLineA"));
|
|
auto command_w = reinterpret_cast<decltype(&GetCommandLineW)>(GetProcAddress(host, "GetCommandLineW"));
|
|
if (!command_a || !command_w || std::strcmp(command_a(), GetCommandLineA()) != 0 ||
|
|
std::wcscmp(command_w(), GetCommandLineW()) != 0 ||
|
|
!std::strstr(command_a(), "host-shim-probe") || !std::wcsstr(command_w(), L"host-shim-probe"))
|
|
return fail("dynamic command line matches direct imports", 0);
|
|
auto process_id = reinterpret_cast<decltype(&GetCurrentProcessId)>(GetProcAddress(host, "GetCurrentProcessId"));
|
|
if (!process_id || process_id() != GetCurrentProcessId())
|
|
return fail("unshimmed host export", GetLastError());
|
|
SetLastError(0);
|
|
if (GetProcAddress(host, "LeonidaNoSuchHostExport") || GetLastError() != ERROR_PROC_NOT_FOUND)
|
|
return fail("missing host export", GetLastError());
|
|
auto create_file = reinterpret_cast<decltype(&CreateFileW)>(GetProcAddress(host, "CreateFileW"));
|
|
if (!create_file) return fail("dynamic CreateFileW", GetLastError());
|
|
HANDLE file = create_file(L"GAME:\\host_shim_probe.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (file == INVALID_HANDLE_VALUE) return fail("dynamic GAME: open", GetLastError());
|
|
char data[4]{};
|
|
DWORD count = 0;
|
|
const BOOL read = ReadFile(file, data, sizeof(data), &count, nullptr);
|
|
CloseHandle(file);
|
|
if (!read || count != sizeof(data) || std::memcmp(data, "shim", sizeof(data)) != 0)
|
|
return fail("dynamic GAME: contents", 0);
|
|
}
|
|
HMODULE runtime = LoadLibraryW(L"xgameruntime.dll");
|
|
if (!runtime) return fail("LoadLibraryW(xgameruntime.dll)", GetLastError());
|
|
// The loader shim hands out a pseudo-handle for a module it owns: not an image the host loaded.
|
|
HMODULE host_view = nullptr;
|
|
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"xgameruntime.dll", &host_view) && host_view == runtime)
|
|
return fail("xgameruntime.dll came from the host loader, not the shims", 0);
|
|
|
|
// XErrorReport too: the thunks look it up with the five entry points and refuse the runtime when it is missing.
|
|
const char* names[] = {"InitializeApiImpl", "InitializeApiImplEx", "InitializeApiImplEx2", "UninitializeApiImpl",
|
|
"QueryApiImpl", "XErrorReport"};
|
|
for (const char* name : names)
|
|
if (!GetProcAddress(runtime, name)) return fail(name, GetLastError());
|
|
SetLastError(0);
|
|
if (GetProcAddress(runtime, "NoSuchRuntimeFunction") || GetLastError() != ERROR_PROC_NOT_FOUND)
|
|
return fail("an unserved name is NULL with ERROR_PROC_NOT_FOUND", GetLastError());
|
|
|
|
auto init = reinterpret_cast<InitEx2>(GetProcAddress(runtime, "InitializeApiImplEx2"));
|
|
auto query = reinterpret_cast<Query>(GetProcAddress(runtime, "QueryApiImpl"));
|
|
auto uninit = reinterpret_cast<Uninit>(GetProcAddress(runtime, "UninitializeApiImpl"));
|
|
const Options options{1, FALSE, nullptr};
|
|
HRESULT hr = init(0, 0, 0x02, &options);
|
|
if (hr != S_OK) return fail("InitializeApiImplEx2", static_cast<unsigned long>(hr));
|
|
|
|
// XThreadingImpl, the first class XCurl.dll references (docs/runtime_imports.md), used as its thunks do: the
|
|
// class id as both ids, a slot with `this` first, then Release (slot 2). Slot 12 is XTaskQueueCreate, 17
|
|
// XTaskQueueCloseHandle.
|
|
const GUID threading = {0x073b7dcb, 0x1fcf, 0x4030, {0x94, 0xbe, 0xe3, 0xc9, 0xeb, 0x62, 0x34, 0x28}};
|
|
void* obj = nullptr;
|
|
hr = query(&threading, threading, &obj);
|
|
if (hr != S_OK || !obj) return fail("QueryApiImpl hands out XThreadingImpl", static_cast<unsigned long>(hr));
|
|
void* const* vtable = *static_cast<void* const* const*>(obj);
|
|
using Create = HRESULT(STDMETHODCALLTYPE*)(void*, UINT32, UINT32, void**);
|
|
using Close = void(STDMETHODCALLTYPE*)(void*, void*);
|
|
using Release = ULONG(STDMETHODCALLTYPE*)(void*);
|
|
void* queue = nullptr;
|
|
hr = reinterpret_cast<Create>(vtable[12])(obj, 0, 0, &queue); // XTaskQueueDispatchMode::Manual twice
|
|
if (hr != S_OK || !queue) return fail("XThreadingImpl slot 12 (XTaskQueueCreate)", static_cast<unsigned long>(hr));
|
|
reinterpret_cast<Close>(vtable[17])(obj, queue);
|
|
reinterpret_cast<Release>(vtable[2])(obj);
|
|
|
|
// The XErrorReport export and XErrorImpl's slot 3 are one function: a report through the export reaches the
|
|
// callback XErrorImpl's slot 4 (XErrorSetCallback) set.
|
|
const GUID error_class = {0x8ca467f7, 0x22e8, 0x4096, {0x84, 0x56, 0xbb, 0x8a, 0xa1, 0x3f, 0x79, 0xd8}};
|
|
void* error = nullptr;
|
|
hr = query(&error_class, error_class, &error);
|
|
if (hr != S_OK || !error) return fail("QueryApiImpl hands out XErrorImpl", static_cast<unsigned long>(hr));
|
|
void* const* error_vtable = *static_cast<void* const* const*>(error);
|
|
using Callback = bool(CALLBACK*)(HRESULT, const char*, void*);
|
|
using SetCallback = void(STDMETHODCALLTYPE*)(void*, Callback, void*);
|
|
const Callback on_error = [](HRESULT reported, const char*, void*) {
|
|
g_reported = reported;
|
|
return true;
|
|
};
|
|
reinterpret_cast<SetCallback>(error_vtable[4])(error, on_error, nullptr);
|
|
auto report = reinterpret_cast<Report>(GetProcAddress(runtime, "XErrorReport"));
|
|
hr = report(E_FAIL, "tiny_gdk_load");
|
|
reinterpret_cast<SetCallback>(error_vtable[4])(error, nullptr, nullptr);
|
|
reinterpret_cast<Release>(error_vtable[2])(error);
|
|
if (hr != S_OK || g_reported != E_FAIL)
|
|
return fail("the XErrorReport export reaches the XErrorSetCallback callback", static_cast<unsigned long>(hr));
|
|
|
|
// A class not served (an id no runtime class has, made up for this test) is refused, with the out pointer
|
|
// cleared.
|
|
const GUID unserved = {0x4c454f4e, 0x4944, 0x4100, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}};
|
|
void* out = reinterpret_cast<void*>(1);
|
|
hr = query(&unserved, unserved, &out);
|
|
if (hr != static_cast<HRESULT>(0x80070032) || out != nullptr)
|
|
return fail("QueryApiImpl refuses an unprovided class and clears its out pointer", static_cast<unsigned long>(hr));
|
|
hr = uninit();
|
|
if (hr != S_OK) return fail("UninitializeApiImpl", static_cast<unsigned long>(hr));
|
|
|
|
// Host modules still come from the host.
|
|
HMODULE k32 = LoadLibraryW(L"kernel32.dll");
|
|
if (!k32 || GetProcAddress(k32, "GetTickCount") != GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetTickCount"))
|
|
return fail("kernel32.dll from the host", GetLastError());
|
|
if (!FreeLibrary(runtime)) return fail("FreeLibrary(xgameruntime.dll)", GetLastError());
|
|
|
|
std::puts("tiny_gdk_load: PASS");
|
|
return 0;
|
|
}
|