diff --git a/core/src/loader/Loader.cpp b/core/src/loader/Loader.cpp index 997b504..04c7e33 100644 --- a/core/src/loader/Loader.cpp +++ b/core/src/loader/Loader.cpp @@ -13,32 +13,33 @@ Loader::Loader() {} Loader::~Loader() {} -shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, DLLoader &dlLoader) { +shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::LibraryTypeGetter getter = nullptr; - getter = dlLoader.loadSymbol(SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME)); + getter = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME)); return getter(); } -void Loader::_loadGameLibrary(const std::string &filepath, DLLoader &dlLoader) { +void Loader::_loadGameLibrary(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::GameProviderGetter game = nullptr; - game = dlLoader.loadSymbol(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME)); + game = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME)); this->_gamesLibraries.push_back(std::unique_ptr(game())); + this->_libraries.push_back(dlLoader); } -void Loader::_loadGraphicsLibrary(const std::string &filepath, DLLoader &dlLoader) { +void Loader::_loadGraphicsLibrary(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::GraphicsProviderGetter graphics = nullptr; - graphics = dlLoader.loadSymbol(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME)); + graphics = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME)); this->_graphicsLibraries.push_back(std::unique_ptr(graphics())); + this->_libraries.push_back(dlLoader); } void Loader::registerLibrary(const std::string &filepath) { shared::types::LibraryType type; - DLLoader dlLoader(filepath); + std::shared_ptr dlLoader = DLLoader::open(filepath); - dlLoader.open(); type = this->_getLibraryGetter(filepath, dlLoader); if (type == shared::types::LibraryType::GAME) this->_loadGameLibrary(filepath, dlLoader); @@ -50,7 +51,7 @@ void Loader::registerLibrary(const std::string &filepath) { void Loader::loadLibraries(std::string path) { for (const auto &entry : std::filesystem::directory_iterator(path)) { - if (entry.is_regular_file() && entry.path().extension() == ".so") + if (entry.is_regular_file()) this->registerLibrary(entry.path()); } } diff --git a/core/src/loader/Loader.hpp b/core/src/loader/Loader.hpp index 809c326..34a4459 100644 --- a/core/src/loader/Loader.hpp +++ b/core/src/loader/Loader.hpp @@ -52,26 +52,29 @@ class Loader { const std::string _path; GameProviders _gamesLibraries; GraphicsProviders _graphicsLibraries; + std::vector> _libraries; /** * @brief Get the Library Getter object * @param filepath file path of the library + * @param loader DLLoader * @return getter function */ - shared::types::LibraryType _getLibraryGetter(const std::string &filepath, DLLoader &dlLoader); + shared::types::LibraryType _getLibraryGetter(const std::string &filepath, std::shared_ptr loader); /** * @brief Load a game library * @param filepath file path of the library - * @param handle handle pointer to the library + * @param loader DLLoader */ - void _loadGameLibrary(const std::string &filepath, DLLoader &dlLoader); + void _loadGameLibrary(const std::string &filepath, std::shared_ptr loader); /** * @brief Load a graphics library * @param filepath file path of the library + * @param loader DLLoader */ - void _loadGraphicsLibrary(const std::string &filepath, DLLoader &dlLoader); + void _loadGraphicsLibrary(const std::string &filepath, std::shared_ptr loader); /** * @brief Throw an error when loading a library diff --git a/core/src/utils/DLLoader/DLLoader.cpp b/core/src/utils/DLLoader/DLLoader.cpp index d7f7221..b39fab2 100644 --- a/core/src/utils/DLLoader/DLLoader.cpp +++ b/core/src/utils/DLLoader/DLLoader.cpp @@ -13,8 +13,11 @@ void DLLoader::_throwError() { throw DLLoaderExeption(error.empty() ? "Unknown error while loading library" : error); } -DLLoader::DLLoader(const std::string &filepath) : _filepath(filepath) { - this->_handle = nullptr; +DLLoader::DLLoader(const std::string &filepath, LoadingMode mode) { + this->_handle = dlopen(filepath.c_str(), mode); + if (!this->_handle) + this->_throwError(); + dlerror(); } DLLoader::~DLLoader() { @@ -22,13 +25,8 @@ DLLoader::~DLLoader() { dlclose(this->_handle); } -void DLLoader::open(DLLoader::LoadingMode mode) { - if (this->_handle) - dlclose(this->_handle); - this->_handle = dlopen(this->_filepath.c_str(), mode); - if (!this->_handle) - this->_throwError(); - dlerror(); +std::shared_ptr DLLoader::open(const std::string &filepath, DLLoader::LoadingMode mode) { + return std::make_shared(filepath, mode); } DLLoader::DLLoaderExeption::DLLoaderExeption(const std::string &message) : _message(message) {} diff --git a/core/src/utils/DLLoader/DLLoader.hpp b/core/src/utils/DLLoader/DLLoader.hpp index 13303a7..3adcd7d 100644 --- a/core/src/utils/DLLoader/DLLoader.hpp +++ b/core/src/utils/DLLoader/DLLoader.hpp @@ -7,37 +7,38 @@ #pragma once +#include #include #include class DLLoader { public: - /** - * @brief Construct a new DLLoader object - * - * @param filepath File path of the library - */ - DLLoader(const std::string &filepath); - - ~DLLoader(); - typedef enum { LAZY = RTLD_LAZY, NOW = RTLD_NOW, LOCAL = RTLD_LOCAL, GLOBAL = RTLD_GLOBAL, NODELETE = RTLD_NODELETE, - DEEPBIND = RTLD_DEEPBIND, NOLOAD = RTLD_NOLOAD } LoadingMode; + /** + * @brief Construct a new DLLoader object + * + * @param filepath Path to the library + */ + DLLoader(const std::string &filepath, LoadingMode mode = LAZY); + + ~DLLoader(); + /** * @brief Open the library * + * @param filepath Path to the library * @param mode Loading mode */ - void open(LoadingMode mode = LAZY); + static std::shared_ptr open(const std::string &filepath, LoadingMode mode = LAZY); /** * @brief Get a function from the library @@ -69,7 +70,7 @@ class DLLoader { protected: private: void *_handle; - const std::string _filepath; + std::string _filepath; /** * @brief Throw an error