Core: Rewrite plugin system

This commit is contained in:
SirLynix 2022-08-18 23:23:00 +02:00 committed by Jérôme Leclercq
parent d7ad5cc846
commit 3a366cc1e4
22 changed files with 596 additions and 434 deletions

View File

@ -12,6 +12,7 @@
#include <Nazara/Renderer.hpp>
#include <Nazara/Utility.hpp>
#include <Nazara/Utility/Components.hpp>
#include <Nazara/Utility/Plugins/AssimpPlugin.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <NZSL/Math/FieldOffsets.hpp>
#include <entt/entt.hpp>
@ -31,12 +32,9 @@ int main()
rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL;
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::PluginManager::Mount(Nz::Plugin::Assimp);
Nz::CallOnExit unmountAssimp([]
{
Nz::PluginManager::Unmount(Nz::Plugin::Assimp);
});
Nz::PluginLoader loader;
Nz::Plugin<Nz::AssimpPlugin> assimp = loader.Load<Nz::AssimpPlugin>();
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();

View File

@ -61,7 +61,9 @@
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/ParameterList.hpp>
#include <Nazara/Core/PluginManager.hpp>
#include <Nazara/Core/Plugin.hpp>
#include <Nazara/Core/PluginInterface.hpp>
#include <Nazara/Core/PluginLoader.hpp>
#include <Nazara/Core/PoolByteStream.hpp>
#include <Nazara/Core/Primitive.hpp>
#include <Nazara/Core/PrimitiveList.hpp>

View File

@ -132,16 +132,6 @@ namespace Nz
Max = Userdata
};
enum class Plugin
{
Assimp,
FFmpeg,
Max = Assimp
};
constexpr std::size_t PluginCount = static_cast<std::size_t>(Plugin::Max) + 1;
enum class PrimitiveType
{
Box,

View File

@ -0,0 +1,55 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_PLUGIN_HPP
#define NAZARA_CORE_PLUGIN_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/PluginInterface.hpp>
#include <memory>
#include <type_traits>
namespace Nz
{
template<typename T>
class Plugin
{
static_assert(std::is_base_of_v<PluginInterface, T>);
public:
Plugin(DynLib dynLib, std::unique_ptr<T> pluginInterface, bool isActive = false);
Plugin(const Plugin&) = delete;
Plugin(Plugin&&) = delete;
~Plugin();
bool Activate();
template<typename U> Plugin<U> Cast() &&;
void Deactivate();
const DynLib& GetDynamicLibrary() const;
T* operator->();
const T* operator->() const;
Plugin& operator=(const Plugin&) = delete;
Plugin& operator=(Plugin&&) = delete;
private:
std::unique_ptr<T> m_interface;
DynLib m_lib;
bool m_isActive;
};
using GenericPlugin = Plugin<PluginInterface>;
}
#include <Nazara/Core/Plugin.inl>
#endif // NAZARA_CORE_PLUGIN_HPP

View File

@ -0,0 +1,82 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Plugin.hpp>
#include <stdexcept>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
template<typename T>
Plugin<T>::Plugin(DynLib dynLib, std::unique_ptr<T> pluginInterface, bool isActive) :
m_interface(std::move(pluginInterface)),
m_lib(std::move(dynLib)),
m_isActive(isActive)
{
}
template<typename T>
Plugin<T>::~Plugin()
{
if (m_interface)
{
Deactivate();
// Destroys interface before freeing the library
m_interface.reset();
}
m_lib.Unload();
}
template<typename T>
bool Plugin<T>::Activate()
{
if (m_isActive)
return true;
if (!m_interface->Activate())
return false;
m_isActive = true;
return true;
}
template<typename T>
template<typename U>
Plugin<U> Plugin<T>::Cast() &&
{
return Plugin<U>(std::move(m_lib), std::unique_ptr<U>(static_cast<U*>(m_interface.release())), m_isActive);
}
template<typename T>
void Plugin<T>::Deactivate()
{
if (m_isActive)
{
m_interface->Deactivate();
m_isActive = false;
}
}
template<typename T>
const DynLib& Plugin<T>::GetDynamicLibrary() const
{
return m_lib;
}
template<typename T>
T* Plugin<T>::operator->()
{
return m_interface;
}
template<typename T>
const T* Plugin<T>::operator->() const
{
return m_interface;
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -0,0 +1,38 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_PLUGININTERFACE_HPP
#define NAZARA_CORE_PLUGININTERFACE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <string>
namespace Nz
{
class NAZARA_CORE_API PluginInterface
{
public:
PluginInterface() = default;
PluginInterface(const PluginInterface&) = delete;
PluginInterface(PluginInterface&&) = delete;
virtual ~PluginInterface();
virtual bool Activate() = 0;
virtual void Deactivate() = 0;
virtual std::string_view GetDescription() const = 0;
virtual std::string_view GetName() const = 0;
virtual UInt32 GetVersion() const = 0;
PluginInterface& operator=(const PluginInterface&) = delete;
PluginInterface& operator=(PluginInterface&&) = delete;
};
}
#include <Nazara/Core/PluginInterface.inl>
#endif // NAZARA_CORE_PLUGININTERFACE_HPP

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PluginInterface.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -0,0 +1,48 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_PLUGINLOADER_HPP
#define NAZARA_CORE_PLUGINLOADER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Enums.hpp>
#include <Nazara/Core/Plugin.hpp>
#include <filesystem>
#include <vector>
namespace Nz
{
class PluginInterface;
class NAZARA_CORE_API PluginLoader
{
public:
PluginLoader();
PluginLoader(const PluginLoader&) = delete;
PluginLoader(PluginLoader&&) = delete;
~PluginLoader() = default;
void AddSearchDirectory(const std::filesystem::path& directoryPath);
template<typename T> Plugin<T> Load(bool activate = true);
GenericPlugin Load(const std::filesystem::path& pluginPath, bool activate = true);
void RemoveSearchDirectory(const std::filesystem::path& directoryPath);
PluginLoader& operator=(const PluginLoader&) = delete;
PluginLoader& operator=(PluginLoader&&) = delete;
private:
using PluginLoadCallback = PluginInterface* (*)();
std::vector<std::filesystem::path> m_directories;
};
}
#include <Nazara/Core/PluginLoader.inl>
#endif // NAZARA_CORE_PLUGINLOADER_HPP

View File

@ -0,0 +1,19 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PluginLoader.hpp>
#include <Nazara/Utils/Algorithm.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
template<typename T>
Plugin<T> PluginLoader::Load(bool activate)
{
GenericPlugin plugin = Load(Utf8Path(T::Filename), activate);
return std::move(plugin).Cast<T>();
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -1,58 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_PLUGINMANAGER_HPP
#define NAZARA_CORE_PLUGINMANAGER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Enums.hpp>
#include <filesystem>
#include <set>
#include <unordered_map>
///TODO: Revision
namespace Nz
{
class DynLib;
class NAZARA_CORE_API PluginManager
{
public:
PluginManager() = delete;
~PluginManager() = delete;
static void AddDirectory(const std::filesystem::path& directoryPath);
static bool Initialize();
static bool Mount(Plugin plugin);
static bool Mount(const std::filesystem::path& pluginPath, bool appendExtension = true);
static void RemoveDirectory(const std::filesystem::path& directoryPath);
static void Unmount(Plugin plugin);
static void Unmount(const std::filesystem::path& pluginPath);
static void Uninitialize();
private:
// https://stackoverflow.com/questions/51065244/is-there-no-standard-hash-for-stdfilesystempath
struct PathHash
{
std::size_t operator()(const std::filesystem::path& p) const
{
return hash_value(p);
}
};
static std::set<std::filesystem::path> s_directories;
static std::unordered_map<std::filesystem::path, std::unique_ptr<DynLib>, PathHash> s_plugins;
static bool s_initialized;
};
}
#endif // NAZARA_CORE_PLUGINMANAGER_HPP

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_UTILITY_PLUGIN_ASSIMPPLUGIN_HPP
#define NAZARA_UTILITY_PLUGIN_ASSIMPPLUGIN_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/PluginInterface.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz
{
class NAZARA_UTILITY_API AssimpPlugin : public PluginInterface
{
public:
#ifdef NAZARA_DEBUG
static constexpr std::string_view Filename = "PluginAssimp-d";
#else
static constexpr std::string_view Filename = "PluginAssimp";
#endif
AssimpPlugin() = default;
AssimpPlugin(const AssimpPlugin&) = delete;
AssimpPlugin(AssimpPlugin&&) = delete;
~AssimpPlugin() = default;
AssimpPlugin& operator=(const AssimpPlugin&) = delete;
AssimpPlugin& operator=(AssimpPlugin&&) = delete;
};
}
#include <Nazara/Utility/Plugins/AssimpPlugin.inl>
#endif // NAZARA_UTILITY_PLUGIN_ASSIMPPLUGIN_HPP

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Plugins/AssimpPlugin.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@ -0,0 +1,39 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_UTILITY_PLUGIN_FFMPEGPLUGIN_HPP
#define NAZARA_UTILITY_PLUGIN_FFMPEGPLUGIN_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/PluginInterface.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz
{
class NAZARA_UTILITY_API FFmpegPlugin : public PluginInterface
{
public:
#ifdef NAZARA_DEBUG
static constexpr std::string_view Filename = "PluginFFmpeg-d";
#else
static constexpr std::string_view Filename = "PluginFFmpeg";
#endif
FFmpegPlugin() = default;
FFmpegPlugin(const FFmpegPlugin&) = delete;
FFmpegPlugin(FFmpegPlugin&&) = delete;
~FFmpegPlugin() = default;
FFmpegPlugin& operator=(const FFmpegPlugin&) = delete;
FFmpegPlugin& operator=(FFmpegPlugin&&) = delete;
private:
};
}
#include <Nazara/Utility/Plugins/FFmpegPlugin.inl>
#endif // NAZARA_UTILITY_PLUGIN_FFMPEGPLUGIN_HPP

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Plugins/FFmpegPlugin.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@ -40,6 +40,7 @@ SOFTWARE.
#include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Utility/VertexMapper.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Utility/Plugins/AssimpPlugin.hpp>
#include <assimp/cfileio.h>
#include <assimp/cimport.h>
#include <assimp/config.h>
@ -888,49 +889,80 @@ std::shared_ptr<Nz::Mesh> LoadMesh(Nz::Stream& stream, const Nz::MeshParams& par
namespace
{
const Nz::AnimationLoader::Entry* animationLoaderEntry = nullptr;
const Nz::MeshLoader::Entry* meshLoaderEntry = nullptr;
class AssimpPluginImpl final : public Nz::AssimpPlugin
{
public:
bool Activate() override
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
m_animationLoaderEntry = animationLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckAnimation,
LoadAnimation
});
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
m_meshLoaderEntry = meshLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckMesh,
LoadMesh
});
return true;
}
void Deactivate() override
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
animationLoader.UnregisterLoader(m_animationLoaderEntry);
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
meshLoader.UnregisterLoader(m_meshLoaderEntry);
}
std::string_view GetDescription() const
{
return "Adds supports to load meshes and animations using Assimp";
}
std::string_view GetName() const override
{
return "Assimp loader";
}
Nz::UInt32 GetVersion() const override
{
return 100;
}
private:
const Nz::AnimationLoader::Entry* m_animationLoaderEntry = nullptr;
const Nz::MeshLoader::Entry* m_meshLoaderEntry = nullptr;
};
}
extern "C"
{
NAZARA_EXPORT int PluginLoad()
NAZARA_EXPORT Nz::PluginInterface* PluginLoad()
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
if (!utility)
{
NazaraError("Utility module must be initialized");
return nullptr;
}
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
animationLoaderEntry = animationLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckAnimation,
LoadAnimation
});
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
meshLoaderEntry = meshLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckMesh,
LoadMesh
});
return 1;
}
NAZARA_EXPORT void PluginUnload()
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
animationLoader.UnregisterLoader(animationLoaderEntry);
animationLoaderEntry = nullptr;
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
meshLoader.UnregisterLoader(meshLoaderEntry);
meshLoaderEntry = nullptr;
std::unique_ptr<AssimpPluginImpl> plugin = std::make_unique<AssimpPluginImpl>();
return plugin.release();
}
}

View File

@ -14,6 +14,7 @@ You should have received a copy of the GNU General Public License along with thi
#include <Nazara/Core/ByteStream.hpp>
#include <Nazara/Utility/ImageStream.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Utility/Plugins/FFmpegPlugin.hpp>
extern "C"
{
@ -28,8 +29,6 @@ extern "C"
namespace
{
const Nz::ImageStreamLoader::Entry* ffmpegLoaderEntry = nullptr;
class FFmpegStream : public Nz::ImageStream
{
public:
@ -448,35 +447,70 @@ namespace
return ffmpegStream;
}
class FFmpegPluginImpl final : public Nz::FFmpegPlugin
{
public:
bool Activate() override
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::ImageStreamLoader::Entry loaderEntry;
loaderEntry.extensionSupport = CheckVideoExtension;
loaderEntry.streamChecker = CheckVideo;
loaderEntry.fileLoader = LoadFile;
loaderEntry.memoryLoader = LoadMemory;
loaderEntry.streamLoader = LoadStream;
Nz::ImageStreamLoader& imageStreamLoader = utility->GetImageStreamLoader();
m_ffmpegLoaderEntry = imageStreamLoader.RegisterLoader(loaderEntry);
return true;
}
void Deactivate() override
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::ImageStreamLoader& imageStreamLoader = utility->GetImageStreamLoader();
imageStreamLoader.UnregisterLoader(m_ffmpegLoaderEntry);
m_ffmpegLoaderEntry = nullptr;
}
std::string_view GetDescription() const
{
return "Adds supports to load and decode videos streams";
}
std::string_view GetName() const override
{
return "FFMpeg loader";
}
Nz::UInt32 GetVersion() const override
{
return 100;
}
private:
const Nz::ImageStreamLoader::Entry* m_ffmpegLoaderEntry = nullptr;
};
}
extern "C"
{
NAZARA_EXPORT int PluginLoad()
NAZARA_EXPORT Nz::PluginInterface* PluginLoad()
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
if (!utility)
{
NazaraError("Utility module must be initialized");
return nullptr;
}
Nz::ImageStreamLoader::Entry loaderEntry;
loaderEntry.extensionSupport = CheckVideoExtension;
loaderEntry.streamChecker = CheckVideo;
loaderEntry.fileLoader = LoadFile;
loaderEntry.memoryLoader = LoadMemory;
loaderEntry.streamLoader = LoadStream;
Nz::ImageStreamLoader& imageStreamLoader = utility->GetImageStreamLoader();
ffmpegLoaderEntry = imageStreamLoader.RegisterLoader(loaderEntry);
return 1;
}
NAZARA_EXPORT void PluginUnload()
{
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::ImageStreamLoader& imageStreamLoader = utility->GetImageStreamLoader();
imageStreamLoader.UnregisterLoader(ffmpegLoaderEntry);
ffmpegLoaderEntry = nullptr;
std::unique_ptr<FFmpegPluginImpl> plugin = std::make_unique<FFmpegPluginImpl>();
return plugin.release();
}
}

View File

@ -7,7 +7,7 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/HardwareInfo.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/PluginManager.hpp>
#include <Nazara/Core/PluginLoader.hpp>
#include <Nazara/Core/TaskScheduler.hpp>
#include <Nazara/Core/Debug.hpp>
@ -30,7 +30,6 @@ namespace Nz
Core::~Core()
{
HardwareInfo::Uninitialize();
PluginManager::Uninitialize();
TaskScheduler::Uninitialize();
LogUninit();
Log::Uninitialize();

View File

@ -0,0 +1,11 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PluginInterface.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
PluginInterface::~PluginInterface() = default;
}

View File

@ -0,0 +1,75 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PluginLoader.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp>
#include <stdexcept>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
PluginLoader::PluginLoader()
{
AddSearchDirectory(".");
AddSearchDirectory("plugins");
}
void PluginLoader::AddSearchDirectory(const std::filesystem::path& directoryPath)
{
m_directories.push_back(std::filesystem::absolute(directoryPath));
}
GenericPlugin PluginLoader::Load(const std::filesystem::path& pluginPath, bool activate)
{
std::filesystem::path path = pluginPath;
if (path.extension() != NAZARA_DYNLIB_EXTENSION)
path += NAZARA_DYNLIB_EXTENSION;
bool exists = false;
if (!path.is_absolute())
{
for (const std::filesystem::path& dir : m_directories)
{
std::filesystem::path testPath = dir / path;
if (std::filesystem::exists(testPath))
{
path = testPath;
exists = true;
break;
}
}
}
else if (std::filesystem::exists(path))
exists = true;
if (!exists)
throw std::runtime_error("failed to find plugin file");
DynLib library;
if (!library.Load(path))
throw std::runtime_error("failed to load plugin");
PluginLoadCallback loadFunc = reinterpret_cast<PluginLoadCallback>(library.GetSymbol("PluginLoad"));
if (!loadFunc)
throw std::runtime_error("failed to load plugin: PluginLoad symbol not found");
std::unique_ptr<PluginInterface> pluginInterface = std::unique_ptr<PluginInterface>(loadFunc());
if (!pluginInterface)
throw std::runtime_error("plugin failed to load");
if (activate && !pluginInterface->Activate())
throw std::runtime_error("failed to activate plugin");
return GenericPlugin(std::move(library), std::move(pluginInterface), activate);
}
void PluginLoader::RemoveSearchDirectory(const std::filesystem::path& directoryPath)
{
auto it = std::find(m_directories.begin(), m_directories.end(), std::filesystem::absolute(directoryPath));
if (it != m_directories.end())
m_directories.erase(it);
}
}

View File

@ -1,295 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PluginManager.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp>
#ifdef NAZARA_PLATFORM_WINDOWS
#define NAZARA_DYNLIB_PREFIX ""
#else
#define NAZARA_DYNLIB_PREFIX "lib"
#endif
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
using PluginLoad = int (*)();
using PluginUnload = void (*)();
const char* s_pluginFiles[] =
{
NAZARA_DYNLIB_PREFIX "PluginAssimp", // Plugin::Assimp
NAZARA_DYNLIB_PREFIX "PluginFFmpeg", // Plugin::FFmpeg
};
}
/*!
* \ingroup core
* \class Nz::PluginManager
* \brief Core class that represents a manager for plugin
*/
/*!
* \brief Adds a directory
*
* \param directoryPath Path to the directory
*
* \remark Produces a NazaraError if not initialized
*/
void PluginManager::AddDirectory(const std::filesystem::path& directoryPath)
{
if (!Initialize())
{
NazaraError("Failed to initialize PluginManager");
return;
}
s_directories.insert(std::filesystem::absolute(directoryPath));
}
/*!
* \brief Initializes the plugin manager
* \return true if everything is ok
*/
bool PluginManager::Initialize()
{
if (s_initialized)
return true;
s_initialized = true;
AddDirectory(".");
AddDirectory("plugins");
return true;
}
/*!
* \brief Mounts the plugin
* \return true if mounting was a success
*
* \remark Produces a NazaraError if not initialized
* \remark Produces a NazaraError if plugin is not found
* \remark Produces a NazaraError if fail to load plugin
* \remark Produces a NazaraError if fail to get symbol PluginLoad
* \remark Produces a NazaraError if fail to initialize the plugin with PluginLoad
*/
bool PluginManager::Mount(Plugin plugin)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::filesystem::path pluginName = s_pluginFiles[UnderlyingCast(plugin)];
#ifdef NAZARA_DEBUG
std::filesystem::path debugPath = pluginName;
debugPath += "-d";
if (Mount(debugPath, true))
return true;
#endif
return Mount(pluginName, true);
}
/*!
* \brief Mounts the plugin with a path
* \return true if mounting was a success
*
* \param pluginPath Path to the plugin
* \param appendExtension Adds the extension to the path or not
*
* \remark Produces a NazaraError if not initialized
* \remark Produces a NazaraError if plugin is not found
* \remark Produces a NazaraError if fail to load plugin
* \remark Produces a NazaraError if fail to get symbol PluginLoad
* \remark Produces a NazaraError if fail to initialize the plugin with PluginLoad
*/
bool PluginManager::Mount(const std::filesystem::path& pluginPath, bool appendExtension)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
if (!Initialize())
{
NazaraError("Failed to initialize PluginManager");
return false;
}
std::filesystem::path path = pluginPath;
if (appendExtension && path.extension() != NAZARA_DYNLIB_EXTENSION)
path += NAZARA_DYNLIB_EXTENSION;
bool exists = false;
if (!path.is_absolute())
{
for (const std::filesystem::path& dir : s_directories)
{
std::filesystem::path testPath = dir / path;
if (std::filesystem::exists(testPath))
{
path = testPath;
exists = true;
break;
}
}
}
else if (std::filesystem::exists(path))
exists = true;
if (!exists)
{
NazaraError("Failed to find plugin file");
return false;
}
std::unique_ptr<DynLib> library = std::make_unique<DynLib>();
if (!library->Load(path))
{
NazaraError("Failed to load plugin");
return false;
}
PluginLoad func = reinterpret_cast<PluginLoad>(library->GetSymbol("PluginLoad"));
if (!func)
{
NazaraError("Failed to get symbol PluginLoad");
return false;
}
if (!func())
{
NazaraError("Plugin failed to load");
return false;
}
std::filesystem::path canonicalPath = std::filesystem::canonical(path);
s_plugins[canonicalPath] = std::move(library);
return true;
}
/*!
* \brief Removes a directory
*
* \param directoryPath Path to the directory
*
* \remark Produces a NazaraError if not initialized
*/
void PluginManager::RemoveDirectory(const std::filesystem::path& directoryPath)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
if (!Initialize())
{
NazaraError("Failed to initialize PluginManager");
return;
}
s_directories.erase(std::filesystem::canonical(directoryPath));
}
/*!
* \brief Unmounts the plugin with a path
*
* \param plugin Path to the plugin
*
* \remark Produces a NazaraError if not initialized
* \remark Produces a NazaraError if plugin is not loaded
*/
void PluginManager::Unmount(Plugin plugin)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::filesystem::path pluginName = s_pluginFiles[UnderlyingCast(plugin)];
#ifdef NAZARA_DEBUG
std::filesystem::path debugPath = pluginName;
debugPath += "-d";
Unmount(debugPath);
#endif
Unmount(pluginName);
}
/*!
* \brief Unmounts the plugin with a path
*
* \param pluginPath Path to the plugin
*
* \remark Produces a NazaraError if not initialized
* \remark Produces a NazaraError if plugin is not loaded
*/
void PluginManager::Unmount(const std::filesystem::path& pluginPath)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
if (!Initialize())
{
NazaraError("Failed to initialize PluginManager");
return;
}
std::filesystem::path path = pluginPath;
if (path.extension() != NAZARA_DYNLIB_EXTENSION)
path += NAZARA_DYNLIB_EXTENSION;
if (!std::filesystem::exists(path))
return;
std::filesystem::path canonicalPath = std::filesystem::canonical(path);
auto it = s_plugins.find(canonicalPath);
if (it == s_plugins.end())
{
NazaraError("Plugin not loaded");
return;
}
PluginUnload func = reinterpret_cast<PluginUnload>(it->second->GetSymbol("PluginUnload"));
if (func)
func();
s_plugins.erase(it);
}
/*!
* \brief Uninitializes the plugin manager
*/
void PluginManager::Uninitialize()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
if (!s_initialized)
return;
s_initialized = false;
s_directories.clear();
for (auto& pair : s_plugins)
{
PluginUnload func = reinterpret_cast<PluginUnload>(pair.second->GetSymbol("PluginUnload"));
if (func)
func();
}
s_plugins.clear();
}
std::set<std::filesystem::path> PluginManager::s_directories;
std::unordered_map<std::filesystem::path, std::unique_ptr<DynLib>, PluginManager::PathHash> PluginManager::s_plugins;
bool PluginManager::s_initialized = false;
}

View File

@ -0,0 +1,10 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Plugins/AssimpPlugin.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
}

View File

@ -0,0 +1,10 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Plugins/FFmpegPlugin.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
}