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
|
// 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 "java/services/RuntimeCatalog.hpp"
#include <QtNetwork>
#include <QtXml>
#include <QDebug>
#include <algorithm>
#include "Application.h"
#include "java/services/RuntimeScanner.hpp"
#include "tasks/ConcurrentTask.h"
namespace projt::java
{
RuntimeCatalog::RuntimeCatalog(QObject* parent, Scope scope) : BaseVersionList(parent), m_scope(scope)
{}
Task::Ptr RuntimeCatalog::getLoadTask()
{
load();
return currentTask();
}
Task::Ptr RuntimeCatalog::currentTask() const
{
if (m_state == State::Loading)
{
return m_task;
}
return nullptr;
}
void RuntimeCatalog::load()
{
if (m_state != State::Loading)
{
m_state = State::Loading;
m_task.reset(new RuntimeCatalogTask(this, m_scope));
m_task->start();
}
}
const BaseVersion::Ptr RuntimeCatalog::at(int i) const
{
return m_entries.at(i);
}
bool RuntimeCatalog::isLoaded()
{
return m_state == State::Ready;
}
int RuntimeCatalog::count() const
{
return m_entries.count();
}
QVariant RuntimeCatalog::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() > count())
return QVariant();
auto runtime = std::dynamic_pointer_cast<RuntimeInstall>(m_entries[index.row()]);
switch (role)
{
case SortRole: return -index.row();
case VersionPointerRole: return QVariant::fromValue(m_entries[index.row()]);
case VersionIdRole: return runtime->descriptor();
case VersionRole: return runtime->version.toString();
case RecommendedRole: return false;
case PathRole: return runtime->path;
case CPUArchitectureRole: return runtime->arch;
default: return QVariant();
}
}
BaseVersionList::RoleList RuntimeCatalog::providesRoles() const
{
return { VersionPointerRole, VersionIdRole, VersionRole, RecommendedRole, PathRole, CPUArchitectureRole };
}
void RuntimeCatalog::updateListData(QList<BaseVersion::Ptr> versions)
{
beginResetModel();
m_entries = std::move(versions);
sortVersions();
endResetModel();
m_state = State::Ready;
m_task.reset();
}
static bool sortRuntimes(BaseVersion::Ptr left, BaseVersion::Ptr right)
{
auto rleft = std::dynamic_pointer_cast<RuntimeInstall>(right);
auto rright = std::dynamic_pointer_cast<RuntimeInstall>(left);
return (*rleft) > (*rright);
}
void RuntimeCatalog::sortVersions()
{
std::sort(m_entries.begin(), m_entries.end(), sortRuntimes);
}
RuntimeCatalogTask::RuntimeCatalogTask(RuntimeCatalog* catalog, RuntimeCatalog::Scope scope)
: Task(),
m_catalog(catalog),
m_scope(scope)
{}
void RuntimeCatalogTask::executeTask()
{
setStatus(tr("Detecting Java installations..."));
RuntimeScanner scanner;
QStringList candidatePaths = scanner.collectPaths(m_scope == RuntimeCatalog::Scope::ManagedOnly);
ConcurrentTask::Ptr job(
new ConcurrentTask("Runtime detection", APPLICATION->settings()->get("NumberOfConcurrentTasks").toInt()));
m_job.reset(job);
connect(m_job.get(), &Task::finished, this, &RuntimeCatalogTask::probeFinished);
connect(m_job.get(), &Task::progress, this, &Task::setProgress);
qDebug() << "Probing the following runtime paths:";
int token = 0;
for (const auto& candidate : candidatePaths)
{
RuntimeProbeTask::ProbeSettings settings;
settings.binaryPath = candidate;
settings.token = token;
auto probe = new RuntimeProbeTask(settings);
connect(probe,
&RuntimeProbeTask::probeFinished,
this,
[this](const RuntimeProbeTask::ProbeReport& report) { m_results << report; });
job->addTask(Task::Ptr(probe));
token++;
}
m_job->start();
}
void RuntimeCatalogTask::probeFinished()
{
QList<RuntimeInstallPtr> candidates;
std::sort(m_results.begin(),
m_results.end(),
[](const RuntimeProbeTask::ProbeReport& a, const RuntimeProbeTask::ProbeReport& b)
{ return a.token < b.token; });
qDebug() << "Found the following valid Java installations:";
for (const auto& result : m_results)
{
if (result.status == RuntimeProbeTask::ProbeReport::Status::Valid)
{
RuntimeInstallPtr runtime(new RuntimeInstall());
runtime->version = result.version;
runtime->arch = result.platformArch;
runtime->path = result.path;
runtime->vendor = result.vendor;
runtime->is_64bit = result.is_64bit;
runtime->managed = (m_scope == RuntimeCatalog::Scope::ManagedOnly);
candidates.append(runtime);
qDebug() << " " << runtime->version.toString() << runtime->arch << runtime->path;
}
}
QList<BaseVersion::Ptr> entries;
for (const auto& runtime : candidates)
{
BaseVersion::Ptr base = std::dynamic_pointer_cast<BaseVersion>(runtime);
if (base)
{
entries.append(runtime);
}
}
m_catalog->updateListData(entries);
emitSucceeded();
}
} // namespace projt::java
|