upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/registry.cpp
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-20 15:22:16 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-20 15:22:16 -0500
commitdb0dc8bd5ebce6e2bc27d47085f7f104eb3037d3 (patch)
tree4fb3edca45f8b4dd3368c6ac2a0e4fdc8ed7065c /runtime/shims/registry.cpp
Initial commit: Leonida, build-it-yourself PC port groundwork
Shader pipeline (carve, FSSM unpack, gfx1013 disassembly, RDNA2 to SPIR-V), asset export via rpf9-explorer's Core (submodule), PE loader and shim skeleton, and make_game.py, which builds from the user's own copy of the game. No game data, keys or executables are included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/registry.cpp')
-rw-r--r--runtime/shims/registry.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/runtime/shims/registry.cpp b/runtime/shims/registry.cpp
new file mode 100644
index 000000000..e10ccb051
--- /dev/null
+++ b/runtime/shims/registry.cpp
@@ -0,0 +1,81 @@
1#include <windows.h>
2
3#include <cstdio>
4#include <cstring>
5#include <map>
6#include <mutex>
7#include <set>
8#include <string>
9
10#include "shims.h"
11
12namespace rt::shims {
13namespace {
14
15std::map<std::string, void*>& table() {
16 static std::map<std::string, void*> t; // "dll!name"
17 return t;
18}
19
20std::mutex g_called_mutex;
21std::set<std::string> g_called;
22
23// Called by the thunks with the import's "dll!name" string.
24extern "C" uint64_t rt_unimplemented(const char* what) {
25 std::lock_guard lock(g_called_mutex);
26 if (g_called.insert(what).second) std::fprintf(stderr, "[rt] unimplemented: %s\n", what);
27 return 0;
28}
29
30// Thunk layout (x86-64):
31// 48 B9 <imm64> mov rcx, what
32// 48 B8 <imm64> mov rax, rt_unimplemented
33// 0F 57 C0 xorps xmm0, xmm0
34// FF E0 jmp rax (tail call: returns straight to the game's caller)
35constexpr size_t kThunkSize = 32;
36
37uint8_t* g_pool = nullptr;
38size_t g_pool_used = 0, g_pool_size = 0;
39std::mutex g_pool_mutex;
40
41} // namespace
42
43Table::Table(const Entry* entries, size_t count) {
44 for (size_t i = 0; i < count; ++i) table()[std::string(entries[i].dll) + "!" + entries[i].name] = entries[i].fn;
45}
46
47void* find(const std::string& dll_lower, const std::string& name) {
48 auto it = table().find(dll_lower + "!" + name);
49 return it == table().end() ? nullptr : it->second;
50}
51
52void* make_unimplemented_thunk(const std::string& dll, const std::string& name) {
53 std::lock_guard lock(g_pool_mutex);
54 if (!g_pool || g_pool_used + kThunkSize > g_pool_size) {
55 g_pool_size = 1 << 20;
56 g_pool = static_cast<uint8_t*>(VirtualAlloc(nullptr, g_pool_size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE));
57 g_pool_used = 0;
58 if (!g_pool) return nullptr;
59 }
60 const char* what = _strdup((dll + "!" + name).c_str()); // lives for the process
61 uint8_t* t = g_pool + g_pool_used;
62 g_pool_used += kThunkSize;
63 uint64_t a = reinterpret_cast<uint64_t>(what), f = reinterpret_cast<uint64_t>(&rt_unimplemented);
64 uint8_t code[kThunkSize] = {0x48, 0xB9};
65 std::memcpy(code + 2, &a, 8);
66 code[10] = 0x48; code[11] = 0xB8;
67 std::memcpy(code + 12, &f, 8);
68 code[20] = 0x0F; code[21] = 0x57; code[22] = 0xC0;
69 code[23] = 0xFF; code[24] = 0xE0;
70 std::memset(code + 25, 0xCC, kThunkSize - 25);
71 std::memcpy(t, code, kThunkSize);
72 FlushInstructionCache(GetCurrentProcess(), t, kThunkSize);
73 return t;
74}
75
76void report_called_unimplemented() {
77 std::lock_guard lock(g_called_mutex);
78 for (auto& s : g_called) std::fprintf(stderr, " called: %s\n", s.c_str());
79}
80
81} // namespace rt::shims