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 From 8321187a20eec9871f327aa23f421e79dcfdd409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 12 Mar 2017 16:00:06 +0100 Subject: NOISSUE fix downloading of metadata files --- api/logic/meta/Util.cpp | 2 +- api/logic/meta/VersionList.cpp | 2 +- api/logic/meta/format/Format.cpp | 28 +++++---- api/logic/meta/format/FormatV1.cpp | 119 ++++++++++++++++++++----------------- 4 files changed, 83 insertions(+), 68 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/Util.cpp b/api/logic/meta/Util.cpp index 2ccbe5c4..76941f83 100644 --- a/api/logic/meta/Util.cpp +++ b/api/logic/meta/Util.cpp @@ -34,7 +34,7 @@ QUrl indexUrl() QUrl versionListUrl(const QString &uid) { - return rootUrl().resolved(uid + ".json"); + return rootUrl().resolved(uid + "/index.json"); } QUrl versionUrl(const QString &uid, const QString &version) diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index 11e20c12..aab3e0f7 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -178,7 +178,7 @@ std::unique_ptr VersionList::localUpdateTask() QString VersionList::localFilename() const { - return m_uid + ".json"; + return m_uid + "/index.json"; } QJsonObject VersionList::serialized() const { diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp index a3f9f730..e0fddb0b 100644 --- a/api/logic/meta/format/Format.cpp +++ b/api/logic/meta/format/Format.cpp @@ -39,46 +39,52 @@ void Format::parseIndex(const QJsonObject &obj, Index *ptr) { const int version = formatVersion(obj); switch (version) { - case 1: + case 0: ptr->merge(FormatV1().parseIndexInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersion(const QJsonObject &obj, Version *ptr) + +void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) { const int version = formatVersion(obj); switch (version) { - case 1: - ptr->merge(FormatV1().parseVersionInternal(obj)); + case 0: + ptr->merge(FormatV1().parseVersionListInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) + +void Format::parseVersion(const QJsonObject &obj, Version *ptr) { const int version = formatVersion(obj); switch (version) { - case 10: - ptr->merge(FormatV1().parseVersionListInternal(obj)); + case 0: + ptr->merge(FormatV1().parseVersionInternal(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); } + +QJsonObject Format::serializeVersion(const Version *ptr) +{ + return FormatV1().serializeVersionInternal(ptr); +} + } diff --git a/api/logic/meta/format/FormatV1.cpp b/api/logic/meta/format/FormatV1.cpp index 23f57100..ee2c5291 100644 --- a/api/logic/meta/format/FormatV1.cpp +++ b/api/logic/meta/format/FormatV1.cpp @@ -27,6 +27,44 @@ using namespace Json; namespace Meta { + +static const int currentFormatVersion = 0; + +// Index + +BaseEntity::Ptr FormatV1::parseIndexInternal(const QJsonObject &obj) const +{ + const QVector objects = requireIsArrayOf(obj, "packages"); + 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); +} + +QJsonObject FormatV1::serializeIndexInternal(const Index *ptr) const +{ + QJsonArray packages; + for (const VersionListPtr &list : ptr->lists()) + { + QJsonObject out; + out["uid"] = list->uid(); + out["name"] = list->name(); + packages.append(out); + } + QJsonObject out; + out["formatVersion"] = currentFormatVersion; + out["packages"] = packages; + return out; +} + + +// Version + static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) { const QVector requiresRaw = obj.contains("requires") ? requireIsArrayOf(obj, "requires") : QVector(); @@ -40,18 +78,22 @@ static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) }); 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->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); version->setType(ensureString(obj, "type", QString())); version->setRequires(requires); return version; } + +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; +} + static void serializeCommonVersion(const Version *version, QJsonObject &obj) { QJsonArray requires; @@ -74,32 +116,25 @@ static void serializeCommonVersion(const Version *version, QJsonObject &obj) obj.insert("version", version->version()); obj.insert("type", version->type()); - obj.insert("time", version->time().toString(Qt::ISODate)); + obj.insert("releaseTime", 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 +QJsonObject FormatV1::serializeVersionInternal(const Version *ptr) const { - VersionPtr version = parseCommonVersion(requireString(obj, "uid"), obj); + QJsonObject obj = OneSixVersionFormat::versionFileToJson(ptr->data(), true).object(); + serializeCommonVersion(ptr, obj); + obj.insert("formatVersion", currentFormatVersion); + 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()); - version->setData(OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj), - QString("%1/%2.json").arg(version->uid(), version->version()), - obj.contains("order"))); - return version; + return obj; } + + +// Version list / package + BaseEntity::Ptr FormatV1::parseVersionListInternal(const QJsonObject &obj) const { const QString uid = requireString(obj, "uid"); @@ -116,32 +151,6 @@ BaseEntity::Ptr FormatV1::parseVersionListInternal(const QJsonObject &obj) const 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; @@ -152,7 +161,7 @@ QJsonObject FormatV1::serializeVersionListInternal(const VersionList *ptr) const versions.append(obj); } QJsonObject out; - out["formatVersion"] = 10; + out["formatVersion"] = currentFormatVersion; out["uid"] = ptr->uid(); out["name"] = ptr->name().isNull() ? QJsonValue() : ptr->name(); out["versions"] = versions; -- cgit v1.2.3 From 0060b506257b906d40ef53d1e23404dba76afcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Fri, 17 Mar 2017 01:48:54 +0100 Subject: NOISSUE simplify. --- api/logic/meta/BaseEntity.cpp | 7 +- api/logic/meta/BaseEntity.h | 5 +- api/logic/meta/Index.cpp | 27 +++-- api/logic/meta/Index.h | 7 +- api/logic/meta/Index_test.cpp | 6 +- api/logic/meta/Util.h | 3 - api/logic/meta/Version.cpp | 13 +-- api/logic/meta/Version.h | 2 +- api/logic/meta/VersionList.cpp | 14 +-- api/logic/meta/VersionList.h | 3 +- api/logic/meta/format/Format.cpp | 101 ++++++++++++++----- api/logic/meta/format/Format.h | 23 +---- api/logic/meta/format/FormatV1.cpp | 170 -------------------------------- api/logic/meta/format/FormatV1.h | 33 ------- api/logic/meta/tasks/LocalLoadTask.cpp | 72 +------------- api/logic/meta/tasks/LocalLoadTask.h | 47 +-------- api/logic/meta/tasks/RemoteLoadTask.cpp | 139 +++++++++++--------------- api/logic/meta/tasks/RemoteLoadTask.h | 51 +--------- 18 files changed, 190 insertions(+), 533 deletions(-) delete mode 100644 api/logic/meta/format/FormatV1.cpp delete mode 100644 api/logic/meta/format/FormatV1.h (limited to 'api/logic/meta') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index fd44e29c..87cd55c8 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -24,19 +24,18 @@ BaseEntity::~BaseEntity() { } -void BaseEntity::store() const +QUrl BaseEntity::url() const { - Json::write(serialized(), Meta::localDir().absoluteFilePath(localFilename())); + return rootUrl().resolved(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 index b7a241c6..7064e9d2 100644 --- a/api/logic/meta/BaseEntity.h +++ b/api/logic/meta/BaseEntity.h @@ -17,6 +17,7 @@ #include #include +#include #include "multimc_logic_export.h" @@ -33,10 +34,10 @@ public: virtual std::unique_ptr remoteUpdateTask() = 0; virtual std::unique_ptr localUpdateTask() = 0; virtual void merge(const std::shared_ptr &other) = 0; + virtual void parse(const QJsonObject &obj) = 0; - void store() const; virtual QString localFilename() const = 0; - virtual QJsonObject serialized() const = 0; + virtual QUrl url() const; bool isComplete() const { return m_localLoaded || m_remoteLoaded; } diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp index 8a6b1355..e0e8bc5d 100644 --- a/api/logic/meta/Index.cpp +++ b/api/logic/meta/Index.cpp @@ -80,29 +80,36 @@ QVariant Index::headerData(int section, Qt::Orientation orientation, int role) c std::unique_ptr Index::remoteUpdateTask() { - return std::unique_ptr(new IndexRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr Index::localUpdateTask() { - return std::unique_ptr(new IndexLocalLoadTask(this, this)); -} - -QJsonObject Index::serialized() const -{ - return Format::serializeIndex(this); + return std::unique_ptr(new LocalLoadTask(this)); } bool Index::hasUid(const QString &uid) const { return m_uids.contains(uid); } -VersionListPtr Index::getList(const QString &uid) const + +VersionListPtr Index::get(const QString &uid) { return m_uids.value(uid, nullptr); } -VersionListPtr Index::getListGuaranteed(const QString &uid) const + +VersionPtr Index::get(const QString &uid, const QString &version) +{ + auto list = get(uid); + if(list) + { + return list->getVersion(version); + } + return nullptr; +} + +void Index::parse(const QJsonObject& obj) { - return m_uids.value(uid, std::make_shared(uid)); + parseIndex(obj, this); } void Index::merge(const Ptr &other) diff --git a/api/logic/meta/Index.h b/api/logic/meta/Index.h index 16e95d5a..37341656 100644 --- a/api/logic/meta/Index.h +++ b/api/logic/meta/Index.h @@ -27,6 +27,7 @@ class Task; namespace Meta { using VersionListPtr = std::shared_ptr; +using VersionPtr = std::shared_ptr; class MULTIMC_LOGIC_EXPORT Index : public QAbstractListModel, public BaseEntity { @@ -51,17 +52,21 @@ public: std::unique_ptr localUpdateTask() override; QString localFilename() const override { return "index.json"; } - QJsonObject serialized() const override; // queries + VersionListPtr get(const QString &uid); + VersionPtr get(const QString &uid, const QString &version); 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; + void parse(const QJsonObject &obj) override; private: QVector m_lists; diff --git a/api/logic/meta/Index_test.cpp b/api/logic/meta/Index_test.cpp index d26700ca..b4dbd009 100644 --- a/api/logic/meta/Index_test.cpp +++ b/api/logic/meta/Index_test.cpp @@ -27,9 +27,9 @@ slots: 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); + QVERIFY(windex.get("list2") != nullptr); + QCOMPARE(windex.get("list2")->uid(), QString("list2")); + QVERIFY(windex.get("adsf") == nullptr); } void test_merge() diff --git a/api/logic/meta/Util.h b/api/logic/meta/Util.h index 28163fe4..188bcaa3 100644 --- a/api/logic/meta/Util.h +++ b/api/logic/meta/Util.h @@ -24,8 +24,5 @@ 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 index 039f4fef..b79c178a 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -48,11 +48,16 @@ QDateTime Version::time() const std::unique_ptr Version::remoteUpdateTask() { - return std::unique_ptr(new VersionRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr Version::localUpdateTask() { - return std::unique_ptr(new VersionLocalLoadTask(this, this)); + return std::unique_ptr(new LocalLoadTask(this)); +} + +void Version::parse(const QJsonObject& obj) +{ + parseVersion(obj, this); } void Version::merge(const std::shared_ptr &other) @@ -78,10 +83,6 @@ QString Version::localFilename() const { return m_uid + '/' + m_version + ".json"; } -QJsonObject Version::serialized() const -{ - return Format::serializeVersion(this); -} void Version::setType(const QString &type) { diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 4a791880..635b79f6 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -59,9 +59,9 @@ public: std::unique_ptr remoteUpdateTask() override; std::unique_ptr localUpdateTask() override; void merge(const std::shared_ptr &other) override; + void parse(const QJsonObject &obj) override; QString localFilename() const override; - QJsonObject serialized() const override; public: // for usage by format parsers only void setType(const QString &type); diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index aab3e0f7..7196d4be 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -169,21 +169,17 @@ QHash VersionList::roleNames() const std::unique_ptr VersionList::remoteUpdateTask() { - return std::unique_ptr(new VersionListRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr VersionList::localUpdateTask() { - return std::unique_ptr(new VersionListLocalLoadTask(this, this)); + return std::unique_ptr(new LocalLoadTask(this)); } QString VersionList::localFilename() const { return m_uid + "/index.json"; } -QJsonObject VersionList::serialized() const -{ - return Format::serializeVersionList(this); -} QString VersionList::humanReadable() const { @@ -224,6 +220,11 @@ void VersionList::setVersions(const QVector &versions) endResetModel(); } +void VersionList::parse(const QJsonObject& obj) +{ + parseVersionList(obj, this); +} + void VersionList::merge(const BaseEntity::Ptr &other) { const VersionListPtr list = std::dynamic_pointer_cast(other); @@ -285,4 +286,5 @@ BaseVersionPtr VersionList::getRecommended() const } + #include "VersionList.moc" diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index 0eb85bf8..934b20e4 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -17,6 +17,7 @@ #include "BaseVersionList.h" #include "BaseEntity.h" +#include #include namespace Meta @@ -57,7 +58,6 @@ public: 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; } @@ -72,6 +72,7 @@ public: // for usage only by parsers void setName(const QString &name); void setVersions(const QVector &versions); void merge(const BaseEntity::Ptr &other) override; + void parse(const QJsonObject &obj) override; signals: void nameChanged(const QString &name); diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp index e0fddb0b..39d3f14f 100644 --- a/api/logic/meta/format/Format.cpp +++ b/api/logic/meta/format/Format.cpp @@ -15,15 +15,85 @@ #include "Format.h" -#include "FormatV1.h" +#include "minecraft/onesix/OneSixVersionFormat.h"" #include "meta/Index.h" #include "meta/Version.h" #include "meta/VersionList.h" +#include "Json.h" +using namespace Json; + namespace Meta { +static const int currentFormatVersion = 0; + +// Index +static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) +{ + const QVector objects = requireIsArrayOf(obj, "packages"); + 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); +} + +// Version +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")); + version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); + version->setType(ensureString(obj, "type", QString())); + version->setRequires(requires); + return version; +} + +static BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) +{ + 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; +} + +// Version list / package +static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) +{ + 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), [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; +} + + static int formatVersion(const QJsonObject &obj) { if (!obj.contains("formatVersion")) { @@ -35,56 +105,39 @@ static int formatVersion(const QJsonObject &obj) return obj.value("formatVersion").toInt(); } -void Format::parseIndex(const QJsonObject &obj, Index *ptr) +void parseIndex(const QJsonObject &obj, Index *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseIndexInternal(obj)); + ptr->merge(parseIndexInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) +void parseVersionList(const QJsonObject &obj, VersionList *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionListInternal(obj)); + ptr->merge(parseVersionListInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersion(const QJsonObject &obj, Version *ptr) +void parseVersion(const QJsonObject &obj, Version *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionInternal(obj)); + ptr->merge(parseVersionInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } - - -QJsonObject Format::serializeIndex(const Index *ptr) -{ - return FormatV1().serializeIndexInternal(ptr); -} - -QJsonObject Format::serializeVersionList(const VersionList *ptr) -{ - return FormatV1().serializeVersionListInternal(ptr); -} - -QJsonObject Format::serializeVersion(const Version *ptr) -{ - return FormatV1().serializeVersionInternal(ptr); -} - } diff --git a/api/logic/meta/format/Format.h b/api/logic/meta/format/Format.h index 79605a6f..aaed07fc 100644 --- a/api/logic/meta/format/Format.h +++ b/api/logic/meta/format/Format.h @@ -33,25 +33,8 @@ 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); +void parseIndex(const QJsonObject &obj, Index *ptr); +void parseVersion(const QJsonObject &obj, Version *ptr); +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 deleted file mode 100644 index ee2c5291..00000000 --- a/api/logic/meta/format/FormatV1.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* 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 const int currentFormatVersion = 0; - -// Index - -BaseEntity::Ptr FormatV1::parseIndexInternal(const QJsonObject &obj) const -{ - const QVector objects = requireIsArrayOf(obj, "packages"); - 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); -} - -QJsonObject FormatV1::serializeIndexInternal(const Index *ptr) const -{ - QJsonArray packages; - for (const VersionListPtr &list : ptr->lists()) - { - QJsonObject out; - out["uid"] = list->uid(); - out["name"] = list->name(); - packages.append(out); - } - QJsonObject out; - out["formatVersion"] = currentFormatVersion; - out["packages"] = packages; - return out; -} - - -// Version - -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")); - version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); - version->setType(ensureString(obj, "type", QString())); - version->setRequires(requires); - return version; -} - -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; -} - -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("releaseTime", version->time().toString(Qt::ISODate)); - obj.insert("requires", requires); -} - -QJsonObject FormatV1::serializeVersionInternal(const Version *ptr) const -{ - QJsonObject obj = OneSixVersionFormat::versionFileToJson(ptr->data(), true).object(); - serializeCommonVersion(ptr, obj); - obj.insert("formatVersion", currentFormatVersion); - 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; -} - - -// Version list / package - -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::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"] = currentFormatVersion; - 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 deleted file mode 100644 index ab986083..00000000 --- a/api/logic/meta/format/FormatV1.h +++ /dev/null @@ -1,33 +0,0 @@ -/* 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 index 33ea8016..b64fc5d3 100644 --- a/api/logic/meta/tasks/LocalLoadTask.cpp +++ b/api/logic/meta/tasks/LocalLoadTask.cpp @@ -34,19 +34,18 @@ LocalLoadTask::LocalLoadTask(BaseEntity *entity, QObject *parent) void LocalLoadTask::executeTask() { - const QString fname = Meta::localDir().absoluteFilePath(filename()); + const QString fname = Meta::localDir().absoluteFilePath(m_entity->localFilename()); if (!QFile::exists(fname)) { emitFailed(tr("File doesn't exist")); return; } - - setStatus(tr("Reading %1...").arg(name())); + setStatus(tr("Reading %1...").arg(fname)); setProgress(0, 0); try { - parse(Json::requireObject(Json::requireDocument(fname, name()), name())); + m_entity->parse(Json::requireObject(Json::requireDocument(fname, fname), fname)); m_entity->notifyLocalLoadComplete(); emitSucceeded(); } @@ -55,69 +54,4 @@ void LocalLoadTask::executeTask() 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 index 36ae1b6f..905660ed 100644 --- a/api/logic/meta/tasks/LocalLoadTask.h +++ b/api/logic/meta/tasks/LocalLoadTask.h @@ -25,60 +25,15 @@ class Index; class VersionList; class Version; +// FIXME: this is now just an odd function, get rid of it 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 index 69a59c07..b73af021 100644 --- a/api/logic/meta/tasks/RemoteLoadTask.cpp +++ b/api/logic/meta/tasks/RemoteLoadTask.cpp @@ -34,99 +34,70 @@ RemoteLoadTask::RemoteLoadTask(BaseEntity *entity, QObject *parent) { } -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() +class ParsingValidator : public Net::Validator { - setStatus(tr("Parsing...")); - setProgress(0, 0); +public: /* con/des */ + ParsingValidator(BaseEntity *entity) : m_entity(entity) + { + }; + virtual ~ParsingValidator() + { + }; - try +public: /* methods */ + bool init(QNetworkRequest &) override { - parse(Json::requireObject(Json::requireDocument(m_dl->getTargetFilepath(), name()), name())); - m_entity->notifyRemoteLoadComplete(); - emitSucceeded(); + return true; } - catch (Exception &e) + bool write(QByteArray & data) override { - emitFailed(tr("Unable to parse response: %1").arg(e.cause())); + this->data.append(data); + return true; + } + bool abort() override + { + return true; + } + bool validate(QNetworkReply &) override + { + auto fname = m_entity->localFilename(); + try + { + m_entity->parse(Json::requireObject(Json::requireDocument(data, fname), fname)); + m_entity->notifyRemoteLoadComplete(); + return true; + } + catch (Exception &e) + { + qWarning() << "Unable to parse response:" << e.cause(); + return false; + } } -} - -// 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())); -} +private: /* data */ + QByteArray data; + BaseEntity *m_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 +void RemoteLoadTask::executeTask() { - return dynamic_cast(entity()); -} + // FIXME: leak here!!! + NetJob *job = new NetJob(tr("Download of meta file %1").arg(m_entity->localFilename())); - -// 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()); + auto url = m_entity->url(); + auto entry = ENV.metacache()->resolveEntry("meta", m_entity->localFilename()); + entry->setStale(true); + m_dl = Net::Download::makeCached(url, entry); + /* + * The validator parses the file and loads it into the object. + * If that fails, the file is not written to storage. + */ + m_dl->addValidator(new ParsingValidator(m_entity)); + job->addNetAction(m_dl); + connect(job, &NetJob::failed, this, &RemoteLoadTask::emitFailed); + connect(job, &NetJob::succeeded, this, &RemoteLoadTask::succeeded); + connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); + connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); + job->start(); } } diff --git a/api/logic/meta/tasks/RemoteLoadTask.h b/api/logic/meta/tasks/RemoteLoadTask.h index 3b09404f..6d81d8ea 100644 --- a/api/logic/meta/tasks/RemoteLoadTask.h +++ b/api/logic/meta/tasks/RemoteLoadTask.h @@ -30,66 +30,17 @@ class Index; class VersionList; class Version; +// FIXME: this is now just an oddly constructed NetJob, get rid of it. 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 From e46aba9da584338db8d8a1a8a487bdcc6cf84343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 18 Mar 2017 02:22:36 +0100 Subject: NOISSUE sanitize loading and downloading of metadata files --- api/logic/meta/BaseEntity.cpp | 119 +++++++++++++++++++++++--- api/logic/meta/BaseEntity.h | 45 +++++++--- api/logic/meta/Index.cpp | 13 +-- api/logic/meta/Index.h | 7 -- api/logic/meta/Index_test.cpp | 6 -- api/logic/meta/JsonFormat.cpp | 144 ++++++++++++++++++++++++++++++++ api/logic/meta/JsonFormat.h | 40 +++++++++ api/logic/meta/Util.cpp | 50 ----------- api/logic/meta/Util.h | 28 ------- api/logic/meta/Version.cpp | 13 +-- api/logic/meta/Version.h | 2 - api/logic/meta/VersionList.cpp | 70 ++-------------- api/logic/meta/VersionList.h | 3 - api/logic/meta/format/Format.cpp | 143 ------------------------------- api/logic/meta/format/Format.h | 40 --------- api/logic/meta/tasks/LocalLoadTask.cpp | 57 ------------- api/logic/meta/tasks/LocalLoadTask.h | 39 --------- api/logic/meta/tasks/RemoteLoadTask.cpp | 103 ----------------------- api/logic/meta/tasks/RemoteLoadTask.h | 46 ---------- 19 files changed, 332 insertions(+), 636 deletions(-) create mode 100644 api/logic/meta/JsonFormat.cpp create mode 100644 api/logic/meta/JsonFormat.h delete mode 100644 api/logic/meta/Util.cpp delete mode 100644 api/logic/meta/Util.h delete mode 100644 api/logic/meta/format/Format.cpp delete mode 100644 api/logic/meta/format/Format.h delete mode 100644 api/logic/meta/tasks/LocalLoadTask.cpp delete mode 100644 api/logic/meta/tasks/LocalLoadTask.h delete mode 100644 api/logic/meta/tasks/RemoteLoadTask.cpp delete 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 index 87cd55c8..18b7f92f 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -16,26 +16,125 @@ #include "BaseEntity.h" #include "Json.h" -#include "Util.h" -namespace Meta +#include "net/Download.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" + +#include "Env.h" +#include "Json.h" + +class ParsingValidator : public Net::Validator { -BaseEntity::~BaseEntity() +public: /* con/des */ + ParsingValidator(Meta::BaseEntity *entity) : m_entity(entity) + { + }; + virtual ~ParsingValidator() + { + }; + +public: /* methods */ + bool init(QNetworkRequest &) override + { + return true; + } + bool write(QByteArray & data) override + { + this->data.append(data); + return true; + } + bool abort() override + { + return true; + } + bool validate(QNetworkReply &) override + { + auto fname = m_entity->localFilename(); + try + { + m_entity->parse(Json::requireObject(Json::requireDocument(data, fname), fname)); + return true; + } + catch (Exception &e) + { + qWarning() << "Unable to parse response:" << e.cause(); + return false; + } + } + +private: /* data */ + QByteArray data; + Meta::BaseEntity *m_entity; +}; + +Meta::BaseEntity::~BaseEntity() { } -QUrl BaseEntity::url() const +QUrl Meta::BaseEntity::url() const { - return rootUrl().resolved(localFilename()); + return QUrl("https://meta.multimc.org").resolved(localFilename()); } -void BaseEntity::notifyLocalLoadComplete() +bool Meta::BaseEntity::loadLocalFile() { - m_localLoaded = true; + const QString fname = QDir("meta").absoluteFilePath(localFilename()); + if (!QFile::exists(fname)) + { + return false; + } + // TODO: check if the file has the expected checksum + try + { + parse(Json::requireObject(Json::requireDocument(fname, fname), fname)); + return true; + } + catch (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. + QFile::remove(fname); + return false; + } } -void BaseEntity::notifyRemoteLoadComplete() +void Meta::BaseEntity::load() { - m_remoteLoaded = true; -} + if(!isLoaded()) + { + loadLocalFile(); + } + if(!shouldStartRemoteUpdate()) + { + return; + } + NetJob *job = new NetJob(QObject::tr("Download of meta file %1").arg(localFilename())); + + auto url = this->url(); + auto entry = ENV.metacache()->resolveEntry("meta", localFilename()); + entry->setStale(true); + auto dl = Net::Download::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)); + job->addNetAction(dl); + m_updateStatus = UpdateStatus::InProgress; + m_updateTask.reset(job); + QObject::connect(job, &NetJob::succeeded, [&]() + { + m_loadStatus = LoadStatus::Remote; + m_updateStatus = UpdateStatus::Succeeded; + m_updateTask.reset(); + }); + QObject::connect(job, &NetJob::failed, [&]() + { + m_updateStatus = UpdateStatus::Failed; + m_updateTask.reset(); + }); + m_updateTask->start(); } + +#include "BaseEntity.moc" diff --git a/api/logic/meta/BaseEntity.h b/api/logic/meta/BaseEntity.h index 7064e9d2..92a39272 100644 --- a/api/logic/meta/BaseEntity.h +++ b/api/logic/meta/BaseEntity.h @@ -15,9 +15,9 @@ #pragma once -#include -#include #include +#include +#include "QObjectPtr.h" #include "multimc_logic_export.h" @@ -26,29 +26,48 @@ namespace Meta { class MULTIMC_LOGIC_EXPORT BaseEntity { +public: /* types */ + using Ptr = std::shared_ptr; + enum class LoadStatus + { + NotLoaded, + Local, + Remote + }; + enum class UpdateStatus + { + NotDone, + InProgress, + Failed, + Succeeded + }; + 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; virtual void parse(const QJsonObject &obj) = 0; virtual QString localFilename() const = 0; virtual QUrl url() const; - bool isComplete() const { return m_localLoaded || m_remoteLoaded; } + bool isLoaded() const + { + return m_loadStatus > LoadStatus::NotLoaded; + } + bool shouldStartRemoteUpdate() const + { + return m_updateStatus == UpdateStatus::NotDone; + } - bool isLocalLoaded() const { return m_localLoaded; } - bool isRemoteLoaded() const { return m_remoteLoaded; } + void load(); - void notifyLocalLoadComplete(); - void notifyRemoteLoadComplete(); +protected: /* methods */ + bool loadLocalFile(); private: - bool m_localLoaded = false; - bool m_remoteLoaded = false; + LoadStatus m_loadStatus = LoadStatus::NotLoaded; + UpdateStatus m_updateStatus = UpdateStatus::NotDone; + shared_qobject_ptr m_updateTask; }; } diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp index e0e8bc5d..35b9fb6f 100644 --- a/api/logic/meta/Index.cpp +++ b/api/logic/meta/Index.cpp @@ -16,9 +16,7 @@ #include "Index.h" #include "VersionList.h" -#include "tasks/LocalLoadTask.h" -#include "tasks/RemoteLoadTask.h" -#include "format/Format.h" +#include "JsonFormat.h" namespace Meta { @@ -78,15 +76,6 @@ QVariant Index::headerData(int section, Qt::Orientation orientation, int role) c } } -std::unique_ptr Index::remoteUpdateTask() -{ - return std::unique_ptr(new RemoteLoadTask(this)); -} -std::unique_ptr Index::localUpdateTask() -{ - return std::unique_ptr(new LocalLoadTask(this)); -} - bool Index::hasUid(const QString &uid) const { return m_uids.contains(uid); diff --git a/api/logic/meta/Index.h b/api/logic/meta/Index.h index 37341656..544a8b96 100644 --- a/api/logic/meta/Index.h +++ b/api/logic/meta/Index.h @@ -48,19 +48,12 @@ public: 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"; } // queries VersionListPtr get(const QString &uid); VersionPtr get(const QString &uid, const QString &version); bool hasUid(const QString &uid) const; - /* - VersionListPtr getList(const QString &uid) const; - VersionListPtr getListGuaranteed(const QString &uid) const; - */ QVector lists() const { return m_lists; } diff --git a/api/logic/meta/Index_test.cpp b/api/logic/meta/Index_test.cpp index b4dbd009..d4343c37 100644 --- a/api/logic/meta/Index_test.cpp +++ b/api/logic/meta/Index_test.cpp @@ -16,12 +16,6 @@ slots: 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")}); diff --git a/api/logic/meta/JsonFormat.cpp b/api/logic/meta/JsonFormat.cpp new file mode 100644 index 00000000..4e43b715 --- /dev/null +++ b/api/logic/meta/JsonFormat.cpp @@ -0,0 +1,144 @@ +/* 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 "JsonFormat.h" + +// FIXME: remove this from here... somehow +#include "minecraft/onesix/OneSixVersionFormat.h" +#include "Json.h" + +#include "Index.h" +#include "Version.h" +#include "VersionList.h" + +using namespace Json; + +namespace Meta +{ + +static const int currentFormatVersion = 0; + +// Index +static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) +{ + const QVector objects = requireIsArrayOf(obj, "packages"); + 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); +} + +// Version +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")); + version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); + version->setType(ensureString(obj, "type", QString())); + version->setRequires(requires); + return version; +} + +static BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) +{ + 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; +} + +// Version list / package +static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) +{ + 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), [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; +} + + +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 parseIndex(const QJsonObject &obj, Index *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 0: + ptr->merge(parseIndexInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} + +void parseVersionList(const QJsonObject &obj, VersionList *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 0: + ptr->merge(parseVersionListInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} + +void parseVersion(const QJsonObject &obj, Version *ptr) +{ + const int version = formatVersion(obj); + switch (version) { + case 0: + ptr->merge(parseVersionInternal(obj)); + break; + default: + throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); + } +} +} diff --git a/api/logic/meta/JsonFormat.h b/api/logic/meta/JsonFormat.h new file mode 100644 index 00000000..aaed07fc --- /dev/null +++ b/api/logic/meta/JsonFormat.h @@ -0,0 +1,40 @@ +/* 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; +}; + +void parseIndex(const QJsonObject &obj, Index *ptr); +void parseVersion(const QJsonObject &obj, Version *ptr); +void parseVersionList(const QJsonObject &obj, VersionList *ptr); + +} diff --git a/api/logic/meta/Util.cpp b/api/logic/meta/Util.cpp deleted file mode 100644 index 76941f83..00000000 --- a/api/logic/meta/Util.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* 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 + "/index.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 deleted file mode 100644 index 188bcaa3..00000000 --- a/api/logic/meta/Util.h +++ /dev/null @@ -1,28 +0,0 @@ -/* 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 QDir localDir(); -} diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index b79c178a..f8c865e7 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -17,9 +17,7 @@ #include -#include "tasks/LocalLoadTask.h" -#include "tasks/RemoteLoadTask.h" -#include "format/Format.h" +#include "JsonFormat.h" namespace Meta { @@ -46,15 +44,6 @@ QDateTime Version::time() const return QDateTime::fromMSecsSinceEpoch(m_time * 1000, Qt::UTC); } -std::unique_ptr Version::remoteUpdateTask() -{ - return std::unique_ptr(new RemoteLoadTask(this)); -} -std::unique_ptr Version::localUpdateTask() -{ - return std::unique_ptr(new LocalLoadTask(this)); -} - void Version::parse(const QJsonObject& obj) { parseVersion(obj, this); diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 635b79f6..0be2d94a 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -56,8 +56,6 @@ public: 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; void parse(const QJsonObject &obj) override; diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index 7196d4be..a12f5418 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -18,64 +18,11 @@ #include #include "Version.h" -#include "tasks/RemoteLoadTask.h" -#include "tasks/LocalLoadTask.h" -#include "format/Format.h" +#include "JsonFormat.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) { @@ -84,12 +31,13 @@ VersionList::VersionList(const QString &uid, QObject *parent) Task *VersionList::getLoadTask() { - return new WVLLoadTask(this); + // TODO: create a wrapper task that will chain from root to here. + return nullptr; } bool VersionList::isLoaded() { - return isLocalLoaded() && isRemoteLoaded(); + return isLoaded(); } const BaseVersionPtr VersionList::at(int i) const @@ -167,15 +115,6 @@ QHash VersionList::roleNames() const return roles; } -std::unique_ptr VersionList::remoteUpdateTask() -{ - return std::unique_ptr(new RemoteLoadTask(this)); -} -std::unique_ptr VersionList::localUpdateTask() -{ - return std::unique_ptr(new LocalLoadTask(this)); -} - QString VersionList::localFilename() const { return m_uid + "/index.json"; @@ -200,6 +139,7 @@ void VersionList::setName(const QString &name) m_name = name; emit nameChanged(name); } + void VersionList::setVersions(const QVector &versions) { beginResetModel(); diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index 934b20e4..e958475e 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -54,9 +54,6 @@ public: RoleList providesRoles() const override; QHash roleNames() const override; - std::unique_ptr remoteUpdateTask() override; - std::unique_ptr localUpdateTask() override; - QString localFilename() const override; QString uid() const { return m_uid; } diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp deleted file mode 100644 index 39d3f14f..00000000 --- a/api/logic/meta/format/Format.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* 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 "minecraft/onesix/OneSixVersionFormat.h"" - -#include "meta/Index.h" -#include "meta/Version.h" -#include "meta/VersionList.h" - -#include "Json.h" -using namespace Json; - -namespace Meta -{ - -static const int currentFormatVersion = 0; - -// Index -static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) -{ - const QVector objects = requireIsArrayOf(obj, "packages"); - 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); -} - -// Version -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")); - version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); - version->setType(ensureString(obj, "type", QString())); - version->setRequires(requires); - return version; -} - -static BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) -{ - 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; -} - -// Version list / package -static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) -{ - 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), [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; -} - - -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 parseIndex(const QJsonObject &obj, Index *ptr) -{ - const int version = formatVersion(obj); - switch (version) { - case 0: - ptr->merge(parseIndexInternal(obj)); - break; - default: - throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); - } -} - -void parseVersionList(const QJsonObject &obj, VersionList *ptr) -{ - const int version = formatVersion(obj); - switch (version) { - case 0: - ptr->merge(parseVersionListInternal(obj)); - break; - default: - throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); - } -} - -void parseVersion(const QJsonObject &obj, Version *ptr) -{ - const int version = formatVersion(obj); - switch (version) { - case 0: - ptr->merge(parseVersionInternal(obj)); - break; - default: - throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); - } -} -} diff --git a/api/logic/meta/format/Format.h b/api/logic/meta/format/Format.h deleted file mode 100644 index aaed07fc..00000000 --- a/api/logic/meta/format/Format.h +++ /dev/null @@ -1,40 +0,0 @@ -/* 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; -}; - -void parseIndex(const QJsonObject &obj, Index *ptr); -void parseVersion(const QJsonObject &obj, Version *ptr); -void parseVersionList(const QJsonObject &obj, VersionList *ptr); - -} diff --git a/api/logic/meta/tasks/LocalLoadTask.cpp b/api/logic/meta/tasks/LocalLoadTask.cpp deleted file mode 100644 index b64fc5d3..00000000 --- a/api/logic/meta/tasks/LocalLoadTask.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* 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(m_entity->localFilename()); - if (!QFile::exists(fname)) - { - emitFailed(tr("File doesn't exist")); - return; - } - setStatus(tr("Reading %1...").arg(fname)); - setProgress(0, 0); - - try - { - m_entity->parse(Json::requireObject(Json::requireDocument(fname, fname), fname)); - m_entity->notifyLocalLoadComplete(); - emitSucceeded(); - } - catch (Exception &e) - { - emitFailed(tr("Unable to parse file %1: %2").arg(fname, e.cause())); - } -} -} diff --git a/api/logic/meta/tasks/LocalLoadTask.h b/api/logic/meta/tasks/LocalLoadTask.h deleted file mode 100644 index 905660ed..00000000 --- a/api/logic/meta/tasks/LocalLoadTask.h +++ /dev/null @@ -1,39 +0,0 @@ -/* 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; - -// FIXME: this is now just an odd function, get rid of it -class LocalLoadTask : public Task -{ - Q_OBJECT -public: - explicit LocalLoadTask(BaseEntity *entity, QObject *parent = nullptr); - -private: - void executeTask() override; - BaseEntity *m_entity; -}; -} diff --git a/api/logic/meta/tasks/RemoteLoadTask.cpp b/api/logic/meta/tasks/RemoteLoadTask.cpp deleted file mode 100644 index b73af021..00000000 --- a/api/logic/meta/tasks/RemoteLoadTask.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* 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) -{ -} - -class ParsingValidator : public Net::Validator -{ -public: /* con/des */ - ParsingValidator(BaseEntity *entity) : m_entity(entity) - { - }; - virtual ~ParsingValidator() - { - }; - -public: /* methods */ - bool init(QNetworkRequest &) override - { - return true; - } - bool write(QByteArray & data) override - { - this->data.append(data); - return true; - } - bool abort() override - { - return true; - } - bool validate(QNetworkReply &) override - { - auto fname = m_entity->localFilename(); - try - { - m_entity->parse(Json::requireObject(Json::requireDocument(data, fname), fname)); - m_entity->notifyRemoteLoadComplete(); - return true; - } - catch (Exception &e) - { - qWarning() << "Unable to parse response:" << e.cause(); - return false; - } - } - -private: /* data */ - QByteArray data; - BaseEntity *m_entity; -}; - -void RemoteLoadTask::executeTask() -{ - // FIXME: leak here!!! - NetJob *job = new NetJob(tr("Download of meta file %1").arg(m_entity->localFilename())); - - auto url = m_entity->url(); - auto entry = ENV.metacache()->resolveEntry("meta", m_entity->localFilename()); - entry->setStale(true); - m_dl = Net::Download::makeCached(url, entry); - /* - * The validator parses the file and loads it into the object. - * If that fails, the file is not written to storage. - */ - m_dl->addValidator(new ParsingValidator(m_entity)); - job->addNetAction(m_dl); - connect(job, &NetJob::failed, this, &RemoteLoadTask::emitFailed); - connect(job, &NetJob::succeeded, this, &RemoteLoadTask::succeeded); - connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); - connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); - job->start(); -} -} diff --git a/api/logic/meta/tasks/RemoteLoadTask.h b/api/logic/meta/tasks/RemoteLoadTask.h deleted file mode 100644 index 6d81d8ea..00000000 --- a/api/logic/meta/tasks/RemoteLoadTask.h +++ /dev/null @@ -1,46 +0,0 @@ -/* 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; - -// FIXME: this is now just an oddly constructed NetJob, get rid of it. -class RemoteLoadTask : public Task -{ - Q_OBJECT -public: - explicit RemoteLoadTask(BaseEntity *entity, QObject *parent = nullptr); - -private: - void executeTask() override; - - BaseEntity *m_entity; - std::shared_ptr m_dl; -}; -} -- cgit v1.2.3 From 2660418d58efb33cd3a0ab8ed9d48c359c076905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 19 Mar 2017 02:13:49 +0100 Subject: NOISSUE hack it together enough to get launching back Meta index will now always return valid objects. They just might never load if they don't exist on the server. --- api/logic/meta/BaseEntity.cpp | 9 +++++ api/logic/meta/BaseEntity.h | 1 + api/logic/meta/Index.cpp | 14 ++++--- api/logic/meta/Index.h | 1 + api/logic/meta/Version.cpp | 35 +++++++++++------- api/logic/meta/Version.h | 84 +++++++++++++++++++++++++++++++++++++++++- api/logic/meta/VersionList.cpp | 14 ++++--- api/logic/meta/VersionList.h | 3 +- 8 files changed, 131 insertions(+), 30 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index 18b7f92f..809f88cb 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -137,4 +137,13 @@ void Meta::BaseEntity::load() m_updateTask->start(); } +shared_qobject_ptr Meta::BaseEntity::getCurrentTask() +{ + if(m_updateStatus == UpdateStatus::InProgress) + { + return m_updateTask; + } + return nullptr; +} + #include "BaseEntity.moc" diff --git a/api/logic/meta/BaseEntity.h b/api/logic/meta/BaseEntity.h index 92a39272..85051d97 100644 --- a/api/logic/meta/BaseEntity.h +++ b/api/logic/meta/BaseEntity.h @@ -61,6 +61,7 @@ public: } void load(); + shared_qobject_ptr getCurrentTask(); protected: /* methods */ bool loadLocalFile(); diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp index 35b9fb6f..0749651a 100644 --- a/api/logic/meta/Index.cpp +++ b/api/logic/meta/Index.cpp @@ -83,17 +83,19 @@ bool Index::hasUid(const QString &uid) const VersionListPtr Index::get(const QString &uid) { - return m_uids.value(uid, nullptr); + VersionListPtr out = m_uids.value(uid, nullptr); + if(!out) + { + out = std::make_shared(uid); + m_uids[uid] = out; + } + return out; } VersionPtr Index::get(const QString &uid, const QString &version) { auto list = get(uid); - if(list) - { - return list->getVersion(version); - } - return nullptr; + return list->getVersion(version); } void Index::parse(const QJsonObject& obj) diff --git a/api/logic/meta/Index.h b/api/logic/meta/Index.h index 544a8b96..9811e152 100644 --- a/api/logic/meta/Index.h +++ b/api/logic/meta/Index.h @@ -68,3 +68,4 @@ private: void connectVersionList(const int row, const VersionListPtr &list); }; } + diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index f8c865e7..2790b2f3 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -18,38 +18,45 @@ #include #include "JsonFormat.h" +#include "minecraft/MinecraftProfile.h" -namespace Meta +void Meta::Version::applyTo(MinecraftProfile* profile) { -Version::Version(const QString &uid, const QString &version) + if(m_data) + { + m_data->applyTo(profile); + } +} + +Meta::Version::Version(const QString &uid, const QString &version) : BaseVersion(), m_uid(uid), m_version(version) { } -QString Version::descriptor() +QString Meta::Version::descriptor() { return m_version; } -QString Version::name() +QString Meta::Version::name() { return m_version; } -QString Version::typeString() const +QString Meta::Version::typeString() const { return m_type; } -QDateTime Version::time() const +QDateTime Meta::Version::time() const { return QDateTime::fromMSecsSinceEpoch(m_time * 1000, Qt::UTC); } -void Version::parse(const QJsonObject& obj) +void Meta::Version::parse(const QJsonObject& obj) { parseVersion(obj, this); } -void Version::merge(const std::shared_ptr &other) +void Meta::Version::merge(const std::shared_ptr &other) { VersionPtr version = std::dynamic_pointer_cast(other); if (m_type != version->m_type) @@ -68,28 +75,28 @@ void Version::merge(const std::shared_ptr &other) setData(version->m_data); } -QString Version::localFilename() const +QString Meta::Version::localFilename() const { return m_uid + '/' + m_version + ".json"; } -void Version::setType(const QString &type) +void Meta::Version::setType(const QString &type) { m_type = type; emit typeChanged(); } -void Version::setTime(const qint64 time) +void Meta::Version::setTime(const qint64 time) { m_time = time; emit timeChanged(); } -void Version::setRequires(const QVector &requires) +void Meta::Version::setRequires(const QVector &requires) { m_requires = requires; emit requiresChanged(); } -void Version::setData(const VersionFilePtr &data) +void Meta::Version::setData(const VersionFilePtr &data) { m_data = data; } -} + diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 0be2d94a..b8ea7e44 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -33,7 +33,7 @@ namespace Meta { using VersionPtr = std::shared_ptr; -class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public BaseEntity +class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public BaseEntity, public ProfilePatch { Q_OBJECT Q_PROPERTY(QString uid READ uid CONSTANT) @@ -41,9 +41,89 @@ class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public 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: + +public: /* con/des */ explicit Version(const QString &uid, const QString &version); +// FIXME: none of this belongs here... +public: /* ProfilePatch overrides */ + QString getFilename() override + { + return QString(); + } + QString getID() override + { + return m_uid; + } + QList getJarMods() override + { + return {}; + } + QString getName() override + { + return name(); + } + QDateTime getReleaseDateTime() override + { + return time(); + } + QString getVersion() override + { + return m_version; + } + std::shared_ptr getVersionFile() override + { + return m_data; + } + int getOrder() override + { + return 0; + } + VersionSource getVersionSource() override + { + return VersionSource::Local; + } + bool isVersionChangeable() override + { + return true; + } + bool isRevertible() override + { + return false; + } + bool isRemovable() override + { + return true; + } + bool isCustom() override + { + return false; + } + bool isCustomizable() override + { + return true; + } + bool isMoveable() override + { + return true; + } + bool isEditable() override + { + return false; + } + void setOrder(int) override + { + } + bool hasJarMods() override + { + return false; + } + bool isMinecraftVersion() override + { + return m_uid == "net.minecraft"; + } + void applyTo(MinecraftProfile * profile) override; + QString descriptor() override; QString name() override; QString typeString() const override; diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index a12f5418..d6bbf70f 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -125,13 +125,15 @@ QString VersionList::humanReadable() const return m_name.isEmpty() ? m_uid : m_name; } -bool VersionList::hasVersion(const QString &version) const +VersionPtr VersionList::getVersion(const QString &version) { - return m_lookup.contains(version); -} -VersionPtr VersionList::getVersion(const QString &version) const -{ - return m_lookup.value(version); + VersionPtr out = m_lookup.value(version, nullptr); + if(!out) + { + out = std::make_shared(m_uid, version); + m_lookup[version] = out; + } + return out; } void VersionList::setName(const QString &name) diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index e958475e..26fa6c5a 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -60,8 +60,7 @@ public: QString name() const { return m_name; } QString humanReadable() const; - bool hasVersion(const QString &version) const; - VersionPtr getVersion(const QString &version) const; + VersionPtr getVersion(const QString &version); QVector versions() const { return m_versions; } -- cgit v1.2.3 From f557c1367994a61935fa0e8d0b1b67688d2692d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 19 Mar 2017 23:58:54 +0100 Subject: NOISSUE stuff and things happened. Maybe. --- api/logic/meta/BaseEntity.cpp | 8 +++-- api/logic/meta/Index_test.cpp | 2 +- api/logic/meta/Version.cpp | 12 ++----- api/logic/meta/Version.h | 82 ++---------------------------------------- api/logic/meta/VersionList.cpp | 8 ++--- api/logic/meta/VersionList.h | 2 +- 6 files changed, 17 insertions(+), 97 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index 809f88cb..633afab2 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -101,16 +101,20 @@ bool Meta::BaseEntity::loadLocalFile() void Meta::BaseEntity::load() { + // load local file if nothing is loaded yet if(!isLoaded()) { - loadLocalFile(); + if(loadLocalFile()) + { + m_loadStatus = LoadStatus::Local; + } } + // if we need remote update, run the update task if(!shouldStartRemoteUpdate()) { return; } NetJob *job = new NetJob(QObject::tr("Download of meta file %1").arg(localFilename())); - auto url = this->url(); auto entry = ENV.metacache()->resolveEntry("meta", localFilename()); entry->setStale(true); diff --git a/api/logic/meta/Index_test.cpp b/api/logic/meta/Index_test.cpp index d4343c37..1c5face2 100644 --- a/api/logic/meta/Index_test.cpp +++ b/api/logic/meta/Index_test.cpp @@ -23,7 +23,7 @@ slots: QVERIFY(!windex.hasUid("asdf")); QVERIFY(windex.get("list2") != nullptr); QCOMPARE(windex.get("list2")->uid(), QString("list2")); - QVERIFY(windex.get("adsf") == nullptr); + QVERIFY(windex.get("adsf") != nullptr); } void test_merge() diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index 2790b2f3..fee7d049 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -20,14 +20,6 @@ #include "JsonFormat.h" #include "minecraft/MinecraftProfile.h" -void Meta::Version::applyTo(MinecraftProfile* profile) -{ - if(m_data) - { - m_data->applyTo(profile); - } -} - Meta::Version::Version(const QString &uid, const QString &version) : BaseVersion(), m_uid(uid), m_version(version) { @@ -39,7 +31,9 @@ QString Meta::Version::descriptor() } QString Meta::Version::name() { - return m_version; + if(m_data) + return m_data->getName(); + return m_uid; } QString Meta::Version::typeString() const { diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index b8ea7e44..b3943f47 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -33,7 +33,7 @@ namespace Meta { using VersionPtr = std::shared_ptr; -class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public BaseEntity, public ProfilePatch +class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public BaseEntity { Q_OBJECT Q_PROPERTY(QString uid READ uid CONSTANT) @@ -45,85 +45,6 @@ class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public public: /* con/des */ explicit Version(const QString &uid, const QString &version); -// FIXME: none of this belongs here... -public: /* ProfilePatch overrides */ - QString getFilename() override - { - return QString(); - } - QString getID() override - { - return m_uid; - } - QList getJarMods() override - { - return {}; - } - QString getName() override - { - return name(); - } - QDateTime getReleaseDateTime() override - { - return time(); - } - QString getVersion() override - { - return m_version; - } - std::shared_ptr getVersionFile() override - { - return m_data; - } - int getOrder() override - { - return 0; - } - VersionSource getVersionSource() override - { - return VersionSource::Local; - } - bool isVersionChangeable() override - { - return true; - } - bool isRevertible() override - { - return false; - } - bool isRemovable() override - { - return true; - } - bool isCustom() override - { - return false; - } - bool isCustomizable() override - { - return true; - } - bool isMoveable() override - { - return true; - } - bool isEditable() override - { - return false; - } - void setOrder(int) override - { - } - bool hasJarMods() override - { - return false; - } - bool isMinecraftVersion() override - { - return m_uid == "net.minecraft"; - } - void applyTo(MinecraftProfile * profile) override; - QString descriptor() override; QString name() override; QString typeString() const override; @@ -153,6 +74,7 @@ signals: void requiresChanged(); private: + QString m_name; QString m_uid; QString m_version; QString m_type; diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index d6bbf70f..28d9dd26 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -29,15 +29,15 @@ VersionList::VersionList(const QString &uid, QObject *parent) setObjectName("Version list: " + uid); } -Task *VersionList::getLoadTask() +shared_qobject_ptr VersionList::getLoadTask() { - // TODO: create a wrapper task that will chain from root to here. - return nullptr; + load(); + return getCurrentTask(); } bool VersionList::isLoaded() { - return isLoaded(); + return BaseEntity::isLoaded(); } const BaseVersionPtr VersionList::at(int i) const diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index 26fa6c5a..08d71230 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -41,7 +41,7 @@ public: VersionPtrRole }; - Task *getLoadTask() override; + shared_qobject_ptr getLoadTask() override; bool isLoaded() override; const BaseVersionPtr at(int i) const override; int count() const override; -- cgit v1.2.3 From da4ae1bc1ec74cdb4e75f4ebac30886ba4a1909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Fri, 24 Mar 2017 02:26:06 +0100 Subject: NOISSUE reimplement package dependencies It is now stored as a hashmap There is also a parentUid to limit depsolving by encapsulating by version --- api/logic/meta/JsonFormat.cpp | 25 ++++++++++++---------- api/logic/meta/Reference.cpp | 48 ------------------------------------------ api/logic/meta/Reference.h | 44 -------------------------------------- api/logic/meta/Version.cpp | 15 ++++++++++++- api/logic/meta/Version.h | 48 +++++++++++++++++++++++++++++++----------- api/logic/meta/VersionList.cpp | 26 +++++++++++++++++------ api/logic/meta/VersionList.h | 28 ++++++++++++++++++------ 7 files changed, 105 insertions(+), 129 deletions(-) delete mode 100644 api/logic/meta/Reference.cpp delete mode 100644 api/logic/meta/Reference.h (limited to 'api/logic/meta') diff --git a/api/logic/meta/JsonFormat.cpp b/api/logic/meta/JsonFormat.cpp index 4e43b715..8a063f48 100644 --- a/api/logic/meta/JsonFormat.cpp +++ b/api/logic/meta/JsonFormat.cpp @@ -48,20 +48,22 @@ static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) // Version 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")); version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); version->setType(ensureString(obj, "type", QString())); - version->setRequires(requires); + version->setParentUid(ensureString(obj, "parentUid", QString())); + if(obj.contains("requires")) + { + QHash requires; + auto reqobj = requireObject(obj, "requires"); + auto iter = reqobj.begin(); + while(iter != reqobj.end()) + { + requires[iter.key()] = requireString(iter.value()); + iter++; + } + version->setRequires(requires); + } return version; } @@ -90,6 +92,7 @@ static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) VersionListPtr list = std::make_shared(uid); list->setName(ensureString(obj, "name", QString())); + list->setParentUid(ensureString(obj, "parentUid", QString())); list->setVersions(versions); return list; } diff --git a/api/logic/meta/Reference.cpp b/api/logic/meta/Reference.cpp deleted file mode 100644 index c5cef172..00000000 --- a/api/logic/meta/Reference.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* 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 deleted file mode 100644 index 027076cc..00000000 --- a/api/logic/meta/Reference.h +++ /dev/null @@ -1,44 +0,0 @@ -/* 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/Version.cpp b/api/logic/meta/Version.cpp index fee7d049..af9d4f10 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -65,6 +65,10 @@ void Meta::Version::merge(const std::shared_ptr &other) { setRequires(version->m_requires); } + if (m_parentUid != version->m_parentUid) + { + setParentUid(version->m_parentUid); + } setData(version->m_data); } @@ -74,21 +78,30 @@ QString Meta::Version::localFilename() const return m_uid + '/' + m_version + ".json"; } +void Meta::Version::setParentUid(const QString& parentUid) +{ + m_parentUid = parentUid; + emit requiresChanged(); +} + void Meta::Version::setType(const QString &type) { m_type = type; emit typeChanged(); } + void Meta::Version::setTime(const qint64 time) { m_time = time; emit timeChanged(); } -void Meta::Version::setRequires(const QVector &requires) + +void Meta::Version::setRequires(const QHash &requires) { m_requires = requires; emit requiresChanged(); } + void Meta::Version::setData(const VersionFilePtr &data) { m_data = data; diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index b3943f47..1d921035 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -17,15 +17,14 @@ #include "BaseVersion.h" -#include -#include #include +#include +#include #include #include "minecraft/VersionFile.h" #include "BaseEntity.h" -#include "Reference.h" #include "multimc_logic_export.h" @@ -37,10 +36,11 @@ class MULTIMC_LOGIC_EXPORT Version : public QObject, public BaseVersion, public { Q_OBJECT Q_PROPERTY(QString uid READ uid CONSTANT) + Q_PROPERTY(QString parentUid READ parentUid) 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) + Q_PROPERTY(QHash requires READ requires NOTIFY requiresChanged) public: /* con/des */ explicit Version(const QString &uid, const QString &version); @@ -49,13 +49,35 @@ public: /* con/des */ 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; } + QString uid() const + { + return m_uid; + } + QString parentUid() const + { + return m_parentUid; + } + 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; } + qint64 rawTime() const + { + return m_time; + } + const QHash &requires() const + { + return m_requires; + } + VersionFilePtr data() const + { + return m_data; + } void merge(const std::shared_ptr &other) override; void parse(const QJsonObject &obj) override; @@ -63,9 +85,10 @@ public: /* con/des */ QString localFilename() const override; public: // for usage by format parsers only + void setParentUid(const QString &parentUid); void setType(const QString &type); void setTime(const qint64 time); - void setRequires(const QVector &requires); + void setRequires(const QHash &requires); void setData(const VersionFilePtr &data); signals: @@ -76,10 +99,11 @@ signals: private: QString m_name; QString m_uid; + QString m_parentUid; QString m_version; QString m_type; qint64 m_time; - QVector m_requires; + QHash m_requires; VersionFilePtr m_data; }; } diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index 28d9dd26..c2712155 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -19,7 +19,6 @@ #include "Version.h" #include "JsonFormat.h" -#include "Reference.h" namespace Meta { @@ -76,14 +75,17 @@ QVariant VersionList::data(const QModelIndex &index, int role) const 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) + auto parentUid = this->parentUid(); + if(parentUid.isEmpty()) { - return (*it).version(); + return QVariant(); + } + auto & reqs = version->requires(); + auto iter = reqs.find(parentUid); + if (iter != reqs.end()) + { + return iter.value(); } - return QVariant(); } case TypeRole: return version->type(); @@ -175,6 +177,11 @@ void VersionList::merge(const BaseEntity::Ptr &other) setName(list->m_name); } + if(m_parentUid != list->m_parentUid) + { + setParentUid(list->m_parentUid); + } + if (m_versions.isEmpty()) { setVersions(list->m_versions); @@ -228,5 +235,10 @@ BaseVersionPtr VersionList::getRecommended() const } +void Meta::VersionList::setParentUid(const QString& parentUid) +{ + m_parentUid = parentUid; +} + #include "VersionList.moc" diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index 08d71230..faacdbf8 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -15,8 +15,8 @@ #pragma once -#include "BaseVersionList.h" #include "BaseEntity.h" +#include "BaseVersionList.h" #include #include @@ -56,16 +56,30 @@ public: QString localFilename() const override; - QString uid() const { return m_uid; } - QString name() const { return m_name; } + QString parentUid() const + { + return m_parentUid; + } + QString uid() const + { + return m_uid; + } + QString name() const + { + return m_name; + } QString humanReadable() const; VersionPtr getVersion(const QString &version); - QVector versions() const { return m_versions; } + QVector versions() const + { + return m_versions; + } public: // for usage only by parsers void setName(const QString &name); + void setParentUid(const QString &parentUid); void setVersions(const QVector &versions); void merge(const BaseEntity::Ptr &other) override; void parse(const QJsonObject &obj) override; @@ -74,12 +88,15 @@ signals: void nameChanged(const QString &name); protected slots: - void updateListData(QList) override {} + void updateListData(QList) override + { + } private: QVector m_versions; QHash m_lookup; QString m_uid; + QString m_parentUid; QString m_name; VersionPtr m_recommended; @@ -87,6 +104,5 @@ private: void setupAddedVersion(const int row, const VersionPtr &version); }; - } Q_DECLARE_METATYPE(Meta::VersionListPtr) -- cgit v1.2.3 From 6f2a87167a13101d80d2fbc096bbb6a5eb5ab0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 26 Mar 2017 20:50:31 +0200 Subject: NOISSUE remove some dead code from version related classes --- api/logic/meta/Version.h | 6 ------ 1 file changed, 6 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 1d921035..8aac8ea1 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -35,12 +35,6 @@ 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 parentUid READ parentUid) - 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(QHash requires READ requires NOTIFY requiresChanged) public: /* con/des */ explicit Version(const QString &uid, const QString &version); -- cgit v1.2.3 From 5fabb4f2546fa6b79a4e2c29679f506e587a0070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 27 Mar 2017 03:34:39 +0200 Subject: NOISSUE Rough refactor of ProfilePatch and VersionFile internals. They are now distinct classes with distinct responsibilities. * ProfilePatch is an entry in MinecraftProfile and can hold VersionFile or Meta::Version. * VersionFile is the basic element that holds version information loaded from JSON. * Meta::Version is the loader class for VersionFile(s) from a server. --- api/logic/meta/Version.cpp | 2 +- api/logic/meta/VersionList.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index af9d4f10..2a8e1780 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -32,7 +32,7 @@ QString Meta::Version::descriptor() QString Meta::Version::name() { if(m_data) - return m_data->getName(); + return m_data->name; return m_uid; } QString Meta::Version::typeString() const diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index c2712155..41ed1352 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -73,7 +73,7 @@ QVariant VersionList::data(const QModelIndex &index, int role) const case VersionRole: case VersionIdRole: return version->version(); - case ParentGameVersionRole: + case ParentVersionRole: { auto parentUid = this->parentUid(); if(parentUid.isEmpty()) @@ -102,7 +102,7 @@ QVariant VersionList::data(const QModelIndex &index, int role) const BaseVersionList::RoleList VersionList::providesRoles() const { - return {VersionPointerRole, VersionRole, VersionIdRole, ParentGameVersionRole, + return {VersionPointerRole, VersionRole, VersionIdRole, ParentVersionRole, TypeRole, UidRole, TimeRole, RequiresRole, SortRole, RecommendedRole, LatestRole, VersionPtrRole}; } -- cgit v1.2.3 From 643d74f66c2b22660264a393d5c3fa38f68a5d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 6 Apr 2017 23:30:18 +0200 Subject: NOISSUE implement recommended versions using the new JSON format --- api/logic/meta/JsonFormat.cpp | 5 ++++- api/logic/meta/Version.cpp | 16 ++++++++++++++++ api/logic/meta/Version.h | 8 ++++++++ api/logic/meta/VersionList.cpp | 15 +++------------ api/logic/meta/VersionList.h | 2 -- 5 files changed, 31 insertions(+), 15 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/JsonFormat.cpp b/api/logic/meta/JsonFormat.cpp index 8a063f48..1638105a 100644 --- a/api/logic/meta/JsonFormat.cpp +++ b/api/logic/meta/JsonFormat.cpp @@ -52,6 +52,7 @@ static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); version->setType(ensureString(obj, "type", QString())); version->setParentUid(ensureString(obj, "parentUid", QString())); + version->setRecommended(ensureBoolean(obj, "recommended", false)); if(obj.contains("requires")) { QHash requires; @@ -87,7 +88,9 @@ static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) versions.reserve(versionsRaw.size()); std::transform(versionsRaw.begin(), versionsRaw.end(), std::back_inserter(versions), [uid](const QJsonObject &vObj) { - return parseCommonVersion(uid, vObj); + auto version = parseCommonVersion(uid, vObj); + version->setProvidesRecommendations(); + return version; }); VersionListPtr list = std::make_shared(uid); diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index 2a8e1780..338e180b 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -53,6 +53,13 @@ void Meta::Version::parse(const QJsonObject& obj) void Meta::Version::merge(const std::shared_ptr &other) { VersionPtr version = std::dynamic_pointer_cast(other); + if(version->m_providesRecommendations) + { + if(m_recommended != version->m_recommended) + { + setRecommended(version->m_recommended); + } + } if (m_type != version->m_type) { setType(version->m_type); @@ -107,3 +114,12 @@ void Meta::Version::setData(const VersionFilePtr &data) m_data = data; } +void Meta::Version::setProvidesRecommendations() +{ + m_providesRecommendations = true; +} + +void Meta::Version::setRecommended(bool recommended) +{ + m_recommended = recommended; +} diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 8aac8ea1..f132b861 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -72,6 +72,10 @@ public: /* con/des */ { return m_data; } + bool isRecommended() const + { + return m_recommended; + } void merge(const std::shared_ptr &other) override; void parse(const QJsonObject &obj) override; @@ -83,6 +87,8 @@ public: // for usage by format parsers only void setType(const QString &type); void setTime(const qint64 time); void setRequires(const QHash &requires); + void setRecommended(bool recommended); + void setProvidesRecommendations(); void setData(const VersionFilePtr &data); signals: @@ -91,6 +97,8 @@ signals: void requiresChanged(); private: + bool m_providesRecommendations = false; + bool m_recommended = false; QString m_name; QString m_uid; QString m_parentUid; diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index 41ed1352..0f1404ba 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -94,8 +94,9 @@ QVariant VersionList::data(const QModelIndex &index, int role) const 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(); + case RecommendedRole: return version->isRecommended(); + // FIXME: this should be determined in whatever view/proxy is used... + // case LatestRole: return version == getLatestStable(); default: return QVariant(); } } @@ -158,7 +159,6 @@ void VersionList::setVersions(const QVector &versions) 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(); @@ -202,11 +202,6 @@ void VersionList::merge(const BaseEntity::Ptr &other) 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; @@ -224,10 +219,6 @@ void VersionList::setupAddedVersion(const int row, const VersionPtr &version) 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; diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index faacdbf8..e8016314 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -47,7 +47,6 @@ public: int count() const override; void sortVersions() override; - BaseVersionPtr getLatestStable() const override; BaseVersionPtr getRecommended() const override; QVariant data(const QModelIndex &index, int role) const override; @@ -100,7 +99,6 @@ private: QString m_name; VersionPtr m_recommended; - VersionPtr m_latest; void setupAddedVersion(const int row, const VersionPtr &version); }; -- cgit v1.2.3 From dddc5cedf380d1321b94b0bc7d244d23289e6d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 6 Apr 2017 23:45:24 +0200 Subject: NOISSUE make a call to ensureBoolean in the JSON format code less ambiguous --- api/logic/meta/JsonFormat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'api/logic/meta') diff --git a/api/logic/meta/JsonFormat.cpp b/api/logic/meta/JsonFormat.cpp index 1638105a..fda1cf97 100644 --- a/api/logic/meta/JsonFormat.cpp +++ b/api/logic/meta/JsonFormat.cpp @@ -52,7 +52,7 @@ static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); version->setType(ensureString(obj, "type", QString())); version->setParentUid(ensureString(obj, "parentUid", QString())); - version->setRecommended(ensureBoolean(obj, "recommended", false)); + version->setRecommended(ensureBoolean(obj, QString("recommended"), false)); if(obj.contains("requires")) { QHash requires; -- cgit v1.2.3