-
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 #11 from G-Epitech/8-library-loader
Library loader
- Loading branch information
Showing
15 changed files
with
225 additions
and
6 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
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
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,8 @@ | ||
add_executable(arcade | ||
main.cpp | ||
) | ||
|
||
target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) | ||
target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) | ||
|
||
add_subdirectory(src) |
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,2 @@ | ||
add_subdirectory(exception) | ||
add_subdirectory(loader) |
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,14 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** ArcadeError | ||
*/ | ||
|
||
#include "ArcadeError.hpp" | ||
|
||
ArcadeError::ArcadeError(std::string const &message) : _message(message) {} | ||
|
||
const char *ArcadeError::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,19 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** ArcadeError | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
class ArcadeError : public std::exception { | ||
public: | ||
explicit ArcadeError(std::string const &message); | ||
|
||
const char *what() const noexcept; | ||
private: | ||
const std::string _message; | ||
}; |
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(arcade PRIVATE | ||
ArcadeError.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,3 @@ | ||
target_sources(arcade 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,82 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** DLLoader | ||
*/ | ||
|
||
#include <dlfcn.h> | ||
#include <dirent.h> | ||
#include <memory.h> | ||
#include <iostream> | ||
#include "DLLoader.hpp" | ||
#include "exception/ArcadeError.hpp" | ||
|
||
shared::types::LibraryType DLLoader::_getLibraryGetter(const std::string &filepath, void *handle) { | ||
shared::types::LibraryTypeGetter getter = nullptr; | ||
|
||
getter = reinterpret_cast<shared::types::LibraryTypeGetter>(dlsym(handle, SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME))); | ||
if (!getter) | ||
throw ArcadeError("Cannot find library type getter in library: " + filepath); | ||
return getter(); | ||
} | ||
|
||
void DLLoader::_loadGameLibrary(const std::string &filepath, void *handle) { | ||
shared::types::GameProvider game = nullptr; | ||
|
||
game = reinterpret_cast<shared::types::GameProvider>(dlsym(handle, SHARED_STRINGIFY(SHARED_GAME_PROVIDER_LOADER_NAME))); | ||
if (!game) | ||
throw ArcadeError("Cannot find game provider in library: " + filepath); | ||
this->_gamesLibraries.push_back(game()); | ||
} | ||
|
||
void DLLoader::_loadGraphicsLibrary(const std::string &filepath, void *handle) { | ||
shared::types::GraphicsProvider graphics = nullptr; | ||
|
||
graphics = reinterpret_cast<shared::types::GraphicsProvider>(dlsym(handle, SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_LOADER_NAME))); | ||
if (!graphics) | ||
throw ArcadeError("Cannot find graphics provider in library: " + filepath); | ||
this->_graphicsLibraries.push_back(graphics()); | ||
} | ||
|
||
void DLLoader::registerLibrary(const std::string &filepath) { | ||
void *handle = dlopen(filepath.c_str(), RTLD_LAZY); | ||
shared::types::LibraryType type; | ||
|
||
if (!handle) | ||
throw ArcadeError("Cannot load library: " + filepath); | ||
type = this->_getLibraryGetter(filepath, handle); | ||
if (type == shared::types::LibraryType::GAME) | ||
this->_loadGameLibrary(filepath, handle); | ||
else if (type == shared::types::LibraryType::GRAPHIC) | ||
this->_loadGraphicsLibrary(filepath, handle); | ||
else | ||
throw ArcadeError("Unknown library type: " + filepath); | ||
} | ||
|
||
void DLLoader::loadLibraries(std::string path) { | ||
DIR *dir; | ||
struct dirent *ent; | ||
|
||
dir = opendir(path.c_str()); | ||
if (!dir) | ||
throw ArcadeError("Cannot open directory: " + path); | ||
ent = readdir(dir); | ||
while (ent) { | ||
if (ent->d_name[0] == '.') { | ||
ent = readdir(dir); | ||
continue; | ||
} | ||
this->registerLibrary(path + "/" + ent->d_name); | ||
ent = readdir(dir); | ||
} | ||
closedir(dir); | ||
} | ||
|
||
const GameProviders &DLLoader::getGamesLibraries() const { | ||
return this->_gamesLibraries; | ||
} | ||
|
||
const GraphicsProviders &DLLoader::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** DLLoader | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <iostream> | ||
#include "types/Providers.hpp" | ||
|
||
class DLLoader { | ||
public: | ||
|
||
/** | ||
* @brief Register a library | ||
* @param path std::string | ||
*/ | ||
void registerLibrary(const std::string &filepath); | ||
|
||
/** | ||
* @brief Load all libraries in the given folder path | ||
* @param path std::string | ||
*/ | ||
void loadLibraries(std::string path); | ||
|
||
/** | ||
* @brief Get all games libraries | ||
* @return Loaded games libraries | ||
*/ | ||
const GameProviders &getGamesLibraries() const; | ||
|
||
/** | ||
* @brief Get all graphics libraries | ||
* @return Loaded graphics libraries | ||
*/ | ||
const GraphicsProviders &getGraphicsLibraries() const; | ||
|
||
private: | ||
const std::string _path; | ||
GameProviders _gamesLibraries; | ||
GraphicsProviders _graphicsLibraries; | ||
|
||
/** | ||
* @brief Get the Library Getter object | ||
* @param filepath file path of the library | ||
* @param handle handle pointer to the library | ||
* @return getter function | ||
*/ | ||
shared::types::LibraryType _getLibraryGetter(const std::string &filepath, void *handle); | ||
|
||
/** | ||
* @brief Load a game library | ||
* @param filepath file path of the library | ||
* @param handle handle pointer to the library | ||
*/ | ||
void _loadGameLibrary(const std::string &filepath, void *handle); | ||
|
||
/** | ||
* @brief Load a graphics library | ||
* @param filepath file path of the library | ||
* @param handle handle pointer to the library | ||
*/ | ||
void _loadGraphicsLibrary(const std::string &filepath, void *handle); | ||
}; |
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,13 @@ | ||
/* | ||
** EPITECH PROJECT, 2024 | ||
** arcade | ||
** File description: | ||
** Providers | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common/types/Libraries.hpp" | ||
|
||
typedef std::vector<std::shared_ptr<shared::games::IGameProvider>> GameProviders; | ||
typedef std::vector<std::shared_ptr<shared::graphics::IGraphicsProvider>> GraphicsProviders; |