upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/library_loader.cpp
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-27 17:58:38 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-27 17:58:38 -0500
commit00131bb85ee294179e5179b00a2c68f4ea3610b5 (patch)
tree11854f35d928e964919b81746360146c3657ce07 /runtime/shims/library_loader.cpp
parenteac96d744c3a899fa6e326e296f387fe11e05eab (diff)
main keeps its own runtime, tools and tests; the branch's variants stay on port/d3d12-nvidia
After the merge the tree held this branch's older states of files main maintains. Run against main's code, tests/test_acphal_abi.py and 11 of the 15 translator tests that differed failed, and the C++ runtime would have been built from two generations of its sources. So that the merge cannot break what main builds and tests: - runtime/ is main's, file for file (68 files only this branch had are not carried over); - every file under tools/, tests/ and docs/ that both sides have is main's (80 files); - ten translator tests only this branch has, which test rules main's translator does not have, and their fixtures are left out. What main gains: d3d12/ (the Rust D3D12 runtime), its launchers and helpers (Launcher.bat, scripts/play*.ps1, play-linux.sh, port_ctl.py, smooth-motion.ps1, fetch_vkd3d_proton.py, sync_victory.py), mods/example_lua, eleven notes under docs/, seven tools and the tests that pass with main's code, the README's sections and credits, and the "Runtime traps" section of AGENTS.md. Everything left out here is on the branch port/d3d12-nvidia. Checked: compileall; make_game.py --help; check_no_game_data.py over every file this adds to main; the line-ending, docs-encoding, CI-registration and docs tests; each test file that differs from main, on its own. Not run: the whole translator suite, the C++ build, cargo over d3d12/ after the merge (d3d12/ is byte for byte the branch's), CI. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp54
1 files changed, 2 insertions, 52 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index f0a86221f..3a96092df 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -14,7 +14,6 @@
14// loader see these: they are bound through the registry, the runtime's own code calls the host directly. 14// loader see these: they are bound through the registry, the runtime's own code calls the host directly.
15#include <windows.h> 15#include <windows.h>
16 16
17#include <algorithm>
18#include <cstdio> 17#include <cstdio>
19#include <deque> 18#include <deque>
20#include <map> 19#include <map>
@@ -202,12 +201,8 @@ HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
202} 201}
203 202
204// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any 203// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any
205// bundled package module already mapped by the native loader. NULL names the title's main image once main.cpp has 204// bundled package module already mapped by the native loader.
206// set it (VIctory victory-hle src/modules/kernel.rs, GPL-3.0-or-later: "GetModuleHandleW(NULL) must return the
207// title's image, not our exe"), as the console's loader would answer: the title's process is its own.
208HMODULE WINAPI get_module_handle_a(LPCSTR name) { 205HMODULE WINAPI get_module_handle_a(LPCSTR name) {
209 if (!name)
210 if (void* main = main_image()) return static_cast<HMODULE>(main);
211 if (name) { 206 if (name) {
212 if (HMODULE h = owned_handle(name)) return h; 207 if (HMODULE h = owned_handle(name)) return h;
213 const std::string dll = module_name(name); 208 const std::string dll = module_name(name);
@@ -218,8 +213,6 @@ HMODULE WINAPI get_module_handle_a(LPCSTR name) {
218} 213}
219 214
220HMODULE WINAPI get_module_handle_w(LPCWSTR name) { 215HMODULE WINAPI get_module_handle_w(LPCWSTR name) {
221 if (!name)
222 if (void* main = main_image()) return static_cast<HMODULE>(main);
223 if (name) { 216 if (name) {
224 if (ascii_basename(name)) { 217 if (ascii_basename(name)) {
225 const std::string path = narrow(name); 218 const std::string path = narrow(name);
@@ -284,49 +277,6 @@ FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) {
284 return GetProcAddress(module, name); 277 return GetProcAddress(module, name);
285} 278}
286 279
287// GetModuleFileName of a mapped image (NULL: the main image, once set) is the file it was mapped from, the title's
288// own path, not gta6rt.exe's; Windows cannot answer for an image it did not map (ERROR_MOD_NOT_FOUND). Ported from
289// VIctory kernel.rs: the title relaunches itself and finds its folder through this path. The copy follows
290// Windows' rules: a buffer too small gets the truncated, terminated path, the return value `size` and
291// ERROR_INSUFFICIENT_BUFFER; otherwise the length without the terminator.
292bool mapped_module_path(HMODULE module, std::wstring& path) {
293 void* const base = module ? static_cast<void*>(module) : main_image();
294 return base && bundled_module_path(base, path);
295}
296
297template <class Char> DWORD copy_module_path(const std::basic_string<Char>& path, Char* out, DWORD size) {
298 if (!out || size == 0) {
299 SetLastError(ERROR_INSUFFICIENT_BUFFER);
300 return 0;
301 }
302 if (path.size() >= size) {
303 std::copy_n(path.data(), size - 1, out);
304 out[size - 1] = 0;
305 SetLastError(ERROR_INSUFFICIENT_BUFFER);
306 return size;
307 }
308 std::copy_n(path.data(), path.size(), out);
309 out[path.size()] = 0;
310 SetLastError(ERROR_SUCCESS);
311 return static_cast<DWORD>(path.size());
312}
313
314DWORD WINAPI get_module_file_name_w(HMODULE module, LPWSTR out, DWORD size) {
315 std::wstring path;
316 if (!mapped_module_path(module, path)) return GetModuleFileNameW(module, out, size);
317 return copy_module_path(path, out, size);
318}
319
320DWORD WINAPI get_module_file_name_a(HMODULE module, LPSTR out, DWORD size) {
321 std::wstring path;
322 if (!mapped_module_path(module, path)) return GetModuleFileNameA(module, out, size);
323 // In the ANSI code page, as GetModuleFileNameA gives it.
324 const int n = WideCharToMultiByte(CP_ACP, 0, path.c_str(), -1, nullptr, 0, nullptr, nullptr);
325 std::string narrow(n > 0 ? static_cast<size_t>(n - 1) : 0, ' ');
326 if (n > 1) WideCharToMultiByte(CP_ACP, 0, path.c_str(), -1, narrow.data(), n, nullptr, nullptr);
327 return copy_module_path(narrow, out, size);
328}
329
330BOOL WINAPI free_library(HMODULE module) { 280BOOL WINAPI free_library(HMODULE module) {
331 if (pseudo(module)) return TRUE; // owned modules are never unloaded 281 if (pseudo(module)) return TRUE; // owned modules are never unloaded
332 if (owns_bundled_module_handle(module)) return TRUE; // mapped modules stay live until ~Loader detaches them 282 if (owns_bundled_module_handle(module)) return TRUE; // mapped modules stay live until ~Loader detaches them
@@ -353,7 +303,7 @@ BOOL WINAPI disable_thread_calls(HMODULE module) {
353 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \ 303 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \
354 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ 304 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \
355 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \ 305 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \
356 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}, {dll, "GetModuleFileNameA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_a)}, {dll, "GetModuleFileNameW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_w)} 306 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}
357 307
358} // namespace 308} // namespace
359} // namespace rt::shims::library_loader 309} // namespace rt::shims::library_loader