Technic: Replace inline parsing code with API models

This commit is contained in:
Jamie Mansfield 2022-04-02 13:26:03 +01:00
parent 8df88e7fbb
commit c8205fda9f
No known key found for this signature in database
GPG Key ID: 36F61598F39F67B0

View File

@ -1,16 +1,36 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * This program is free software: you can redistribute it and/or modify
* you may not use this file except in compliance with the License. * it under the terms of the GNU General Public License as published by
* You may obtain a copy of the License at * the Free Software Foundation, version 3.
* *
* http://www.apache.org/licenses/LICENSE-2.0 * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* *
* Unless required by applicable law or agreed to in writing, software * You should have received a copy of the GNU General Public License
* distributed under the License is distributed on an "AS IS" BASIS, * along with this program. If not, see <https://www.gnu.org/licenses/>.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and * This file incorporates work covered by the following copyright and
* limitations under the License. * permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
#include "SolderPackInstallTask.h" #include "SolderPackInstallTask.h"
@ -19,7 +39,9 @@
#include <Json.h> #include <Json.h>
#include <QtConcurrentRun> #include <QtConcurrentRun>
#include <MMCZip.h> #include <MMCZip.h>
#include "TechnicPackProcessor.h" #include "TechnicPackProcessor.h"
#include "SolderPackManifest.h"
Technic::SolderPackInstallTask::SolderPackInstallTask( Technic::SolderPackInstallTask::SolderPackInstallTask(
shared_qobject_ptr<QNetworkAccessManager> network, shared_qobject_ptr<QNetworkAccessManager> network,
@ -41,9 +63,11 @@ bool Technic::SolderPackInstallTask::abort() {
void Technic::SolderPackInstallTask::executeTask() void Technic::SolderPackInstallTask::executeTask()
{ {
setStatus(tr("Finding recommended version:\n%1").arg(m_sourceUrl.toString())); setStatus(tr("Finding recommended version"));
m_filesNetJob = new NetJob(tr("Finding recommended version"), m_network); m_filesNetJob = new NetJob(tr("Finding recommended version"), m_network);
m_filesNetJob->addNetAction(Net::Download::makeByteArray(m_sourceUrl, &m_response)); m_filesNetJob->addNetAction(Net::Download::makeByteArray(m_sourceUrl, &m_response));
auto job = m_filesNetJob.get(); auto job = m_filesNetJob.get();
connect(job, &NetJob::succeeded, this, &Technic::SolderPackInstallTask::versionSucceeded); connect(job, &NetJob::succeeded, this, &Technic::SolderPackInstallTask::versionSucceeded);
connect(job, &NetJob::failed, this, &Technic::SolderPackInstallTask::downloadFailed); connect(job, &NetJob::failed, this, &Technic::SolderPackInstallTask::downloadFailed);
@ -52,21 +76,29 @@ void Technic::SolderPackInstallTask::executeTask()
void Technic::SolderPackInstallTask::versionSucceeded() void Technic::SolderPackInstallTask::versionSucceeded()
{ {
try setStatus(tr("Resolving modpack files"));
{
QJsonDocument doc = Json::requireDocument(m_response); QJsonParseError parse_error {};
QJsonObject obj = Json::requireObject(doc); QJsonDocument doc = QJsonDocument::fromJson(m_response, &parse_error);
QString version = Json::requireString(obj, "recommended", "__placeholder__"); if (parse_error.error != QJsonParseError::NoError) {
m_sourceUrl = m_sourceUrl.toString() + '/' + version; qWarning() << "Error while parsing JSON response from Solder at " << parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << m_response;
return;
} }
catch (const JSONValidationError &e) auto obj = doc.object();
{
emitFailed(e.cause()); TechnicSolder::Pack pack;
try {
TechnicSolder::loadPack(pack, obj);
}
catch (const JSONValidationError& e) {
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
m_filesNetJob.reset(); m_filesNetJob.reset();
return; return;
} }
setStatus(tr("Resolving modpack files:\n%1").arg(m_sourceUrl.toString())); m_sourceUrl = m_sourceUrl.toString() + '/' + pack.recommended;
m_filesNetJob = new NetJob(tr("Resolving modpack files"), m_network); m_filesNetJob = new NetJob(tr("Resolving modpack files"), m_network);
m_filesNetJob->addNetAction(Net::Download::makeByteArray(m_sourceUrl, &m_response)); m_filesNetJob->addNetAction(Net::Download::makeByteArray(m_sourceUrl, &m_response));
auto job = m_filesNetJob.get(); auto job = m_filesNetJob.get();
@ -77,38 +109,41 @@ void Technic::SolderPackInstallTask::versionSucceeded()
void Technic::SolderPackInstallTask::fileListSucceeded() void Technic::SolderPackInstallTask::fileListSucceeded()
{ {
setStatus(tr("Downloading modpack:")); setStatus(tr("Downloading modpack"));
QStringList modUrls;
try QJsonParseError parse_error {};
{ QJsonDocument doc = QJsonDocument::fromJson(m_response, &parse_error);
QJsonDocument doc = Json::requireDocument(m_response); if (parse_error.error != QJsonParseError::NoError) {
QJsonObject obj = Json::requireObject(doc); qWarning() << "Error while parsing JSON response from Solder at " << parse_error.offset << " reason: " << parse_error.errorString();
QString minecraftVersion = Json::ensureString(obj, "minecraft", QString(), "__placeholder__"); qWarning() << m_response;
if (!minecraftVersion.isEmpty()) return;
m_minecraftVersion = minecraftVersion;
QJsonArray mods = Json::requireArray(obj, "mods", "'mods'");
for (auto mod: mods)
{
QJsonObject modObject = Json::requireObject(mod);
modUrls.append(Json::requireString(modObject, "url", "'url'"));
}
} }
catch (const JSONValidationError &e) auto obj = doc.object();
{
emitFailed(e.cause()); TechnicSolder::PackBuild build;
try {
TechnicSolder::loadPackBuild(build, obj);
}
catch (const JSONValidationError& e) {
emitFailed(tr("Could not understand pack manifest:\n") + e.cause());
m_filesNetJob.reset(); m_filesNetJob.reset();
return; return;
} }
if (!build.minecraft.isEmpty())
m_minecraftVersion = build.minecraft;
m_filesNetJob = new NetJob(tr("Downloading modpack"), m_network); m_filesNetJob = new NetJob(tr("Downloading modpack"), m_network);
int i = 0; int i = 0;
for (auto &modUrl: modUrls) for (const auto &mod : build.mods)
{ {
auto path = FS::PathCombine(m_outputDir.path(), QString("%1").arg(i)); auto path = FS::PathCombine(m_outputDir.path(), QString("%1").arg(i));
m_filesNetJob->addNetAction(Net::Download::makeFile(modUrl, path)); m_filesNetJob->addNetAction(Net::Download::makeFile(mod.url, path));
i++; i++;
} }
m_modCount = modUrls.size(); m_modCount = build.mods.size();
connect(m_filesNetJob.get(), &NetJob::succeeded, this, &Technic::SolderPackInstallTask::downloadSucceeded); connect(m_filesNetJob.get(), &NetJob::succeeded, this, &Technic::SolderPackInstallTask::downloadSucceeded);
connect(m_filesNetJob.get(), &NetJob::progress, this, &Technic::SolderPackInstallTask::downloadProgressChanged); connect(m_filesNetJob.get(), &NetJob::progress, this, &Technic::SolderPackInstallTask::downloadProgressChanged);
@ -206,6 +241,5 @@ void Technic::SolderPackInstallTask::extractFinished()
void Technic::SolderPackInstallTask::extractAborted() void Technic::SolderPackInstallTask::extractAborted()
{ {
emitFailed(tr("Instance import has been aborted.")); emitFailed(tr("Instance import has been aborted."));
return;
} }