NOISSUE sanitize Json
Removes magical parameter madness. All require* can throw All ensure* need a default value and never throw
This commit is contained in:
@ -157,11 +157,11 @@ void MinecraftVersionList::loadBuiltinList()
|
||||
qDebug() << "Loading builtin version list.";
|
||||
// grab the version list data from internal resources.
|
||||
const QJsonDocument doc =
|
||||
Json::ensureDocument(QString(":/versions/minecraft.json"), "builtin version list");
|
||||
Json::requireDocument(QString(":/versions/minecraft.json"), "builtin version list");
|
||||
const QJsonObject root = doc.object();
|
||||
|
||||
// parse all the versions
|
||||
for (const auto version : Json::ensureArray(root.value("versions")))
|
||||
for (const auto version : Json::requireArray(root.value("versions")))
|
||||
{
|
||||
QJsonObject versionObj = version.toObject();
|
||||
QString versionID = versionObj.value("id").toString("");
|
||||
@ -203,9 +203,9 @@ void MinecraftVersionList::loadBuiltinList()
|
||||
mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
|
||||
if (versionObj.contains("+traits"))
|
||||
{
|
||||
for (auto traitVal : Json::ensureArray(versionObj.value("+traits")))
|
||||
for (auto traitVal : Json::requireArray(versionObj.value("+traits")))
|
||||
{
|
||||
mcVersion->m_traits.insert(Json::ensureString(traitVal));
|
||||
mcVersion->m_traits.insert(Json::requireString(traitVal));
|
||||
}
|
||||
}
|
||||
m_lookup[versionID] = mcVersion;
|
||||
@ -226,9 +226,9 @@ void MinecraftVersionList::loadMojangList(QJsonDocument jsonDoc, VersionSource s
|
||||
|
||||
try
|
||||
{
|
||||
QJsonObject latest = Json::ensureObject(root.value("latest"));
|
||||
m_latestReleaseID = Json::ensureString(latest.value("release"));
|
||||
m_latestSnapshotID = Json::ensureString(latest.value("snapshot"));
|
||||
QJsonObject latest = Json::requireObject(root.value("latest"));
|
||||
m_latestReleaseID = Json::requireString(latest.value("release"));
|
||||
m_latestSnapshotID = Json::requireString(latest.value("snapshot"));
|
||||
}
|
||||
catch (Exception &err)
|
||||
{
|
||||
|
@ -74,18 +74,18 @@ bool readOverrideOrders(QString path, PatchOrder &order)
|
||||
// and then read it and process it if all above is true.
|
||||
try
|
||||
{
|
||||
auto obj = Json::ensureObject(doc);
|
||||
auto obj = Json::requireObject(doc);
|
||||
// check order file version.
|
||||
auto version = Json::ensureInteger(obj.value("version"));
|
||||
auto version = Json::requireInteger(obj.value("version"));
|
||||
if (version != currentOrderFileVersion)
|
||||
{
|
||||
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
||||
.arg(currentOrderFileVersion));
|
||||
}
|
||||
auto orderArray = Json::ensureArray(obj.value("order"));
|
||||
auto orderArray = Json::requireArray(obj.value("order"));
|
||||
for(auto item: orderArray)
|
||||
{
|
||||
order.append(Json::ensureString(item));
|
||||
order.append(Json::requireString(item));
|
||||
}
|
||||
}
|
||||
catch (JSONValidationError &err)
|
||||
|
@ -39,15 +39,15 @@ RawLibraryPtr RawLibrary::fromJson(const QJsonObject &libObj, const QString &fil
|
||||
if (libObj.contains("extract"))
|
||||
{
|
||||
out->applyExcludes = true;
|
||||
auto extractObj = ensureObject(libObj.value("extract"));
|
||||
for (auto excludeVal : ensureArray(extractObj.value("exclude")))
|
||||
auto extractObj = requireObject(libObj.value("extract"));
|
||||
for (auto excludeVal : requireArray(extractObj.value("exclude")))
|
||||
{
|
||||
out->extract_excludes.append(ensureString(excludeVal));
|
||||
out->extract_excludes.append(requireString(excludeVal));
|
||||
}
|
||||
}
|
||||
if (libObj.contains("natives"))
|
||||
{
|
||||
QJsonObject nativesObj = ensureObject(libObj.value("natives"));
|
||||
QJsonObject nativesObj = requireObject(libObj.value("natives"));
|
||||
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
||||
{
|
||||
if (!it.value().isString())
|
||||
@ -127,7 +127,7 @@ RawLibraryPtr RawLibrary::fromJsonPlus(const QJsonObject &libObj, const QString
|
||||
}
|
||||
if (libObj.contains("MMC-depend"))
|
||||
{
|
||||
const QString dependString = ensureString(libObj.value("MMC-depend"));
|
||||
const QString dependString = requireString(libObj.value("MMC-depend"));
|
||||
if (dependString == "hard")
|
||||
{
|
||||
lib->dependType = RawLibrary::Hard;
|
||||
|
@ -52,7 +52,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
{
|
||||
if (root.contains("order"))
|
||||
{
|
||||
out->order = ensureInteger(root.value("order"));
|
||||
out->order = requireInteger(root.value("order"));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -71,7 +71,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
{
|
||||
if (root.contains(key))
|
||||
{
|
||||
variable = ensureString(root.value(key));
|
||||
variable = requireString(root.value(key));
|
||||
}
|
||||
};
|
||||
|
||||
@ -79,7 +79,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
{
|
||||
if (root.contains(key))
|
||||
{
|
||||
return ensureString(root.value(key));
|
||||
return requireString(root.value(key));
|
||||
}
|
||||
return QString();
|
||||
};
|
||||
@ -101,48 +101,48 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
|
||||
if (root.contains("minimumLauncherVersion"))
|
||||
{
|
||||
out->minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion"));
|
||||
out->minimumLauncherVersion = requireInteger(root.value("minimumLauncherVersion"));
|
||||
}
|
||||
|
||||
if (root.contains("tweakers"))
|
||||
{
|
||||
out->shouldOverwriteTweakers = true;
|
||||
for (auto tweakerVal : ensureArray(root.value("tweakers")))
|
||||
for (auto tweakerVal : requireArray(root.value("tweakers")))
|
||||
{
|
||||
out->overwriteTweakers.append(ensureString(tweakerVal));
|
||||
out->overwriteTweakers.append(requireString(tweakerVal));
|
||||
}
|
||||
}
|
||||
|
||||
if (root.contains("+tweakers"))
|
||||
{
|
||||
for (auto tweakerVal : ensureArray(root.value("+tweakers")))
|
||||
for (auto tweakerVal : requireArray(root.value("+tweakers")))
|
||||
{
|
||||
out->addTweakers.append(ensureString(tweakerVal));
|
||||
out->addTweakers.append(requireString(tweakerVal));
|
||||
}
|
||||
}
|
||||
|
||||
if (root.contains("-tweakers"))
|
||||
{
|
||||
for (auto tweakerVal : ensureArray(root.value("-tweakers")))
|
||||
for (auto tweakerVal : requireArray(root.value("-tweakers")))
|
||||
{
|
||||
out->removeTweakers.append(ensureString(tweakerVal));
|
||||
out->removeTweakers.append(requireString(tweakerVal));
|
||||
}
|
||||
}
|
||||
|
||||
if (root.contains("+traits"))
|
||||
{
|
||||
for (auto tweakerVal : ensureArray(root.value("+traits")))
|
||||
for (auto tweakerVal : requireArray(root.value("+traits")))
|
||||
{
|
||||
out->traits.insert(ensureString(tweakerVal));
|
||||
out->traits.insert(requireString(tweakerVal));
|
||||
}
|
||||
}
|
||||
|
||||
if (root.contains("libraries"))
|
||||
{
|
||||
out->shouldOverwriteLibs = true;
|
||||
for (auto libVal : ensureArray(root.value("libraries")))
|
||||
for (auto libVal : requireArray(root.value("libraries")))
|
||||
{
|
||||
auto libObj = ensureObject(libVal);
|
||||
auto libObj = requireObject(libVal);
|
||||
|
||||
auto lib = RawLibrary::fromJson(libObj, filename);
|
||||
out->overwriteLibs.append(lib);
|
||||
@ -151,9 +151,9 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
|
||||
if (root.contains("+jarMods"))
|
||||
{
|
||||
for (auto libVal : ensureArray(root.value("+jarMods")))
|
||||
for (auto libVal : requireArray(root.value("+jarMods")))
|
||||
{
|
||||
QJsonObject libObj = ensureObject(libVal);
|
||||
QJsonObject libObj = requireObject(libVal);
|
||||
// parse the jarmod
|
||||
auto lib = Jarmod::fromJson(libObj, filename, out->name);
|
||||
if(lib->originalName.isEmpty())
|
||||
@ -169,9 +169,9 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
|
||||
if (root.contains("+libraries"))
|
||||
{
|
||||
for (auto libVal : ensureArray(root.value("+libraries")))
|
||||
for (auto libVal : requireArray(root.value("+libraries")))
|
||||
{
|
||||
QJsonObject libObj = ensureObject(libVal);
|
||||
QJsonObject libObj = requireObject(libVal);
|
||||
// parse the library
|
||||
auto lib = RawLibrary::fromJsonPlus(libObj, filename);
|
||||
out->addLibs.append(lib);
|
||||
@ -180,10 +180,10 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
||||
|
||||
if (root.contains("-libraries"))
|
||||
{
|
||||
for (auto libVal : ensureArray(root.value("-libraries")))
|
||||
for (auto libVal : requireArray(root.value("-libraries")))
|
||||
{
|
||||
auto libObj = ensureObject(libVal);
|
||||
out->removeLibs.append(ensureString(libObj.value("name")));
|
||||
auto libObj = requireObject(libVal);
|
||||
out->removeLibs.append(requireString(libObj.value("name")));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
|
Reference in New Issue
Block a user