-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPluginManager.cpp
331 lines (281 loc) · 13.3 KB
/
PluginManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "LogManager.h"
#include "PluginManager.h"
PluginManager::PluginManager(bool dark_theme_val)
{
/*---------------------------------------------------------*\
| Initialize plugin manager class variables |
\*---------------------------------------------------------*/
dark_theme = dark_theme_val;
AddPluginCallbackVal = nullptr;
AddPluginCallbackArg = nullptr;
RemovePluginCallbackVal = nullptr;
RemovePluginCallbackArg = nullptr;
}
void PluginManager::RegisterAddPluginCallback(AddPluginCallback new_callback, void * new_callback_arg)
{
AddPluginCallbackVal = new_callback;
AddPluginCallbackArg = new_callback_arg;
}
void PluginManager::RegisterRemovePluginCallback(RemovePluginCallback new_callback, void * new_callback_arg)
{
RemovePluginCallbackVal = new_callback;
RemovePluginCallbackArg = new_callback_arg;
}
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++)
{
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();
AddPlugin(plugin_path);
}
}
void PluginManager::AddPlugin(std::string path)
{
OpenRGBPluginInterface* plugin = nullptr;
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the path does not match an existing entry, create a new entry |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
/*-----------------------------------------------------------------*\
| Create a QPluginLoader and load the plugin |
\*-----------------------------------------------------------------*/
QPluginLoader* loader = new QPluginLoader(QString().fromStdString(path));
QObject* instance = loader->instance();
/*-----------------------------------------------------------------*\
| Check that the plugin is valid, then check the API version |
\*-----------------------------------------------------------------*/
if(instance)
{
plugin = qobject_cast<OpenRGBPluginInterface*>(instance);
if(plugin)
{
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
{
/*-----------------------------------------------------*\
| Get the plugin information |
\*-----------------------------------------------------*/
OpenRGBPluginInfo info = plugin->GetPluginInfo();
/*-----------------------------------------------------*\
| 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"))
{
plugin_ct = plugin_settings["plugins"].size();
for(unsigned int plugin_idx = 0; plugin_idx < plugin_settings["plugins"].size(); plugin_idx++)
{
if(plugin_settings["plugins"][plugin_idx].contains("name"))
{
name = plugin_settings["plugins"][plugin_idx]["name"];
}
if(plugin_settings["plugins"][plugin_idx].contains("description"))
{
description = plugin_settings["plugins"][plugin_idx]["description"];
}
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 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();
}
LOG_VERBOSE("Loaded plugin %s", info.Name.c_str());
/*-----------------------------------------------------*\
| Add the plugin to the PluginManager active plugins |
\*-----------------------------------------------------*/
OpenRGBPluginEntry entry;
entry.info = info;
entry.plugin = plugin;
entry.loader = loader;
entry.loaded = false;
entry.path = path;
entry.enabled = enabled;
entry.widget = nullptr;
loader->unload();
PluginManager::ActivePlugins.push_back(entry);
if(entry.enabled)
{
LoadPlugin(path);
}
}
}
}
}
}
void PluginManager::RemovePlugin(std::string path)
{
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
return;
}
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(ActivePlugins[plugin_idx].loaded)
{
UnloadPlugin(path);
}
/*---------------------------------------------------------------------*\
| Remove the plugin from the active plugins list |
\*---------------------------------------------------------------------*/
ActivePlugins.erase(ActivePlugins.begin() + plugin_idx);
}
void PluginManager::LoadPlugin(std::string path)
{
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
return;
}
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list but not loaded, load it |
\*---------------------------------------------------------------------*/
if(!ActivePlugins[plugin_idx].loaded)
{
ActivePlugins[plugin_idx].loader->load();
ActivePlugins[plugin_idx].loaded = true;
QObject* instance = ActivePlugins[plugin_idx].loader->instance();
if(instance)
{
OpenRGBPluginInterface* plugin = qobject_cast<OpenRGBPluginInterface*>(instance);
if(plugin)
{
if(plugin->GetPluginAPIVersion() == OPENRGB_PLUGIN_API_VERSION)
{
ActivePlugins[plugin_idx].plugin = plugin;
plugin->Load(dark_theme, ResourceManager::get());
/*-------------------------------------------------*\
| Call the Add Plugin callback |
\*-------------------------------------------------*/
if(AddPluginCallbackArg != nullptr)
{
AddPluginCallbackVal(AddPluginCallbackArg, &ActivePlugins[plugin_idx]);
}
}
}
}
}
}
void PluginManager::UnloadPlugin(std::string path)
{
unsigned int plugin_idx;
/*---------------------------------------------------------------------*\
| Search active plugins to see if this path already exists |
\*---------------------------------------------------------------------*/
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
{
if(path == ActivePlugins[plugin_idx].path)
{
break;
}
}
/*---------------------------------------------------------------------*\
| If the plugin path does not exist in the active plugins list, return |
\*---------------------------------------------------------------------*/
if(plugin_idx == ActivePlugins.size())
{
return;
}
/*---------------------------------------------------------------------*\
| If the selected plugin is in the list and loaded, unload it |
\*---------------------------------------------------------------------*/
if(ActivePlugins[plugin_idx].loaded)
{
/*-------------------------------------------------*\
| Call plugin's Unload function before GUI removal |
\*-------------------------------------------------*/
ActivePlugins[plugin_idx].plugin->Unload();
/*-------------------------------------------------*\
| Call the Remove Plugin callback |
\*-------------------------------------------------*/
if(RemovePluginCallbackVal != nullptr)
{
RemovePluginCallbackVal(RemovePluginCallbackArg, &ActivePlugins[plugin_idx]);
}
ActivePlugins[plugin_idx].loader->unload();
ActivePlugins[plugin_idx].loaded = false;
}
}