Validate JSON parsing results
Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
parent
0a1001ee84
commit
50fb2db718
2 changed files with 13 additions and 12 deletions
|
@ -427,7 +427,19 @@ QList<ShortcutData> BaseInstance::shortcuts() const
|
||||||
QList<ShortcutData> results;
|
QList<ShortcutData> results;
|
||||||
for (const auto& elem : document.array()) {
|
for (const auto& elem : document.array()) {
|
||||||
auto dict = elem.toObject();
|
auto dict = elem.toObject();
|
||||||
results.append({ dict["name"].toString(), dict["filePath"].toString(), static_cast<ShortcutTarget>(dict["target"].toInt()) });
|
if (!dict.contains("name") || !dict.contains("filePath") || !dict.contains("target"))
|
||||||
|
return {};
|
||||||
|
int value = dict["target"].toInt(-1);
|
||||||
|
if (!dict["name"].isString() || !dict["filePath"].isString() || value < 0 || value >= 3)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
QString shortcutName = dict["name"].toString();
|
||||||
|
QString filePath = dict["filePath"].toString();
|
||||||
|
if (!QDir(filePath).exists()) {
|
||||||
|
qWarning() << "Shortcut" << shortcutName << "for instance" << name() << "have non-existent path" << filePath;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
results.append({ shortcutName, filePath, static_cast<ShortcutTarget>(value) });
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
|
@ -686,18 +686,7 @@ InstancePtr InstanceList::loadInstance(const InstanceId& id)
|
||||||
}
|
}
|
||||||
qDebug() << "Loaded instance" << inst->name() << "from" << inst->instanceRoot();
|
qDebug() << "Loaded instance" << inst->name() << "from" << inst->instanceRoot();
|
||||||
|
|
||||||
// Fixup the shortcuts by pruning all non-existing links
|
|
||||||
auto shortcut = inst->shortcuts();
|
auto shortcut = inst->shortcuts();
|
||||||
for (auto it = shortcut.begin(); it != shortcut.end();) {
|
|
||||||
const auto& [name, filePath, target] = *it;
|
|
||||||
if (!QDir(filePath).exists()) {
|
|
||||||
qWarning() << "Shortcut" << name << "have non-existent path" << filePath;
|
|
||||||
it = shortcut.erase(it);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
inst->setShortcuts(shortcut);
|
|
||||||
if (!shortcut.isEmpty())
|
if (!shortcut.isEmpty())
|
||||||
qDebug() << "Loaded" << shortcut.size() << "shortcut(s) for instance" << inst->name();
|
qDebug() << "Loaded" << shortcut.size() << "shortcut(s) for instance" << inst->name();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue