Sfoglia il codice sorgente

Merge pull request #99226 from KoBeWi/uidification

Resource UID fixes and improvements
Thaddeus Crews 10 mesi fa
parent
commit
8726f84d5f
3 ha cambiato i file con 28 aggiunte e 6 eliminazioni
  1. 8 6
      core/io/image.cpp
  2. 16 0
      core/io/resource_uid.cpp
  3. 4 0
      core/io/resource_uid.h

+ 8 - 6
core/io/image.cpp

@@ -2611,23 +2611,25 @@ Image::AlphaMode Image::detect_alpha() const {
 }
 
 Error Image::load(const String &p_path) {
+	String path = ResourceUID::ensure_path(p_path);
 #ifdef DEBUG_ENABLED
-	if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
-		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));
+	if (path.begins_with("res://") && ResourceLoader::exists(path)) {
+		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));
 	}
 #endif
-	return ImageLoader::load_image(p_path, this);
+	return ImageLoader::load_image(ResourceUID::ensure_path(p_path), this);
 }
 
 Ref<Image> Image::load_from_file(const String &p_path) {
+	String path = ResourceUID::ensure_path(p_path);
 #ifdef DEBUG_ENABLED
-	if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
-		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));
+	if (path.begins_with("res://") && ResourceLoader::exists(path)) {
+		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));
 	}
 #endif
 	Ref<Image> image;
 	image.instantiate();
-	Error err = ImageLoader::load_image(p_path, image);
+	Error err = ImageLoader::load_image(path, image);
 	if (err != OK) {
 		ERR_FAIL_V_MSG(Ref<Image>(), vformat("Failed to load image. Error %d", err));
 	}

+ 16 - 0
core/io/resource_uid.cpp

@@ -34,6 +34,7 @@
 #include "core/crypto/crypto_core.h"
 #include "core/io/dir_access.h"
 #include "core/io/file_access.h"
+#include "core/io/resource_loader.h"
 
 // These constants are off by 1, causing the 'z' and '9' characters never to be used.
 // This cannot be fixed without breaking compatibility; see GH-83843.
@@ -139,6 +140,21 @@ void ResourceUID::remove_id(ID p_id) {
 	unique_ids.erase(p_id);
 }
 
+String ResourceUID::uid_to_path(const String &p_uid) {
+	return singleton->get_id_path(singleton->text_to_id(p_uid));
+}
+
+String ResourceUID::path_to_uid(const String &p_path) {
+	return singleton->id_to_text(ResourceLoader::get_resource_uid(p_path));
+}
+
+String ResourceUID::ensure_path(const String &p_uid_or_path) {
+	if (p_uid_or_path.begins_with("uid://")) {
+		return uid_to_path(p_uid_or_path);
+	}
+	return p_uid_or_path;
+}
+
 Error ResourceUID::save_to_cache() {
 	String cache_file = get_cache_file();
 	if (!FileAccess::exists(cache_file)) {

+ 4 - 0
core/io/resource_uid.h

@@ -73,6 +73,10 @@ public:
 	String get_id_path(ID p_id) const;
 	void remove_id(ID p_id);
 
+	static String uid_to_path(const String &p_uid);
+	static String path_to_uid(const String &p_path);
+	static String ensure_path(const String &p_uid_or_path);
+
 	Error load_from_cache(bool p_reset);
 	Error save_to_cache();
 	Error update_cache();