From e62ca29da90a18bcc4c1fa9401d291b8126400c7 Mon Sep 17 00:00:00 2001 From: kobewi Date: Wed, 31 May 2023 16:52:50 +0200 Subject: [PATCH] Add a way to invalidate preview cache --- editor/editor_resource_preview.cpp | 12 ++++++++---- editor/editor_resource_preview.h | 5 +++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index ddf230dfdb4..dd698d74b62 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -118,8 +118,6 @@ Variant EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() const { return Variant(); // Needed because of how the callback is used. } -EditorResourcePreview *EditorResourcePreview::singleton = nullptr; - bool EditorResourcePreview::is_threaded() const { return RSG::texture_storage->can_create_resources_async(); } @@ -291,13 +289,17 @@ void EditorResourcePreview::_iterate() { bool has_small_texture; uint64_t last_modtime; String hash; - _read_preview_cache(f, &tsize, &has_small_texture, &last_modtime, &hash, &preview_metadata); + bool outdated; + _read_preview_cache(f, &tsize, &has_small_texture, &last_modtime, &hash, &preview_metadata, &outdated); bool cache_valid = true; if (tsize != thumbnail_size) { cache_valid = false; f.unref(); + } else if (outdated) { + cache_valid = false; + f.unref(); } else if (last_modtime != modtime) { String last_md5 = f->get_line(); String md5 = FileAccess::get_md5(item.path); @@ -357,14 +359,16 @@ void EditorResourcePreview::_write_preview_cache(Ref p_file, int p_t p_file->store_line(itos(p_modified_time)); p_file->store_line(p_hash); p_file->store_line(VariantUtilityFunctions::var_to_str(p_metadata).replace("\n", " ")); + p_file->store_line(itos(CURRENT_METADATA_VERSION)); } -void EditorResourcePreview::_read_preview_cache(Ref p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata) { +void EditorResourcePreview::_read_preview_cache(Ref p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata, bool *r_outdated) { *r_thumbnail_size = p_file->get_line().to_int(); *r_has_small_texture = p_file->get_line().to_int(); *r_modified_time = p_file->get_line().to_int(); *r_hash = p_file->get_line(); *r_metadata = VariantUtilityFunctions::str_to_var(p_file->get_line()); + *r_outdated = p_file->get_line().to_int() < CURRENT_METADATA_VERSION; } void EditorResourcePreview::_thread() { diff --git a/editor/editor_resource_preview.h b/editor/editor_resource_preview.h index 84616517324..6b67acceaac 100644 --- a/editor/editor_resource_preview.h +++ b/editor/editor_resource_preview.h @@ -76,7 +76,8 @@ public: class EditorResourcePreview : public Node { GDCLASS(EditorResourcePreview, Node); - static EditorResourcePreview *singleton; + inline static constexpr int CURRENT_METADATA_VERSION = 1; // Increment this number to invalidate all previews. + inline static EditorResourcePreview *singleton = nullptr; struct QueueItem { Ref resource; @@ -118,7 +119,7 @@ class EditorResourcePreview : public Node { void _iterate(); void _write_preview_cache(Ref p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, const String &p_hash, const Dictionary &p_metadata); - void _read_preview_cache(Ref p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata); + void _read_preview_cache(Ref p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata, bool *r_outdated); Vector> preview_generators;