-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptmanager.cpp
120 lines (114 loc) · 3.16 KB
/
scriptmanager.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
#include "scriptmanager.h"
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
ScriptManager *ScriptManager::m_instance = nullptr;
ScriptManager *ScriptManager::instance() {
if (m_instance == nullptr)
m_instance = new ScriptManager;
return m_instance;
}
ScriptManager::ScriptManager(QObject *parent) : QObject(parent) {
auto dir = PLUGINDIR + "/WingHexPyScript";
QDir pdir(dir);
if (!pdir.exists()) {
return;
}
pdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
auto fdirs = pdir.entryInfoList();
for (auto folder : fdirs) {
auto na = folder.fileName();
if (na == ".git")
continue;
QDir jdir(folder.filePath());
QList<ScriptMeta> m;
jdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
auto paks = jdir.entryInfoList();
for (auto f : paks) {
auto na = f.fileName();
auto fn = f.absoluteFilePath();
QDir dir(fn);
auto j = fn + "/" + f.fileName() + ".json";
if (QFile::exists(j))
parseMeta(QFileInfo(j), fn, m);
}
m_metas.insert(folder.fileName(), m);
}
}
void ScriptManager::loadMenu(QMenu *menu) {
if (!menu)
return;
if (!m_metas.count()) {
auto a = new QAction(tr("NoScript"));
a->setEnabled(false);
menu->addAction(a);
return;
}
auto ke = m_metas.keyValueEnd();
auto k = m_metas.keyValueBegin();
for (; k != ke; k++) {
auto i = new QMenu;
auto n = *k;
i->setTitle(n.first);
for (auto mitem : n.second) {
QAction *a;
PluginMenuAddItemLamba(i, mitem.name, [=] {
PlgInterface::instance()->RunPyFile(mitem.filename);
});
}
menu->addMenu(i);
}
}
void ScriptManager::loadTreeWidget(QTreeWidget *tree) {
if (!tree)
return;
tree->clear();
auto ke = m_metas.keyValueEnd();
auto k = m_metas.keyValueBegin();
for (; k != ke; k++) {
auto n = *k;
auto p = new QTreeWidgetItem(tree, {k->first});
p->setIcon(0, QIcon(":/WingHexPy/img/pydb.png"));
for (auto mitem : n.second) {
auto i = new QTreeWidgetItem(p, {mitem.name});
i->setIcon(0, QIcon(":/WingHexPy/img/py.png"));
i->setData(0, Qt::UserRole, QVariant::fromValue(mitem));
}
}
}
void ScriptManager::parseMeta(QFileInfo fileinfo, QString folder,
QList<ScriptMeta> &m) {
QFile f(fileinfo.absoluteFilePath());
QJsonParseError err;
if (!f.open(QFile::ReadOnly))
return;
QJsonDocument doc = QJsonDocument::fromJson(f.readAll(), &err);
f.close();
if (err.error != QJsonParseError::NoError)
return;
auto jobj = doc.object();
auto n = fileinfo.fileName();
ScriptMeta meta;
auto p = n.lastIndexOf('.');
meta.name = n.left(p);
meta.filename = folder + "/" + meta.name + ".py";
auto t = jobj.value("author");
if (!t.isUndefined() && t.isString()) {
meta.author = t.toString();
}
t = jobj.value("license");
if (!t.isUndefined() && t.isString()) {
meta.license = t.toString();
} else {
meta.license = "Unlicense";
}
t = jobj.value("commnet");
if (!t.isUndefined() && t.isString()) {
meta.commnet = t.toString();
}
t = jobj.value("version");
meta.version = uint(t.toInt());
m.push_back(meta);
}