Skip to content

Commit

Permalink
refactor(DLLoader): division of previous code into several methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann-Masson committed Mar 22, 2024
1 parent 3582ba2 commit a280446
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
71 changes: 39 additions & 32 deletions core/src/loader/DLLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,52 @@
#include "DLLoader.hpp"
#include "exception/ArcadeError.hpp"

void DLLoader::registerLibrary(const std::string &path) {
void *handle = dlopen(path.c_str(), RTLD_LAZY);
shared::types::LibraryTypeGetter getter;
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;
shared::types::GameProvider game;
shared::types::GraphicsProvider graphics;

std::cout << path << std::endl;
if (!handle)
throw ArcadeError("Cannot load library: " + path);
getter = reinterpret_cast<shared::types::LibraryTypeGetter>(dlsym(handle, SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME)));
if (!getter) {
dlclose(handle);
throw ArcadeError("Cannot find getter in library: " + path);
}
type = getter();
if (type == shared::types::LibraryType::GAME) {
game = reinterpret_cast<shared::types::GameProvider>(dlsym(handle, SHARED_STRINGIFY(SHARED_GAME_PROVIDER_LOADER_NAME)));
if (!game) {
dlclose(handle);
throw ArcadeError("Cannot find game provider in library: " + path);
}
this->_gamesLibraries.push_back(game());
}
if (type == shared::types::LibraryType::GRAPHIC) {
graphics = reinterpret_cast<shared::types::GraphicsProvider>(dlsym(handle, SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_LOADER_NAME)));
if (!graphics) {
dlclose(handle);
throw ArcadeError("Cannot find graphics provider in library: " + path);
}
this->_graphicsLibraries.push_back(graphics());
}
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;

std::cout << path << std::endl;
dir = opendir(path.c_str());
if (!dir)
throw ArcadeError("Cannot open directory: " + path);
Expand All @@ -66,10 +73,10 @@ void DLLoader::loadLibraries(std::string path) {
closedir(dir);
}

LoaderGamesProvider DLLoader::getGamesLibraries() const {
const GameProviders &DLLoader::getGamesLibraries() const {
return this->_gamesLibraries;
}

LoaderGraphicsProvider DLLoader::getGraphicsLibraries() const {
const GraphicsProviders &DLLoader::getGraphicsLibraries() const {
return this->_graphicsLibraries;
}
32 changes: 27 additions & 5 deletions core/src/loader/DLLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DLLoader {
* @brief Register a library
* @param path std::string
*/
void registerLibrary(const std::string &path);
void registerLibrary(const std::string &filepath);

/**
* @brief Load all libraries in the given folder path
Expand All @@ -29,16 +29,38 @@ class DLLoader {
* @brief Get all games libraries
* @return Loaded games libraries
*/
LoaderGamesProvider getGamesLibraries() const;
const GameProviders &getGamesLibraries() const;

/**
* @brief Get all graphics libraries
* @return Loaded graphics libraries
*/
LoaderGraphicsProvider getGraphicsLibraries() const;
const GraphicsProviders &getGraphicsLibraries() const;

private:
const std::string _path;
LoaderGamesProvider _gamesLibraries;
LoaderGraphicsProvider _graphicsLibraries;
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);
};
4 changes: 2 additions & 2 deletions core/src/types/Providers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

#include "common/types/Libraries.hpp"

typedef std::vector<std::shared_ptr<shared::games::IGameProvider>> LoaderGamesProvider;
typedef std::vector<std::shared_ptr<shared::graphics::IGraphicsProvider>> LoaderGraphicsProvider;
typedef std::vector<std::shared_ptr<shared::games::IGameProvider>> GameProviders;
typedef std::vector<std::shared_ptr<shared::graphics::IGraphicsProvider>> GraphicsProviders;

0 comments on commit a280446

Please sign in to comment.