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.
}
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<FileAccess> 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<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_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() {

View File

@ -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> resource;
@ -118,7 +119,7 @@ class EditorResourcePreview : public Node {
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 _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;