geode/loader/src/platform/mac/Mod.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#include <Geode/DefaultInclude.hpp>
#ifdef GEODE_IS_MACOS
2022-10-30 14:56:36 -04:00
#include <Geode/loader/Mod.hpp>
#include <dlfcn.h>
2022-07-30 12:24:03 -04:00
USE_GEODE_NAMESPACE();
2022-10-30 14:56:36 -04:00
template <typename T>
T findSymbolOrMangled(void* dylib, char const* name, char const* mangled) {
auto res = reinterpret_cast<T>(dlsym(dylib, name));
if (!res) {
res = reinterpret_cast<T>(dlsym(dylib, mangled));
}
return res;
2022-07-30 12:24:03 -04:00
}
Result<> Mod::loadPlatformBinary() {
2022-10-30 14:56:36 -04:00
auto dylib =
dlopen((this->m_tempDirName / this->m_info.m_binaryName).string().c_str(), RTLD_LAZY);
if (dylib) {
this->m_implicitLoadFunc =
findSymbolOrMangled<geode_load>(dylib, "geode_implicit_load", "_geode_implicit_load");
2022-07-30 12:24:03 -04:00
if (!this->m_implicitLoadFunc) {
return Err("Unable to find mod entry point");
2022-10-30 14:56:36 -04:00
}
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
if (this->m_platformInfo) {
delete this->m_platformInfo;
}
this->m_platformInfo = new PlatformInfo { dylib };
2022-07-30 12:24:03 -04:00
return Ok();
2022-10-30 14:56:36 -04:00
}
std::string err = (char const*)dlerror();
return Err("Unable to load the DYLIB: dlerror returned (" + err + ")");
2022-07-30 12:24:03 -04:00
}
Result<> Mod::unloadPlatformBinary() {
2022-10-30 14:56:36 -04:00
auto dylib = this->m_platformInfo->m_dylib;
delete this->m_platformInfo;
this->m_platformInfo = nullptr;
if (dlclose(dylib) == 0) {
this->m_implicitLoadFunc = nullptr;
return Ok();
2022-10-30 14:56:36 -04:00
}
else {
return Err("Unable to free library");
2022-10-30 14:56:36 -04:00
}
2022-07-30 12:24:03 -04:00
}
#endif