diff --git a/core/crypto/crypto.cpp b/core/crypto/crypto.cpp index 1824580110..edbfae32d5 100644 --- a/core/crypto/crypto.cpp +++ b/core/crypto/crypto.cpp @@ -179,82 +179,3 @@ void Crypto::_bind_methods() { ClassDB::bind_method(D_METHOD("hmac_digest", "hash_type", "key", "msg"), &Crypto::hmac_digest); ClassDB::bind_method(D_METHOD("constant_time_compare", "trusted", "received"), &Crypto::constant_time_compare); } - -/// Resource loader/saver - -Ref ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - String el = p_path.get_extension().to_lower(); - if (el == "crt") { - X509Certificate *cert = X509Certificate::create(); - if (cert) { - cert->load(p_path); - } - return cert; - } else if (el == "key") { - CryptoKey *key = CryptoKey::create(); - if (key) { - key->load(p_path, false); - } - return key; - } else if (el == "pub") { - CryptoKey *key = CryptoKey::create(); - if (key) { - key->load(p_path, true); - } - return key; - } - return nullptr; -} - -void ResourceFormatLoaderCrypto::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("crt"); - p_extensions->push_back("key"); - p_extensions->push_back("pub"); -} - -bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const { - return p_type == "X509Certificate" || p_type == "CryptoKey"; -} - -String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const { - String el = p_path.get_extension().to_lower(); - if (el == "crt") { - return "X509Certificate"; - } else if (el == "key" || el == "pub") { - return "CryptoKey"; - } - return ""; -} - -Error ResourceFormatSaverCrypto::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Error err; - Ref cert = p_resource; - Ref key = p_resource; - if (cert.is_valid()) { - err = cert->save(p_path); - } else if (key.is_valid()) { - err = key->save(p_path, p_path.has_extension("pub")); - } else { - ERR_FAIL_V(ERR_INVALID_PARAMETER); - } - ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save Crypto resource to file '%s'.", p_path)); - return OK; -} - -void ResourceFormatSaverCrypto::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - const X509Certificate *cert = Object::cast_to(*p_resource); - const CryptoKey *key = Object::cast_to(*p_resource); - if (cert) { - p_extensions->push_back("crt"); - } - if (key) { - if (!key->is_public_only()) { - p_extensions->push_back("key"); - } - p_extensions->push_back("pub"); - } -} - -bool ResourceFormatSaverCrypto::recognize(const Ref &p_resource) const { - return Object::cast_to(*p_resource) || Object::cast_to(*p_resource); -} diff --git a/core/crypto/crypto.h b/core/crypto/crypto.h index 48bf589abe..5339321965 100644 --- a/core/crypto/crypto.h +++ b/core/crypto/crypto.h @@ -32,8 +32,6 @@ #include "core/crypto/hashing_context.h" #include "core/io/resource.h" -#include "core/io/resource_loader.h" -#include "core/io/resource_saver.h" #include "core/object/ref_counted.h" class CryptoKey : public Resource { @@ -144,26 +142,3 @@ public: // @see: https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy bool constant_time_compare(const PackedByteArray &p_trusted, const PackedByteArray &p_received); }; - -class ResourceFormatLoaderCrypto : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; - - // Treat certificates as text files, do not generate a `*.{crt,key,pub}.uid` file. - virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; } - virtual bool has_custom_uid_support() const override { return true; } -}; - -class ResourceFormatSaverCrypto : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverCrypto, ResourceFormatSaver); - -public: - virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - virtual bool recognize(const Ref &p_resource) const override; -}; diff --git a/core/crypto/crypto_resource_format.cpp b/core/crypto/crypto_resource_format.cpp new file mode 100644 index 0000000000..09fc2bed0c --- /dev/null +++ b/core/crypto/crypto_resource_format.cpp @@ -0,0 +1,110 @@ +/**************************************************************************/ +/* crypto_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "crypto_resource_format.h" + +#include "core/crypto/crypto.h" + +Ref ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + String el = p_path.get_extension().to_lower(); + if (el == "crt") { + X509Certificate *cert = X509Certificate::create(); + if (cert) { + cert->load(p_path); + } + return cert; + } else if (el == "key") { + CryptoKey *key = CryptoKey::create(); + if (key) { + key->load(p_path, false); + } + return key; + } else if (el == "pub") { + CryptoKey *key = CryptoKey::create(); + if (key) { + key->load(p_path, true); + } + return key; + } + return nullptr; +} + +void ResourceFormatLoaderCrypto::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("crt"); + p_extensions->push_back("key"); + p_extensions->push_back("pub"); +} + +bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const { + return p_type == "X509Certificate" || p_type == "CryptoKey"; +} + +String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const { + String el = p_path.get_extension().to_lower(); + if (el == "crt") { + return "X509Certificate"; + } else if (el == "key" || el == "pub") { + return "CryptoKey"; + } + return ""; +} + +Error ResourceFormatSaverCrypto::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Error err; + Ref cert = p_resource; + Ref key = p_resource; + if (cert.is_valid()) { + err = cert->save(p_path); + } else if (key.is_valid()) { + err = key->save(p_path, p_path.has_extension("pub")); + } else { + ERR_FAIL_V(ERR_INVALID_PARAMETER); + } + ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save Crypto resource to file '%s'.", p_path)); + return OK; +} + +void ResourceFormatSaverCrypto::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + const X509Certificate *cert = Object::cast_to(*p_resource); + const CryptoKey *key = Object::cast_to(*p_resource); + if (cert) { + p_extensions->push_back("crt"); + } + if (key) { + if (!key->is_public_only()) { + p_extensions->push_back("key"); + } + p_extensions->push_back("pub"); + } +} + +bool ResourceFormatSaverCrypto::recognize(const Ref &p_resource) const { + return Object::cast_to(*p_resource) || Object::cast_to(*p_resource); +} diff --git a/core/crypto/crypto_resource_format.h b/core/crypto/crypto_resource_format.h new file mode 100644 index 0000000000..128e975124 --- /dev/null +++ b/core/crypto/crypto_resource_format.h @@ -0,0 +1,58 @@ +/**************************************************************************/ +/* crypto_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" +#include "core/io/resource_uid.h" + +class ResourceFormatLoaderCrypto : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; + + // Treat certificates as text files, do not generate a `*.{crt,key,pub}.uid` file. + virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; } + virtual bool has_custom_uid_support() const override { return true; } +}; + +class ResourceFormatSaverCrypto : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverCrypto, ResourceFormatSaver); + +public: + virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + virtual bool recognize(const Ref &p_resource) const override; +}; diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp index cc25ff8f1a..7b991b9993 100644 --- a/core/extension/gdextension.cpp +++ b/core/extension/gdextension.cpp @@ -33,7 +33,6 @@ #include "core/config/project_settings.h" #include "core/extension/gdextension_library_loader.h" -#include "core/extension/gdextension_manager.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/object/method_bind.h" @@ -845,68 +844,7 @@ void GDExtension::finalize_gdextensions() { gdextension_interface_functions.clear(); } -Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref &p_extension) { - ERR_FAIL_COND_V_MSG(p_extension.is_valid() && p_extension->is_library_open(), ERR_ALREADY_IN_USE, "Cannot load GDExtension resource into already opened library."); - - GDExtensionManager *extension_manager = GDExtensionManager::get_singleton(); - - GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path); - if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) { - // Errors already logged in load_extension(). - return FAILED; - } - - p_extension = extension_manager->get_extension(p_path); - return OK; -} - -Ref GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - // We can't have two GDExtension resource object representing the same library, because - // loading (or unloading) a GDExtension affects global data. So, we need reuse the same - // object if one has already been loaded (even if caching is disabled at the resource - // loader level). - GDExtensionManager *manager = GDExtensionManager::get_singleton(); - if (manager->is_extension_loaded(p_path)) { - return manager->get_extension(p_path); - } - - Ref lib; - Error err = load_gdextension_resource(p_path, lib); - if (err != OK && r_error) { - // Errors already logged in load_gdextension_resource(). - *r_error = err; - } - return lib; -} - -void GDExtensionResourceLoader::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("gdextension"); -} - -bool GDExtensionResourceLoader::handles_type(const String &p_type) const { - return p_type == "GDExtension"; -} - -String GDExtensionResourceLoader::get_resource_type(const String &p_path) const { - if (p_path.has_extension("gdextension")) { - return "GDExtension"; - } - return ""; -} - #ifdef TOOLS_ENABLED -void GDExtensionResourceLoader::get_classes_used(const String &p_path, HashSet *r_classes) { - Ref gdext = ResourceLoader::load(p_path); - if (gdext.is_null()) { - return; - } - - for (const StringName class_name : gdext->get_classes_used()) { - if (ClassDB::class_exists(class_name)) { - r_classes->insert(class_name); - } - } -} bool GDExtension::has_library_changed() const { return loader->has_library_changed(); diff --git a/core/extension/gdextension.h b/core/extension/gdextension.h index 8bf3ebe6c3..78b30ac999 100644 --- a/core/extension/gdextension.h +++ b/core/extension/gdextension.h @@ -32,8 +32,7 @@ #include "core/extension/gdextension_interface.gen.h" #include "core/extension/gdextension_loader.h" -#include "core/io/resource_loader.h" -#include "core/object/ref_counted.h" +#include "core/io/resource.h" class GDExtensionMethodBind; @@ -181,21 +180,6 @@ public: VARIANT_ENUM_CAST(GDExtension::InitializationLevel) -class GDExtensionResourceLoader : public ResourceFormatLoader { - GDSOFTCLASS(GDExtensionResourceLoader, ResourceFormatLoader); - -public: - static Error load_gdextension_resource(const String &p_path, Ref &p_extension); - - virtual Ref load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -#ifdef TOOLS_ENABLED - virtual void get_classes_used(const String &p_path, HashSet *r_classes) override; -#endif // TOOLS_ENABLED -}; - #ifdef TOOLS_ENABLED class GDExtensionEditorPlugins { private: diff --git a/core/extension/gdextension_resource_format.cpp b/core/extension/gdextension_resource_format.cpp new file mode 100644 index 0000000000..f8b71be17f --- /dev/null +++ b/core/extension/gdextension_resource_format.cpp @@ -0,0 +1,98 @@ +/**************************************************************************/ +/* gdextension_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "gdextension_resource_format.h" + +#include "core/extension/gdextension_manager.h" +#include "core/object/class_db.h" + +Ref GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + // We can't have two GDExtension resource object representing the same library, because + // loading (or unloading) a GDExtension affects global data. So, we need reuse the same + // object if one has already been loaded (even if caching is disabled at the resource + // loader level). + GDExtensionManager *manager = GDExtensionManager::get_singleton(); + if (manager->is_extension_loaded(p_path)) { + return manager->get_extension(p_path); + } + + Ref lib; + Error err = load_gdextension_resource(p_path, lib); + if (err != OK && r_error) { + // Errors already logged in load_gdextension_resource(). + *r_error = err; + } + return lib; +} + +void GDExtensionResourceLoader::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("gdextension"); +} + +bool GDExtensionResourceLoader::handles_type(const String &p_type) const { + return p_type == "GDExtension"; +} + +String GDExtensionResourceLoader::get_resource_type(const String &p_path) const { + if (p_path.has_extension("gdextension")) { + return "GDExtension"; + } + return ""; +} + +Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref &p_extension) { + ERR_FAIL_COND_V_MSG(p_extension.is_valid() && p_extension->is_library_open(), ERR_ALREADY_IN_USE, "Cannot load GDExtension resource into already opened library."); + + GDExtensionManager *extension_manager = GDExtensionManager::get_singleton(); + + GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path); + if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) { + // Errors already logged in load_extension(). + return FAILED; + } + + p_extension = extension_manager->get_extension(p_path); + return OK; +} + +#ifdef TOOLS_ENABLED +void GDExtensionResourceLoader::get_classes_used(const String &p_path, HashSet *r_classes) { + Ref gdext = ResourceLoader::load(p_path); + if (gdext.is_null()) { + return; + } + + for (const StringName class_name : gdext->get_classes_used()) { + if (ClassDB::class_exists(class_name)) { + r_classes->insert(class_name); + } + } +} +#endif // TOOLS_ENABLED diff --git a/core/extension/gdextension_resource_format.h b/core/extension/gdextension_resource_format.h new file mode 100644 index 0000000000..1bd867fd09 --- /dev/null +++ b/core/extension/gdextension_resource_format.h @@ -0,0 +1,49 @@ +/**************************************************************************/ +/* gdextension_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/extension/gdextension.h" +#include "core/io/resource_loader.h" + +class GDExtensionResourceLoader : public ResourceFormatLoader { + GDSOFTCLASS(GDExtensionResourceLoader, ResourceFormatLoader); + +public: + static Error load_gdextension_resource(const String &p_path, Ref &p_extension); + + virtual Ref load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +#ifdef TOOLS_ENABLED + virtual void get_classes_used(const String &p_path, HashSet *r_classes) override; +#endif // TOOLS_ENABLED +}; diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index c8e909e281..83298371f3 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -139,74 +139,3 @@ void ImageLoader::cleanup() { remove_image_format_loader(loader[0]); } } - -///////////////// - -Ref ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - Ref f = FileAccess::open(p_path, FileAccess::READ); - if (f.is_null()) { - if (r_error) { - *r_error = ERR_CANT_OPEN; - } - return Ref(); - } - - uint8_t header[4] = { 0, 0, 0, 0 }; - f->get_buffer(header, 4); - - bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M'; - if (unrecognized) { - if (r_error) { - *r_error = ERR_FILE_UNRECOGNIZED; - } - ERR_FAIL_V(Ref()); - } - - String extension = f->get_pascal_string(); - - int idx = -1; - - for (int i = 0; i < ImageLoader::loader.size(); i++) { - if (ImageLoader::loader[i]->recognize(extension)) { - idx = i; - break; - } - } - - if (idx == -1) { - if (r_error) { - *r_error = ERR_FILE_UNRECOGNIZED; - } - ERR_FAIL_V(Ref()); - } - - Ref image; - image.instantiate(); - - Error err = ImageLoader::loader.write[idx]->load_image(image, f); - - if (err != OK) { - if (r_error) { - *r_error = err; - } - return Ref(); - } - - if (r_error) { - *r_error = OK; - } - - return image; -} - -void ResourceFormatLoaderImage::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("image"); -} - -bool ResourceFormatLoaderImage::handles_type(const String &p_type) const { - return p_type == "Image"; -} - -String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const { - return p_path.get_extension().to_lower() == "image" ? "Image" : String(); -} diff --git a/core/io/image_loader.h b/core/io/image_loader.h index e8fbbc5110..12441cca7a 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -32,7 +32,6 @@ #include "core/io/file_access.h" #include "core/io/image.h" -#include "core/io/resource_loader.h" #include "core/object/gdvirtual.gen.h" #include "core/string/ustring.h" #include "core/templates/list.h" @@ -98,13 +97,3 @@ public: static void cleanup(); }; - -class ResourceFormatLoaderImage : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderImage, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; diff --git a/core/io/image_resource_format.cpp b/core/io/image_resource_format.cpp new file mode 100644 index 0000000000..244350f619 --- /dev/null +++ b/core/io/image_resource_format.cpp @@ -0,0 +1,100 @@ +/**************************************************************************/ +/* image_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "image_resource_format.h" + +Ref ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + Ref f = FileAccess::open(p_path, FileAccess::READ); + if (f.is_null()) { + if (r_error) { + *r_error = ERR_CANT_OPEN; + } + return Ref(); + } + + uint8_t header[4] = { 0, 0, 0, 0 }; + f->get_buffer(header, 4); + + bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M'; + if (unrecognized) { + if (r_error) { + *r_error = ERR_FILE_UNRECOGNIZED; + } + ERR_FAIL_V(Ref()); + } + + String extension = f->get_pascal_string(); + + int idx = -1; + + for (int i = 0; i < ImageLoader::loader.size(); i++) { + if (ImageLoader::loader[i]->recognize(extension)) { + idx = i; + break; + } + } + + if (idx == -1) { + if (r_error) { + *r_error = ERR_FILE_UNRECOGNIZED; + } + ERR_FAIL_V(Ref()); + } + + Ref image; + image.instantiate(); + + Error err = ImageLoader::loader.write[idx]->load_image(image, f); + + if (err != OK) { + if (r_error) { + *r_error = err; + } + return Ref(); + } + + if (r_error) { + *r_error = OK; + } + + return image; +} + +void ResourceFormatLoaderImage::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("image"); +} + +bool ResourceFormatLoaderImage::handles_type(const String &p_type) const { + return p_type == "Image"; +} + +String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const { + return p_path.get_extension().to_lower() == "image" ? "Image" : String(); +} diff --git a/core/io/image_resource_format.h b/core/io/image_resource_format.h new file mode 100644 index 0000000000..7d76de0a5c --- /dev/null +++ b/core/io/image_resource_format.h @@ -0,0 +1,44 @@ +/**************************************************************************/ +/* image_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/image_loader.h" +#include "core/io/resource_loader.h" + +class ResourceFormatLoaderImage : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderImage, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; diff --git a/core/io/json.cpp b/core/io/json.cpp index dd91816155..e055346c43 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -30,8 +30,7 @@ #include "json.h" -#include "core/config/engine.h" -#include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "core/variant/container_type_validate.h" @@ -1551,88 +1550,3 @@ Variant JSON::_to_native(const Variant &p_json, bool p_allow_objects, int p_dept #undef VALUE_TYPE #undef ARGS #undef PROPS - -//////////// - -Ref ResourceFormatLoaderJSON::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - if (r_error) { - *r_error = ERR_FILE_CANT_OPEN; - } - - if (!FileAccess::exists(p_path)) { - if (r_error) { - *r_error = ERR_FILE_NOT_FOUND; - } - return Ref(); - } - - Ref json; - json.instantiate(); - - Error err = json->parse(FileAccess::get_file_as_string(p_path), Engine::get_singleton()->is_editor_hint()); - if (err != OK) { - String err_text = "Error parsing JSON file at '" + p_path + "', on line " + itos(json->get_error_line()) + ": " + json->get_error_message(); - - if (Engine::get_singleton()->is_editor_hint()) { - // If running on editor, still allow opening the JSON so the code editor can edit it. - WARN_PRINT(err_text); - } else { - if (r_error) { - *r_error = err; - } - ERR_PRINT(err_text); - return Ref(); - } - } - - if (r_error) { - *r_error = OK; - } - - return json; -} - -void ResourceFormatLoaderJSON::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("json"); -} - -bool ResourceFormatLoaderJSON::handles_type(const String &p_type) const { - return (p_type == "JSON"); -} - -String ResourceFormatLoaderJSON::get_resource_type(const String &p_path) const { - if (p_path.has_extension("json")) { - return "JSON"; - } - return ""; -} - -Error ResourceFormatSaverJSON::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Ref json = p_resource; - ERR_FAIL_COND_V(json.is_null(), ERR_INVALID_PARAMETER); - - String source = json->get_parsed_text().is_empty() ? JSON::stringify(json->get_data(), "\t", false, true) : json->get_parsed_text(); - - Error err; - Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); - - ERR_FAIL_COND_V_MSG(err, err, vformat("Cannot save json '%s'.", p_path)); - - file->store_string(source); - if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { - return ERR_CANT_CREATE; - } - - return OK; -} - -void ResourceFormatSaverJSON::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - Ref json = p_resource; - if (json.is_valid()) { - p_extensions->push_back("json"); - } -} - -bool ResourceFormatSaverJSON::recognize(const Ref &p_resource) const { - return p_resource->get_class_name() == "JSON"; //only json, not inherited -} diff --git a/core/io/json.h b/core/io/json.h index 34a27f3061..9346a75bd3 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -31,8 +31,6 @@ #pragma once #include "core/io/resource.h" -#include "core/io/resource_loader.h" -#include "core/io/resource_saver.h" #include "core/variant/variant.h" class JSON : public Resource { @@ -105,26 +103,3 @@ public: _FORCE_INLINE_ int get_error_line() const { return err_line; } _FORCE_INLINE_ String get_error_message() const { return err_str; } }; - -class ResourceFormatLoaderJSON : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderJSON, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; - - // Treat JSON as a text file, do not generate a `*.json.uid` file. - virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; } - virtual bool has_custom_uid_support() const override { return true; } -}; - -class ResourceFormatSaverJSON : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverJSON, ResourceFormatSaver); - -public: - virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - virtual bool recognize(const Ref &p_resource) const override; -}; diff --git a/core/io/json_resource_format.cpp b/core/io/json_resource_format.cpp new file mode 100644 index 0000000000..7955fbf971 --- /dev/null +++ b/core/io/json_resource_format.cpp @@ -0,0 +1,118 @@ +/**************************************************************************/ +/* json_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "json_resource_format.h" + +#include "core/config/engine.h" +#include "core/io/file_access.h" +#include "core/io/json.h" + +Ref ResourceFormatLoaderJSON::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + if (!FileAccess::exists(p_path)) { + if (r_error) { + *r_error = ERR_FILE_NOT_FOUND; + } + return Ref(); + } + + Ref json; + json.instantiate(); + + Error err = json->parse(FileAccess::get_file_as_string(p_path), Engine::get_singleton()->is_editor_hint()); + if (err != OK) { + String err_text = "Error parsing JSON file at '" + p_path + "', on line " + itos(json->get_error_line()) + ": " + json->get_error_message(); + + if (Engine::get_singleton()->is_editor_hint()) { + // If running on editor, still allow opening the JSON so the code editor can edit it. + WARN_PRINT(err_text); + } else { + if (r_error) { + *r_error = err; + } + ERR_PRINT(err_text); + return Ref(); + } + } + + if (r_error) { + *r_error = OK; + } + + return json; +} + +void ResourceFormatLoaderJSON::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("json"); +} + +bool ResourceFormatLoaderJSON::handles_type(const String &p_type) const { + return (p_type == "JSON"); +} + +String ResourceFormatLoaderJSON::get_resource_type(const String &p_path) const { + if (p_path.has_extension("json")) { + return "JSON"; + } + return ""; +} + +Error ResourceFormatSaverJSON::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Ref json = p_resource; + ERR_FAIL_COND_V(json.is_null(), ERR_INVALID_PARAMETER); + + String source = json->get_parsed_text().is_empty() ? JSON::stringify(json->get_data(), "\t", false, true) : json->get_parsed_text(); + + Error err; + Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); + + ERR_FAIL_COND_V_MSG(err, err, vformat("Cannot save json '%s'.", p_path)); + + file->store_string(source); + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + + return OK; +} + +void ResourceFormatSaverJSON::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + Ref json = p_resource; + if (json.is_valid()) { + p_extensions->push_back("json"); + } +} + +bool ResourceFormatSaverJSON::recognize(const Ref &p_resource) const { + return p_resource->get_class_name() == "JSON"; //only json, not inherited +} diff --git a/core/io/json_resource_format.h b/core/io/json_resource_format.h new file mode 100644 index 0000000000..3867a92b5e --- /dev/null +++ b/core/io/json_resource_format.h @@ -0,0 +1,58 @@ +/**************************************************************************/ +/* json_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" +#include "core/io/resource_uid.h" + +class ResourceFormatLoaderJSON : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderJSON, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; + + // Treat JSON as a text file, do not generate a `*.json.uid` file. + virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; } + virtual bool has_custom_uid_support() const override { return true; } +}; + +class ResourceFormatSaverJSON : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverJSON, ResourceFormatSaver); + +public: + virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + virtual bool recognize(const Ref &p_resource) const override; +}; diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index b6abee964d..a2085cdd39 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -272,7 +272,7 @@ ResourceLoader::LoadToken::~LoadToken() { clear(); } -Ref ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress) { +Ref ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress) { const String &original_path = p_original_path.is_empty() ? p_path : p_original_path; load_nesting++; @@ -388,8 +388,8 @@ void ResourceLoader::_run_load_task(void *p_userdata) { } load_task.need_wait = false; - bool ignoring = load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE || load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP; - bool replacing = load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE || load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP; + bool ignoring = load_task.cache_mode == CACHE_MODE_IGNORE || load_task.cache_mode == CACHE_MODE_IGNORE_DEEP; + bool replacing = load_task.cache_mode == CACHE_MODE_REPLACE || load_task.cache_mode == CACHE_MODE_REPLACE_DEEP; bool unlock_pending = true; if (load_task.resource.is_valid()) { // From now on, no critical section needed as no one will write to the task anymore. @@ -485,7 +485,7 @@ String ResourceLoader::_validate_local_path(const String &p_path) { } } -Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, ResourceFormatLoader::CacheMode p_cache_mode) { +Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, CacheMode p_cache_mode) { Ref token = _load_start(p_path, p_type_hint, p_use_sub_threads ? LOAD_THREAD_DISTRIBUTE : LOAD_THREAD_SPAWN_SINGLE, p_cache_mode, true); return token.is_valid() ? OK : FAILED; } @@ -510,7 +510,7 @@ void ResourceLoader::_load_threaded_request_setup_user_token(LoadToken *p_token, print_lt("REQUEST: user load tokens: " + itos(user_load_tokens.size())); } -Ref ResourceLoader::load(const String &p_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error) { +Ref ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode, Error *r_error) { if (r_error) { *r_error = OK; } @@ -535,11 +535,11 @@ Ref ResourceLoader::load(const String &p_path, const String &p_type_hi return res; } -Ref ResourceLoader::_load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, ResourceFormatLoader::CacheMode p_cache_mode, bool p_for_user) { +Ref ResourceLoader::_load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, CacheMode p_cache_mode, bool p_for_user) { String local_path = _validate_local_path(p_path); ERR_FAIL_COND_V(local_path.is_empty(), Ref()); - bool ignoring_cache = p_cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE || p_cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP; + bool ignoring_cache = p_cache_mode == CACHE_MODE_IGNORE || p_cache_mode == CACHE_MODE_IGNORE_DEEP; Ref load_token; bool must_not_register = false; @@ -585,7 +585,7 @@ Ref ResourceLoader::_load_start(const String &p_path, load_task.type_hint = p_type_hint; load_task.cache_mode = p_cache_mode; load_task.use_sub_threads = p_thread_mode == LOAD_THREAD_DISTRIBUTE; - if (p_cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE) { + if (p_cache_mode == CACHE_MODE_REUSE) { Ref existing = ResourceCache::get_ref(local_path); if (existing.is_valid()) { //referencing is fine diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index ef6ab41415..ec738adca9 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -31,6 +31,7 @@ #pragma once #include "core/io/resource.h" +#include "core/io/resource_loader_constants.h" #include "core/object/gdvirtual.gen.h" #include "core/object/worker_thread_pool.h" #include "core/os/thread.h" @@ -48,13 +49,12 @@ class ResourceFormatLoader : public RefCounted { GDCLASS(ResourceFormatLoader, RefCounted); public: - enum CacheMode { - CACHE_MODE_IGNORE, - CACHE_MODE_REUSE, - CACHE_MODE_REPLACE, - CACHE_MODE_IGNORE_DEEP, - CACHE_MODE_REPLACE_DEEP, - }; + using CacheMode = ResourceLoaderConstants::CacheMode; + static constexpr CacheMode CACHE_MODE_IGNORE = ResourceLoaderConstants::CACHE_MODE_IGNORE; + static constexpr CacheMode CACHE_MODE_REUSE = ResourceLoaderConstants::CACHE_MODE_REUSE; + static constexpr CacheMode CACHE_MODE_REPLACE = ResourceLoaderConstants::CACHE_MODE_REPLACE; + static constexpr CacheMode CACHE_MODE_IGNORE_DEEP = ResourceLoaderConstants::CACHE_MODE_IGNORE_DEEP; + static constexpr CacheMode CACHE_MODE_REPLACE_DEEP = ResourceLoaderConstants::CACHE_MODE_REPLACE_DEEP; protected: static void _bind_methods(); @@ -94,8 +94,6 @@ public: virtual ~ResourceFormatLoader() {} }; -VARIANT_ENUM_CAST(ResourceFormatLoader::CacheMode) - typedef void (*ResourceLoadErrorNotify)(const String &p_text); typedef void (*DependencyErrorNotify)(const String &p_loading, const String &p_which, const String &p_type); @@ -113,6 +111,13 @@ class ResourceLoader { struct ThreadLoadTask; public: + using CacheMode = ResourceLoaderConstants::CacheMode; + static constexpr CacheMode CACHE_MODE_IGNORE = ResourceLoaderConstants::CACHE_MODE_IGNORE; + static constexpr CacheMode CACHE_MODE_REUSE = ResourceLoaderConstants::CACHE_MODE_REUSE; + static constexpr CacheMode CACHE_MODE_REPLACE = ResourceLoaderConstants::CACHE_MODE_REPLACE; + static constexpr CacheMode CACHE_MODE_IGNORE_DEEP = ResourceLoaderConstants::CACHE_MODE_IGNORE_DEEP; + static constexpr CacheMode CACHE_MODE_REPLACE_DEEP = ResourceLoaderConstants::CACHE_MODE_REPLACE_DEEP; + enum ThreadLoadStatus { THREAD_LOAD_INVALID_RESOURCE, THREAD_LOAD_IN_PROGRESS, @@ -139,7 +144,7 @@ public: static const int BINARY_MUTEX_TAG = 1; - static Ref _load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, ResourceFormatLoader::CacheMode p_cache_mode, bool p_for_user = false); + static Ref _load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, CacheMode p_cache_mode, bool p_for_user = false); static Ref _load_complete(LoadToken &p_load_token, Error *r_error); private: @@ -167,7 +172,7 @@ private: friend class ResourceFormatImporter; - static Ref _load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress); + static Ref _load(const String &p_path, const String &p_original_path, const String &p_type_hint, CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress); static ResourceLoadedCallback _loaded_callback; @@ -185,7 +190,7 @@ private: float max_reported_progress = 0.0f; uint64_t last_progress_check_main_thread_frame = UINT64_MAX; ThreadLoadStatus status = THREAD_LOAD_IN_PROGRESS; - ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE; + CacheMode cache_mode = CACHE_MODE_REUSE; Error error = OK; Ref resource; ThreadLoadTask *parent_task = nullptr; @@ -231,7 +236,7 @@ private: static String _validate_local_path(const String &p_path); public: - static Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE); + static Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, CacheMode p_cache_mode = CACHE_MODE_REUSE); static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr); static Ref load_threaded_get(const String &p_path, Error *r_error = nullptr); @@ -241,7 +246,7 @@ public: static void resource_changed_disconnect(Resource *p_source, const Callable &p_callable); static void resource_changed_emit(Resource *p_source); - static Ref load(const String &p_path, const String &p_type_hint = "", ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, Error *r_error = nullptr); + static Ref load(const String &p_path, const String &p_type_hint = "", CacheMode p_cache_mode = CACHE_MODE_REUSE, Error *r_error = nullptr); static bool exists(const String &p_path, const String &p_type_hint = ""); static void get_recognized_extensions_for_type(const String &p_type, List *p_extensions); diff --git a/core/io/resource_loader_constants.h b/core/io/resource_loader_constants.h new file mode 100644 index 0000000000..c84a8f01ce --- /dev/null +++ b/core/io/resource_loader_constants.h @@ -0,0 +1,47 @@ +/**************************************************************************/ +/* resource_loader_constants.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/variant/type_info.h" + +namespace ResourceLoaderConstants { + +enum CacheMode { + CACHE_MODE_IGNORE, + CACHE_MODE_REUSE, + CACHE_MODE_REPLACE, + CACHE_MODE_IGNORE_DEEP, + CACHE_MODE_REPLACE_DEEP, +}; + +} // namespace ResourceLoaderConstants + +VARIANT_ENUM_CAST(ResourceLoaderConstants::CacheMode) diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index e85aadd473..80696814aa 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -35,10 +35,12 @@ #include "core/core_bind.h" #include "core/crypto/aes_context.h" #include "core/crypto/crypto.h" +#include "core/crypto/crypto_resource_format.h" #include "core/crypto/hashing_context.h" #include "core/debugger/engine_profiler.h" #include "core/extension/gdextension.h" #include "core/extension/gdextension_manager.h" +#include "core/extension/gdextension_resource_format.h" #include "core/extension/godot_instance.h" #include "core/input/input.h" #include "core/input/input_map.h" @@ -49,7 +51,9 @@ #include "core/io/file_access_encrypted.h" #include "core/io/http_client.h" #include "core/io/image_loader.h" +#include "core/io/image_resource_format.h" #include "core/io/json.h" +#include "core/io/json_resource_format.h" #include "core/io/marshalls.h" #include "core/io/missing_resource.h" #include "core/io/packet_peer.h" @@ -58,6 +62,8 @@ #include "core/io/pck_packer.h" #include "core/io/resource_format_binary.h" #include "core/io/resource_importer.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/io/resource_uid.h" #include "core/io/stream_peer_gzip.h" #include "core/io/stream_peer_tls.h" diff --git a/drivers/register_driver_types.cpp b/drivers/register_driver_types.cpp index 1ec5ed33c5..9d8a87fe5e 100644 --- a/drivers/register_driver_types.cpp +++ b/drivers/register_driver_types.cpp @@ -30,6 +30,7 @@ #include "register_driver_types.h" +#include "core/io/resource_saver.h" #include "drivers/png/image_loader_png.h" #include "drivers/png/resource_saver_png.h" diff --git a/editor/animation/animation_blend_space_1d_editor.cpp b/editor/animation/animation_blend_space_1d_editor.cpp index 4082c6c059..fe092e7e08 100644 --- a/editor/animation/animation_blend_space_1d_editor.cpp +++ b/editor/animation/animation_blend_space_1d_editor.cpp @@ -30,6 +30,7 @@ #include "animation_blend_space_1d_editor.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/editor/animation/animation_track_editor.cpp b/editor/animation/animation_track_editor.cpp index f92f7efd92..3f0103c927 100644 --- a/editor/animation/animation_track_editor.cpp +++ b/editor/animation/animation_track_editor.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/error/error_macros.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/string/translation_server.h" diff --git a/editor/animation/animation_track_editor_plugins.cpp b/editor/animation/animation_track_editor_plugins.cpp index 66762dff04..61a5d0a503 100644 --- a/editor/animation/animation_track_editor_plugins.cpp +++ b/editor/animation/animation_track_editor_plugins.cpp @@ -30,6 +30,7 @@ #include "animation_track_editor_plugins.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "editor/audio/audio_stream_preview.h" #include "editor/editor_string_names.h" diff --git a/editor/audio/editor_audio_buses.cpp b/editor/audio/editor_audio_buses.cpp index 1898f03cd5..1245e7fb88 100644 --- a/editor/audio/editor_audio_buses.cpp +++ b/editor/audio/editor_audio_buses.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp index 6570a72622..b61619dc00 100644 --- a/editor/debugger/script_editor_debugger.cpp +++ b/editor/debugger/script_editor_debugger.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/debugger/debugger_marshalls.h" #include "core/debugger/remote_debugger.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/os.h" diff --git a/editor/doc/editor_help.cpp b/editor/doc/editor_help.cpp index 3364a6a685..914365759e 100644 --- a/editor/doc/editor_help.cpp +++ b/editor/doc/editor_help.cpp @@ -36,6 +36,8 @@ #include "core/extension/gdextension.h" #include "core/input/input.h" #include "core/io/json.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/object/script_language.h" diff --git a/editor/docks/filesystem_dock.cpp b/editor/docks/filesystem_dock.cpp index 6307178a7c..dd98b04bda 100644 --- a/editor/docks/filesystem_dock.cpp +++ b/editor/docks/filesystem_dock.cpp @@ -34,7 +34,9 @@ #include "core/input/input.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" +#include "core/io/resource_importer.h" #include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/editor/docks/import_dock.cpp b/editor/docks/import_dock.cpp index cfd7b953a0..ae86cd22ac 100644 --- a/editor/docks/import_dock.cpp +++ b/editor/docks/import_dock.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/io/resource_importer.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/docks/inspector_dock.cpp b/editor/docks/inspector_dock.cpp index 55861a0b45..4d6fbd794c 100644 --- a/editor/docks/inspector_dock.cpp +++ b/editor/docks/inspector_dock.cpp @@ -30,6 +30,7 @@ #include "inspector_dock.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/debugger/editor_debugger_inspector.h" diff --git a/editor/docks/scene_tree_dock.cpp b/editor/docks/scene_tree_dock.cpp index 94789570be..3de79619c3 100644 --- a/editor/docks/scene_tree_dock.cpp +++ b/editor/docks/scene_tree_dock.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index f9fecb6905..3b63b048b3 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -37,6 +37,7 @@ #include "core/io/config_file.h" #include "core/io/file_access.h" #include "core/io/image.h" +#include "core/io/resource_importer.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" diff --git a/editor/export/editor_export_platform.cpp b/editor/export/editor_export_platform.cpp index 63dfc6a475..e841fa445c 100644 --- a/editor/export/editor_export_platform.cpp +++ b/editor/export/editor_export_platform.cpp @@ -40,6 +40,8 @@ #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION #include "core/io/image.h" #include "core/io/image_loader.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/io/resource_uid.h" #include "core/io/zip_io.h" #include "core/math/random_pcg.h" diff --git a/editor/export/editor_export_platform_apple_embedded.cpp b/editor/export/editor_export_platform_apple_embedded.cpp index 2f9db30283..a468ff7e01 100644 --- a/editor/export/editor_export_platform_apple_embedded.cpp +++ b/editor/export/editor_export_platform_apple_embedded.cpp @@ -32,6 +32,7 @@ #include "core/io/file_access.h" #include "core/io/plist.h" +#include "core/io/resource_loader.h" #include "core/io/zip_io.h" #include "core/os/os.h" #include "core/string/translation_server.h" diff --git a/editor/file_system/editor_file_system.cpp b/editor/file_system/editor_file_system.cpp index 5df769aa70..fde1c6eab8 100644 --- a/editor/file_system/editor_file_system.cpp +++ b/editor/file_system/editor_file_system.cpp @@ -34,6 +34,8 @@ #include "core/extension/gdextension_manager.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" +#include "core/io/resource_importer.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" @@ -3140,7 +3142,7 @@ Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) { Error err = OK; Ref res = ResourceCache::get_ref(p_from); if (res.is_null()) { - res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err); + res = ResourceLoader::load(p_from, "", ResourceLoaderConstants::CACHE_MODE_REUSE, &err); } else { bool edited = false; List> cached; @@ -3441,7 +3443,7 @@ Error EditorFileSystem::_resource_import(const String &p_path) { return OK; } -Ref EditorFileSystem::_load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode) { +Ref EditorFileSystem::_load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceLoaderConstants::CacheMode p_cache_mode) { ERR_FAIL_NULL_V(p_importer, Ref()); if (!FileAccess::exists(p_path)) { diff --git a/editor/file_system/editor_file_system.h b/editor/file_system/editor_file_system.h index 6201834e75..1381df83f8 100644 --- a/editor/file_system/editor_file_system.h +++ b/editor/file_system/editor_file_system.h @@ -31,14 +31,15 @@ #pragma once #include "core/io/dir_access.h" -#include "core/io/resource_importer.h" -#include "core/io/resource_loader.h" +#include "core/io/resource_loader_constants.h" +#include "core/os/semaphore.h" #include "core/os/thread.h" #include "core/os/thread_safe.h" #include "core/templates/hash_set.h" #include "core/templates/safe_refcount.h" #include "scene/main/node.h" +class ResourceFormatImporter; class FileAccess; struct EditorProgressBG; @@ -330,7 +331,7 @@ class EditorFileSystem : public Node { ScriptClassInfo _get_global_script_class(const String &p_type, const String &p_path) const; static Error _resource_import(const String &p_path); - static Ref _load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode); + static Ref _load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceLoaderConstants::CacheMode p_cache_mode); bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time) diff --git a/editor/gui/editor_quick_open_dialog.cpp b/editor/gui/editor_quick_open_dialog.cpp index 1974b05189..24e8f7915d 100644 --- a/editor/gui/editor_quick_open_dialog.cpp +++ b/editor/gui/editor_quick_open_dialog.cpp @@ -31,6 +31,7 @@ #include "editor_quick_open_dialog.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/os.h" diff --git a/editor/import/3d/editor_import_collada.cpp b/editor/import/3d/editor_import_collada.cpp index efb214f006..fb2a3004ed 100644 --- a/editor/import/3d/editor_import_collada.cpp +++ b/editor/import/3d/editor_import_collada.cpp @@ -31,6 +31,7 @@ #include "editor_import_collada.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/templates/rb_set.h" #include "editor/import/3d/collada.h" #include "scene/3d/camera_3d.h" diff --git a/editor/import/3d/post_import_plugin_skeleton_renamer.cpp b/editor/import/3d/post_import_plugin_skeleton_renamer.cpp index b4fcc9ecaf..7d658d6058 100644 --- a/editor/import/3d/post_import_plugin_skeleton_renamer.cpp +++ b/editor/import/3d/post_import_plugin_skeleton_renamer.cpp @@ -30,6 +30,7 @@ #include "post_import_plugin_skeleton_renamer.h" +#include "core/io/resource_importer.h" #include "scene/3d/bone_attachment_3d.h" #include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/skeleton_3d.h" diff --git a/editor/import/3d/post_import_plugin_skeleton_rest_fixer.cpp b/editor/import/3d/post_import_plugin_skeleton_rest_fixer.cpp index e0e916658b..0d98c9e378 100644 --- a/editor/import/3d/post_import_plugin_skeleton_rest_fixer.cpp +++ b/editor/import/3d/post_import_plugin_skeleton_rest_fixer.cpp @@ -30,6 +30,7 @@ #include "post_import_plugin_skeleton_rest_fixer.h" +#include "core/io/resource_importer.h" #include "scene/3d/bone_attachment_3d.h" #include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/retarget_modifier_3d.h" diff --git a/editor/import/3d/post_import_plugin_skeleton_track_organizer.cpp b/editor/import/3d/post_import_plugin_skeleton_track_organizer.cpp index 6cc93e7ed7..62eaaee7e1 100644 --- a/editor/import/3d/post_import_plugin_skeleton_track_organizer.cpp +++ b/editor/import/3d/post_import_plugin_skeleton_track_organizer.cpp @@ -30,6 +30,7 @@ #include "post_import_plugin_skeleton_track_organizer.h" +#include "core/io/resource_importer.h" #include "scene/3d/skeleton_3d.h" #include "scene/animation/animation_player.h" #include "scene/resources/bone_map.h" diff --git a/editor/import/3d/resource_importer_obj.cpp b/editor/import/3d/resource_importer_obj.cpp index c8f3a34dc9..7c57e8dd17 100644 --- a/editor/import/3d/resource_importer_obj.cpp +++ b/editor/import/3d/resource_importer_obj.cpp @@ -31,6 +31,7 @@ #include "resource_importer_obj.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/node_3d.h" diff --git a/editor/import/3d/resource_importer_scene.cpp b/editor/import/3d/resource_importer_scene.cpp index 370a61b96b..cddb841871 100644 --- a/editor/import/3d/resource_importer_scene.cpp +++ b/editor/import/3d/resource_importer_scene.cpp @@ -32,6 +32,7 @@ #include "core/error/error_macros.h" #include "core/io/dir_access.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/class_db.h" #include "core/object/script_language.h" diff --git a/editor/import/3d/scene_import_settings.cpp b/editor/import/3d/scene_import_settings.cpp index 53bfb6115f..94c0d7c573 100644 --- a/editor/import/3d/scene_import_settings.cpp +++ b/editor/import/3d/scene_import_settings.cpp @@ -31,6 +31,8 @@ #include "scene_import_settings.h" #include "core/config/project_settings.h" +#include "core/io/resource_importer.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" diff --git a/editor/import/dynamic_font_import_settings.cpp b/editor/import/dynamic_font_import_settings.cpp index 63ed09b5a8..daea3f004b 100644 --- a/editor/import/dynamic_font_import_settings.cpp +++ b/editor/import/dynamic_font_import_settings.cpp @@ -31,6 +31,7 @@ #include "dynamic_font_import_settings.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/os/os.h" #include "core/string/translation.h" diff --git a/editor/import/resource_importer_dynamic_font.cpp b/editor/import/resource_importer_dynamic_font.cpp index 52fe1e781c..b0312152ec 100644 --- a/editor/import/resource_importer_dynamic_font.cpp +++ b/editor/import/resource_importer_dynamic_font.cpp @@ -31,6 +31,7 @@ #include "resource_importer_dynamic_font.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "editor/import/dynamic_font_import_settings.h" #include "scene/resources/font.h" diff --git a/editor/import/resource_importer_svg.cpp b/editor/import/resource_importer_svg.cpp index 9337694f3c..d638919db3 100644 --- a/editor/import/resource_importer_svg.cpp +++ b/editor/import/resource_importer_svg.cpp @@ -31,6 +31,7 @@ #include "resource_importer_svg.h" #include "core/io/file_access.h" +#include "core/io/resource_saver.h" #include "scene/resources/dpi_texture.h" String ResourceImporterSVG::get_importer_name() const { diff --git a/editor/inspector/editor_inspector.cpp b/editor/inspector/editor_inspector.cpp index 0c75ce7807..c1532f5c68 100644 --- a/editor/inspector/editor_inspector.cpp +++ b/editor/inspector/editor_inspector.cpp @@ -32,6 +32,7 @@ #include "editor_inspector.compat.inc" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/editor/inspector/editor_properties.cpp b/editor/inspector/editor_properties.cpp index 06fb0bea49..2e3fc0ae5d 100644 --- a/editor/inspector/editor_properties.cpp +++ b/editor/inspector/editor_properties.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/input/input_map.h" #include "core/io/marshalls.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/string/translation_server.h" diff --git a/editor/inspector/editor_properties_array_dict.cpp b/editor/inspector/editor_properties_array_dict.cpp index f877938fdf..88f3286b31 100644 --- a/editor/inspector/editor_properties_array_dict.cpp +++ b/editor/inspector/editor_properties_array_dict.cpp @@ -32,6 +32,7 @@ #include "core/input/input.h" #include "core/io/marshalls.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/inspector_dock.h" diff --git a/editor/inspector/editor_resource_picker.cpp b/editor/inspector/editor_resource_picker.cpp index cbcf880956..1f92e30fc7 100644 --- a/editor/inspector/editor_resource_picker.cpp +++ b/editor/inspector/editor_resource_picker.cpp @@ -31,6 +31,7 @@ #include "editor_resource_picker.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/os.h" diff --git a/editor/inspector/editor_resource_tooltip_plugins.cpp b/editor/inspector/editor_resource_tooltip_plugins.cpp index cb16173d22..e92b9e4ac2 100644 --- a/editor/inspector/editor_resource_tooltip_plugins.cpp +++ b/editor/inspector/editor_resource_tooltip_plugins.cpp @@ -30,6 +30,7 @@ #include "editor_resource_tooltip_plugins.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/plugins/editor_plugin.cpp b/editor/plugins/editor_plugin.cpp index 70b30b35e2..be0bb7e8b1 100644 --- a/editor/plugins/editor_plugin.cpp +++ b/editor/plugins/editor_plugin.cpp @@ -31,6 +31,7 @@ #include "editor_plugin.h" #include "editor_plugin.compat.inc" +#include "core/io/resource_importer.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/debugger/editor_debugger_node.h" diff --git a/editor/plugins/plugin_config_dialog.cpp b/editor/plugins/plugin_config_dialog.cpp index b6d341af3f..1cc605beed 100644 --- a/editor/plugins/plugin_config_dialog.cpp +++ b/editor/plugins/plugin_config_dialog.cpp @@ -32,6 +32,7 @@ #include "core/io/config_file.h" #include "core/io/dir_access.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" // IWYU pragma: keep. `ADD_SIGNAL` macro. #include "core/object/script_language.h" diff --git a/editor/project_upgrade/project_upgrade_tool.cpp b/editor/project_upgrade/project_upgrade_tool.cpp index 76792e0fa7..a74ed9a7bd 100644 --- a/editor/project_upgrade/project_upgrade_tool.cpp +++ b/editor/project_upgrade/project_upgrade_tool.cpp @@ -32,6 +32,8 @@ #include "core/config/project_settings.h" #include "core/io/dir_access.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" // IWYU pragma: keep. `ADD_SIGNAL` macro. #include "editor/editor_node.h" diff --git a/editor/register_editor_types.cpp b/editor/register_editor_types.cpp index ddd9e49512..8a3c8e7298 100644 --- a/editor/register_editor_types.cpp +++ b/editor/register_editor_types.cpp @@ -31,6 +31,8 @@ #include "register_editor_types.h" #include "core/config/engine.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "core/os/os.h" diff --git a/editor/scene/2d/scene_paint_2d_editor_plugin.cpp b/editor/scene/2d/scene_paint_2d_editor_plugin.cpp index 78b526dda9..5fcd513b9b 100644 --- a/editor/scene/2d/scene_paint_2d_editor_plugin.cpp +++ b/editor/scene/2d/scene_paint_2d_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "scene_paint_2d_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/filesystem_dock.h" diff --git a/editor/scene/2d/tiles/atlas_merging_dialog.cpp b/editor/scene/2d/tiles/atlas_merging_dialog.cpp index 786fb17ce9..a0e7fb84be 100644 --- a/editor/scene/2d/tiles/atlas_merging_dialog.cpp +++ b/editor/scene/2d/tiles/atlas_merging_dialog.cpp @@ -30,6 +30,7 @@ #include "atlas_merging_dialog.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_undo_redo_manager.h" diff --git a/editor/scene/2d/tiles/tile_set_editor.cpp b/editor/scene/2d/tiles/tile_set_editor.cpp index 0d6a488fca..e2f8613c38 100644 --- a/editor/scene/2d/tiles/tile_set_editor.cpp +++ b/editor/scene/2d/tiles/tile_set_editor.cpp @@ -30,6 +30,7 @@ #include "tile_set_editor.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/scene/2d/tiles/tile_set_scenes_collection_source_editor.cpp b/editor/scene/2d/tiles/tile_set_scenes_collection_source_editor.cpp index 529935b3ab..0076a366fc 100644 --- a/editor/scene/2d/tiles/tile_set_scenes_collection_source_editor.cpp +++ b/editor/scene/2d/tiles/tile_set_scenes_collection_source_editor.cpp @@ -30,6 +30,7 @@ #include "tile_set_scenes_collection_source_editor.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/scene/2d/tiles/tiles_editor_plugin.h b/editor/scene/2d/tiles/tiles_editor_plugin.h index 53ffd07839..61aa41c555 100644 --- a/editor/scene/2d/tiles/tiles_editor_plugin.h +++ b/editor/scene/2d/tiles/tiles_editor_plugin.h @@ -30,6 +30,7 @@ #pragma once +#include "core/os/semaphore.h" #include "editor/plugins/editor_plugin.h" #include "editor/scene/2d/tiles/tile_map_layer_editor.h" #include "editor/scene/2d/tiles/tile_set_editor.h" diff --git a/editor/scene/3d/mesh_instance_3d_editor_plugin.cpp b/editor/scene/3d/mesh_instance_3d_editor_plugin.cpp index e49841fb3c..ab5e649fc7 100644 --- a/editor/scene/3d/mesh_instance_3d_editor_plugin.cpp +++ b/editor/scene/3d/mesh_instance_3d_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "mesh_instance_3d_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" diff --git a/editor/scene/3d/mesh_library_editor_plugin.cpp b/editor/scene/3d/mesh_library_editor_plugin.cpp index f495f1920d..75a9426ea6 100644 --- a/editor/scene/3d/mesh_library_editor_plugin.cpp +++ b/editor/scene/3d/mesh_library_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "mesh_library_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/editor_dock_manager.h" diff --git a/editor/scene/3d/node_3d_editor_plugin.cpp b/editor/scene/3d/node_3d_editor_plugin.cpp index d64bed8506..e59c5ec6fb 100644 --- a/editor/scene/3d/node_3d_editor_plugin.cpp +++ b/editor/scene/3d/node_3d_editor_plugin.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/input/input.h" #include "core/input/input_map.h" +#include "core/io/resource_loader.h" #include "core/math/geometry_3d.h" #include "core/math/math_funcs.h" #include "core/math/projection.h" diff --git a/editor/scene/3d/skeleton_3d_editor_plugin.cpp b/editor/scene/3d/skeleton_3d_editor_plugin.cpp index bf63f6ebea..748cb6cb6a 100644 --- a/editor/scene/3d/skeleton_3d_editor_plugin.cpp +++ b/editor/scene/3d/skeleton_3d_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "skeleton_3d_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cpp index 241d8ba54d..2ea6d37b20 100644 --- a/editor/scene/canvas_item_editor_plugin.cpp +++ b/editor/scene/canvas_item_editor_plugin.cpp @@ -33,6 +33,7 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/editor/scene/group_settings_editor.cpp b/editor/scene/group_settings_editor.cpp index ff2b981abe..adaa8fd129 100644 --- a/editor/scene/group_settings_editor.cpp +++ b/editor/scene/group_settings_editor.cpp @@ -31,6 +31,8 @@ #include "group_settings_editor.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/filesystem_dock.h" diff --git a/editor/scene/gui/theme_editor_plugin.cpp b/editor/scene/gui/theme_editor_plugin.cpp index 43796f465c..72d03ea498 100644 --- a/editor/scene/gui/theme_editor_plugin.cpp +++ b/editor/scene/gui/theme_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "theme_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/doc/editor_help.h" diff --git a/editor/scene/gui/theme_editor_preview.cpp b/editor/scene/gui/theme_editor_preview.cpp index 1a1f4b2f42..67416ea98a 100644 --- a/editor/scene/gui/theme_editor_preview.cpp +++ b/editor/scene/gui/theme_editor_preview.cpp @@ -31,6 +31,7 @@ #include "theme_editor_preview.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" // IWYU pragma: keep. `ADD_SIGNAL` macro. #include "editor/editor_node.h" diff --git a/editor/scene/scene_tree_editor.cpp b/editor/scene/scene_tree_editor.cpp index 7fe1bf079b..505a78d5e3 100644 --- a/editor/scene/scene_tree_editor.cpp +++ b/editor/scene/scene_tree_editor.cpp @@ -32,6 +32,7 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/object/script_language.h" diff --git a/editor/script/script_create_dialog.cpp b/editor/script/script_create_dialog.cpp index 6449702f4c..dec715171e 100644 --- a/editor/script/script_create_dialog.cpp +++ b/editor/script/script_create_dialog.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp index dd712cab48..5d053dec2f 100644 --- a/editor/script/script_editor_plugin.cpp +++ b/editor/script/script_editor_plugin.cpp @@ -36,6 +36,7 @@ #include "core/io/file_access.h" #include "core/io/json.h" #include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/editor/script/script_text_editor.cpp b/editor/script/script_text_editor.cpp index 567d6bf32c..8091b9b043 100644 --- a/editor/script/script_text_editor.cpp +++ b/editor/script/script_text_editor.cpp @@ -34,6 +34,7 @@ #include "core/input/input.h" #include "core/io/dir_access.h" #include "core/io/json.h" +#include "core/io/resource_loader.h" #include "core/math/expression.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/settings/editor_autoload_settings.cpp b/editor/settings/editor_autoload_settings.cpp index e744e47cc5..2f6d6ab187 100644 --- a/editor/settings/editor_autoload_settings.cpp +++ b/editor/settings/editor_autoload_settings.cpp @@ -32,6 +32,8 @@ #include "core/config/project_settings.h" #include "core/core_constants.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/filesystem_dock.h" diff --git a/editor/settings/editor_build_profile.cpp b/editor/settings/editor_build_profile.cpp index 224a324880..20bbe20eeb 100644 --- a/editor/settings/editor_build_profile.cpp +++ b/editor/settings/editor_build_profile.cpp @@ -32,6 +32,8 @@ #include "core/config/project_settings.h" #include "core/io/json.h" +#include "core/io/resource_importer.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/shader/shader_create_dialog.cpp b/editor/shader/shader_create_dialog.cpp index 45fd991182..a5d02f6f86 100644 --- a/editor/shader/shader_create_dialog.cpp +++ b/editor/shader/shader_create_dialog.cpp @@ -32,6 +32,8 @@ #include "core/config/project_settings.h" #include "core/io/dir_access.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/editor_node.h" diff --git a/editor/shader/shader_editor_plugin.cpp b/editor/shader/shader_editor_plugin.cpp index cad7770e33..e66e67df5c 100644 --- a/editor/shader/shader_editor_plugin.cpp +++ b/editor/shader/shader_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "shader_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "editor/docks/editor_dock_manager.h" #include "editor/docks/filesystem_dock.h" diff --git a/editor/shader/text_shader_editor.cpp b/editor/shader/text_shader_editor.cpp index 7f621dc0d6..0c51aeaf4f 100644 --- a/editor/shader/text_shader_editor.cpp +++ b/editor/shader/text_shader_editor.cpp @@ -31,6 +31,8 @@ #include "text_shader_editor.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/os.h" diff --git a/editor/shader/visual_shader_editor_plugin.cpp b/editor/shader/visual_shader_editor_plugin.cpp index b557195d4d..b194b50d45 100644 --- a/editor/shader/visual_shader_editor_plugin.cpp +++ b/editor/shader/visual_shader_editor_plugin.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/input/input.h" #include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/math/math_defs.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/editor/translations/localization_editor.cpp b/editor/translations/localization_editor.cpp index c387074545..ccd1e89953 100644 --- a/editor/translations/localization_editor.cpp +++ b/editor/translations/localization_editor.cpp @@ -31,6 +31,7 @@ #include "localization_editor.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/string/translation_server.h" diff --git a/editor/version_control/version_control_editor_plugin.cpp b/editor/version_control/version_control_editor_plugin.cpp index b1921751e8..b470c5d685 100644 --- a/editor/version_control/version_control_editor_plugin.cpp +++ b/editor/version_control/version_control_editor_plugin.cpp @@ -31,6 +31,7 @@ #include "version_control_editor_plugin.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" diff --git a/main/main.cpp b/main/main.cpp index 97f88117fd..d68f373579 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -47,6 +47,7 @@ #include "core/io/image.h" #include "core/io/image_loader.h" #include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/class_db.h" #include "core/object/message_queue.h" #include "core/object/script_language.h" diff --git a/modules/dds/register_types.cpp b/modules/dds/register_types.cpp index e0681c4c40..ec60d8e367 100644 --- a/modules/dds/register_types.cpp +++ b/modules/dds/register_types.cpp @@ -33,6 +33,7 @@ #include "image_saver_dds.h" #include "texture_loader_dds.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "scene/resources/texture.h" diff --git a/modules/fbx/editor/editor_scene_importer_fbx2gltf.cpp b/modules/fbx/editor/editor_scene_importer_fbx2gltf.cpp index bc9c6a93e8..a58556a373 100644 --- a/modules/fbx/editor/editor_scene_importer_fbx2gltf.cpp +++ b/modules/fbx/editor/editor_scene_importer_fbx2gltf.cpp @@ -33,6 +33,7 @@ #include "editor_scene_importer_ufbx.h" #include "core/config/project_settings.h" +#include "core/io/resource_importer.h" #include "core/os/os.h" #include "editor/settings/editor_settings.h" diff --git a/modules/fbx/editor/editor_scene_importer_ufbx.cpp b/modules/fbx/editor/editor_scene_importer_ufbx.cpp index a0d1bd93aa..091621bed4 100644 --- a/modules/fbx/editor/editor_scene_importer_ufbx.cpp +++ b/modules/fbx/editor/editor_scene_importer_ufbx.cpp @@ -34,6 +34,7 @@ #include "editor_scene_importer_fbx2gltf.h" #include "core/config/project_settings.h" +#include "core/io/resource_importer.h" void EditorSceneFormatImporterUFBX::get_extensions(List *r_extensions) const { r_extensions->push_back("fbx"); diff --git a/modules/fbx/fbx_document.cpp b/modules/fbx/fbx_document.cpp index ddca283c4e..da57edaf05 100644 --- a/modules/fbx/fbx_document.cpp +++ b/modules/fbx/fbx_document.cpp @@ -37,6 +37,7 @@ #include "core/io/file_access.h" #include "core/io/file_access_memory.h" #include "core/io/image.h" +#include "core/io/resource_loader.h" #include "core/math/color.h" #include "scene/3d/bone_attachment_3d.h" #include "scene/3d/camera_3d.h" diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index a05b46c4df..96e4dad0d8 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -38,6 +38,7 @@ #include "gdscript_tokenizer_buffer.h" #include "gdscript_warning.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" @@ -2885,150 +2886,3 @@ Ref GDScriptLanguage::get_orphan_subclass(const String &p_qualified_na } return Ref(Object::cast_to(obj)); } - -/*************** RESOURCE ***************/ - -Ref ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - Error err; - bool ignoring = p_cache_mode == CACHE_MODE_IGNORE || p_cache_mode == CACHE_MODE_IGNORE_DEEP; - Ref scr = GDScriptCache::get_full_script(p_original_path, err, "", ignoring); - - if (err && scr.is_valid()) { - // If !scr.is_valid(), the error was likely from scr->load_source_code(), which already generates an error. - ERR_PRINT_ED(vformat(R"(Failed to load script "%s" with error "%s".)", p_original_path, error_names[err])); - } - - if (r_error) { - // Don't fail loading because of parsing error. - *r_error = scr.is_valid() ? OK : err; - } - - return scr; -} - -void ResourceFormatLoaderGDScript::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("gd"); - p_extensions->push_back("gdc"); -} - -bool ResourceFormatLoaderGDScript::handles_type(const String &p_type) const { - return (p_type == "Script" || p_type == "GDScript"); -} - -String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const { - String el = p_path.get_extension().to_lower(); - if (el == "gd" || el == "gdc") { - return "GDScript"; - } - return ""; -} - -void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types) { - Ref file = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_path + "'."); - - String source = file->get_as_utf8_string(); - if (source.is_empty()) { - return; - } - - GDScriptParser parser; - if (OK != parser.parse(source, p_path, false)) { - return; - } - - for (const String &E : parser.get_dependencies()) { - p_dependencies->push_back(E); - } -} - -void ResourceFormatLoaderGDScript::get_classes_used(const String &p_path, HashSet *r_classes) { - Ref scr = ResourceLoader::load(p_path); - if (scr.is_null()) { - return; - } - - const String source = scr->get_source_code(); - GDScriptTokenizerText tokenizer; - tokenizer.set_source_code(source); - GDScriptTokenizer::Token current = tokenizer.scan(); - while (current.type != GDScriptTokenizer::Token::TK_EOF) { - if (!current.is_identifier()) { - current = tokenizer.scan(); - continue; - } - - int insert_idx = 0; - for (int i = 0; i < current.start_line - 1; i++) { - insert_idx = source.find("\n", insert_idx) + 1; - } - // Insert the "cursor" character, needed for the lookup to work. - const String source_with_cursor = source.insert(insert_idx + current.start_column, String::chr(0xFFFF)); - - ScriptLanguage::LookupResult result; - if (scr->get_language()->lookup_code(source_with_cursor, current.get_identifier(), p_path, nullptr, result) == OK) { - if (!result.class_name.is_empty() && ClassDB::class_exists(result.class_name)) { - r_classes->insert(result.class_name); - } - - if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY) { - PropertyInfo prop; - if (ClassDB::get_property_info(result.class_name, result.class_member, &prop)) { - if (!prop.class_name.is_empty() && ClassDB::class_exists(prop.class_name)) { - r_classes->insert(prop.class_name); - } - if (!prop.hint_string.is_empty() && ClassDB::class_exists(prop.hint_string)) { - r_classes->insert(prop.hint_string); - } - } - } else if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD) { - MethodInfo met; - if (ClassDB::get_method_info(result.class_name, result.class_member, &met)) { - if (!met.return_val.class_name.is_empty() && ClassDB::class_exists(met.return_val.class_name)) { - r_classes->insert(met.return_val.class_name); - } - if (!met.return_val.hint_string.is_empty() && ClassDB::class_exists(met.return_val.hint_string)) { - r_classes->insert(met.return_val.hint_string); - } - } - } - } - - current = tokenizer.scan(); - } -} - -Error ResourceFormatSaverGDScript::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Ref sqscr = p_resource; - ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER); - - String source = sqscr->get_source_code(); - - { - Error err; - Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); - - ERR_FAIL_COND_V_MSG(err, err, "Cannot save GDScript file '" + p_path + "'."); - - file->store_string(source); - if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { - return ERR_CANT_CREATE; - } - } - - if (ScriptServer::is_reload_scripts_on_save_enabled()) { - GDScriptLanguage::get_singleton()->reload_tool_script(p_resource, true); - } - - return OK; -} - -void ResourceFormatSaverGDScript::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - if (Object::cast_to(*p_resource)) { - p_extensions->push_back("gd"); - } -} - -bool ResourceFormatSaverGDScript::recognize(const Ref &p_resource) const { - return Object::cast_to(*p_resource) != nullptr; -} diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 8662a77b1c..ed6eabd675 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -35,8 +35,6 @@ #include "core/debugger/engine_debugger.h" #include "core/debugger/script_debugger.h" #include "core/doc_data.h" -#include "core/io/resource_loader.h" -#include "core/io/resource_saver.h" #include "core/object/script_language.h" #include "core/templates/rb_set.h" @@ -659,24 +657,3 @@ public: GDScriptLanguage(); ~GDScriptLanguage(); }; - -class ResourceFormatLoaderGDScript : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderGDScript, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; - virtual void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false) override; - virtual void get_classes_used(const String &p_path, HashSet *r_classes) override; -}; - -class ResourceFormatSaverGDScript : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverGDScript, ResourceFormatSaver); - -public: - virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - virtual bool recognize(const Ref &p_resource) const override; -}; diff --git a/modules/gdscript/gdscript_cache.cpp b/modules/gdscript/gdscript_cache.cpp index 84c7b66a7e..efa4ff3d4d 100644 --- a/modules/gdscript/gdscript_cache.cpp +++ b/modules/gdscript/gdscript_cache.cpp @@ -36,6 +36,7 @@ #include "gdscript_parser.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/templates/vector.h" GDScriptParserRef::Status GDScriptParserRef::get_status() const { diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 8adbb6f3f9..be7d140093 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -38,6 +38,7 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) { diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index ddf368ed30..6ff00e6f77 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -42,6 +42,7 @@ #include "core/config/engine.h" #include "core/core_constants.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/math/expression.h" #include "core/object/class_db.h" #include "core/variant/container_type_validate.h" diff --git a/modules/gdscript/gdscript_resource_format.cpp b/modules/gdscript/gdscript_resource_format.cpp new file mode 100644 index 0000000000..2d6582f311 --- /dev/null +++ b/modules/gdscript/gdscript_resource_format.cpp @@ -0,0 +1,182 @@ +/**************************************************************************/ +/* gdscript_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "gdscript_resource_format.h" + +#include "gdscript_cache.h" +#include "gdscript_parser.h" + +#include "core/io/file_access.h" +#include "core/object/class_db.h" + +Ref ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + Error err; + bool ignoring = p_cache_mode == CACHE_MODE_IGNORE || p_cache_mode == CACHE_MODE_IGNORE_DEEP; + Ref scr = GDScriptCache::get_full_script(p_original_path, err, "", ignoring); + + if (err && scr.is_valid()) { + // If !scr.is_valid(), the error was likely from scr->load_source_code(), which already generates an error. + ERR_PRINT_ED(vformat(R"(Failed to load script "%s" with error "%s".)", p_original_path, error_names[err])); + } + + if (r_error) { + // Don't fail loading because of parsing error. + *r_error = scr.is_valid() ? OK : err; + } + + return scr; +} + +void ResourceFormatLoaderGDScript::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("gd"); + p_extensions->push_back("gdc"); +} + +bool ResourceFormatLoaderGDScript::handles_type(const String &p_type) const { + return (p_type == "Script" || p_type == "GDScript"); +} + +String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const { + String el = p_path.get_extension().to_lower(); + if (el == "gd" || el == "gdc") { + return "GDScript"; + } + return ""; +} + +void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types) { + Ref file = FileAccess::open(p_path, FileAccess::READ); + ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_path + "'."); + + String source = file->get_as_utf8_string(); + if (source.is_empty()) { + return; + } + + GDScriptParser parser; + if (OK != parser.parse(source, p_path, false)) { + return; + } + + for (const String &E : parser.get_dependencies()) { + p_dependencies->push_back(E); + } +} + +void ResourceFormatLoaderGDScript::get_classes_used(const String &p_path, HashSet *r_classes) { + Ref scr = ResourceLoader::load(p_path); + if (scr.is_null()) { + return; + } + + const String source = scr->get_source_code(); + GDScriptTokenizerText tokenizer; + tokenizer.set_source_code(source); + GDScriptTokenizer::Token current = tokenizer.scan(); + while (current.type != GDScriptTokenizer::Token::TK_EOF) { + if (!current.is_identifier()) { + current = tokenizer.scan(); + continue; + } + + int insert_idx = 0; + for (int i = 0; i < current.start_line - 1; i++) { + insert_idx = source.find("\n", insert_idx) + 1; + } + // Insert the "cursor" character, needed for the lookup to work. + const String source_with_cursor = source.insert(insert_idx + current.start_column, String::chr(0xFFFF)); + + ScriptLanguage::LookupResult result; + if (scr->get_language()->lookup_code(source_with_cursor, current.get_identifier(), p_path, nullptr, result) == OK) { + if (!result.class_name.is_empty() && ClassDB::class_exists(result.class_name)) { + r_classes->insert(result.class_name); + } + + if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY) { + PropertyInfo prop; + if (ClassDB::get_property_info(result.class_name, result.class_member, &prop)) { + if (!prop.class_name.is_empty() && ClassDB::class_exists(prop.class_name)) { + r_classes->insert(prop.class_name); + } + if (!prop.hint_string.is_empty() && ClassDB::class_exists(prop.hint_string)) { + r_classes->insert(prop.hint_string); + } + } + } else if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD) { + MethodInfo met; + if (ClassDB::get_method_info(result.class_name, result.class_member, &met)) { + if (!met.return_val.class_name.is_empty() && ClassDB::class_exists(met.return_val.class_name)) { + r_classes->insert(met.return_val.class_name); + } + if (!met.return_val.hint_string.is_empty() && ClassDB::class_exists(met.return_val.hint_string)) { + r_classes->insert(met.return_val.hint_string); + } + } + } + } + + current = tokenizer.scan(); + } +} + +Error ResourceFormatSaverGDScript::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Ref sqscr = p_resource; + ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER); + + String source = sqscr->get_source_code(); + + { + Error err; + Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); + + ERR_FAIL_COND_V_MSG(err, err, "Cannot save GDScript file '" + p_path + "'."); + + file->store_string(source); + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + } + + if (ScriptServer::is_reload_scripts_on_save_enabled()) { + GDScriptLanguage::get_singleton()->reload_tool_script(p_resource, true); + } + + return OK; +} + +void ResourceFormatSaverGDScript::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + if (Object::cast_to(*p_resource)) { + p_extensions->push_back("gd"); + } +} + +bool ResourceFormatSaverGDScript::recognize(const Ref &p_resource) const { + return Object::cast_to(*p_resource) != nullptr; +} diff --git a/modules/gdscript/gdscript_resource_format.h b/modules/gdscript/gdscript_resource_format.h new file mode 100644 index 0000000000..8fdc1136ef --- /dev/null +++ b/modules/gdscript/gdscript_resource_format.h @@ -0,0 +1,55 @@ +/**************************************************************************/ +/* gdscript_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" + +class ResourceFormatLoaderGDScript : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderGDScript, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; + virtual void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false) override; + virtual void get_classes_used(const String &p_path, HashSet *r_classes) override; +}; + +class ResourceFormatSaverGDScript : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverGDScript, ResourceFormatSaver); + +public: + virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + virtual bool recognize(const Ref &p_resource) const override; +}; diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp index d931ab4f2e..2db9aba74d 100644 --- a/modules/gdscript/language_server/gdscript_text_document.cpp +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -34,6 +34,7 @@ #include "gdscript_extend_parser.h" #include "gdscript_language_protocol.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/script/script_editor_plugin.h" diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 52f7397abe..a7b342723e 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -33,6 +33,7 @@ #include "gdscript.h" #include "gdscript_cache.h" #include "gdscript_parser.h" +#include "gdscript_resource_format.h" #include "gdscript_tokenizer_buffer.h" #include "gdscript_utility_functions.h" @@ -53,6 +54,7 @@ #include "core/io/file_access.h" #include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/class_db.h" #ifdef TOOLS_ENABLED diff --git a/modules/gdscript/tests/gdscript_test_runner.cpp b/modules/gdscript/tests/gdscript_test_runner.cpp index 27bea5db34..4728976b9a 100644 --- a/modules/gdscript/tests/gdscript_test_runner.cpp +++ b/modules/gdscript/tests/gdscript_test_runner.cpp @@ -40,6 +40,8 @@ #include "core/core_globals.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_uid.h" #include "core/object/class_db.h" #include "core/os/os.h" #include "core/string/string_builder.h" diff --git a/modules/gdscript/tests/gdscript_test_runner_suite.h b/modules/gdscript/tests/gdscript_test_runner_suite.h index df3f0d84cf..bc098db579 100644 --- a/modules/gdscript/tests/gdscript_test_runner_suite.h +++ b/modules/gdscript/tests/gdscript_test_runner_suite.h @@ -34,6 +34,7 @@ #include "gdscript_test_runner.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "tests/test_macros.h" #include "tests/test_utils.h" diff --git a/modules/gdscript/tests/test_completion.h b/modules/gdscript/tests/test_completion.h index 7b053f695c..bfa27e0ed9 100644 --- a/modules/gdscript/tests/test_completion.h +++ b/modules/gdscript/tests/test_completion.h @@ -39,6 +39,7 @@ #include "core/io/config_file.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/object/script_language.h" #include "core/variant/dictionary.h" #include "core/variant/variant.h" diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp index b8b0a25904..2f2b03dd42 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.cpp +++ b/modules/gltf/editor/editor_scene_importer_blend.cpp @@ -35,6 +35,7 @@ #include "editor_import_blend_runner.h" #include "core/config/project_settings.h" +#include "core/io/resource_importer.h" #include "core/object/callable_mp.h" #include "core/os/os.h" #include "editor/editor_node.h" diff --git a/modules/gltf/editor/editor_scene_importer_gltf.cpp b/modules/gltf/editor/editor_scene_importer_gltf.cpp index 2b5249cf33..a40ac99959 100644 --- a/modules/gltf/editor/editor_scene_importer_gltf.cpp +++ b/modules/gltf/editor/editor_scene_importer_gltf.cpp @@ -33,6 +33,8 @@ #include "../gltf_defines.h" #include "../gltf_document.h" +#include "core/io/resource_importer.h" + void EditorSceneFormatImporterGLTF::get_extensions(List *r_extensions) const { r_extensions->push_back("gltf"); r_extensions->push_back("glb"); diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 65a49231bd..7d37c9dae7 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -44,6 +44,7 @@ #include "core/io/file_access.h" #include "core/io/file_access_memory.h" #include "core/io/json.h" +#include "core/io/resource_loader.h" #include "core/io/stream_peer.h" #include "core/object/class_db.h" #include "core/object/object_id.h" diff --git a/modules/gltf/tests/test_gltf.h b/modules/gltf/tests/test_gltf.h index 9b28f9f30e..d346fb89ae 100644 --- a/modules/gltf/tests/test_gltf.h +++ b/modules/gltf/tests/test_gltf.h @@ -44,6 +44,7 @@ #include "editor/import/3d/resource_importer_scene.h" #include "editor/import/resource_importer_texture.h" #include "scene/resources/compressed_texture.h" +#include "scene/resources/compressed_texture_resource_format.h" #include "scene/resources/packed_scene.h" #include "tests/test_utils.h" diff --git a/modules/ktx/register_types.cpp b/modules/ktx/register_types.cpp index d0be8e7a69..65c717c88f 100644 --- a/modules/ktx/register_types.cpp +++ b/modules/ktx/register_types.cpp @@ -32,6 +32,7 @@ #include "texture_loader_ktx.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "scene/resources/image_texture.h" diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index e4dad17f6b..b3be32af3d 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -79,13 +79,6 @@ // This must be a superset of `ignored_types` in bindings_generator.cpp. const Vector ignored_types = {}; -#ifdef TOOLS_ENABLED -static bool _create_project_solution_if_needed() { - CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr); - return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolutionIfNeeded"); -} -#endif - CSharpLanguage *CSharpLanguage::singleton = nullptr; GDExtensionInstanceBindingCallbacks CSharpLanguage::_instance_binding_callbacks = { @@ -2826,123 +2819,3 @@ void CSharpScript::get_members(HashSet *p_members) { } #endif // DEBUG_ENABLED } - -/*************** RESOURCE ***************/ - -Ref ResourceFormatLoaderCSharpScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - if (r_error) { - *r_error = ERR_FILE_CANT_OPEN; - } - - // TODO ignore anything inside bin/ and obj/ in tools builds? - - String real_path = p_path; - if (p_path.begins_with("csharp://")) { - // This is a virtual path used by generic types, extract the real path. - real_path = "res://" + p_path.trim_prefix("csharp://"); - real_path = real_path.substr(0, real_path.rfind_char(':')); - } - - Ref scr; - - if (GDMonoCache::godot_api_cache_updated) { - GDMonoCache::managed_callbacks.ScriptManagerBridge_GetOrCreateScriptBridgeForPath(&p_path, &scr); - ERR_FAIL_COND_V_MSG(scr.is_null(), Ref(), "Could not create C# script '" + real_path + "'."); - } else { - scr.instantiate(); - } - -#ifdef DEBUG_ENABLED - Error err = scr->load_source_code(real_path); - ERR_FAIL_COND_V_MSG(err != OK, Ref(), "Cannot load C# script file '" + real_path + "'."); -#endif // DEBUG_ENABLED - - // Only one instance of a C# script is allowed to exist. - ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref(), - "The C# script path is different from the path it was registered in the C# dictionary."); - - Ref existing = ResourceCache::get_ref(p_path); - switch (p_cache_mode) { - case ResourceFormatLoader::CACHE_MODE_IGNORE: - case ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP: - break; - case ResourceFormatLoader::CACHE_MODE_REUSE: - if (existing.is_null()) { - scr->set_path(p_original_path); - } else { - scr = existing; - } - break; - case ResourceFormatLoader::CACHE_MODE_REPLACE: - case ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP: - scr->set_path(p_original_path, true); - break; - } - - scr->reload(); - - if (r_error) { - *r_error = OK; - } - - return scr; -} - -void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("cs"); -} - -bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const { - return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type(); -} - -String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const { - return p_path.has_extension("cs") ? CSharpLanguage::get_singleton()->get_type() : ""; -} - -Error ResourceFormatSaverCSharpScript::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Ref sqscr = p_resource; - ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER); - - String source = sqscr->get_source_code(); - -#ifdef TOOLS_ENABLED - if (!FileAccess::exists(p_path)) { - // The file does not yet exist, let's assume the user just created this script. In such - // cases we need to check whether the solution and csproj were already created or not. - if (!_create_project_solution_if_needed()) { - ERR_PRINT("C# project could not be created; cannot add file: '" + p_path + "'."); - } - } -#endif - - { - Error err; - Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); - ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save C# script file '" + p_path + "'."); - - file->store_string(source); - - if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { - return ERR_CANT_CREATE; - } - } - -#ifdef TOOLS_ENABLED - if (ScriptServer::is_reload_scripts_on_save_enabled()) { - CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false); - } -#endif - - return OK; -} - -void ResourceFormatSaverCSharpScript::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - if (Object::cast_to(p_resource.ptr())) { - p_extensions->push_back("cs"); - } -} - -bool ResourceFormatSaverCSharpScript::recognize(const Ref &p_resource) const { - return Object::cast_to(p_resource.ptr()) != nullptr; -} diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h index edfe090862..366f2be7b8 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -34,8 +34,6 @@ #include "mono_gd/gd_mono.h" #include "core/doc_data.h" -#include "core/io/resource_loader.h" -#include "core/io/resource_saver.h" #include "core/object/script_language.h" #include "core/templates/rb_map.h" #include "core/templates/self_list.h" @@ -584,22 +582,3 @@ public: CSharpLanguage(); ~CSharpLanguage(); }; - -class ResourceFormatLoaderCSharpScript : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderCSharpScript, ResourceFormatLoader); - -public: - Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - void get_recognized_extensions(List *p_extensions) const override; - bool handles_type(const String &p_type) const override; - String get_resource_type(const String &p_path) const override; -}; - -class ResourceFormatSaverCSharpScript : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverCSharpScript, ResourceFormatSaver); - -public: - Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - bool recognize(const Ref &p_resource) const override; -}; diff --git a/modules/mono/csharp_script_resource_format.cpp b/modules/mono/csharp_script_resource_format.cpp new file mode 100644 index 0000000000..61f6772b70 --- /dev/null +++ b/modules/mono/csharp_script_resource_format.cpp @@ -0,0 +1,160 @@ +/**************************************************************************/ +/* csharp_script_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "csharp_script_resource_format.h" + +#include "mono_gd/gd_mono_cache.h" + +#include "core/io/file_access.h" + +#ifdef TOOLS_ENABLED +static bool _create_project_solution_if_needed() { + CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr); + return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolutionIfNeeded"); +} +#endif + +Ref ResourceFormatLoaderCSharpScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + // TODO ignore anything inside bin/ and obj/ in tools builds? + + String real_path = p_path; + if (p_path.begins_with("csharp://")) { + // This is a virtual path used by generic types, extract the real path. + real_path = "res://" + p_path.trim_prefix("csharp://"); + real_path = real_path.substr(0, real_path.rfind_char(':')); + } + + Ref scr; + + if (GDMonoCache::godot_api_cache_updated) { + GDMonoCache::managed_callbacks.ScriptManagerBridge_GetOrCreateScriptBridgeForPath(&p_path, &scr); + ERR_FAIL_COND_V_MSG(scr.is_null(), Ref(), "Could not create C# script '" + real_path + "'."); + } else { + scr.instantiate(); + } + +#ifdef DEBUG_ENABLED + Error err = scr->load_source_code(real_path); + ERR_FAIL_COND_V_MSG(err != OK, Ref(), "Cannot load C# script file '" + real_path + "'."); +#endif // DEBUG_ENABLED + + // Only one instance of a C# script is allowed to exist. + ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref(), + "The C# script path is different from the path it was registered in the C# dictionary."); + + Ref existing = ResourceCache::get_ref(p_path); + switch (p_cache_mode) { + case ResourceFormatLoader::CACHE_MODE_IGNORE: + case ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP: + break; + case ResourceFormatLoader::CACHE_MODE_REUSE: + if (existing.is_null()) { + scr->set_path(p_original_path); + } else { + scr = existing; + } + break; + case ResourceFormatLoader::CACHE_MODE_REPLACE: + case ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP: + scr->set_path(p_original_path, true); + break; + } + + scr->reload(); + + if (r_error) { + *r_error = OK; + } + + return scr; +} + +void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("cs"); +} + +bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const { + return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type(); +} + +String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const { + return p_path.has_extension("cs") ? CSharpLanguage::get_singleton()->get_type() : ""; +} + +Error ResourceFormatSaverCSharpScript::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Ref sqscr = p_resource; + ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER); + + String source = sqscr->get_source_code(); + +#ifdef TOOLS_ENABLED + if (!FileAccess::exists(p_path)) { + // The file does not yet exist, let's assume the user just created this script. In such + // cases we need to check whether the solution and csproj were already created or not. + if (!_create_project_solution_if_needed()) { + ERR_PRINT("C# project could not be created; cannot add file: '" + p_path + "'."); + } + } +#endif + + { + Error err; + Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save C# script file '" + p_path + "'."); + + file->store_string(source); + + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + } + +#ifdef TOOLS_ENABLED + if (ScriptServer::is_reload_scripts_on_save_enabled()) { + CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false); + } +#endif + + return OK; +} + +void ResourceFormatSaverCSharpScript::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + if (Object::cast_to(p_resource.ptr())) { + p_extensions->push_back("cs"); + } +} + +bool ResourceFormatSaverCSharpScript::recognize(const Ref &p_resource) const { + return Object::cast_to(p_resource.ptr()) != nullptr; +} diff --git a/modules/mono/csharp_script_resource_format.h b/modules/mono/csharp_script_resource_format.h new file mode 100644 index 0000000000..a01c750f6e --- /dev/null +++ b/modules/mono/csharp_script_resource_format.h @@ -0,0 +1,53 @@ +/**************************************************************************/ +/* csharp_script_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" + +class ResourceFormatLoaderCSharpScript : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderCSharpScript, ResourceFormatLoader); + +public: + Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + void get_recognized_extensions(List *p_extensions) const override; + bool handles_type(const String &p_type) const override; + String get_resource_type(const String &p_path) const override; +}; + +class ResourceFormatSaverCSharpScript : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverCSharpScript, ResourceFormatSaver); + +public: + Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + bool recognize(const Ref &p_resource) const override; +}; diff --git a/modules/mono/editor/code_completion.cpp b/modules/mono/editor/code_completion.cpp index d26e3e75d6..940dd82e3f 100644 --- a/modules/mono/editor/code_completion.cpp +++ b/modules/mono/editor/code_completion.cpp @@ -31,6 +31,7 @@ #include "code_completion.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "editor/file_system/editor_file_system.h" diff --git a/modules/mono/glue/runtime_interop.cpp b/modules/mono/glue/runtime_interop.cpp index e45e20794a..2026407c03 100644 --- a/modules/mono/glue/runtime_interop.cpp +++ b/modules/mono/glue/runtime_interop.cpp @@ -42,6 +42,7 @@ #include "core/debugger/script_debugger.h" #include "core/io/compression.h" #include "core/io/marshalls.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "core/object/method_bind.h" #include "core/os/os.h" diff --git a/modules/mono/register_types.cpp b/modules/mono/register_types.cpp index b666563a98..b259117c1a 100644 --- a/modules/mono/register_types.cpp +++ b/modules/mono/register_types.cpp @@ -31,7 +31,10 @@ #include "register_types.h" #include "csharp_script.h" +#include "csharp_script_resource_format.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/class_db.h" CSharpLanguage *script_language_cs = nullptr; diff --git a/modules/mp3/register_types.cpp b/modules/mp3/register_types.cpp index a1593a697f..65bbe93ad0 100644 --- a/modules/mp3/register_types.cpp +++ b/modules/mp3/register_types.cpp @@ -32,6 +32,7 @@ #include "audio_stream_mp3.h" +#include "core/io/resource_importer.h" #include "core/object/class_db.h" #ifdef TOOLS_ENABLED diff --git a/modules/navigation_2d/nav_region_2d.h b/modules/navigation_2d/nav_region_2d.h index 88a93a3754..7dd4191092 100644 --- a/modules/navigation_2d/nav_region_2d.h +++ b/modules/navigation_2d/nav_region_2d.h @@ -34,6 +34,7 @@ #include "nav_base_2d.h" #include "nav_utils_2d.h" +#include "core/object/worker_thread_pool.h" #include "core/os/rw_lock.h" #include "scene/resources/2d/navigation_polygon.h" diff --git a/modules/navigation_3d/editor/navigation_region_3d_editor_plugin.cpp b/modules/navigation_3d/editor/navigation_region_3d_editor_plugin.cpp index 6c84681922..cbcf1a921c 100644 --- a/modules/navigation_3d/editor/navigation_region_3d_editor_plugin.cpp +++ b/modules/navigation_3d/editor/navigation_region_3d_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "navigation_region_3d_editor_plugin.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" diff --git a/modules/navigation_3d/nav_region_3d.h b/modules/navigation_3d/nav_region_3d.h index 1490f30230..a9da756cd9 100644 --- a/modules/navigation_3d/nav_region_3d.h +++ b/modules/navigation_3d/nav_region_3d.h @@ -34,6 +34,7 @@ #include "nav_base_3d.h" #include "nav_utils_3d.h" +#include "core/object/worker_thread_pool.h" #include "core/os/rw_lock.h" #include "scene/resources/navigation_mesh.h" diff --git a/modules/objectdb_profiler/editor/snapshot_data.cpp b/modules/objectdb_profiler/editor/snapshot_data.cpp index f229394ca3..c62b90c19a 100644 --- a/modules/objectdb_profiler/editor/snapshot_data.cpp +++ b/modules/objectdb_profiler/editor/snapshot_data.cpp @@ -32,6 +32,7 @@ #include "core/core_bind.h" #include "core/io/compression.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "scene/debugger/scene_debugger_object.h" diff --git a/modules/theora/register_types.cpp b/modules/theora/register_types.cpp index 14e8c214dd..069817b672 100644 --- a/modules/theora/register_types.cpp +++ b/modules/theora/register_types.cpp @@ -32,6 +32,7 @@ #include "video_stream_theora.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #ifdef TOOLS_ENABLED diff --git a/modules/vorbis/register_types.cpp b/modules/vorbis/register_types.cpp index 12025b9a4d..622ef1edee 100644 --- a/modules/vorbis/register_types.cpp +++ b/modules/vorbis/register_types.cpp @@ -32,6 +32,7 @@ #include "audio_stream_ogg_vorbis.h" +#include "core/io/resource_importer.h" #include "core/object/class_db.h" #ifdef TOOLS_ENABLED diff --git a/modules/webp/register_types.cpp b/modules/webp/register_types.cpp index 1d8f67999e..f68e9e97a8 100644 --- a/modules/webp/register_types.cpp +++ b/modules/webp/register_types.cpp @@ -33,6 +33,8 @@ #include "image_loader_webp.h" #include "resource_saver_webp.h" +#include "core/io/resource_saver.h" + static Ref image_loader_webp; static Ref resource_saver_webp; diff --git a/scene/3d/lightmap_gi.cpp b/scene/3d/lightmap_gi.cpp index c216c567fc..176d5c4eff 100644 --- a/scene/3d/lightmap_gi.cpp +++ b/scene/3d/lightmap_gi.cpp @@ -33,6 +33,8 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/io/config_file.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/math/delaunay_3d.h" #include "core/math/geometry_3d.h" #include "core/object/class_db.h" diff --git a/scene/3d/occluder_instance_3d.cpp b/scene/3d/occluder_instance_3d.cpp index 8d1374be2f..f64748d72b 100644 --- a/scene/3d/occluder_instance_3d.cpp +++ b/scene/3d/occluder_instance_3d.cpp @@ -33,6 +33,7 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/io/marshalls.h" +#include "core/io/resource_saver.h" #include "core/math/geometry_2d.h" #include "core/math/triangulate.h" #include "core/object/callable_mp.h" diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index 94ea01ee19..6fb8754266 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -33,6 +33,8 @@ #include "core/config/engine.h" #include "core/input/input.h" #include "core/io/image.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/math/expression.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 75f8f1da9f..9d3424e885 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -32,6 +32,8 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/object/class_db.h" #include "core/os/os.h" #include "scene/animation/animation_blend_space_1d.h" @@ -122,6 +124,7 @@ #include "scene/resources/color_palette.h" #include "scene/resources/compositor.h" #include "scene/resources/compressed_texture.h" +#include "scene/resources/compressed_texture_resource_format.h" #include "scene/resources/curve_texture.h" #include "scene/resources/drawable_texture_2d.h" #include "scene/resources/environment.h" @@ -146,6 +149,8 @@ #include "scene/resources/portable_compressed_texture.h" #include "scene/resources/resource_format_text.h" #include "scene/resources/shader_include.h" +#include "scene/resources/shader_include_resource_format.h" +#include "scene/resources/shader_resource_format.h" #include "scene/resources/skeleton_profile.h" #include "scene/resources/sky.h" #include "scene/resources/style_box.h" diff --git a/scene/resources/compressed_texture.cpp b/scene/resources/compressed_texture.cpp index 2ea03b3599..22d12c2f56 100644 --- a/scene/resources/compressed_texture.cpp +++ b/scene/resources/compressed_texture.cpp @@ -31,6 +31,7 @@ #include "compressed_texture.h" #include "core/io/file_access.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "scene/resources/bit_map.h" #include "servers/rendering/rendering_server.h" @@ -465,35 +466,6 @@ CompressedTexture2D::~CompressedTexture2D() { } } -Ref ResourceFormatLoaderCompressedTexture2D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - Ref st; - st.instantiate(); - Error err = st->load(p_path); - if (r_error) { - *r_error = err; - } - if (err != OK) { - return Ref(); - } - - return st; -} - -void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("ctex"); -} - -bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const { - return p_type == "CompressedTexture2D"; -} - -String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const { - if (p_path.has_extension("ctex")) { - return "CompressedTexture2D"; - } - return ""; -} - void CompressedTexture3D::set_path(const String &p_path, bool p_take_over) { if (texture.is_valid()) { RenderingServer::get_singleton()->texture_set_path(texture, p_path); @@ -649,35 +621,6 @@ CompressedTexture3D::~CompressedTexture3D() { } } -Ref ResourceFormatLoaderCompressedTexture3D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - Ref st; - st.instantiate(); - Error err = st->load(p_path); - if (r_error) { - *r_error = err; - } - if (err != OK) { - return Ref(); - } - - return st; -} - -void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("ctex3d"); -} - -bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const { - return p_type == "CompressedTexture3D"; -} - -String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const { - if (p_path.has_extension("ctex3d")) { - return "CompressedTexture3D"; - } - return ""; -} - void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) { if (texture.is_valid()) { RenderingServer::get_singleton()->texture_set_path(texture, p_path); @@ -841,59 +784,3 @@ CompressedTextureLayered::~CompressedTextureLayered() { RS::get_singleton()->free_rid(texture); } } - -///////////////////////////////////////////////// - -Ref ResourceFormatLoaderCompressedTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - Ref ct; - if (p_path.has_extension("ctexarray")) { - Ref c; - c.instantiate(); - ct = c; - } else if (p_path.has_extension("ccube")) { - Ref c; - c.instantiate(); - ct = c; - } else if (p_path.has_extension("ccubearray")) { - Ref c; - c.instantiate(); - ct = c; - } else { - if (r_error) { - *r_error = ERR_FILE_UNRECOGNIZED; - } - return Ref(); - } - Error err = ct->load(p_path); - if (r_error) { - *r_error = err; - } - if (err != OK) { - return Ref(); - } - - return ct; -} - -void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("ctexarray"); - p_extensions->push_back("ccube"); - p_extensions->push_back("ccubearray"); -} - -bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const { - return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray"; -} - -String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const { - if (p_path.has_extension("ctexarray")) { - return "CompressedTexture2DArray"; - } - if (p_path.has_extension("ccube")) { - return "CompressedCubemap"; - } - if (p_path.has_extension("ccubearray")) { - return "CompressedCubemapArray"; - } - return ""; -} diff --git a/scene/resources/compressed_texture.h b/scene/resources/compressed_texture.h index 35f3677dcb..d0b7937272 100644 --- a/scene/resources/compressed_texture.h +++ b/scene/resources/compressed_texture.h @@ -30,7 +30,6 @@ #pragma once -#include "core/io/resource_loader.h" #include "scene/resources/texture.h" #include "servers/rendering/rendering_server_enums.h" @@ -110,16 +109,6 @@ public: ~CompressedTexture2D(); }; -class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderCompressedTexture2D, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; - class CompressedTextureLayered : public TextureLayered { GDCLASS(CompressedTextureLayered, TextureLayered); @@ -176,16 +165,6 @@ public: ~CompressedTextureLayered(); }; -class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderCompressedTextureLayered, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; - class CompressedTexture2DArray : public CompressedTextureLayered { GDCLASS(CompressedTexture2DArray, CompressedTextureLayered) public: @@ -261,13 +240,3 @@ public: ~CompressedTexture3D(); }; - -class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderCompressedTexture3D, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; diff --git a/scene/resources/compressed_texture_resource_format.cpp b/scene/resources/compressed_texture_resource_format.cpp new file mode 100644 index 0000000000..0958dbb905 --- /dev/null +++ b/scene/resources/compressed_texture_resource_format.cpp @@ -0,0 +1,145 @@ +/**************************************************************************/ +/* compressed_texture_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "compressed_texture_resource_format.h" + +#include "scene/resources/compressed_texture.h" + +Ref ResourceFormatLoaderCompressedTexture2D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + Ref st; + st.instantiate(); + Error err = st->load(p_path); + if (r_error) { + *r_error = err; + } + if (err != OK) { + return Ref(); + } + + return st; +} + +void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("ctex"); +} + +bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const { + return p_type == "CompressedTexture2D"; +} + +String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const { + if (p_path.has_extension("ctex")) { + return "CompressedTexture2D"; + } + return ""; +} + +Ref ResourceFormatLoaderCompressedTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + Ref ct; + if (p_path.has_extension("ctexarray")) { + Ref c; + c.instantiate(); + ct = c; + } else if (p_path.has_extension("ccube")) { + Ref c; + c.instantiate(); + ct = c; + } else if (p_path.has_extension("ccubearray")) { + Ref c; + c.instantiate(); + ct = c; + } else { + if (r_error) { + *r_error = ERR_FILE_UNRECOGNIZED; + } + return Ref(); + } + Error err = ct->load(p_path); + if (r_error) { + *r_error = err; + } + if (err != OK) { + return Ref(); + } + + return ct; +} + +void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("ctexarray"); + p_extensions->push_back("ccube"); + p_extensions->push_back("ccubearray"); +} + +bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const { + return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray"; +} + +String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const { + if (p_path.has_extension("ctexarray")) { + return "CompressedTexture2DArray"; + } + if (p_path.has_extension("ccube")) { + return "CompressedCubemap"; + } + if (p_path.has_extension("ccubearray")) { + return "CompressedCubemapArray"; + } + return ""; +} + +Ref ResourceFormatLoaderCompressedTexture3D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + Ref st; + st.instantiate(); + Error err = st->load(p_path); + if (r_error) { + *r_error = err; + } + if (err != OK) { + return Ref(); + } + + return st; +} + +void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("ctex3d"); +} + +bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const { + return p_type == "CompressedTexture3D"; +} + +String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const { + if (p_path.has_extension("ctex3d")) { + return "CompressedTexture3D"; + } + return ""; +} diff --git a/scene/resources/compressed_texture_resource_format.h b/scene/resources/compressed_texture_resource_format.h new file mode 100644 index 0000000000..7003553843 --- /dev/null +++ b/scene/resources/compressed_texture_resource_format.h @@ -0,0 +1,63 @@ +/**************************************************************************/ +/* compressed_texture_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" + +class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderCompressedTexture2D, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; + +class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderCompressedTextureLayered, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; + +class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderCompressedTexture3D, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; diff --git a/scene/resources/image_texture.cpp b/scene/resources/image_texture.cpp index 7f38a38955..fed1bba06a 100644 --- a/scene/resources/image_texture.cpp +++ b/scene/resources/image_texture.cpp @@ -31,6 +31,7 @@ #include "image_texture.h" #include "core/io/image_loader.h" +#include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "scene/resources/bit_map.h" #include "scene/resources/placeholder_textures.h" diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 8141491d3e..1ba8d9606b 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -33,6 +33,7 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/error/error_macros.h" +#include "core/io/resource_loader.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/os.h" diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index 01a7a85a1f..2b8c1300ce 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -32,7 +32,6 @@ #include "shader.compat.inc" #include "core/config/engine.h" -#include "core/io/file_access.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "scene/main/scene_tree.h" @@ -308,79 +307,3 @@ Shader::~Shader() { RenderingServer::get_singleton()->free_rid(shader_rid); } } - -//////////// - -Ref ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - if (r_error) { - *r_error = ERR_FILE_CANT_OPEN; - } - - Error error = OK; - Vector buffer = FileAccess::get_file_as_bytes(p_path, &error); - ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader: " + p_path); - - String str; - if (buffer.size() > 0) { - error = str.append_utf8((const char *)buffer.ptr(), buffer.size()); - ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader: " + p_path); - } - - Ref shader; - shader.instantiate(); - - shader->set_include_path(p_path); - shader->set_code(str); - - if (r_error) { - *r_error = OK; - } - - return shader; -} - -void ResourceFormatLoaderShader::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("gdshader"); -} - -bool ResourceFormatLoaderShader::handles_type(const String &p_type) const { - return (p_type == "Shader"); -} - -String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { - if (p_path.has_extension("gdshader")) { - return "Shader"; - } - return ""; -} - -Error ResourceFormatSaverShader::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Ref shader = p_resource; - ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); - - String source = shader->get_code(); - - Error err; - Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); - - ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'."); - - file->store_string(source); - if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { - return ERR_CANT_CREATE; - } - - return OK; -} - -void ResourceFormatSaverShader::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - if (const Shader *shader = Object::cast_to(*p_resource)) { - if (shader->is_text_shader()) { - p_extensions->push_back("gdshader"); - } - } -} - -bool ResourceFormatSaverShader::recognize(const Ref &p_resource) const { - return p_resource->get_class_name() == "Shader"; //only shader, not inherited -} diff --git a/scene/resources/shader.h b/scene/resources/shader.h index 7863003b77..6fb7865659 100644 --- a/scene/resources/shader.h +++ b/scene/resources/shader.h @@ -106,22 +106,3 @@ public: }; VARIANT_ENUM_CAST(Shader::Mode); - -class ResourceFormatLoaderShader : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderShader, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; - -class ResourceFormatSaverShader : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverShader, ResourceFormatSaver); - -public: - virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - virtual bool recognize(const Ref &p_resource) const override; -}; diff --git a/scene/resources/shader_include.cpp b/scene/resources/shader_include.cpp index d30c668e0e..9476fd0cfa 100644 --- a/scene/resources/shader_include.cpp +++ b/scene/resources/shader_include.cpp @@ -30,7 +30,6 @@ #include "shader_include.h" -#include "core/io/file_access.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "servers/rendering/shader_preprocessor.h" @@ -83,80 +82,3 @@ void ShaderInclude::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code"); } - -// ResourceFormatLoaderShaderInclude - -Ref ResourceFormatLoaderShaderInclude::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { - if (r_error) { - *r_error = ERR_FILE_CANT_OPEN; - } - - Error error = OK; - Vector buffer = FileAccess::get_file_as_bytes(p_path, &error); - ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader include: " + p_path); - - String str; - if (buffer.size() > 0) { - error = str.append_utf8((const char *)buffer.ptr(), buffer.size()); - ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader include: " + p_path); - } - - Ref shader_inc; - shader_inc.instantiate(); - - shader_inc->set_include_path(p_path); - shader_inc->set_code(str); - - if (r_error) { - *r_error = OK; - } - - return shader_inc; -} - -void ResourceFormatLoaderShaderInclude::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("gdshaderinc"); -} - -bool ResourceFormatLoaderShaderInclude::handles_type(const String &p_type) const { - return (p_type == "ShaderInclude"); -} - -String ResourceFormatLoaderShaderInclude::get_resource_type(const String &p_path) const { - if (p_path.has_extension("gdshaderinc")) { - return "ShaderInclude"; - } - return ""; -} - -// ResourceFormatSaverShaderInclude - -Error ResourceFormatSaverShaderInclude::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { - Ref shader_inc = p_resource; - ERR_FAIL_COND_V(shader_inc.is_null(), ERR_INVALID_PARAMETER); - - String source = shader_inc->get_code(); - - Error error; - Ref file = FileAccess::open(p_path, FileAccess::WRITE, &error); - - ERR_FAIL_COND_V_MSG(error, error, "Cannot save shader include '" + p_path + "'."); - - file->store_string(source); - if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { - return ERR_CANT_CREATE; - } - - return OK; -} - -void ResourceFormatSaverShaderInclude::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { - const ShaderInclude *shader_inc = Object::cast_to(*p_resource); - if (shader_inc != nullptr) { - p_extensions->push_back("gdshaderinc"); - } -} - -bool ResourceFormatSaverShaderInclude::recognize(const Ref &p_resource) const { - return p_resource->get_class_name() == "ShaderInclude"; //only shader, not inherited -} diff --git a/scene/resources/shader_include.h b/scene/resources/shader_include.h index 0930f5b475..f4ceb8ba31 100644 --- a/scene/resources/shader_include.h +++ b/scene/resources/shader_include.h @@ -31,8 +31,6 @@ #pragma once #include "core/io/resource.h" -#include "core/io/resource_loader.h" -#include "core/io/resource_saver.h" #include "core/templates/hash_set.h" class ShaderInclude : public Resource { @@ -54,22 +52,3 @@ public: void set_include_path(const String &p_path); }; - -class ResourceFormatLoaderShaderInclude : public ResourceFormatLoader { - GDSOFTCLASS(ResourceFormatLoaderShaderInclude, ResourceFormatLoader); - -public: - virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; - virtual void get_recognized_extensions(List *p_extensions) const override; - virtual bool handles_type(const String &p_type) const override; - virtual String get_resource_type(const String &p_path) const override; -}; - -class ResourceFormatSaverShaderInclude : public ResourceFormatSaver { - GDSOFTCLASS(ResourceFormatSaverShaderInclude, ResourceFormatSaver); - -public: - virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; - virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; - virtual bool recognize(const Ref &p_resource) const override; -}; diff --git a/scene/resources/shader_include_resource_format.cpp b/scene/resources/shader_include_resource_format.cpp new file mode 100644 index 0000000000..a74e0106aa --- /dev/null +++ b/scene/resources/shader_include_resource_format.cpp @@ -0,0 +1,111 @@ +/**************************************************************************/ +/* shader_include_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "shader_include_resource_format.h" + +#include "core/io/file_access.h" +#include "scene/resources/shader_include.h" + +// ResourceFormatLoaderShaderInclude + +Ref ResourceFormatLoaderShaderInclude::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + Error error = OK; + Vector buffer = FileAccess::get_file_as_bytes(p_path, &error); + ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader include: " + p_path); + + String str; + if (buffer.size() > 0) { + error = str.append_utf8((const char *)buffer.ptr(), buffer.size()); + ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader include: " + p_path); + } + + Ref shader_inc; + shader_inc.instantiate(); + + shader_inc->set_include_path(p_path); + shader_inc->set_code(str); + + if (r_error) { + *r_error = OK; + } + + return shader_inc; +} + +void ResourceFormatLoaderShaderInclude::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("gdshaderinc"); +} + +bool ResourceFormatLoaderShaderInclude::handles_type(const String &p_type) const { + return (p_type == "ShaderInclude"); +} + +String ResourceFormatLoaderShaderInclude::get_resource_type(const String &p_path) const { + if (p_path.has_extension("gdshaderinc")) { + return "ShaderInclude"; + } + return ""; +} + +// ResourceFormatSaverShaderInclude + +Error ResourceFormatSaverShaderInclude::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Ref shader_inc = p_resource; + ERR_FAIL_COND_V(shader_inc.is_null(), ERR_INVALID_PARAMETER); + + String source = shader_inc->get_code(); + + Error error; + Ref file = FileAccess::open(p_path, FileAccess::WRITE, &error); + + ERR_FAIL_COND_V_MSG(error, error, "Cannot save shader include '" + p_path + "'."); + + file->store_string(source); + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + + return OK; +} + +void ResourceFormatSaverShaderInclude::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + const ShaderInclude *shader_inc = Object::cast_to(*p_resource); + if (shader_inc != nullptr) { + p_extensions->push_back("gdshaderinc"); + } +} + +bool ResourceFormatSaverShaderInclude::recognize(const Ref &p_resource) const { + return p_resource->get_class_name() == "ShaderInclude"; //only shader, not inherited +} diff --git a/scene/resources/shader_include_resource_format.h b/scene/resources/shader_include_resource_format.h new file mode 100644 index 0000000000..080049e133 --- /dev/null +++ b/scene/resources/shader_include_resource_format.h @@ -0,0 +1,53 @@ +/**************************************************************************/ +/* shader_include_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" + +class ResourceFormatLoaderShaderInclude : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderShaderInclude, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; + +class ResourceFormatSaverShaderInclude : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverShaderInclude, ResourceFormatSaver); + +public: + virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + virtual bool recognize(const Ref &p_resource) const override; +}; diff --git a/scene/resources/shader_resource_format.cpp b/scene/resources/shader_resource_format.cpp new file mode 100644 index 0000000000..9f2510bb38 --- /dev/null +++ b/scene/resources/shader_resource_format.cpp @@ -0,0 +1,108 @@ +/**************************************************************************/ +/* shader_resource_format.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "shader_resource_format.h" + +#include "core/io/file_access.h" +#include "scene/resources/shader.h" + +Ref ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + Error error = OK; + Vector buffer = FileAccess::get_file_as_bytes(p_path, &error); + ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader: " + p_path); + + String str; + if (buffer.size() > 0) { + error = str.append_utf8((const char *)buffer.ptr(), buffer.size()); + ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader: " + p_path); + } + + Ref shader; + shader.instantiate(); + + shader->set_include_path(p_path); + shader->set_code(str); + + if (r_error) { + *r_error = OK; + } + + return shader; +} + +void ResourceFormatLoaderShader::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("gdshader"); +} + +bool ResourceFormatLoaderShader::handles_type(const String &p_type) const { + return (p_type == "Shader"); +} + +String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { + if (p_path.has_extension("gdshader")) { + return "Shader"; + } + return ""; +} + +Error ResourceFormatSaverShader::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { + Ref shader = p_resource; + ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); + + String source = shader->get_code(); + + Error err; + Ref file = FileAccess::open(p_path, FileAccess::WRITE, &err); + + ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'."); + + file->store_string(source); + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + + return OK; +} + +void ResourceFormatSaverShader::get_recognized_extensions(const Ref &p_resource, List *p_extensions) const { + if (const Shader *shader = Object::cast_to(*p_resource)) { + if (shader->is_text_shader()) { + p_extensions->push_back("gdshader"); + } + } +} + +bool ResourceFormatSaverShader::recognize(const Ref &p_resource) const { + return p_resource->get_class_name() == "Shader"; //only shader, not inherited +} diff --git a/scene/resources/shader_resource_format.h b/scene/resources/shader_resource_format.h new file mode 100644 index 0000000000..b6fea34daa --- /dev/null +++ b/scene/resources/shader_resource_format.h @@ -0,0 +1,53 @@ +/**************************************************************************/ +/* shader_resource_format.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" + +class ResourceFormatLoaderShader : public ResourceFormatLoader { + GDSOFTCLASS(ResourceFormatLoaderShader, ResourceFormatLoader); + +public: + virtual Ref load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; + virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool handles_type(const String &p_type) const override; + virtual String get_resource_type(const String &p_path) const override; +}; + +class ResourceFormatSaverShader : public ResourceFormatSaver { + GDSOFTCLASS(ResourceFormatSaverShader, ResourceFormatSaver); + +public: + virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0) override; + virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const override; + virtual bool recognize(const Ref &p_resource) const override; +}; diff --git a/servers/rendering/shader_preprocessor.cpp b/servers/rendering/shader_preprocessor.cpp index f024a6b44b..2cdb1442d1 100644 --- a/servers/rendering/shader_preprocessor.cpp +++ b/servers/rendering/shader_preprocessor.cpp @@ -30,6 +30,7 @@ #include "shader_preprocessor.h" +#include "core/io/resource_loader.h" #include "core/os/os.h" #include "servers/rendering/shader_expression.h" diff --git a/tests/core/string/test_translation.cpp b/tests/core/string/test_translation.cpp index bc1444d0d9..2547438305 100644 --- a/tests/core/string/test_translation.cpp +++ b/tests/core/string/test_translation.cpp @@ -32,6 +32,7 @@ TEST_FORCE_LINK(test_translation) +#include "core/io/resource_loader.h" #include "core/string/optimized_translation.h" #include "core/string/plural_rules.h" #include "core/string/translation.h"