resource.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************/
  2. /* resource.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "object.h"
  33. #include "object_type_db.h"
  34. #include "ref_ptr.h"
  35. #include "reference.h"
  36. #include "safe_refcount.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. #define RES_BASE_EXTENSION(m_ext) \
  41. public: \
  42. static void register_custom_data_to_otdb() { ObjectTypeDB::add_resource_base_extension(m_ext, get_type_static()); } \
  43. virtual String get_base_extension() const { return m_ext; } \
  44. \
  45. private:
  46. class ResourceImportMetadata : public Reference {
  47. OBJ_TYPE(ResourceImportMetadata, Reference);
  48. struct Source {
  49. String path;
  50. String md5;
  51. };
  52. Vector<Source> sources;
  53. String editor;
  54. Map<String, Variant> options;
  55. StringArray _get_options() const;
  56. protected:
  57. virtual bool _use_builtin_script() const { return false; }
  58. static void _bind_methods();
  59. public:
  60. void set_editor(const String &p_editor);
  61. String get_editor() const;
  62. void add_source(const String &p_path, const String &p_md5 = "");
  63. String get_source_path(int p_idx) const;
  64. String get_source_md5(int p_idx) const;
  65. void set_source_md5(int p_idx, const String &p_md5);
  66. void remove_source(int p_idx);
  67. int get_source_count() const;
  68. void set_option(const String &p_key, const Variant &p_value);
  69. Variant get_option(const String &p_key) const;
  70. bool has_option(const String &p_key) const;
  71. void get_options(List<String> *r_options) const;
  72. ResourceImportMetadata();
  73. };
  74. class Resource : public Reference {
  75. OBJ_TYPE(Resource, Reference);
  76. OBJ_CATEGORY("Resources");
  77. RES_BASE_EXTENSION("res");
  78. Set<ObjectID> owners;
  79. friend class ResBase;
  80. friend class ResourceCache;
  81. String name;
  82. String path_cache;
  83. int subindex;
  84. virtual bool _use_builtin_script() const { return true; }
  85. #ifdef TOOLS_ENABLED
  86. Ref<ResourceImportMetadata> import_metadata;
  87. uint64_t last_modified_time;
  88. #endif
  89. protected:
  90. void emit_changed();
  91. void notify_change_to_owners();
  92. virtual void _resource_path_changed();
  93. static void _bind_methods();
  94. void _set_path(const String &p_path);
  95. void _take_over_path(const String &p_path);
  96. public:
  97. virtual bool editor_can_reload_from_file();
  98. virtual void reload_from_file();
  99. void register_owner(Object *p_owner);
  100. void unregister_owner(Object *p_owner);
  101. void set_name(const String &p_name);
  102. String get_name() const;
  103. virtual void set_path(const String &p_path, bool p_take_over = false);
  104. String get_path() const;
  105. void set_subindex(int p_sub_index);
  106. int get_subindex() const;
  107. Ref<Resource> duplicate(bool p_subresources = false);
  108. void set_import_metadata(const Ref<ResourceImportMetadata> &p_metadata);
  109. Ref<ResourceImportMetadata> get_import_metadata() const;
  110. #ifdef TOOLS_ENABLED
  111. uint32_t hash_edited_version() const;
  112. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
  113. uint64_t get_last_modified_time() const { return last_modified_time; }
  114. #endif
  115. virtual RID get_rid() const; // some resources may offer conversion to RID
  116. Resource();
  117. ~Resource();
  118. };
  119. typedef Ref<Resource> RES;
  120. class ResourceCache {
  121. friend class Resource;
  122. static HashMap<String, Resource *> resources;
  123. friend void unregister_core_types();
  124. static void clear();
  125. public:
  126. static void reload_externals();
  127. static bool has(const String &p_path);
  128. static Resource *get(const String &p_path);
  129. static void dump(const char *p_file = NULL, bool p_short = false);
  130. static void get_cached_resources(List<Ref<Resource> > *p_resources);
  131. static int get_cached_resource_count();
  132. };
  133. #endif