-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: Merge pull request #22 from G-Epitech/20-refactor-dlloader
refactor: DLLoader
- Loading branch information
Showing
47 changed files
with
683 additions
and
837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(exception) | ||
add_subdirectory(loader) | ||
add_subdirectory(utils) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
target_sources(${CMAKE_PROJECT_NAME} PRIVATE | ||
DLLoader.cpp | ||
Loader.cpp | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** Loader | ||
*/ | ||
|
||
#include <filesystem> | ||
#include "Loader.hpp" | ||
#include "exception/ArcadeError.hpp" | ||
|
||
Loader::Loader() {} | ||
|
||
Loader::~Loader() {} | ||
|
||
shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, DLLoader &dlLoader) { | ||
shared::types::LibraryTypeGetter getter = nullptr; | ||
|
||
getter = dlLoader.loadSymbol<shared::types::LibraryTypeGetter>(SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME)); | ||
return getter(); | ||
} | ||
|
||
void Loader::_loadGameLibrary(const std::string &filepath, DLLoader &dlLoader) { | ||
shared::types::GameProviderGetter game = nullptr; | ||
|
||
game = dlLoader.loadSymbol<shared::types::GameProviderGetter>(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME)); | ||
this->_gamesLibraries.push_back(std::unique_ptr<shared::games::IGameProvider>(game())); | ||
} | ||
|
||
void Loader::_loadGraphicsLibrary(const std::string &filepath, DLLoader &dlLoader) { | ||
shared::types::GraphicsProviderGetter graphics = nullptr; | ||
|
||
graphics = dlLoader.loadSymbol<shared::types::GraphicsProviderGetter>(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME)); | ||
this->_graphicsLibraries.push_back(std::unique_ptr<shared::graphics::IGraphicsProvider>(graphics())); | ||
} | ||
|
||
void Loader::registerLibrary(const std::string &filepath) { | ||
shared::types::LibraryType type; | ||
DLLoader dlLoader(filepath); | ||
|
||
dlLoader.open(); | ||
type = this->_getLibraryGetter(filepath, dlLoader); | ||
if (type == shared::types::LibraryType::GAME) | ||
this->_loadGameLibrary(filepath, dlLoader); | ||
else if (type == shared::types::LibraryType::GRAPHIC) | ||
this->_loadGraphicsLibrary(filepath, dlLoader); | ||
else | ||
throw ArcadeError(filepath + ": Unknown library type!"); | ||
} | ||
|
||
void Loader::loadLibraries(std::string path) { | ||
for (const auto &entry : std::filesystem::directory_iterator(path)) { | ||
if (entry.is_regular_file() && entry.path().extension() == ".so") | ||
this->registerLibrary(entry.path()); | ||
} | ||
} | ||
|
||
const GameProviders &Loader::getGamesLibraries() const { | ||
return this->_gamesLibraries; | ||
} | ||
|
||
const GraphicsProviders &Loader::getGraphicsLibraries() const { | ||
return this->_graphicsLibraries; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(DLLoader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target_sources(${CMAKE_PROJECT_NAME} PRIVATE | ||
DLLoader.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** DLLoader | ||
*/ | ||
|
||
#include "DLLoader.hpp" | ||
|
||
void DLLoader::_throwError() { | ||
std::string error = dlerror(); | ||
|
||
throw DLLoaderExeption(error.empty() ? "Unknown error while loading library" : error); | ||
} | ||
|
||
DLLoader::DLLoader(const std::string &filepath) : _filepath(filepath) { | ||
this->_handle = nullptr; | ||
} | ||
|
||
DLLoader::~DLLoader() { | ||
if (this->_handle) | ||
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(); | ||
} | ||
|
||
DLLoader::DLLoaderExeption::DLLoaderExeption(const std::string &message) : _message(message) {} | ||
|
||
const char *DLLoader::DLLoaderExeption::what() const noexcept { | ||
return this->_message.c_str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** DLLoader | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <dlfcn.h> | ||
#include <iostream> | ||
|
||
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 Open the library | ||
* | ||
* @param mode Loading mode | ||
*/ | ||
void open(LoadingMode mode = LAZY); | ||
|
||
/** | ||
* @brief Get a function from the library | ||
* | ||
* @tparam T Function prototype | ||
* @param name Symbol name | ||
* @return T Function founded | ||
*/ | ||
template <typename T> | ||
T loadSymbol(std::string name) { | ||
if (!this->_handle) | ||
throw DLLoaderExeption("Library not loaded"); | ||
T symbol = reinterpret_cast<T>(dlsym(this->_handle, name.c_str())); | ||
if (!symbol) | ||
this->_throwError(); | ||
return symbol; | ||
} | ||
|
||
class DLLoaderExeption : public std::exception { | ||
public: | ||
DLLoaderExeption(const std::string &message); | ||
|
||
const char *what() const noexcept override; | ||
|
||
private: | ||
const std::string _message; | ||
}; | ||
|
||
protected: | ||
private: | ||
void *_handle; | ||
const std::string _filepath; | ||
|
||
/** | ||
* @brief Throw an error | ||
* | ||
*/ | ||
void _throwError(); | ||
}; |
Oops, something went wrong.