1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
* ProjT Launcher - Minecraft Launcher
* Copyright (C) 2026 Project Tick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "VersionList.hpp"
#include <QDateTime>
#include <QEventLoop>
#include <algorithm>
#include "Application.h"
#include "Index.hpp"
#include "JsonFormat.hpp"
#include "net/Mode.h"
#include "tasks/SequentialTask.h"
namespace projt::meta
{
MetaVersionList::MetaVersionList(const QString& componentUid, QObject* parent)
: BaseVersionList(parent),
m_uid(componentUid)
{
setObjectName("MetaVersionList: " + componentUid);
m_activeRoles = { VersionPointerRole, VersionRole, VersionIdRole, ParentVersionRole,
TypeRole, ComponentUidRole, TimestampRole, DependencyRole,
SortRole, RecommendedRole, LatestRole, VersionPtrRole };
}
bool MetaVersionList::isLoaded()
{
return MetaEntity::isReady();
}
Task::Ptr MetaVersionList::getLoadTask()
{
auto seq = makeShared<SequentialTask>(tr("Load metadata for %1").arg(m_uid));
seq->addTask(APPLICATION->metadataIndex()->createLoadTask(Net::Mode::Online));
seq->addTask(this->createLoadTask(Net::Mode::Online));
return seq;
}
const BaseVersion::Ptr MetaVersionList::at(int i) const
{
return m_entries.at(i);
}
int MetaVersionList::count() const
{
return m_entries.size();
}
void MetaVersionList::sortVersions()
{
beginResetModel();
std::sort(m_entries.begin(),
m_entries.end(),
[](const MetaVersion::Ptr& a, const MetaVersion::Ptr& b)
{ return a->asComparableVersion() < b->asComparableVersion(); });
endResetModel();
}
QVariant MetaVersionList::data(const QModelIndex& idx, int role) const
{
if (!idx.isValid() || idx.row() < 0 || idx.row() >= m_entries.size())
return QVariant();
const MetaVersion::Ptr& ver = m_entries.at(idx.row());
switch (role)
{
case VersionPointerRole: return QVariant::fromValue(std::dynamic_pointer_cast<BaseVersion>(ver));
case VersionRole:
case VersionIdRole: return ver->versionId();
case ParentVersionRole:
{
QString parent = ver->parentComponentVersion();
return parent.isEmpty() ? QVariant() : parent;
}
case TypeRole: return ver->releaseType();
case ComponentUidRole: return ver->componentUid();
case TimestampRole: return ver->releaseTime();
case DependencyRole: return QVariant::fromValue(ver->dependencies());
case SortRole: return ver->releaseTimestamp();
case VersionPtrRole: return QVariant::fromValue(ver);
case RecommendedRole: return ver->isStable() || m_externalStableVersions.contains(ver->versionId());
case JavaMajorRole:
{
QString display = ver->versionId();
if (display.startsWith("java"))
display = "Java " + display.mid(4);
return display;
}
default: return QVariant();
}
}
BaseVersionList::RoleList MetaVersionList::providesRoles() const
{
return m_activeRoles;
}
void MetaVersionList::setAvailableRoles(RoleList roles)
{
m_activeRoles = roles;
}
QHash<int, QByteArray> MetaVersionList::roleNames() const
{
QHash<int, QByteArray> names = BaseVersionList::roleNames();
names.insert(ComponentUidRole, "componentUid");
names.insert(TimestampRole, "timestamp");
names.insert(SortRole, "sortKey");
names.insert(DependencyRole, "dependencies");
return names;
}
QString MetaVersionList::cacheFilePath() const
{
return m_uid + "/index.json";
}
void MetaVersionList::loadFromJson(const QJsonObject& json)
{
loadVersionListFromJson(json, this);
}
MetaVersion::Ptr MetaVersionList::getMetaVersion(const QString& versionId)
{
return m_versionIndex.value(versionId, nullptr);
}
MetaVersion::Ptr MetaVersionList::getOrCreateVersion(const QString& versionId)
{
MetaVersion::Ptr existing = m_versionIndex.value(versionId);
if (existing)
return existing;
auto newVer = std::make_shared<MetaVersion>(m_uid, versionId);
int row = m_entries.size();
beginInsertRows(QModelIndex(), row, row);
m_versionIndex.insert(versionId, newVer);
registerVersion(row, newVer);
m_entries.append(newVer);
endInsertRows();
return newVer;
}
bool MetaVersionList::containsVersion(const QString& versionId) const
{
return m_versionIndex.contains(versionId);
}
void MetaVersionList::setDisplayName(const QString& name)
{
m_displayName = name;
emit displayNameChanged(name);
}
void MetaVersionList::setVersionEntries(const QList<MetaVersion::Ptr>& entries)
{
beginResetModel();
m_entries = entries;
std::sort(m_entries.begin(),
m_entries.end(),
[](const MetaVersion::Ptr& a, const MetaVersion::Ptr& b)
{ return a->releaseTimestamp() > b->releaseTimestamp(); });
m_versionIndex.clear();
for (int i = 0; i < m_entries.size(); ++i)
{
const auto& ver = m_entries.at(i);
m_versionIndex.insert(ver->versionId(), ver);
registerVersion(i, ver);
}
auto stableIt = std::find_if(m_entries.constBegin(),
m_entries.constEnd(),
[](const MetaVersion::Ptr& v) { return v->releaseType() == "release"; });
m_stableVersion = (stableIt != m_entries.constEnd()) ? *stableIt : nullptr;
endResetModel();
}
void MetaVersionList::addExternalStableVersions(const QStringList& versions)
{
m_externalStableVersions.append(versions);
}
void MetaVersionList::clearExternalStableVersions()
{
m_externalStableVersions.clear();
}
const MetaVersion::Ptr& MetaVersionList::selectBetterVersion(const MetaVersion::Ptr& a, const MetaVersion::Ptr& b)
{
if (!a)
return b;
if (!b)
return a;
if (a->releaseType() != b->releaseType())
{
if (a->releaseType() == "release")
return a;
if (b->releaseType() == "release")
return b;
}
return (a->releaseTimestamp() > b->releaseTimestamp()) ? a : b;
}
void MetaVersionList::updateFromIndex(const MetaVersionList::Ptr& other)
{
if (m_displayName != other->m_displayName)
setDisplayName(other->m_displayName);
if (!other->m_expectedSha256.isEmpty())
m_expectedSha256 = other->m_expectedSha256;
}
void MetaVersionList::mergeWith(const MetaVersionList::Ptr& other)
{
if (m_displayName != other->m_displayName)
setDisplayName(other->m_displayName);
if (!other->m_expectedSha256.isEmpty())
m_expectedSha256 = other->m_expectedSha256;
beginResetModel();
for (const auto& incoming : other->m_entries)
{
MetaVersion::Ptr existing = m_versionIndex.value(incoming->versionId());
if (existing)
{
existing->updateMetadataFrom(incoming);
}
else
{
m_versionIndex.insert(incoming->versionId(), incoming);
int row = m_entries.size();
registerVersion(row, incoming);
m_entries.append(incoming);
}
m_stableVersion = selectBetterVersion(m_stableVersion, incoming);
}
endResetModel();
}
void MetaVersionList::registerVersion(int row, const MetaVersion::Ptr& version)
{
disconnect(version.get(), &MetaVersion::dependenciesChanged, this, nullptr);
disconnect(version.get(), &MetaVersion::timestampChanged, this, nullptr);
disconnect(version.get(), &MetaVersion::releaseTypeChanged, this, nullptr);
connect(version.get(),
&MetaVersion::dependenciesChanged,
this,
[this, row]() { emit dataChanged(index(row), index(row), { DependencyRole }); });
connect(version.get(),
&MetaVersion::timestampChanged,
this,
[this, row]() { emit dataChanged(index(row), index(row), { TimestampRole, SortRole }); });
connect(version.get(),
&MetaVersion::releaseTypeChanged,
this,
[this, row]() { emit dataChanged(index(row), index(row), { TypeRole }); });
}
BaseVersion::Ptr MetaVersionList::getRecommended() const
{
return m_stableVersion;
}
void MetaVersionList::waitUntilReady()
{
if (isLoaded())
return;
QEventLoop loop;
auto task = getLoadTask();
connect(task.get(), &Task::finished, &loop, &QEventLoop::quit);
task->start();
loop.exec();
}
MetaVersion::Ptr MetaVersionList::stableForParent(const QString& parentUid, const QString& parentVersion)
{
for (const auto& ver : m_entries)
{
if (!ver->isStable())
continue;
const auto& deps = ver->dependencies();
auto match = std::find_if(deps.begin(),
deps.end(),
[&](const ComponentDependency& d)
{ return d.uid == parentUid && d.equalsVersion == parentVersion; });
if (match != deps.end())
return ver;
}
return nullptr;
}
MetaVersion::Ptr MetaVersionList::latestForParent(const QString& parentUid, const QString& parentVersion)
{
MetaVersion::Ptr best = nullptr;
for (const auto& ver : m_entries)
{
const auto& deps = ver->dependencies();
auto match = std::find_if(deps.begin(),
deps.end(),
[&](const ComponentDependency& d)
{ return d.uid == parentUid && d.equalsVersion == parentVersion; });
if (match != deps.end())
best = selectBetterVersion(best, ver);
}
return best;
}
} // namespace projt::meta
|