Files
praxisandClaude Fable 5.2 8e02dccb86 In-client stack: transport, session lifecycle and engine bridge
175 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

154 lines
7.2 KiB
C

/* App-local Agility/VKD3D forwarding candidate. No DllMain, worker, environment
* dependency, system-file replacement or external startup controller. */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdint.h>
#include "d3d12core_proxy_forward.h"
__declspec(dllexport) const UINT D3D12SDKVersion = 618;
struct GwCoreProxyStatus {
uint32_t abi, size;
volatile LONG calls, successes, load_error;
uint32_t reserved;
uint64_t provider;
volatile LONG bridge_attempts, bridge_error, bridge_signals;
uint32_t reserved2;
uint64_t bridge;
};
__declspec(dllexport) struct GwCoreProxyStatus gw_core_proxy_status_v1 = {
.abi = 2, .size = sizeof(struct GwCoreProxyStatus)
};
static INIT_ONCE provider_once = INIT_ONCE_STATIC_INIT;
static INIT_ONCE bridge_once = INIT_ONCE_STATIC_INIT;
static GwCoreGetInterface provider_get_interface;
static GwCoreReady bridge_ready;
static HMODULE provider_module;
static LONG bridge_signal_once;
static BOOL sibling_path(HMODULE owner, const WCHAR *filename, WCHAR *path, DWORD capacity) {
DWORD count = GetModuleFileNameW(owner, path, capacity), slash = 0, i, length = 0;
if (!count || count >= capacity) {
SetLastError(ERROR_INSUFFICIENT_BUFFER); return FALSE;
}
for (i = 0; i < count; ++i) if (path[i] == L'\\' || path[i] == L'/') slash = i + 1;
while (filename[length]) ++length;
if (!slash || slash + length + 1 > capacity) {
SetLastError(ERROR_BAD_PATHNAME); return FALSE;
}
for (i = 0; i <= length; ++i) path[slash + i] = filename[i];
return TRUE;
}
static BOOL CALLBACK load_provider(PINIT_ONCE once, PVOID parameter, PVOID *context) {
const WCHAR *filename;
WCHAR path[32768];
HMODULE self = NULL, module;
union { FARPROC address; GwCoreGetInterface function; } symbol;
(void)once; (void)parameter; (void)context;
/* One PE uses the normal rendering provider for its current OS. Both
* immutable vendor DLLs are package siblings; no environment/prefix or
* basename search selects project code. */
{
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
BOOL wine = ntdll && GetProcAddress(ntdll, "wine_get_version");
filename = wine ? L"GWGraphicsProviderWine.dll" : L"GWGraphicsProviderWindows.dll";
gw_core_proxy_status_v1.reserved = wine ? 1u : 0u;
}
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)(uintptr_t)&load_provider, &self)) goto failed;
if (!sibling_path(self, filename, path, sizeof(path) / sizeof(path[0]))) goto failed;
module = LoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (!module) goto failed;
symbol.address = GetProcAddress(module, "D3D12GetInterface");
if (!symbol.address || module == self) {
DWORD error = symbol.address ? ERROR_BAD_EXE_FORMAT : GetLastError();
FreeLibrary(module); SetLastError(error); goto failed;
}
provider_module = module; provider_get_interface = symbol.function;
gw_core_proxy_status_v1.provider = (uint64_t)(uintptr_t)module;
InterlockedExchange(&gw_core_proxy_status_v1.load_error, 0);
return TRUE;
failed:
InterlockedExchange(&gw_core_proxy_status_v1.load_error, (LONG)GetLastError());
return FALSE;
}
static BOOL CALLBACK load_optional_bridge(PINIT_ONCE once, PVOID parameter, PVOID *context) {
WCHAR path[32768], observed[32768];
HMODULE module;
#ifndef GW_D3D12_BRIDGE_SYSTEM32_CANDIDATE
HMODULE self = NULL;
#endif
DWORD length, error;
union { FARPROC address; GwCoreReady function; } symbol;
(void)once; (void)parameter; (void)context;
InterlockedIncrement(&gw_core_proxy_status_v1.bridge_attempts);
#ifdef GW_D3D12_BRIDGE_SYSTEM32_CANDIDATE
/* Explicitly selected isolated placement experiment. The installed
* runtime must be in this OS directory; do not search other locations. */
{
static const WCHAR filename[] = L"\\GWNativeBridge.dll";
DWORD i, count = GetSystemDirectoryW(path, sizeof(path) / sizeof(path[0]));
if (!count || count >= sizeof(path) / sizeof(path[0]) ||
count + sizeof(filename) / sizeof(filename[0]) > sizeof(path) / sizeof(path[0])) {
SetLastError(ERROR_INSUFFICIENT_BUFFER); goto failed;
}
for (i = 0; i < sizeof(filename) / sizeof(filename[0]); ++i) path[count + i] = filename[i];
}
#else
/* The private assembly owns the complete runtime dependency set. Resolve
* Bridge beside this module, exactly as the real graphics providers are
* resolved; never fall back to the application or current directory. */
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)(uintptr_t)&load_optional_bridge, &self)) goto failed;
if (!sibling_path(self, L"GWNativeBridge.dll", path, sizeof(path) / sizeof(path[0]))) goto failed;
#endif
module = LoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (!module) goto failed;
length = GetModuleFileNameW(module, observed, sizeof(observed) / sizeof(observed[0]));
if (!length || length >= sizeof(observed) / sizeof(observed[0]) ||
CompareStringOrdinal(path, -1, observed, -1, TRUE) != CSTR_EQUAL) {
error = ERROR_BAD_PATHNAME; goto unload;
}
symbol.address = GetProcAddress(module, "gw_startup_D3D12CoreInterfaceReady");
if (!symbol.address) { error = GetLastError(); goto unload; }
bridge_ready = symbol.function;
gw_core_proxy_status_v1.bridge = (uint64_t)(uintptr_t)module;
return TRUE;
unload:
FreeLibrary(module); SetLastError(error);
failed:
InterlockedExchange(&gw_core_proxy_status_v1.bridge_error, (LONG)GetLastError());
/* Absence is a completed optional attempt, not a retry on every graphics
* request. A matching Bridge is installed before a new process lifetime. */
return TRUE;
}
static void GW_CORE_CALL signal_bridge(void *provider) {
if (InterlockedCompareExchange(&bridge_signal_once, 1, 0)) return;
InterlockedIncrement(&gw_core_proxy_status_v1.bridge_signals);
bridge_ready(provider);
}
static GwCoreReady resolve_optional_bridge(void) {
InterlockedIncrement(&gw_core_proxy_status_v1.successes);
if (!InitOnceExecuteOnce(&bridge_once, load_optional_bridge, NULL, NULL)) {
InterlockedExchange(&gw_core_proxy_status_v1.bridge_error, (LONG)GetLastError());
return NULL;
}
return bridge_ready ? signal_bridge : NULL;
}
__declspec(dllexport) HRESULT WINAPI D3D12GetInterface(const void *clsid,
const void *iid, void **object) {
InterlockedIncrement(&gw_core_proxy_status_v1.calls);
if (!InitOnceExecuteOnce(&provider_once, load_provider, NULL, NULL)) {
DWORD error = (DWORD)gw_core_proxy_status_v1.load_error;
return HRESULT_FROM_WIN32(error ? error : ERROR_DLL_INIT_FAILED);
}
return gw_core_forward(provider_get_interface, clsid, iid, object,
provider_module, resolve_optional_bridge);
}