feat: move resource pack images to QPixmapCache
This takes care of evicting entries when the cache gets too big for us, so we can add new entries without much worries. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
0331f5a1eb
commit
3b13e692d2
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
|
|
||||||
|
#include "minecraft/mod/tasks/LocalResourcePackParseTask.h"
|
||||||
|
|
||||||
// Values taken from:
|
// Values taken from:
|
||||||
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
// 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 = {
|
static const QMap<int, std::pair<Version, Version>> s_pack_format_versions = {
|
||||||
@ -41,7 +43,29 @@ void ResourcePack::setImage(QImage new_image)
|
|||||||
|
|
||||||
Q_ASSERT(!new_image.isNull());
|
Q_ASSERT(!new_image.isNull());
|
||||||
|
|
||||||
m_pack_image = new_image;
|
if (m_pack_image_cache_key.key.isValid())
|
||||||
|
QPixmapCache::remove(m_pack_image_cache_key.key);
|
||||||
|
|
||||||
|
m_pack_image_cache_key.key = QPixmapCache::insert(QPixmap::fromImage(new_image));
|
||||||
|
m_pack_image_cache_key.was_ever_used = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap ResourcePack::image(QSize size)
|
||||||
|
{
|
||||||
|
QPixmap cached_image;
|
||||||
|
if (QPixmapCache::find(m_pack_image_cache_key.key, &cached_image)) {
|
||||||
|
if (size.isNull())
|
||||||
|
return cached_image;
|
||||||
|
return cached_image.scaled(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No valid image we can get
|
||||||
|
if (!m_pack_image_cache_key.was_ever_used)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// Imaged got evicted from the cache. Re-process it and retry.
|
||||||
|
ResourcePackUtils::process(*this);
|
||||||
|
return image(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<Version, Version> ResourcePack::compatibleVersions() const
|
std::pair<Version, Version> ResourcePack::compatibleVersions() const
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QPixmap>
|
#include <QPixmap>
|
||||||
|
#include <QPixmapCache>
|
||||||
|
|
||||||
class Version;
|
class Version;
|
||||||
|
|
||||||
@ -30,8 +31,7 @@ class ResourcePack : public Resource {
|
|||||||
[[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. */
|
/** 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]] QPixmap image(QSize 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);
|
||||||
@ -57,8 +57,13 @@ class ResourcePack : public Resource {
|
|||||||
*/
|
*/
|
||||||
QString m_description;
|
QString m_description;
|
||||||
|
|
||||||
/** The resource pack's image, as per the pack.png file.
|
/** The resource pack's image file cache key, for access in the QPixmapCache global instance.
|
||||||
* TODO: This could probably be just a key into a static image cache.
|
*
|
||||||
|
* The 'was_ever_used' state simply identifies whether the key was never inserted on the cache (true),
|
||||||
|
* so as to tell whether a cache entry is inexistent or if it was just evicted from the cache.
|
||||||
*/
|
*/
|
||||||
QImage m_pack_image;
|
struct {
|
||||||
|
QPixmapCache::Key key;
|
||||||
|
bool was_ever_used = false;
|
||||||
|
} m_pack_image_cache_key;
|
||||||
};
|
};
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
#include <quazip/quazip.h>
|
#include <quazip/quazip.h>
|
||||||
#include <quazip/quazipfile.h>
|
#include <quazip/quazipfile.h>
|
||||||
|
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
namespace ResourcePackUtils {
|
namespace ResourcePackUtils {
|
||||||
|
|
||||||
bool process(ResourcePack& pack)
|
bool process(ResourcePack& pack)
|
||||||
|
@ -105,7 +105,7 @@ static const QMap<QChar, QString> s_value_to_color = {
|
|||||||
{'f', "#FFFFFF"}
|
{'f', "#FFFFFF"}
|
||||||
};
|
};
|
||||||
|
|
||||||
void InfoFrame::updateWithResourcePack(const ResourcePack& resource_pack)
|
void InfoFrame::updateWithResourcePack(ResourcePack& resource_pack)
|
||||||
{
|
{
|
||||||
setName(resource_pack.name());
|
setName(resource_pack.name());
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class InfoFrame : public QFrame {
|
|||||||
|
|
||||||
void updateWithMod(Mod const& m);
|
void updateWithMod(Mod const& m);
|
||||||
void updateWithResource(Resource const& resource);
|
void updateWithResource(Resource const& resource);
|
||||||
void updateWithResourcePack(ResourcePack const& rp);
|
void updateWithResourcePack(ResourcePack& rp);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void descriptionEllipsisHandler(QString link);
|
void descriptionEllipsisHandler(QString link);
|
||||||
|
Loading…
Reference in New Issue
Block a user