Add a way to invalidate preview cache

This commit is contained in:
kobewi 2023-05-31 16:52:50 +02:00
parent 6118592c6d
commit e62ca29da9
2 changed files with 11 additions and 6 deletions

View File

@ -118,8 +118,6 @@ Variant EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() const {
return Variant(); // Needed because of how the callback is used. return Variant(); // Needed because of how the callback is used.
} }
EditorResourcePreview *EditorResourcePreview::singleton = nullptr;
bool EditorResourcePreview::is_threaded() const { bool EditorResourcePreview::is_threaded() const {
return RSG::texture_storage->can_create_resources_async(); return RSG::texture_storage->can_create_resources_async();
} }
@ -291,13 +289,17 @@ void EditorResourcePreview::_iterate() {
bool has_small_texture; bool has_small_texture;
uint64_t last_modtime; uint64_t last_modtime;
String hash; 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; bool cache_valid = true;
if (tsize != thumbnail_size) { if (tsize != thumbnail_size) {
cache_valid = false; cache_valid = false;
f.unref(); f.unref();
} else if (outdated) {
cache_valid = false;
f.unref();
} else if (last_modtime != modtime) { } else if (last_modtime != modtime) {
String last_md5 = f->get_line(); String last_md5 = f->get_line();
String md5 = FileAccess::get_md5(item.path); String md5 = FileAccess::get_md5(item.path);
@ -357,14 +359,16 @@ void EditorResourcePreview::_write_preview_cache(Ref<FileAccess> p_file, int p_t
p_file->store_line(itos(p_modified_time)); p_file->store_line(itos(p_modified_time));
p_file->store_line(p_hash); p_file->store_line(p_hash);
p_file->store_line(VariantUtilityFunctions::var_to_str(p_metadata).replace("\n", " ")); 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<FileAccess> 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<FileAccess> 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_thumbnail_size = p_file->get_line().to_int();
*r_has_small_texture = 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_modified_time = p_file->get_line().to_int();
*r_hash = p_file->get_line(); *r_hash = p_file->get_line();
*r_metadata = VariantUtilityFunctions::str_to_var(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() { void EditorResourcePreview::_thread() {

View File

@ -76,7 +76,8 @@ public:
class EditorResourcePreview : public Node { class EditorResourcePreview : public Node {
GDCLASS(EditorResourcePreview, 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 { struct QueueItem {
Ref<Resource> resource; Ref<Resource> resource;
@ -118,7 +119,7 @@ class EditorResourcePreview : public Node {
void _iterate(); void _iterate();
void _write_preview_cache(Ref<FileAccess> 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 _write_preview_cache(Ref<FileAccess> 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<FileAccess> 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<FileAccess> 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<Ref<EditorResourcePreviewGenerator>> preview_generators; Vector<Ref<EditorResourcePreviewGenerator>> preview_generators;