2022-06-12 17:20:58 +05:30
|
|
|
// 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 11:43:41 +05:30
|
|
|
|
2019-08-04 06:57:53 +05:30
|
|
|
#include "ModFolderModel.h"
|
2022-04-16 05:05:17 +05:30
|
|
|
|
2015-10-05 05:17:27 +05:30
|
|
|
#include <FileSystem.h>
|
2022-04-16 05:05:17 +05:30
|
|
|
#include <QDebug>
|
|
|
|
#include <QFileSystemWatcher>
|
2013-08-19 00:22:17 +05:30
|
|
|
#include <QMimeData>
|
2013-12-26 09:44:32 +05:30
|
|
|
#include <QString>
|
2019-08-04 06:57:53 +05:30
|
|
|
#include <QThreadPool>
|
2022-04-16 05:05:17 +05:30
|
|
|
#include <QUrl>
|
|
|
|
#include <QUuid>
|
2019-08-04 06:57:53 +05:30
|
|
|
#include <algorithm>
|
2022-04-16 05:05:17 +05:30
|
|
|
|
|
|
|
#include "minecraft/mod/tasks/LocalModParseTask.h"
|
|
|
|
#include "minecraft/mod/tasks/ModFolderLoadTask.h"
|
2013-08-14 11:43:41 +05:30
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
ModFolderModel::ModFolderModel(const QString &dir, bool is_indexed) : ResourceFolderModel(QDir(dir)), m_is_indexed(is_indexed)
|
2013-08-14 11:43:41 +05:30
|
|
|
{
|
2022-08-10 23:12:24 +05:30
|
|
|
m_column_sort_keys = { SortType::ENABLED, SortType::NAME, SortType::VERSION, SortType::DATE };
|
2013-08-14 11:43:41 +05:30
|
|
|
}
|
|
|
|
|
2022-08-09 21:26:38 +05:30
|
|
|
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 10:28:22 +05:30
|
|
|
Task* ModFolderModel::createUpdateTask()
|
2013-08-25 05:02:42 +05:30
|
|
|
{
|
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 10:28:22 +05:30
|
|
|
auto index_dir = indexDir();
|
2022-09-03 21:55:05 +05:30
|
|
|
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 10:28:22 +05:30
|
|
|
m_first_folder_load = false;
|
|
|
|
return task;
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-08-29 07:03:44 +05:30
|
|
|
Task* ModFolderModel::createParseTask(Resource& resource)
|
2022-08-09 21:26:38 +05:30
|
|
|
{
|
|
|
|
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-13 01:39:56 +05:30
|
|
|
|
|
|
|
update();
|
|
|
|
|
2022-08-09 21:26:38 +05:30
|
|
|
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-13 01:39:56 +05:30
|
|
|
|
|
|
|
update();
|
|
|
|
|
2022-08-09 21:26:38 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModFolderModel::isValid()
|
|
|
|
{
|
|
|
|
return m_dir.exists() && m_dir.isReadable();
|
|
|
|
}
|
|
|
|
|
2022-08-10 23:12:24 +05:30
|
|
|
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 10:28:22 +05:30
|
|
|
{
|
2022-08-11 21:30:09 +05:30
|
|
|
// Remove orphaned metadata next time
|
|
|
|
m_first_folder_load = true;
|
2022-08-10 23:12:24 +05:30
|
|
|
return ResourceFolderModel::startWatching({ m_dir.absolutePath(), indexDir().absolutePath() });
|
2013-08-25 05:02:42 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:12:24 +05:30
|
|
|
bool ModFolderModel::stopWatching()
|
2013-08-25 05:02:42 +05:30
|
|
|
{
|
2022-08-10 23:12:24 +05:30
|
|
|
return ResourceFolderModel::stopWatching({ m_dir.absolutePath(), indexDir().absolutePath() });
|
2013-08-25 05:02:42 +05:30
|
|
|
}
|
|
|
|
|
2022-08-12 02:54:26 +05:30
|
|
|
auto ModFolderModel::selectedMods(QModelIndexList& indexes) -> QList<Mod*>
|
2022-08-09 21:26:38 +05:30
|
|
|
{
|
2022-08-12 02:54:26 +05:30
|
|
|
QList<Mod*> selected_resources;
|
2022-08-09 21:26:38 +05:30
|
|
|
for (auto i : indexes) {
|
|
|
|
if(i.column() != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
selected_resources.push_back(at(i.row()));
|
|
|
|
}
|
|
|
|
return selected_resources;
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:54:26 +05:30
|
|
|
auto ModFolderModel::allMods() -> QList<Mod*>
|
2022-08-09 21:26:38 +05:30
|
|
|
{
|
2022-08-12 02:54:26 +05:30
|
|
|
QList<Mod*> mods;
|
2022-08-09 21:26:38 +05:30
|
|
|
|
2022-09-17 04:32:21 +05:30
|
|
|
for (auto& res : qAsConst(m_resources)) {
|
2022-08-09 21:26:38 +05:30
|
|
|
mods.append(static_cast<Mod*>(res.get()));
|
2022-08-12 02:54:26 +05:30
|
|
|
}
|
2022-08-09 21:26:38 +05:30
|
|
|
|
|
|
|
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 10:28:22 +05:30
|
|
|
void ModFolderModel::onUpdateSucceeded()
|
2013-08-14 11:43:41 +05:30
|
|
|
{
|
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 10:28:22 +05:30
|
|
|
auto update_results = static_cast<ModFolderLoadTask*>(m_current_update_task.get())->result();
|
2022-04-15 08:54:57 +05:30
|
|
|
|
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 10:28:22 +05:30
|
|
|
auto& new_mods = update_results->mods;
|
2019-08-04 06:57:53 +05:30
|
|
|
|
2022-05-02 22:40:45 +05:30
|
|
|
#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 10:28:22 +05:30
|
|
|
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 22:40:45 +05:30
|
|
|
#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 10:28:22 +05:30
|
|
|
QSet<QString> current_set(m_resources_index.keys().toSet());
|
|
|
|
QSet<QString> new_set(new_mods.keys().toSet());
|
2022-05-02 22:40:45 +05:30
|
|
|
#endif
|
2019-08-04 06:57:53 +05:30
|
|
|
|
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 10:28:22 +05:30
|
|
|
applyUpdates(current_set, new_set, new_mods);
|
2019-08-04 06:57:53 +05:30
|
|
|
}
|
|
|
|
|
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 10:28:22 +05:30
|
|
|
void ModFolderModel::onParseSucceeded(int ticket, QString mod_id)
|
2019-08-04 06:57:53 +05:30
|
|
|
{
|
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 10:28:22 +05:30
|
|
|
auto iter = m_active_parse_tasks.constFind(ticket);
|
|
|
|
if (iter == m_active_parse_tasks.constEnd())
|
2019-08-04 06:57:53 +05:30
|
|
|
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 10:28:22 +05:30
|
|
|
|
|
|
|
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-13 01:36:20 +05:30
|
|
|
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 10:28:22 +05:30
|
|
|
|
2019-08-04 06:57:53 +05:30
|
|
|
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
|
2019-07-15 04:37:21 +05:30
|
|
|
}
|