resource.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* resource.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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/class_db.h"
  33. #include "core/object.h"
  34. #include "core/ref_ptr.h"
  35. #include "core/reference.h"
  36. #include "core/safe_refcount.h"
  37. #include "core/self_list.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. #define RES_BASE_EXTENSION(m_ext) \
  42. public: \
  43. static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \
  44. virtual String get_base_extension() const { return m_ext; } \
  45. \
  46. private:
  47. class Resource : public Reference {
  48. GDCLASS(Resource, Reference);
  49. OBJ_CATEGORY("Resources");
  50. RES_BASE_EXTENSION("res");
  51. Set<ObjectID> owners;
  52. friend class ResBase;
  53. friend class ResourceCache;
  54. String name;
  55. String path_cache;
  56. int subindex;
  57. virtual bool _use_builtin_script() const { return true; }
  58. #ifdef TOOLS_ENABLED
  59. uint64_t last_modified_time;
  60. uint64_t import_last_modified_time;
  61. String import_path;
  62. #endif
  63. bool local_to_scene;
  64. friend class SceneState;
  65. Node *local_scene;
  66. SelfList<Resource> remapped_list;
  67. protected:
  68. void emit_changed();
  69. void notify_change_to_owners();
  70. virtual void _resource_path_changed();
  71. static void _bind_methods();
  72. void _set_path(const String &p_path);
  73. void _take_over_path(const String &p_path);
  74. public:
  75. static Node *(*_get_local_scene_func)(); //used by editor
  76. virtual bool editor_can_reload_from_file();
  77. virtual void reload_from_file();
  78. void register_owner(Object *p_owner);
  79. void unregister_owner(Object *p_owner);
  80. void set_name(const String &p_name);
  81. String get_name() const;
  82. virtual void set_path(const String &p_path, bool p_take_over = false);
  83. String get_path() const;
  84. void set_subindex(int p_sub_index);
  85. int get_subindex() const;
  86. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  87. Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache);
  88. void configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache);
  89. void set_local_to_scene(bool p_enable);
  90. bool is_local_to_scene() const;
  91. virtual void setup_local_to_scene();
  92. Node *get_local_scene() const;
  93. #ifdef TOOLS_ENABLED
  94. uint32_t hash_edited_version() const;
  95. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
  96. uint64_t get_last_modified_time() const { return last_modified_time; }
  97. virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; }
  98. uint64_t get_import_last_modified_time() const { return import_last_modified_time; }
  99. void set_import_path(const String &p_path) { import_path = p_path; }
  100. String get_import_path() const { return import_path; }
  101. #endif
  102. void set_as_translation_remapped(bool p_remapped);
  103. bool is_translation_remapped() const;
  104. virtual RID get_rid() const; // some resources may offer conversion to RID
  105. Resource();
  106. ~Resource();
  107. };
  108. typedef Ref<Resource> RES;
  109. class ResourceCache {
  110. friend class Resource;
  111. friend class ResourceLoader; //need the lock
  112. static RWLock *lock;
  113. static HashMap<String, Resource *> resources;
  114. friend void unregister_core_types();
  115. static void clear();
  116. friend void register_core_types();
  117. static void setup();
  118. public:
  119. static void reload_externals();
  120. static bool has(const String &p_path);
  121. static Resource *get(const String &p_path);
  122. static void dump(const char *p_file = NULL, bool p_short = false);
  123. static void get_cached_resources(List<Ref<Resource> > *p_resources);
  124. static int get_cached_resource_count();
  125. };
  126. #endif