2023-07-18 05:48:59 -04:00
|
|
|
#include <format>
|
|
|
|
#include <lucore/Library.hpp>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "OsLibrary.hpp"
|
|
|
|
|
|
|
|
namespace lucore {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
std::string FormatLibraryName(std::string_view dllName) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return std::format("{}.dll", dllName);
|
|
|
|
#else
|
2023-07-18 06:01:32 -04:00
|
|
|
return std::format("lib{}.so", dllName);
|
2023-07-18 05:48:59 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2023-07-19 03:18:34 -04:00
|
|
|
Library* Library::OpenExisting(std::string_view dllname) {
|
|
|
|
auto name = FormatLibraryName(dllname);
|
2023-07-26 03:08:44 -04:00
|
|
|
if(!detail::OsLibraryLoaded(name.c_str())) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
// Try without a prefix; some libraries need this.
|
|
|
|
name = std::format("{}.so", dllname);
|
|
|
|
if(!detail::OsLibraryLoaded(name.c_str()))
|
|
|
|
return nullptr;
|
|
|
|
#else
|
2023-07-19 03:18:34 -04:00
|
|
|
return nullptr;
|
2023-07-26 03:08:44 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-19 03:18:34 -04:00
|
|
|
return new Library(detail::OsOpenLibrary(name.c_str()));
|
2023-07-18 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Library::Loaded(std::string_view dllname) {
|
|
|
|
return detail::OsLibraryLoaded(FormatLibraryName(dllname).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
Library::~Library() {
|
|
|
|
if(handle) {
|
2023-07-18 06:01:32 -04:00
|
|
|
detail::OsFreeLibrary(handle);
|
2023-07-18 05:48:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Library::SymbolImpl(const char* symbolName) {
|
2023-07-18 06:01:32 -04:00
|
|
|
return detail::OsLibrarySymbol(handle, symbolName);
|
2023-07-18 05:48:59 -04:00
|
|
|
}
|
|
|
|
} // namespace lucore
|