2022-05-10 23:57:47 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-06-12 12:50:58 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-05-10 23:57:47 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-08-04 02:27:53 +01:00
|
|
|
|
2022-04-16 02:07:35 +01:00
|
|
|
#include "Mod.h"
|
|
|
|
|
2022-08-09 05:53:50 +01:00
|
|
|
#include <QDebug>
|
2019-08-04 02:27:53 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QString>
|
2022-08-10 18:42:24 +01:00
|
|
|
#include <QRegularExpression>
|
2019-08-04 02:27:53 +01:00
|
|
|
|
2023-05-05 07:42:42 +01:00
|
|
|
#include "MTPixmapCache.h"
|
2022-04-16 17:27:29 +01:00
|
|
|
#include "MetadataHandler.h"
|
2022-08-10 18:42:24 +01:00
|
|
|
#include "Version.h"
|
2022-12-10 07:52:50 +00:00
|
|
|
#include "minecraft/mod/ModDetails.h"
|
2023-05-05 07:42:42 +01:00
|
|
|
#include "minecraft/mod/tasks/LocalModParseTask.h"
|
2019-08-04 02:27:53 +01:00
|
|
|
|
2022-12-15 15:02:08 +00:00
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
2022-08-12 21:06:20 +01:00
|
|
|
Mod::Mod(const QFileInfo& file) : Resource(file), m_local_details()
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
2022-08-09 05:53:50 +01:00
|
|
|
m_enabled = (file.suffix() != "disabled");
|
2019-08-04 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
2022-04-16 17:27:29 +01:00
|
|
|
Mod::Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata)
|
2022-08-09 05:53:50 +01:00
|
|
|
: Mod(mods_dir.absoluteFilePath(metadata.filename))
|
2022-04-15 04:24:57 +01:00
|
|
|
{
|
2022-08-09 05:53:50 +01:00
|
|
|
m_name = metadata.name;
|
2022-08-12 21:06:20 +01:00
|
|
|
m_local_details.metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
|
2022-04-15 04:24:57 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:45:39 +01:00
|
|
|
void Mod::setStatus(ModStatus status)
|
|
|
|
{
|
2022-08-12 21:06:20 +01:00
|
|
|
m_local_details.status = status;
|
2022-04-20 22:45:39 +01:00
|
|
|
}
|
2022-08-12 21:06:20 +01:00
|
|
|
void Mod::setMetadata(std::shared_ptr<Metadata::ModStruct>&& metadata)
|
2022-04-20 22:45:39 +01:00
|
|
|
{
|
2022-06-05 03:11:25 +01:00
|
|
|
if (status() == ModStatus::NoMetadata)
|
2022-04-20 22:45:39 +01:00
|
|
|
setStatus(ModStatus::Installed);
|
|
|
|
|
2022-08-12 21:06:20 +01:00
|
|
|
m_local_details.metadata = metadata;
|
2022-04-20 22:45:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 07:52:50 +00:00
|
|
|
void Mod::setDetails(const ModDetails& details) {
|
|
|
|
m_local_details = details;
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:42:24 +01:00
|
|
|
std::pair<int, bool> Mod::compare(const Resource& other, SortType type) const
|
|
|
|
{
|
|
|
|
auto cast_other = dynamic_cast<Mod const*>(&other);
|
|
|
|
if (!cast_other)
|
|
|
|
return Resource::compare(other, type);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
case SortType::ENABLED:
|
|
|
|
case SortType::NAME:
|
|
|
|
case SortType::DATE: {
|
|
|
|
auto res = Resource::compare(other, type);
|
|
|
|
if (res.first != 0)
|
|
|
|
return res;
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-08-10 18:42:24 +01:00
|
|
|
}
|
|
|
|
case SortType::VERSION: {
|
|
|
|
auto this_ver = Version(version());
|
|
|
|
auto other_ver = Version(cast_other->version());
|
|
|
|
if (this_ver > other_ver)
|
|
|
|
return { 1, type == SortType::VERSION };
|
|
|
|
if (this_ver < other_ver)
|
|
|
|
return { -1, type == SortType::VERSION };
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-08-10 18:42:24 +01:00
|
|
|
}
|
2022-12-29 15:39:20 +00:00
|
|
|
case SortType::PROVIDER: {
|
|
|
|
auto compare_result = QString::compare(provider().value_or("Unknown"), cast_other->provider().value_or("Unknown"), Qt::CaseInsensitive);
|
2022-12-15 15:02:08 +00:00
|
|
|
if (compare_result != 0)
|
|
|
|
return { compare_result, type == SortType::PROVIDER };
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-12-29 15:39:20 +00:00
|
|
|
}
|
2022-08-10 18:42:24 +01:00
|
|
|
}
|
|
|
|
return { 0, false };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mod::applyFilter(QRegularExpression filter) const
|
|
|
|
{
|
|
|
|
if (filter.match(description()).hasMatch())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto& author : authors()) {
|
|
|
|
if (filter.match(author).hasMatch()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Resource::applyFilter(filter);
|
|
|
|
}
|
|
|
|
|
2022-06-10 23:27:25 +01:00
|
|
|
auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
2022-06-19 18:26:15 +01:00
|
|
|
if (!preserve_metadata) {
|
|
|
|
qDebug() << QString("Destroying metadata for '%1' on purpose").arg(name());
|
|
|
|
|
|
|
|
if (metadata()) {
|
|
|
|
Metadata::remove(index_dir, metadata()->slug);
|
|
|
|
} else {
|
|
|
|
auto n = name();
|
|
|
|
Metadata::remove(index_dir, n);
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 04:24:57 +01:00
|
|
|
|
2022-08-09 05:53:50 +01:00
|
|
|
return Resource::destroy();
|
2019-08-04 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::details() const -> const ModDetails&
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
2022-08-12 21:06:20 +01:00
|
|
|
return m_local_details;
|
2019-08-04 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::name() const -> QString
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
2022-04-16 02:07:35 +01:00
|
|
|
auto d_name = details().name;
|
2022-06-10 20:43:01 +01:00
|
|
|
if (!d_name.isEmpty())
|
2022-04-16 02:07:35 +01:00
|
|
|
return d_name;
|
2022-06-10 20:43:01 +01:00
|
|
|
|
2022-06-19 18:26:15 +01:00
|
|
|
if (metadata())
|
2022-06-10 20:43:01 +01:00
|
|
|
return metadata()->name;
|
|
|
|
|
2019-08-04 02:27:53 +01:00
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::version() const -> QString
|
2022-04-16 02:07:35 +01:00
|
|
|
{
|
|
|
|
return details().version;
|
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::homeurl() const -> QString
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
|
|
|
return details().homeurl;
|
|
|
|
}
|
|
|
|
|
2023-06-25 08:41:29 +01:00
|
|
|
auto Mod::metaurl() const -> QString
|
|
|
|
{
|
|
|
|
if (metadata() == nullptr)
|
|
|
|
return homeurl();
|
|
|
|
return ModPlatform::getMetaURL(metadata()->provider, metadata()->project_id);
|
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::description() const -> QString
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
|
|
|
return details().description;
|
|
|
|
}
|
|
|
|
|
2022-04-20 00:19:51 +01:00
|
|
|
auto Mod::authors() const -> QStringList
|
2019-08-04 02:27:53 +01:00
|
|
|
{
|
|
|
|
return details().authors;
|
|
|
|
}
|
2022-04-17 13:30:32 +01:00
|
|
|
|
2022-04-20 22:45:39 +01:00
|
|
|
auto Mod::status() const -> ModStatus
|
|
|
|
{
|
|
|
|
return details().status;
|
|
|
|
}
|
|
|
|
|
2022-06-05 03:11:25 +01:00
|
|
|
auto Mod::metadata() -> std::shared_ptr<Metadata::ModStruct>
|
|
|
|
{
|
2022-08-12 21:06:20 +01:00
|
|
|
return m_local_details.metadata;
|
2022-06-05 03:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Mod::metadata() const -> const std::shared_ptr<Metadata::ModStruct>
|
|
|
|
{
|
2022-08-12 21:06:20 +01:00
|
|
|
return m_local_details.metadata;
|
2022-06-05 03:11:25 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 21:06:20 +01:00
|
|
|
void Mod::finishResolvingWithDetails(ModDetails&& details)
|
2022-04-17 13:30:32 +01:00
|
|
|
{
|
2022-08-09 05:53:50 +01:00
|
|
|
m_is_resolving = false;
|
|
|
|
m_is_resolved = true;
|
2022-04-17 13:30:32 +01:00
|
|
|
|
2022-08-12 21:06:20 +01:00
|
|
|
std::shared_ptr<Metadata::ModStruct> metadata = details.metadata;
|
|
|
|
if (details.status == ModStatus::Unknown)
|
|
|
|
details.status = m_local_details.status;
|
2022-06-19 18:26:15 +01:00
|
|
|
|
2022-08-12 21:06:20 +01:00
|
|
|
m_local_details = std::move(details);
|
|
|
|
if (metadata)
|
|
|
|
setMetadata(std::move(metadata));
|
2023-05-05 07:42:42 +01:00
|
|
|
if (!iconPath().isEmpty()) {
|
|
|
|
m_pack_image_cache_key.was_read_attempt = false;
|
|
|
|
}
|
2023-05-05 19:13:36 +01:00
|
|
|
}
|
2022-12-15 15:02:08 +00:00
|
|
|
|
2022-12-29 15:39:20 +00:00
|
|
|
auto Mod::provider() const -> std::optional<QString>
|
2022-12-15 15:02:08 +00:00
|
|
|
{
|
2022-12-29 15:39:20 +00:00
|
|
|
if (metadata())
|
2022-12-15 15:02:08 +00:00
|
|
|
return ProviderCaps.readableName(metadata()->provider);
|
2022-12-29 15:39:20 +00:00
|
|
|
return {};
|
2022-04-17 13:30:32 +01:00
|
|
|
}
|
2022-12-10 07:52:50 +00:00
|
|
|
|
2023-05-05 21:46:38 +01:00
|
|
|
auto Mod::licenses() const -> const QList<ModLicense>&
|
|
|
|
{
|
|
|
|
return details().licenses;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Mod::issueTracker() const -> QString
|
|
|
|
{
|
|
|
|
return details().issue_tracker;
|
|
|
|
}
|
2023-05-05 07:42:42 +01:00
|
|
|
|
|
|
|
void Mod::setIcon(QImage new_image) const
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_data_lock);
|
|
|
|
|
|
|
|
Q_ASSERT(!new_image.isNull());
|
|
|
|
|
|
|
|
if (m_pack_image_cache_key.key.isValid())
|
|
|
|
PixmapCache::remove(m_pack_image_cache_key.key);
|
|
|
|
|
|
|
|
// scale the image to avoid flooding the pixmapcache
|
2023-05-05 19:28:19 +01:00
|
|
|
auto pixmap = QPixmap::fromImage(new_image.scaled({64, 64}, Qt::AspectRatioMode::KeepAspectRatioByExpanding));
|
2023-05-05 07:42:42 +01:00
|
|
|
|
|
|
|
m_pack_image_cache_key.key = PixmapCache::insert(pixmap);
|
|
|
|
m_pack_image_cache_key.was_ever_used = true;
|
|
|
|
m_pack_image_cache_key.was_read_attempt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap Mod::icon(QSize size, Qt::AspectRatioMode mode) const
|
|
|
|
{
|
|
|
|
QPixmap cached_image;
|
|
|
|
if (PixmapCache::find(m_pack_image_cache_key.key, &cached_image)) {
|
|
|
|
if (size.isNull())
|
|
|
|
return cached_image;
|
|
|
|
return cached_image.scaled(size, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No valid image we can get
|
|
|
|
if ((!m_pack_image_cache_key.was_ever_used && m_pack_image_cache_key.was_read_attempt) || iconPath().isEmpty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (m_pack_image_cache_key.was_ever_used) {
|
|
|
|
qDebug() << "Mod" << name() << "Had it's icon evicted form the cache. reloading...";
|
|
|
|
PixmapCache::markCacheMissByEviciton();
|
|
|
|
}
|
2023-05-05 19:13:36 +01:00
|
|
|
// Image got evicted from the cache or an attempt to load it has not been made. load it and retry.
|
2023-05-05 07:42:42 +01:00
|
|
|
m_pack_image_cache_key.was_read_attempt = true;
|
|
|
|
ModUtils::loadIconFile(*this);
|
|
|
|
return icon(size);
|
|
|
|
}
|
|
|
|
|
2022-12-10 07:52:50 +00:00
|
|
|
bool Mod::valid() const
|
|
|
|
{
|
|
|
|
return !m_local_details.mod_id.isEmpty();
|
2022-12-26 21:29:13 +00:00
|
|
|
}
|