Merge pull request #500 from flowln/net_refactor

Refactor a little the code in `launcher/net/` files
This commit is contained in:
Sefa Eyeoglu 2022-05-21 12:06:34 +02:00 committed by GitHub
commit b4707f46ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 1399 additions and 840 deletions

View File

@ -41,6 +41,7 @@
#include "FileSystem.h" #include "FileSystem.h"
#include "MMCZip.h" #include "MMCZip.h"
#include "NullInstance.h" #include "NullInstance.h"
#include "icons/IconList.h"
#include "icons/IconUtils.h" #include "icons/IconUtils.h"
#include "settings/INISettingsObject.h" #include "settings/INISettingsObject.h"

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -297,7 +317,7 @@ NetAction::Ptr AssetObject::getDownloadAction()
auto rawHash = QByteArray::fromHex(hash.toLatin1()); auto rawHash = QByteArray::fromHex(hash.toLatin1());
objectDL->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash)); objectDL->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash));
} }
objectDL->m_total_progress = size; objectDL->setProgress(objectDL->getProgress(), size);
return objectDL; return objectDL;
} }
return nullptr; return nullptr;

View File

@ -1,62 +1,89 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "Sink.h" #include "Sink.h"
namespace Net { namespace Net {
/* /*
* Sink object for downloads that uses an external QByteArray it doesn't own as a target. * Sink object for downloads that uses an external QByteArray it doesn't own as a target.
* FIXME: It is possible that the QByteArray is freed while we're doing some operation on it,
* causing a segmentation fault.
*/ */
class ByteArraySink : public Sink class ByteArraySink : public Sink {
{ public:
public: ByteArraySink(QByteArray* output) : m_output(output){};
ByteArraySink(QByteArray *output)
:m_output(output)
{
// nil
};
virtual ~ByteArraySink() virtual ~ByteArraySink() = default;
{
// nil
}
public: public:
JobStatus init(QNetworkRequest & request) override auto init(QNetworkRequest& request) -> Task::State override
{ {
m_output->clear(); m_output->clear();
if(initAllValidators(request)) if (initAllValidators(request))
return Job_InProgress; return Task::State::Running;
return Job_Failed; return Task::State::Failed;
}; };
JobStatus write(QByteArray & data) override auto write(QByteArray& data) -> Task::State override
{ {
m_output->append(data); m_output->append(data);
if(writeAllValidators(data)) if (writeAllValidators(data))
return Job_InProgress; return Task::State::Running;
return Job_Failed; return Task::State::Failed;
} }
JobStatus abort() override auto abort() -> Task::State override
{ {
m_output->clear(); m_output->clear();
failAllValidators(); failAllValidators();
return Job_Failed; return Task::State::Failed;
} }
JobStatus finalize(QNetworkReply &reply) override auto finalize(QNetworkReply& reply) -> Task::State override
{ {
if(finalizeAllValidators(reply)) if (finalizeAllValidators(reply))
return Job_Finished; return Task::State::Succeeded;
return Job_Failed; return Task::State::Failed;
} }
bool hasLocalData() override auto hasLocalData() -> bool override { return false; }
{
return false;
}
private: private:
QByteArray * m_output; QByteArray* m_output;
}; };
} } // namespace Net

View File

@ -1,55 +1,82 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "Validator.h" #include "Validator.h"
#include <QCryptographicHash> #include <QCryptographicHash>
#include <memory>
#include <QFile> #include <QFile>
namespace Net { namespace Net {
class ChecksumValidator: public Validator class ChecksumValidator : public Validator {
{ public:
public: /* con/des */
ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray()) ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray())
:m_checksum(algorithm), m_expected(expected) : m_checksum(algorithm), m_expected(expected){};
{ virtual ~ChecksumValidator() = default;
};
virtual ~ChecksumValidator() {};
public: /* methods */ public:
bool init(QNetworkRequest &) override auto init(QNetworkRequest&) -> bool override
{ {
m_checksum.reset(); m_checksum.reset();
return true; return true;
} }
bool write(QByteArray & data) override
auto write(QByteArray& data) -> bool override
{ {
m_checksum.addData(data); m_checksum.addData(data);
return true; return true;
} }
bool abort() override
{ auto abort() -> bool override { return true; }
return true;
} auto validate(QNetworkReply&) -> bool override
bool validate(QNetworkReply &) override
{
if(m_expected.size() && m_expected != hash())
{ {
if (m_expected.size() && m_expected != hash()) {
qWarning() << "Checksum mismatch, download is bad."; qWarning() << "Checksum mismatch, download is bad.";
return false; return false;
} }
return true; return true;
} }
QByteArray hash()
{
return m_checksum.result();
}
void setExpected(QByteArray expected)
{
m_expected = expected;
}
private: /* data */ auto hash() -> QByteArray { return m_checksum.result(); }
void setExpected(QByteArray expected) { m_expected = expected; }
private:
QCryptographicHash m_checksum; QCryptographicHash m_checksum;
QByteArray m_expected; QByteArray m_expected;
}; };
} } // namespace Net

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +36,6 @@
#include "Download.h" #include "Download.h"
#include <QDateTime> #include <QDateTime>
#include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include "ByteArraySink.h" #include "ByteArraySink.h"
@ -31,33 +50,32 @@ namespace Net {
Download::Download() : NetAction() Download::Download() : NetAction()
{ {
m_status = Job_NotStarted; m_state = State::Inactive;
} }
Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
{ {
Download* dl = new Download(); auto* dl = new Download();
dl->m_url = url; dl->m_url = url;
dl->m_options = options; dl->m_options = options;
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5); auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
auto cachedNode = new MetaCacheSink(entry, md5Node); auto cachedNode = new MetaCacheSink(entry, md5Node);
dl->m_sink.reset(cachedNode); dl->m_sink.reset(cachedNode);
dl->m_target_path = entry->getFullPath();
return dl; return dl;
} }
Download::Ptr Download::makeByteArray(QUrl url, QByteArray* output, Options options) auto Download::makeByteArray(QUrl url, QByteArray* output, Options options) -> Download::Ptr
{ {
Download* dl = new Download(); auto* dl = new Download();
dl->m_url = url; dl->m_url = url;
dl->m_options = options; dl->m_options = options;
dl->m_sink.reset(new ByteArraySink(output)); dl->m_sink.reset(new ByteArraySink(output));
return dl; return dl;
} }
Download::Ptr Download::makeFile(QUrl url, QString path, Options options) auto Download::makeFile(QUrl url, QString path, Options options) -> Download::Ptr
{ {
Download* dl = new Download(); auto* dl = new Download();
dl->m_url = url; dl->m_url = url;
dl->m_options = options; dl->m_options = options;
dl->m_sink.reset(new FileSink(path)); dl->m_sink.reset(new FileSink(path));
@ -69,29 +87,32 @@ void Download::addValidator(Validator* v)
m_sink->addValidator(v); m_sink->addValidator(v);
} }
void Download::startImpl() void Download::executeTask()
{ {
if (m_status == Job_Aborted) { setStatus(tr("Downloading %1").arg(m_url.toString()));
if (getState() == Task::State::AbortedByUser) {
qWarning() << "Attempt to start an aborted Download:" << m_url.toString(); qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
emit aborted(m_index_within_job); emitAborted();
return; return;
} }
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
m_status = m_sink->init(request); m_state = m_sink->init(request);
switch (m_status) { switch (m_state) {
case Job_Finished: case State::Succeeded:
emit succeeded(m_index_within_job); emit succeeded();
qDebug() << "Download cache hit " << m_url.toString(); qDebug() << "Download cache hit " << m_url.toString();
return; return;
case Job_InProgress: case State::Running:
qDebug() << "Downloading " << m_url.toString(); qDebug() << "Downloading " << m_url.toString();
break; break;
case Job_Failed_Proceed: // this is meaningless in this context. We do need a sink. case State::Inactive:
case Job_NotStarted: case State::Failed:
case Job_Failed: emitFailed();
emit failed(m_index_within_job);
return; return;
case Job_Aborted: case State::AbortedByUser:
emitAborted();
return; return;
} }
@ -103,8 +124,8 @@ void Download::startImpl()
QNetworkReply* rep = m_network->get(request); QNetworkReply* rep = m_network->get(request);
m_reply.reset(rep); m_reply.reset(rep);
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError))); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors); connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead); connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
@ -112,26 +133,24 @@ void Download::startImpl()
void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
m_total_progress = bytesTotal; setProgress(bytesReceived, bytesTotal);
m_progress = bytesReceived;
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
} }
void Download::downloadError(QNetworkReply::NetworkError error) void Download::downloadError(QNetworkReply::NetworkError error)
{ {
if (error == QNetworkReply::OperationCanceledError) { if (error == QNetworkReply::OperationCanceledError) {
qCritical() << "Aborted " << m_url.toString(); qCritical() << "Aborted " << m_url.toString();
m_status = Job_Aborted; m_state = State::AbortedByUser;
} else { } else {
if (m_options & Option::AcceptLocalFiles) { if (m_options & Option::AcceptLocalFiles) {
if (m_sink->hasLocalData()) { if (m_sink->hasLocalData()) {
m_status = Job_Failed_Proceed; m_state = State::Succeeded;
return; return;
} }
} }
// error happened during download. // error happened during download.
qCritical() << "Failed " << m_url.toString() << " with reason " << error; qCritical() << "Failed " << m_url.toString() << " with reason " << error;
m_status = Job_Failed; m_state = State::Failed;
} }
} }
@ -146,7 +165,7 @@ void Download::sslErrors(const QList<QSslError>& errors)
} }
} }
bool Download::handleRedirect() auto Download::handleRedirect() -> bool
{ {
QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl(); QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
if (!redirect.isValid()) { if (!redirect.isValid()) {
@ -195,7 +214,8 @@ bool Download::handleRedirect()
m_url = QUrl(redirect.toString()); m_url = QUrl(redirect.toString());
qDebug() << "Following redirect to " << m_url.toString(); qDebug() << "Following redirect to " << m_url.toString();
start(m_network); startAction(m_network);
return true; return true;
} }
@ -208,74 +228,71 @@ void Download::downloadFinished()
} }
// if the download failed before this point ... // if the download failed before this point ...
if (m_status == Job_Failed_Proceed) { if (m_state == State::Succeeded) // pretend to succeed so we continue processing :)
{
qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString(); qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} else if (m_status == Job_Failed) { } else if (m_state == State::Failed) {
qDebug() << "Download failed in previous step:" << m_url.toString(); qDebug() << "Download failed in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emit failed("");
return; return;
} else if (m_status == Job_Aborted) { } else if (m_state == State::AbortedByUser) {
qDebug() << "Download aborted in previous step:" << m_url.toString(); qDebug() << "Download aborted in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit aborted(m_index_within_job); emit aborted();
return; return;
} }
// make sure we got all the remaining data, if any // make sure we got all the remaining data, if any
auto data = m_reply->readAll(); auto data = m_reply->readAll();
if (data.size()) { if (data.size()) {
qDebug() << "Writing extra" << data.size() << "bytes to" << m_target_path; qDebug() << "Writing extra" << data.size() << "bytes";
m_status = m_sink->write(data); m_state = m_sink->write(data);
} }
// otherwise, finalize the whole graph // otherwise, finalize the whole graph
m_status = m_sink->finalize(*m_reply.get()); m_state = m_sink->finalize(*m_reply.get());
if (m_status != Job_Finished) { if (m_state != State::Succeeded) {
qDebug() << "Download failed to finalize:" << m_url.toString(); qDebug() << "Download failed to finalize:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emit failed("");
return; return;
} }
m_reply.reset(); m_reply.reset();
qDebug() << "Download succeeded:" << m_url.toString(); qDebug() << "Download succeeded:" << m_url.toString();
emit succeeded(m_index_within_job); emit succeeded();
} }
void Download::downloadReadyRead() void Download::downloadReadyRead()
{ {
if (m_status == Job_InProgress) { if (m_state == State::Running) {
auto data = m_reply->readAll(); auto data = m_reply->readAll();
m_status = m_sink->write(data); m_state = m_sink->write(data);
if (m_status == Job_Failed) { if (m_state == State::Failed) {
qCritical() << "Failed to process response chunk for " << m_target_path; qCritical() << "Failed to process response chunk";
} }
// qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes"; // qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes";
} else { } else {
qCritical() << "Cannot write to " << m_target_path << ", illegal status" << m_status; qCritical() << "Cannot write download data! illegal status " << m_status;
} }
} }
} // namespace Net } // namespace Net
bool Net::Download::abort() auto Net::Download::abort() -> bool
{ {
if (m_reply) { if (m_reply) {
m_reply->abort(); m_reply->abort();
} else { } else {
m_status = Job_Aborted; m_state = State::AbortedByUser;
} }
return true; return true;
} }
bool Net::Download::canAbort()
{
return true;
}

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,63 +35,54 @@
#pragma once #pragma once
#include "NetAction.h"
#include "HttpMetaCache.h" #include "HttpMetaCache.h"
#include "Validator.h" #include "NetAction.h"
#include "Sink.h" #include "Sink.h"
#include "Validator.h"
#include "QObjectPtr.h" #include "QObjectPtr.h"
namespace Net { namespace Net {
class Download : public NetAction class Download : public NetAction {
{
Q_OBJECT Q_OBJECT
public: /* types */ public:
typedef shared_qobject_ptr<class Download> Ptr; using Ptr = shared_qobject_ptr<class Download>;
enum class Option enum class Option { NoOptions = 0, AcceptLocalFiles = 1 };
{
NoOptions = 0,
AcceptLocalFiles = 1
};
Q_DECLARE_FLAGS(Options, Option) Q_DECLARE_FLAGS(Options, Option)
protected: /* con/des */ protected:
explicit Download(); explicit Download();
public:
virtual ~Download(){};
static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry, Options options = Option::NoOptions);
static Download::Ptr makeByteArray(QUrl url, QByteArray *output, Options options = Option::NoOptions);
static Download::Ptr makeFile(QUrl url, QString path, Options options = Option::NoOptions);
public: /* methods */ public:
QString getTargetFilepath() ~Download() override = default;
{
return m_target_path;
}
void addValidator(Validator * v);
bool abort() override;
bool canAbort() override;
private: /* methods */ static auto makeCached(QUrl url, MetaEntryPtr entry, Options options = Option::NoOptions) -> Download::Ptr;
bool handleRedirect(); static auto makeByteArray(QUrl url, QByteArray* output, Options options = Option::NoOptions) -> Download::Ptr;
static auto makeFile(QUrl url, QString path, Options options = Option::NoOptions) -> Download::Ptr;
protected slots: public:
void addValidator(Validator* v);
auto abort() -> bool override;
auto canAbort() const -> bool override { return true; };
private:
auto handleRedirect() -> bool;
protected slots:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override; void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
void downloadError(QNetworkReply::NetworkError error) override; void downloadError(QNetworkReply::NetworkError error) override;
void sslErrors(const QList<QSslError> & errors); void sslErrors(const QList<QSslError>& errors);
void downloadFinished() override; void downloadFinished() override;
void downloadReadyRead() override; void downloadReadyRead() override;
public slots: public slots:
void startImpl() override; void executeTask() override;
private: /* data */ private:
// FIXME: remove this, it has no business being here.
QString m_target_path;
std::unique_ptr<Sink> m_sink; std::unique_ptr<Sink> m_sink;
Options m_options; Options m_options;
}; };
} } // namespace Net
Q_DECLARE_OPERATORS_FOR_FLAGS(Net::Download::Options) Q_DECLARE_OPERATORS_FOR_FLAGS(Net::Download::Options)

View File

@ -1,109 +1,131 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include "FileSink.h" #include "FileSink.h"
#include <QFile>
#include <QFileInfo>
#include "FileSystem.h" #include "FileSystem.h"
namespace Net { namespace Net {
FileSink::FileSink(QString filename) Task::State FileSink::init(QNetworkRequest& request)
:m_filename(filename)
{
// nil
}
FileSink::~FileSink()
{
// nil
}
JobStatus FileSink::init(QNetworkRequest& request)
{ {
auto result = initCache(request); auto result = initCache(request);
if(result != Job_InProgress) if (result != Task::State::Running) {
{
return result; return result;
} }
// create a new save file and open it for writing // create a new save file and open it for writing
if (!FS::ensureFilePathExists(m_filename)) if (!FS::ensureFilePathExists(m_filename)) {
{
qCritical() << "Could not create folder for " + m_filename; qCritical() << "Could not create folder for " + m_filename;
return Job_Failed; return Task::State::Failed;
} }
wroteAnyData = false; wroteAnyData = false;
m_output_file.reset(new QSaveFile(m_filename)); m_output_file.reset(new QSaveFile(m_filename));
if (!m_output_file->open(QIODevice::WriteOnly)) if (!m_output_file->open(QIODevice::WriteOnly)) {
{
qCritical() << "Could not open " + m_filename + " for writing"; qCritical() << "Could not open " + m_filename + " for writing";
return Job_Failed; return Task::State::Failed;
} }
if(initAllValidators(request)) if (initAllValidators(request))
return Job_InProgress; return Task::State::Running;
return Job_Failed; return Task::State::Failed;
} }
JobStatus FileSink::initCache(QNetworkRequest &) Task::State FileSink::write(QByteArray& data)
{ {
return Job_InProgress; if (!writeAllValidators(data) || m_output_file->write(data) != data.size()) {
}
JobStatus FileSink::write(QByteArray& data)
{
if (!writeAllValidators(data) || m_output_file->write(data) != data.size())
{
qCritical() << "Failed writing into " + m_filename; qCritical() << "Failed writing into " + m_filename;
m_output_file->cancelWriting(); m_output_file->cancelWriting();
m_output_file.reset(); m_output_file.reset();
wroteAnyData = false; wroteAnyData = false;
return Job_Failed; return Task::State::Failed;
} }
wroteAnyData = true; wroteAnyData = true;
return Job_InProgress; return Task::State::Running;
} }
JobStatus FileSink::abort() Task::State FileSink::abort()
{ {
m_output_file->cancelWriting(); m_output_file->cancelWriting();
failAllValidators(); failAllValidators();
return Job_Failed; return Task::State::Failed;
} }
JobStatus FileSink::finalize(QNetworkReply& reply) Task::State FileSink::finalize(QNetworkReply& reply)
{ {
bool gotFile = false; bool gotFile = false;
QVariant statusCodeV = reply.attribute(QNetworkRequest::HttpStatusCodeAttribute); QVariant statusCodeV = reply.attribute(QNetworkRequest::HttpStatusCodeAttribute);
bool validStatus = false; bool validStatus = false;
int statusCode = statusCodeV.toInt(&validStatus); int statusCode = statusCodeV.toInt(&validStatus);
if(validStatus) if (validStatus) {
{
// this leaves out 304 Not Modified // this leaves out 304 Not Modified
gotFile = statusCode == 200 || statusCode == 203; gotFile = statusCode == 200 || statusCode == 203;
} }
// if we wrote any data to the save file, we try to commit the data to the real file. // if we wrote any data to the save file, we try to commit the data to the real file.
// if it actually got a proper file, we write it even if it was empty // if it actually got a proper file, we write it even if it was empty
if (gotFile || wroteAnyData) if (gotFile || wroteAnyData) {
{
// ask validators for data consistency // ask validators for data consistency
// we only do this for actual downloads, not 'your data is still the same' cache hits // we only do this for actual downloads, not 'your data is still the same' cache hits
if(!finalizeAllValidators(reply)) if (!finalizeAllValidators(reply))
return Job_Failed; return Task::State::Failed;
// nothing went wrong... // nothing went wrong...
if (!m_output_file->commit()) if (!m_output_file->commit()) {
{
qCritical() << "Failed to commit changes to " << m_filename; qCritical() << "Failed to commit changes to " << m_filename;
m_output_file->cancelWriting(); m_output_file->cancelWriting();
return Job_Failed; return Task::State::Failed;
} }
} }
// then get rid of the save file // then get rid of the save file
m_output_file.reset(); m_output_file.reset();
return finalizeCache(reply); return finalizeCache(reply);
} }
JobStatus FileSink::finalizeCache(QNetworkReply &) Task::State FileSink::initCache(QNetworkRequest&)
{ {
return Job_Finished; return Task::State::Running;
}
Task::State FileSink::finalizeCache(QNetworkReply&)
{
return Task::State::Succeeded;
} }
bool FileSink::hasLocalData() bool FileSink::hasLocalData()
@ -111,4 +133,4 @@ bool FileSink::hasLocalData()
QFileInfo info(m_filename); QFileInfo info(m_filename);
return info.exists() && info.size() != 0; return info.exists() && info.size() != 0;
} }
} } // namespace Net

View File

@ -1,28 +1,65 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "Sink.h"
#include <QSaveFile> #include <QSaveFile>
#include "Sink.h"
namespace Net { namespace Net {
class FileSink : public Sink class FileSink : public Sink {
{ public:
public: /* con/des */ FileSink(QString filename) : m_filename(filename){};
FileSink(QString filename); virtual ~FileSink() = default;
virtual ~FileSink();
public: /* methods */ public:
JobStatus init(QNetworkRequest & request) override; auto init(QNetworkRequest& request) -> Task::State override;
JobStatus write(QByteArray & data) override; auto write(QByteArray& data) -> Task::State override;
JobStatus abort() override; auto abort() -> Task::State override;
JobStatus finalize(QNetworkReply & reply) override; auto finalize(QNetworkReply& reply) -> Task::State override;
bool hasLocalData() override;
protected: /* methods */ auto hasLocalData() -> bool override;
virtual JobStatus initCache(QNetworkRequest &);
virtual JobStatus finalizeCache(QNetworkReply &reply);
protected: /* data */ protected:
virtual auto initCache(QNetworkRequest&) -> Task::State;
virtual auto finalizeCache(QNetworkReply& reply) -> Task::State;
protected:
QString m_filename; QString m_filename;
bool wroteAnyData = false; bool wroteAnyData = false;
std::unique_ptr<QSaveFile> m_output_file; std::unique_ptr<QSaveFile> m_output_file;
}; };
} } // namespace Net

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,29 +35,26 @@
#include "HttpMetaCache.h" #include "HttpMetaCache.h"
#include "FileSystem.h" #include "FileSystem.h"
#include "Json.h"
#include <QFileInfo>
#include <QFile>
#include <QDateTime>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QDebug> #include <QDebug>
#include <QJsonDocument> auto MetaEntry::getFullPath() -> QString
#include <QJsonArray>
#include <QJsonObject>
QString MetaEntry::getFullPath()
{ {
// FIXME: make local? // FIXME: make local?
return FS::PathCombine(basePath, relativePath); return FS::PathCombine(basePath, relativePath);
} }
HttpMetaCache::HttpMetaCache(QString path) : QObject() HttpMetaCache::HttpMetaCache(QString path) : QObject(), m_index_file(path)
{ {
m_index_file = path;
saveBatchingTimer.setSingleShot(true); saveBatchingTimer.setSingleShot(true);
saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer); saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer);
connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow())); connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow()));
} }
@ -47,45 +64,42 @@ HttpMetaCache::~HttpMetaCache()
SaveNow(); SaveNow();
} }
MetaEntryPtr HttpMetaCache::getEntry(QString base, QString resource_path) auto HttpMetaCache::getEntry(QString base, QString resource_path) -> MetaEntryPtr
{ {
// no base. no base path. can't store // no base. no base path. can't store
if (!m_entries.contains(base)) if (!m_entries.contains(base)) {
{
// TODO: log problem // TODO: log problem
return MetaEntryPtr(); return {};
} }
EntryMap &map = m_entries[base];
if (map.entry_list.contains(resource_path)) EntryMap& map = m_entries[base];
{ if (map.entry_list.contains(resource_path)) {
return map.entry_list[resource_path]; return map.entry_list[resource_path];
} }
return MetaEntryPtr();
return {};
} }
MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag) auto HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag) -> MetaEntryPtr
{ {
auto entry = getEntry(base, resource_path); auto entry = getEntry(base, resource_path);
// it's not present? generate a default stale entry // it's not present? generate a default stale entry
if (!entry) if (!entry) {
{
return staleEntry(base, resource_path); return staleEntry(base, resource_path);
} }
auto &selected_base = m_entries[base]; auto& selected_base = m_entries[base];
QString real_path = FS::PathCombine(selected_base.base_path, resource_path); QString real_path = FS::PathCombine(selected_base.base_path, resource_path);
QFileInfo finfo(real_path); QFileInfo finfo(real_path);
// is the file really there? if not -> stale // is the file really there? if not -> stale
if (!finfo.isFile() || !finfo.isReadable()) if (!finfo.isFile() || !finfo.isReadable()) {
{
// if the file doesn't exist, we disown the entry // if the file doesn't exist, we disown the entry
selected_base.entry_list.remove(resource_path); selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path); return staleEntry(base, resource_path);
} }
if (!expected_etag.isEmpty() && expected_etag != entry->etag) if (!expected_etag.isEmpty() && expected_etag != entry->etag) {
{
// if the etag doesn't match expected, we disown the entry // if the etag doesn't match expected, we disown the entry
selected_base.entry_list.remove(resource_path); selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path); return staleEntry(base, resource_path);
@ -93,18 +107,15 @@ MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QS
// if the file changed, check md5sum // if the file changed, check md5sum
qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch(); qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch();
if (file_last_changed != entry->local_changed_timestamp) if (file_last_changed != entry->local_changed_timestamp) {
{
QFile input(real_path); QFile input(real_path);
input.open(QIODevice::ReadOnly); input.open(QIODevice::ReadOnly);
QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5) QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5).toHex().constData();
.toHex() if (entry->md5sum != md5sum) {
.constData();
if (entry->md5sum != md5sum)
{
selected_base.entry_list.remove(resource_path); selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path); return staleEntry(base, resource_path);
} }
// md5sums matched... keep entry and save the new state to file // md5sums matched... keep entry and save the new state to file
entry->local_changed_timestamp = file_last_changed; entry->local_changed_timestamp = file_last_changed;
SaveEventually(); SaveEventually();
@ -115,42 +126,42 @@ MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QS
return entry; return entry;
} }
bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry) auto HttpMetaCache::updateEntry(MetaEntryPtr stale_entry) -> bool
{ {
if (!m_entries.contains(stale_entry->baseId)) if (!m_entries.contains(stale_entry->baseId)) {
{ qCritical() << "Cannot add entry with unknown base: " << stale_entry->baseId.toLocal8Bit();
qCritical() << "Cannot add entry with unknown base: "
<< stale_entry->baseId.toLocal8Bit();
return false; return false;
} }
if (stale_entry->stale)
{ if (stale_entry->stale) {
qCritical() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit(); qCritical() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit();
return false; return false;
} }
m_entries[stale_entry->baseId].entry_list[stale_entry->relativePath] = stale_entry; m_entries[stale_entry->baseId].entry_list[stale_entry->relativePath] = stale_entry;
SaveEventually(); SaveEventually();
return true; return true;
} }
bool HttpMetaCache::evictEntry(MetaEntryPtr entry) auto HttpMetaCache::evictEntry(MetaEntryPtr entry) -> bool
{ {
if(entry) if (!entry)
{ return false;
entry->stale = true; entry->stale = true;
SaveEventually(); SaveEventually();
return true; return true;
}
return false;
} }
MetaEntryPtr HttpMetaCache::staleEntry(QString base, QString resource_path) auto HttpMetaCache::staleEntry(QString base, QString resource_path) -> MetaEntryPtr
{ {
auto foo = new MetaEntry(); auto foo = new MetaEntry();
foo->baseId = base; foo->baseId = base;
foo->basePath = getBasePath(base); foo->basePath = getBasePath(base);
foo->relativePath = resource_path; foo->relativePath = resource_path;
foo->stale = true; foo->stale = true;
return MetaEntryPtr(foo); return MetaEntryPtr(foo);
} }
@ -159,24 +170,25 @@ void HttpMetaCache::addBase(QString base, QString base_root)
// TODO: report error // TODO: report error
if (m_entries.contains(base)) if (m_entries.contains(base))
return; return;
// TODO: check if the base path is valid // TODO: check if the base path is valid
EntryMap foo; EntryMap foo;
foo.base_path = base_root; foo.base_path = base_root;
m_entries[base] = foo; m_entries[base] = foo;
} }
QString HttpMetaCache::getBasePath(QString base) auto HttpMetaCache::getBasePath(QString base) -> QString
{ {
if (m_entries.contains(base)) if (m_entries.contains(base)) {
{
return m_entries[base].base_path; return m_entries[base].base_path;
} }
return QString();
return {};
} }
void HttpMetaCache::Load() void HttpMetaCache::Load()
{ {
if(m_index_file.isNull()) if (m_index_file.isNull())
return; return;
QFile index(m_index_file); QFile index(m_index_file);
@ -184,41 +196,35 @@ void HttpMetaCache::Load()
return; return;
QJsonDocument json = QJsonDocument::fromJson(index.readAll()); QJsonDocument json = QJsonDocument::fromJson(index.readAll());
if (!json.isObject())
return; auto root = Json::requireObject(json, "HttpMetaCache root");
auto root = json.object();
// check file version first // check file version first
auto version_val = root.value("version"); auto version_val = Json::ensureString(root, "version");
if (!version_val.isString()) if (version_val != "1")
return;
if (version_val.toString() != "1")
return; return;
// read the entry array // read the entry array
auto entries_val = root.value("entries"); auto array = Json::ensureArray(root, "entries");
if (!entries_val.isArray()) for (auto element : array) {
return; auto element_obj = Json::ensureObject(element);
QJsonArray array = entries_val.toArray(); auto base = Json::ensureString(element_obj, "base");
for (auto element : array)
{
if (!element.isObject())
return;
auto element_obj = element.toObject();
QString base = element_obj.value("base").toString();
if (!m_entries.contains(base)) if (!m_entries.contains(base))
continue; continue;
auto &entrymap = m_entries[base];
auto& entrymap = m_entries[base];
auto foo = new MetaEntry(); auto foo = new MetaEntry();
foo->baseId = base; foo->baseId = base;
QString path = foo->relativePath = element_obj.value("path").toString(); foo->relativePath = Json::ensureString(element_obj, "path");
foo->md5sum = element_obj.value("md5sum").toString(); foo->md5sum = Json::ensureString(element_obj, "md5sum");
foo->etag = element_obj.value("etag").toString(); foo->etag = Json::ensureString(element_obj, "etag");
foo->local_changed_timestamp = element_obj.value("last_changed_timestamp").toDouble(); foo->local_changed_timestamp = Json::ensureDouble(element_obj, "last_changed_timestamp");
foo->remote_changed_timestamp = foo->remote_changed_timestamp = Json::ensureString(element_obj, "remote_changed_timestamp");
element_obj.value("remote_changed_timestamp").toString();
// presumed innocent until closer examination // presumed innocent until closer examination
foo->stale = false; foo->stale = false;
entrymap.entry_list[path] = MetaEntryPtr(foo);
entrymap.entry_list[foo->relativePath] = MetaEntryPtr(foo);
} }
} }
@ -231,42 +237,36 @@ void HttpMetaCache::SaveEventually()
void HttpMetaCache::SaveNow() void HttpMetaCache::SaveNow()
{ {
if(m_index_file.isNull()) if (m_index_file.isNull())
return; return;
QJsonObject toplevel; QJsonObject toplevel;
toplevel.insert("version", QJsonValue(QString("1"))); Json::writeString(toplevel, "version", "1");
QJsonArray entriesArr; QJsonArray entriesArr;
for (auto group : m_entries) for (auto group : m_entries) {
{ for (auto entry : group.entry_list) {
for (auto entry : group.entry_list)
{
// do not save stale entries. they are dead. // do not save stale entries. they are dead.
if(entry->stale) if (entry->stale) {
{
continue; continue;
} }
QJsonObject entryObj; QJsonObject entryObj;
entryObj.insert("base", QJsonValue(entry->baseId)); Json::writeString(entryObj, "base", entry->baseId);
entryObj.insert("path", QJsonValue(entry->relativePath)); Json::writeString(entryObj, "path", entry->relativePath);
entryObj.insert("md5sum", QJsonValue(entry->md5sum)); Json::writeString(entryObj, "md5sum", entry->md5sum);
entryObj.insert("etag", QJsonValue(entry->etag)); Json::writeString(entryObj, "etag", entry->etag);
entryObj.insert("last_changed_timestamp", entryObj.insert("last_changed_timestamp", QJsonValue(double(entry->local_changed_timestamp)));
QJsonValue(double(entry->local_changed_timestamp)));
if (!entry->remote_changed_timestamp.isEmpty()) if (!entry->remote_changed_timestamp.isEmpty())
entryObj.insert("remote_changed_timestamp", entryObj.insert("remote_changed_timestamp", QJsonValue(entry->remote_changed_timestamp));
QJsonValue(entry->remote_changed_timestamp));
entriesArr.append(entryObj); entriesArr.append(entryObj);
} }
} }
toplevel.insert("entries", entriesArr); toplevel.insert("entries", entriesArr);
QJsonDocument doc(toplevel); try {
try Json::write(toplevel, m_index_file);
{ } catch (const Exception& e) {
FS::write(m_index_file, doc.toJson());
}
catch (const Exception &e)
{
qWarning() << e.what(); qWarning() << e.what();
} }
} }

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,57 +34,37 @@
*/ */
#pragma once #pragma once
#include <QString>
#include <QMap> #include <QMap>
#include <qtimer.h> #include <QString>
#include <QTimer>
#include <memory> #include <memory>
class HttpMetaCache; class HttpMetaCache;
class MetaEntry class MetaEntry {
{ friend class HttpMetaCache;
friend class HttpMetaCache;
protected: protected:
MetaEntry() {} MetaEntry() = default;
public:
bool isStale() public:
{ auto isStale() -> bool { return stale; }
return stale; void setStale(bool stale) { this->stale = stale; }
}
void setStale(bool stale) auto getFullPath() -> QString;
{
this->stale = stale; auto getRemoteChangedTimestamp() -> QString { return remote_changed_timestamp; }
} void setRemoteChangedTimestamp(QString remote_changed_timestamp) { this->remote_changed_timestamp = remote_changed_timestamp; }
QString getFullPath(); void setLocalChangedTimestamp(qint64 timestamp) { local_changed_timestamp = timestamp; }
QString getRemoteChangedTimestamp()
{ auto getETag() -> QString { return etag; }
return remote_changed_timestamp; void setETag(QString etag) { this->etag = etag; }
}
void setRemoteChangedTimestamp(QString remote_changed_timestamp) auto getMD5Sum() -> QString { return md5sum; }
{ void setMD5Sum(QString md5sum) { this->md5sum = md5sum; }
this->remote_changed_timestamp = remote_changed_timestamp;
} protected:
void setLocalChangedTimestamp(qint64 timestamp)
{
local_changed_timestamp = timestamp;
}
QString getETag()
{
return etag;
}
void setETag(QString etag)
{
this->etag = etag;
}
QString getMD5Sum()
{
return md5sum;
}
void setMD5Sum(QString md5sum)
{
this->md5sum = md5sum;
}
protected:
QString baseId; QString baseId;
QString basePath; QString basePath;
QString relativePath; QString relativePath;
@ -75,48 +75,48 @@ protected:
bool stale = true; bool stale = true;
}; };
typedef std::shared_ptr<MetaEntry> MetaEntryPtr; using MetaEntryPtr = std::shared_ptr<MetaEntry>;
class HttpMetaCache : public QObject class HttpMetaCache : public QObject {
{
Q_OBJECT Q_OBJECT
public: public:
// supply path to the cache index file // supply path to the cache index file
HttpMetaCache(QString path = QString()); HttpMetaCache(QString path = QString());
~HttpMetaCache(); ~HttpMetaCache() override;
// get the entry solely from the cache // get the entry solely from the cache
// you probably don't want this, unless you have some specific caching needs. // you probably don't want this, unless you have some specific caching needs.
MetaEntryPtr getEntry(QString base, QString resource_path); auto getEntry(QString base, QString resource_path) -> MetaEntryPtr;
// get the entry from cache and verify that it isn't stale (within reason) // get the entry from cache and verify that it isn't stale (within reason)
MetaEntryPtr resolveEntry(QString base, QString resource_path, auto resolveEntry(QString base, QString resource_path, QString expected_etag = QString()) -> MetaEntryPtr;
QString expected_etag = QString());
// add a previously resolved stale entry // add a previously resolved stale entry
bool updateEntry(MetaEntryPtr stale_entry); auto updateEntry(MetaEntryPtr stale_entry) -> bool;
// evict selected entry from cache // evict selected entry from cache
bool evictEntry(MetaEntryPtr entry); auto evictEntry(MetaEntryPtr entry) -> bool;
void addBase(QString base, QString base_root); void addBase(QString base, QString base_root);
// (re)start a timer that calls SaveNow later. // (re)start a timer that calls SaveNow later.
void SaveEventually(); void SaveEventually();
void Load(); void Load();
QString getBasePath(QString base);
public auto getBasePath(QString base) -> QString;
slots:
public slots:
void SaveNow(); void SaveNow();
private: private:
// create a new stale entry, given the parameters // create a new stale entry, given the parameters
MetaEntryPtr staleEntry(QString base, QString resource_path); auto staleEntry(QString base, QString resource_path) -> MetaEntryPtr;
struct EntryMap
{ struct EntryMap {
QString base_path; QString base_path;
QMap<QString, MetaEntryPtr> entry_list; QMap<QString, MetaEntryPtr> entry_list;
}; };
QMap<QString, EntryMap> m_entries; QMap<QString, EntryMap> m_entries;
QString m_index_file; QString m_index_file;
QTimer saveBatchingTimer; QTimer saveBatchingTimer;

View File

@ -1,3 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include "MetaCacheSink.h" #include "MetaCacheSink.h"
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
@ -12,17 +47,13 @@ MetaCacheSink::MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum)
addValidator(md5sum); addValidator(md5sum);
} }
MetaCacheSink::~MetaCacheSink() Task::State MetaCacheSink::initCache(QNetworkRequest& request)
{
// nil
}
JobStatus MetaCacheSink::initCache(QNetworkRequest& request)
{ {
if (!m_entry->isStale()) if (!m_entry->isStale())
{ {
return Job_Finished; return Task::State::Succeeded;
} }
// check if file exists, if it does, use its information for the request // check if file exists, if it does, use its information for the request
QFile current(m_filename); QFile current(m_filename);
if(current.exists() && current.size() != 0) if(current.exists() && current.size() != 0)
@ -36,25 +67,31 @@ JobStatus MetaCacheSink::initCache(QNetworkRequest& request)
request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1()); request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1());
} }
} }
return Job_InProgress;
return Task::State::Running;
} }
JobStatus MetaCacheSink::finalizeCache(QNetworkReply & reply) Task::State MetaCacheSink::finalizeCache(QNetworkReply & reply)
{ {
QFileInfo output_file_info(m_filename); QFileInfo output_file_info(m_filename);
if(wroteAnyData) if(wroteAnyData)
{ {
m_entry->setMD5Sum(m_md5Node->hash().toHex().constData()); m_entry->setMD5Sum(m_md5Node->hash().toHex().constData());
} }
m_entry->setETag(reply.rawHeader("ETag").constData()); m_entry->setETag(reply.rawHeader("ETag").constData());
if (reply.hasRawHeader("Last-Modified")) if (reply.hasRawHeader("Last-Modified"))
{ {
m_entry->setRemoteChangedTimestamp(reply.rawHeader("Last-Modified").constData()); m_entry->setRemoteChangedTimestamp(reply.rawHeader("Last-Modified").constData());
} }
m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch()); m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch());
m_entry->setStale(false); m_entry->setStale(false);
APPLICATION->metacache()->updateEntry(m_entry); APPLICATION->metacache()->updateEntry(m_entry);
return Job_Finished;
return Task::State::Succeeded;
} }
bool MetaCacheSink::hasLocalData() bool MetaCacheSink::hasLocalData()

View File

@ -1,22 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "FileSink.h"
#include "ChecksumValidator.h" #include "ChecksumValidator.h"
#include "FileSink.h"
#include "net/HttpMetaCache.h" #include "net/HttpMetaCache.h"
namespace Net { namespace Net {
class MetaCacheSink : public FileSink class MetaCacheSink : public FileSink {
{ public:
public: /* con/des */ MetaCacheSink(MetaEntryPtr entry, ChecksumValidator* md5sum);
MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum); virtual ~MetaCacheSink() = default;
virtual ~MetaCacheSink();
bool hasLocalData() override;
protected: /* methods */ auto hasLocalData() -> bool override;
JobStatus initCache(QNetworkRequest & request) override;
JobStatus finalizeCache(QNetworkReply & reply) override;
private: /* data */ protected:
auto initCache(QNetworkRequest& request) -> Task::State override;
auto finalizeCache(QNetworkReply& reply) -> Task::State override;
private:
MetaEntryPtr m_entry; MetaEntryPtr m_entry;
ChecksumValidator * m_md5Node; ChecksumValidator* m_md5Node;
}; };
} } // namespace Net

View File

@ -1,10 +1,5 @@
#pragma once #pragma once
namespace Net namespace Net {
{ enum class Mode { Offline, Online };
enum class Mode
{
Offline,
Online
};
} }

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,94 +35,42 @@
#pragma once #pragma once
#include <QObject>
#include <QUrl>
#include <memory>
#include <QNetworkReply> #include <QNetworkReply>
#include <QObjectPtr.h> #include <QUrl>
enum JobStatus #include "QObjectPtr.h"
{ #include "tasks/Task.h"
Job_NotStarted,
Job_InProgress,
Job_Finished,
Job_Failed,
Job_Aborted,
/*
* FIXME: @NUKE this confuses the task failing with us having a fallback in the form of local data. Clear up the confusion.
* Same could be true for aborted task - the presence of pre-existing result is a separate concern
*/
Job_Failed_Proceed
};
class NetAction : public QObject class NetAction : public Task {
{
Q_OBJECT Q_OBJECT
protected: protected:
explicit NetAction() : QObject(nullptr) {}; explicit NetAction() : Task() {};
public: public:
using Ptr = shared_qobject_ptr<NetAction>; using Ptr = shared_qobject_ptr<NetAction>;
virtual ~NetAction() {}; virtual ~NetAction() = default;
bool isRunning() const QUrl url() { return m_url; }
{ auto index() -> int { return m_index_within_job; }
return m_status == Job_InProgress;
}
bool isFinished() const
{
return m_status >= Job_Finished;
}
bool wasSuccessful() const
{
return m_status == Job_Finished || m_status == Job_Failed_Proceed;
}
qint64 totalProgress() const protected slots:
{
return m_total_progress;
}
qint64 currentProgress() const
{
return m_progress;
}
virtual bool abort()
{
return false;
}
virtual bool canAbort()
{
return false;
}
QUrl url()
{
return m_url;
}
signals:
void started(int index);
void netActionProgress(int index, qint64 current, qint64 total);
void succeeded(int index);
void failed(int index);
void aborted(int index);
protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0; virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
virtual void downloadError(QNetworkReply::NetworkError error) = 0; virtual void downloadError(QNetworkReply::NetworkError error) = 0;
virtual void downloadFinished() = 0; virtual void downloadFinished() = 0;
virtual void downloadReadyRead() = 0; virtual void downloadReadyRead() = 0;
public slots: public slots:
void start(shared_qobject_ptr<QNetworkAccessManager> network) { void startAction(shared_qobject_ptr<QNetworkAccessManager> network)
{
m_network = network; m_network = network;
startImpl(); executeTask();
} }
protected: protected:
virtual void startImpl() = 0; void executeTask() override {};
public: public:
shared_qobject_ptr<QNetworkAccessManager> m_network; shared_qobject_ptr<QNetworkAccessManager> m_network;
/// index within the parent job, FIXME: nuke /// index within the parent job, FIXME: nuke
@ -113,10 +81,4 @@ public:
/// source URL /// source URL
QUrl m_url; QUrl m_url;
qint64 m_progress = 0;
qint64 m_total_progress = 1;
protected:
JobStatus m_status = Job_NotStarted;
}; };

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,64 +36,138 @@
#include "NetJob.h" #include "NetJob.h"
#include "Download.h" #include "Download.h"
#include <QDebug> auto NetJob::addNetAction(NetAction::Ptr action) -> bool
{
action->m_index_within_job = m_downloads.size();
m_downloads.append(action);
part_info pi;
m_parts_progress.append(pi);
partProgress(m_parts_progress.count() - 1, action->getProgress(), action->getTotalProgress());
if (action->isRunning()) {
connect(action.get(), &NetAction::succeeded, [this, action]{ partSucceeded(action->index()); });
connect(action.get(), &NetAction::failed, [this, action](QString){ partFailed(action->index()); });
connect(action.get(), &NetAction::aborted, [this, action](){ partAborted(action->index()); });
connect(action.get(), &NetAction::progress, [this, action](qint64 done, qint64 total) { partProgress(action->index(), done, total); });
connect(action.get(), &NetAction::status, this, &NetJob::status);
} else {
m_todo.append(m_parts_progress.size() - 1);
}
return true;
}
auto NetJob::canAbort() const -> bool
{
bool canFullyAbort = true;
// can abort the downloads on the queue?
for (auto index : m_todo) {
auto part = m_downloads[index];
canFullyAbort &= part->canAbort();
}
// can abort the active downloads?
for (auto index : m_doing) {
auto part = m_downloads[index];
canFullyAbort &= part->canAbort();
}
return canFullyAbort;
}
void NetJob::executeTask()
{
// hack that delays early failures so they can be caught easier
QMetaObject::invokeMethod(this, "startMoreParts", Qt::QueuedConnection);
}
auto NetJob::getFailedFiles() -> QStringList
{
QStringList failed;
for (auto index : m_failed) {
failed.push_back(m_downloads[index]->url().toString());
}
failed.sort();
return failed;
}
auto NetJob::abort() -> bool
{
bool fullyAborted = true;
// fail all downloads on the queue
m_failed.unite(m_todo.toSet());
m_todo.clear();
// abort active downloads
auto toKill = m_doing.toList();
for (auto index : toKill) {
auto part = m_downloads[index];
fullyAborted &= part->abort();
}
return fullyAborted;
}
void NetJob::partSucceeded(int index) void NetJob::partSucceeded(int index)
{ {
// do progress. all slots are 1 in size at least // do progress. all slots are 1 in size at least
auto &slot = parts_progress[index]; auto& slot = m_parts_progress[index];
partProgress(index, slot.total_progress, slot.total_progress); partProgress(index, slot.total_progress, slot.total_progress);
m_doing.remove(index); m_doing.remove(index);
m_done.insert(index); m_done.insert(index);
downloads[index].get()->disconnect(this); m_downloads[index].get()->disconnect(this);
startMoreParts(); startMoreParts();
} }
void NetJob::partFailed(int index) void NetJob::partFailed(int index)
{ {
m_doing.remove(index); m_doing.remove(index);
auto &slot = parts_progress[index];
if (slot.failures == 3) auto& slot = m_parts_progress[index];
{ // Can try 3 times before failing by definitive
if (slot.failures == 3) {
m_failed.insert(index); m_failed.insert(index);
} } else {
else
{
slot.failures++; slot.failures++;
m_todo.enqueue(index); m_todo.enqueue(index);
} }
downloads[index].get()->disconnect(this);
m_downloads[index].get()->disconnect(this);
startMoreParts(); startMoreParts();
} }
void NetJob::partAborted(int index) void NetJob::partAborted(int index)
{ {
m_aborted = true; m_aborted = true;
m_doing.remove(index); m_doing.remove(index);
m_failed.insert(index); m_failed.insert(index);
downloads[index].get()->disconnect(this); m_downloads[index].get()->disconnect(this);
startMoreParts(); startMoreParts();
} }
void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal) void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal)
{ {
auto &slot = parts_progress[index]; auto& slot = m_parts_progress[index];
slot.current_progress = bytesReceived; slot.current_progress = bytesReceived;
slot.total_progress = bytesTotal; slot.total_progress = bytesTotal;
int done = m_done.size(); int done = m_done.size();
int doing = m_doing.size(); int doing = m_doing.size();
int all = parts_progress.size(); int all = m_parts_progress.size();
qint64 bytesAll = 0; qint64 bytesAll = 0;
qint64 bytesTotalAll = 0; qint64 bytesTotalAll = 0;
for(auto & partIdx: m_doing) for (auto& partIdx : m_doing) {
{ auto part = m_parts_progress[partIdx];
auto part = parts_progress[partIdx];
// do not count parts with unknown/nonsensical total size // do not count parts with unknown/nonsensical total size
if(part.total_progress <= 0) if (part.total_progress <= 0) {
{
continue; continue;
} }
bytesAll += part.current_progress; bytesAll += part.current_progress;
@ -85,134 +179,54 @@ void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal)
auto current_total = all * 1000; auto current_total = all * 1000;
// HACK: make sure it never jumps backwards. // HACK: make sure it never jumps backwards.
// FAIL: This breaks if the size is not known (or is it something else?) and jumps to 1000, so if it is 1000 reset it to inprogress // FAIL: This breaks if the size is not known (or is it something else?) and jumps to 1000, so if it is 1000 reset it to inprogress
if(m_current_progress == 1000) { if (m_current_progress == 1000) {
m_current_progress = inprogress; m_current_progress = inprogress;
} }
if(m_current_progress > current) if (m_current_progress > current) {
{
current = m_current_progress; current = m_current_progress;
} }
m_current_progress = current; m_current_progress = current;
setProgress(current, current_total); setProgress(current, current_total);
} }
void NetJob::executeTask()
{
// hack that delays early failures so they can be caught easier
QMetaObject::invokeMethod(this, "startMoreParts", Qt::QueuedConnection);
}
void NetJob::startMoreParts() void NetJob::startMoreParts()
{ {
if(!isRunning()) if (!isRunning()) {
{ // this actually makes sense. You can put running m_downloads into a NetJob and then not start it until much later.
// this actually makes sense. You can put running downloads into a NetJob and then not start it until much later.
return; return;
} }
// OK. We are actively processing tasks, proceed. // OK. We are actively processing tasks, proceed.
// Check for final conditions if there's nothing in the queue. // Check for final conditions if there's nothing in the queue.
if(!m_todo.size()) if (!m_todo.size()) {
{ if (!m_doing.size()) {
if(!m_doing.size()) if (!m_failed.size()) {
{
if(!m_failed.size())
{
emitSucceeded(); emitSucceeded();
} } else if (m_aborted) {
else if(m_aborted)
{
emitAborted(); emitAborted();
} } else {
else
{
emitFailed(tr("Job '%1' failed to process:\n%2").arg(objectName()).arg(getFailedFiles().join("\n"))); emitFailed(tr("Job '%1' failed to process:\n%2").arg(objectName()).arg(getFailedFiles().join("\n")));
} }
} }
return; return;
} }
// There's work to do, try to start more parts.
while (m_doing.size() < 6) // There's work to do, try to start more parts, to a maximum of 6 concurrent ones.
{ while (m_doing.size() < 6) {
if(!m_todo.size()) if (m_todo.size() == 0)
return; return;
int doThis = m_todo.dequeue(); int doThis = m_todo.dequeue();
m_doing.insert(doThis); m_doing.insert(doThis);
auto part = downloads[doThis];
auto part = m_downloads[doThis];
// connect signals :D // connect signals :D
connect(part.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int))); connect(part.get(), &NetAction::succeeded, this, [this, part]{ partSucceeded(part->index()); });
connect(part.get(), SIGNAL(failed(int)), SLOT(partFailed(int))); connect(part.get(), &NetAction::failed, this, [this, part](QString){ partFailed(part->index()); });
connect(part.get(), SIGNAL(aborted(int)), SLOT(partAborted(int))); connect(part.get(), &NetAction::aborted, this, [this, part]{ partAborted(part->index()); });
connect(part.get(), SIGNAL(netActionProgress(int, qint64, qint64)), connect(part.get(), &NetAction::progress, this, [this, part](qint64 done, qint64 total) { partProgress(part->index(), done, total); });
SLOT(partProgress(int, qint64, qint64))); connect(part.get(), &NetAction::status, this, &NetJob::status);
part->start(m_network);
part->startAction(m_network);
} }
} }
QStringList NetJob::getFailedFiles()
{
QStringList failed;
for (auto index: m_failed)
{
failed.push_back(downloads[index]->url().toString());
}
failed.sort();
return failed;
}
bool NetJob::canAbort() const
{
bool canFullyAbort = true;
// can abort the waiting?
for(auto index: m_todo)
{
auto part = downloads[index];
canFullyAbort &= part->canAbort();
}
// can abort the active?
for(auto index: m_doing)
{
auto part = downloads[index];
canFullyAbort &= part->canAbort();
}
return canFullyAbort;
}
bool NetJob::abort()
{
bool fullyAborted = true;
// fail all waiting
m_failed.unite(m_todo.toSet());
m_todo.clear();
// abort active
auto toKill = m_doing.toList();
for(auto index: toKill)
{
auto part = downloads[index];
fullyAborted &= part->abort();
}
return fullyAborted;
}
bool NetJob::addNetAction(NetAction::Ptr action)
{
action->m_index_within_job = downloads.size();
downloads.append(action);
part_info pi;
parts_progress.append(pi);
partProgress(parts_progress.count() - 1, action->currentProgress(), action->totalProgress());
if(action->isRunning())
{
connect(action.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
connect(action.get(), SIGNAL(failed(int)), SLOT(partFailed(int)));
connect(action.get(), SIGNAL(netActionProgress(int, qint64, qint64)), SLOT(partProgress(int, qint64, qint64)));
}
else
{
m_todo.append(parts_progress.size() - 1);
}
return true;
}
NetJob::~NetJob() = default;

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,75 +34,65 @@
*/ */
#pragma once #pragma once
#include <QtNetwork> #include <QtNetwork>
#include <QObject>
#include "NetAction.h" #include "NetAction.h"
#include "Download.h"
#include "HttpMetaCache.h"
#include "tasks/Task.h" #include "tasks/Task.h"
#include "QObjectPtr.h"
class NetJob; // Those are included so that they are also included by anyone using NetJob
#include "net/Download.h"
#include "net/HttpMetaCache.h"
class NetJob : public Task class NetJob : public Task {
{
Q_OBJECT Q_OBJECT
public:
public:
using Ptr = shared_qobject_ptr<NetJob>; using Ptr = shared_qobject_ptr<NetJob>;
explicit NetJob(QString job_name, shared_qobject_ptr<QNetworkAccessManager> network) : Task(), m_network(network) explicit NetJob(QString job_name, shared_qobject_ptr<QNetworkAccessManager> network) : Task(), m_network(network)
{ {
setObjectName(job_name); setObjectName(job_name);
} }
virtual ~NetJob(); virtual ~NetJob() = default;
bool addNetAction(NetAction::Ptr action); void executeTask() override;
NetAction::Ptr operator[](int index) auto canAbort() const -> bool override;
{
return downloads[index];
}
const NetAction::Ptr at(const int index)
{
return downloads.at(index);
}
NetAction::Ptr first()
{
if (downloads.size())
return downloads[0];
return NetAction::Ptr();
}
int size() const
{
return downloads.size();
}
QStringList getFailedFiles();
bool canAbort() const override; auto addNetAction(NetAction::Ptr action) -> bool;
private slots: auto operator[](int index) -> NetAction::Ptr { return m_downloads[index]; }
auto at(int index) -> const NetAction::Ptr { return m_downloads.at(index); }
auto size() const -> int { return m_downloads.size(); }
auto first() -> NetAction::Ptr { return m_downloads.size() != 0 ? m_downloads[0] : NetAction::Ptr{}; }
auto getFailedFiles() -> QStringList;
public slots:
// Qt can't handle auto at the start for some reason?
bool abort() override;
private slots:
void startMoreParts(); void startMoreParts();
public slots:
virtual void executeTask() override;
virtual bool abort() override;
private slots:
void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal); void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal);
void partSucceeded(int index); void partSucceeded(int index);
void partFailed(int index); void partFailed(int index);
void partAborted(int index); void partAborted(int index);
private: private:
shared_qobject_ptr<QNetworkAccessManager> m_network; shared_qobject_ptr<QNetworkAccessManager> m_network;
struct part_info struct part_info {
{
qint64 current_progress = 0; qint64 current_progress = 0;
qint64 total_progress = 1; qint64 total_progress = 1;
int failures = 0; int failures = 0;
}; };
QList<NetAction::Ptr> downloads;
QList<part_info> parts_progress; QList<NetAction::Ptr> m_downloads;
QList<part_info> m_parts_progress;
QQueue<int> m_todo; QQueue<int> m_todo;
QSet<int> m_doing; QSet<int> m_doing;
QSet<int> m_done; QSet<int> m_done;

View File

@ -1,3 +1,37 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
*
* 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.
*
* 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.
*
* 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.
*/
#include "PasteUpload.h" #include "PasteUpload.h"
#include "BuildConfig.h" #include "BuildConfig.h"
#include "Application.h" #include "Application.h"

View File

@ -1,4 +1,39 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "tasks/Task.h" #include "tasks/Task.h"
#include <QNetworkReply> #include <QNetworkReply>
#include <QBuffer> #include <QBuffer>

View File

@ -1,3 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "net/NetAction.h" #include "net/NetAction.h"
@ -5,33 +40,39 @@
#include "Validator.h" #include "Validator.h"
namespace Net { namespace Net {
class Sink class Sink {
{ public:
public: /* con/des */ Sink() = default;
Sink() {}; virtual ~Sink() = default;
virtual ~Sink() {};
public: /* methods */ public:
virtual JobStatus init(QNetworkRequest & request) = 0; virtual auto init(QNetworkRequest& request) -> Task::State = 0;
virtual JobStatus write(QByteArray & data) = 0; virtual auto write(QByteArray& data) -> Task::State = 0;
virtual JobStatus abort() = 0; virtual auto abort() -> Task::State = 0;
virtual JobStatus finalize(QNetworkReply & reply) = 0; virtual auto finalize(QNetworkReply& reply) -> Task::State = 0;
virtual bool hasLocalData() = 0;
void addValidator(Validator * validator) virtual auto hasLocalData() -> bool = 0;
{
if(validator) void addValidator(Validator* validator)
{ {
if (validator) {
validators.push_back(std::shared_ptr<Validator>(validator)); validators.push_back(std::shared_ptr<Validator>(validator));
} }
} }
protected: /* methods */ protected:
bool finalizeAllValidators(QNetworkReply & reply) bool initAllValidators(QNetworkRequest& request)
{ {
for(auto & validator: validators) for (auto& validator : validators) {
if (!validator->init(request))
return false;
}
return true;
}
bool finalizeAllValidators(QNetworkReply& reply)
{ {
if(!validator->validate(reply)) for (auto& validator : validators) {
if (!validator->validate(reply))
return false; return false;
} }
return true; return true;
@ -39,32 +80,21 @@ protected: /* methods */
bool failAllValidators() bool failAllValidators()
{ {
bool success = true; bool success = true;
for(auto & validator: validators) for (auto& validator : validators) {
{
success &= validator->abort(); success &= validator->abort();
} }
return success; return success;
} }
bool initAllValidators(QNetworkRequest & request) bool writeAllValidators(QByteArray& data)
{ {
for(auto & validator: validators) for (auto& validator : validators) {
{ if (!validator->write(data))
if(!validator->init(request))
return false;
}
return true;
}
bool writeAllValidators(QByteArray & data)
{
for(auto & validator: validators)
{
if(!validator->write(data))
return false; return false;
} }
return true; return true;
} }
protected: /* data */ protected:
std::vector<std::shared_ptr<Validator>> validators; std::vector<std::shared_ptr<Validator>> validators;
}; };
} } // namespace Net

View File

@ -1,3 +1,37 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "net/NetAction.h" #include "net/NetAction.h"

View File

@ -1,3 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include "ImgurAlbumCreation.h" #include "ImgurAlbumCreation.h"
#include <QNetworkRequest> #include <QNetworkRequest>
@ -13,12 +48,12 @@
ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots) : NetAction(), m_screenshots(screenshots) ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots) : NetAction(), m_screenshots(screenshots)
{ {
m_url = BuildConfig.IMGUR_BASE_URL + "album.json"; m_url = BuildConfig.IMGUR_BASE_URL + "album.json";
m_status = Job_NotStarted; m_state = State::Inactive;
} }
void ImgurAlbumCreation::startImpl() void ImgurAlbumCreation::executeTask()
{ {
m_status = Job_InProgress; m_state = State::Running;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
@ -43,11 +78,11 @@ void ImgurAlbumCreation::startImpl()
void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error) void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error)
{ {
qDebug() << m_reply->errorString(); qDebug() << m_reply->errorString();
m_status = Job_Failed; m_state = State::Failed;
} }
void ImgurAlbumCreation::downloadFinished() void ImgurAlbumCreation::downloadFinished()
{ {
if (m_status != Job_Failed) if (m_state != State::Failed)
{ {
QByteArray data = m_reply->readAll(); QByteArray data = m_reply->readAll();
m_reply.reset(); m_reply.reset();
@ -56,33 +91,32 @@ void ImgurAlbumCreation::downloadFinished()
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {
qDebug() << jsonError.errorString(); qDebug() << jsonError.errorString();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
auto object = doc.object(); auto object = doc.object();
if (!object.value("success").toBool()) if (!object.value("success").toBool())
{ {
qDebug() << doc.toJson(); qDebug() << doc.toJson();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
m_deleteHash = object.value("data").toObject().value("deletehash").toString(); m_deleteHash = object.value("data").toObject().value("deletehash").toString();
m_id = object.value("data").toObject().value("id").toString(); m_id = object.value("data").toObject().value("id").toString();
m_status = Job_Finished; m_state = State::Succeeded;
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} }
else else
{ {
qDebug() << m_reply->readAll(); qDebug() << m_reply->readAll();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
} }
void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
m_total_progress = bytesTotal; setProgress(bytesReceived, bytesTotal);
m_progress = bytesReceived; emit progress(bytesReceived, bytesTotal);
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
} }

View File

@ -1,7 +1,42 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "net/NetAction.h" #include "net/NetAction.h"
#include "Screenshot.h" #include "Screenshot.h"
#include "QObjectPtr.h"
typedef shared_qobject_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr; typedef shared_qobject_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr;
class ImgurAlbumCreation : public NetAction class ImgurAlbumCreation : public NetAction
@ -24,16 +59,14 @@ public:
protected protected
slots: slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
virtual void downloadError(QNetworkReply::NetworkError error); void downloadError(QNetworkReply::NetworkError error) override;
virtual void downloadFinished(); void downloadFinished() override;
virtual void downloadReadyRead() void downloadReadyRead() override {}
{
}
public public
slots: slots:
virtual void startImpl(); void executeTask() override;
private: private:
QList<ScreenShot::Ptr> m_screenshots; QList<ScreenShot::Ptr> m_screenshots;

View File

@ -1,3 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include "ImgurUpload.h" #include "ImgurUpload.h"
#include "BuildConfig.h" #include "BuildConfig.h"
@ -13,13 +48,13 @@
ImgurUpload::ImgurUpload(ScreenShot::Ptr shot) : NetAction(), m_shot(shot) ImgurUpload::ImgurUpload(ScreenShot::Ptr shot) : NetAction(), m_shot(shot)
{ {
m_url = BuildConfig.IMGUR_BASE_URL + "upload.json"; m_url = BuildConfig.IMGUR_BASE_URL + "upload.json";
m_status = Job_NotStarted; m_state = State::Inactive;
} }
void ImgurUpload::startImpl() void ImgurUpload::executeTask()
{ {
finished = false; finished = false;
m_status = Job_InProgress; m_state = Task::State::Running;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED);
request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str()); request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str());
@ -28,7 +63,7 @@ void ImgurUpload::startImpl()
QFile f(m_shot->m_file.absoluteFilePath()); QFile f(m_shot->m_file.absoluteFilePath());
if (!f.open(QFile::ReadOnly)) if (!f.open(QFile::ReadOnly))
{ {
emit failed(m_index_within_job); emitFailed();
return; return;
} }
@ -63,10 +98,10 @@ void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
qCritical() << "Double finished ImgurUpload!"; qCritical() << "Double finished ImgurUpload!";
return; return;
} }
m_status = Job_Failed; m_state = Task::State::Failed;
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
} }
void ImgurUpload::downloadFinished() void ImgurUpload::downloadFinished()
{ {
@ -84,7 +119,7 @@ void ImgurUpload::downloadFinished()
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString(); qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
auto object = doc.object(); auto object = doc.object();
@ -93,20 +128,19 @@ void ImgurUpload::downloadFinished()
qDebug() << "Screenshot upload not successful:" << doc.toJson(); qDebug() << "Screenshot upload not successful:" << doc.toJson();
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
m_shot->m_imgurId = object.value("data").toObject().value("id").toString(); m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
m_shot->m_url = object.value("data").toObject().value("link").toString(); m_shot->m_url = object.value("data").toObject().value("link").toString();
m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString(); m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString();
m_status = Job_Finished; m_state = Task::State::Succeeded;
finished = true; finished = true;
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} }
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
m_total_progress = bytesTotal; setProgress(bytesReceived, bytesTotal);
m_progress = bytesReceived; emit progress(bytesReceived, bytesTotal);
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
} }

View File

@ -1,5 +1,40 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#pragma once #pragma once
#include "QObjectPtr.h"
#include "net/NetAction.h" #include "net/NetAction.h"
#include "Screenshot.h" #include "Screenshot.h"
@ -21,7 +56,7 @@ slots:
public public
slots: slots:
void startImpl() override; void executeTask() override;
private: private:
ScreenShot::Ptr m_shot; ScreenShot::Ptr m_shot;

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -99,7 +119,7 @@ void Task::emitAborted()
m_state = State::AbortedByUser; m_state = State::AbortedByUser;
m_failReason = "Aborted."; m_failReason = "Aborted.";
qDebug() << "Task" << describe() << "aborted."; qDebug() << "Task" << describe() << "aborted.";
emit failed(m_failReason); emit aborted();
emit finished(); emit finished();
} }

View File

@ -1,4 +1,24 @@
/* Copyright 2013-2021 MultiMC Contributors // SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,10 +35,6 @@
#pragma once #pragma once
#include <QObject>
#include <QString>
#include <QStringList>
#include "QObjectPtr.h" #include "QObjectPtr.h"
class Task : public QObject { class Task : public QObject {
@ -52,6 +68,8 @@ class Task : public QObject {
virtual bool canAbort() const { return false; } virtual bool canAbort() const { return false; }
auto getState() const -> State { return m_state; }
QString getStatus() { return m_status; } QString getStatus() { return m_status; }
virtual auto getStepStatus() const -> QString { return m_status; } virtual auto getStepStatus() const -> QString { return m_status; }
@ -68,15 +86,16 @@ class Task : public QObject {
signals: signals:
void started(); void started();
virtual void progress(qint64 current, qint64 total); void progress(qint64 current, qint64 total);
void finished(); void finished();
void succeeded(); void succeeded();
void aborted();
void failed(QString reason); void failed(QString reason);
void status(QString status); void status(QString status);
public slots: public slots:
virtual void start(); virtual void start();
virtual bool abort() { return false; }; virtual bool abort() { if(canAbort()) emitAborted(); return canAbort(); };
protected: protected:
virtual void executeTask() = 0; virtual void executeTask() = 0;
@ -84,13 +103,13 @@ class Task : public QObject {
protected slots: protected slots:
virtual void emitSucceeded(); virtual void emitSucceeded();
virtual void emitAborted(); virtual void emitAborted();
virtual void emitFailed(QString reason); virtual void emitFailed(QString reason = "");
public slots: public slots:
void setStatus(const QString& status); void setStatus(const QString& status);
void setProgress(qint64 current, qint64 total); void setProgress(qint64 current, qint64 total);
private: protected:
State m_state = State::Inactive; State m_state = State::Inactive;
QStringList m_Warnings; QStringList m_Warnings;
QString m_failReason = ""; QString m_failReason = "";

View File

@ -1,3 +1,38 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include "TranslationsModel.h" #include "TranslationsModel.h"
#include <QCoreApplication> #include <QCoreApplication>
@ -667,7 +702,7 @@ void TranslationsModel::downloadTranslation(QString key)
auto dl = Net::Download::makeCached(QUrl(BuildConfig.TRANSLATIONS_BASE_URL + lang->file_name), entry); auto dl = Net::Download::makeCached(QUrl(BuildConfig.TRANSLATIONS_BASE_URL + lang->file_name), entry);
auto rawHash = QByteArray::fromHex(lang->file_sha1.toLatin1()); auto rawHash = QByteArray::fromHex(lang->file_sha1.toLatin1());
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash)); dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash));
dl->m_total_progress = lang->file_size; dl->setProgress(dl->getProgress(), lang->file_size);
d->m_dl_job = new NetJob("Translation for " + key, APPLICATION->network()); d->m_dl_job = new NetJob("Translation for " + key, APPLICATION->network());
d->m_dl_job->addNetAction(dl); d->m_dl_job->addNetAction(dl);