From f9a6333c940ebe95fecc7bac827a85619c1560f6 Mon Sep 17 00:00:00 2001 From: Chloe <25387744+qimiko@users.noreply.github.com> Date: Sat, 14 Sep 2024 20:23:16 -0700 Subject: [PATCH] possibly mac native binaries --- loader/src/loader/ModImpl.cpp | 5 ++++- loader/src/platform/mac/LoaderImpl.mm | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/loader/src/loader/ModImpl.cpp b/loader/src/loader/ModImpl.cpp index 68b1638d..555cbbc1 100644 --- a/loader/src/loader/ModImpl.cpp +++ b/loader/src/loader/ModImpl.cpp @@ -66,7 +66,10 @@ Result<> Mod::Impl::setup() { CCFileUtils::get()->addSearchPath(searchPathRoot.string().c_str()); }); - const auto binariesDir = searchPathRoot / m_metadata.getID() / "binaries" / PlatformID::toShortString(GEODE_PLATFORM_TARGET); + // binaries on macos are merged, so make the platform binaries merged as well + auto const binaryPlatformId = PlatformID::toShortString(GEODE_PLATFORM_TARGET GEODE_MACOS(, true)); + + auto const binariesDir = searchPathRoot / m_metadata.getID() / "binaries" / binaryPlatformId; if (std::filesystem::exists(binariesDir)) LoaderImpl::get()->addNativeBinariesPath(binariesDir); diff --git a/loader/src/platform/mac/LoaderImpl.mm b/loader/src/platform/mac/LoaderImpl.mm index 11956e1c..34bc0ead 100644 --- a/loader/src/platform/mac/LoaderImpl.mm +++ b/loader/src/platform/mac/LoaderImpl.mm @@ -8,6 +8,7 @@ #include #include #include +#include using namespace geode::prelude; @@ -132,7 +133,31 @@ bool Loader::Impl::userTriedToLoadDLLs() const { } void Loader::Impl::addNativeBinariesPath(std::filesystem::path const& path) { - log::warn("LoaderImpl::addNativeBinariesPath not implement on this platform, not adding path {}", path.string()); + // this takes advantage of dyld using already loaded binaries when loading relative shared libraries + // however, this also means that the binaries are loaded, which could have some weird side effects + // but if you could use dlopen (and thus control when libraries are loaded), then you wouldn't be using this, would you? + + for (const auto& entry : std::filesystem::directory_iterator(path)) { + if (!entry.is_regular_file()) { + continue; + } + + auto& entry_path = entry.path(); + + if (entry_path.extension() != ".dylib") { + continue; + } + + auto handle = dlopen(entry_path.string().c_str(), RTLD_LAZY); + + if (!handle) { + auto err = dlerror(); + log::warn("failed to load native binary at {}: dlerror returned ({})", entry_path.string(), err); + continue; + } + + dlclose(handle); + } } std::string Loader::Impl::getGameVersion() {