From ab868df50eb6f9f3958bdc0a7ab9199dcdf46b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 11 Mar 2017 01:39:45 +0100 Subject: NOISSUE Wonko is the new Meta And then Wonko was the Meta. --- api/logic/meta/BaseEntity.cpp | 42 +++++ api/logic/meta/BaseEntity.h | 53 ++++++ api/logic/meta/Index.cpp | 150 +++++++++++++++++ api/logic/meta/Index.h | 72 ++++++++ api/logic/meta/Index_test.cpp | 50 ++++++ api/logic/meta/Reference.cpp | 48 ++++++ api/logic/meta/Reference.h | 44 +++++ api/logic/meta/Util.cpp | 50 ++++++ api/logic/meta/Util.h | 31 ++++ api/logic/meta/Version.cpp | 105 ++++++++++++ api/logic/meta/Version.h | 87 ++++++++++ api/logic/meta/VersionList.cpp | 288 ++++++++++++++++++++++++++++++++ api/logic/meta/VersionList.h | 95 +++++++++++ api/logic/meta/format/Format.cpp | 84 ++++++++++ api/logic/meta/format/Format.h | 57 +++++++ api/logic/meta/format/FormatV1.cpp | 161 ++++++++++++++++++ api/logic/meta/format/FormatV1.h | 33 ++++ api/logic/meta/tasks/LocalLoadTask.cpp | 123 ++++++++++++++ api/logic/meta/tasks/LocalLoadTask.h | 84 ++++++++++ api/logic/meta/tasks/RemoteLoadTask.cpp | 132 +++++++++++++++ api/logic/meta/tasks/RemoteLoadTask.h | 95 +++++++++++ 21 files changed, 1884 insertions(+) create mode 100644 api/logic/meta/BaseEntity.cpp create mode 100644 api/logic/meta/BaseEntity.h create mode 100644 api/logic/meta/Index.cpp create mode 100644 api/logic/meta/Index.h create mode 100644 api/logic/meta/Index_test.cpp create mode 100644 api/logic/meta/Reference.cpp create mode 100644 api/logic/meta/Reference.h create mode 100644 api/logic/meta/Util.cpp create mode 100644 api/logic/meta/Util.h create mode 100644 api/logic/meta/Version.cpp create mode 100644 api/logic/meta/Version.h create mode 100644 api/logic/meta/VersionList.cpp create mode 100644 api/logic/meta/VersionList.h create mode 100644 api/logic/meta/format/Format.cpp create mode 100644 api/logic/meta/format/Format.h create mode 100644 api/logic/meta/format/FormatV1.cpp create mode 100644 api/logic/meta/format/FormatV1.h create mode 100644 api/logic/meta/tasks/LocalLoadTask.cpp create mode 100644 api/logic/meta/tasks/LocalLoadTask.h create mode 100644 api/logic/meta/tasks/RemoteLoadTask.cpp create mode 100644 api/logic/meta/tasks/RemoteLoadTask.h (limited to 'api/logic/meta') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp new file mode 100644 index 00000000..fd44e29c --- /dev/null +++ b/api/logic/meta/BaseEntity.cpp @@ -0,0 +1,42 @@ +/* Copyright 2015-2017 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 "BaseEntity.h" + +#include "Json.h" +#include "Util.h" + +namespace Meta +{ +BaseEntity::~BaseEntity() +{ +} + +void BaseEntity::store() const +{ + Json::write(serialized(), Meta::localDir().absoluteFilePath(localFilename())); +} + +void BaseEntity::notifyLocalLoadComplete() +{ + m_localLoaded = true; + store(); +} +void BaseEntity::notifyRemoteLoadComplete() +{ + m_remoteLoaded = true; + store(); +} +} diff --git a/api/logic/meta/BaseEntity.h b/api/logic/meta/BaseEntity.h new file mode 100644 index 00000000..b7a241c6 --- /dev/null +++ b/api/logic/meta/BaseEntity.h @@ -0,0 +1,53 @@ +/* Copyright 2015-2017 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 + +#include +#include + +#include "multimc_logic_export.h" + +class Task; +namespace Meta +{ +class MULTIMC_LOGIC_EXPORT BaseEntity +{ +public: + virtual ~BaseEntity(); + + using Ptr = std::shared_ptr; + + virtual std::unique_ptr remoteUpdateTask() = 0; + virtual std::unique_ptr localUpdateTask() = 0; + virtual void merge(const std::shared_ptr &other) = 0; + + void store() const; + virtual QString localFilename() const = 0; + virtual QJsonObject serialized() const = 0; + + bool isComplete() const { return m_localLoaded || m_remoteLoaded; } + + bool isLocalLoaded() const { return m_localLoaded; } + bool isRemoteLoaded() const { return m_remoteLoaded; } + + void notifyLocalLoadComplete(); + void notifyRemoteLoadComplete(); + +private: + bool m_localLoaded = false; + bool m_remoteLoaded = false; +}; +} diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp new file mode 100644 index 00000000..8a6b1355 --- /dev/null +++ b/api/logic/meta/Index.cpp @@ -0,0 +1,150 @@ +/* Copyright 2015-2017 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 "Index.h" + +#include "VersionList.h" +#include "tasks/LocalLoadTask.h" +#include "tasks/RemoteLoadTask.h" +#include "format/Format.h" + +namespace Meta +{ +Index::Index(QObject *parent) + : QAbstractListModel(parent) +{ +} +Index::Index(const QVector &lists, QObject *parent) + : QAbstractListModel(parent), m_lists(lists) +{ + for (int i = 0; i < m_lists.size(); ++i) + { + m_uids.insert(m_lists.at(i)->uid(), m_lists.at(i)); + connectVersionList(i, m_lists.at(i)); + } +} + +QVariant Index::data(const QModelIndex &index, int role) const +{ + if (index.parent().isValid() || index.row() < 0 || index.row() >= m_lists.size()) + { + return QVariant(); + } + + VersionListPtr list = m_lists.at(index.row()); + switch (role) + { + case Qt::DisplayRole: + switch (index.column()) + { + case 0: return list->humanReadable(); + default: break; + } + case UidRole: return list->uid(); + case NameRole: return list->name(); + case ListPtrRole: return QVariant::fromValue(list); + } + return QVariant(); +} +int Index::rowCount(const QModelIndex &parent) const +{ + return m_lists.size(); +} +int Index::columnCount(const QModelIndex &parent) const +{ + return 1; +} +QVariant Index::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) + { + return tr("Name"); + } + else + { + return QVariant(); + } +} + +std::unique_ptr Index::remoteUpdateTask() +{ + return std::unique_ptr(new IndexRemoteLoadTask(this, this)); +} +std::unique_ptr Index::localUpdateTask() +{ + return std::unique_ptr(new IndexLocalLoadTask(this, this)); +} + +QJsonObject Index::serialized() const +{ + return Format::serializeIndex(this); +} + +bool Index::hasUid(const QString &uid) const +{ + return m_uids.contains(uid); +} +VersionListPtr Index::getList(const QString &uid) const +{ + return m_uids.value(uid, nullptr); +} +VersionListPtr Index::getListGuaranteed(const QString &uid) const +{ + return m_uids.value(uid, std::make_shared(uid)); +} + +void Index::merge(const Ptr &other) +{ + const QVector lists = std::dynamic_pointer_cast(other)->m_lists; + // initial load, no need to merge + if (m_lists.isEmpty()) + { + beginResetModel(); + m_lists = lists; + for (int i = 0; i < lists.size(); ++i) + { + m_uids.insert(lists.at(i)->uid(), lists.at(i)); + connectVersionList(i, lists.at(i)); + } + endResetModel(); + } + else + { + for (const VersionListPtr &list : lists) + { + if (m_uids.contains(list->uid())) + { + m_uids[list->uid()]->merge(list); + } + else + { + beginInsertRows(QModelIndex(), m_lists.size(), m_lists.size()); + connectVersionList(m_lists.size(), list); + m_lists.append(list); + m_uids.insert(list->uid(), list); + endInsertRows(); + } + } + } +} + +void Index::connectVersionList(const int row, const VersionListPtr &list) +{ + connect(list.get(), &VersionList::nameChanged, this, [this, row]() + { + emit dataChanged(index(row), index(row), QVector() << Qt::DisplayRole); + }); +} +} diff --git a/api/logic/meta/Index.h b/api/logic/meta/Index.h new file mode 100644 index 00000000..16e95d5a --- /dev/null +++ b/api/logic/meta/Index.h @@ -0,0 +1,72 @@ +/* Copyright 2015-2017 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 + +#include +#include + +#include "BaseEntity.h" + +#include "multimc_logic_export.h" + +class Task; + +namespace Meta +{ +using VersionListPtr = std::shared_ptr; + +class MULTIMC_LOGIC_EXPORT Index : public QAbstractListModel, public BaseEntity +{ + Q_OBJECT +public: + explicit Index(QObject *parent = nullptr); + explicit Index(const QVector &lists, QObject *parent = nullptr); + + enum + { + UidRole = Qt::UserRole, + NameRole, + ListPtrRole + }; + + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + + std::unique_ptr remoteUpdateTask() override; + std::unique_ptr localUpdateTask() override; + + QString localFilename() const override { return "index.json"; } + QJsonObject serialized() const override; + + // queries + bool hasUid(const QString &uid) const; + VersionListPtr getList(const QString &uid) const; + VersionListPtr getListGuaranteed(const QString &uid) const; + + QVector lists() const { return m_lists; } + +public: // for usage by parsers only + void merge(const BaseEntity::Ptr &other) override; + +private: + QVector m_lists; + QHash m_uids; + + void connectVersionList(const int row, const VersionListPtr &list); +}; +} diff --git a/api/logic/meta/Index_test.cpp b/api/logic/meta/Index_test.cpp new file mode 100644 index 00000000..d26700ca --- /dev/null +++ b/api/logic/meta/Index_test.cpp @@ -0,0 +1,50 @@ +#include +#include "TestUtil.h" + +#include "meta/Index.h" +#include "meta/VersionList.h" +#include "Env.h" + +class IndexTest : public QObject +{ + Q_OBJECT +private +slots: + void test_isProvidedByEnv() + { + QVERIFY(ENV.metadataIndex()); + QCOMPARE(ENV.metadataIndex(), ENV.metadataIndex()); + } + + void test_providesTasks() + { + QVERIFY(ENV.metadataIndex()->localUpdateTask() != nullptr); + QVERIFY(ENV.metadataIndex()->remoteUpdateTask() != nullptr); + } + + void test_hasUid_and_getList() + { + Meta::Index windex({std::make_shared("list1"), std::make_shared("list2"), std::make_shared("list3")}); + QVERIFY(windex.hasUid("list1")); + QVERIFY(!windex.hasUid("asdf")); + QVERIFY(windex.getList("list2") != nullptr); + QCOMPARE(windex.getList("list2")->uid(), QString("list2")); + QVERIFY(windex.getList("adsf") == nullptr); + } + + void test_merge() + { + Meta::Index windex({std::make_shared("list1"), std::make_shared("list2"), std::make_shared("list3")}); + QCOMPARE(windex.lists().size(), 3); + windex.merge(std::shared_ptr(new Meta::Index({std::make_shared("list1"), std::make_shared("list2"), std::make_shared("list3")}))); + QCOMPARE(windex.lists().size(), 3); + windex.merge(std::shared_ptr(new Meta::Index({std::make_shared("list4"), std::make_shared("list2"), std::make_shared("list5")}))); + QCOMPARE(windex.lists().size(), 5); + windex.merge(std::shared_ptr(new Meta::Index({std::make_shared("list6")}))); + QCOMPARE(windex.lists().size(), 6); + } +}; + +QTEST_GUILESS_MAIN(IndexTest) + +#include "Index_test.moc" diff --git a/api/logic/meta/Reference.cpp b/api/logic/meta/Reference.cpp new file mode 100644 index 00000000..c5cef172 --- /dev/null +++ b/api/logic/meta/Reference.cpp @@ -0,0 +1,48 @@ +/* Copyright 2015-2017 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 "Reference.h" + +namespace Meta +{ +Reference::Reference(const QString &uid) + : m_uid(uid) +{ +} + +QString Reference::uid() const +{ + return m_uid; +} + +QString Reference::version() const +{ + return m_version; +} +void Reference::setVersion(const QString &version) +{ + m_version = version; +} + +bool Reference::operator==(const Reference &other) const +{ + return m_uid == other.m_uid && m_version == other.m_version; +} + +bool Reference::operator!=(const Reference &other) const +{ + return m_uid != other.m_uid || m_version != other.m_version; +} +} diff --git a/api/logic/meta/Reference.h b/api/logic/meta/Reference.h new file mode 100644 index 00000000..027076cc --- /dev/null +++ b/api/logic/meta/Reference.h @@ -0,0 +1,44 @@ +/* Copyright 2015-2017 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 + +#include +#include + +#include "multimc_logic_export.h" + +namespace Meta +{ +class MULTIMC_LOGIC_EXPORT Reference +{ +public: + Reference() {} + explicit Reference(const QString &uid); + + QString uid() const; + + QString version() const; + void setVersion(const QString &version); + + bool operator==(const Reference &other) const; + bool operator!=(const Reference &other) const; + +private: + QString m_uid; + QString m_version; +}; +} +Q_DECLARE_METATYPE(Meta::Reference) diff --git a/api/logic/meta/Util.cpp b/api/logic/meta/Util.cpp new file mode 100644 index 00000000..2ccbe5c4 --- /dev/null +++ b/api/logic/meta/Util.cpp @@ -0,0 +1,50 @@ +/* Copyright 2015-2017 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 "Util.h" + +#include +#include + +#include "Env.h" + +namespace Meta +{ +QUrl rootUrl() +{ + return QUrl("https://meta.multimc.org"); +} + +QUrl indexUrl() +{ + return rootUrl().resolved(QStringLiteral("index.json")); +} + +QUrl versionListUrl(const QString &uid) +{ + return rootUrl().resolved(uid + ".json"); +} + +QUrl versionUrl(const QString &uid, const QString &version) +{ + return rootUrl().resolved(uid + "/" + version + ".json"); +} + +QDir localDir() +{ + return QDir("meta"); +} + +} diff --git a/api/logic/meta/Util.h b/api/logic/meta/Util.h new file mode 100644 index 00000000..28163fe4 --- /dev/null +++ b/api/logic/meta/Util.h @@ -0,0 +1,31 @@ +/* Copyright 2015-2017 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 + +#include "multimc_logic_export.h" + +class QUrl; +class QString; +class QDir; + +namespace Meta +{ +MULTIMC_LOGIC_EXPORT QUrl rootUrl(); +MULTIMC_LOGIC_EXPORT QUrl indexUrl(); +MULTIMC_LOGIC_EXPORT QUrl versionListUrl(const QString &uid); +MULTIMC_LOGIC_EXPORT QUrl versionUrl(const QString &uid, const QString &version); +MULTIMC_LOGIC_EXPORT QDir localDir(); +} diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp new file mode 100644 index 00000000..039f4fef --- /dev/null +++ b/api/logic/meta/Version.cpp @@ -0,0 +1,105 @@ +/* Copyright 2015-2017 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 "Version.h" + +#include + +#include "tasks/LocalLoadTask.h" +#include "tasks/RemoteLoadTask.h" +#include "format/Format.h" + +namespace Meta +{ +Version::Version(const QString &uid, const QString &version) + : BaseVersion(), m_uid(uid), m_version(version) +{ +} + +QString Version::descriptor() +{ + return m_version; +} +QString Version::name() +{ + return m_version; +} +QString Version::typeString() const +{ + return m_type; +} + +QDateTime Version::time() const +{ + return QDateTime::fromMSecsSinceEpoch(m_time * 1000, Qt::UTC); +} + +std::unique_ptr Version::remoteUpdateTask() +{ + return std::unique_ptr(new VersionRemoteLoadTask(this, this)); +} +std::unique_ptr Version::localUpdateTask() +{ + return std::unique_ptr(new VersionLocalLoadTask(this, this)); +} + +void Version::merge(const std::shared_ptr &other) +{ + VersionPtr version = std::dynamic_pointer_cast(other); + if (m_type != version->m_type) + { + setType(version->m_type); + } + if (m_time != version->m_time) + { + setTime(version->m_time); + } + if (m_requires != version->m_requires) + { + setRequires(version->m_requires); + } + + setData(version->m_data); +} + +QString Version::localFilename() const +{ + return m_uid + '/' + m_version + ".json"; +} +QJsonObject Version::serialized() const +{ + return Format::serializeVersion(this); +} + +void Version::setType(const QString &type) +{ + m_type = type; + emit typeChanged(); +} +void Version::setTime(const qint64 time) +{ + m_time = time; + emit timeChanged(); +} +void Version::setRequires(const QVector &requires) +{ + m_requires = requires; + emit requiresChanged(); +} +void Version::setData(const VersionFilePtr &data) +{ + m_data = data; +} +} diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h new file mode 100644 index 00000000..4a791880 --- /dev/null +++ b/api/logic/meta/Version.h @@ -0,0 +1,87 @@ +/* Copyright 2015-2017 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 + +#include "BaseVersion.h" + +#include +#include +#include +#include + +#include "minecraft/VersionFile.h" + +#include "BaseEntity.h" +#include "Reference.h" + +#include "multimc_logic_export.h" + +namespace Meta +{ +using VersionPtr = std::shared_ptr; + +class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public BaseEntity +{ + Q_OBJECT + Q_PROPERTY(QString uid READ uid CONSTANT) + Q_PROPERTY(QString version READ version CONSTANT) + Q_PROPERTY(QString type READ type NOTIFY typeChanged) + Q_PROPERTY(QDateTime time READ time NOTIFY timeChanged) + Q_PROPERTY(QVector requires READ requires NOTIFY requiresChanged) +public: + explicit Version(const QString &uid, const QString &version); + + QString descriptor() override; + QString name() override; + QString typeString() const override; + + QString uid() const { return m_uid; } + QString version() const { return m_version; } + QString type() const { return m_type; } + QDateTime time() const; + qint64 rawTime() const { return m_time; } + QVector requires() const { return m_requires; } + VersionFilePtr data() const { return m_data; } + + std::unique_ptr remoteUpdateTask() override; + std::unique_ptr localUpdateTask() override; + void merge(const std::shared_ptr &other) override; + + QString localFilename() const override; + QJsonObject serialized() const override; + +public: // for usage by format parsers only + void setType(const QString &type); + void setTime(const qint64 time); + void setRequires(const QVector &requires); + void setData(const VersionFilePtr &data); + +signals: + void typeChanged(); + void timeChanged(); + void requiresChanged(); + +private: + QString m_uid; + QString m_version; + QString m_type; + qint64 m_time; + QVector m_requires; + VersionFilePtr m_data; +}; +} + +Q_DECLARE_METATYPE(Meta::VersionPtr) diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp new file mode 100644 index 00000000..11e20c12 --- /dev/null +++ b/api/logic/meta/VersionList.cpp @@ -0,0 +1,288 @@ +/* Copyright 2015-2017 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 "VersionList.h" + +#include + +#include "Version.h" +#include "tasks/RemoteLoadTask.h" +#include "tasks/LocalLoadTask.h" +#include "format/Format.h" +#include "Reference.h" + +namespace Meta +{ + +class WVLLoadTask : public Task +{ + Q_OBJECT +public: + explicit WVLLoadTask(VersionList *list, QObject *parent = nullptr) + : Task(parent), m_list(list) + { + } + + bool canAbort() const override + { + return !m_currentTask || m_currentTask->canAbort(); + } + bool abort() override + { + return m_currentTask->abort(); + } + +private: + void executeTask() override + { + if (!m_list->isLocalLoaded()) + { + m_currentTask = m_list->localUpdateTask(); + connect(m_currentTask.get(), &Task::succeeded, this, &WVLLoadTask::next); + } + else + { + m_currentTask = m_list->remoteUpdateTask(); + connect(m_currentTask.get(), &Task::succeeded, this, &WVLLoadTask::emitSucceeded); + } + connect(m_currentTask.get(), &Task::status, this, &WVLLoadTask::setStatus); + connect(m_currentTask.get(), &Task::progress, this, &WVLLoadTask::setProgress); + connect(m_currentTask.get(), &Task::failed, this, &WVLLoadTask::emitFailed); + m_currentTask->start(); + } + + void next() + { + m_currentTask = m_list->remoteUpdateTask(); + connect(m_currentTask.get(), &Task::status, this, &WVLLoadTask::setStatus); + connect(m_currentTask.get(), &Task::progress, this, &WVLLoadTask::setProgress); + connect(m_currentTask.get(), &Task::succeeded, this, &WVLLoadTask::emitSucceeded); + m_currentTask->start(); + } + + VersionList *m_list; + std::unique_ptr m_currentTask; +}; + +VersionList::VersionList(const QString &uid, QObject *parent) + : BaseVersionList(parent), m_uid(uid) +{ + setObjectName("Version list: " + uid); +} + +Task *VersionList::getLoadTask() +{ + return new WVLLoadTask(this); +} + +bool VersionList::isLoaded() +{ + return isLocalLoaded() && isRemoteLoaded(); +} + +const BaseVersionPtr VersionList::at(int i) const +{ + return m_versions.at(i); +} +int VersionList::count() const +{ + return m_versions.size(); +} + +void VersionList::sortVersions() +{ + beginResetModel(); + std::sort(m_versions.begin(), m_versions.end(), [](const VersionPtr &a, const VersionPtr &b) + { + return *a.get() < *b.get(); + }); + endResetModel(); +} + +QVariant VersionList::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() < 0 || index.row() >= m_versions.size() || index.parent().isValid()) + { + return QVariant(); + } + + VersionPtr version = m_versions.at(index.row()); + + switch (role) + { + case VersionPointerRole: return QVariant::fromValue(std::dynamic_pointer_cast(version)); + case VersionRole: + case VersionIdRole: + return version->version(); + case ParentGameVersionRole: + { + const auto end = version->requires().end(); + const auto it = std::find_if(version->requires().begin(), end, + [](const Reference &ref) { return ref.uid() == "net.minecraft"; }); + if (it != end) + { + return (*it).version(); + } + return QVariant(); + } + case TypeRole: return version->type(); + + case UidRole: return version->uid(); + case TimeRole: return version->time(); + case RequiresRole: return QVariant::fromValue(version->requires()); + case SortRole: return version->rawTime(); + case VersionPtrRole: return QVariant::fromValue(version); + case RecommendedRole: return version == getRecommended(); + case LatestRole: return version == getLatestStable(); + default: return QVariant(); + } +} + +BaseVersionList::RoleList VersionList::providesRoles() const +{ + return {VersionPointerRole, VersionRole, VersionIdRole, ParentGameVersionRole, + TypeRole, UidRole, TimeRole, RequiresRole, SortRole, + RecommendedRole, LatestRole, VersionPtrRole}; +} + +QHash VersionList::roleNames() const +{ + QHash roles = BaseVersionList::roleNames(); + roles.insert(UidRole, "uid"); + roles.insert(TimeRole, "time"); + roles.insert(SortRole, "sort"); + roles.insert(RequiresRole, "requires"); + return roles; +} + +std::unique_ptr VersionList::remoteUpdateTask() +{ + return std::unique_ptr(new VersionListRemoteLoadTask(this, this)); +} +std::unique_ptr VersionList::localUpdateTask() +{ + return std::unique_ptr(new VersionListLocalLoadTask(this, this)); +} + +QString VersionList::localFilename() const +{ + return m_uid + ".json"; +} +QJsonObject VersionList::serialized() const +{ + return Format::serializeVersionList(this); +} + +QString VersionList::humanReadable() const +{ + return m_name.isEmpty() ? m_uid : m_name; +} + +bool VersionList::hasVersion(const QString &version) const +{ + return m_lookup.contains(version); +} +VersionPtr VersionList::getVersion(const QString &version) const +{ + return m_lookup.value(version); +} + +void VersionList::setName(const QString &name) +{ + m_name = name; + emit nameChanged(name); +} +void VersionList::setVersions(const QVector &versions) +{ + beginResetModel(); + m_versions = versions; + std::sort(m_versions.begin(), m_versions.end(), [](const VersionPtr &a, const VersionPtr &b) + { + return a->rawTime() > b->rawTime(); + }); + for (int i = 0; i < m_versions.size(); ++i) + { + m_lookup.insert(m_versions.at(i)->version(), m_versions.at(i)); + setupAddedVersion(i, m_versions.at(i)); + } + + m_latest = m_versions.isEmpty() ? nullptr : m_versions.first(); + auto recommendedIt = std::find_if(m_versions.constBegin(), m_versions.constEnd(), [](const VersionPtr &ptr) { return ptr->type() == "release"; }); + m_recommended = recommendedIt == m_versions.constEnd() ? nullptr : *recommendedIt; + endResetModel(); +} + +void VersionList::merge(const BaseEntity::Ptr &other) +{ + const VersionListPtr list = std::dynamic_pointer_cast(other); + if (m_name != list->m_name) + { + setName(list->m_name); + } + + if (m_versions.isEmpty()) + { + setVersions(list->m_versions); + } + else + { + for (const VersionPtr &version : list->m_versions) + { + if (m_lookup.contains(version->version())) + { + m_lookup.value(version->version())->merge(version); + } + else + { + beginInsertRows(QModelIndex(), m_versions.size(), m_versions.size()); + setupAddedVersion(m_versions.size(), version); + m_versions.append(version); + m_lookup.insert(version->uid(), version); + endInsertRows(); + + if (!m_latest || version->rawTime() > m_latest->rawTime()) + { + m_latest = version; + emit dataChanged(index(0), index(m_versions.size() - 1), QVector() << LatestRole); + } + if (!m_recommended || (version->type() == "release" && version->rawTime() > m_recommended->rawTime())) + { + m_recommended = version; + emit dataChanged(index(0), index(m_versions.size() - 1), QVector() << RecommendedRole); + } + } + } + } +} + +void VersionList::setupAddedVersion(const int row, const VersionPtr &version) +{ + connect(version.get(), &Version::requiresChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector() << RequiresRole); }); + connect(version.get(), &Version::timeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector() << TimeRole << SortRole); }); + connect(version.get(), &Version::typeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector() << TypeRole); }); +} + +BaseVersionPtr VersionList::getLatestStable() const +{ + return m_latest; +} +BaseVersionPtr VersionList::getRecommended() const +{ + return m_recommended; +} + +} + +#include "VersionList.moc" diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h new file mode 100644 index 00000000..0eb85bf8 --- /dev/null +++ b/api/logic/meta/VersionList.h @@ -0,0 +1,95 @@ +/* Copyright 2015-2017 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 + +#include "BaseVersionList.h" +#include "BaseEntity.h" +#include + +namespace Meta +{ +using VersionPtr = std::shared_ptr; +using VersionListPtr = std::shared_ptr; + +class MULTIMC_LOGIC_EXPORT VersionList : public BaseVersionList, public BaseEntity +{ + Q_OBJECT + Q_PROPERTY(QString uid READ uid CONSTANT) + Q_PROPERTY(QString name READ name NOTIFY nameChanged) +public: + explicit VersionList(const QString &uid, QObject *parent = nullptr); + + enum Roles + { + UidRole = Qt::UserRole + 100, + TimeRole, + RequiresRole, + VersionPtrRole + }; + + Task *getLoadTask() override; + bool isLoaded() override; + const BaseVersionPtr at(int i) const override; + int count() const override; + void sortVersions() override; + + BaseVersionPtr getLatestStable() const override; + BaseVersionPtr getRecommended() const override; + + QVariant data(const QModelIndex &index, int role) const override; + RoleList providesRoles() const override; + QHash roleNames() const override; + + std::unique_ptr remoteUpdateTask() override; + std::unique_ptr localUpdateTask() override; + + QString localFilename() const override; + QJsonObject serialized() const override; + + QString uid() const { return m_uid; } + QString name() const { return m_name; } + QString humanReadable() const; + + bool hasVersion(const QString &version) const; + VersionPtr getVersion(const QString &version) const; + + QVector versions() const { return m_versions; } + +public: // for usage only by parsers + void setName(const QString &name); + void setVersions(const QVector &versions); + void merge(const BaseEntity::Ptr &other) override; + +signals: + void nameChanged(const QString &name); + +protected slots: + void updateListData(QList) override {} + +private: + QVector m_versions; + QHash m_lookup; + QString m_uid; + QString m_name; + + VersionPtr m_recommended; + VersionPtr m_latest; + + void setupAddedVersion(const int row, const VersionPtr &version); +}; + +} +Q_DECLARE_METATYPE(Meta::VersionListPtr) diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp new file mode 100644 index 00000000..a3f9f730 --- /dev/null +++ b/api/logic/meta/format/Format.cpp @@ -0,0 +1,84 @@ +/* Copyright 2015-2017 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 "Format.h" + +#include "FormatV1.h" + +#include "meta/Index.h" +#include "meta/Version.h" +#include "meta/VersionList.h" + +namespace Meta +{ + +static int formatVersion(const QJsonObject &obj) +{ + if (!obj.contains("formatVersion")) { + throw ParseException(QObject::tr("Missing required field: 'formatVersion'")); + } + if (!obj.value("formatVersion").isDouble()) { + throw ParseException(QObject::tr("Required field has invalid type: 'formatVersion'")); + } + return obj.value("formatVersion").toInt(); +} + +void Format::parseIndex(const QJsonObject &obj, Index *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 1: + ptr->merge(FormatV1().parseIndexInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} +void Format::parseVersion(const QJsonObject &obj, Version *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 1: + ptr->merge(FormatV1().parseVersionInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} +void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 10: + ptr->merge(FormatV1().parseVersionListInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} + +QJsonObject Format::serializeIndex(const Index *ptr) +{ + return FormatV1().serializeIndexInternal(ptr); +} +QJsonObject Format::serializeVersion(const Version *ptr) +{ + return FormatV1().serializeVersionInternal(ptr); +} +QJsonObject Format::serializeVersionList(const VersionList *ptr) +{ + return FormatV1().serializeVersionListInternal(ptr); +} +} diff --git a/api/logic/meta/format/Format.h b/api/logic/meta/format/Format.h new file mode 100644 index 00000000..79605a6f --- /dev/null +++ b/api/logic/meta/format/Format.h @@ -0,0 +1,57 @@ +/* Copyright 2015-2017 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 + +#include +#include + +#include "Exception.h" +#include "meta/BaseEntity.h" + +namespace Meta +{ +class Index; +class Version; +class VersionList; + +class ParseException : public Exception +{ +public: + using Exception::Exception; +}; + +class Format +{ +public: + virtual ~Format() {} + + static void parseIndex(const QJsonObject &obj, Index *ptr); + static void parseVersion(const QJsonObject &obj, Version *ptr); + static void parseVersionList(const QJsonObject &obj, VersionList *ptr); + + static QJsonObject serializeIndex(const Index *ptr); + static QJsonObject serializeVersion(const Version *ptr); + static QJsonObject serializeVersionList(const VersionList *ptr); + +protected: + virtual BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const = 0; + virtual BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const = 0; + virtual BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const = 0; + virtual QJsonObject serializeIndexInternal(const Index *ptr) const = 0; + virtual QJsonObject serializeVersionInternal(const Version *ptr) const = 0; + virtual QJsonObject serializeVersionListInternal(const VersionList *ptr) const = 0; +}; +} diff --git a/api/logic/meta/format/FormatV1.cpp b/api/logic/meta/format/FormatV1.cpp new file mode 100644 index 00000000..23f57100 --- /dev/null +++ b/api/logic/meta/format/FormatV1.cpp @@ -0,0 +1,161 @@ +/* Copyright 2015-2017 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 "FormatV1.h" +#include + +#include "Json.h" + +#include "meta/Index.h" +#include "meta/Version.h" +#include "meta/VersionList.h" +#include "Env.h" + +using namespace Json; + +namespace Meta +{ +static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) +{ + const QVector requiresRaw = obj.contains("requires") ? requireIsArrayOf(obj, "requires") : QVector(); + QVector requires; + requires.reserve(requiresRaw.size()); + std::transform(requiresRaw.begin(), requiresRaw.end(), std::back_inserter(requires), [](const QJsonObject &rObj) + { + Reference ref(requireString(rObj, "uid")); + ref.setVersion(ensureString(rObj, "version", QString())); + return ref; + }); + + VersionPtr version = std::make_shared(uid, requireString(obj, "version")); + if (obj.value("time").isString()) + { + version->setTime(QDateTime::fromString(requireString(obj, "time"), Qt::ISODate).toMSecsSinceEpoch() / 1000); + } + else + { + version->setTime(requireInteger(obj, "time")); + } + version->setType(ensureString(obj, "type", QString())); + version->setRequires(requires); + return version; +} +static void serializeCommonVersion(const Version *version, QJsonObject &obj) +{ + QJsonArray requires; + for (const Reference &ref : version->requires()) + { + if (ref.version().isEmpty()) + { + QJsonObject out; + out["uid"] = ref.uid(); + requires.append(out); + } + else + { + QJsonObject out; + out["uid"] = ref.uid(); + out["version"] = ref.version(); + requires.append(out); + } + } + + obj.insert("version", version->version()); + obj.insert("type", version->type()); + obj.insert("time", version->time().toString(Qt::ISODate)); + obj.insert("requires", requires); +} + +BaseEntity::Ptr FormatV1::parseIndexInternal(const QJsonObject &obj) const +{ + const QVector objects = requireIsArrayOf(obj, "index"); + QVector lists; + lists.reserve(objects.size()); + std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject &obj) + { + VersionListPtr list = std::make_shared(requireString(obj, "uid")); + list->setName(ensureString(obj, "name", QString())); + return list; + }); + return std::make_shared(lists); +} +BaseEntity::Ptr FormatV1::parseVersionInternal(const QJsonObject &obj) const +{ + VersionPtr version = parseCommonVersion(requireString(obj, "uid"), obj); + + version->setData(OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj), + QString("%1/%2.json").arg(version->uid(), version->version()), + obj.contains("order"))); + return version; +} +BaseEntity::Ptr FormatV1::parseVersionListInternal(const QJsonObject &obj) const +{ + const QString uid = requireString(obj, "uid"); + + const QVector versionsRaw = requireIsArrayOf(obj, "versions"); + QVector versions; + versions.reserve(versionsRaw.size()); + std::transform(versionsRaw.begin(), versionsRaw.end(), std::back_inserter(versions), [this, uid](const QJsonObject &vObj) + { return parseCommonVersion(uid, vObj); }); + + VersionListPtr list = std::make_shared(uid); + list->setName(ensureString(obj, "name", QString())); + list->setVersions(versions); + return list; +} + +QJsonObject FormatV1::serializeIndexInternal(const Index *ptr) const +{ + QJsonArray index; + for (const VersionListPtr &list : ptr->lists()) + { + QJsonObject out; + out["uid"] = list->uid(); + out["version"] = list->name(); + index.append(out); + } + QJsonObject out; + out["formatVersion"] = 1; + out["index"] = index; + return out; +} +QJsonObject FormatV1::serializeVersionInternal(const Version *ptr) const +{ + QJsonObject obj = OneSixVersionFormat::versionFileToJson(ptr->data(), true).object(); + serializeCommonVersion(ptr, obj); + obj.insert("formatVersion", 1); + obj.insert("uid", ptr->uid()); + // TODO: the name should be looked up in the UI based on the uid + obj.insert("name", ENV.metadataIndex()->getListGuaranteed(ptr->uid())->name()); + + return obj; +} +QJsonObject FormatV1::serializeVersionListInternal(const VersionList *ptr) const +{ + QJsonArray versions; + for (const VersionPtr &version : ptr->versions()) + { + QJsonObject obj; + serializeCommonVersion(version.get(), obj); + versions.append(obj); + } + QJsonObject out; + out["formatVersion"] = 10; + out["uid"] = ptr->uid(); + out["name"] = ptr->name().isNull() ? QJsonValue() : ptr->name(); + out["versions"] = versions; + return out; +} +} diff --git a/api/logic/meta/format/FormatV1.h b/api/logic/meta/format/FormatV1.h new file mode 100644 index 00000000..ab986083 --- /dev/null +++ b/api/logic/meta/format/FormatV1.h @@ -0,0 +1,33 @@ +/* Copyright 2015-2017 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 + +#include "Format.h" + +namespace Meta +{ +class FormatV1 : public Format +{ +public: + BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const override; + BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const override; + BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const override; + + QJsonObject serializeIndexInternal(const Index *ptr) const override; + QJsonObject serializeVersionInternal(const Version *ptr) const override; + QJsonObject serializeVersionListInternal(const VersionList *ptr) const override; +}; +} diff --git a/api/logic/meta/tasks/LocalLoadTask.cpp b/api/logic/meta/tasks/LocalLoadTask.cpp new file mode 100644 index 00000000..33ea8016 --- /dev/null +++ b/api/logic/meta/tasks/LocalLoadTask.cpp @@ -0,0 +1,123 @@ +/* Copyright 2015-2017 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 "LocalLoadTask.h" + +#include + +#include "meta/format/Format.h" +#include "meta/Util.h" +#include "meta/Index.h" +#include "meta/Version.h" +#include "meta/VersionList.h" +#include "Env.h" +#include "Json.h" + +namespace Meta +{ +LocalLoadTask::LocalLoadTask(BaseEntity *entity, QObject *parent) + : Task(parent), m_entity(entity) +{ +} + +void LocalLoadTask::executeTask() +{ + const QString fname = Meta::localDir().absoluteFilePath(filename()); + if (!QFile::exists(fname)) + { + emitFailed(tr("File doesn't exist")); + return; + } + + setStatus(tr("Reading %1...").arg(name())); + setProgress(0, 0); + + try + { + parse(Json::requireObject(Json::requireDocument(fname, name()), name())); + m_entity->notifyLocalLoadComplete(); + emitSucceeded(); + } + catch (Exception &e) + { + emitFailed(tr("Unable to parse file %1: %2").arg(fname, e.cause())); + } +} + + +// INDEX +IndexLocalLoadTask::IndexLocalLoadTask(Index *index, QObject *parent) + : LocalLoadTask(index, parent) +{ +} +QString IndexLocalLoadTask::filename() const +{ + return "index.json"; +} +QString IndexLocalLoadTask::name() const +{ + return tr("Metadata Index"); +} +void IndexLocalLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseIndex(obj, dynamic_cast(entity())); +} + + +// VERSION LIST +VersionListLocalLoadTask::VersionListLocalLoadTask(VersionList *list, QObject *parent) + : LocalLoadTask(list, parent) +{ +} +QString VersionListLocalLoadTask::filename() const +{ + return list()->uid() + ".json"; +} +QString VersionListLocalLoadTask::name() const +{ + return tr("Version List for %1").arg(list()->humanReadable()); +} +void VersionListLocalLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseVersionList(obj, list()); +} +VersionList *VersionListLocalLoadTask::list() const +{ + return dynamic_cast(entity()); +} + + +// VERSION +VersionLocalLoadTask::VersionLocalLoadTask(Version *version, QObject *parent) + : LocalLoadTask(version, parent) +{ +} +QString VersionLocalLoadTask::filename() const +{ + return version()->uid() + "/" + version()->version() + ".json"; +} +QString VersionLocalLoadTask::name() const +{ + return tr(" Version for %1").arg(version()->name()); +} +void VersionLocalLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseVersion(obj, version()); +} +Version *VersionLocalLoadTask::version() const +{ + return dynamic_cast(entity()); +} +} diff --git a/api/logic/meta/tasks/LocalLoadTask.h b/api/logic/meta/tasks/LocalLoadTask.h new file mode 100644 index 00000000..36ae1b6f --- /dev/null +++ b/api/logic/meta/tasks/LocalLoadTask.h @@ -0,0 +1,84 @@ +/* Copyright 2015-2017 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 + +#include "tasks/Task.h" +#include + +namespace Meta +{ +class BaseEntity; +class Index; +class VersionList; +class Version; + +class LocalLoadTask : public Task +{ + Q_OBJECT +public: + explicit LocalLoadTask(BaseEntity *entity, QObject *parent = nullptr); + +protected: + virtual QString filename() const = 0; + virtual QString name() const = 0; + virtual void parse(const QJsonObject &obj) const = 0; + + BaseEntity *entity() const { return m_entity; } + +private: + void executeTask() override; + + BaseEntity *m_entity; +}; + +class IndexLocalLoadTask : public LocalLoadTask +{ + Q_OBJECT +public: + explicit IndexLocalLoadTask(Index *index, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; +}; +class VersionListLocalLoadTask : public LocalLoadTask +{ + Q_OBJECT +public: + explicit VersionListLocalLoadTask(VersionList *list, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + VersionList *list() const; +}; +class VersionLocalLoadTask : public LocalLoadTask +{ + Q_OBJECT +public: + explicit VersionLocalLoadTask(Version *version, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + Version *version() const; +}; +} diff --git a/api/logic/meta/tasks/RemoteLoadTask.cpp b/api/logic/meta/tasks/RemoteLoadTask.cpp new file mode 100644 index 00000000..69a59c07 --- /dev/null +++ b/api/logic/meta/tasks/RemoteLoadTask.cpp @@ -0,0 +1,132 @@ +/* Copyright 2015-2017 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 "RemoteLoadTask.h" + +#include "net/Download.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" +#include "meta/format/Format.h" +#include "meta/Util.h" +#include "meta/Index.h" +#include "meta/Version.h" +#include "meta/VersionList.h" +#include "Env.h" +#include "Json.h" + +namespace Meta +{ + +RemoteLoadTask::RemoteLoadTask(BaseEntity *entity, QObject *parent) + : Task(parent), m_entity(entity) +{ +} + +void RemoteLoadTask::executeTask() +{ + NetJob *job = new NetJob(name()); + + auto entry = ENV.metacache()->resolveEntry("meta", url().toString()); + entry->setStale(true); + m_dl = Net::Download::makeCached(url(), entry); + job->addNetAction(m_dl); + connect(job, &NetJob::failed, this, &RemoteLoadTask::emitFailed); + connect(job, &NetJob::succeeded, this, &RemoteLoadTask::networkFinished); + connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); + connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); + job->start(); +} + +void RemoteLoadTask::networkFinished() +{ + setStatus(tr("Parsing...")); + setProgress(0, 0); + + try + { + parse(Json::requireObject(Json::requireDocument(m_dl->getTargetFilepath(), name()), name())); + m_entity->notifyRemoteLoadComplete(); + emitSucceeded(); + } + catch (Exception &e) + { + emitFailed(tr("Unable to parse response: %1").arg(e.cause())); + } +} + +// INDEX +IndexRemoteLoadTask::IndexRemoteLoadTask(Index *index, QObject *parent) + : RemoteLoadTask(index, parent) +{ +} +QUrl IndexRemoteLoadTask::url() const +{ + return Meta::indexUrl(); +} +QString IndexRemoteLoadTask::name() const +{ + return tr("Metadata Index"); +} +void IndexRemoteLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseIndex(obj, dynamic_cast(entity())); +} + + +// VERSION LIST +VersionListRemoteLoadTask::VersionListRemoteLoadTask(VersionList *list, QObject *parent) + : RemoteLoadTask(list, parent) +{ +} +QUrl VersionListRemoteLoadTask::url() const +{ + return Meta::versionListUrl(list()->uid()); +} +QString VersionListRemoteLoadTask::name() const +{ + return tr("Version List for %1").arg(list()->humanReadable()); +} +void VersionListRemoteLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseVersionList(obj, list()); +} +VersionList* Meta::VersionListRemoteLoadTask::list() const +{ + return dynamic_cast(entity()); +} + + +// VERSION +VersionRemoteLoadTask::VersionRemoteLoadTask(Version *version, QObject *parent) + : RemoteLoadTask(version, parent) +{ +} +QUrl VersionRemoteLoadTask::url() const +{ + return Meta::versionUrl(version()->uid(), version()->version()); +} +QString VersionRemoteLoadTask::name() const +{ + return tr("Meta Version for %1").arg(version()->name()); +} +void VersionRemoteLoadTask::parse(const QJsonObject &obj) const +{ + Format::parseVersion(obj, version()); +} +Version *VersionRemoteLoadTask::version() const +{ + return dynamic_cast(entity()); +} +} diff --git a/api/logic/meta/tasks/RemoteLoadTask.h b/api/logic/meta/tasks/RemoteLoadTask.h new file mode 100644 index 00000000..3b09404f --- /dev/null +++ b/api/logic/meta/tasks/RemoteLoadTask.h @@ -0,0 +1,95 @@ +/* Copyright 2015-2017 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 + +#include "tasks/Task.h" +#include + +namespace Net +{ +class Download; +} + +namespace Meta +{ +class BaseEntity; +class Index; +class VersionList; +class Version; + +class RemoteLoadTask : public Task +{ + Q_OBJECT +public: + explicit RemoteLoadTask(BaseEntity *entity, QObject *parent = nullptr); + +protected: + virtual QUrl url() const = 0; + virtual QString name() const = 0; + virtual void parse(const QJsonObject &obj) const = 0; + + BaseEntity *entity() const { return m_entity; } + +private slots: + void networkFinished(); + +private: + void executeTask() override; + + BaseEntity *m_entity; + std::shared_ptr m_dl; +}; + +class IndexRemoteLoadTask : public RemoteLoadTask +{ + Q_OBJECT +public: + explicit IndexRemoteLoadTask(Index *index, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; +}; + +class VersionListRemoteLoadTask : public RemoteLoadTask +{ + Q_OBJECT +public: + explicit VersionListRemoteLoadTask(VersionList *list, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + VersionList *list() const; +}; + +class VersionRemoteLoadTask : public RemoteLoadTask +{ + Q_OBJECT +public: + explicit VersionRemoteLoadTask(Version *version, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + Version *version() const; +}; +} -- cgit v1.2.3