resource.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef RESOURCE_H
  31. #define RESOURCE_H
  32. #include "core/io/resource_uid.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/gdvirtual.gen.inc"
  35. #include "core/object/ref_counted.h"
  36. #include "core/templates/safe_refcount.h"
  37. #include "core/templates/self_list.h"
  38. class Node;
  39. #define RES_BASE_EXTENSION(m_ext) \
  40. public: \
  41. static void register_custom_data_to_otdb() { \
  42. ClassDB::add_resource_base_extension(m_ext, get_class_static()); \
  43. } \
  44. virtual String get_base_extension() const override { \
  45. return m_ext; \
  46. } \
  47. \
  48. private:
  49. class Resource : public RefCounted {
  50. GDCLASS(Resource, RefCounted);
  51. public:
  52. static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); }
  53. virtual String get_base_extension() const { return "res"; }
  54. private:
  55. friend class ResBase;
  56. friend class ResourceCache;
  57. String name;
  58. String path_cache;
  59. String scene_unique_id;
  60. #ifdef TOOLS_ENABLED
  61. uint64_t last_modified_time = 0;
  62. uint64_t import_last_modified_time = 0;
  63. String import_path;
  64. #endif
  65. bool local_to_scene = false;
  66. friend class SceneState;
  67. Node *local_scene = nullptr;
  68. SelfList<Resource> remapped_list;
  69. void _dupe_sub_resources(Variant &r_variant, Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache);
  70. void _find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found);
  71. protected:
  72. virtual void _resource_path_changed();
  73. static void _bind_methods();
  74. void _set_path(const String &p_path);
  75. void _take_over_path(const String &p_path);
  76. virtual void reset_local_to_scene();
  77. GDVIRTUAL0(_setup_local_to_scene);
  78. GDVIRTUAL0RC(RID, _get_rid);
  79. GDVIRTUAL1C(_set_path_cache, String);
  80. GDVIRTUAL0(_reset_state);
  81. public:
  82. static Node *(*_get_local_scene_func)(); //used by editor
  83. static void (*_update_configuration_warning)(); //used by editor
  84. void update_configuration_warning();
  85. virtual bool editor_can_reload_from_file();
  86. virtual void reset_state(); //for resources that use variable amount of properties, either via _validate_property or _get_property_list, this function needs to be implemented to correctly clear state
  87. virtual Error copy_from(const Ref<Resource> &p_resource);
  88. virtual void reload_from_file();
  89. void emit_changed();
  90. void connect_changed(const Callable &p_callable, uint32_t p_flags = 0);
  91. void disconnect_changed(const Callable &p_callable);
  92. void set_name(const String &p_name);
  93. String get_name() const;
  94. virtual void set_path(const String &p_path, bool p_take_over = false);
  95. String get_path() const;
  96. virtual void set_path_cache(const String &p_path); // Set raw path without involving resource cache.
  97. _FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.contains("::") || path_cache.begins_with("local://"); }
  98. static void seed_scene_unique_id(uint32_t p_seed);
  99. static String generate_scene_unique_id();
  100. void set_scene_unique_id(const String &p_id);
  101. String get_scene_unique_id() const;
  102. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  103. Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache);
  104. void configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache);
  105. void set_local_to_scene(bool p_enable);
  106. bool is_local_to_scene() const;
  107. virtual void setup_local_to_scene();
  108. Node *get_local_scene() const;
  109. #ifdef TOOLS_ENABLED
  110. virtual uint32_t hash_edited_version_for_preview() const;
  111. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
  112. uint64_t get_last_modified_time() const { return last_modified_time; }
  113. virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; }
  114. uint64_t get_import_last_modified_time() const { return import_last_modified_time; }
  115. void set_import_path(const String &p_path) { import_path = p_path; }
  116. String get_import_path() const { return import_path; }
  117. #endif
  118. void set_as_translation_remapped(bool p_remapped);
  119. virtual RID get_rid() const; // some resources may offer conversion to RID
  120. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  121. void set_id_for_path(const String &p_path, const String &p_id);
  122. String get_id_for_path(const String &p_path) const;
  123. Resource();
  124. ~Resource();
  125. };
  126. class ResourceCache {
  127. friend class Resource;
  128. friend class ResourceLoader; //need the lock
  129. static Mutex lock;
  130. static HashMap<String, Resource *> resources;
  131. #ifdef TOOLS_ENABLED
  132. static HashMap<String, HashMap<String, String>> resource_path_cache; // Each tscn has a set of resource paths and IDs.
  133. static RWLock path_cache_lock;
  134. #endif // TOOLS_ENABLED
  135. friend void unregister_core_types();
  136. static void clear();
  137. friend void register_core_types();
  138. public:
  139. static bool has(const String &p_path);
  140. static Ref<Resource> get_ref(const String &p_path);
  141. static void get_cached_resources(List<Ref<Resource>> *p_resources);
  142. static int get_cached_resource_count();
  143. };
  144. #endif // RESOURCE_H