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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
// 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.
*
* === Upstream License Block (Do Not Modify) ==============================
*
*
*
*
*
*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2022 Rachel Powers <508861+Ryex@users.noreply.github.com>
* Copyright (C) 2022 kumquat-ir <66188216+kumquat-ir@users.noreply.github.com>
*
* 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 "BlockedModsDialog.h"
#include "ui_BlockedModsDialog.h"
#include "Application.h"
#include "modplatform/helpers/HashUtils.h"
#include <QDebug>
#include <QDesktopServices>
#include <QDialogButtonBox>
#include <QDir>
#include <QDirIterator>
#include <QDragEnterEvent>
#include <QFileDialog>
#include <QFileInfo>
#include <QMimeData>
#include <QPushButton>
#include <QStandardPaths>
#include <QTimer>
BlockedModsDialog::BlockedModsDialog(QWidget* parent,
const QString& title,
const QString& text,
QList<BlockedMod>& mods,
QString hash_type)
: QDialog(parent),
ui(new Ui::BlockedModsDialog),
m_mods(mods),
m_hashType(hash_type)
{
m_hashingTask = shared_qobject_ptr<ConcurrentTask>(
new ConcurrentTask("MakeHashesTask", APPLICATION->settings()->get("NumberOfConcurrentTasks").toInt()));
connect(m_hashingTask.get(), &Task::finished, this, &BlockedModsDialog::hashTaskFinished);
ui->setupUi(this);
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("OK"));
connect(ui->openMissingButton, &QPushButton::clicked, this, [this]() { openAll(true); });
connect(ui->downloadFolderButton, &QPushButton::clicked, this, &BlockedModsDialog::addDownloadFolder);
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &BlockedModsDialog::directoryChanged);
qDebug() << "[Blocked Mods Dialog] Mods List: " << mods;
// defer setup of file system watchers until after the dialog is shown
// this allows OS (namely macOS) permission prompts to show after the relevant dialog appears
QTimer::singleShot(0,
this,
[this]
{
setupWatch();
scanPaths();
update();
});
this->setWindowTitle(title);
ui->labelDescription->setText(text);
// force all URL handling as external
connect(ui->textBrowserWatched,
&QTextBrowser::anchorClicked,
this,
[](const QUrl url) { QDesktopServices::openUrl(url); });
setAcceptDrops(true);
update();
}
BlockedModsDialog::~BlockedModsDialog()
{
delete ui;
}
void BlockedModsDialog::dragEnterEvent(QDragEnterEvent* e)
{
if (e->mimeData()->hasUrls())
{
e->acceptProposedAction();
}
}
void BlockedModsDialog::dropEvent(QDropEvent* e)
{
for (QUrl& url : e->mimeData()->urls())
{
if (url.scheme().isEmpty())
{ // ensure isLocalFile() works correctly
url.setScheme("file");
}
if (!url.isLocalFile())
{ // can't drop external files here.
continue;
}
QString filePath = url.toLocalFile();
qDebug() << "[Blocked Mods Dialog] Dropped file:" << filePath;
addHashTask(filePath);
// watch for changes
QFileInfo file = QFileInfo(filePath);
QString path = file.dir().absolutePath();
qDebug() << "[Blocked Mods Dialog] Adding watch path:" << path;
m_watcher.addPath(path);
}
scanPaths();
update();
}
void BlockedModsDialog::done(int r)
{
QDialog::done(r);
disconnect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &BlockedModsDialog::directoryChanged);
}
void BlockedModsDialog::openAll(bool missingOnly)
{
for (auto& mod : m_mods)
{
if (!missingOnly || !mod.matched)
{
QDesktopServices::openUrl(mod.websiteUrl);
}
}
}
void BlockedModsDialog::addDownloadFolder()
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory where you downloaded the mods"),
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation),
QFileDialog::ShowDirsOnly);
qDebug() << "[Blocked Mods Dialog] Adding watch path:" << dir;
m_watcher.addPath(dir);
scanPath(dir, true);
update();
}
/// @brief update UI with current status of the blocked mod detection
void BlockedModsDialog::update()
{
QString text;
QString span;
for (auto& mod : m_mods)
{
if (mod.matched)
{
// ✔ -> html for HEAVY CHECK MARK : ✔
span = QString(tr("<span style=\"color:green\"> ✔ Found at %1 </span>")).arg(mod.localPath);
}
else
{
// ✘ -> html for HEAVY BALLOT X : ✘
span = QString(tr("<span style=\"color:red\"> ✘ Not Found </span>"));
}
text += QString(tr("%1: <a href='%2'>%2</a> <p>Hash: %3 %4</p> <br/>"))
.arg(mod.name, mod.websiteUrl, mod.hash, span);
}
ui->textBrowserModsListing->setText(text);
QString watching;
for (auto& dir : m_watcher.directories())
{
QUrl fileURL = QUrl::fromLocalFile(dir);
watching += QString("<a href=\"%1\">%2</a><br/>").arg(fileURL.toString(), dir);
}
ui->textBrowserWatched->setText(watching);
if (allModsMatched())
{
ui->labelModsFound->setText("<span style=\"color:green\">✔</span>" + tr("All mods found"));
ui->openMissingButton->setDisabled(true);
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("OK"));
}
else
{
ui->labelModsFound->setText(tr("Please download the missing mods."));
ui->openMissingButton->setDisabled(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Skip"));
}
}
/// @brief Signal fired when a watched directory has changed
/// @param path the path to the changed directory
void BlockedModsDialog::directoryChanged(QString path)
{
qDebug() << "[Blocked Mods Dialog] Directory changed: " << path;
validateMatchedMods();
scanPath(path, true);
}
/// @brief add the user downloads folder and the global mods folder to the filesystem watcher
void BlockedModsDialog::setupWatch()
{
const QString downloadsFolder = APPLICATION->settings()->get("DownloadsDir").toString();
const QString modsFolder = APPLICATION->settings()->get("CentralModsDir").toString();
const bool downloadsFolderWatchRecursive = APPLICATION->settings()->get("DownloadsDirWatchRecursive").toBool();
watchPath(downloadsFolder, downloadsFolderWatchRecursive);
watchPath(modsFolder, true);
}
void BlockedModsDialog::watchPath(QString path, bool watch_recursive)
{
auto to_watch = QFileInfo(path);
if (!to_watch.isReadable())
{
qWarning() << "[Blocked Mods Dialog] Failed to add Watch Path (unable to read):" << path;
return;
}
auto to_watch_path = to_watch.canonicalFilePath();
if (m_watcher.directories().contains(to_watch_path))
return; // don't watch the same path twice (no loops!)
qDebug() << "[Blocked Mods Dialog] Adding Watch Path:" << path;
m_watcher.addPath(to_watch_path);
if (!to_watch.isDir() || !watch_recursive)
return;
QDirIterator it(to_watch_path, QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot, QDirIterator::NoIteratorFlags);
while (it.hasNext())
{
QString watch_dir = QDir(it.next()).canonicalPath(); // resolve symlinks and relative paths
watchPath(watch_dir, watch_recursive);
}
}
/// @brief scan all watched folder
void BlockedModsDialog::scanPaths()
{
for (auto& dir : m_watcher.directories())
{
scanPath(dir, false);
}
runHashTask();
}
/// @brief Scan the directory at path, skip paths that do not contain a file name
/// of a blocked mod we are looking for
/// @param path the directory to scan
void BlockedModsDialog::scanPath(QString path, bool start_task)
{
QDir scan_dir(path);
QDirIterator scan_it(path, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::NoIteratorFlags);
while (scan_it.hasNext())
{
QString file = scan_it.next();
if (!checkValidPath(file))
{
continue;
}
addHashTask(file);
}
if (start_task)
{
runHashTask();
}
}
/// @brief add a hashing task for the file located at path, add the path to the pending set if the hashing task is already running
/// @param path the path to the local file being hashed
void BlockedModsDialog::addHashTask(QString path)
{
qDebug() << "[Blocked Mods Dialog] adding a Hash task for" << path << "to the pending set.";
m_pendingHashPaths.insert(path);
}
/// @brief add a hashing task for the file located at path and connect it to check that hash against
/// our blocked mods list
/// @param path the path to the local file being hashed
void BlockedModsDialog::buildHashTask(QString path)
{
auto hash_task = Hashing::createHasher(path, m_hashType);
qDebug() << "[Blocked Mods Dialog] Creating Hash task for path: " << path;
connect(hash_task.get(),
&Task::succeeded,
this,
[this, hash_task, path] { checkMatchHash(hash_task->getResult(), path); });
connect(hash_task.get(), &Task::failed, this, [path] { qDebug() << "Failed to hash path: " << path; });
m_hashingTask->addTask(hash_task);
}
/// @brief check if the computed hash for the provided path matches a blocked
/// mod we are looking for
/// @param hash the computed hash for the provided path
/// @param path the path to the local file being compared
void BlockedModsDialog::checkMatchHash(QString hash, QString path)
{
bool match = false;
qDebug() << "[Blocked Mods Dialog] Checking for match on hash: " << hash << "| From path:" << path;
auto downloadDir = QFileInfo(APPLICATION->settings()->get("DownloadsDir").toString()).absoluteFilePath();
auto moveFiles = APPLICATION->settings()->get("MoveModsFromDownloadsDir").toBool();
for (auto& mod : m_mods)
{
if (mod.matched)
{
continue;
}
if (mod.hash.compare(hash, Qt::CaseInsensitive) == 0)
{
mod.matched = true;
mod.localPath = path;
if (moveFiles)
{
mod.move = QFileInfo(path).absoluteFilePath().startsWith(downloadDir);
}
match = true;
qDebug() << "[Blocked Mods Dialog] Hash match found:" << mod.name << hash << "| From path:" << path;
break;
}
}
if (match)
{
update();
}
}
/// @brief Check if the name of the file at path matches the name of a blocked mod we are searching for
/// @param path the path to check
/// @return boolean: did the path match the name of a blocked mod?
bool BlockedModsDialog::checkValidPath(QString path)
{
const QFileInfo file = QFileInfo(path);
const QString filename = file.fileName();
auto compare = [](QString fsFilename, QString metadataFilename)
{ return metadataFilename.compare(fsFilename, Qt::CaseInsensitive) == 0; };
// super lax compare (but not fuzzy)
// convert to lowercase
// convert all speratores to whitespace
// simplify sequence of internal whitespace to a single space
// efectivly compare two strings ignoring all separators and case
auto laxCompare = [](QString fsfilename, QString metadataFilename)
{
// allowed character seperators
QList<QChar> allowedSeperators = { '-', '+', '.', '_' };
// copy in lowercase
auto fsName = fsfilename.toLower();
auto metaName = metadataFilename.toLower();
// replace all potential allowed seperatores with whitespace
for (auto sep : allowedSeperators)
{
fsName = fsName.replace(sep, ' ');
metaName = metaName.replace(sep, ' ');
}
// remove extraneous whitespace
fsName = fsName.simplified();
metaName = metaName.simplified();
return fsName.compare(metaName) == 0;
};
auto downloadDir = QFileInfo(APPLICATION->settings()->get("DownloadsDir").toString()).absoluteFilePath();
auto moveFiles = APPLICATION->settings()->get("MoveModsFromDownloadsDir").toBool();
for (auto& mod : m_mods)
{
if (compare(filename, mod.name))
{
// if the mod is not yet matched and doesn't have a hash then
// just match it with the file that has the exact same name
if (!mod.matched && mod.hash.isEmpty())
{
mod.matched = true;
mod.localPath = path;
if (moveFiles)
{
mod.move = QFileInfo(path).absoluteFilePath().startsWith(downloadDir);
}
return false;
}
qDebug() << "[Blocked Mods Dialog] Name match found:" << mod.name << "| From path:" << path;
return true;
}
if (laxCompare(filename, mod.name))
{
qDebug() << "[Blocked Mods Dialog] Lax name match found:" << mod.name << "| From path:" << path;
return true;
}
}
return false;
}
bool BlockedModsDialog::allModsMatched()
{
return std::all_of(m_mods.begin(), m_mods.end(), [](auto const& mod) { return mod.matched; });
}
/// @brief ensure matched file paths still exist
void BlockedModsDialog::validateMatchedMods()
{
bool changed = false;
for (auto& mod : m_mods)
{
if (mod.matched)
{
QFileInfo file = QFileInfo(mod.localPath);
if (!file.exists() || !file.isFile())
{
qDebug() << "[Blocked Mods Dialog] File" << mod.localPath << "for mod" << mod.name
<< "has vanshed! marking as not matched.";
mod.localPath = "";
mod.matched = false;
changed = true;
}
}
}
if (changed)
{
update();
}
}
/// @brief run hash task or mark a pending run if it is already running
void BlockedModsDialog::runHashTask()
{
if (!m_hashingTask->isRunning())
{
m_rehashPending = false;
if (!m_pendingHashPaths.isEmpty())
{
qDebug() << "[Blocked Mods Dialog] there are pending hash tasks, building and running tasks";
auto path = m_pendingHashPaths.begin();
while (path != m_pendingHashPaths.end())
{
buildHashTask(*path);
path = m_pendingHashPaths.erase(path);
}
m_hashingTask->start();
}
}
else
{
qDebug() << "[Blocked Mods Dialog] queueing another run of the hashing task";
qDebug() << "[Blocked Mods Dialog] pending hash tasks:" << m_pendingHashPaths;
m_rehashPending = true;
}
}
void BlockedModsDialog::hashTaskFinished()
{
qDebug() << "[Blocked Mods Dialog] All hash tasks finished";
if (m_rehashPending)
{
qDebug() << "[Blocked Mods Dialog] task finished with a rehash pending, rerunning";
runHashTask();
}
}
/// qDebug print support for the BlockedMod struct
QDebug operator<<(QDebug debug, const BlockedMod& m)
{
QDebugStateSaver saver(debug);
debug.nospace() << "{ name: " << m.name << ", websiteUrl: " << m.websiteUrl << ", hash: " << m.hash
<< ", matched: " << m.matched << ", localPath: " << m.localPath << "}";
return debug;
}
|