Skip to content

Commit 0dabcd9

Browse files
committed
Resource UID fixes and improvements
1 parent 76fa7b2 commit 0dabcd9

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

core/io/image.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,23 +2611,25 @@ Image::AlphaMode Image::detect_alpha() const {
26112611
}
26122612

26132613
Error Image::load(const String &p_path) {
2614+
String path = ResourceUID::ensure_path(p_path);
26142615
#ifdef DEBUG_ENABLED
2615-
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
2616-
WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", p_path));
2616+
if (path.begins_with("res://") && ResourceLoader::exists(path)) {
2617+
WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", path));
26172618
}
26182619
#endif
2619-
return ImageLoader::load_image(p_path, this);
2620+
return ImageLoader::load_image(ResourceUID::ensure_path(p_path), this);
26202621
}
26212622

26222623
Ref<Image> Image::load_from_file(const String &p_path) {
2624+
String path = ResourceUID::ensure_path(p_path);
26232625
#ifdef DEBUG_ENABLED
2624-
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
2625-
WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", p_path));
2626+
if (path.begins_with("res://") && ResourceLoader::exists(path)) {
2627+
WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", path));
26262628
}
26272629
#endif
26282630
Ref<Image> image;
26292631
image.instantiate();
2630-
Error err = ImageLoader::load_image(p_path, image);
2632+
Error err = ImageLoader::load_image(path, image);
26312633
if (err != OK) {
26322634
ERR_FAIL_V_MSG(Ref<Image>(), vformat("Failed to load image. Error %d", err));
26332635
}

core/io/resource_uid.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "core/crypto/crypto_core.h"
3535
#include "core/io/dir_access.h"
3636
#include "core/io/file_access.h"
37+
#include "core/io/resource_loader.h"
3738

3839
// These constants are off by 1, causing the 'z' and '9' characters never to be used.
3940
// This cannot be fixed without breaking compatibility; see GH-83843.
@@ -139,6 +140,21 @@ void ResourceUID::remove_id(ID p_id) {
139140
unique_ids.erase(p_id);
140141
}
141142

143+
String ResourceUID::uid_to_path(const String &p_uid) {
144+
return singleton->get_id_path(singleton->text_to_id(p_uid));
145+
}
146+
147+
String ResourceUID::path_to_uid(const String &p_path) {
148+
return singleton->id_to_text(ResourceLoader::get_resource_uid(p_path));
149+
}
150+
151+
String ResourceUID::ensure_path(const String &p_uid_or_path) {
152+
if (p_uid_or_path.begins_with("uid://")) {
153+
return uid_to_path(p_uid_or_path);
154+
}
155+
return p_uid_or_path;
156+
}
157+
142158
Error ResourceUID::save_to_cache() {
143159
String cache_file = get_cache_file();
144160
if (!FileAccess::exists(cache_file)) {

core/io/resource_uid.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class ResourceUID : public Object {
7373
String get_id_path(ID p_id) const;
7474
void remove_id(ID p_id);
7575

76+
static String uid_to_path(const String &p_uid);
77+
static String path_to_uid(const String &p_path);
78+
static String ensure_path(const String &p_uid_or_path);
79+
7680
Error load_from_cache(bool p_reset);
7781
Error save_to_cache();
7882
Error update_cache();

0 commit comments

Comments
 (0)