2017-04-20 03:22:04 +00:00
|
|
|
#include "FileResolvingTask.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
|
|
|
const char * metabase = "https://cursemeta.dries007.net";
|
|
|
|
|
2017-04-22 16:51:04 +00:00
|
|
|
Flame::FileResolvingTask::FileResolvingTask(Flame::Manifest& toProcess)
|
2017-04-20 03:22:04 +00:00
|
|
|
: m_toProcess(toProcess)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:51:04 +00:00
|
|
|
void Flame::FileResolvingTask::executeTask()
|
2017-04-20 03:22:04 +00:00
|
|
|
{
|
2017-05-03 21:11:52 +00:00
|
|
|
setStatus(tr("Resolving mod IDs..."));
|
2017-04-21 20:23:31 +00:00
|
|
|
setProgress(0, m_toProcess.files.size());
|
2017-04-22 16:51:04 +00:00
|
|
|
m_dljob.reset(new NetJob("Mod id resolver"));
|
2017-04-21 20:23:31 +00:00
|
|
|
results.resize(m_toProcess.files.size());
|
2017-04-20 03:22:04 +00:00
|
|
|
int index = 0;
|
2017-04-21 20:23:31 +00:00
|
|
|
for(auto & file: m_toProcess.files)
|
2017-04-20 03:22:04 +00:00
|
|
|
{
|
|
|
|
auto projectIdStr = QString::number(file.projectId);
|
|
|
|
auto fileIdStr = QString::number(file.fileId);
|
|
|
|
QString metaurl = QString("%1/%2/%3.json").arg(metabase, projectIdStr, fileIdStr);
|
|
|
|
auto dl = Net::Download::makeByteArray(QUrl(metaurl), &results[index]);
|
|
|
|
m_dljob->addNetAction(dl);
|
|
|
|
index ++;
|
|
|
|
}
|
2017-04-22 16:51:04 +00:00
|
|
|
connect(m_dljob.get(), &NetJob::finished, this, &Flame::FileResolvingTask::netJobFinished);
|
2017-04-20 03:22:04 +00:00
|
|
|
m_dljob->start();
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:51:04 +00:00
|
|
|
void Flame::FileResolvingTask::netJobFinished()
|
2017-04-20 03:22:04 +00:00
|
|
|
{
|
|
|
|
bool failed = false;
|
|
|
|
int index = 0;
|
|
|
|
for(auto & bytes: results)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto doc = Json::requireDocument(bytes);
|
|
|
|
auto obj = Json::requireObject(doc);
|
2017-05-09 19:56:33 +00:00
|
|
|
auto & out = m_toProcess.files[index];
|
2017-04-20 03:22:04 +00:00
|
|
|
// result code signifies true failure.
|
|
|
|
if(obj.contains("code"))
|
|
|
|
{
|
2017-05-09 19:56:33 +00:00
|
|
|
qCritical() << "Resolving of" << out.projectId << out.fileId << "failed because of a negative result:";
|
|
|
|
qCritical() << bytes;
|
2017-04-20 03:22:04 +00:00
|
|
|
failed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.fileName = Json::requireString(obj, "FileNameOnDisk");
|
2018-01-28 23:47:18 +00:00
|
|
|
QString rawUrl = Json::requireString(obj, "DownloadURL");
|
|
|
|
out.url = QUrl(rawUrl, QUrl::TolerantMode);
|
2018-01-21 02:49:54 +00:00
|
|
|
if(!out.url.isValid())
|
|
|
|
{
|
2018-01-28 23:47:18 +00:00
|
|
|
throw JSONValidationError(QString("Invalid URL: %1").arg(rawUrl));
|
2018-01-21 02:49:54 +00:00
|
|
|
}
|
2017-09-08 07:02:27 +00:00
|
|
|
// This is a piece of a Flame project JSON pulled out into the file metadata (here) for convenience
|
|
|
|
// It is also optional
|
|
|
|
QJsonObject projObj = Json::ensureObject(obj, "_Project", {});
|
|
|
|
if(!projObj.isEmpty())
|
|
|
|
{
|
|
|
|
QString strType = Json::ensureString(projObj, "PackageType", "mod").toLower();
|
|
|
|
if(strType == "singlefile")
|
|
|
|
{
|
|
|
|
out.type = File::Type::SingleFile;
|
|
|
|
}
|
|
|
|
else if(strType == "ctoc")
|
|
|
|
{
|
|
|
|
out.type = File::Type::Ctoc;
|
|
|
|
}
|
|
|
|
else if(strType == "cmod2")
|
|
|
|
{
|
|
|
|
out.type = File::Type::Cmod2;
|
|
|
|
}
|
|
|
|
else if(strType == "mod")
|
|
|
|
{
|
|
|
|
out.type = File::Type::Mod;
|
|
|
|
}
|
|
|
|
else if(strType == "folder")
|
|
|
|
{
|
|
|
|
out.type = File::Type::Folder;
|
|
|
|
}
|
|
|
|
else if(strType == "modpack")
|
|
|
|
{
|
|
|
|
out.type = File::Type::Modpack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCritical() << "Resolving of" << out.projectId << out.fileId << "failed because of unknown file type:" << strType;
|
|
|
|
out.type = File::Type::Unknown;
|
|
|
|
failed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.targetFolder = Json::ensureString(projObj, "Path", "mods");
|
|
|
|
}
|
2017-04-20 03:22:04 +00:00
|
|
|
out.resolved = true;
|
|
|
|
}
|
2018-05-19 23:18:26 +00:00
|
|
|
catch (const JSONValidationError &e)
|
2017-04-20 03:22:04 +00:00
|
|
|
{
|
2017-05-09 19:56:33 +00:00
|
|
|
auto & out = m_toProcess.files[index];
|
|
|
|
qCritical() << "Resolving of" << out.projectId << out.fileId << "failed because of a parsing error:";
|
2017-05-09 19:49:36 +00:00
|
|
|
qCritical() << e.cause();
|
|
|
|
qCritical() << "JSON:";
|
|
|
|
qCritical() << bytes;
|
2017-04-20 03:22:04 +00:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if(!failed)
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-22 16:51:04 +00:00
|
|
|
emitFailed(tr("Some mod ID resolving tasks failed."));
|
2017-04-20 03:22:04 +00:00
|
|
|
}
|
|
|
|
}
|