upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index e5becb472..318362746 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -944,6 +944,9 @@ struct RuntimeLoadScope {
944} // namespace 944} // namespace
945 945
946Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { 946Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) {
947 std::vector<wchar_t> compat(32768);
948 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.data(), static_cast<DWORD>(compat.size()));
949 if (n > 0 && n < compat.size()) compat_dir_ = std::filesystem::path(compat.data());
947 shims::add_module_lookup(this, &lookup_in_loader); 950 shims::add_module_lookup(this, &lookup_in_loader);
948 shims::add_bundled_loader(this, &kBundledOps); 951 shims::add_bundled_loader(this, &kBundledOps);
949} 952}
@@ -1563,10 +1566,41 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
1563 return reinterpret_cast<void*>(p); 1566 return reinterpret_cast<void*>(p);
1564} 1567}
1565 1568
1569bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal,
1570 bool by_ordinal, void*& out) {
1571 out = nullptr;
1572 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false;
1573 HMODULE h = nullptr;
1574 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) {
1575 h = static_cast<HMODULE>(it->second);
1576 } else {
1577 const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll);
1578 std::error_code ec;
1579 if (!std::filesystem::is_regular_file(candidate, ec)) return false;
1580 // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game
1581 // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL.
1582 h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
1583 if (!h)
1584 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " +
1585 "load it (error " + std::to_string(GetLastError()) + ")");
1586 compat_modules_.emplace(key, h);
1587 std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str());
1588 }
1589 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()));
1590 return true;
1591}
1592
1566void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { 1593void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {
1567 std::string key = lower(dll); 1594 std::string key = lower(dll);
1568 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; 1595 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name;
1569 1596
1597 // Opt-in compat modules come first: a DLL the player put in LEONIDA_COMPAT_DIR owns all of its imports, so one
1598 // console module is never served half by a shim and half by the drop-in (they would not share state).
1599 if (void* p = nullptr; resolve_compat(key, dll, name, ordinal, by_ordinal, p)) {
1600 m.imports.push_back({dll, sym, p ? Binding::Host : Binding::Unimplemented});
1601 return p ? p : shims::make_unimplemented_thunk(dll, sym);
1602 }
1603
1570 if (void* p = shims::find(key, sym)) { 1604 if (void* p = shims::find(key, sym)) {
1571 m.imports.push_back({dll, sym, Binding::Shim}); 1605 m.imports.push_back({dll, sym, Binding::Shim});
1572 return p; 1606 return p;