Skip to content

Commit

Permalink
merge: Merge pull request #26 from G-Epitech/fix-dlloader-cache
Browse files Browse the repository at this point in the history
refactor(dlloader)
  • Loading branch information
Yann-Masson authored Apr 2, 2024
2 parents 8c8b73e + 168b3ba commit 99fb840
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
19 changes: 10 additions & 9 deletions core/src/loader/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> dlLoader) {
shared::types::LibraryTypeGetter getter = nullptr;

getter = dlLoader.loadSymbol<shared::types::LibraryTypeGetter>(SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME));
getter = dlLoader->loadSymbol<shared::types::LibraryTypeGetter>(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> dlLoader) {
shared::types::GameProviderGetter game = nullptr;

game = dlLoader.loadSymbol<shared::types::GameProviderGetter>(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME));
game = dlLoader->loadSymbol<shared::types::GameProviderGetter>(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME));
this->_gamesLibraries.push_back(std::unique_ptr<shared::games::IGameProvider>(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> dlLoader) {
shared::types::GraphicsProviderGetter graphics = nullptr;

graphics = dlLoader.loadSymbol<shared::types::GraphicsProviderGetter>(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME));
graphics = dlLoader->loadSymbol<shared::types::GraphicsProviderGetter>(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME));
this->_graphicsLibraries.push_back(std::unique_ptr<shared::graphics::IGraphicsProvider>(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 = DLLoader::open(filepath);

dlLoader.open();
type = this->_getLibraryGetter(filepath, dlLoader);
if (type == shared::types::LibraryType::GAME)
this->_loadGameLibrary(filepath, dlLoader);
Expand All @@ -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());
}
}
Expand Down
11 changes: 7 additions & 4 deletions core/src/loader/Loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,29 @@ class Loader {
const std::string _path;
GameProviders _gamesLibraries;
GraphicsProviders _graphicsLibraries;
std::vector<std::shared_ptr<DLLoader>> _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<DLLoader> 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<DLLoader> 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<DLLoader> loader);

/**
* @brief Throw an error when loading a library
Expand Down
16 changes: 7 additions & 9 deletions core/src/utils/DLLoader/DLLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ 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() {
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();
std::shared_ptr<DLLoader> DLLoader::open(const std::string &filepath, DLLoader::LoadingMode mode) {
return std::make_shared<DLLoader>(filepath, mode);
}

DLLoader::DLLoaderExeption::DLLoaderExeption(const std::string &message) : _message(message) {}
Expand Down
25 changes: 13 additions & 12 deletions core/src/utils/DLLoader/DLLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@

#pragma once

#include <memory>
#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 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<DLLoader> open(const std::string &filepath, LoadingMode mode = LAZY);

/**
* @brief Get a function from the library
Expand Down Expand Up @@ -69,7 +70,7 @@ class DLLoader {
protected:
private:
void *_handle;
const std::string _filepath;
std::string _filepath;

/**
* @brief Throw an error
Expand Down

0 comments on commit 99fb840

Please sign in to comment.