1
0
mirror of synced 2024-11-24 15:50:16 +01:00

I wish I was good at C++ (Properly fix plugin system)

This commit is contained in:
WerWolv 2021-02-07 14:57:13 +01:00
parent 8e7bfb7f1a
commit cfb4b5bd51
3 changed files with 14 additions and 7 deletions

View File

@ -12,6 +12,8 @@ namespace hex {
class Plugin {
public:
Plugin(std::string_view path);
Plugin(const Plugin&) = delete;
Plugin(Plugin &&other);
~Plugin();
void initializePlugin() const;
@ -38,7 +40,7 @@ namespace hex {
private:
static inline std::string s_pluginFolder;
static inline std::vector<Plugin*> s_plugins;
static inline std::vector<Plugin> s_plugins;
};
}

View File

@ -8,7 +8,7 @@ namespace hex {
namespace fs = std::filesystem;
// hex::plugin::internal::initializePlugin()
// hex::plugin::<pluginName>::internal::initializePlugin()
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
Plugin::Plugin(std::string_view path) {
@ -21,6 +21,14 @@ namespace hex {
this->m_initializePluginFunction = reinterpret_cast<InitializePluginFunc>(dlsym(this->m_handle, symbolName.c_str()));
}
Plugin::Plugin(Plugin &&other) {
this->m_handle = other.m_handle;
this->m_initializePluginFunction = other.m_initializePluginFunction;
other.m_handle = nullptr;
other.m_initializePluginFunction = nullptr;
}
Plugin::~Plugin() {
dlclose(this->m_handle);
}
@ -39,14 +47,11 @@ namespace hex {
for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
PluginHandler::s_plugins.push_back(new Plugin(pluginPath.path().string()));
PluginHandler::s_plugins.emplace_back(pluginPath.path().string());
}
}
void PluginHandler::unload() {
for (auto &plugin : PluginHandler::s_plugins)
delete plugin;
PluginHandler::s_plugins.clear();
PluginHandler::s_pluginFolder.clear();
}

View File

@ -554,7 +554,7 @@ namespace hex {
} catch (std::runtime_error &e) { return; }
for (const auto &plugin : PluginHandler::getPlugins()) {
plugin->initializePlugin();
plugin.initializePlugin();
}
}