2020-12-22 18:10:01 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
#include <hex.hpp>
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
#include <string>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-06-29 21:34:17 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2021-08-21 00:51:50 +02:00
|
|
|
struct ImGuiContext;
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
class Plugin {
|
|
|
|
public:
|
2022-03-04 11:36:37 +01:00
|
|
|
explicit Plugin(const std::fs::path &path);
|
2022-01-24 20:53:17 +01:00
|
|
|
Plugin(const Plugin &) = delete;
|
2021-02-19 13:22:12 +01:00
|
|
|
Plugin(Plugin &&other) noexcept;
|
2020-12-22 18:10:01 +01:00
|
|
|
~Plugin();
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
[[nodiscard]] bool initializePlugin() const;
|
2021-09-08 15:18:24 +02:00
|
|
|
[[nodiscard]] std::string getPluginName() const;
|
|
|
|
[[nodiscard]] std::string getPluginAuthor() const;
|
|
|
|
[[nodiscard]] std::string getPluginDescription() const;
|
2022-01-23 23:28:56 +01:00
|
|
|
[[nodiscard]] std::string getCompatibleVersion() const;
|
2021-08-21 00:51:50 +02:00
|
|
|
void setImGuiContext(ImGuiContext *ctx) const;
|
2022-02-01 18:09:40 +01:00
|
|
|
[[nodiscard]] bool isBuiltinPlugin() const;
|
2021-02-19 13:22:12 +01:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
[[nodiscard]] const std::fs::path &getPath() const;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-23 23:28:56 +01:00
|
|
|
[[nodiscard]] bool isLoaded() const;
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
private:
|
2022-02-01 22:09:44 +01:00
|
|
|
using InitializePluginFunc = void (*)();
|
|
|
|
using GetPluginNameFunc = const char *(*)();
|
|
|
|
using GetPluginAuthorFunc = const char *(*)();
|
2022-01-24 20:53:17 +01:00
|
|
|
using GetPluginDescriptionFunc = const char *(*)();
|
|
|
|
using GetCompatibleVersionFunc = const char *(*)();
|
2022-02-01 22:09:44 +01:00
|
|
|
using SetImGuiContextFunc = void (*)(ImGuiContext *);
|
|
|
|
using IsBuiltinPluginFunc = bool (*)();
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-06-29 21:34:17 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
HMODULE m_handle = nullptr;
|
|
|
|
#else
|
|
|
|
void *m_handle = nullptr;
|
|
|
|
#endif
|
2022-03-04 11:36:37 +01:00
|
|
|
std::fs::path m_path;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-23 23:28:56 +01:00
|
|
|
mutable bool m_initialized = false;
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
InitializePluginFunc m_initializePluginFunction = nullptr;
|
|
|
|
GetPluginNameFunc m_getPluginNameFunction = nullptr;
|
|
|
|
GetPluginAuthorFunc m_getPluginAuthorFunction = nullptr;
|
2022-01-24 20:53:17 +01:00
|
|
|
GetPluginDescriptionFunc m_getPluginDescriptionFunction = nullptr;
|
|
|
|
GetCompatibleVersionFunc m_getCompatibleVersionFunction = nullptr;
|
2022-02-01 22:09:44 +01:00
|
|
|
SetImGuiContextFunc m_setImGuiContextFunction = nullptr;
|
|
|
|
IsBuiltinPluginFunc m_isBuiltinPluginFunction = nullptr;
|
2021-02-19 13:22:12 +01:00
|
|
|
|
|
|
|
template<typename T>
|
2022-01-23 23:28:56 +01:00
|
|
|
[[nodiscard]] auto getPluginFunction(const std::string &symbol) {
|
|
|
|
return reinterpret_cast<T>(this->getPluginFunction(symbol));
|
2022-01-18 00:10:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] void *getPluginFunction(const std::string &symbol);
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
class PluginManager {
|
2020-12-22 18:10:01 +01:00
|
|
|
public:
|
2021-04-20 21:46:48 +02:00
|
|
|
PluginManager() = delete;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
static bool load(const std::fs::path &pluginFolder);
|
2020-12-22 18:10:01 +01:00
|
|
|
static void unload();
|
|
|
|
static void reload();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
static const auto &getPlugins() {
|
2021-04-20 21:46:48 +02:00
|
|
|
return PluginManager::s_plugins;
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-03-04 11:36:37 +01:00
|
|
|
static std::fs::path s_pluginFolder;
|
2022-02-01 18:09:40 +01:00
|
|
|
static std::vector<Plugin> s_plugins;
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|