feat: add image from pack.png to resource packs

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-08-29 09:15:06 -03:00
parent dd9e30b24a
commit 9b984cedac
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 65 additions and 3 deletions

View File

@ -35,6 +35,15 @@ void ResourcePack::setDescription(QString new_description)
m_description = new_description;
}
void ResourcePack::setImage(QImage new_image)
{
QMutexLocker locker(&m_data_lock);
Q_ASSERT(!new_image.isNull());
m_pack_image = new_image;
}
std::pair<Version, Version> ResourcePack::compatibleVersions() const
{
if (!s_pack_format_versions.contains(m_pack_format)) {

View File

@ -2,7 +2,9 @@
#include "Resource.h"
#include <QImage>
#include <QMutex>
#include <QPixmap>
class Version;
@ -28,12 +30,19 @@ class ResourcePack : public Resource {
/** Gets the description of the resource pack. */
[[nodiscard]] QString description() const { return m_description; }
/** Gets the image of the resource pack, converted to a QPixmap for drawing, and scaled to size. */
[[nodiscard]] QPixmap image(QSize size) const { return QPixmap::fromImage(m_pack_image).scaled(size); }
[[nodiscard]] QSize image_size() const { return m_pack_image.size(); }
/** Thread-safe. */
void setPackFormat(int new_format_id);
/** Thread-safe. */
void setDescription(QString new_description);
/** Thread-safe. */
void setImage(QImage new_image);
[[nodiscard]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override;
[[nodiscard]] bool applyFilter(QRegularExpression filter) const override;
@ -48,4 +57,9 @@ class ResourcePack : public Resource {
/** The resource pack's description, as defined in the pack.mcmeta file.
*/
QString m_description;
/** The resource pack's image, as per the pack.png file.
* TODO: This could probably be just a key into a static image cache.
*/
QImage m_pack_image;
};

View File

@ -54,6 +54,16 @@ void LocalResourcePackParseTask::processMCMeta(QByteArray&& raw_data)
}
}
void LocalResourcePackParseTask::processPackPNG(QByteArray&& raw_data)
{
auto img = QImage::fromData(raw_data);
if (!img.isNull()) {
m_resource_pack.setImage(img);
} else {
qWarning() << "Failed to parse pack.png.";
}
}
void LocalResourcePackParseTask::processAsFolder()
{
QFileInfo mcmeta_file_info(FS::PathCombine(m_resource_pack.fileinfo().filePath(), "pack.mcmeta"));
@ -63,10 +73,23 @@ void LocalResourcePackParseTask::processAsFolder()
return;
auto data = mcmeta_file.readAll();
if (data.isEmpty() || data.isNull())
return;
processMCMeta(std::move(data));
mcmeta_file.close();
}
QFileInfo image_file_info(FS::PathCombine(m_resource_pack.fileinfo().filePath(), "pack.png"));
if (image_file_info.isFile()) {
QFile mcmeta_file(image_file_info.filePath());
if (!mcmeta_file.open(QIODevice::ReadOnly))
return;
auto data = mcmeta_file.readAll();
processPackPNG(std::move(data));
mcmeta_file.close();
}
}
@ -90,6 +113,21 @@ void LocalResourcePackParseTask::processAsZip()
processMCMeta(std::move(data));
file.close();
}
if (zip.setCurrentFile("pack.png")) {
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open file in zip.";
zip.close();
return;
}
auto data = file.readAll();
processPackPNG(std::move(data));
file.close();
}
zip.close();
}
}

View File

@ -21,6 +21,7 @@ class LocalResourcePackParseTask : public Task {
private:
void processMCMeta(QByteArray&& raw_data);
void processPackPNG(QByteArray&& raw_data);
void processAsFolder();
void processAsZip();