-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathEffectFileHandler.cpp
372 lines (317 loc) · 10.5 KB
/
EffectFileHandler.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <effectengine/EffectFileHandler.h>
// util
#include <utils/JsonUtils.h>
// qt
#include <QJsonArray>
#include <QFileInfo>
#include <QDir>
#include <QMap>
#include <QByteArray>
EffectFileHandler* EffectFileHandler::efhInstance;
EffectFileHandler::EffectFileHandler(const QString& rootPath, const QJsonDocument& effectConfig, QObject* parent)
: QObject(parent)
, _log(Logger::getInstance("EFFECTFILES"))
, _rootPath(rootPath)
{
EffectFileHandler::efhInstance = this;
Q_INIT_RESOURCE(EffectEngine);
// init
handleSettingsUpdate(settings::EFFECTS, effectConfig);
}
void EffectFileHandler::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if (type == settings::EFFECTS)
{
_effectConfig = config.object();
QJsonArray effectPathArray = _effectConfig["paths"].toArray();
// TODO: Remove workaround and move effect config to global settings
if (effectPathArray.empty())
{
effectPathArray.append("$ROOT/custom-effects");
}
_effectConfig["paths"] = effectPathArray;
// update effects and schemas
updateEffects();
}
}
QString EffectFileHandler::deleteEffect(const QString& effectName)
{
QString resultMsg;
std::list<EffectDefinition> effectsDefinition = getEffects();
std::list<EffectDefinition>::iterator it = std::find_if(effectsDefinition.begin(), effectsDefinition.end(),
[&effectName](const EffectDefinition& effectDefinition) {return effectDefinition.name == effectName; }
);
if (it != effectsDefinition.end())
{
QFileInfo effectConfigurationFile(it->file);
if (!effectConfigurationFile.absoluteFilePath().startsWith(':'))
{
if (effectConfigurationFile.exists())
{
if ((it->script == ":/effects/gif.py") && !it->args.value("file").toString("").isEmpty())
{
QFileInfo effectImageFile(it->args.value("file").toString());
if (effectImageFile.exists())
{
QFile::remove(effectImageFile.absoluteFilePath());
}
}
bool result = QFile::remove(effectConfigurationFile.absoluteFilePath());
if (result)
{
updateEffects();
resultMsg = "";
}
else
{
resultMsg = "Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions";
}
}
else
{
resultMsg = "Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath();
}
}
else
{
resultMsg = "Can't delete internal effect: " + effectName;
}
}
else
{
resultMsg = "Effect " + effectName + " not found";
}
return resultMsg;
}
QString EffectFileHandler::saveEffect(const QJsonObject& message)
{
QString resultMsg;
if (!message["args"].toObject().isEmpty())
{
QString scriptName = message["script"].toString();
std::list<EffectSchema> effectsSchemas = getEffectSchemas();
std::list<EffectSchema>::iterator it = std::find_if(effectsSchemas.begin(), effectsSchemas.end(),
[&scriptName](const EffectSchema& schema) {return schema.pyFile == scriptName; }
);
if (it != effectsSchemas.end())
{
if (!JsonUtils::validate("EffectFileHandler", message["args"], it->schemaFile, _log).first)
{
return "Error during arg validation against schema, please consult the Hyperion Log";
}
QJsonObject effectJson;
QJsonArray effectArray;
effectArray = _effectConfig["paths"].toArray();
if (!effectArray.empty())
{
QString effectName = message["name"].toString();
if (effectName.trimmed().isEmpty() || effectName.trimmed().startsWith(":"))
{
return "Can't save new effect. Effect name is empty or begins with a dot.";
}
effectJson["name"] = effectName;
effectJson["script"] = message["script"].toString();
effectJson["args"] = message["args"].toObject();
std::list<EffectDefinition> availableEffects = getEffects();
std::list<EffectDefinition>::iterator iter = std::find_if(availableEffects.begin(), availableEffects.end(),
[&effectName](const EffectDefinition& effectDefinition) {return effectDefinition.name == effectName; }
);
QFileInfo newFileName;
if (iter != availableEffects.end())
{
newFileName.setFile(iter->file);
if (newFileName.absoluteFilePath().startsWith(':'))
{
return "The effect name '" + effectName + "' is assigned to an internal effect. Please rename your effect.";
}
}
else
{
QString f = effectArray[0].toString().replace("$ROOT", _rootPath) + '/' + effectName.replace(QString(" "), QString("")) + QString(".json");
newFileName.setFile(f);
}
if (!message["imageData"].toString("").isEmpty() && !message["args"].toObject().value("file").toString("").isEmpty())
{
QJsonObject args = message["args"].toObject();
QString imageFilePath = effectArray[0].toString().replace("$ROOT", _rootPath) + '/' + args.value("file").toString();
QFileInfo imageFileName(imageFilePath);
if (!FileUtils::writeFile(imageFileName.absoluteFilePath(), QByteArray::fromBase64(message["imageData"].toString("").toUtf8()), _log))
{
return "Error while saving image file '" + message["args"].toObject().value("file").toString() + ", please check the Hyperion Log";
}
//Update json with image file location
args["file"] = imageFilePath;
effectJson["args"] = args;
}
if (message["args"].toObject().value("imageSource").toString("") == "url" || message["args"].toObject().value("imageSource").toString("") == "file")
{
QJsonObject args = message["args"].toObject();
args.remove(args.value("imageSource").toString("") == "url" ? "file" : "url");
effectJson["args"] = args;
}
if (!JsonUtils::write(newFileName.absoluteFilePath(), effectJson, _log))
{
return "Error while saving effect, please check the Hyperion Log";
}
Info(_log, "Reload effect list");
updateEffects();
resultMsg = "";
}
else
{
resultMsg = "Can't save new effect. Effect path empty";
}
}
else
{
resultMsg = "Missing schema file for Python script " + message["script"].toString();
}
}
else
{
resultMsg = "Missing or empty Object 'args'";
}
return resultMsg;
}
void EffectFileHandler::updateEffects()
{
// clear all lists
_availableEffects.clear();
_effectSchemas.clear();
// read all effects
const QJsonArray& paths = _effectConfig["paths"].toArray();
const QJsonArray& disabledEfx = _effectConfig["disable"].toArray();
QStringList efxPathList;
efxPathList << ":/effects/";
QStringList disableList;
for (const auto& p : paths)
{
QString effectPath = p.toString();
if (!effectPath.endsWith('/'))
{
effectPath.append('/');
}
efxPathList << effectPath.replace("$ROOT", _rootPath);
}
for (const auto& efx : disabledEfx)
{
disableList << efx.toString();
}
QMap<QString, EffectDefinition> availableEffects;
for (const QString& path : std::as_const(efxPathList))
{
QDir directory(path);
if (!directory.exists())
{
if (directory.mkpath(path))
{
Info(_log, "New Effect path \"%s\" created successfully", QSTRING_CSTR(path));
}
else
{
Warning(_log, "Failed to create Effect path \"%s\", please check permissions", QSTRING_CSTR(path));
}
}
else
{
int efxCount = 0;
const QStringList filenames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
for (const QString& filename : filenames)
{
EffectDefinition def;
if (loadEffectDefinition(path, filename, def))
{
InfoIf(availableEffects.find(def.name) != availableEffects.end(), _log,
"effect overload effect '%s' is now taken from '%s'", QSTRING_CSTR(def.name), QSTRING_CSTR(path));
if (disableList.contains(def.name))
{
Info(_log, "effect '%s' not loaded, because it is disabled in hyperion config", QSTRING_CSTR(def.name));
}
else
{
availableEffects[def.name] = def;
efxCount++;
}
}
}
Info(_log, "%d effects loaded from directory %s", efxCount, QSTRING_CSTR(path));
// collect effect schemas
efxCount = 0;
QString schemaPath = path + "schema" + '/';
directory.setPath(schemaPath);
const QStringList schemaFileNames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
for (const QString& schemaFileName : schemaFileNames)
{
EffectSchema pyEffect;
if (loadEffectSchema(path, directory.filePath(schemaFileName), pyEffect))
{
_effectSchemas.push_back(pyEffect);
efxCount++;
}
}
InfoIf(efxCount > 0, _log, "%d effect schemas loaded from directory %s", efxCount, QSTRING_CSTR(schemaPath));
}
}
for (const auto& item : std::as_const(availableEffects))
{
_availableEffects.push_back(item);
}
ErrorIf(_availableEffects.empty(), _log, "no effects found, check your effect directories");
emit effectListChanged();
}
bool EffectFileHandler::loadEffectDefinition(const QString& path, const QString& effectConfigFile, EffectDefinition& effectDefinition)
{
QString fileName = path + effectConfigFile;
// Read and parse the effect json config file
QJsonObject configEffect;
if (!JsonUtils::readFile(fileName, configEffect, _log).first) {
return false;
}
// validate effect config with effect schema(path)
if (!JsonUtils::validate(fileName, configEffect, ":effect-schema", _log).first) {
return false;
}
// setup the definition
effectDefinition.file = fileName;
QJsonObject config = configEffect;
QString scriptName = config["script"].toString();
effectDefinition.name = config["name"].toString();
if (scriptName.isEmpty()) {
return false;
}
QFile fileInfo(scriptName);
if (!fileInfo.exists())
{
effectDefinition.script = path + scriptName;
}
else
{
effectDefinition.script = scriptName;
}
effectDefinition.args = config["args"].toObject();
effectDefinition.smoothCfg = 1; // pause config
return true;
}
bool EffectFileHandler::loadEffectSchema(const QString& path, const QString& schemaFilePath, EffectSchema& effectSchema)
{
// Read and parse the effect schema file
QJsonObject schemaEffect;
if (!JsonUtils::readFile(schemaFilePath, schemaEffect, _log).first)
{
return false;
}
// setup the definition
QString scriptName = schemaEffect["script"].toString();
effectSchema.schemaFile = schemaFilePath;
QString scriptFilePath = path + scriptName;
QFile pyScriptFile(scriptFilePath);
if (scriptName.isEmpty() || !pyScriptFile.open(QIODevice::ReadOnly))
{
Error(_log, "Python script '%s' in effect schema '%s' could not be loaded", QSTRING_CSTR(scriptName), QSTRING_CSTR(schemaFilePath));
return false;
}
pyScriptFile.close();
effectSchema.pyFile = scriptFilePath;
effectSchema.pySchema = schemaEffect;
return true;
}