2022-06-12 12:50:58 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
|
|
|
*/
|
2013-08-14 07:13:41 +01:00
|
|
|
|
2019-08-04 02:27:53 +01:00
|
|
|
#include "ModFolderModel.h"
|
2022-04-16 00:35:17 +01:00
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <FileSystem.h>
|
2022-04-16 00:35:17 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QFileSystemWatcher>
|
2013-08-18 19:52:17 +01:00
|
|
|
#include <QMimeData>
|
2013-12-26 04:14:32 +00:00
|
|
|
#include <QString>
|
2019-08-04 02:27:53 +01:00
|
|
|
#include <QThreadPool>
|
2022-04-16 00:35:17 +01:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QUuid>
|
2019-08-04 02:27:53 +01:00
|
|
|
#include <algorithm>
|
2022-04-16 00:35:17 +01:00
|
|
|
|
|
|
|
#include "minecraft/mod/tasks/LocalModParseTask.h"
|
|
|
|
#include "minecraft/mod/tasks/ModFolderLoadTask.h"
|
2013-08-14 07:13:41 +01:00
|
|
|
|
2022-08-10 18:48:34 +01:00
|
|
|
ModFolderModel::ModFolderModel(const QString &dir, bool is_indexed) : ResourceFolderModel(QDir(dir)), m_is_indexed(is_indexed)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2022-08-10 18:42:24 +01:00
|
|
|
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::VERSION, SortType::DATE };
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:56:38 +01:00
|
|
|
QVariant ModFolderModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!validateIndex(index))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
int column = index.column();
|
|
|
|
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (column)
|
|
|
|
{
|
|
|
|
case NameColumn:
|
|
|
|
return m_resources[row]->name();
|
|
|
|
case VersionColumn: {
|
|
|
|
switch(m_resources[row]->type()) {
|
|
|
|
case ResourceType::FOLDER:
|
|
|
|
return tr("Folder");
|
|
|
|
case ResourceType::SINGLEFILE:
|
|
|
|
return tr("File");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return at(row)->version();
|
|
|
|
}
|
|
|
|
case DateColumn:
|
|
|
|
return m_resources[row]->dateTimeChanged();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return m_resources[row]->internal_id();
|
|
|
|
|
|
|
|
case Qt::CheckStateRole:
|
|
|
|
switch (column)
|
|
|
|
{
|
|
|
|
case ActiveColumn:
|
|
|
|
return at(row)->enabled() ? Qt::Checked : Qt::Unchecked;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ModFolderModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (section)
|
|
|
|
{
|
|
|
|
case ActiveColumn:
|
|
|
|
return QString();
|
|
|
|
case NameColumn:
|
|
|
|
return tr("Name");
|
|
|
|
case VersionColumn:
|
|
|
|
return tr("Version");
|
|
|
|
case DateColumn:
|
|
|
|
return tr("Last changed");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
switch (section)
|
|
|
|
{
|
|
|
|
case ActiveColumn:
|
|
|
|
return tr("Is the mod enabled?");
|
|
|
|
case NameColumn:
|
|
|
|
return tr("The name of the mod.");
|
|
|
|
case VersionColumn:
|
|
|
|
return tr("The version of the mod.");
|
|
|
|
case DateColumn:
|
|
|
|
return tr("The date and time this mod was last changed (or added).");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModFolderModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return NUM_COLUMNS;
|
|
|
|
}
|
|
|
|
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
Task* ModFolderModel::createUpdateTask()
|
2013-08-25 00:32:42 +01:00
|
|
|
{
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
auto index_dir = indexDir();
|
2022-09-03 17:25:05 +01:00
|
|
|
auto task = new ModFolderLoadTask(dir(), index_dir, m_is_indexed, m_first_folder_load);
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
m_first_folder_load = false;
|
|
|
|
return task;
|
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-08-29 02:33:44 +01:00
|
|
|
Task* ModFolderModel::createParseTask(Resource& resource)
|
2022-08-09 16:56:38 +01:00
|
|
|
{
|
|
|
|
return new LocalModParseTask(m_next_resolution_ticket, resource.type(), resource.fileinfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModFolderModel::uninstallMod(const QString& filename, bool preserve_metadata)
|
|
|
|
{
|
|
|
|
for(auto mod : allMods()){
|
|
|
|
if(mod->fileinfo().fileName() == filename){
|
|
|
|
auto index_dir = indexDir();
|
|
|
|
mod->destroy(index_dir, preserve_metadata);
|
2022-08-12 21:09:56 +01:00
|
|
|
|
|
|
|
update();
|
|
|
|
|
2022-08-09 16:56:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModFolderModel::deleteMods(const QModelIndexList& indexes)
|
|
|
|
{
|
|
|
|
if(!m_can_interact) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(indexes.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto i: indexes)
|
|
|
|
{
|
|
|
|
if(i.column() != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto m = at(i.row());
|
|
|
|
auto index_dir = indexDir();
|
|
|
|
m->destroy(index_dir);
|
|
|
|
}
|
2022-08-12 21:09:56 +01:00
|
|
|
|
|
|
|
update();
|
|
|
|
|
2022-08-09 16:56:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModFolderModel::isValid()
|
|
|
|
{
|
|
|
|
return m_dir.exists() && m_dir.isReadable();
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:42:24 +01:00
|
|
|
bool ModFolderModel::startWatching()
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
{
|
2022-08-11 17:00:09 +01:00
|
|
|
// Remove orphaned metadata next time
|
|
|
|
m_first_folder_load = true;
|
2022-08-10 18:42:24 +01:00
|
|
|
return ResourceFolderModel::startWatching({ m_dir.absolutePath(), indexDir().absolutePath() });
|
2013-08-25 00:32:42 +01:00
|
|
|
}
|
|
|
|
|
2022-08-10 18:42:24 +01:00
|
|
|
bool ModFolderModel::stopWatching()
|
2013-08-25 00:32:42 +01:00
|
|
|
{
|
2022-08-10 18:42:24 +01:00
|
|
|
return ResourceFolderModel::stopWatching({ m_dir.absolutePath(), indexDir().absolutePath() });
|
2013-08-25 00:32:42 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 22:24:26 +01:00
|
|
|
auto ModFolderModel::selectedMods(QModelIndexList& indexes) -> QList<Mod*>
|
2022-08-09 16:56:38 +01:00
|
|
|
{
|
2022-08-11 22:24:26 +01:00
|
|
|
QList<Mod*> selected_resources;
|
2022-08-09 16:56:38 +01:00
|
|
|
for (auto i : indexes) {
|
|
|
|
if(i.column() != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
selected_resources.push_back(at(i.row()));
|
|
|
|
}
|
|
|
|
return selected_resources;
|
|
|
|
}
|
|
|
|
|
2022-08-11 22:24:26 +01:00
|
|
|
auto ModFolderModel::allMods() -> QList<Mod*>
|
2022-08-09 16:56:38 +01:00
|
|
|
{
|
2022-08-11 22:24:26 +01:00
|
|
|
QList<Mod*> mods;
|
2022-08-09 16:56:38 +01:00
|
|
|
|
2022-09-17 00:02:21 +01:00
|
|
|
for (auto& res : qAsConst(m_resources)) {
|
2022-08-09 16:56:38 +01:00
|
|
|
mods.append(static_cast<Mod*>(res.get()));
|
2022-08-11 22:24:26 +01:00
|
|
|
}
|
2022-08-09 16:56:38 +01:00
|
|
|
|
|
|
|
return mods;
|
|
|
|
}
|
|
|
|
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
void ModFolderModel::onUpdateSucceeded()
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
auto update_results = static_cast<ModFolderLoadTask*>(m_current_update_task.get())->result();
|
2022-04-15 04:24:57 +01:00
|
|
|
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
auto& new_mods = update_results->mods;
|
2019-08-04 02:27:53 +01:00
|
|
|
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
auto current_list = m_resources_index.keys();
|
|
|
|
QSet<QString> current_set(current_list.begin(), current_list.end());
|
|
|
|
|
|
|
|
auto new_list = new_mods.keys();
|
|
|
|
QSet<QString> new_set(new_list.begin(), new_list.end());
|
2022-05-02 18:10:45 +01:00
|
|
|
#else
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
QSet<QString> current_set(m_resources_index.keys().toSet());
|
|
|
|
QSet<QString> new_set(new_mods.keys().toSet());
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2019-08-04 02:27:53 +01:00
|
|
|
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
applyUpdates(current_set, new_set, new_mods);
|
2019-08-04 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
void ModFolderModel::onParseSucceeded(int ticket, QString mod_id)
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
auto iter = m_active_parse_tasks.constFind(ticket);
|
|
|
|
if (iter == m_active_parse_tasks.constEnd())
|
2019-08-04 02:27:53 +01:00
|
|
|
return;
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
|
|
|
|
int row = m_resources_index[mod_id];
|
|
|
|
|
|
|
|
auto parse_task = *iter;
|
|
|
|
auto cast_task = static_cast<LocalModParseTask*>(parse_task.get());
|
|
|
|
|
|
|
|
Q_ASSERT(cast_task->token() == ticket);
|
|
|
|
|
|
|
|
auto resource = find(mod_id);
|
|
|
|
|
|
|
|
auto result = cast_task->result();
|
|
|
|
if (result && resource)
|
2022-08-12 21:06:20 +01:00
|
|
|
resource->finishResolvingWithDetails(std::move(result->details));
|
refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource
packs, shader packs, etc) from mods, so that we don't have to
continuously watch our backs for changes to one of them affecting the
others.
To do so, this creates a more general list model for resources, based on
the mods one, that allows you to extend it with functionality for other
resources.
I had to do some template and preprocessor stuff to get around the
QObject limitation of not allowing templated classes, so that's sadge :c
On the other hand, I tried cleaning up most general-purpose code in the
mod model, and added some documentation, because it looks nice :D
Signed-off-by: flow <flowlnlnln@gmail.com>
2022-08-09 05:58:22 +01:00
|
|
|
|
2019-08-04 02:27:53 +01:00
|
|
|
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
|
2019-07-15 00:07:21 +01:00
|
|
|
}
|