From ab868df50eb6f9f3958bdc0a7ab9199dcdf46b3c Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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 From 8321187a20eec9871f327aa23f421e79dcfdd409 Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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 +++++++------- application/CMakeLists.txt | 6 +- application/MultiMC.cpp | 2 + application/VersionProxyModel.cpp | 2 +- application/pages/global/MetadataPage.cpp | 242 ---------------------------- application/pages/global/MetadataPage.h | 57 ------- application/pages/global/MetadataPage.ui | 252 ------------------------------ application/pages/global/PackagesPage.cpp | 242 ++++++++++++++++++++++++++++ application/pages/global/PackagesPage.h | 57 +++++++ application/pages/global/PackagesPage.ui | 252 ++++++++++++++++++++++++++++++ 13 files changed, 640 insertions(+), 623 deletions(-) delete mode 100644 application/pages/global/MetadataPage.cpp delete mode 100644 application/pages/global/MetadataPage.h delete mode 100644 application/pages/global/MetadataPage.ui create mode 100644 application/pages/global/PackagesPage.cpp create mode 100644 application/pages/global/PackagesPage.h create mode 100644 application/pages/global/PackagesPage.ui (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; diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt index 55457b1c..1b6ff3fd 100644 --- a/application/CMakeLists.txt +++ b/application/CMakeLists.txt @@ -184,8 +184,8 @@ SET(MULTIMC_SOURCES pages/global/ProxyPage.h pages/global/PasteEEPage.cpp pages/global/PasteEEPage.h - pages/global/MetadataPage.cpp - pages/global/MetadataPage.h + pages/global/PackagesPage.cpp + pages/global/PackagesPage.h # GUI - dialogs dialogs/AboutDialog.cpp @@ -284,7 +284,7 @@ SET(MULTIMC_UIS pages/global/MultiMCPage.ui pages/global/ProxyPage.ui pages/global/PasteEEPage.ui - pages/global/MetadataPage.ui + pages/global/PackagesPage.ui # Dialogs dialogs/CopyInstanceDialog.ui diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 081de41c..3a91fb5f 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -10,6 +10,7 @@ #include "pages/global/ExternalToolsPage.h" #include "pages/global/AccountListPage.h" #include "pages/global/PasteEEPage.h" +#include "pages/global/PackagesPage.h" #include "themes/ITheme.h" #include "themes/SystemTheme.h" @@ -842,6 +843,7 @@ void MultiMC::initGlobalSettings() m_globalSettingsProvider->addPage(); m_globalSettingsProvider->addPage(); m_globalSettingsProvider->addPage(); + m_globalSettingsProvider->addPage(); m_globalSettingsProvider->addPage(); m_globalSettingsProvider->addPage(); m_globalSettingsProvider->addPage(); diff --git a/application/VersionProxyModel.cpp b/application/VersionProxyModel.cpp index 22df7e09..50b94d9e 100644 --- a/application/VersionProxyModel.cpp +++ b/application/VersionProxyModel.cpp @@ -313,9 +313,9 @@ void VersionProxyModel::setSourceModel(QAbstractItemModel *replacingRaw) auto replacing = dynamic_cast(replacingRaw); beginResetModel(); + m_columns.clear(); if(!replacing) { - m_columns.clear(); roles.clear(); filterModel->setSourceModel(replacing); return; diff --git a/application/pages/global/MetadataPage.cpp b/application/pages/global/MetadataPage.cpp deleted file mode 100644 index 4e355997..00000000 --- a/application/pages/global/MetadataPage.cpp +++ /dev/null @@ -1,242 +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 "MetadataPage.h" -#include "ui_MetadataPage.h" - -#include -#include -#include - -#include "dialogs/ProgressDialog.h" -#include "VersionProxyModel.h" - -#include "meta/Index.h" -#include "meta/VersionList.h" -#include "meta/Version.h" -#include "Env.h" -#include "MultiMC.h" - -using namespace Meta; - -static QString formatRequires(const VersionPtr &version) -{ - QStringList lines; - for (const Reference &ref : version->requires()) - { - const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->getList(ref.uid())->humanReadable() : ref.uid(); - if (ref.version().isEmpty()) - { - lines.append(readable); - } - else - { - lines.append(QString("%1 (%2)").arg(readable, ref.version())); - } - } - return lines.join('\n'); -} - -MetadataPage::MetadataPage(QWidget *parent) : - QWidget(parent), - ui(new Ui::MetadataPage) -{ - ui->setupUi(this); - ui->tabWidget->tabBar()->hide(); - - m_fileProxy = new QSortFilterProxyModel(this); - m_fileProxy->setSortRole(Qt::DisplayRole); - m_fileProxy->setSortCaseSensitivity(Qt::CaseInsensitive); - m_fileProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); - m_fileProxy->setFilterRole(Qt::DisplayRole); - m_fileProxy->setFilterKeyColumn(0); - m_fileProxy->sort(0); - m_fileProxy->setSourceModel(ENV.metadataIndex().get()); - ui->indexView->setModel(m_fileProxy); - - m_filterProxy = new QSortFilterProxyModel(this); - m_filterProxy->setSortRole(VersionList::SortRole); - m_filterProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); - m_filterProxy->setFilterRole(Qt::DisplayRole); - m_filterProxy->setFilterKeyColumn(0); - m_filterProxy->sort(0, Qt::DescendingOrder); - ui->versionsView->setModel(m_filterProxy); - - m_versionProxy = new VersionProxyModel(this); - m_filterProxy->setSourceModel(m_versionProxy); - - connect(ui->indexView->selectionModel(), &QItemSelectionModel::currentChanged, this, &MetadataPage::updateCurrentVersionList); - connect(ui->versionsView->selectionModel(), &QItemSelectionModel::currentChanged, this, &MetadataPage::updateVersion); - connect(m_filterProxy, &QSortFilterProxyModel::dataChanged, this, &MetadataPage::versionListDataChanged); - - updateCurrentVersionList(QModelIndex()); - updateVersion(); -} - -MetadataPage::~MetadataPage() -{ - delete ui; -} - -QIcon MetadataPage::icon() const -{ - return MMC->getThemedIcon("looney"); -} - -void MetadataPage::on_refreshIndexBtn_clicked() -{ - ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); -} -void MetadataPage::on_refreshFileBtn_clicked() -{ - VersionListPtr list = ui->indexView->currentIndex().data(Index::ListPtrRole).value(); - if (!list) - { - return; - } - ProgressDialog(this).execWithTask(list->remoteUpdateTask()); -} -void MetadataPage::on_refreshVersionBtn_clicked() -{ - VersionPtr version = ui->versionsView->currentIndex().data(VersionList::VersionPtrRole).value(); - if (!version) - { - return; - } - ProgressDialog(this).execWithTask(version->remoteUpdateTask()); -} - -void MetadataPage::on_fileSearchEdit_textChanged(const QString &search) -{ - if (search.isEmpty()) - { - m_fileProxy->setFilterFixedString(QString()); - } - else - { - QStringList parts = search.split(' '); - std::transform(parts.begin(), parts.end(), parts.begin(), &QRegularExpression::escape); - m_fileProxy->setFilterRegExp(".*" + parts.join(".*") + ".*"); - } -} -void MetadataPage::on_versionSearchEdit_textChanged(const QString &search) -{ - if (search.isEmpty()) - { - m_filterProxy->setFilterFixedString(QString()); - } - else - { - QStringList parts = search.split(' '); - std::transform(parts.begin(), parts.end(), parts.begin(), &QRegularExpression::escape); - m_filterProxy->setFilterRegExp(".*" + parts.join(".*") + ".*"); - } -} - -void MetadataPage::updateCurrentVersionList(const QModelIndex &index) -{ - if (index.isValid()) - { - VersionListPtr list = index.data(Index::ListPtrRole).value(); - ui->versionsBox->setEnabled(true); - ui->refreshFileBtn->setEnabled(true); - ui->fileUidLabel->setEnabled(true); - ui->fileUid->setText(list->uid()); - ui->fileNameLabel->setEnabled(true); - ui->fileName->setText(list->name()); - m_versionProxy->setSourceModel(list.get()); - ui->refreshFileBtn->setText(tr("Refresh %1").arg(list->humanReadable())); - - if (!list->isLocalLoaded()) - { - std::unique_ptr task = list->localUpdateTask(); - connect(task.get(), &Task::finished, this, [this, list]() - { - if (list->count() == 0 && !list->isRemoteLoaded()) - { - ProgressDialog(this).execWithTask(list->remoteUpdateTask()); - } - }); - ProgressDialog(this).execWithTask(task); - } - } - else - { - ui->versionsBox->setEnabled(false); - ui->refreshFileBtn->setEnabled(false); - ui->fileUidLabel->setEnabled(false); - ui->fileUid->clear(); - ui->fileNameLabel->setEnabled(false); - ui->fileName->clear(); - m_versionProxy->setSourceModel(nullptr); - ui->refreshFileBtn->setText(tr("Refresh ___")); - } -} - -void MetadataPage::versionListDataChanged(const QModelIndex &tl, const QModelIndex &br) -{ - if (QItemSelection(tl, br).contains(ui->versionsView->currentIndex())) - { - updateVersion(); - } -} - -void MetadataPage::updateVersion() -{ - VersionPtr version = std::dynamic_pointer_cast( - ui->versionsView->currentIndex().data(VersionList::VersionPointerRole).value()); - if (version) - { - ui->refreshVersionBtn->setEnabled(true); - ui->versionVersionLabel->setEnabled(true); - ui->versionVersion->setText(version->version()); - ui->versionTimeLabel->setEnabled(true); - ui->versionTime->setText(version->time().toString("yyyy-MM-dd HH:mm")); - ui->versionTypeLabel->setEnabled(true); - ui->versionType->setText(version->type()); - ui->versionRequiresLabel->setEnabled(true); - ui->versionRequires->setText(formatRequires(version)); - ui->refreshVersionBtn->setText(tr("Refresh %1").arg(version->version())); - } - else - { - ui->refreshVersionBtn->setEnabled(false); - ui->versionVersionLabel->setEnabled(false); - ui->versionVersion->clear(); - ui->versionTimeLabel->setEnabled(false); - ui->versionTime->clear(); - ui->versionTypeLabel->setEnabled(false); - ui->versionType->clear(); - ui->versionRequiresLabel->setEnabled(false); - ui->versionRequires->clear(); - ui->refreshVersionBtn->setText(tr("Refresh ___")); - } -} - -void MetadataPage::opened() -{ - if (!ENV.metadataIndex()->isLocalLoaded()) - { - std::unique_ptr task = ENV.metadataIndex()->localUpdateTask(); - connect(task.get(), &Task::finished, this, [this]() - { - if (!ENV.metadataIndex()->isRemoteLoaded()) - { - ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); - } - }); - ProgressDialog(this).execWithTask(task); - } -} diff --git a/application/pages/global/MetadataPage.h b/application/pages/global/MetadataPage.h deleted file mode 100644 index f75b4952..00000000 --- a/application/pages/global/MetadataPage.h +++ /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. - */ - -#pragma once - -#include - -#include "pages/BasePage.h" - -namespace Ui { -class MetadataPage; -} - -class QSortFilterProxyModel; -class VersionProxyModel; - -class MetadataPage : public QWidget, public BasePage -{ - Q_OBJECT -public: - explicit MetadataPage(QWidget *parent = 0); - ~MetadataPage(); - - QString id() const override { return "metadata-global"; } - QString displayName() const override { return tr("Metadata"); } - QIcon icon() const override; - void opened() override; - -private slots: - void on_refreshIndexBtn_clicked(); - void on_refreshFileBtn_clicked(); - void on_refreshVersionBtn_clicked(); - void on_fileSearchEdit_textChanged(const QString &search); - void on_versionSearchEdit_textChanged(const QString &search); - void updateCurrentVersionList(const QModelIndex &index); - void versionListDataChanged(const QModelIndex &tl, const QModelIndex &br); - -private: - Ui::MetadataPage *ui; - QSortFilterProxyModel *m_fileProxy; - QSortFilterProxyModel *m_filterProxy; - VersionProxyModel *m_versionProxy; - - void updateVersion(); -}; diff --git a/application/pages/global/MetadataPage.ui b/application/pages/global/MetadataPage.ui deleted file mode 100644 index 0bd33f32..00000000 --- a/application/pages/global/MetadataPage.ui +++ /dev/null @@ -1,252 +0,0 @@ - - - MetadataPage - - - - 0 - 0 - 640 - 480 - - - - Form - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 0 - - - - Tab 1 - - - - - - Versions - - - - - - Search... - - - true - - - - - - - true - - - false - - - - - - - - - Refresh ___ - - - - - - - - - - - Version: - - - - - - - - - - - - - - Time: - - - - - - - - - - - - - - Type: - - - - - - - - - - - - - - Dependencies: - - - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - Resources - - - - - - Search... - - - true - - - - - - - true - - - false - - - - - - - - - Refresh ___ - - - - - - - - - - - UID: - - - - - - - - - - - - - - Name: - - - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - Refresh Index - - - - - - - - - - - - diff --git a/application/pages/global/PackagesPage.cpp b/application/pages/global/PackagesPage.cpp new file mode 100644 index 00000000..52efaf9a --- /dev/null +++ b/application/pages/global/PackagesPage.cpp @@ -0,0 +1,242 @@ +/* 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 "PackagesPage.h" +#include "ui_PackagesPage.h" + +#include +#include +#include + +#include "dialogs/ProgressDialog.h" +#include "VersionProxyModel.h" + +#include "meta/Index.h" +#include "meta/VersionList.h" +#include "meta/Version.h" +#include "Env.h" +#include "MultiMC.h" + +using namespace Meta; + +static QString formatRequires(const VersionPtr &version) +{ + QStringList lines; + for (const Reference &ref : version->requires()) + { + const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->getList(ref.uid())->humanReadable() : ref.uid(); + if (ref.version().isEmpty()) + { + lines.append(readable); + } + else + { + lines.append(QString("%1 (%2)").arg(readable, ref.version())); + } + } + return lines.join('\n'); +} + +PackagesPage::PackagesPage(QWidget *parent) : + QWidget(parent), + ui(new Ui::PackagesPage) +{ + ui->setupUi(this); + ui->tabWidget->tabBar()->hide(); + + m_fileProxy = new QSortFilterProxyModel(this); + m_fileProxy->setSortRole(Qt::DisplayRole); + m_fileProxy->setSortCaseSensitivity(Qt::CaseInsensitive); + m_fileProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); + m_fileProxy->setFilterRole(Qt::DisplayRole); + m_fileProxy->setFilterKeyColumn(0); + m_fileProxy->sort(0); + m_fileProxy->setSourceModel(ENV.metadataIndex().get()); + ui->indexView->setModel(m_fileProxy); + + m_filterProxy = new QSortFilterProxyModel(this); + m_filterProxy->setSortRole(VersionList::SortRole); + m_filterProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); + m_filterProxy->setFilterRole(Qt::DisplayRole); + m_filterProxy->setFilterKeyColumn(0); + m_filterProxy->sort(0, Qt::DescendingOrder); + ui->versionsView->setModel(m_filterProxy); + + m_versionProxy = new VersionProxyModel(this); + m_filterProxy->setSourceModel(m_versionProxy); + + connect(ui->indexView->selectionModel(), &QItemSelectionModel::currentChanged, this, &PackagesPage::updateCurrentVersionList); + connect(ui->versionsView->selectionModel(), &QItemSelectionModel::currentChanged, this, &PackagesPage::updateVersion); + connect(m_filterProxy, &QSortFilterProxyModel::dataChanged, this, &PackagesPage::versionListDataChanged); + + updateCurrentVersionList(QModelIndex()); + updateVersion(); +} + +PackagesPage::~PackagesPage() +{ + delete ui; +} + +QIcon PackagesPage::icon() const +{ + return MMC->getThemedIcon("packages"); +} + +void PackagesPage::on_refreshIndexBtn_clicked() +{ + ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); +} +void PackagesPage::on_refreshFileBtn_clicked() +{ + VersionListPtr list = ui->indexView->currentIndex().data(Index::ListPtrRole).value(); + if (!list) + { + return; + } + ProgressDialog(this).execWithTask(list->remoteUpdateTask()); +} +void PackagesPage::on_refreshVersionBtn_clicked() +{ + VersionPtr version = ui->versionsView->currentIndex().data(VersionList::VersionPtrRole).value(); + if (!version) + { + return; + } + ProgressDialog(this).execWithTask(version->remoteUpdateTask()); +} + +void PackagesPage::on_fileSearchEdit_textChanged(const QString &search) +{ + if (search.isEmpty()) + { + m_fileProxy->setFilterFixedString(QString()); + } + else + { + QStringList parts = search.split(' '); + std::transform(parts.begin(), parts.end(), parts.begin(), &QRegularExpression::escape); + m_fileProxy->setFilterRegExp(".*" + parts.join(".*") + ".*"); + } +} +void PackagesPage::on_versionSearchEdit_textChanged(const QString &search) +{ + if (search.isEmpty()) + { + m_filterProxy->setFilterFixedString(QString()); + } + else + { + QStringList parts = search.split(' '); + std::transform(parts.begin(), parts.end(), parts.begin(), &QRegularExpression::escape); + m_filterProxy->setFilterRegExp(".*" + parts.join(".*") + ".*"); + } +} + +void PackagesPage::updateCurrentVersionList(const QModelIndex &index) +{ + if (index.isValid()) + { + VersionListPtr list = index.data(Index::ListPtrRole).value(); + ui->versionsBox->setEnabled(true); + ui->refreshFileBtn->setEnabled(true); + ui->fileUidLabel->setEnabled(true); + ui->fileUid->setText(list->uid()); + ui->fileNameLabel->setEnabled(true); + ui->fileName->setText(list->name()); + m_versionProxy->setSourceModel(list.get()); + ui->refreshFileBtn->setText(tr("Refresh %1").arg(list->humanReadable())); + + if (!list->isLocalLoaded()) + { + std::unique_ptr task = list->localUpdateTask(); + connect(task.get(), &Task::finished, this, [this, list]() + { + if (list->count() == 0 && !list->isRemoteLoaded()) + { + ProgressDialog(this).execWithTask(list->remoteUpdateTask()); + } + }); + ProgressDialog(this).execWithTask(task); + } + } + else + { + ui->versionsBox->setEnabled(false); + ui->refreshFileBtn->setEnabled(false); + ui->fileUidLabel->setEnabled(false); + ui->fileUid->clear(); + ui->fileNameLabel->setEnabled(false); + ui->fileName->clear(); + m_versionProxy->setSourceModel(nullptr); + ui->refreshFileBtn->setText(tr("Refresh")); + } +} + +void PackagesPage::versionListDataChanged(const QModelIndex &tl, const QModelIndex &br) +{ + if (QItemSelection(tl, br).contains(ui->versionsView->currentIndex())) + { + updateVersion(); + } +} + +void PackagesPage::updateVersion() +{ + VersionPtr version = std::dynamic_pointer_cast( + ui->versionsView->currentIndex().data(VersionList::VersionPointerRole).value()); + if (version) + { + ui->refreshVersionBtn->setEnabled(true); + ui->versionVersionLabel->setEnabled(true); + ui->versionVersion->setText(version->version()); + ui->versionTimeLabel->setEnabled(true); + ui->versionTime->setText(version->time().toString("yyyy-MM-dd HH:mm")); + ui->versionTypeLabel->setEnabled(true); + ui->versionType->setText(version->type()); + ui->versionRequiresLabel->setEnabled(true); + ui->versionRequires->setText(formatRequires(version)); + ui->refreshVersionBtn->setText(tr("Refresh %1").arg(version->version())); + } + else + { + ui->refreshVersionBtn->setEnabled(false); + ui->versionVersionLabel->setEnabled(false); + ui->versionVersion->clear(); + ui->versionTimeLabel->setEnabled(false); + ui->versionTime->clear(); + ui->versionTypeLabel->setEnabled(false); + ui->versionType->clear(); + ui->versionRequiresLabel->setEnabled(false); + ui->versionRequires->clear(); + ui->refreshVersionBtn->setText(tr("Refresh")); + } +} + +void PackagesPage::opened() +{ + if (!ENV.metadataIndex()->isLocalLoaded()) + { + std::unique_ptr task = ENV.metadataIndex()->localUpdateTask(); + connect(task.get(), &Task::finished, this, [this]() + { + if (!ENV.metadataIndex()->isRemoteLoaded()) + { + ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); + } + }); + ProgressDialog(this).execWithTask(task); + } +} diff --git a/application/pages/global/PackagesPage.h b/application/pages/global/PackagesPage.h new file mode 100644 index 00000000..80c2886d --- /dev/null +++ b/application/pages/global/PackagesPage.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 "pages/BasePage.h" + +namespace Ui { +class PackagesPage; +} + +class QSortFilterProxyModel; +class VersionProxyModel; + +class PackagesPage : public QWidget, public BasePage +{ + Q_OBJECT +public: + explicit PackagesPage(QWidget *parent = 0); + ~PackagesPage(); + + QString id() const override { return "packages-global"; } + QString displayName() const override { return tr("Packages"); } + QIcon icon() const override; + void opened() override; + +private slots: + void on_refreshIndexBtn_clicked(); + void on_refreshFileBtn_clicked(); + void on_refreshVersionBtn_clicked(); + void on_fileSearchEdit_textChanged(const QString &search); + void on_versionSearchEdit_textChanged(const QString &search); + void updateCurrentVersionList(const QModelIndex &index); + void versionListDataChanged(const QModelIndex &tl, const QModelIndex &br); + +private: + Ui::PackagesPage *ui; + QSortFilterProxyModel *m_fileProxy; + QSortFilterProxyModel *m_filterProxy; + VersionProxyModel *m_versionProxy; + + void updateVersion(); +}; diff --git a/application/pages/global/PackagesPage.ui b/application/pages/global/PackagesPage.ui new file mode 100644 index 00000000..158bf1b4 --- /dev/null +++ b/application/pages/global/PackagesPage.ui @@ -0,0 +1,252 @@ + + + PackagesPage + + + + 0 + 0 + 636 + 621 + + + + Form + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + + Tab 1 + + + + + + Versions + + + + + + Search... + + + true + + + + + + + true + + + false + + + + + + + + + Refresh + + + + + + + + + + + Version: + + + + + + + + + + + + + + Time: + + + + + + + + + + + + + + Type: + + + + + + + + + + + + + + Dependencies: + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + Resources + + + + + + Search... + + + true + + + + + + + true + + + false + + + + + + + + + Refresh + + + + + + + + + + + UID: + + + + + + + + + + + + + + Name: + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + Refresh Index + + + + + + + + + + + + -- cgit From 0060b506257b906d40ef53d1e23404dba76afcee Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Fri, 17 Mar 2017 01:48:54 +0100 Subject: NOISSUE simplify. --- api/logic/CMakeLists.txt | 2 - api/logic/Env.cpp | 2 +- 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 +-------- application/pages/global/PackagesPage.cpp | 2 +- 21 files changed, 192 insertions(+), 537 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/CMakeLists.txt b/api/logic/CMakeLists.txt index b605fbd3..42754157 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -420,8 +420,6 @@ set(META_SOURCES meta/tasks/RemoteLoadTask.h meta/tasks/LocalLoadTask.cpp meta/tasks/LocalLoadTask.h - meta/format/FormatV1.cpp - meta/format/FormatV1.h meta/format/Format.cpp meta/format/Format.h meta/BaseEntity.cpp diff --git a/api/logic/Env.cpp b/api/logic/Env.cpp index 903f1d8a..d7be4a28 100644 --- a/api/logic/Env.cpp +++ b/api/logic/Env.cpp @@ -124,7 +124,7 @@ void Env::initHttpMetaCache() m_metacache->addBase("root", QDir::currentPath()); m_metacache->addBase("translations", QDir("translations").absolutePath()); m_metacache->addBase("icons", QDir("cache/icons").absolutePath()); - m_metacache->addBase("meta", QDir("cache/meta").absolutePath()); + m_metacache->addBase("meta", QDir("meta").absolutePath()); m_metacache->Load(); } 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; -}; } diff --git a/application/pages/global/PackagesPage.cpp b/application/pages/global/PackagesPage.cpp index 52efaf9a..81ad4da1 100644 --- a/application/pages/global/PackagesPage.cpp +++ b/application/pages/global/PackagesPage.cpp @@ -36,7 +36,7 @@ static QString formatRequires(const VersionPtr &version) QStringList lines; for (const Reference &ref : version->requires()) { - const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->getList(ref.uid())->humanReadable() : ref.uid(); + const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->get(ref.uid())->humanReadable() : ref.uid(); if (ref.version().isEmpty()) { lines.append(readable); -- cgit From e46aba9da584338db8d8a1a8a487bdcc6cf84343 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sat, 18 Mar 2017 02:22:36 +0100 Subject: NOISSUE sanitize loading and downloading of metadata files --- api/logic/CMakeLists.txt | 10 +-- 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 ---------- application/MainWindow.cpp | 15 +--- application/pages/global/PackagesPage.cpp | 33 ++----- 22 files changed, 340 insertions(+), 686 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/CMakeLists.txt b/api/logic/CMakeLists.txt index 42754157..544ace80 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -416,12 +416,8 @@ set(TOOLS_SOURCES set(META_SOURCES # Metadata sources - meta/tasks/RemoteLoadTask.cpp - meta/tasks/RemoteLoadTask.h - meta/tasks/LocalLoadTask.cpp - meta/tasks/LocalLoadTask.h - meta/format/Format.cpp - meta/format/Format.h + meta/JsonFormat.cpp + meta/JsonFormat.h meta/BaseEntity.cpp meta/BaseEntity.h meta/VersionList.cpp @@ -430,8 +426,6 @@ set(META_SOURCES meta/Version.h meta/Index.cpp meta/Index.h - meta/Util.cpp - meta/Util.h meta/Reference.cpp meta/Reference.h ) 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; -}; -} diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 9073c006..e0870d06 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -554,21 +554,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow job->start(); } - // run the things that load and download other things... FIXME: this is NOT the place - // FIXME: invisible actions in the background = NOPE. + // load the news { - /* - if (!MMC->minecraftlist()->isLoaded()) - { - m_versionLoadTask = MMC->minecraftlist()->getLoadTask(); - startTask(m_versionLoadTask); - } - if (!MMC->lwjgllist()->isLoaded()) - { - MMC->lwjgllist()->loadList(); - } - */ - m_newsChecker->reloadNews(); updateNewsLabel(); } diff --git a/application/pages/global/PackagesPage.cpp b/application/pages/global/PackagesPage.cpp index 81ad4da1..e4967532 100644 --- a/application/pages/global/PackagesPage.cpp +++ b/application/pages/global/PackagesPage.cpp @@ -97,7 +97,7 @@ QIcon PackagesPage::icon() const void PackagesPage::on_refreshIndexBtn_clicked() { - ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); + ENV.metadataIndex()->load(); } void PackagesPage::on_refreshFileBtn_clicked() { @@ -106,7 +106,7 @@ void PackagesPage::on_refreshFileBtn_clicked() { return; } - ProgressDialog(this).execWithTask(list->remoteUpdateTask()); + list->load(); } void PackagesPage::on_refreshVersionBtn_clicked() { @@ -115,7 +115,7 @@ void PackagesPage::on_refreshVersionBtn_clicked() { return; } - ProgressDialog(this).execWithTask(version->remoteUpdateTask()); + version->load(); } void PackagesPage::on_fileSearchEdit_textChanged(const QString &search) @@ -158,19 +158,7 @@ void PackagesPage::updateCurrentVersionList(const QModelIndex &index) ui->fileName->setText(list->name()); m_versionProxy->setSourceModel(list.get()); ui->refreshFileBtn->setText(tr("Refresh %1").arg(list->humanReadable())); - - if (!list->isLocalLoaded()) - { - std::unique_ptr task = list->localUpdateTask(); - connect(task.get(), &Task::finished, this, [this, list]() - { - if (list->count() == 0 && !list->isRemoteLoaded()) - { - ProgressDialog(this).execWithTask(list->remoteUpdateTask()); - } - }); - ProgressDialog(this).execWithTask(task); - } + list->load(); } else { @@ -227,16 +215,5 @@ void PackagesPage::updateVersion() void PackagesPage::opened() { - if (!ENV.metadataIndex()->isLocalLoaded()) - { - std::unique_ptr task = ENV.metadataIndex()->localUpdateTask(); - connect(task.get(), &Task::finished, this, [this]() - { - if (!ENV.metadataIndex()->isRemoteLoaded()) - { - ProgressDialog(this).execWithTask(ENV.metadataIndex()->remoteUpdateTask()); - } - }); - ProgressDialog(this).execWithTask(task); - } + ENV.metadataIndex()->load(); } -- cgit From 2660418d58efb33cd3a0ab8ed9d48c359c076905 Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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 +- api/logic/minecraft/ProfilePatch.h | 4 -- .../minecraft/onesix/OneSixProfileStrategy.cpp | 6 +- api/logic/minecraft/onesix/OneSixUpdate.cpp | 48 +++++++------ application/pages/VersionPage.cpp | 1 + 12 files changed, 162 insertions(+), 58 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; } diff --git a/api/logic/minecraft/ProfilePatch.h b/api/logic/minecraft/ProfilePatch.h index 26230092..b61fb8b3 100644 --- a/api/logic/minecraft/ProfilePatch.h +++ b/api/logic/minecraft/ProfilePatch.h @@ -92,10 +92,6 @@ public: { return m_problemSeverity; } - virtual bool hasFailed() - { - return getProblemSeverity() == PROBLEM_ERROR; - } protected: QList m_problems; diff --git a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp index e3d3f674..b19a2dea 100644 --- a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp +++ b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include OneSixProfileStrategy::OneSixProfileStrategy(OneSixInstance* instance) { @@ -98,7 +100,7 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() } else { - auto mcversion = ENV.getVersion("net.minecraft", m_instance->intendedVersionId()); + auto mcversion = ENV.metadataIndex()->get("net.minecraft", m_instance->intendedVersionId()); minecraftPatch = std::dynamic_pointer_cast(mcversion); } if (!minecraftPatch) @@ -121,7 +123,7 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() } else { - auto lwjglversion = ENV.getVersion("org.lwjgl", "2.9.1" /*m_instance->intendedVersionId()*/); + auto lwjglversion = ENV.metadataIndex()->get("org.lwjgl", "2.9.1"); lwjglPatch = std::dynamic_pointer_cast(lwjglversion); } if (!lwjglPatch) diff --git a/api/logic/minecraft/onesix/OneSixUpdate.cpp b/api/logic/minecraft/onesix/OneSixUpdate.cpp index ec40e086..5bc76b01 100644 --- a/api/logic/minecraft/onesix/OneSixUpdate.cpp +++ b/api/logic/minecraft/onesix/OneSixUpdate.cpp @@ -34,6 +34,9 @@ #include "update/FMLLibrariesTask.h" #include "update/AssetUpdateTask.h" +#include +#include + OneSixUpdate::OneSixUpdate(OneSixInstance *inst, QObject *parent) : Task(parent), m_inst(inst) { // create folders @@ -44,30 +47,22 @@ OneSixUpdate::OneSixUpdate(OneSixInstance *inst, QObject *parent) : Task(parent) // add a version update task, if necessary { /* - auto list = std::dynamic_pointer_cast(ENV.getVersionList("net.minecraft")); - auto version = std::dynamic_pointer_cast(list->findVersion(m_inst->intendedVersionId())); - if (version == nullptr) - { - // don't do anything if it was invalid - m_preFailure = tr("The specified Minecraft version is invalid. Choose a different one."); - } - else if (m_inst->providesVersionFile() || !version->needsUpdate()) + * FIXME: there are some corner cases here that remain unhandled: + * what if local load succeeds but remote fails? The version is still usable... + */ + // FIXME: derive this from the actual list of version patches... + auto loadVersion = [&](const QString & uid, const QString & version) { - qDebug() << "Instance either provides a version file or doesn't need an update."; - } - else - { - auto versionUpdateTask = list->createUpdateTask(m_inst->intendedVersionId()); - if (!versionUpdateTask) - { - qDebug() << "Didn't spawn an update task."; - } - else + auto obj = ENV.metadataIndex()->get(uid, version); + obj->load(); + auto task = obj->getCurrentTask(); + if(task) { - m_tasks.append(versionUpdateTask); + m_tasks.append(task.unwrap()); } - } - */ + }; + loadVersion("org.lwjgl", "2.9.1"); + loadVersion("net.minecraft", m_inst->intendedVersionId()); } // libraries download @@ -118,11 +113,20 @@ void OneSixUpdate::next() return; } auto task = m_tasks[m_currentTask]; + // if the task is already finished by the time we look at it, skip it + if(task->isFinished()) + { + next(); + } connect(task.get(), &Task::succeeded, this, &OneSixUpdate::subtaskSucceeded); connect(task.get(), &Task::failed, this, &OneSixUpdate::subtaskFailed); connect(task.get(), &Task::progress, this, &OneSixUpdate::progress); connect(task.get(), &Task::status, this, &OneSixUpdate::setStatus); - task->start(); + // if the task is already running, do not start it again + if(!task->isRunning()) + { + task->start(); + } } void OneSixUpdate::subtaskSucceeded() diff --git a/application/pages/VersionPage.cpp b/application/pages/VersionPage.cpp index 42eb31de..78e2a888 100644 --- a/application/pages/VersionPage.cpp +++ b/application/pages/VersionPage.cpp @@ -320,6 +320,7 @@ void VersionPage::on_moveDownBtn_clicked() void VersionPage::on_changeVersionBtn_clicked() { + // FIXME: this is hilariously broken because it assumes m_inst->versionList() is a sensible thing... VersionSelectDialog vselect(m_inst->versionList().get(), tr("Change Minecraft version"), this); if (!vselect.exec() || !vselect.selectedVersion()) -- cgit From f557c1367994a61935fa0e8d0b1b67688d2692d0 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sun, 19 Mar 2017 23:58:54 +0100 Subject: NOISSUE stuff and things happened. Maybe. --- api/logic/BaseVersionList.h | 13 ++- api/logic/java/JavaInstallList.cpp | 2 +- api/logic/java/JavaInstallList.h | 18 +-- 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 +- api/logic/minecraft/MinecraftInstance.cpp | 4 +- api/logic/minecraft/MojangVersionFormat.cpp | 2 +- api/logic/minecraft/ProfilePatch.h | 2 - api/logic/minecraft/ProfileUtils.cpp | 2 +- api/logic/minecraft/VersionFile.cpp | 9 +- api/logic/minecraft/VersionFile.h | 17 +-- api/logic/minecraft/ftb/FTBProfileStrategy.cpp | 4 +- api/logic/minecraft/legacy/LwjglVersionList.cpp | 5 +- api/logic/minecraft/legacy/LwjglVersionList.h | 4 +- .../minecraft/onesix/OneSixProfileStrategy.cpp | 130 ++++++++++++++++++--- api/logic/minecraft/onesix/OneSixVersionFormat.cpp | 20 +++- application/pages/VersionPage.cpp | 25 +--- application/widgets/VersionSelectWidget.cpp | 44 +++---- application/widgets/VersionSelectWidget.h | 5 +- 23 files changed, 209 insertions(+), 211 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/BaseVersionList.h b/api/logic/BaseVersionList.h index eadc6933..8afcae1d 100644 --- a/api/logic/BaseVersionList.h +++ b/api/logic/BaseVersionList.h @@ -22,6 +22,7 @@ #include "BaseVersion.h" #include "tasks/Task.h" #include "multimc_logic_export.h" +#include "QObjectPtr.h" /*! * \brief Class that each instance type's version list derives from. @@ -63,7 +64,7 @@ public: * The task returned by this function should reset the model when it's done. * \return A pointer to a task that reloads the version list. */ - virtual Task *getLoadTask() = 0; + virtual shared_qobject_ptr getLoadTask() = 0; //! Checks whether or not the list is loaded. If this returns false, the list should be //loaded. @@ -76,17 +77,17 @@ public: virtual int count() const = 0; //////// List Model Functions //////// - virtual QVariant data(const QModelIndex &index, int role) const; - virtual int rowCount(const QModelIndex &parent) const; - virtual int columnCount(const QModelIndex &parent) const; - virtual QHash roleNames() const override; + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + virtual QHash roleNames() const; //! which roles are provided by this version list? virtual RoleList providesRoles() const; /*! * \brief Finds a version by its descriptor. - * \param The descriptor of the version to find. + * \param descriptor The descriptor of the version to find. * \return A const pointer to the version with the given descriptor. NULL if * one doesn't exist. */ diff --git a/api/logic/java/JavaInstallList.cpp b/api/logic/java/JavaInstallList.cpp index c98ef279..dbf66f5f 100644 --- a/api/logic/java/JavaInstallList.cpp +++ b/api/logic/java/JavaInstallList.cpp @@ -29,7 +29,7 @@ JavaInstallList::JavaInstallList(QObject *parent) : BaseVersionList(parent) { } -Task *JavaInstallList::getLoadTask() +shared_qobject_ptr JavaInstallList::getLoadTask() { return new JavaListLoadTask(this); } diff --git a/api/logic/java/JavaInstallList.h b/api/logic/java/JavaInstallList.h index 2055d154..afc5ef43 100644 --- a/api/logic/java/JavaInstallList.h +++ b/api/logic/java/JavaInstallList.h @@ -34,17 +34,17 @@ class MULTIMC_LOGIC_EXPORT JavaInstallList : public BaseVersionList public: explicit JavaInstallList(QObject *parent = 0); - virtual Task *getLoadTask() override; - virtual bool isLoaded() override; - virtual const BaseVersionPtr at(int i) const override; - virtual int count() const override; - virtual void sortVersions() override; + shared_qobject_ptr getLoadTask() override; + bool isLoaded() override; + const BaseVersionPtr at(int i) const override; + int count() const override; + void sortVersions() override; - virtual QVariant data(const QModelIndex &index, int role) const override; - virtual RoleList providesRoles() const override; + QVariant data(const QModelIndex &index, int role) const override; + RoleList providesRoles() const override; public slots: - virtual void updateListData(QList versions) override; + void updateListData(QList versions) override; protected: QList m_vlist; @@ -60,7 +60,7 @@ public: explicit JavaListLoadTask(JavaInstallList *vlist); ~JavaListLoadTask(); - virtual void executeTask(); + void executeTask() override; public slots: void javaCheckerFinished(QList results); 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; diff --git a/api/logic/minecraft/MinecraftInstance.cpp b/api/logic/minecraft/MinecraftInstance.cpp index b88d0c2a..cb080bfe 100644 --- a/api/logic/minecraft/MinecraftInstance.cpp +++ b/api/logic/minecraft/MinecraftInstance.cpp @@ -20,6 +20,8 @@ #include "minecraft/launch/ModMinecraftJar.h" #include "minecraft/launch/ClaimAccount.h" #include "java/launch/CheckJava.h" +#include +#include #include @@ -105,7 +107,7 @@ QString MinecraftInstance::binRoot() const std::shared_ptr< BaseVersionList > MinecraftInstance::versionList() const { - return ENV.getVersionList("net.minecraft"); + return ENV.metadataIndex()->get("net.minecraft"); } QStringList MinecraftInstance::javaArguments() const diff --git a/api/logic/minecraft/MojangVersionFormat.cpp b/api/logic/minecraft/MojangVersionFormat.cpp index 1312421f..651e2fbc 100644 --- a/api/logic/minecraft/MojangVersionFormat.cpp +++ b/api/logic/minecraft/MojangVersionFormat.cpp @@ -210,7 +210,7 @@ VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc readVersionProperties(root, out.get()); out->name = "Minecraft"; - out->fileId = "net.minecraft"; + out->uid = "net.minecraft"; out->version = out->minecraftVersion; out->filename = filename; diff --git a/api/logic/minecraft/ProfilePatch.h b/api/logic/minecraft/ProfilePatch.h index b61fb8b3..6f63f3c4 100644 --- a/api/logic/minecraft/ProfilePatch.h +++ b/api/logic/minecraft/ProfilePatch.h @@ -51,8 +51,6 @@ public: virtual void applyTo(MinecraftProfile *profile) = 0; virtual bool isMinecraftVersion() = 0; - virtual bool hasJarMods() = 0; - virtual QList getJarMods() = 0; virtual bool isMoveable() = 0; virtual bool isCustomizable() = 0; diff --git a/api/logic/minecraft/ProfileUtils.cpp b/api/logic/minecraft/ProfileUtils.cpp index ef9b3b28..c7c56dd6 100644 --- a/api/logic/minecraft/ProfileUtils.cpp +++ b/api/logic/minecraft/ProfileUtils.cpp @@ -102,7 +102,7 @@ bool readOverrideOrders(QString path, PatchOrder &order) static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, QString error) { auto outError = std::make_shared(); - outError->fileId = outError->name = fileId; + outError->uid = outError->name = fileId; outError->filename = filepath; outError->addProblem(PROBLEM_ERROR, error); return outError; diff --git a/api/logic/minecraft/VersionFile.cpp b/api/logic/minecraft/VersionFile.cpp index 573c4cb4..17dc6a49 100644 --- a/api/logic/minecraft/VersionFile.cpp +++ b/api/logic/minecraft/VersionFile.cpp @@ -14,12 +14,7 @@ bool VersionFile::isMinecraftVersion() { - return fileId == "net.minecraft"; -} - -bool VersionFile::hasJarMods() -{ - return !jarMods.isEmpty(); + return uid == "net.minecraft"; } void VersionFile::applyTo(MinecraftProfile *profile) @@ -29,7 +24,7 @@ void VersionFile::applyTo(MinecraftProfile *profile) { if (QRegExp(dependsOnMinecraftVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(theirVersion) == -1) { - throw MinecraftVersionMismatch(fileId, dependsOnMinecraftVersion, theirVersion); + throw MinecraftVersionMismatch(uid, dependsOnMinecraftVersion, theirVersion); } } profile->applyMinecraftVersion(minecraftVersion); diff --git a/api/logic/minecraft/VersionFile.h b/api/logic/minecraft/VersionFile.h index 249d0965..6a922d74 100644 --- a/api/logic/minecraft/VersionFile.h +++ b/api/logic/minecraft/VersionFile.h @@ -25,7 +25,6 @@ class VersionFile : public ProfilePatch public: /* methods */ virtual void applyTo(MinecraftProfile *profile) override; virtual bool isMinecraftVersion() override; - virtual bool hasJarMods() override; virtual int getOrder() override { return order; @@ -34,13 +33,9 @@ public: /* methods */ { this->order = order; } - virtual QList getJarMods() override - { - return jarMods; - } virtual QString getID() override { - return fileId; + return uid; } virtual QString getName() override { @@ -120,9 +115,6 @@ public: /* methods */ public: /* data */ - /// MultiMC: order hint for this version file if no explicit order is set - int order = 0; - // Flags for UI and version file manipulation in general bool m_isVanilla = false; bool m_isRemovable = false; @@ -130,6 +122,9 @@ public: /* data */ bool m_isCustomizable = false; bool m_isMovable = false; + /// MultiMC: order hint for this version file if no explicit order is set + int order = 0; + /// MultiMC: filename of the file this was loaded from QString filename; @@ -137,7 +132,7 @@ public: /* data */ QString name; /// MultiMC: package ID of this package - QString fileId; + QString uid; /// MultiMC: version of this package QString version; @@ -191,5 +186,3 @@ public: // Mojang: extended asset index download information std::shared_ptr mojangAssetIndex; }; - - diff --git a/api/logic/minecraft/ftb/FTBProfileStrategy.cpp b/api/logic/minecraft/ftb/FTBProfileStrategy.cpp index 92a1555a..45765cd4 100644 --- a/api/logic/minecraft/ftb/FTBProfileStrategy.cpp +++ b/api/logic/minecraft/ftb/FTBProfileStrategy.cpp @@ -27,7 +27,7 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() if(QFile::exists(mcJson)) { auto file = ProfileUtils::parseJsonFile(QFileInfo(mcJson), false); - file->fileId = "net.minecraft"; + file->uid = "net.minecraft"; file->name = QObject::tr("Minecraft (tracked)"); file->setVanilla(true); if(file->version.isEmpty()) @@ -64,7 +64,7 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() addLib->setHint("local"); addLib->setStoragePrefix(nativeInstance->librariesPath().absolutePath()); } - file->fileId = "org.multimc.ftb.pack"; + file->uid = "org.multimc.ftb.pack"; file->setVanilla(true); file->name = QObject::tr("%1 (FTB pack)").arg(m_instance->name()); if(file->version.isEmpty()) diff --git a/api/logic/minecraft/legacy/LwjglVersionList.cpp b/api/logic/minecraft/legacy/LwjglVersionList.cpp index 7f4e7b38..3d7ad2d4 100644 --- a/api/logic/minecraft/legacy/LwjglVersionList.cpp +++ b/api/logic/minecraft/legacy/LwjglVersionList.cpp @@ -99,6 +99,7 @@ inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname) void LWJGLVersionList::rssFailed(const QString& reason) { + m_rssDLJob.reset(); m_loading = false; qWarning() << "Failed to load LWJGL list. Network error: " + reason; } @@ -116,8 +117,9 @@ void LWJGLVersionList::rssSucceeded() if (!doc.setContent(m_rssData, false, &xmlErrorMsg, &errorLine)) { qWarning() << "Failed to load LWJGL list. XML error: " + xmlErrorMsg + " at line " + QString::number(errorLine); - m_loading = false; + m_rssDLJob.reset(); m_rssData.clear(); + m_loading = false; return; } m_rssData.clear(); @@ -162,5 +164,6 @@ void LWJGLVersionList::rssSucceeded() endResetModel(); qDebug() << "Loaded LWJGL list."; + m_rssDLJob.reset(); m_loading = false; } diff --git a/api/logic/minecraft/legacy/LwjglVersionList.h b/api/logic/minecraft/legacy/LwjglVersionList.h index 652a3fda..f5312e2c 100644 --- a/api/logic/minecraft/legacy/LwjglVersionList.h +++ b/api/logic/minecraft/legacy/LwjglVersionList.h @@ -78,9 +78,9 @@ public: return m_vlist[i]; } - virtual Task* getLoadTask() override + virtual shared_qobject_ptr getLoadTask() override { - return nullptr; + return m_rssDLJob; } virtual void sortVersions() override {}; diff --git a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp index b19a2dea..3a9b10e6 100644 --- a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp +++ b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp @@ -56,7 +56,7 @@ void OneSixProfileStrategy::upgradeDeprecatedFiles() } auto file = ProfileUtils::parseJsonFile(QFileInfo(sourceFile), false); ProfileUtils::removeLwjglFromPatch(file); - file->fileId = "net.minecraft"; + file->uid = "net.minecraft"; file->version = file->minecraftVersion; file->name = "Minecraft"; auto data = OneSixVersionFormat::versionFileToJson(file, false).toJson(); @@ -81,6 +81,107 @@ void OneSixProfileStrategy::upgradeDeprecatedFiles() } } +class MetaPatchProvider : public ProfilePatch +{ +public: /* con/des */ + MetaPatchProvider(std::shared_ptr data) + :m_version(data) + { + } +public: + QString getFilename() override + { + return QString(); + } + QString getID() override + { + return m_version->uid(); + } + QString getName() override + { + auto vfile = getFile(); + if(vfile) + { + return vfile->getName(); + } + return m_version->name(); + } + QDateTime getReleaseDateTime() override + { + return m_version->time(); + } + QString getVersion() override + { + return m_version->version(); + } + std::shared_ptr getVersionFile() override + { + return m_version->data(); + } + void setOrder(int) override + { + } + 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; + } + bool isMinecraftVersion() override + { + return getID() == "net.minecraft"; + } + void applyTo(MinecraftProfile * profile) override + { + auto vfile = getFile(); + if(vfile) + { + vfile->applyTo(profile); + } + } +private: + VersionFilePtr getFile() + { + if(!m_version->isLoaded()) + { + m_version->load(); + } + return m_version->data(); + } +private: + std::shared_ptr m_version; +}; + void OneSixProfileStrategy::loadDefaultBuiltinPatches() { { @@ -101,7 +202,7 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() else { auto mcversion = ENV.metadataIndex()->get("net.minecraft", m_instance->intendedVersionId()); - minecraftPatch = std::dynamic_pointer_cast(mcversion); + minecraftPatch = std::make_shared(mcversion); } if (!minecraftPatch) { @@ -124,7 +225,7 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() else { auto lwjglversion = ENV.metadataIndex()->get("org.lwjgl", "2.9.1"); - lwjglPatch = std::dynamic_pointer_cast(lwjglversion); + lwjglPatch = std::make_shared(lwjglversion); } if (!lwjglPatch) { @@ -162,10 +263,10 @@ void OneSixProfileStrategy::loadUserPatches() qDebug() << "Reading" << filename << "by user order"; VersionFilePtr file = ProfileUtils::parseJsonFile(finfo, false); // sanity check. prevent tampering with files. - if (file->fileId != id) + if (file->uid != id) { - file->addProblem(PROBLEM_WARNING, QObject::tr("load id %1 does not match internal id %2").arg(id, file->fileId)); - seen_extra.insert(file->fileId); + file->addProblem(PROBLEM_WARNING, QObject::tr("load id %1 does not match internal id %2").arg(id, file->uid)); + seen_extra.insert(file->uid); } file->setRemovable(true); file->setMovable(true); @@ -185,15 +286,15 @@ void OneSixProfileStrategy::loadUserPatches() qDebug() << "Reading" << info.fileName(); auto file = ProfileUtils::parseJsonFile(info, true); // ignore builtins - if (file->fileId == "net.minecraft") + if (file->uid == "net.minecraft") continue; - if (file->fileId == "org.lwjgl") + if (file->uid == "org.lwjgl") continue; // do not load versions with broken IDs twice - if(seen_extra.contains(file->fileId)) + if(seen_extra.contains(file->uid)) continue; // do not load what we already loaded in the first pass - if (userOrder.contains(file->fileId)) + if (userOrder.contains(file->uid)) continue; file->setRemovable(true); file->setMovable(true); @@ -203,7 +304,7 @@ void OneSixProfileStrategy::loadUserPatches() file->assets = QString(); file->mojangAssetIndex.reset(); // HACK - files.insert(file->order, file); + files.insert(file->getOrder(), file); } QSet seen; for (auto order : files.keys()) @@ -284,7 +385,8 @@ bool OneSixProfileStrategy::removePatch(ProfilePatchPtr patch) return true; }; - for(auto &jarmod: patch->getJarMods()) + auto &jarMods = patch->getVersionFile()->jarMods; + for(auto &jarmod: jarMods) { ok &= preRemoveJarMod(jarmod); } @@ -404,8 +506,8 @@ bool OneSixProfileStrategy::installJarMods(QStringList filepaths) jarMod->originalName = sourceInfo.completeBaseName(); f->jarMods.append(jarMod); f->name = target_name; - f->fileId = target_id; - f->order = profile->getFreeOrderNumber(); + f->uid = target_id; + f->setOrder(profile->getFreeOrderNumber()); QString patchFileName = FS::PathCombine(patchDir, target_id + ".json"); f->filename = patchFileName; f->setMovable(true); diff --git a/api/logic/minecraft/onesix/OneSixVersionFormat.cpp b/api/logic/minecraft/onesix/OneSixVersionFormat.cpp index 403eb112..944e68a4 100644 --- a/api/logic/minecraft/onesix/OneSixVersionFormat.cpp +++ b/api/logic/minecraft/onesix/OneSixVersionFormat.cpp @@ -51,7 +51,7 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc { if (root.contains("order")) { - out->order = requireInteger(root.value("order")); + out->setOrder(requireInteger(root.value("order"))); } else { @@ -61,7 +61,16 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc } out->name = root.value("name").toString(); - out->fileId = root.value("fileId").toString(); + + if(root.contains("uid")) + { + out->uid = root.value("uid").toString(); + } + else + { + out->uid = root.value("fileId").toString(); + } + out->version = root.value("version").toString(); out->dependsOnMinecraftVersion = root.value("mcVersion").toString(); out->filename = filename; @@ -161,10 +170,13 @@ QJsonDocument OneSixVersionFormat::versionFileToJson(const VersionFilePtr &patch QJsonObject root; if (saveOrder) { - root.insert("order", patch->order); + root.insert("order", patch->getOrder()); } writeString(root, "name", patch->name); - writeString(root, "fileId", patch->fileId); + + writeString(root, "uid", patch->uid); + writeString(root, "fileId", patch->uid); + writeString(root, "version", patch->version); writeString(root, "mcVersion", patch->dependsOnMinecraftVersion); diff --git a/application/pages/VersionPage.cpp b/application/pages/VersionPage.cpp index 78e2a888..732a33eb 100644 --- a/application/pages/VersionPage.cpp +++ b/application/pages/VersionPage.cpp @@ -487,22 +487,16 @@ int VersionPage::currentRow() void VersionPage::on_customizeBtn_clicked() { - // TODO: implement - /* auto version = currentRow(); if(version == -1) { return; } - //HACK HACK remove, this is dumb auto patch = m_profile->versionPatch(version); - auto mc = std::dynamic_pointer_cast(patch); - if(mc && mc->needsUpdate()) + if(!patch->getVersionFile()) { - if(!doUpdate()) - { - return; - } + // TODO: wait for the update task to finish here... + return; } if(!m_profile->customize(version)) { @@ -510,7 +504,6 @@ void VersionPage::on_customizeBtn_clicked() } updateButtons(); preselect(currentIdx); - */ } void VersionPage::on_editBtn_clicked() @@ -531,22 +524,11 @@ void VersionPage::on_editBtn_clicked() void VersionPage::on_revertBtn_clicked() { - // TODO: implement - /* auto version = currentRow(); if(version == -1) { return; } - auto mcraw = MMC->minecraftlist()->findVersion(m_inst->intendedVersionId()); - auto mc = std::dynamic_pointer_cast(mcraw); - if(mc && mc->needsUpdate()) - { - if(!doUpdate()) - { - return; - } - } if(!m_profile->revertToBase(version)) { // TODO: some error box here @@ -554,7 +536,6 @@ void VersionPage::on_revertBtn_clicked() updateButtons(); preselect(currentIdx); m_container->refreshContainer(); - */ } #include "VersionPage.moc" diff --git a/application/widgets/VersionSelectWidget.cpp b/application/widgets/VersionSelectWidget.cpp index 18284a91..a4eb428b 100644 --- a/application/widgets/VersionSelectWidget.cpp +++ b/application/widgets/VersionSelectWidget.cpp @@ -80,52 +80,42 @@ void VersionSelectWidget::initialize() void VersionSelectWidget::closeEvent(QCloseEvent * event) { - if(loadTask) - { - loadTask->abort(); - loadTask->deleteLater(); - loadTask = nullptr; - } QWidget::closeEvent(event); } void VersionSelectWidget::loadList() { - if(loadTask) + auto newTask = m_vlist->getLoadTask(); + if (!newTask) { return; } - loadTask = m_vlist->getLoadTask(); - if (!loadTask) + loadTask = newTask.get(); + connect(loadTask, &Task::succeeded, this, &VersionSelectWidget::onTaskSucceeded); + connect(loadTask, &Task::failed, this, &VersionSelectWidget::onTaskFailed); + connect(loadTask, &Task::progress, this, &VersionSelectWidget::changeProgress); + if(!loadTask->isRunning()) { - return; + loadTask->start(); } - connect(loadTask, &Task::finished, this, &VersionSelectWidget::onTaskFinished); - connect(loadTask, &Task::progress, this, &VersionSelectWidget::changeProgress); - loadTask->start(); sneakyProgressBar->setHidden(false); } -void VersionSelectWidget::onTaskFinished() +void VersionSelectWidget::onTaskSucceeded() { - if (!loadTask->successful()) - { - CustomMessageBox::selectable(this, tr("Error"), - tr("List update failed:\n%1").arg(loadTask->failReason()), - QMessageBox::Warning)->show(); - if (m_proxyModel->rowCount() == 0) - { - listView->setEmptyMode(VersionListView::ErrorString); - } - } - else if (m_proxyModel->rowCount() == 0) + if (m_proxyModel->rowCount() == 0) { listView->setEmptyMode(VersionListView::String); } sneakyProgressBar->setHidden(true); - loadTask->deleteLater(); - loadTask = nullptr; preselect(); + loadTask = nullptr; +} + +void VersionSelectWidget::onTaskFailed(const QString& reason) +{ + CustomMessageBox::selectable(this, tr("Error"), tr("List update failed:\n%1").arg(reason), QMessageBox::Warning)->show(); + onTaskSucceeded(); } void VersionSelectWidget::changeProgress(qint64 current, qint64 total) diff --git a/application/widgets/VersionSelectWidget.h b/application/widgets/VersionSelectWidget.h index 0fc9f2e6..66e512ac 100644 --- a/application/widgets/VersionSelectWidget.h +++ b/application/widgets/VersionSelectWidget.h @@ -55,7 +55,8 @@ protected: virtual void closeEvent ( QCloseEvent* ); private slots: - void onTaskFinished(); + void onTaskSucceeded(); + void onTaskFailed(const QString &reason); void changeProgress(qint64 current, qint64 total); void currentRowChanged(const QModelIndex ¤t, const QModelIndex &); @@ -66,7 +67,7 @@ private: BaseVersionList *m_vlist = nullptr; VersionProxyModel *m_proxyModel = nullptr; int resizeOnColumn = 0; - Task * loadTask = nullptr; + Task * loadTask; bool preselectedAlready = false; private: -- cgit From da4ae1bc1ec74cdb4e75f4ebac30886ba4a1909c Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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/CMakeLists.txt | 2 -- 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 ++++++++++++++---- application/pages/VersionPage.cpp | 7 +++-- application/pages/global/PackagesPage.cpp | 18 ++++++------ 10 files changed, 118 insertions(+), 143 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/CMakeLists.txt b/api/logic/CMakeLists.txt index 544ace80..7f04a41e 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -426,8 +426,6 @@ set(META_SOURCES meta/Version.h meta/Index.cpp meta/Index.h - meta/Reference.cpp - meta/Reference.h ) add_unit_test(Index 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) diff --git a/application/pages/VersionPage.cpp b/application/pages/VersionPage.cpp index 732a33eb..0608b2bf 100644 --- a/application/pages/VersionPage.cpp +++ b/application/pages/VersionPage.cpp @@ -43,6 +43,9 @@ #include "MultiMC.h" +#include +#include + class IconProxy : public QIdentityProxyModel { Q_OBJECT @@ -372,7 +375,7 @@ int VersionPage::doUpdate() void VersionPage::on_forgeBtn_clicked() { - auto vlist = ENV.getVersionList("net.minecraftforge"); + auto vlist = ENV.metadataIndex()->get("net.minecraftforge"); if(!vlist) { return; @@ -391,7 +394,7 @@ void VersionPage::on_forgeBtn_clicked() void VersionPage::on_liteloaderBtn_clicked() { - auto vlist = ENV.getVersionList("com.liteloader"); + auto vlist = ENV.metadataIndex()->get("com.liteloader"); if(!vlist) { return; diff --git a/application/pages/global/PackagesPage.cpp b/application/pages/global/PackagesPage.cpp index e4967532..e15ddbab 100644 --- a/application/pages/global/PackagesPage.cpp +++ b/application/pages/global/PackagesPage.cpp @@ -34,17 +34,15 @@ using namespace Meta; static QString formatRequires(const VersionPtr &version) { QStringList lines; - for (const Reference &ref : version->requires()) + auto & reqs = version->requires(); + auto iter = reqs.begin(); + while (iter != reqs.end()) { - const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->get(ref.uid())->humanReadable() : ref.uid(); - if (ref.version().isEmpty()) - { - lines.append(readable); - } - else - { - lines.append(QString("%1 (%2)").arg(readable, ref.version())); - } + auto &uid = iter.key(); + auto &version = iter.value(); + const QString readable = ENV.metadataIndex()->hasUid(uid) ? ENV.metadataIndex()->get(uid)->humanReadable() : uid; + lines.append(QString("%1 (%2)").arg(readable, version)); + iter++; } return lines.join('\n'); } -- cgit From 6f2a87167a13101d80d2fbc096bbb6a5eb5ab0c9 Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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 ------ api/logic/minecraft/ProfilePatch.h | 10 ---------- api/logic/minecraft/VersionFile.h | 4 ---- api/logic/minecraft/onesix/OneSixProfileStrategy.cpp | 4 ---- 4 files changed, 24 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); diff --git a/api/logic/minecraft/ProfilePatch.h b/api/logic/minecraft/ProfilePatch.h index 6f63f3c4..699a38ae 100644 --- a/api/logic/minecraft/ProfilePatch.h +++ b/api/logic/minecraft/ProfilePatch.h @@ -15,14 +15,6 @@ enum ProblemSeverity PROBLEM_ERROR }; -/// where is a version from? -enum class VersionSource -{ - Builtin, //!< version loaded from the internal resources. - Local, //!< version loaded from a file in the cache. - Remote, //!< incomplete version on a remote server. -}; - class PatchProblem { public: @@ -70,8 +62,6 @@ public: virtual QString getFilename() = 0; - virtual VersionSource getVersionSource() = 0; - virtual std::shared_ptr getVersionFile() = 0; virtual const QList& getProblems() diff --git a/api/logic/minecraft/VersionFile.h b/api/logic/minecraft/VersionFile.h index 6a922d74..dad9a057 100644 --- a/api/logic/minecraft/VersionFile.h +++ b/api/logic/minecraft/VersionFile.h @@ -53,10 +53,6 @@ public: /* methods */ { return m_releaseTime; } - VersionSource getVersionSource() override - { - return VersionSource::Local; - } std::shared_ptr getVersionFile() override { diff --git a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp index 3a9b10e6..3f33dd7f 100644 --- a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp +++ b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp @@ -125,10 +125,6 @@ public: { return 0; } - VersionSource getVersionSource() override - { - return VersionSource::Local; - } bool isVersionChangeable() override { return true; -- cgit From 5fabb4f2546fa6b79a4e2c29679f506e587a0070 Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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/BaseVersionList.cpp | 2 +- api/logic/BaseVersionList.h | 2 +- api/logic/CMakeLists.txt | 1 + api/logic/ProblemProvider.h | 54 ++++++ api/logic/meta/Version.cpp | 2 +- api/logic/meta/VersionList.cpp | 4 +- api/logic/minecraft/MinecraftProfile.cpp | 6 +- api/logic/minecraft/MinecraftProfile.h | 4 +- api/logic/minecraft/MojangVersionFormat.cpp | 18 +- api/logic/minecraft/ProfilePatch.cpp | 214 +++++++++++++++++++++ api/logic/minecraft/ProfilePatch.h | 104 ++++------ api/logic/minecraft/ProfileStrategy.h | 1 + api/logic/minecraft/ProfileUtils.cpp | 4 +- api/logic/minecraft/VersionBuildError.h | 17 -- api/logic/minecraft/VersionFile.cpp | 35 ++-- api/logic/minecraft/VersionFile.h | 108 +---------- api/logic/minecraft/ftb/FTBProfileStrategy.cpp | 8 +- api/logic/minecraft/onesix/OneSixInstance.cpp | 2 +- .../minecraft/onesix/OneSixProfileStrategy.cpp | 178 +++++------------ api/logic/minecraft/onesix/OneSixVersionFormat.cpp | 19 +- application/VersionProxyModel.cpp | 6 +- application/pages/VersionPage.cpp | 16 +- 22 files changed, 431 insertions(+), 374 deletions(-) create mode 100644 api/logic/ProblemProvider.h create mode 100644 api/logic/minecraft/ProfilePatch.cpp (limited to 'api/logic/meta') diff --git a/api/logic/BaseVersionList.cpp b/api/logic/BaseVersionList.cpp index 5ad4df3c..dae604a2 100644 --- a/api/logic/BaseVersionList.cpp +++ b/api/logic/BaseVersionList.cpp @@ -93,7 +93,7 @@ QHash BaseVersionList::roleNames() const QHash roles = QAbstractListModel::roleNames(); roles.insert(VersionRole, "version"); roles.insert(VersionIdRole, "versionId"); - roles.insert(ParentGameVersionRole, "parentGameVersion"); + roles.insert(ParentVersionRole, "parentGameVersion"); roles.insert(RecommendedRole, "recommended"); roles.insert(LatestRole, "latest"); roles.insert(TypeRole, "type"); diff --git a/api/logic/BaseVersionList.h b/api/logic/BaseVersionList.h index 8afcae1d..eb3b622a 100644 --- a/api/logic/BaseVersionList.h +++ b/api/logic/BaseVersionList.h @@ -45,7 +45,7 @@ public: VersionPointerRole = Qt::UserRole, VersionRole, VersionIdRole, - ParentGameVersionRole, + ParentVersionRole, RecommendedRole, LatestRole, TypeRole, diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index 7f04a41e..da673c13 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -267,6 +267,7 @@ set(MINECRAFT_SOURCES minecraft/VersionBuildError.h minecraft/VersionFile.cpp minecraft/VersionFile.h + minecraft/ProfilePatch.cpp minecraft/ProfilePatch.h minecraft/VersionFilterData.h minecraft/VersionFilterData.cpp diff --git a/api/logic/ProblemProvider.h b/api/logic/ProblemProvider.h new file mode 100644 index 00000000..64a31c59 --- /dev/null +++ b/api/logic/ProblemProvider.h @@ -0,0 +1,54 @@ +#pragma once + +enum class ProblemSeverity +{ + None, + Warning, + Error +}; + +class PatchProblem +{ +public: + PatchProblem(ProblemSeverity severity, const QString & description) + { + m_severity = severity; + m_description = description; + } + const QString & getDescription() const + { + return m_description; + } + const ProblemSeverity getSeverity() const + { + return m_severity; + } +private: + ProblemSeverity m_severity; + QString m_description; +}; + +class ProblemProvider +{ +public: + virtual const QList& getProblems() + { + return m_problems; + } + virtual void addProblem(ProblemSeverity severity, const QString &description) + { + if(severity > m_problemSeverity) + { + m_problemSeverity = severity; + } + m_problems.append(PatchProblem(severity, description)); + } + virtual ProblemSeverity getProblemSeverity() + { + return m_problemSeverity; + } + +private: + QList m_problems; + ProblemSeverity m_problemSeverity = ProblemSeverity::None; +}; 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}; } diff --git a/api/logic/minecraft/MinecraftProfile.cpp b/api/logic/minecraft/MinecraftProfile.cpp index b74141d6..8638f5fa 100644 --- a/api/logic/minecraft/MinecraftProfile.cpp +++ b/api/logic/minecraft/MinecraftProfile.cpp @@ -80,7 +80,7 @@ void MinecraftProfile::clear() m_traits.clear(); m_jarMods.clear(); mojangDownloads.clear(); - m_problemSeverity = ProblemSeverity::PROBLEM_NONE; + m_problemSeverity = ProblemSeverity::None; } void MinecraftProfile::clearPatches() @@ -273,9 +273,9 @@ QVariant MinecraftProfile::data(const QModelIndex &index, int role) const auto severity = patch->getProblemSeverity(); switch (severity) { - case PROBLEM_WARNING: + case ProblemSeverity::Warning: return "warning"; - case PROBLEM_ERROR: + case ProblemSeverity::Error: return "error"; default: return QVariant(); diff --git a/api/logic/minecraft/MinecraftProfile.h b/api/logic/minecraft/MinecraftProfile.h index a6845e9c..a3b8fb61 100644 --- a/api/logic/minecraft/MinecraftProfile.h +++ b/api/logic/minecraft/MinecraftProfile.h @@ -22,7 +22,7 @@ #include #include "Library.h" -#include "VersionFile.h" +#include "ProfilePatch.h" #include "JarMod.h" #include "BaseVersion.h" #include "MojangDownloadInfo.h" @@ -175,7 +175,7 @@ private: /* data */ /// A list of jar mods. version files can add those. QList m_jarMods; - ProblemSeverity m_problemSeverity = PROBLEM_NONE; + ProblemSeverity m_problemSeverity = ProblemSeverity::None; /* FIXME: add support for those rules here? Looks like a pile of quick hacks to me though. diff --git a/api/logic/minecraft/MojangVersionFormat.cpp b/api/logic/minecraft/MojangVersionFormat.cpp index 651e2fbc..ea8dcd4d 100644 --- a/api/logic/minecraft/MojangVersionFormat.cpp +++ b/api/logic/minecraft/MojangVersionFormat.cpp @@ -151,7 +151,7 @@ void MojangVersionFormat::readVersionProperties(const QJsonObject &in, VersionFi } else if (!toCompare.isEmpty()) { - out->addProblem(PROBLEM_ERROR, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments)); + out->addProblem(ProblemSeverity::Error, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments)); } } Bits::readString(in, "type", out->type); @@ -166,8 +166,8 @@ void MojangVersionFormat::readVersionProperties(const QJsonObject &in, VersionFi out->mojangAssetIndex = std::make_shared(out->assets); } - out->m_releaseTime = timeFromS3Time(in.value("releaseTime").toString("")); - out->m_updateTime = timeFromS3Time(in.value("time").toString("")); + out->releaseTime = timeFromS3Time(in.value("releaseTime").toString("")); + out->updateTime = timeFromS3Time(in.value("time").toString("")); if (in.contains("minimumLauncherVersion")) { @@ -175,7 +175,7 @@ void MojangVersionFormat::readVersionProperties(const QJsonObject &in, VersionFi if (out->minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) { out->addProblem( - PROBLEM_WARNING, + ProblemSeverity::Warning, QObject::tr("The 'minimumLauncherVersion' value of this version (%1) is higher than supported by MultiMC (%2). It might not work properly!") .arg(out->minimumLauncherVersion) .arg(CURRENT_MINIMUM_LAUNCHER_VERSION)); @@ -212,7 +212,7 @@ VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc out->name = "Minecraft"; out->uid = "net.minecraft"; out->version = out->minecraftVersion; - out->filename = filename; + // out->filename = filename; if (root.contains("libraries")) @@ -234,13 +234,13 @@ void MojangVersionFormat::writeVersionProperties(const VersionFile* in, QJsonObj writeString(out, "mainClass", in->mainClass); writeString(out, "minecraftArguments", in->minecraftArguments); writeString(out, "type", in->type); - if(!in->m_releaseTime.isNull()) + if(!in->releaseTime.isNull()) { - writeString(out, "releaseTime", timeToS3Time(in->m_releaseTime)); + writeString(out, "releaseTime", timeToS3Time(in->releaseTime)); } - if(!in->m_updateTime.isNull()) + if(!in->updateTime.isNull()) { - writeString(out, "time", timeToS3Time(in->m_updateTime)); + writeString(out, "time", timeToS3Time(in->updateTime)); } if(in->minimumLauncherVersion != -1) { diff --git a/api/logic/minecraft/ProfilePatch.cpp b/api/logic/minecraft/ProfilePatch.cpp new file mode 100644 index 00000000..0ff5895d --- /dev/null +++ b/api/logic/minecraft/ProfilePatch.cpp @@ -0,0 +1,214 @@ +#include "ProfilePatch.h" + +#include "meta/Version.h" +#include "VersionFile.h" + +ProfilePatch::ProfilePatch(std::shared_ptr version) + :m_metaVersion(version) +{ +} + +ProfilePatch::ProfilePatch(std::shared_ptr file, const QString& filename) + :m_file(file), m_filename(filename) +{ +} + +void ProfilePatch::applyTo(MinecraftProfile* profile) +{ + auto vfile = getVersionFile(); + if(vfile) + { + vfile->applyTo(profile); + } +} + +std::shared_ptr ProfilePatch::getVersionFile() +{ + if(m_metaVersion) + { + if(!m_metaVersion->isLoaded()) + { + m_metaVersion->load(); + } + return m_metaVersion->data(); + } + return m_file; +} + +int ProfilePatch::getOrder() +{ + if(m_orderOverride) + return m_order; + + auto vfile = getVersionFile(); + if(vfile) + { + return vfile->order; + } + return 0; +} +void ProfilePatch::setOrder(int order) +{ + m_orderOverride = true; + m_order = order; +} +QString ProfilePatch::getID() +{ + if(m_metaVersion) + return m_metaVersion->uid(); + return getVersionFile()->uid; +} +QString ProfilePatch::getName() +{ + if(m_metaVersion) + return m_metaVersion->name(); + return getVersionFile()->name; +} +QString ProfilePatch::getVersion() +{ + if(m_metaVersion) + return m_metaVersion->version(); + return getVersionFile()->version; +} +QString ProfilePatch::getFilename() +{ + return m_filename; +} +QDateTime ProfilePatch::getReleaseDateTime() +{ + if(m_metaVersion) + { + return m_metaVersion->time(); + } + return getVersionFile()->releaseTime; +} + +bool ProfilePatch::isCustom() +{ + return !m_isVanilla; +}; + +bool ProfilePatch::isCustomizable() +{ + if(m_metaVersion) + { + if(getVersionFile()) + { + return true; + } + } + return false; +} +bool ProfilePatch::isRemovable() +{ + return m_isRemovable; +} +bool ProfilePatch::isRevertible() +{ + return m_isRevertible; +} +bool ProfilePatch::isMoveable() +{ + return m_isMovable; +} +bool ProfilePatch::isVersionChangeable() +{ + return false; +} + +void ProfilePatch::setVanilla (bool state) +{ + m_isVanilla = state; +} +void ProfilePatch::setRemovable (bool state) +{ + m_isRemovable = state; +} +void ProfilePatch::setRevertible (bool state) +{ + m_isRevertible = state; +} +void ProfilePatch::setMovable (bool state) +{ + m_isMovable = state; +} + +/* +class MetaPatchProvider : public ProfilePatch +{ +public: + MetaPatchProvider(std::shared_ptr data) + :m_version(data) + { + } +public: + QString getFilename() override + { + return QString(); + } + QString getID() override + { + return m_version->uid(); + } + QString getName() override + { + auto vfile = getFile(); + if(vfile) + { + return vfile->name; + } + return m_version->name(); + } + QDateTime getReleaseDateTime() override + { + return m_version->time(); + } + QString getVersion() override + { + return m_version->version(); + } + std::shared_ptr getVersionFile() override + { + return m_version->data(); + } + void setOrder(int) override + { + } + int getOrder() override + { + return 0; + } + 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; + } + +private: + VersionFilePtr getFile() + { + + } +private: + std::shared_ptr m_version; +}; +*/ \ No newline at end of file diff --git a/api/logic/minecraft/ProfilePatch.h b/api/logic/minecraft/ProfilePatch.h index 699a38ae..9e2555af 100644 --- a/api/logic/minecraft/ProfilePatch.h +++ b/api/logic/minecraft/ProfilePatch.h @@ -5,85 +5,63 @@ #include #include #include "JarMod.h" +#include "ProblemProvider.h" class MinecraftProfile; - -enum ProblemSeverity +namespace Meta { - PROBLEM_NONE, - PROBLEM_WARNING, - PROBLEM_ERROR -}; + class Version; +} +class VersionFile; -class PatchProblem +class ProfilePatch : public ProblemProvider { public: - PatchProblem(ProblemSeverity severity, const QString & description) - { - m_severity = severity; - m_description = description; - } - const QString & getDescription() const - { - return m_description; - } - const ProblemSeverity getSeverity() const - { - return m_severity; - } -private: - ProblemSeverity m_severity; - QString m_description; -}; + ProfilePatch(std::shared_ptr version); + ProfilePatch(std::shared_ptr file, const QString &filename = QString()); -class ProfilePatch : public std::enable_shared_from_this -{ -public: virtual ~ProfilePatch(){}; - virtual void applyTo(MinecraftProfile *profile) = 0; - - virtual bool isMinecraftVersion() = 0; + virtual void applyTo(MinecraftProfile *profile); - virtual bool isMoveable() = 0; - virtual bool isCustomizable() = 0; - virtual bool isRevertible() = 0; - virtual bool isRemovable() = 0; - virtual bool isCustom() = 0; - virtual bool isEditable() = 0; - virtual bool isVersionChangeable() = 0; + virtual bool isMoveable(); + virtual bool isCustomizable(); + virtual bool isRevertible(); + virtual bool isRemovable(); + virtual bool isCustom(); + virtual bool isVersionChangeable(); - virtual void setOrder(int order) = 0; - virtual int getOrder() = 0; + virtual void setOrder(int order); + virtual int getOrder(); - virtual QString getID() = 0; - virtual QString getName() = 0; - virtual QString getVersion() = 0; - virtual QDateTime getReleaseDateTime() = 0; + virtual QString getID(); + virtual QString getName(); + virtual QString getVersion(); + virtual QDateTime getReleaseDateTime(); - virtual QString getFilename() = 0; + virtual QString getFilename(); - virtual std::shared_ptr getVersionFile() = 0; + virtual std::shared_ptr getVersionFile(); - virtual const QList& getProblems() - { - return m_problems; - } - virtual void addProblem(ProblemSeverity severity, const QString &description) - { - if(severity > m_problemSeverity) - { - m_problemSeverity = severity; - } - m_problems.append(PatchProblem(severity, description)); - } - virtual ProblemSeverity getProblemSeverity() - { - return m_problemSeverity; - } + void setVanilla (bool state); + void setRemovable (bool state); + void setRevertible (bool state); + void setCustomizable (bool state); + void setMovable (bool state); protected: - QList m_problems; - ProblemSeverity m_problemSeverity = PROBLEM_NONE; + // Properties for UI and version manipulation from UI in general + bool m_isMovable = false; + bool m_isCustomizable = false; + bool m_isRevertible = false; + bool m_isRemovable = false; + bool m_isVanilla = false; + + bool m_orderOverride = false; + int m_order = 0; + + std::shared_ptr m_metaVersion; + std::shared_ptr m_file; + QString m_filename; }; typedef std::shared_ptr ProfilePatchPtr; diff --git a/api/logic/minecraft/ProfileStrategy.h b/api/logic/minecraft/ProfileStrategy.h index b4dfc4b3..26bdf661 100644 --- a/api/logic/minecraft/ProfileStrategy.h +++ b/api/logic/minecraft/ProfileStrategy.h @@ -1,6 +1,7 @@ #pragma once #include "ProfileUtils.h" +#include "ProfilePatch.h" class MinecraftProfile; diff --git a/api/logic/minecraft/ProfileUtils.cpp b/api/logic/minecraft/ProfileUtils.cpp index c7c56dd6..8c5bc052 100644 --- a/api/logic/minecraft/ProfileUtils.cpp +++ b/api/logic/minecraft/ProfileUtils.cpp @@ -103,8 +103,8 @@ static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, Q { auto outError = std::make_shared(); outError->uid = outError->name = fileId; - outError->filename = filepath; - outError->addProblem(PROBLEM_ERROR, error); + // outError->filename = filepath; + outError->addProblem(ProblemSeverity::Error, error); return outError; } diff --git a/api/logic/minecraft/VersionBuildError.h b/api/logic/minecraft/VersionBuildError.h index fda453e5..f362c405 100644 --- a/api/logic/minecraft/VersionBuildError.h +++ b/api/logic/minecraft/VersionBuildError.h @@ -26,23 +26,6 @@ public: } }; -/** - * some patch was intended for a different version of minecraft - */ -class MinecraftVersionMismatch : public VersionBuildError -{ -public: - MinecraftVersionMismatch(QString fileId, QString mcVersion, QString parentMcVersion) - : VersionBuildError(QObject::tr("The patch %1 is for a different version of Minecraft " - "(%2) than that of the instance (%3).") - .arg(fileId) - .arg(mcVersion) - .arg(parentMcVersion)) {}; - virtual ~MinecraftVersionMismatch() noexcept - { - } -}; - /** * files required for the version are not (yet?) present */ diff --git a/api/logic/minecraft/VersionFile.cpp b/api/logic/minecraft/VersionFile.cpp index 17dc6a49..b261ff2a 100644 --- a/api/logic/minecraft/VersionFile.cpp +++ b/api/logic/minecraft/VersionFile.cpp @@ -12,32 +12,28 @@ #include "VersionBuildError.h" #include -bool VersionFile::isMinecraftVersion() +static bool isMinecraftVersion(const QString &uid) { return uid == "net.minecraft"; } void VersionFile::applyTo(MinecraftProfile *profile) { - auto theirVersion = profile->getMinecraftVersion(); - if (!theirVersion.isNull() && !dependsOnMinecraftVersion.isNull()) + // Only real Minecraft can set those. Don't let anything override them. + if (isMinecraftVersion(uid)) { - if (QRegExp(dependsOnMinecraftVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(theirVersion) == -1) - { - throw MinecraftVersionMismatch(uid, dependsOnMinecraftVersion, theirVersion); - } + profile->applyMinecraftVersion(minecraftVersion); + profile->applyMinecraftVersionType(type); + // HACK: ignore assets from other version files than Minecraft + // workaround for stupid assets issue caused by amazon: + // https://www.theregister.co.uk/2017/02/28/aws_is_awol_as_s3_goes_haywire/ + profile->applyMinecraftAssets(mojangAssetIndex); } - profile->applyMinecraftVersion(minecraftVersion); + profile->applyMainClass(mainClass); profile->applyAppletClass(appletClass); profile->applyMinecraftArguments(minecraftArguments); - if (isMinecraftVersion()) - { - profile->applyMinecraftVersionType(type); - } - profile->applyMinecraftAssets(mojangAssetIndex); profile->applyTweakers(addTweakers); - profile->applyJarMods(jarMods); profile->applyTraits(traits); @@ -53,3 +49,14 @@ void VersionFile::applyTo(MinecraftProfile *profile) iter++; } } + +/* + auto theirVersion = profile->getMinecraftVersion(); + if (!theirVersion.isNull() && !dependsOnMinecraftVersion.isNull()) + { + if (QRegExp(dependsOnMinecraftVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(theirVersion) == -1) + { + throw MinecraftVersionMismatch(uid, dependsOnMinecraftVersion, theirVersion); + } + } +*/ \ No newline at end of file diff --git a/api/logic/minecraft/VersionFile.h b/api/logic/minecraft/VersionFile.h index dad9a057..a71c6228 100644 --- a/api/logic/minecraft/VersionFile.h +++ b/api/logic/minecraft/VersionFile.h @@ -8,7 +8,7 @@ #include #include "minecraft/OpSys.h" #include "minecraft/Rule.h" -#include "ProfilePatch.h" +#include "ProblemProvider.h" #include "Library.h" #include "JarMod.h" @@ -18,111 +18,19 @@ struct MojangDownloadInfo; struct MojangAssetIndexInfo; typedef std::shared_ptr VersionFilePtr; -class VersionFile : public ProfilePatch +class VersionFile : public ProblemProvider { friend class MojangVersionFormat; friend class OneSixVersionFormat; public: /* methods */ - virtual void applyTo(MinecraftProfile *profile) override; - virtual bool isMinecraftVersion() override; - virtual int getOrder() override - { - return order; - } - virtual void setOrder(int order) override - { - this->order = order; - } - virtual QString getID() override - { - return uid; - } - virtual QString getName() override - { - return name; - } - virtual QString getVersion() override - { - return version; - } - virtual QString getFilename() override - { - return filename; - } - virtual QDateTime getReleaseDateTime() override - { - return m_releaseTime; - } - - std::shared_ptr getVersionFile() override - { - return std::dynamic_pointer_cast(shared_from_this()); - } - - virtual bool isCustom() override - { - return !m_isVanilla; - }; - virtual bool isCustomizable() override - { - return m_isCustomizable; - } - virtual bool isRemovable() override - { - return m_isRemovable; - } - virtual bool isRevertible() override - { - return m_isRevertible; - } - virtual bool isMoveable() override - { - return m_isMovable; - } - virtual bool isEditable() override - { - return isCustom(); - } - virtual bool isVersionChangeable() override - { - return false; - } - - void setVanilla (bool state) - { - m_isVanilla = state; - } - void setRemovable (bool state) - { - m_isRemovable = state; - } - void setRevertible (bool state) - { - m_isRevertible = state; - } - void setCustomizable (bool state) - { - m_isCustomizable = state; - } - void setMovable (bool state) - { - m_isMovable = state; - } - + void applyTo(MinecraftProfile *profile); public: /* data */ - // Flags for UI and version file manipulation in general - bool m_isVanilla = false; - bool m_isRemovable = false; - bool m_isRevertible = false; - bool m_isCustomizable = false; - bool m_isMovable = false; - /// MultiMC: order hint for this version file if no explicit order is set int order = 0; /// MultiMC: filename of the file this was loaded from - QString filename; + // QString filename; /// MultiMC: human readable name of this package QString name; @@ -139,13 +47,13 @@ public: /* data */ /// Mojang: used to version the Mojang version format int minimumLauncherVersion = -1; - /// Mojang: version of Minecraft this is + /// Mojang: DEPRECATED version of Minecraft this is QString minecraftVersion; /// Mojang: class to launch Minecraft with QString mainClass; - /// MultiMC: DEPRECATED class to launch legacy Minecraft with (ambed in a custom window) + /// MultiMC: class to launch legacy Minecraft with (embed in a custom window) QString appletClass; /// Mojang: Minecraft launch arguments (may contain placeholders for variable substitution) @@ -155,10 +63,10 @@ public: /* data */ QString type; /// Mojang: the time this version was actually released by Mojang - QDateTime m_releaseTime; + QDateTime releaseTime; /// Mojang: the time this version was last updated by Mojang - QDateTime m_updateTime; + QDateTime updateTime; /// Mojang: DEPRECATED asset group to be used with Minecraft QString assets; diff --git a/api/logic/minecraft/ftb/FTBProfileStrategy.cpp b/api/logic/minecraft/ftb/FTBProfileStrategy.cpp index 45765cd4..c3d9cc6a 100644 --- a/api/logic/minecraft/ftb/FTBProfileStrategy.cpp +++ b/api/logic/minecraft/ftb/FTBProfileStrategy.cpp @@ -29,7 +29,6 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() auto file = ProfileUtils::parseJsonFile(QFileInfo(mcJson), false); file->uid = "net.minecraft"; file->name = QObject::tr("Minecraft (tracked)"); - file->setVanilla(true); if(file->version.isEmpty()) { file->version = mcVersion; @@ -39,7 +38,8 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() addLib->setHint("local"); addLib->setStoragePrefix(nativeInstance->librariesPath().absolutePath()); } - minecraftPatch = std::dynamic_pointer_cast(file); + minecraftPatch = std::make_shared(file); + minecraftPatch->setVanilla(true); } else { @@ -65,7 +65,6 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() addLib->setStoragePrefix(nativeInstance->librariesPath().absolutePath()); } file->uid = "org.multimc.ftb.pack"; - file->setVanilla(true); file->name = QObject::tr("%1 (FTB pack)").arg(m_instance->name()); if(file->version.isEmpty()) { @@ -81,7 +80,8 @@ void FTBProfileStrategy::loadDefaultBuiltinPatches() } } } - packPatch = std::dynamic_pointer_cast(file); + packPatch = std::make_shared(file); + packPatch->setVanilla(true); } else { diff --git a/api/logic/minecraft/onesix/OneSixInstance.cpp b/api/logic/minecraft/onesix/OneSixInstance.cpp index b471ff3e..6e4a5bdc 100644 --- a/api/logic/minecraft/onesix/OneSixInstance.cpp +++ b/api/logic/minecraft/onesix/OneSixInstance.cpp @@ -523,7 +523,7 @@ QString OneSixInstance::currentVersionId() const void OneSixInstance::reloadProfile() { m_profile->reload(); - setVersionBroken(m_profile->getProblemSeverity() == ProblemSeverity::PROBLEM_ERROR); + setVersionBroken(m_profile->getProblemSeverity() == ProblemSeverity::Error); emit versionReloaded(); } diff --git a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp index 3f33dd7f..a9cad832 100644 --- a/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp +++ b/api/logic/minecraft/onesix/OneSixProfileStrategy.cpp @@ -15,6 +15,8 @@ #include #include +#include + OneSixProfileStrategy::OneSixProfileStrategy(OneSixInstance* instance) { m_instance = instance; @@ -81,103 +83,6 @@ void OneSixProfileStrategy::upgradeDeprecatedFiles() } } -class MetaPatchProvider : public ProfilePatch -{ -public: /* con/des */ - MetaPatchProvider(std::shared_ptr data) - :m_version(data) - { - } -public: - QString getFilename() override - { - return QString(); - } - QString getID() override - { - return m_version->uid(); - } - QString getName() override - { - auto vfile = getFile(); - if(vfile) - { - return vfile->getName(); - } - return m_version->name(); - } - QDateTime getReleaseDateTime() override - { - return m_version->time(); - } - QString getVersion() override - { - return m_version->version(); - } - std::shared_ptr getVersionFile() override - { - return m_version->data(); - } - void setOrder(int) override - { - } - int getOrder() override - { - return 0; - } - 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; - } - bool isMinecraftVersion() override - { - return getID() == "net.minecraft"; - } - void applyTo(MinecraftProfile * profile) override - { - auto vfile = getFile(); - if(vfile) - { - vfile->applyTo(profile); - } - } -private: - VersionFilePtr getFile() - { - if(!m_version->isLoaded()) - { - m_version->load(); - } - return m_version->data(); - } -private: - std::shared_ptr m_version; -}; - void OneSixProfileStrategy::loadDefaultBuiltinPatches() { { @@ -191,14 +96,15 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() { file->version = m_instance->intendedVersionId(); } - file->setVanilla(false); - file->setRevertible(true); - minecraftPatch = std::dynamic_pointer_cast(file); + minecraftPatch = std::make_shared(file, mcJson); + minecraftPatch->setVanilla(false); + minecraftPatch->setRevertible(true); } else { auto mcversion = ENV.metadataIndex()->get("net.minecraft", m_instance->intendedVersionId()); - minecraftPatch = std::make_shared(mcversion); + minecraftPatch = std::make_shared(mcversion); + minecraftPatch->setVanilla(true); } if (!minecraftPatch) { @@ -214,14 +120,15 @@ void OneSixProfileStrategy::loadDefaultBuiltinPatches() if(QFile::exists(lwjglJson)) { auto file = ProfileUtils::parseJsonFile(QFileInfo(lwjglJson), false); - file->setVanilla(false); - file->setRevertible(true); - lwjglPatch = std::dynamic_pointer_cast(file); + lwjglPatch = std::make_shared(file, lwjglJson); + lwjglPatch->setVanilla(false); + lwjglPatch->setRevertible(true); } else { auto lwjglversion = ENV.metadataIndex()->get("org.lwjgl", "2.9.1"); - lwjglPatch = std::make_shared(lwjglversion); + lwjglPatch = std::make_shared(lwjglversion); + lwjglPatch->setVanilla(true); } if (!lwjglPatch) { @@ -261,21 +168,17 @@ void OneSixProfileStrategy::loadUserPatches() // sanity check. prevent tampering with files. if (file->uid != id) { - file->addProblem(PROBLEM_WARNING, QObject::tr("load id %1 does not match internal id %2").arg(id, file->uid)); + file->addProblem(ProblemSeverity::Warning, QObject::tr("load id %1 does not match internal id %2").arg(id, file->uid)); seen_extra.insert(file->uid); } - file->setRemovable(true); - file->setMovable(true); - // HACK: ignore assets from other version files than Minecraft - // workaround for stupid assets issue caused by amazon: - // https://www.theregister.co.uk/2017/02/28/aws_is_awol_as_s3_goes_haywire/ - file->assets = QString(); - file->mojangAssetIndex.reset(); - // HACK - profile->appendPatch(file); + auto patchEntry = std::make_shared(file, filename); + patchEntry->setRemovable(true); + patchEntry->setMovable(true); + profile->appendPatch(patchEntry); } // now load the rest by internal preference. - QMultiMap files; + using FileEntry = std::tuple; + QMultiMap files; for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) { // parse the file @@ -292,16 +195,18 @@ void OneSixProfileStrategy::loadUserPatches() // do not load what we already loaded in the first pass if (userOrder.contains(file->uid)) continue; - file->setRemovable(true); - file->setMovable(true); - // HACK: ignore assets from other version files than Minecraft - // workaround for stupid assets issue caused by amazon: - // https://www.theregister.co.uk/2017/02/28/aws_is_awol_as_s3_goes_haywire/ - file->assets = QString(); - file->mojangAssetIndex.reset(); - // HACK - files.insert(file->getOrder(), file); + files.insert(file->order, std::make_tuple(file, info.filePath())); } + auto appendFilePatch = [&](FileEntry tuple) + { + VersionFilePtr file; + QString filename; + std::tie(file, filename) = tuple; + auto patchEntry = std::make_shared(file, filename); + patchEntry->setRemovable(true); + patchEntry->setMovable(true); + profile->appendPatch(patchEntry); + }; QSet seen; for (auto order : files.keys()) { @@ -311,7 +216,7 @@ void OneSixProfileStrategy::loadUserPatches() const auto &values = files.values(order); if(values.size() == 1) { - profile->appendPatch(values[0]); + appendFilePatch(values[0]); continue; } for(auto &file: values) @@ -320,10 +225,13 @@ void OneSixProfileStrategy::loadUserPatches() for(auto &file2: values) { if(file != file2) - list.append(file2->name); + { + list.append(std::get<0>(file2)->name); + } } - file->addProblem(PROBLEM_WARNING, QObject::tr("%1 has the same order as the following components:\n%2").arg(file->name, list.join(", "))); - profile->appendPatch(file); + auto vfileptr = std::get<0>(file); + vfileptr->addProblem(ProblemSeverity::Warning, QObject::tr("%1 has the same order as the following components:\n%2").arg(vfileptr->name, list.join(", "))); + appendFilePatch(file); } } } @@ -503,11 +411,9 @@ bool OneSixProfileStrategy::installJarMods(QStringList filepaths) f->jarMods.append(jarMod); f->name = target_name; f->uid = target_id; - f->setOrder(profile->getFreeOrderNumber()); + f->order = profile->getFreeOrderNumber(); QString patchFileName = FS::PathCombine(patchDir, target_id + ".json"); - f->filename = patchFileName; - f->setMovable(true); - f->setRemovable(true); + // f->filename = patchFileName; QFile file(patchFileName); if (!file.open(QFile::WriteOnly)) @@ -518,7 +424,11 @@ bool OneSixProfileStrategy::installJarMods(QStringList filepaths) } file.write(OneSixVersionFormat::versionFileToJson(f, true).toJson()); file.close(); - profile->appendPatch(f); + + auto patch = std::make_shared(f); + patch->setMovable(true); + patch->setRemovable(true); + profile->appendPatch(patch); } profile->saveCurrentOrder(); profile->reapplyPatches(); diff --git a/api/logic/minecraft/onesix/OneSixVersionFormat.cpp b/api/logic/minecraft/onesix/OneSixVersionFormat.cpp index 944e68a4..266bd4bd 100644 --- a/api/logic/minecraft/onesix/OneSixVersionFormat.cpp +++ b/api/logic/minecraft/onesix/OneSixVersionFormat.cpp @@ -51,7 +51,7 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc { if (root.contains("order")) { - out->setOrder(requireInteger(root.value("order"))); + out->order = requireInteger(root.value("order")); } else { @@ -73,7 +73,7 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc out->version = root.value("version").toString(); out->dependsOnMinecraftVersion = root.value("mcVersion").toString(); - out->filename = filename; + // out->filename = filename; MojangVersionFormat::readVersionProperties(root, out.get()); @@ -128,7 +128,8 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc bool hasLibs = root.contains("libraries"); if (hasPlusLibs && hasLibs) { - out->addProblem(PROBLEM_WARNING, QObject::tr("Version file has both '+libraries' and 'libraries'. This is no longer supported.")); + out->addProblem(ProblemSeverity::Warning, + QObject::tr("Version file has both '+libraries' and 'libraries'. This is no longer supported.")); readLibs("libraries"); readLibs("+libraries"); } @@ -144,23 +145,23 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc /* removed features that shouldn't be used */ if (root.contains("tweakers")) { - out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element 'tweakers'")); + out->addProblem(ProblemSeverity::Error, QObject::tr("Version file contains unsupported element 'tweakers'")); } if (root.contains("-libraries")) { - out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-libraries'")); + out->addProblem(ProblemSeverity::Error, QObject::tr("Version file contains unsupported element '-libraries'")); } if (root.contains("-tweakers")) { - out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-tweakers'")); + out->addProblem(ProblemSeverity::Error, QObject::tr("Version file contains unsupported element '-tweakers'")); } if (root.contains("-minecraftArguments")) { - out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-minecraftArguments'")); + out->addProblem(ProblemSeverity::Error, QObject::tr("Version file contains unsupported element '-minecraftArguments'")); } if (root.contains("+minecraftArguments")) { - out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '+minecraftArguments'")); + out->addProblem(ProblemSeverity::Error, QObject::tr("Version file contains unsupported element '+minecraftArguments'")); } return out; } @@ -170,7 +171,7 @@ QJsonDocument OneSixVersionFormat::versionFileToJson(const VersionFilePtr &patch QJsonObject root; if (saveOrder) { - root.insert("order", patch->getOrder()); + root.insert("order", patch->order); } writeString(root, "name", patch->name); diff --git a/application/VersionProxyModel.cpp b/application/VersionProxyModel.cpp index 50b94d9e..00390b36 100644 --- a/application/VersionProxyModel.cpp +++ b/application/VersionProxyModel.cpp @@ -26,7 +26,7 @@ public: switch(role) { - case BaseVersionList::ParentGameVersionRole: + case BaseVersionList::ParentVersionRole: case BaseVersionList::VersionIdRole: { auto versionString = data.toString(); @@ -146,7 +146,7 @@ QVariant VersionProxyModel::data(const QModelIndex &index, int role) const case Name: return sourceModel()->data(parentIndex, BaseVersionList::VersionRole); case ParentVersion: - return sourceModel()->data(parentIndex, BaseVersionList::ParentGameVersionRole); + return sourceModel()->data(parentIndex, BaseVersionList::ParentVersionRole); case Branch: return sourceModel()->data(parentIndex, BaseVersionList::BranchRole); case Type: @@ -327,7 +327,7 @@ void VersionProxyModel::setSourceModel(QAbstractItemModel *replacingRaw) m_columns.push_back(Name); } /* - if(roles.contains(BaseVersionList::ParentGameVersionRole)) + if(roles.contains(BaseVersionList::ParentVersionRole)) { m_columns.push_back(ParentVersion); } diff --git a/application/pages/VersionPage.cpp b/application/pages/VersionPage.cpp index 0608b2bf..5a0b2243 100644 --- a/application/pages/VersionPage.cpp +++ b/application/pages/VersionPage.cpp @@ -152,14 +152,14 @@ void VersionPage::packageCurrent(const QModelIndex ¤t, const QModelIndex & auto severity = patch->getProblemSeverity(); switch(severity) { - case PROBLEM_WARNING: + case ProblemSeverity::Warning: ui->frame->setModText(tr("%1 possibly has issues.").arg(patch->getName())); break; - case PROBLEM_ERROR: + case ProblemSeverity::Error: ui->frame->setModText(tr("%1 has issues!").arg(patch->getName())); break; default: - case PROBLEM_NONE: + case ProblemSeverity::None: ui->frame->clear(); return; } @@ -168,11 +168,11 @@ void VersionPage::packageCurrent(const QModelIndex ¤t, const QModelIndex & QString problemOut; for (auto &problem: problems) { - if(problem.getSeverity() == PROBLEM_ERROR) + if(problem.getSeverity() == ProblemSeverity::Error) { problemOut += tr("Error: "); } - else if(problem.getSeverity() == PROBLEM_WARNING) + else if(problem.getSeverity() == ProblemSeverity::Warning) { problemOut += tr("Warning: "); } @@ -381,7 +381,7 @@ void VersionPage::on_forgeBtn_clicked() return; } VersionSelectDialog vselect(vlist.get(), tr("Select Forge version"), this); - vselect.setExactFilter(BaseVersionList::ParentGameVersionRole, m_inst->currentVersionId()); + vselect.setExactFilter(BaseVersionList::ParentVersionRole, m_inst->currentVersionId()); vselect.setEmptyString(tr("No Forge versions are currently available for Minecraft ") + m_inst->currentVersionId()); vselect.setEmptyErrorString(tr("Couldn't load or download the Forge version lists!")); if (vselect.exec() && vselect.selectedVersion()) @@ -400,7 +400,7 @@ void VersionPage::on_liteloaderBtn_clicked() return; } VersionSelectDialog vselect(vlist.get(), tr("Select LiteLoader version"), this); - vselect.setExactFilter(BaseVersionList::ParentGameVersionRole, m_inst->currentVersionId()); + vselect.setExactFilter(BaseVersionList::ParentVersionRole, m_inst->currentVersionId()); vselect.setEmptyString(tr("No LiteLoader versions are currently available for Minecraft ") + m_inst->currentVersionId()); vselect.setEmptyErrorString(tr("Couldn't load or download the LiteLoader version lists!")); if (vselect.exec() && vselect.selectedVersion()) @@ -457,7 +457,7 @@ void VersionPage::updateButtons(int row) ui->moveDownBtn->setEnabled(patch->isMoveable()); ui->moveUpBtn->setEnabled(patch->isMoveable()); ui->changeVersionBtn->setEnabled(patch->isVersionChangeable()); - ui->editBtn->setEnabled(patch->isEditable()); + ui->editBtn->setEnabled(patch->isCustom()); ui->customizeBtn->setEnabled(patch->isCustomizable()); ui->revertBtn->setEnabled(patch->isRevertible()); } -- cgit From 643d74f66c2b22660264a393d5c3fa38f68a5d59 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Thu, 6 Apr 2017 23:30:18 +0200 Subject: NOISSUE implement recommended versions using the new JSON format --- api/logic/BaseVersionList.cpp | 7 +------ api/logic/BaseVersionList.h | 5 ----- 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 -- 7 files changed, 32 insertions(+), 26 deletions(-) (limited to 'api/logic/meta') diff --git a/api/logic/BaseVersionList.cpp b/api/logic/BaseVersionList.cpp index dae604a2..8b424c11 100644 --- a/api/logic/BaseVersionList.cpp +++ b/api/logic/BaseVersionList.cpp @@ -30,7 +30,7 @@ BaseVersionPtr BaseVersionList::findVersion(const QString &descriptor) return BaseVersionPtr(); } -BaseVersionPtr BaseVersionList::getLatestStable() const +BaseVersionPtr BaseVersionList::getRecommended() const { if (count() <= 0) return BaseVersionPtr(); @@ -38,11 +38,6 @@ BaseVersionPtr BaseVersionList::getLatestStable() const return at(0); } -BaseVersionPtr BaseVersionList::getRecommended() const -{ - return getLatestStable(); -} - QVariant BaseVersionList::data(const QModelIndex &index, int role) const { if (!index.isValid()) diff --git a/api/logic/BaseVersionList.h b/api/logic/BaseVersionList.h index eb3b622a..655bbd43 100644 --- a/api/logic/BaseVersionList.h +++ b/api/logic/BaseVersionList.h @@ -93,11 +93,6 @@ public: */ virtual BaseVersionPtr findVersion(const QString &descriptor); - /*! - * \brief Gets the latest stable version from this list - */ - virtual BaseVersionPtr getLatestStable() const; - /*! * \brief Gets the recommended version from this list * If the list doesn't support recommended versions, this works exactly as getLatestStable 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 From dddc5cedf380d1321b94b0bc7d244d23289e6d3c Mon Sep 17 00:00:00 2001 From: Petr Mrázek 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