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

@@ -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>