NOISSUE Wonko is the new Meta
And then Wonko was the Meta.
This commit is contained in:
42
api/logic/meta/BaseEntity.cpp
Normal file
42
api/logic/meta/BaseEntity.cpp
Normal file
@ -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();
|
||||
}
|
||||
}
|
53
api/logic/meta/BaseEntity.h
Normal file
53
api/logic/meta/BaseEntity.h
Normal file
@ -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 <QObject>
|
||||
#include <memory>
|
||||
|
||||
#include "multimc_logic_export.h"
|
||||
|
||||
class Task;
|
||||
namespace Meta
|
||||
{
|
||||
class MULTIMC_LOGIC_EXPORT BaseEntity
|
||||
{
|
||||
public:
|
||||
virtual ~BaseEntity();
|
||||
|
||||
using Ptr = std::shared_ptr<BaseEntity>;
|
||||
|
||||
virtual std::unique_ptr<Task> remoteUpdateTask() = 0;
|
||||
virtual std::unique_ptr<Task> localUpdateTask() = 0;
|
||||
virtual void merge(const std::shared_ptr<BaseEntity> &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;
|
||||
};
|
||||
}
|
150
api/logic/meta/Index.cpp
Normal file
150
api/logic/meta/Index.cpp
Normal file
@ -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<VersionListPtr> &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<Task> Index::remoteUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<IndexRemoteLoadTask>(new IndexRemoteLoadTask(this, this));
|
||||
}
|
||||
std::unique_ptr<Task> Index::localUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<IndexLocalLoadTask>(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<VersionList>(uid));
|
||||
}
|
||||
|
||||
void Index::merge(const Ptr &other)
|
||||
{
|
||||
const QVector<VersionListPtr> lists = std::dynamic_pointer_cast<Index>(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<int>() << Qt::DisplayRole);
|
||||
});
|
||||
}
|
||||
}
|
72
api/logic/meta/Index.h
Normal file
72
api/logic/meta/Index.h
Normal file
@ -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 <QAbstractListModel>
|
||||
#include <memory>
|
||||
|
||||
#include "BaseEntity.h"
|
||||
|
||||
#include "multimc_logic_export.h"
|
||||
|
||||
class Task;
|
||||
|
||||
namespace Meta
|
||||
{
|
||||
using VersionListPtr = std::shared_ptr<class VersionList>;
|
||||
|
||||
class MULTIMC_LOGIC_EXPORT Index : public QAbstractListModel, public BaseEntity
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Index(QObject *parent = nullptr);
|
||||
explicit Index(const QVector<VersionListPtr> &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<Task> remoteUpdateTask() override;
|
||||
std::unique_ptr<Task> 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<VersionListPtr> lists() const { return m_lists; }
|
||||
|
||||
public: // for usage by parsers only
|
||||
void merge(const BaseEntity::Ptr &other) override;
|
||||
|
||||
private:
|
||||
QVector<VersionListPtr> m_lists;
|
||||
QHash<QString, VersionListPtr> m_uids;
|
||||
|
||||
void connectVersionList(const int row, const VersionListPtr &list);
|
||||
};
|
||||
}
|
50
api/logic/meta/Index_test.cpp
Normal file
50
api/logic/meta/Index_test.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <QTest>
|
||||
#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<Meta::VersionList>("list1"), std::make_shared<Meta::VersionList>("list2"), std::make_shared<Meta::VersionList>("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<Meta::VersionList>("list1"), std::make_shared<Meta::VersionList>("list2"), std::make_shared<Meta::VersionList>("list3")});
|
||||
QCOMPARE(windex.lists().size(), 3);
|
||||
windex.merge(std::shared_ptr<Meta::Index>(new Meta::Index({std::make_shared<Meta::VersionList>("list1"), std::make_shared<Meta::VersionList>("list2"), std::make_shared<Meta::VersionList>("list3")})));
|
||||
QCOMPARE(windex.lists().size(), 3);
|
||||
windex.merge(std::shared_ptr<Meta::Index>(new Meta::Index({std::make_shared<Meta::VersionList>("list4"), std::make_shared<Meta::VersionList>("list2"), std::make_shared<Meta::VersionList>("list5")})));
|
||||
QCOMPARE(windex.lists().size(), 5);
|
||||
windex.merge(std::shared_ptr<Meta::Index>(new Meta::Index({std::make_shared<Meta::VersionList>("list6")})));
|
||||
QCOMPARE(windex.lists().size(), 6);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_GUILESS_MAIN(IndexTest)
|
||||
|
||||
#include "Index_test.moc"
|
48
api/logic/meta/Reference.cpp
Normal file
48
api/logic/meta/Reference.cpp
Normal file
@ -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;
|
||||
}
|
||||
}
|
44
api/logic/meta/Reference.h
Normal file
44
api/logic/meta/Reference.h
Normal file
@ -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 <QString>
|
||||
#include <QMetaType>
|
||||
|
||||
#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)
|
50
api/logic/meta/Util.cpp
Normal file
50
api/logic/meta/Util.cpp
Normal file
@ -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 <QUrl>
|
||||
#include <QDir>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
||||
}
|
31
api/logic/meta/Util.h
Normal file
31
api/logic/meta/Util.h
Normal file
@ -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();
|
||||
}
|
105
api/logic/meta/Version.cpp
Normal file
105
api/logic/meta/Version.cpp
Normal file
@ -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 <QDateTime>
|
||||
|
||||
#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<Task> Version::remoteUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<VersionRemoteLoadTask>(new VersionRemoteLoadTask(this, this));
|
||||
}
|
||||
std::unique_ptr<Task> Version::localUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<VersionLocalLoadTask>(new VersionLocalLoadTask(this, this));
|
||||
}
|
||||
|
||||
void Version::merge(const std::shared_ptr<BaseEntity> &other)
|
||||
{
|
||||
VersionPtr version = std::dynamic_pointer_cast<Version>(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<Reference> &requires)
|
||||
{
|
||||
m_requires = requires;
|
||||
emit requiresChanged();
|
||||
}
|
||||
void Version::setData(const VersionFilePtr &data)
|
||||
{
|
||||
m_data = data;
|
||||
}
|
||||
}
|
87
api/logic/meta/Version.h
Normal file
87
api/logic/meta/Version.h
Normal file
@ -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 <QVector>
|
||||
#include <QStringList>
|
||||
#include <QJsonObject>
|
||||
#include <memory>
|
||||
|
||||
#include "minecraft/VersionFile.h"
|
||||
|
||||
#include "BaseEntity.h"
|
||||
#include "Reference.h"
|
||||
|
||||
#include "multimc_logic_export.h"
|
||||
|
||||
namespace Meta
|
||||
{
|
||||
using VersionPtr = std::shared_ptr<class Version>;
|
||||
|
||||
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<Reference> 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<Reference> requires() const { return m_requires; }
|
||||
VersionFilePtr data() const { return m_data; }
|
||||
|
||||
std::unique_ptr<Task> remoteUpdateTask() override;
|
||||
std::unique_ptr<Task> localUpdateTask() override;
|
||||
void merge(const std::shared_ptr<BaseEntity> &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<Reference> &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<Reference> m_requires;
|
||||
VersionFilePtr m_data;
|
||||
};
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(Meta::VersionPtr)
|
288
api/logic/meta/VersionList.cpp
Normal file
288
api/logic/meta/VersionList.cpp
Normal file
@ -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 <QDateTime>
|
||||
|
||||
#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<Task> 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<BaseVersion>(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<int, QByteArray> VersionList::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles = BaseVersionList::roleNames();
|
||||
roles.insert(UidRole, "uid");
|
||||
roles.insert(TimeRole, "time");
|
||||
roles.insert(SortRole, "sort");
|
||||
roles.insert(RequiresRole, "requires");
|
||||
return roles;
|
||||
}
|
||||
|
||||
std::unique_ptr<Task> VersionList::remoteUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<VersionListRemoteLoadTask>(new VersionListRemoteLoadTask(this, this));
|
||||
}
|
||||
std::unique_ptr<Task> VersionList::localUpdateTask()
|
||||
{
|
||||
return std::unique_ptr<VersionListLocalLoadTask>(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<VersionPtr> &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<VersionList>(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<int>() << 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<int>() << 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<int>() << RequiresRole); });
|
||||
connect(version.get(), &Version::timeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TimeRole << SortRole); });
|
||||
connect(version.get(), &Version::typeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TypeRole); });
|
||||
}
|
||||
|
||||
BaseVersionPtr VersionList::getLatestStable() const
|
||||
{
|
||||
return m_latest;
|
||||
}
|
||||
BaseVersionPtr VersionList::getRecommended() const
|
||||
{
|
||||
return m_recommended;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include "VersionList.moc"
|
95
api/logic/meta/VersionList.h
Normal file
95
api/logic/meta/VersionList.h
Normal file
@ -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 <memory>
|
||||
|
||||
namespace Meta
|
||||
{
|
||||
using VersionPtr = std::shared_ptr<class Version>;
|
||||
using VersionListPtr = std::shared_ptr<class VersionList>;
|
||||
|
||||
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<int, QByteArray> roleNames() const override;
|
||||
|
||||
std::unique_ptr<Task> remoteUpdateTask() override;
|
||||
std::unique_ptr<Task> 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<VersionPtr> versions() const { return m_versions; }
|
||||
|
||||
public: // for usage only by parsers
|
||||
void setName(const QString &name);
|
||||
void setVersions(const QVector<VersionPtr> &versions);
|
||||
void merge(const BaseEntity::Ptr &other) override;
|
||||
|
||||
signals:
|
||||
void nameChanged(const QString &name);
|
||||
|
||||
protected slots:
|
||||
void updateListData(QList<BaseVersionPtr>) override {}
|
||||
|
||||
private:
|
||||
QVector<VersionPtr> m_versions;
|
||||
QHash<QString, VersionPtr> 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)
|
84
api/logic/meta/format/Format.cpp
Normal file
84
api/logic/meta/format/Format.cpp
Normal file
@ -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);
|
||||
}
|
||||
}
|
57
api/logic/meta/format/Format.h
Normal file
57
api/logic/meta/format/Format.h
Normal file
@ -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 <QJsonObject>
|
||||
#include <memory>
|
||||
|
||||
#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;
|
||||
};
|
||||
}
|
161
api/logic/meta/format/FormatV1.cpp
Normal file
161
api/logic/meta/format/FormatV1.cpp
Normal file
@ -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 <minecraft/onesix/OneSixVersionFormat.h>
|
||||
|
||||
#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<QJsonObject> requiresRaw = obj.contains("requires") ? requireIsArrayOf<QJsonObject>(obj, "requires") : QVector<QJsonObject>();
|
||||
QVector<Reference> 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<Version>(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<QJsonObject> objects = requireIsArrayOf<QJsonObject>(obj, "index");
|
||||
QVector<VersionListPtr> lists;
|
||||
lists.reserve(objects.size());
|
||||
std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject &obj)
|
||||
{
|
||||
VersionListPtr list = std::make_shared<VersionList>(requireString(obj, "uid"));
|
||||
list->setName(ensureString(obj, "name", QString()));
|
||||
return list;
|
||||
});
|
||||
return std::make_shared<Index>(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<QJsonObject> versionsRaw = requireIsArrayOf<QJsonObject>(obj, "versions");
|
||||
QVector<VersionPtr> 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<VersionList>(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;
|
||||
}
|
||||
}
|
33
api/logic/meta/format/FormatV1.h
Normal file
33
api/logic/meta/format/FormatV1.h
Normal file
@ -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;
|
||||
};
|
||||
}
|
123
api/logic/meta/tasks/LocalLoadTask.cpp
Normal file
123
api/logic/meta/tasks/LocalLoadTask.cpp
Normal file
@ -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 <QFile>
|
||||
|
||||
#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<Index *>(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<VersionList *>(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<Version *>(entity());
|
||||
}
|
||||
}
|
84
api/logic/meta/tasks/LocalLoadTask.h
Normal file
84
api/logic/meta/tasks/LocalLoadTask.h
Normal file
@ -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 <memory>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
132
api/logic/meta/tasks/RemoteLoadTask.cpp
Normal file
132
api/logic/meta/tasks/RemoteLoadTask.cpp
Normal file
@ -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<Index *>(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<VersionList *>(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<Version *>(entity());
|
||||
}
|
||||
}
|
95
api/logic/meta/tasks/RemoteLoadTask.h
Normal file
95
api/logic/meta/tasks/RemoteLoadTask.h
Normal file
@ -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 <memory>
|
||||
|
||||
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<Net::Download> 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;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user