Merge branch 'develop' of https://github.com/PrismLauncher/PrismLauncher into feature/java-downloader

This commit is contained in:
Trial97 2024-08-20 00:02:14 +03:00
commit 4d24213aa3
No known key found for this signature in database
GPG key ID: 55EF5DA53DB36318
40 changed files with 350 additions and 401 deletions

View file

@ -15,19 +15,26 @@
#include "BaseEntity.h"
#include "Exception.h"
#include "FileSystem.h"
#include "Json.h"
#include "modplatform/helpers/HashUtils.h"
#include "net/ApiDownload.h"
#include "net/ChecksumValidator.h"
#include "net/HttpMetaCache.h"
#include "net/Mode.h"
#include "net/NetJob.h"
#include "Application.h"
#include "BuildConfig.h"
#include "tasks/Task.h"
namespace Meta {
class ParsingValidator : public Net::Validator {
public: /* con/des */
ParsingValidator(Meta::BaseEntity* entity) : m_entity(entity) {};
virtual ~ParsingValidator() {};
ParsingValidator(BaseEntity* entity) : m_entity(entity) {};
virtual ~ParsingValidator() = default;
public: /* methods */
bool init(QNetworkRequest&) override
@ -61,92 +68,131 @@ class ParsingValidator : public Net::Validator {
private: /* data */
QByteArray m_data;
Meta::BaseEntity* m_entity;
BaseEntity* m_entity;
};
Meta::BaseEntity::~BaseEntity() {}
QUrl Meta::BaseEntity::url() const
QUrl BaseEntity::url() const
{
auto s = APPLICATION->settings();
QString metaOverride = s->get("MetaURLOverride").toString();
if (metaOverride.isEmpty()) {
return QUrl(BuildConfig.META_URL).resolved(localFilename());
} else {
return QUrl(metaOverride).resolved(localFilename());
}
return QUrl(metaOverride).resolved(localFilename());
}
bool Meta::BaseEntity::loadLocalFile()
Task::Ptr BaseEntity::loadTask(Net::Mode mode)
{
const QString fname = QDir("meta").absoluteFilePath(localFilename());
if (!QFile::exists(fname)) {
return false;
}
// TODO: check if the file has the expected checksum
try {
auto doc = Json::requireDocument(fname, fname);
auto obj = Json::requireObject(doc, fname);
parse(obj);
return true;
} catch (const Exception& e) {
qDebug() << QString("Unable to parse file %1: %2").arg(fname, e.cause());
// just make sure it's gone and we never consider it again.
return !FS::deletePath(fname);
if (m_task && m_task->isRunning()) {
return m_task;
}
m_task.reset(new BaseEntityLoadTask(this, mode));
return m_task;
}
void Meta::BaseEntity::load(Net::Mode loadType)
bool BaseEntity::isLoaded() const
{
// load local file if nothing is loaded yet
if (!isLoaded()) {
if (loadLocalFile()) {
m_loadStatus = LoadStatus::Local;
// consider it loaded only if the main hash is either empty and was remote loadded or the hashes match and was loaded
return m_sha256.isEmpty() ? m_load_status == LoadStatus::Remote : m_load_status != LoadStatus::NotLoaded && m_sha256 == m_file_sha256;
}
void BaseEntity::setSha256(QString sha256)
{
m_sha256 = sha256;
}
BaseEntity::LoadStatus BaseEntity::status() const
{
return m_load_status;
}
BaseEntityLoadTask::BaseEntityLoadTask(BaseEntity* parent, Net::Mode mode) : m_entity(parent), m_mode(mode) {}
void BaseEntityLoadTask::executeTask()
{
const QString fname = QDir("meta").absoluteFilePath(m_entity->localFilename());
auto hashMatches = false;
// the file exists on disk try to load it
if (QFile::exists(fname)) {
try {
QByteArray fileData;
// read local file if nothing is loaded yet
if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded || m_entity->m_file_sha256.isEmpty()) {
setStatus(tr("Loading local file"));
fileData = FS::read(fname);
m_entity->m_file_sha256 = Hashing::hash(fileData, Hashing::Algorithm::Sha256);
}
// on online the hash needs to match
hashMatches = m_entity->m_sha256 == m_entity->m_file_sha256;
if (m_mode == Net::Mode::Online && !m_entity->m_sha256.isEmpty() && !hashMatches) {
throw Exception("mismatched checksum");
}
// load local file
if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded) {
auto doc = Json::requireDocument(fileData, fname);
auto obj = Json::requireObject(doc, fname);
m_entity->parse(obj);
m_entity->m_load_status = BaseEntity::LoadStatus::Local;
}
} catch (const Exception& e) {
qDebug() << QString("Unable to parse file %1: %2").arg(fname, e.cause());
// just make sure it's gone and we never consider it again.
FS::deletePath(fname);
m_entity->m_load_status = BaseEntity::LoadStatus::NotLoaded;
}
}
// if we need remote update, run the update task
if (loadType == Net::Mode::Offline || !shouldStartRemoteUpdate()) {
auto wasLoadedOffline = m_entity->m_load_status != BaseEntity::LoadStatus::NotLoaded && m_mode == Net::Mode::Offline;
// if has is not present allways fetch from remote(e.g. the main index file), else only fetch if hash doesn't match
auto wasLoadedRemote = m_entity->m_sha256.isEmpty() ? m_entity->m_load_status == BaseEntity::LoadStatus::Remote : hashMatches;
if (wasLoadedOffline || wasLoadedRemote) {
emitSucceeded();
return;
}
m_updateTask.reset(new NetJob(QObject::tr("Download of meta file %1").arg(localFilename()), APPLICATION->network()));
auto url = this->url();
auto entry = APPLICATION->metacache()->resolveEntry("meta", localFilename());
m_task.reset(new NetJob(QObject::tr("Download of meta file %1").arg(m_entity->localFilename()), APPLICATION->network()));
auto url = m_entity->url();
auto entry = APPLICATION->metacache()->resolveEntry("meta", m_entity->localFilename());
entry->setStale(true);
auto dl = Net::ApiDownload::makeCached(url, entry);
/*
* The validator parses the file and loads it into the object.
* If that fails, the file is not written to storage.
*/
dl->addValidator(new ParsingValidator(this));
m_updateTask->addNetAction(dl);
m_updateStatus = UpdateStatus::InProgress;
QObject::connect(m_updateTask.get(), &NetJob::succeeded, [&]() {
m_loadStatus = LoadStatus::Remote;
m_updateStatus = UpdateStatus::Succeeded;
m_updateTask.reset();
if (!m_entity->m_sha256.isEmpty())
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Algorithm::Sha256, m_entity->m_sha256));
dl->addValidator(new ParsingValidator(m_entity));
m_task->addNetAction(dl);
m_task->setAskRetry(false);
connect(m_task.get(), &Task::failed, this, &BaseEntityLoadTask::emitFailed);
connect(m_task.get(), &Task::succeeded, this, &BaseEntityLoadTask::emitSucceeded);
connect(m_task.get(), &Task::succeeded, this, [this]() {
m_entity->m_load_status = BaseEntity::LoadStatus::Remote;
m_entity->m_file_sha256 = m_entity->m_sha256;
});
QObject::connect(m_updateTask.get(), &NetJob::failed, [&]() {
m_updateStatus = UpdateStatus::Failed;
m_updateTask.reset();
});
m_updateTask->start();
connect(m_task.get(), &Task::progress, this, &Task::setProgress);
connect(m_task.get(), &Task::stepProgress, this, &BaseEntityLoadTask::propagateStepProgress);
connect(m_task.get(), &Task::status, this, &Task::setStatus);
connect(m_task.get(), &Task::details, this, &Task::setDetails);
m_task->start();
}
bool Meta::BaseEntity::isLoaded() const
bool BaseEntityLoadTask::canAbort() const
{
return m_loadStatus > LoadStatus::NotLoaded;
return m_task ? m_task->canAbort() : false;
}
bool Meta::BaseEntity::shouldStartRemoteUpdate() const
bool BaseEntityLoadTask::abort()
{
// TODO: version-locks and offline mode?
return m_updateStatus != UpdateStatus::InProgress;
}
Task::Ptr Meta::BaseEntity::getCurrentTask()
{
if (m_updateStatus == UpdateStatus::InProgress) {
return m_updateTask;
if (m_task) {
Task::abort();
return m_task->abort();
}
return nullptr;
return Task::abort();
}
} // namespace Meta

View file

@ -17,38 +17,57 @@
#include <QJsonObject>
#include <QObject>
#include "QObjectPtr.h"
#include "net/Mode.h"
#include "net/NetJob.h"
#include "tasks/Task.h"
namespace Meta {
class BaseEntityLoadTask;
class BaseEntity {
friend BaseEntityLoadTask;
public: /* types */
using Ptr = std::shared_ptr<BaseEntity>;
enum class LoadStatus { NotLoaded, Local, Remote };
enum class UpdateStatus { NotDone, InProgress, Failed, Succeeded };
public:
virtual ~BaseEntity();
virtual void parse(const QJsonObject& obj) = 0;
virtual ~BaseEntity() = default;
virtual QString localFilename() const = 0;
virtual QUrl url() const;
bool isLoaded() const;
bool shouldStartRemoteUpdate() const;
LoadStatus status() const;
void load(Net::Mode loadType);
Task::Ptr getCurrentTask();
/* for parsers */
void setSha256(QString sha256);
protected: /* methods */
bool loadLocalFile();
virtual void parse(const QJsonObject& obj) = 0;
[[nodiscard]] Task::Ptr loadTask(Net::Mode loadType = Net::Mode::Online);
protected:
QString m_sha256; // the expected sha256
QString m_file_sha256; // the file sha256
private:
LoadStatus m_loadStatus = LoadStatus::NotLoaded;
UpdateStatus m_updateStatus = UpdateStatus::NotDone;
NetJob::Ptr m_updateTask;
LoadStatus m_load_status = LoadStatus::NotLoaded;
Task::Ptr m_task;
};
class BaseEntityLoadTask : public Task {
Q_OBJECT
public:
explicit BaseEntityLoadTask(BaseEntity* parent, Net::Mode mode);
~BaseEntityLoadTask() override = default;
virtual void executeTask() override;
virtual bool canAbort() const override;
virtual bool abort() override;
private:
BaseEntity* m_entity;
Net::Mode m_mode;
NetJob::Ptr m_task;
};
} // namespace Meta

View file

@ -16,7 +16,10 @@
#include "Index.h"
#include "JsonFormat.h"
#include "QObjectPtr.h"
#include "VersionList.h"
#include "meta/BaseEntity.h"
#include "tasks/SequentialTask.h"
namespace Meta {
Index::Index(QObject* parent) : QAbstractListModel(parent) {}
@ -51,14 +54,17 @@ QVariant Index::data(const QModelIndex& index, int role) const
}
return QVariant();
}
int Index::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : m_lists.size();
}
int Index::columnCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : 1;
}
QVariant Index::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) {
@ -79,6 +85,7 @@ VersionList::Ptr Index::get(const QString& uid)
if (!out) {
out = std::make_shared<VersionList>(uid);
m_uids[uid] = out;
m_lists.append(out);
}
return out;
}
@ -96,7 +103,7 @@ void Index::parse(const QJsonObject& obj)
void Index::merge(const std::shared_ptr<Index>& other)
{
const QVector<VersionList::Ptr> lists = std::dynamic_pointer_cast<Index>(other)->m_lists;
const QVector<VersionList::Ptr> lists = other->m_lists;
// initial load, no need to merge
if (m_lists.isEmpty()) {
beginResetModel();
@ -123,7 +130,33 @@ void Index::merge(const std::shared_ptr<Index>& other)
void Index::connectVersionList(const int row, const VersionList::Ptr& list)
{
connect(list.get(), &VersionList::nameChanged, this,
[this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << Qt::DisplayRole); });
connect(list.get(), &VersionList::nameChanged, this, [this, row] { emit dataChanged(index(row), index(row), { Qt::DisplayRole }); });
}
Task::Ptr Index::loadVersion(const QString& uid, const QString& version, Net::Mode mode, bool force)
{
if (mode == Net::Mode::Offline) {
return get(uid, version)->loadTask(mode);
}
auto versionList = get(uid);
auto loadTask = makeShared<SequentialTask>(
this, tr("Load meta for %1:%2", "This is for the task name that loads the meta index.").arg(uid, version));
if (status() != BaseEntity::LoadStatus::Remote || force) {
loadTask->addTask(this->loadTask(mode));
}
loadTask->addTask(versionList->loadTask(mode));
loadTask->addTask(versionList->getVersion(version)->loadTask(mode));
return loadTask;
}
Version::Ptr Index::getLoadedVersion(const QString& uid, const QString& version)
{
QEventLoop ev;
auto task = loadVersion(uid, version);
QObject::connect(task.get(), &Task::finished, &ev, &QEventLoop::quit);
task->start();
ev.exec();
return get(uid, version);
}
} // namespace Meta

View file

@ -16,10 +16,10 @@
#pragma once
#include <QAbstractListModel>
#include <memory>
#include "BaseEntity.h"
#include "meta/VersionList.h"
#include "net/Mode.h"
class Task;
@ -30,6 +30,7 @@ class Index : public QAbstractListModel, public BaseEntity {
public:
explicit Index(QObject* parent = nullptr);
explicit Index(const QVector<VersionList::Ptr>& lists, QObject* parent = nullptr);
virtual ~Index() = default;
enum { UidRole = Qt::UserRole, NameRole, ListPtrRole };
@ -47,8 +48,15 @@ class Index : public QAbstractListModel, public BaseEntity {
QVector<VersionList::Ptr> lists() const { return m_lists; }
Task::Ptr loadVersion(const QString& uid, const QString& version = {}, Net::Mode mode = Net::Mode::Online, bool force = false);
// this blocks until the version is loaded
Version::Ptr getLoadedVersion(const QString& uid, const QString& version);
public: // for usage by parsers only
void merge(const std::shared_ptr<Index>& other);
protected:
void parse(const QJsonObject& obj) override;
private:

View file

@ -41,6 +41,7 @@ static std::shared_ptr<Index> parseIndexInternal(const QJsonObject& obj)
std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject& obj) {
VersionList::Ptr list = std::make_shared<VersionList>(requireString(obj, "uid"));
list->setName(ensureString(obj, "name", QString()));
list->setSha256(ensureString(obj, "sha256", QString()));
return list;
});
return std::make_shared<Index>(lists);
@ -58,6 +59,9 @@ static Version::Ptr parseCommonVersion(const QString& uid, const QJsonObject& ob
parseRequires(obj, &reqs, "requires");
parseRequires(obj, &conflicts, "conflicts");
version->setRequires(reqs, conflicts);
if (auto sha256 = ensureString(obj, "sha256", QString()); !sha256.isEmpty()) {
version->setSha256(sha256);
}
return version;
}

View file

@ -16,11 +16,9 @@
#pragma once
#include <QJsonObject>
#include <memory>
#include <set>
#include "Exception.h"
#include "meta/BaseEntity.h"
namespace Meta {
class Index;

View file

@ -18,12 +18,9 @@
#include <QDateTime>
#include "JsonFormat.h"
#include "minecraft/PackProfile.h"
Meta::Version::Version(const QString& uid, const QString& version) : BaseVersion(), m_uid(uid), m_version(version) {}
Meta::Version::~Version() {}
QString Meta::Version::descriptor()
{
return m_version;
@ -71,6 +68,9 @@ void Meta::Version::mergeFromList(const Meta::Version::Ptr& other)
if (m_volatile != other->m_volatile) {
setVolatile(other->m_volatile);
}
if (!other->m_sha256.isEmpty()) {
m_sha256 = other->m_sha256;
}
}
void Meta::Version::merge(const Version::Ptr& other)

View file

@ -38,7 +38,7 @@ class Version : public QObject, public BaseVersion, public BaseEntity {
using Ptr = std::shared_ptr<Version>;
explicit Version(const QString& uid, const QString& version);
virtual ~Version();
virtual ~Version() = default;
QString descriptor() override;
QString name() override;
@ -52,7 +52,7 @@ class Version : public QObject, public BaseVersion, public BaseEntity {
const Meta::RequireSet& requiredSet() const { return m_requires; }
VersionFilePtr data() const { return m_data; }
bool isRecommended() const { return m_recommended; }
bool isLoaded() const { return m_data != nullptr; }
bool isLoaded() const { return m_data != nullptr && BaseEntity::isLoaded(); }
void merge(const Version::Ptr& other);
void mergeFromList(const Version::Ptr& other);

View file

@ -17,8 +17,13 @@
#include <QDateTime>
#include "Application.h"
#include "Index.h"
#include "JsonFormat.h"
#include "Version.h"
#include "meta/BaseEntity.h"
#include "net/Mode.h"
#include "tasks/SequentialTask.h"
namespace Meta {
VersionList::VersionList(const QString& uid, QObject* parent) : BaseVersionList(parent), m_uid(uid)
@ -28,8 +33,11 @@ VersionList::VersionList(const QString& uid, QObject* parent) : BaseVersionList(
Task::Ptr VersionList::getLoadTask()
{
load(Net::Mode::Online);
return getCurrentTask();
auto loadTask =
makeShared<SequentialTask>(this, tr("Load meta for %1", "This is for the task name that loads the meta index.").arg(m_uid));
loadTask->addTask(APPLICATION->metadataIndex()->loadTask(Net::Mode::Online));
loadTask->addTask(this->loadTask(Net::Mode::Online));
return loadTask;
}
bool VersionList::isLoaded()
@ -142,6 +150,8 @@ Version::Ptr VersionList::getVersion(const QString& version)
if (!out) {
out = std::make_shared<Version>(m_uid, version);
m_lookup[version] = out;
setupAddedVersion(m_versions.size(), out);
m_versions.append(out);
}
return out;
}
@ -202,6 +212,9 @@ void VersionList::mergeFromIndex(const VersionList::Ptr& other)
if (m_name != other->m_name) {
setName(other->m_name);
}
if (!other->m_sha256.isEmpty()) {
m_sha256 = other->m_sha256;
}
}
void VersionList::merge(const VersionList::Ptr& other)
@ -209,23 +222,27 @@ void VersionList::merge(const VersionList::Ptr& other)
if (m_name != other->m_name) {
setName(other->m_name);
}
if (!other->m_sha256.isEmpty()) {
m_sha256 = other->m_sha256;
}
// TODO: do not reset the whole model. maybe?
beginResetModel();
m_versions.clear();
if (other->m_versions.isEmpty()) {
qWarning() << "Empty list loaded ...";
}
for (const Version::Ptr& version : other->m_versions) {
for (auto version : other->m_versions) {
// we already have the version. merge the contents
if (m_lookup.contains(version->version())) {
m_lookup.value(version->version())->mergeFromList(version);
auto existing = m_lookup.value(version->version());
existing->mergeFromList(version);
version = existing;
} else {
m_lookup.insert(version->uid(), version);
m_lookup.insert(version->version(), version);
// connect it.
setupAddedVersion(m_versions.size(), version);
m_versions.append(version);
}
// connect it.
setupAddedVersion(m_versions.size(), version);
m_versions.append(version);
m_recommended = getBetterVersion(m_recommended, version);
}
endResetModel();
@ -233,14 +250,15 @@ void VersionList::merge(const VersionList::Ptr& other)
void VersionList::setupAddedVersion(const int row, const Version::Ptr& version)
{
// FIXME: do not disconnect from everythin, disconnect only the lambdas here
version->disconnect();
disconnect(version.get(), &Version::requiresChanged, this, nullptr);
disconnect(version.get(), &Version::timeChanged, this, nullptr);
disconnect(version.get(), &Version::typeChanged, this, nullptr);
connect(version.get(), &Version::requiresChanged, this,
[this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << RequiresRole); });
connect(version.get(), &Version::timeChanged, this,
[this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TimeRole << SortRole); });
connect(version.get(), &Version::typeChanged, this,
[this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TypeRole); });
[this, row]() { emit dataChanged(index(row), index(row), { TimeRole, SortRole }); });
connect(version.get(), &Version::typeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), { TypeRole }); });
}
BaseVersion::Ptr VersionList::getRecommended() const
@ -248,4 +266,14 @@ BaseVersion::Ptr VersionList::getRecommended() const
return m_recommended;
}
void VersionList::waitToLoad()
{
if (isLoaded())
return;
QEventLoop ev;
auto task = getLoadTask();
QObject::connect(task.get(), &Task::finished, &ev, &QEventLoop::quit);
task->start();
ev.exec();
}
} // namespace Meta

View file

@ -30,13 +30,14 @@ class VersionList : public BaseVersionList, public BaseEntity {
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
public:
explicit VersionList(const QString& uid, QObject* parent = nullptr);
virtual ~VersionList() = default;
using Ptr = std::shared_ptr<VersionList>;
enum Roles { UidRole = Qt::UserRole + 100, TimeRole, RequiresRole, VersionPtrRole };
Task::Ptr getLoadTask() override;
bool isLoaded() override;
[[nodiscard]] Task::Ptr getLoadTask() override;
const BaseVersion::Ptr at(int i) const override;
int count() const override;
void sortVersions() override;
@ -60,6 +61,9 @@ class VersionList : public BaseVersionList, public BaseEntity {
QVector<Version::Ptr> versions() const { return m_versions; }
// this blocks until the version list is loaded
void waitToLoad();
public: // for usage only by parsers
void setName(const QString& name);
void setVersions(const QVector<Version::Ptr>& versions);