2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2015-2021 MultiMC Contributors
|
2016-04-06 22:09:30 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
#include "VersionList.h"
|
2016-04-06 22:09:30 +01:00
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
2017-03-18 01:22:36 +00:00
|
|
|
#include "JsonFormat.h"
|
2017-12-14 01:22:20 +00:00
|
|
|
#include "Version.h"
|
2017-03-11 00:39:45 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
namespace Meta {
|
|
|
|
VersionList::VersionList(const QString& uid, QObject* parent) : BaseVersionList(parent), m_uid(uid)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2017-03-11 00:39:45 +00:00
|
|
|
setObjectName("Version list: " + uid);
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-21 22:36:55 +00:00
|
|
|
Task::Ptr VersionList::getLoadTask()
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2017-11-11 00:38:31 +00:00
|
|
|
load(Net::Mode::Online);
|
2017-03-19 22:58:54 +00:00
|
|
|
return getCurrentTask();
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
bool VersionList::isLoaded()
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2017-03-19 22:58:54 +00:00
|
|
|
return BaseEntity::isLoaded();
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-01 22:48:26 +00:00
|
|
|
const BaseVersion::Ptr VersionList::at(int i) const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
return m_versions.at(i);
|
|
|
|
}
|
2017-03-11 00:39:45 +00:00
|
|
|
int VersionList::count() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
return m_versions.size();
|
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
void VersionList::sortVersions()
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2023-08-02 17:35:35 +01:00
|
|
|
std::sort(m_versions.begin(), m_versions.end(), [](const Version::Ptr& a, const Version::Ptr& b) { return *a.get() < *b.get(); });
|
2016-04-06 22:09:30 +01:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QVariant VersionList::data(const QModelIndex& index, int role) const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_versions.size() || index.parent().isValid()) {
|
2016-04-06 22:09:30 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-11-01 22:48:26 +00:00
|
|
|
Version::Ptr version = m_versions.at(index.row());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (role) {
|
|
|
|
case VersionPointerRole:
|
|
|
|
return QVariant::fromValue(std::dynamic_pointer_cast<BaseVersion>(version));
|
|
|
|
case VersionRole:
|
|
|
|
case VersionIdRole:
|
|
|
|
return version->version();
|
|
|
|
case ParentVersionRole: {
|
|
|
|
// FIXME: HACK: this should be generic and be replaced by something else. Anything that is a hard 'equals' dep is a 'parent
|
|
|
|
// uid'.
|
|
|
|
auto& reqs = version->requiredSet();
|
|
|
|
auto iter = std::find_if(reqs.begin(), reqs.end(), [](const Require& req) { return req.uid == "net.minecraft"; });
|
|
|
|
if (iter != reqs.end()) {
|
|
|
|
return (*iter).equalsVersion;
|
|
|
|
}
|
|
|
|
return QVariant();
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
case TypeRole:
|
|
|
|
return version->type();
|
|
|
|
|
|
|
|
case UidRole:
|
|
|
|
return version->uid();
|
|
|
|
case TimeRole:
|
|
|
|
return version->time();
|
|
|
|
case RequiresRole:
|
|
|
|
return QVariant::fromValue(version->requiredSet());
|
|
|
|
case SortRole:
|
|
|
|
return version->rawTime();
|
|
|
|
case VersionPtrRole:
|
|
|
|
return QVariant::fromValue(version);
|
|
|
|
case RecommendedRole:
|
|
|
|
return version->isRecommended();
|
|
|
|
// FIXME: this should be determined in whatever view/proxy is used...
|
|
|
|
// case LatestRole: return version == getLatestStable();
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
BaseVersionList::RoleList VersionList::providesRoles() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
return { VersionPointerRole, VersionRole, VersionIdRole, ParentVersionRole, TypeRole, UidRole,
|
|
|
|
TimeRole, RequiresRole, SortRole, RecommendedRole, LatestRole, VersionPtrRole };
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
QHash<int, QByteArray> VersionList::roleNames() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles = BaseVersionList::roleNames();
|
|
|
|
roles.insert(UidRole, "uid");
|
|
|
|
roles.insert(TimeRole, "time");
|
|
|
|
roles.insert(SortRole, "sort");
|
|
|
|
roles.insert(RequiresRole, "requires");
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
QString VersionList::localFilename() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2017-03-12 15:00:06 +00:00
|
|
|
return m_uid + "/index.json";
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:39:45 +00:00
|
|
|
QString VersionList::humanReadable() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
return m_name.isEmpty() ? m_uid : m_name;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
Version::Ptr VersionList::getVersion(const QString& version)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2022-11-01 22:48:26 +00:00
|
|
|
Version::Ptr out = m_lookup.value(version, nullptr);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!out) {
|
2017-03-19 01:13:49 +00:00
|
|
|
out = std::make_shared<Version>(m_uid, version);
|
|
|
|
m_lookup[version] = out;
|
|
|
|
}
|
|
|
|
return out;
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2022-07-30 21:38:02 +01:00
|
|
|
bool VersionList::hasVersion(QString version) const
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
auto ver =
|
|
|
|
std::find_if(m_versions.constBegin(), m_versions.constEnd(), [&](Meta::Version::Ptr const& a) { return a->version() == version; });
|
2022-07-30 21:38:02 +01:00
|
|
|
return (ver != m_versions.constEnd());
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void VersionList::setName(const QString& name)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
emit nameChanged(name);
|
|
|
|
}
|
2017-03-18 01:22:36 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void VersionList::setVersions(const QVector<Version::Ptr>& versions)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_versions = versions;
|
2023-08-02 17:35:35 +01:00
|
|
|
std::sort(m_versions.begin(), m_versions.end(),
|
|
|
|
[](const Version::Ptr& a, const Version::Ptr& b) { return a->rawTime() > b->rawTime(); });
|
|
|
|
for (int i = 0; i < m_versions.size(); ++i) {
|
2016-04-06 22:09:30 +01:00
|
|
|
m_lookup.insert(m_versions.at(i)->version(), m_versions.at(i));
|
|
|
|
setupAddedVersion(i, m_versions.at(i));
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:38:31 +00:00
|
|
|
// FIXME: this is dumb, we have 'recommended' as part of the metadata already...
|
2023-08-02 17:35:35 +01:00
|
|
|
auto recommendedIt =
|
|
|
|
std::find_if(m_versions.constBegin(), m_versions.constEnd(), [](const Version::Ptr& ptr) { return ptr->type() == "release"; });
|
2016-04-06 22:09:30 +01:00
|
|
|
m_recommended = recommendedIt == m_versions.constEnd() ? nullptr : *recommendedIt;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:48:54 +00:00
|
|
|
void VersionList::parse(const QJsonObject& obj)
|
|
|
|
{
|
|
|
|
parseVersionList(obj, this);
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:38:31 +00:00
|
|
|
// FIXME: this is dumb, we have 'recommended' as part of the metadata already...
|
2023-08-02 17:35:35 +01:00
|
|
|
static const Meta::Version::Ptr& getBetterVersion(const Meta::Version::Ptr& a, const Meta::Version::Ptr& b)
|
2017-11-11 00:38:31 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!a)
|
2017-11-11 00:38:31 +00:00
|
|
|
return b;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!b)
|
2017-11-11 00:38:31 +00:00
|
|
|
return a;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (a->type() == b->type()) {
|
2017-11-11 00:38:31 +00:00
|
|
|
// newer of same type wins
|
|
|
|
return (a->rawTime() > b->rawTime() ? a : b);
|
|
|
|
}
|
|
|
|
// 'release' type wins
|
|
|
|
return (a->type() == "release" ? a : b);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void VersionList::mergeFromIndex(const VersionList::Ptr& other)
|
2017-12-14 01:22:20 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_name != other->m_name) {
|
2017-12-14 01:22:20 +00:00
|
|
|
setName(other->m_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void VersionList::merge(const VersionList::Ptr& other)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_name != other->m_name) {
|
2017-12-14 01:22:20 +00:00
|
|
|
setName(other->m_name);
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2017-04-24 00:30:51 +01:00
|
|
|
// TODO: do not reset the whole model. maybe?
|
|
|
|
beginResetModel();
|
|
|
|
m_versions.clear();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (other->m_versions.isEmpty()) {
|
2017-12-13 23:29:00 +00:00
|
|
|
qWarning() << "Empty list loaded ...";
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
for (const Version::Ptr& version : other->m_versions) {
|
2017-04-24 00:30:51 +01:00
|
|
|
// we already have the version. merge the contents
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_lookup.contains(version->version())) {
|
2017-12-14 01:22:20 +00:00
|
|
|
m_lookup.value(version->version())->mergeFromList(version);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2017-04-24 00:30:51 +01:00
|
|
|
m_lookup.insert(version->uid(), version);
|
|
|
|
}
|
|
|
|
// connect it.
|
|
|
|
setupAddedVersion(m_versions.size(), version);
|
|
|
|
m_versions.append(version);
|
2017-11-11 00:38:31 +00:00
|
|
|
m_recommended = getBetterVersion(m_recommended, version);
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
2017-04-24 00:30:51 +01:00
|
|
|
endResetModel();
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void VersionList::setupAddedVersion(const int row, const Version::Ptr& version)
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
2017-04-24 00:30:51 +01:00
|
|
|
// FIXME: do not disconnect from everythin, disconnect only the lambdas here
|
|
|
|
version->disconnect();
|
2023-08-02 17:35:35 +01:00
|
|
|
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); });
|
2016-04-06 22:09:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-01 22:48:26 +00:00
|
|
|
BaseVersion::Ptr VersionList::getRecommended() const
|
2016-04-06 22:09:30 +01:00
|
|
|
{
|
|
|
|
return m_recommended;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
} // namespace Meta
|