2022-03-21 00:31:08 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2021-12-27 09:22:54 +05:30
|
|
|
* Copyright (c) 2021-2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-03-21 00:31:08 +05:30
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-03-21 00:31:08 +05:30
|
|
|
* 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.
|
2020-06-07 21:16:12 +05:30
|
|
|
*
|
2022-03-21 00:31:08 +05:30
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* 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.
|
2020-06-07 21:16:12 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TechnicPage.h"
|
|
|
|
#include "ui_TechnicPage.h"
|
|
|
|
|
|
|
|
#include <QKeyEvent>
|
2021-11-22 08:25:16 +05:30
|
|
|
|
|
|
|
#include "ui/dialogs/NewInstanceDialog.h"
|
|
|
|
|
2022-01-10 04:32:32 +05:30
|
|
|
#include "BuildConfig.h"
|
2021-11-22 08:25:16 +05:30
|
|
|
#include "TechnicModel.h"
|
2020-06-07 21:16:12 +05:30
|
|
|
#include "modplatform/technic/SingleZipPackInstallTask.h"
|
|
|
|
#include "modplatform/technic/SolderPackInstallTask.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
2021-11-22 08:25:16 +05:30
|
|
|
#include "Application.h"
|
2021-12-27 09:22:54 +05:30
|
|
|
#include "modplatform/technic/SolderPackManifest.h"
|
2021-11-22 08:25:16 +05:30
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
TechnicPage::TechnicPage(NewInstanceDialog* dialog, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::TechnicPage), dialog(dialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &TechnicPage::triggerSearch);
|
|
|
|
ui->searchEdit->installEventFilter(this);
|
|
|
|
model = new Technic::ListModel(this);
|
|
|
|
ui->packView->setModel(model);
|
2021-12-27 09:22:54 +05:30
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TechnicPage::onSelectionChanged);
|
2021-12-27 09:22:54 +05:30
|
|
|
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &TechnicPage::onVersionSelectionChanged);
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool TechnicPage::eventFilter(QObject* watched, QEvent* event)
|
|
|
|
{
|
|
|
|
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
if (keyEvent->key() == Qt::Key_Return) {
|
|
|
|
triggerSearch();
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
TechnicPage::~TechnicPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TechnicPage::shouldDisplay() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-22 23:53:53 +05:30
|
|
|
void TechnicPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
void TechnicPage::openedImpl()
|
|
|
|
{
|
2021-06-19 19:49:39 +05:30
|
|
|
suggestCurrent();
|
|
|
|
triggerSearch();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::triggerSearch() {
|
|
|
|
model->searchWithTerm(ui->searchEdit->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|
|
|
{
|
2021-12-27 09:22:54 +05:30
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
if(!first.isValid())
|
|
|
|
{
|
|
|
|
if(isOpened)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = model->data(first, Qt::UserRole).value<Technic::Modpack>();
|
|
|
|
suggestCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::suggestCurrent()
|
|
|
|
{
|
|
|
|
if (!isOpened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (current.broken)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 19:49:39 +05:30
|
|
|
QString editedLogoName = "technic_" + current.logoName.section(".", 0, 0);
|
2020-06-07 21:16:12 +05:30
|
|
|
model->getLogo(current.logoName, current.logoUrl, [this, editedLogoName](QString logo)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedIconFromFile(logo, editedLogoName);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (current.metadataLoaded)
|
|
|
|
{
|
|
|
|
metadataLoaded();
|
2021-06-19 19:49:39 +05:30
|
|
|
return;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
2021-12-31 09:57:59 +05:30
|
|
|
|
|
|
|
NetJob *netJob = new NetJob(QString("Technic::PackMeta(%1)").arg(current.name), APPLICATION->network());
|
2021-06-19 19:49:39 +05:30
|
|
|
QString slug = current.slug;
|
2022-01-10 04:32:32 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QString("%1modpack/%2?build=%3").arg(BuildConfig.TECHNIC_API_BASE_URL, slug, BuildConfig.TECHNIC_API_BUILD), &response));
|
2021-12-27 09:22:54 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, slug]
|
2020-06-07 21:16:12 +05:30
|
|
|
{
|
2021-12-27 09:22:54 +05:30
|
|
|
jobPtr.reset();
|
|
|
|
|
2021-06-19 19:49:39 +05:30
|
|
|
if (current.slug != slug)
|
2020-06-07 21:16:12 +05:30
|
|
|
{
|
2021-06-19 19:49:39 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-12-27 09:22:54 +05:30
|
|
|
|
|
|
|
QJsonParseError parse_error {};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
|
2021-06-19 19:49:39 +05:30
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
if(parse_error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
qWarning() << "Error while parsing JSON response from Technic at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!obj.contains("url"))
|
|
|
|
{
|
|
|
|
qWarning() << "Json doesn't contain an url key";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonValueRef url = obj["url"];
|
|
|
|
if (url.isString())
|
|
|
|
{
|
|
|
|
current.url = url.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!obj.contains("solder"))
|
2020-06-07 21:16:12 +05:30
|
|
|
{
|
2021-06-19 19:49:39 +05:30
|
|
|
qWarning() << "Json doesn't contain a valid url or solder key";
|
2020-06-07 21:16:12 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-06-19 19:49:39 +05:30
|
|
|
QJsonValueRef solderUrl = obj["solder"];
|
|
|
|
if (solderUrl.isString())
|
2020-06-07 21:16:12 +05:30
|
|
|
{
|
2021-06-19 19:49:39 +05:30
|
|
|
current.url = solderUrl.toString();
|
|
|
|
current.isSolder = true;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-19 19:49:39 +05:30
|
|
|
qWarning() << "Json doesn't contain a valid url or solder key";
|
|
|
|
return;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
2021-06-19 19:49:39 +05:30
|
|
|
}
|
2020-06-07 21:16:12 +05:30
|
|
|
|
2021-06-19 19:49:39 +05:30
|
|
|
current.minecraftVersion = Json::ensureString(obj, "minecraft", QString(), "__placeholder__");
|
|
|
|
current.websiteUrl = Json::ensureString(obj, "platformUrl", QString(), "__placeholder__");
|
|
|
|
current.author = Json::ensureString(obj, "user", QString(), "__placeholder__");
|
|
|
|
current.description = Json::ensureString(obj, "description", QString(), "__placeholder__");
|
2021-12-24 20:40:42 +05:30
|
|
|
current.currentVersion = Json::ensureString(obj, "version", QString(), "__placeholder__");
|
2021-06-19 19:49:39 +05:30
|
|
|
current.metadataLoaded = true;
|
2021-12-27 09:22:54 +05:30
|
|
|
|
2021-06-19 19:49:39 +05:30
|
|
|
metadataLoaded();
|
|
|
|
});
|
2021-12-27 09:22:54 +05:30
|
|
|
|
|
|
|
jobPtr = netJob;
|
|
|
|
jobPtr->start();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// expects current.metadataLoaded to be true
|
|
|
|
void TechnicPage::metadataLoaded()
|
|
|
|
{
|
2020-11-03 02:58:07 +05:30
|
|
|
QString text = "";
|
2020-06-07 21:16:12 +05:30
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.websiteUrl.isEmpty())
|
2021-12-24 20:50:34 +05:30
|
|
|
text = name.toHtmlEscaped();
|
2020-06-07 21:16:12 +05:30
|
|
|
else
|
2021-12-24 20:50:34 +05:30
|
|
|
text = "<a href=\"" + current.websiteUrl.toHtmlEscaped() + "\">" + name.toHtmlEscaped() + "</a>";
|
|
|
|
|
2020-11-03 02:58:07 +05:30
|
|
|
if (!current.author.isEmpty()) {
|
2021-12-27 09:25:08 +05:30
|
|
|
text += "<br>" + tr(" by ") + current.author.toHtmlEscaped();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
|
2021-12-27 09:22:54 +05:30
|
|
|
text += "<br><br>";
|
|
|
|
|
|
|
|
ui->packDescription->setHtml(text + current.description);
|
|
|
|
|
|
|
|
// Strip trailing forward-slashes from Solder URL's
|
|
|
|
if (current.isSolder) {
|
|
|
|
while (current.url.endsWith('/')) current.url.chop(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display versions from Solder
|
|
|
|
if (!current.isSolder) {
|
|
|
|
// If the pack isn't a Solder pack, it only has the single version
|
|
|
|
ui->versionSelectionBox->addItem(current.currentVersion);
|
|
|
|
}
|
|
|
|
else if (current.versionsLoaded) {
|
|
|
|
// reverse foreach, so that the newest versions are first
|
|
|
|
for (auto i = current.versions.size(); i--;) {
|
|
|
|
ui->versionSelectionBox->addItem(current.versions.at(i));
|
|
|
|
}
|
|
|
|
ui->versionSelectionBox->setCurrentText(current.recommended);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// For now, until the versions are pulled from the Solder instance, display the current
|
|
|
|
// version so we can display something quicker
|
|
|
|
ui->versionSelectionBox->addItem(current.currentVersion);
|
|
|
|
|
|
|
|
auto* netJob = new NetJob(QString("Technic::SolderMeta(%1)").arg(current.name), APPLICATION->network());
|
|
|
|
auto url = QString("%1/modpack/%2").arg(current.url, current.slug);
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(url), &response));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &TechnicPage::onSolderLoaded);
|
|
|
|
|
|
|
|
jobPtr = netJob;
|
|
|
|
jobPtr->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
selectVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::selectVersion() {
|
|
|
|
if (!isOpened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (current.broken) {
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:16:12 +05:30
|
|
|
if (!current.isSolder)
|
|
|
|
{
|
2021-12-27 09:22:54 +05:30
|
|
|
dialog->setSuggestedPack(current.name + " " + selectedVersion, new Technic::SingleZipPackInstallTask(current.url, current.minecraftVersion));
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-27 09:22:54 +05:30
|
|
|
dialog->setSuggestedPack(current.name + " " + selectedVersion, new Technic::SolderPackInstallTask(APPLICATION->network(), current.url, current.slug, selectedVersion, current.minecraftVersion));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::onSolderLoaded() {
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
auto fallback = [this]() {
|
|
|
|
current.versionsLoaded = true;
|
|
|
|
|
|
|
|
current.versions.clear();
|
|
|
|
current.versions.append(current.currentVersion);
|
|
|
|
};
|
|
|
|
|
|
|
|
current.versions.clear();
|
|
|
|
|
|
|
|
QJsonParseError parse_error {};
|
|
|
|
auto doc = QJsonDocument::fromJson(response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Solder at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << response;
|
|
|
|
fallback();
|
|
|
|
return;
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|
2021-12-27 09:22:54 +05:30
|
|
|
auto obj = doc.object();
|
|
|
|
|
|
|
|
TechnicSolder::Pack pack;
|
|
|
|
try {
|
|
|
|
TechnicSolder::loadPack(pack, obj);
|
|
|
|
}
|
|
|
|
catch (const JSONValidationError& err) {
|
|
|
|
qCritical() << "Couldn't parse Solder pack metadata:" << err.cause();
|
|
|
|
fallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current.versionsLoaded = true;
|
|
|
|
current.recommended = pack.recommended;
|
|
|
|
current.versions.append(pack.builds);
|
|
|
|
|
|
|
|
// Finally, let's reload :)
|
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
metadataLoaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::onVersionSelectionChanged(QString data) {
|
|
|
|
if (data.isNull() || data.isEmpty()) {
|
|
|
|
selectedVersion = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedVersion = data;
|
|
|
|
selectVersion();
|
2020-06-07 21:16:12 +05:30
|
|
|
}
|