mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2025-12-23 23:37:48 -05:00
First draft of plugin API changes
This commit is contained in:
@@ -16,18 +16,27 @@ void PluginManager::ScanAndLoadPlugins()
|
||||
{
|
||||
LOG_INFO("Loading plugins");
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Get the plugins directory |
|
||||
| |
|
||||
| The plugins directory is a directory named "plugins" in |
|
||||
| the configuration directory |
|
||||
\*---------------------------------------------------------*/
|
||||
const QDir plugins_dir = QString().fromStdString(ResourceManager::get()->GetConfigurationDirectory()) + "plugins/";
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Get a list of all files in the plugins directory |
|
||||
\*---------------------------------------------------------*/
|
||||
std::vector<std::string> FileList;
|
||||
|
||||
for(int i = 0; i < QDir(plugins_dir).entryList(QDir::Files).size(); i++)
|
||||
{
|
||||
/*--------------------------------------*\
|
||||
| Add all of the Plugin Files to a list |
|
||||
\*--------------------------------------*/
|
||||
FileList.push_back(QDir(plugins_dir).entryList(QDir::Files)[i].toStdString());
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Attempt to load each file in the plugins directory |
|
||||
\*---------------------------------------------------------*/
|
||||
for(const std::string &plugin_name : FileList)
|
||||
{
|
||||
const std::string plugin_path = plugins_dir.absoluteFilePath(QString().fromStdString(plugin_name)).toStdString();
|
||||
@@ -38,92 +47,127 @@ void PluginManager::ScanAndLoadPlugins()
|
||||
|
||||
void PluginManager::LoadPlugin(std::string path)
|
||||
{
|
||||
OpenRGBPluginInterface *OpenRGBPlugin = nullptr;
|
||||
OpenRGBPluginInterface* plugin = nullptr;
|
||||
|
||||
LOG_VERBOSE("Attempting to load: %s", path.c_str());
|
||||
|
||||
QPluginLoader loader(QString().fromStdString(path));
|
||||
/*-----------------------------------------------------------------*\
|
||||
| Create a QPluginLoader and load the plugin |
|
||||
\*-----------------------------------------------------------------*/
|
||||
QPluginLoader loader(QString().fromStdString(path));
|
||||
QObject* instance = loader.instance();
|
||||
|
||||
if (QObject *instance = loader.instance())
|
||||
/*-----------------------------------------------------------------*\
|
||||
| Check that the plugin is valid, then check the API version |
|
||||
\*-----------------------------------------------------------------*/
|
||||
if(instance)
|
||||
{
|
||||
if ((OpenRGBPlugin = qobject_cast<OpenRGBPluginInterface*>(instance)))
|
||||
plugin = qobject_cast<OpenRGBPluginInterface*>(instance);
|
||||
|
||||
if(plugin)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Initialize the plugin |
|
||||
\*-----------------------------------------------------*/
|
||||
OpenRGBPlugin->info = OpenRGBPlugin->Initialize(dark_theme, ResourceManager::get());
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Search the settings to see if it is enabled |
|
||||
\*-----------------------------------------------------*/
|
||||
std::string name = "";
|
||||
std::string description = "";
|
||||
bool enabled = true;
|
||||
bool found = false;
|
||||
unsigned int plugin_ct = 0;
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Open device disable list and read in disabled |
|
||||
| device strings |
|
||||
\*-------------------------------------------------*/
|
||||
json plugin_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("Plugins");
|
||||
|
||||
if(plugin_settings.contains("plugins"))
|
||||
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
|
||||
{
|
||||
plugin_ct = plugin_settings["plugins"].size();
|
||||
/*-----------------------------------------------------*\
|
||||
| Get the plugin information |
|
||||
\*-----------------------------------------------------*/
|
||||
OpenRGBPluginInfo info = plugin->GetPluginInfo();
|
||||
|
||||
for(unsigned int plugin_idx = 0; plugin_idx < plugin_settings["plugins"].size(); plugin_idx++)
|
||||
/*-----------------------------------------------------*\
|
||||
| Search the settings to see if it is enabled |
|
||||
\*-----------------------------------------------------*/
|
||||
std::string name = "";
|
||||
std::string description = "";
|
||||
bool enabled = true;
|
||||
bool found = false;
|
||||
unsigned int plugin_ct = 0;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Open plugin list and check if plugin is in the list |
|
||||
\*-----------------------------------------------------*/
|
||||
json plugin_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("Plugins");
|
||||
|
||||
if(plugin_settings.contains("plugins"))
|
||||
{
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("name"))
|
||||
{
|
||||
name = plugin_settings["plugins"][plugin_idx]["name"];
|
||||
}
|
||||
plugin_ct = plugin_settings["plugins"].size();
|
||||
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("description"))
|
||||
for(unsigned int plugin_idx = 0; plugin_idx < plugin_settings["plugins"].size(); plugin_idx++)
|
||||
{
|
||||
description = plugin_settings["plugins"][plugin_idx]["description"];
|
||||
}
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("name"))
|
||||
{
|
||||
name = plugin_settings["plugins"][plugin_idx]["name"];
|
||||
}
|
||||
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("enabled"))
|
||||
{
|
||||
enabled = plugin_settings["plugins"][plugin_idx]["enabled"];
|
||||
}
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("description"))
|
||||
{
|
||||
description = plugin_settings["plugins"][plugin_idx]["description"];
|
||||
}
|
||||
|
||||
if((OpenRGBPlugin->info.PluginName == name)
|
||||
&&(OpenRGBPlugin->info.PluginDescription == description))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("enabled"))
|
||||
{
|
||||
enabled = plugin_settings["plugins"][plugin_idx]["enabled"];
|
||||
}
|
||||
|
||||
if((info.Name == name)
|
||||
&&(info.Description == description))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!found)
|
||||
{
|
||||
plugin_settings["plugins"][plugin_ct]["name"] = OpenRGBPlugin->info.PluginName;
|
||||
plugin_settings["plugins"][plugin_ct]["description"] = OpenRGBPlugin->info.PluginDescription;
|
||||
plugin_settings["plugins"][plugin_ct]["enabled"] = enabled;
|
||||
/*-----------------------------------------------------*\
|
||||
| If the plugin was not in the list, add it to the list |
|
||||
| and default it to enabled, then save the settings |
|
||||
\*-----------------------------------------------------*/
|
||||
if(!found)
|
||||
{
|
||||
plugin_settings["plugins"][plugin_ct]["name"] = info.Name;
|
||||
plugin_settings["plugins"][plugin_ct]["description"] = info.Description;
|
||||
plugin_settings["plugins"][plugin_ct]["enabled"] = enabled;
|
||||
|
||||
ResourceManager::get()->GetSettingsManager()->SetSettings("Plugins", plugin_settings);
|
||||
ResourceManager::get()->GetSettingsManager()->SaveSettings();
|
||||
}
|
||||
ResourceManager::get()->GetSettingsManager()->SetSettings("Plugins", plugin_settings);
|
||||
ResourceManager::get()->GetSettingsManager()->SaveSettings();
|
||||
}
|
||||
|
||||
LOG_VERBOSE("Loaded plugin %s", OpenRGBPlugin->info.PluginName.c_str());
|
||||
LOG_VERBOSE("Loaded plugin %s", info.Name.c_str());
|
||||
|
||||
OpenRGBPluginEntry entry;
|
||||
/*-----------------------------------------------------*\
|
||||
| Add the plugin to the PluginManager active plugins |
|
||||
\*-----------------------------------------------------*/
|
||||
OpenRGBPluginEntry entry;
|
||||
|
||||
entry.plugin = OpenRGBPlugin;
|
||||
entry.path = path;
|
||||
entry.enabled = enabled;
|
||||
entry.info = info;
|
||||
entry.plugin = plugin;
|
||||
entry.path = path;
|
||||
entry.enabled = enabled;
|
||||
|
||||
PluginManager::ActivePlugins.push_back(entry);
|
||||
PluginManager::ActivePlugins.push_back(entry);
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
for(unsigned int callback_idx = 0; callback_idx < AddPluginTabCallbacks.size(); callback_idx++)
|
||||
{
|
||||
AddPluginTabCallbacks[callback_idx](AddPluginTabCallbackArgs[callback_idx], OpenRGBPlugin);
|
||||
/*-----------------------------------------------------*\
|
||||
| If the plugin is enabled, load it |
|
||||
\*-----------------------------------------------------*/
|
||||
if(enabled)
|
||||
{
|
||||
/*-------------------------------------------------*\
|
||||
| Initialize the plugin |
|
||||
\*-------------------------------------------------*/
|
||||
plugin->Initialize(dark_theme, ResourceManager::get());
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
for(unsigned int callback_idx = 0; callback_idx < AddPluginTabCallbacks.size(); callback_idx++)
|
||||
{
|
||||
AddPluginTabCallbacks[callback_idx](AddPluginTabCallbackArgs[callback_idx], entry);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
delete instance;
|
||||
loader.unload();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user