feat: add image from pack.png to resource packs
Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
dd9e30b24a
commit
9b984cedac
@ -35,6 +35,15 @@ void ResourcePack::setDescription(QString new_description)
|
|||||||
m_description = 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
|
std::pair<Version, Version> ResourcePack::compatibleVersions() const
|
||||||
{
|
{
|
||||||
if (!s_pack_format_versions.contains(m_pack_format)) {
|
if (!s_pack_format_versions.contains(m_pack_format)) {
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
#include "Resource.h"
|
#include "Resource.h"
|
||||||
|
|
||||||
|
#include <QImage>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
#include <QPixmap>
|
||||||
|
|
||||||
class Version;
|
class Version;
|
||||||
|
|
||||||
@ -28,12 +30,19 @@ class ResourcePack : public Resource {
|
|||||||
/** Gets the description of the resource pack. */
|
/** Gets the description of the resource pack. */
|
||||||
[[nodiscard]] QString description() const { return m_description; }
|
[[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. */
|
/** Thread-safe. */
|
||||||
void setPackFormat(int new_format_id);
|
void setPackFormat(int new_format_id);
|
||||||
|
|
||||||
/** Thread-safe. */
|
/** Thread-safe. */
|
||||||
void setDescription(QString new_description);
|
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]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override;
|
||||||
[[nodiscard]] bool applyFilter(QRegularExpression filter) const 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.
|
/** The resource pack's description, as defined in the pack.mcmeta file.
|
||||||
*/
|
*/
|
||||||
QString m_description;
|
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;
|
||||||
};
|
};
|
||||||
|
@ -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()
|
void LocalResourcePackParseTask::processAsFolder()
|
||||||
{
|
{
|
||||||
QFileInfo mcmeta_file_info(FS::PathCombine(m_resource_pack.fileinfo().filePath(), "pack.mcmeta"));
|
QFileInfo mcmeta_file_info(FS::PathCombine(m_resource_pack.fileinfo().filePath(), "pack.mcmeta"));
|
||||||
@ -63,10 +73,23 @@ void LocalResourcePackParseTask::processAsFolder()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
auto data = mcmeta_file.readAll();
|
auto data = mcmeta_file.readAll();
|
||||||
if (data.isEmpty() || data.isNull())
|
|
||||||
return;
|
|
||||||
|
|
||||||
processMCMeta(std::move(data));
|
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));
|
processMCMeta(std::move(data));
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
zip.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();
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ class LocalResourcePackParseTask : public Task {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void processMCMeta(QByteArray&& raw_data);
|
void processMCMeta(QByteArray&& raw_data);
|
||||||
|
void processPackPNG(QByteArray&& raw_data);
|
||||||
|
|
||||||
void processAsFolder();
|
void processAsFolder();
|
||||||
void processAsZip();
|
void processAsZip();
|
||||||
|
Loading…
Reference in New Issue
Block a user