resource.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**************************************************************************/
  2. /* resource.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/io/resource_uid.h"
  32. #include "core/object/class_db.h"
  33. #include "core/object/gdvirtual.gen.inc"
  34. #include "core/object/ref_counted.h"
  35. #include "core/templates/safe_refcount.h"
  36. #include "core/templates/self_list.h"
  37. class Node;
  38. #define RES_BASE_EXTENSION(m_ext) \
  39. public: \
  40. static void register_custom_data_to_otdb() { \
  41. ClassDB::add_resource_base_extension(m_ext, get_class_static()); \
  42. } \
  43. virtual String get_base_extension() const override { \
  44. return m_ext; \
  45. } \
  46. \
  47. private:
  48. class Resource : public RefCounted {
  49. GDCLASS(Resource, RefCounted);
  50. public:
  51. static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); }
  52. virtual String get_base_extension() const { return "res"; }
  53. protected:
  54. struct DuplicateParams {
  55. bool deep = false;
  56. ResourceDeepDuplicateMode subres_mode = RESOURCE_DEEP_DUPLICATE_MAX;
  57. Node *local_scene = nullptr;
  58. };
  59. private:
  60. friend class ResBase;
  61. friend class ResourceCache;
  62. String name;
  63. String path_cache;
  64. String scene_unique_id;
  65. #ifdef TOOLS_ENABLED
  66. uint64_t last_modified_time = 0;
  67. uint64_t import_last_modified_time = 0;
  68. String import_path;
  69. #endif
  70. enum EmitChangedState {
  71. EMIT_CHANGED_UNBLOCKED,
  72. EMIT_CHANGED_BLOCKED,
  73. EMIT_CHANGED_BLOCKED_PENDING_EMIT,
  74. };
  75. EmitChangedState emit_changed_state = EMIT_CHANGED_UNBLOCKED;
  76. bool local_to_scene = false;
  77. friend class SceneState;
  78. Node *local_scene = nullptr;
  79. SelfList<Resource> remapped_list;
  80. using DuplicateRemapCacheT = HashMap<Ref<Resource>, Ref<Resource>>;
  81. static thread_local inline DuplicateRemapCacheT *thread_duplicate_remap_cache = nullptr;
  82. Variant _duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage = 0) const;
  83. void _find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found);
  84. // Only for binding the deep duplicate method, so it doesn't need actual members.
  85. enum DeepDuplicateMode : int;
  86. _ALWAYS_INLINE_ Ref<Resource> _duplicate_deep_bind(DeepDuplicateMode p_deep_subresources_mode) const;
  87. protected:
  88. virtual void _resource_path_changed();
  89. static void _bind_methods();
  90. void _block_emit_changed();
  91. void _unblock_emit_changed();
  92. void _set_path(const String &p_path);
  93. void _take_over_path(const String &p_path);
  94. virtual void reset_local_to_scene();
  95. GDVIRTUAL0(_setup_local_to_scene);
  96. GDVIRTUAL0RC(RID, _get_rid);
  97. GDVIRTUAL1C(_set_path_cache, String);
  98. GDVIRTUAL0(_reset_state);
  99. virtual Ref<Resource> _duplicate(const DuplicateParams &p_params) const;
  100. public:
  101. static Node *(*_get_local_scene_func)(); // Used by the editor.
  102. static void (*_update_configuration_warning)(); // Used by the editor.
  103. void update_configuration_warning();
  104. virtual bool editor_can_reload_from_file();
  105. virtual void reset_state(); // For resources that store state in non-exposed properties, such as via _validate_property or _get_property_list, this function must be implemented to clear them.
  106. virtual Error copy_from(const Ref<Resource> &p_resource);
  107. virtual void reload_from_file();
  108. void emit_changed();
  109. void connect_changed(const Callable &p_callable, uint32_t p_flags = 0);
  110. void disconnect_changed(const Callable &p_callable);
  111. void set_name(const String &p_name);
  112. String get_name() const;
  113. virtual void set_path(const String &p_path, bool p_take_over = false);
  114. String get_path() const;
  115. virtual void set_path_cache(const String &p_path); // Set raw path without involving resource cache.
  116. _FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.contains("::") || path_cache.begins_with("local://"); }
  117. static void seed_scene_unique_id(uint32_t p_seed);
  118. static String generate_scene_unique_id();
  119. void set_scene_unique_id(const String &p_id);
  120. String get_scene_unique_id() const;
  121. Ref<Resource> duplicate(bool p_deep = false) const;
  122. Ref<Resource> duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
  123. Ref<Resource> _duplicate_from_variant(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int p_recursion_count) const;
  124. static void _teardown_duplicate_from_variant();
  125. Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) const;
  126. void configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache);
  127. void set_local_to_scene(bool p_enable);
  128. bool is_local_to_scene() const;
  129. virtual void setup_local_to_scene();
  130. Node *get_local_scene() const;
  131. #ifdef TOOLS_ENABLED
  132. virtual uint32_t hash_edited_version_for_preview() const;
  133. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
  134. uint64_t get_last_modified_time() const { return last_modified_time; }
  135. virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; }
  136. uint64_t get_import_last_modified_time() const { return import_last_modified_time; }
  137. void set_import_path(const String &p_path) { import_path = p_path; }
  138. String get_import_path() const { return import_path; }
  139. #endif
  140. void set_as_translation_remapped(bool p_remapped);
  141. virtual RID get_rid() const; // Some resources may offer conversion to RID.
  142. // Helps keep IDs the same when loading/saving scenes. An empty ID clears the entry, and an empty ID is returned when not found.
  143. void set_id_for_path(const String &p_path, const String &p_id);
  144. String get_id_for_path(const String &p_path) const;
  145. Resource();
  146. ~Resource();
  147. };
  148. VARIANT_ENUM_CAST(Resource::DeepDuplicateMode);
  149. class ResourceCache {
  150. friend class Resource;
  151. friend class ResourceLoader; // Need the lock.
  152. static Mutex lock;
  153. static HashMap<String, Resource *> resources;
  154. #ifdef TOOLS_ENABLED
  155. static HashMap<String, HashMap<String, String>> resource_path_cache; // Each tscn has a set of resource paths and IDs.
  156. static RWLock path_cache_lock;
  157. #endif // TOOLS_ENABLED
  158. friend void unregister_core_types();
  159. static void clear();
  160. friend void register_core_types();
  161. public:
  162. static bool has(const String &p_path);
  163. static Ref<Resource> get_ref(const String &p_path);
  164. static void get_cached_resources(List<Ref<Resource>> *p_resources);
  165. static int get_cached_resource_count();
  166. };