Skip to content

Commit

Permalink
Add a way to invalidate preview cache
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Apr 26, 2024
1 parent 6118592 commit e62ca29
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 8 additions & 4 deletions editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions editor/editor_resource_preview.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class EditorResourcePreviewGenerator : public RefCounted {
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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit e62ca29

Please sign in to comment.