Skip to content

Commit

Permalink
merge: Merge pull request #18 from G-Epitech/17-close-dynamique-libra…
Browse files Browse the repository at this point in the history
…ries

fix: correctly close directory and dynamic libraries when an error occured
  • Loading branch information
Yann-Masson authored Mar 26, 2024
2 parents 919ae36 + 2c54d50 commit 4ca0022
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 58 deletions.
6 changes: 3 additions & 3 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
add_executable(arcade
add_executable(${PROJECT_NAME}
main.cpp
)

target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src)

add_subdirectory(src)
2 changes: 1 addition & 1 deletion core/src/exception/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target_sources(arcade PRIVATE
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
ArcadeError.cpp
)
2 changes: 1 addition & 1 deletion core/src/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target_sources(arcade PRIVATE
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
DLLoader.cpp
)
45 changes: 28 additions & 17 deletions core/src/loader/DLLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@
*/

#include <dlfcn.h>
#include <dirent.h>
#include <memory.h>
#include <iostream>
#include "DLLoader.hpp"
#include "exception/ArcadeError.hpp"

void DLLoader::_throwLoadError(void *handle) {
std::string error = dlerror();

if (this->_dir)
closedir(this->_dir);
if (handle)
dlclose(handle);
throw ArcadeError(error.length() ? error : "Unknown error while loading library");
}

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);
this->_throwLoadError(handle);
return getter();
}

Expand All @@ -26,7 +35,7 @@ void DLLoader::_loadGameLibrary(const std::string &filepath, void *handle) {

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->_throwLoadError(handle);
this->_gamesLibraries.push_back(game());
}

Expand All @@ -35,42 +44,44 @@ void DLLoader::_loadGraphicsLibrary(const std::string &filepath, void *handle) {

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->_throwLoadError(handle);
this->_graphicsLibraries.push_back(graphics());
}

void DLLoader::registerLibrary(const std::string &filepath) {
void *handle = dlopen(filepath.c_str(), RTLD_LAZY);
void *handle = dlopen(filepath.c_str(), RTLD_LAZY);;
shared::types::LibraryType type;

if (!handle)
throw ArcadeError("Cannot load library: " + filepath);
this->_throwLoadError(handle);
dlerror();
type = this->_getLibraryGetter(filepath, handle);
if (type == shared::types::LibraryType::GAME)
this->_loadGameLibrary(filepath, handle);
return this->_loadGameLibrary(filepath, handle);
else if (type == shared::types::LibraryType::GRAPHIC)
this->_loadGraphicsLibrary(filepath, handle);
else
throw ArcadeError("Unknown library type: " + filepath);
return this->_loadGraphicsLibrary(filepath, handle);
dlclose(handle);
if (this->_dir)
closedir(this->_dir);
throw ArcadeError(filepath + ": Unknown library type!");
}

void DLLoader::loadLibraries(std::string path) {
DIR *dir;
struct dirent *ent;

dir = opendir(path.c_str());
if (!dir)
this->_dir = opendir(path.c_str());
if (!this->_dir)
throw ArcadeError("Cannot open directory: " + path);
ent = readdir(dir);
ent = readdir(this->_dir);
while (ent) {
if (ent->d_name[0] == '.') {
ent = readdir(dir);
ent = readdir(this->_dir);
continue;
}
this->registerLibrary(path + "/" + ent->d_name);
ent = readdir(dir);
ent = readdir(this->_dir);
}
closedir(dir);
closedir(this->_dir);
}

const GameProviders &DLLoader::getGamesLibraries() const {
Expand Down
8 changes: 8 additions & 0 deletions core/src/loader/DLLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <iostream>
#include <dirent.h>
#include "types/Providers.hpp"

class DLLoader {
Expand Down Expand Up @@ -38,6 +39,7 @@ class DLLoader {
const GraphicsProviders &getGraphicsLibraries() const;

private:
DIR *_dir;
const std::string _path;
GameProviders _gamesLibraries;
GraphicsProviders _graphicsLibraries;
Expand All @@ -63,4 +65,10 @@ class DLLoader {
* @param handle handle pointer to the library
*/
void _loadGraphicsLibrary(const std::string &filepath, void *handle);

/**
* @brief Throw an error when loading a library
* @param handle handle pointer to the library
*/
void _throwLoadError(void *handle);
};
6 changes: 0 additions & 6 deletions shared/games/IGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ class shared::games::IGame
*/
virtual const GameManifest &getManifest(void) const noexcept = 0;

/**
* @brief The minimum window size required for the game (pixels)
*
*/
virtual const Vector2u getWindowMinSize(void) const noexcept = 0;

/**
* @brief Number of tiles that represent the game
* Tile size is managed by the renderer
Expand Down
13 changes: 3 additions & 10 deletions shared/graphics/IGraphicsProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,9 @@ class shared::graphics::IGraphicsProvider {
/**
* @brief Create a texture object
*
* @param path Path of the texture file
* @param bin Path of the binary texture file
* @param ascii Path of the ascii texture file
* @return Created texture object
*/
virtual std::shared_ptr<ITexture> createTexture(const std::string &path) = 0;

/**
* @brief Create a window icon object
*
* @param path Path of the window icon file
* @return Created window icon object
*/
virtual std::unique_ptr<IWindowIcon> createWindowIcon(const std::string &path) = 0;
virtual std::shared_ptr<ITexture> createTexture(const std::string &bin, const std::string &ascii) = 0;
};
5 changes: 2 additions & 3 deletions shared/graphics/window/IWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <string>
#include <vector>

#include "IWindowIcon.hpp"
#include "../events/IEvent.hpp"
#include "../../types/types.hpp"
#include "../types/EntityProps.hpp"
Expand Down Expand Up @@ -101,14 +100,14 @@ class shared::graphics::IWindow {
*
* @param icon Icon to use
*/
virtual void setIcon(std::unique_ptr<IWindowIcon> icon) = 0;
virtual void setIcon(const std::string &icon) = 0;

/**
* @brief Get the icon of the window
*
* @return Icon object of the window
*/
virtual const IWindowIcon &getIcon(void) const = 0;
virtual const std::string &getIcon(void) const = 0;

/**
* @brief Render the entity with given properties
Expand Down
17 changes: 0 additions & 17 deletions shared/graphics/window/IWindowIcon.hpp

This file was deleted.

0 comments on commit 4ca0022

Please sign in to comment.