Plugin manager: simplifies code, fixes a crash on exit

This commit is contained in:
morg
2023-08-12 08:06:09 +02:00
parent 41b7dc3102
commit f8c20128ef
4 changed files with 100 additions and 102 deletions

View File

@@ -200,7 +200,6 @@ void PluginManager::AddPlugin(const filesystem::path& path)
entry.info = info;
entry.plugin = plugin;
entry.loader = loader;
entry.loaded = false;
entry.path = path_string;
entry.enabled = enabled;
entry.widget = nullptr;
@@ -209,7 +208,7 @@ void PluginManager::AddPlugin(const filesystem::path& path)
loader->unload();
PluginManager::ActivePlugins.push_back(entry);
ActivePlugins.push_back(entry);
if(entry.enabled)
{
@@ -236,7 +235,6 @@ void PluginManager::AddPlugin(const filesystem::path& path)
entry.info = info;
entry.plugin = plugin;
entry.loader = loader;
entry.loaded = false;
entry.path = path_string;
entry.enabled = false;
entry.widget = nullptr;
@@ -247,10 +245,24 @@ void PluginManager::AddPlugin(const filesystem::path& path)
PluginManager::ActivePlugins.push_back(entry);
loader->unload();
bool unloaded = loader->unload();
LOG_WARNING("[PluginManager] Plugin %s has an incompatible API version", path.c_str());
if(!unloaded)
{
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", path.c_str());
}
}
}
else
{
LOG_WARNING("[PluginManager] Plugin %s cannot be casted to OpenRGBPluginInterface", path.c_str());
}
}
else
{
LOG_WARNING("[PluginManager] Plugin %s cannot be instantiated.", path.c_str());
}
}
}
@@ -284,7 +296,7 @@ void PluginManager::RemovePlugin(const filesystem::path& path)
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(ActivePlugins[plugin_idx].loaded)
if(ActivePlugins[plugin_idx].loader->isLoaded())
{
LOG_TRACE("[PluginManager] Plugin %s is active, unloading", path.c_str());
UnloadPlugin(path);
@@ -330,10 +342,9 @@ void PluginManager::LoadPlugin(const filesystem::path& path)
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list but not loaded, load it |
\*---------------------------------------------------------------------*/
if(!ActivePlugins[plugin_idx].loaded)
if(!ActivePlugins[plugin_idx].loader->isLoaded())
{
ActivePlugins[plugin_idx].loader->load();
ActivePlugins[plugin_idx].loaded = true;
QObject* instance = ActivePlugins[plugin_idx].loader->instance();
@@ -390,7 +401,7 @@ void PluginManager::UnloadPlugin(const filesystem::path& path)
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(ActivePlugins[plugin_idx].loaded)
if(ActivePlugins[plugin_idx].loader->isLoaded())
{
/*-------------------------------------------------*\
| Call plugin's Unload function before GUI removal |
@@ -405,8 +416,20 @@ void PluginManager::UnloadPlugin(const filesystem::path& path)
RemovePluginCallbackVal(RemovePluginCallbackArg, &ActivePlugins[plugin_idx]);
}
ActivePlugins[plugin_idx].loader->unload();
ActivePlugins[plugin_idx].loaded = false;
bool unloaded = ActivePlugins[plugin_idx].loader->unload();
if(!unloaded)
{
LOG_WARNING("[PluginManager] Plugin %s cannot be unloaded", path.c_str());
}
else
{
LOG_TRACE("[PluginManager] Plugin %s successfully unloaded", path.c_str());
}
}
else
{
LOG_TRACE("[PluginManager] Plugin %s was already unloaded", path.c_str());
}
}
@@ -414,6 +437,9 @@ void PluginManager::UnloadPlugins()
{
for(const OpenRGBPluginEntry& plugin_entry: ActivePlugins)
{
plugin_entry.plugin->Unload();
if(plugin_entry.loader->isLoaded())
{
plugin_entry.plugin->Unload();
}
}
}