Core/DynLib: Fix extension handling

This commit is contained in:
Jérôme Leclercq 2022-03-20 13:42:41 +01:00
parent 2b53ca6101
commit 48b87cc99b
4 changed files with 8 additions and 13 deletions

View File

@ -16,7 +16,7 @@
#elif defined(NAZARA_PLATFORM_LINUX) #elif defined(NAZARA_PLATFORM_LINUX)
#define NAZARA_DYNLIB_EXTENSION ".so" #define NAZARA_DYNLIB_EXTENSION ".so"
#elif defined(NAZARA_PLATFORM_MACOS) #elif defined(NAZARA_PLATFORM_MACOS)
#define NAZARA_DYNLIB_EXTENSION ".dynlib" #define NAZARA_DYNLIB_EXTENSION ".dylib"
#else #else
#error OS not handled #error OS not handled
#endif #endif
@ -40,7 +40,7 @@ namespace Nz
bool IsLoaded() const; bool IsLoaded() const;
bool Load(const std::filesystem::path& libraryPath); bool Load(std::filesystem::path libraryPath);
void Unload(); void Unload();
DynLib& operator=(const DynLib&) = delete; DynLib& operator=(const DynLib&) = delete;

View File

@ -74,10 +74,13 @@ namespace Nz
* *
* \remark Produces a NazaraError if library is could not be loaded * \remark Produces a NazaraError if library is could not be loaded
*/ */
bool DynLib::Load(const std::filesystem::path& libraryPath) bool DynLib::Load(std::filesystem::path libraryPath)
{ {
Unload(); Unload();
if (libraryPath.extension() != NAZARA_DYNLIB_EXTENSION)
libraryPath += NAZARA_DYNLIB_EXTENSION;
auto impl = std::make_unique<DynLibImpl>(this); auto impl = std::make_unique<DynLibImpl>(this);
if (!impl->Load(libraryPath, &m_lastError)) if (!impl->Load(libraryPath, &m_lastError))
{ {

View File

@ -45,12 +45,8 @@ namespace Nz
bool DynLibImpl::Load(const std::filesystem::path& libraryPath, std::string* errorMessage) bool DynLibImpl::Load(const std::filesystem::path& libraryPath, std::string* errorMessage)
{ {
std::filesystem::path path = libraryPath;
if (path.extension() != ".so")
path += ".so";
dlerror(); // Clear error flag dlerror(); // Clear error flag
m_handle = dlopen(path.generic_u8string().data(), RTLD_LAZY | RTLD_GLOBAL); m_handle = dlopen(libraryPath.generic_u8string().data(), RTLD_LAZY | RTLD_GLOBAL);
if (m_handle) if (m_handle)
return true; return true;

View File

@ -33,11 +33,7 @@ namespace Nz
bool DynLibImpl::Load(const std::filesystem::path& libraryPath, std::string* errorMessage) bool DynLibImpl::Load(const std::filesystem::path& libraryPath, std::string* errorMessage)
{ {
std::filesystem::path path = libraryPath; m_handle = LoadLibraryExW(ToWideString(libraryPath.generic_u8string()).data(), nullptr, (libraryPath.is_absolute()) ? LOAD_WITH_ALTERED_SEARCH_PATH : 0);
if (path.extension() != ".dll")
path += ".dll";
m_handle = LoadLibraryExW(ToWideString(path.generic_u8string()).data(), nullptr, (path.is_absolute()) ? LOAD_WITH_ALTERED_SEARCH_PATH : 0);
if (m_handle) if (m_handle)
return true; return true;
else else