diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:41:01 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:41:01 -0500 |
| commit | b05999eb74a7ec2c86f5e00d9f1e3fe8025227e6 (patch) | |
| tree | 9c8e6385a4b01bd3d59bf7457bc932adea6ea448 /runtime/src/pe_loader.cpp | |
| parent | 3c96bb69607dc8c3c5eb4c76cf142f6b23fb8f5e (diff) | |
| parent | 7fc6fa72851362875b7105087030ac651db92b35 (diff) | |
Merge remote-tracking branch 'origin/pr/8969'
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 1f6ec0d33..5e55ac1f9 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1066,6 +1066,9 @@ struct RuntimeLoadScope { | |||
| 1066 | } // namespace | 1066 | } // namespace |
| 1067 | 1067 | ||
| 1068 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { | 1068 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { |
| 1069 | std::vector<wchar_t> compat(32768); | ||
| 1070 | const DWORD n = GetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.data(), static_cast<DWORD>(compat.size())); | ||
| 1071 | if (n > 0 && n < compat.size()) compat_dir_ = std::filesystem::path(compat.data()); | ||
| 1069 | shims::add_module_lookup(this, &lookup_in_loader); | 1072 | shims::add_module_lookup(this, &lookup_in_loader); |
| 1070 | shims::add_bundled_loader(this, &kBundledOps); | 1073 | shims::add_bundled_loader(this, &kBundledOps); |
| 1071 | } | 1074 | } |
| @@ -1705,10 +1708,41 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 1705 | return reinterpret_cast<void*>(p); | 1708 | return reinterpret_cast<void*>(p); |
| 1706 | } | 1709 | } |
| 1707 | 1710 | ||
| 1711 | bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal, | ||
| 1712 | bool by_ordinal, void*& out) { | ||
| 1713 | out = nullptr; | ||
| 1714 | if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false; | ||
| 1715 | HMODULE h = nullptr; | ||
| 1716 | if (auto it = compat_modules_.find(key); it != compat_modules_.end()) { | ||
| 1717 | h = static_cast<HMODULE>(it->second); | ||
| 1718 | } else { | ||
| 1719 | const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll); | ||
| 1720 | std::error_code ec; | ||
| 1721 | if (!std::filesystem::is_regular_file(candidate, ec)) return false; | ||
| 1722 | // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game | ||
| 1723 | // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. | ||
| 1724 | h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 1725 | if (!h) | ||
| 1726 | throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " + | ||
| 1727 | "load it (error " + std::to_string(GetLastError()) + ")"); | ||
| 1728 | compat_modules_.emplace(key, h); | ||
| 1729 | std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); | ||
| 1730 | } | ||
| 1731 | out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str())); | ||
| 1732 | return true; | ||
| 1733 | } | ||
| 1734 | |||
| 1708 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { | 1735 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { |
| 1709 | std::string key = lower(dll); | 1736 | std::string key = lower(dll); |
| 1710 | std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; | 1737 | std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; |
| 1711 | 1738 | ||
| 1739 | // Opt-in compat modules come first: a DLL the player put in LEONIDA_COMPAT_DIR owns all of its imports, so one | ||
| 1740 | // console module is never served half by a shim and half by the drop-in (they would not share state). | ||
| 1741 | if (void* p = nullptr; resolve_compat(key, dll, name, ordinal, by_ordinal, p)) { | ||
| 1742 | m.imports.push_back({dll, sym, p ? Binding::Host : Binding::Unimplemented}); | ||
| 1743 | return p ? p : shims::make_unimplemented_thunk(dll, sym); | ||
| 1744 | } | ||
| 1745 | |||
| 1712 | if (void* p = shims::find(key, sym)) { | 1746 | if (void* p = shims::find(key, sym)) { |
| 1713 | m.imports.push_back({dll, sym, Binding::Shim}); | 1747 | m.imports.push_back({dll, sym, Binding::Shim}); |
| 1714 | return p; | 1748 | return p; |