2022-08-29 02:29:52 +01:00
|
|
|
#include "ResourcePack.h"
|
|
|
|
|
2022-11-15 21:13:15 +00:00
|
|
|
#include <QCoreApplication>
|
2022-08-29 02:29:52 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QMap>
|
2022-08-29 02:58:43 +01:00
|
|
|
#include <QRegularExpression>
|
2022-08-29 02:29:52 +01:00
|
|
|
|
2022-11-15 21:13:15 +00:00
|
|
|
#include "MTPixmapCache.h"
|
2022-08-29 02:29:52 +01:00
|
|
|
#include "Version.h"
|
|
|
|
|
2022-08-30 22:06:13 +01:00
|
|
|
#include "minecraft/mod/tasks/LocalResourcePackParseTask.h"
|
|
|
|
|
2022-08-29 02:29:52 +01:00
|
|
|
// Values taken from:
|
|
|
|
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
|
|
|
static const QMap<int, std::pair<Version, Version>> s_pack_format_versions = {
|
2022-12-26 21:29:13 +00:00
|
|
|
{ 1, { Version("1.6.1"), Version("1.8.9") } }, { 2, { Version("1.9"), Version("1.10.2") } },
|
|
|
|
{ 3, { Version("1.11"), Version("1.12.2") } }, { 4, { Version("1.13"), Version("1.14.4") } },
|
|
|
|
{ 5, { Version("1.15"), Version("1.16.1") } }, { 6, { Version("1.16.2"), Version("1.16.5") } },
|
|
|
|
{ 7, { Version("1.17"), Version("1.17.1") } }, { 8, { Version("1.18"), Version("1.18.2") } },
|
|
|
|
{ 9, { Version("1.19"), Version("1.19.2") } }, { 11, { Version("22w42a"), Version("22w44a") } },
|
2023-06-06 20:10:01 +01:00
|
|
|
{ 12, { Version("1.19.3"), Version("1.19.3") } }, { 13, { Version("1.19.4"), Version("1.19.4") } },
|
|
|
|
{ 14, { Version("1.20"), Version("1.20") } }
|
2022-08-29 02:29:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void ResourcePack::setPackFormat(int new_format_id)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_data_lock);
|
|
|
|
|
|
|
|
if (!s_pack_format_versions.contains(new_format_id)) {
|
2022-12-25 03:38:29 +00:00
|
|
|
qWarning() << "Pack format '" << new_format_id << "' is not a recognized resource pack id!";
|
2022-08-29 02:29:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_pack_format = new_format_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourcePack::setDescription(QString new_description)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_data_lock);
|
|
|
|
|
|
|
|
m_description = new_description;
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:46:00 +01:00
|
|
|
void ResourcePack::setImage(QImage new_image) const
|
2022-08-29 13:15:06 +01:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_data_lock);
|
|
|
|
|
|
|
|
Q_ASSERT(!new_image.isNull());
|
|
|
|
|
2022-08-30 22:06:13 +01:00
|
|
|
if (m_pack_image_cache_key.key.isValid())
|
2022-11-15 21:13:15 +00:00
|
|
|
PixmapCache::instance().remove(m_pack_image_cache_key.key);
|
2022-08-30 22:06:13 +01:00
|
|
|
|
2023-05-05 07:46:00 +01:00
|
|
|
// 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:46:00 +01:00
|
|
|
|
|
|
|
m_pack_image_cache_key.key = PixmapCache::instance().insert(pixmap);
|
2022-08-30 22:06:13 +01:00
|
|
|
m_pack_image_cache_key.was_ever_used = true;
|
2022-11-15 15:07:11 +00:00
|
|
|
|
|
|
|
// This can happen if the pixmap is too big to fit in the cache :c
|
|
|
|
if (!m_pack_image_cache_key.key.isValid()) {
|
|
|
|
qWarning() << "Could not insert a image cache entry! Ignoring it.";
|
|
|
|
m_pack_image_cache_key.was_ever_used = false;
|
|
|
|
}
|
2022-08-30 22:06:13 +01:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:46:00 +01:00
|
|
|
QPixmap ResourcePack::image(QSize size, Qt::AspectRatioMode mode) const
|
2022-08-30 22:06:13 +01:00
|
|
|
{
|
|
|
|
QPixmap cached_image;
|
2022-11-15 21:13:15 +00:00
|
|
|
if (PixmapCache::instance().find(m_pack_image_cache_key.key, &cached_image)) {
|
2022-08-30 22:06:13 +01:00
|
|
|
if (size.isNull())
|
|
|
|
return cached_image;
|
2023-05-05 07:46:00 +01:00
|
|
|
return cached_image.scaled(size, mode);
|
2022-08-30 22:06:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// No valid image we can get
|
2023-05-05 07:46:00 +01:00
|
|
|
if (!m_pack_image_cache_key.was_ever_used) {
|
2022-08-30 22:06:13 +01:00
|
|
|
return {};
|
2023-05-05 07:46:00 +01:00
|
|
|
} else {
|
|
|
|
qDebug() << "Resource Pack" << name() << "Had it's image evicted from the cache. reloading...";
|
|
|
|
PixmapCache::markCacheMissByEviciton();
|
|
|
|
}
|
2022-08-30 22:06:13 +01:00
|
|
|
|
|
|
|
// Imaged got evicted from the cache. Re-process it and retry.
|
2023-05-05 07:46:00 +01:00
|
|
|
ResourcePackUtils::processPackPNG(*this);
|
2022-08-30 22:06:13 +01:00
|
|
|
return image(size);
|
2022-08-29 13:15:06 +01:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:29:52 +01:00
|
|
|
std::pair<Version, Version> ResourcePack::compatibleVersions() const
|
|
|
|
{
|
|
|
|
if (!s_pack_format_versions.contains(m_pack_format)) {
|
2022-08-29 02:52:29 +01:00
|
|
|
return { {}, {} };
|
2022-08-29 02:29:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_pack_format_versions.constFind(m_pack_format).value();
|
|
|
|
}
|
2022-08-29 02:52:29 +01:00
|
|
|
|
|
|
|
std::pair<int, bool> ResourcePack::compare(const Resource& other, SortType type) const
|
|
|
|
{
|
|
|
|
auto const& cast_other = static_cast<ResourcePack const&>(other);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
default: {
|
|
|
|
auto res = Resource::compare(other, type);
|
|
|
|
if (res.first != 0)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
case SortType::PACK_FORMAT: {
|
|
|
|
auto this_ver = packFormat();
|
|
|
|
auto other_ver = cast_other.packFormat();
|
|
|
|
|
|
|
|
if (this_ver > other_ver)
|
|
|
|
return { 1, type == SortType::PACK_FORMAT };
|
|
|
|
if (this_ver < other_ver)
|
|
|
|
return { -1, type == SortType::PACK_FORMAT };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { 0, false };
|
|
|
|
}
|
2022-08-29 02:58:43 +01:00
|
|
|
|
|
|
|
bool ResourcePack::applyFilter(QRegularExpression filter) const
|
|
|
|
{
|
|
|
|
if (filter.match(description()).hasMatch())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (filter.match(QString::number(packFormat())).hasMatch())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (filter.match(compatibleVersions().first.toString()).hasMatch())
|
|
|
|
return true;
|
|
|
|
if (filter.match(compatibleVersions().second.toString()).hasMatch())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Resource::applyFilter(filter);
|
|
|
|
}
|
2022-11-19 15:38:05 +00:00
|
|
|
|
|
|
|
bool ResourcePack::valid() const
|
|
|
|
{
|
2022-11-27 17:41:52 +00:00
|
|
|
return m_pack_format != 0;
|
2022-11-19 15:38:05 +00:00
|
|
|
}
|