resource.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*************************************************************************/
  2. /* resource.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef RESOURCE_H
  30. #define RESOURCE_H
  31. #include "object.h"
  32. #include "safe_refcount.h"
  33. #include "ref_ptr.h"
  34. #include "reference.h"
  35. #include "object_type_db.h"
  36. /**
  37. @author Juan Linietsky <[email protected]>
  38. */
  39. #define RES_BASE_EXTENSION(m_ext)\
  40. public:\
  41. static void register_custom_data_to_otdb() { ObjectTypeDB::add_resource_base_extension(m_ext,get_type_static()); }\
  42. virtual String get_base_extension() const { return m_ext; }\
  43. private:
  44. class ResourceImportMetadata : public Reference {
  45. OBJ_TYPE( ResourceImportMetadata, Reference );
  46. struct Source {
  47. String path;
  48. String md5;
  49. };
  50. Vector<Source> sources;
  51. String editor;
  52. Map<String,Variant> options;
  53. StringArray _get_options() const;
  54. protected:
  55. virtual bool _use_builtin_script() const { return false; }
  56. static void _bind_methods();
  57. public:
  58. void set_editor(const String& p_editor);
  59. String get_editor() const;
  60. void add_source(const String& p_path,const String& p_md5="");
  61. String get_source_path(int p_idx) const;
  62. String get_source_md5(int p_idx) const;
  63. void set_source_md5(int p_idx,const String& p_md5);
  64. void remove_source(int p_idx);
  65. int get_source_count() const;
  66. void set_option(const String& p_key, const Variant& p_value);
  67. Variant get_option(const String& p_key) const;
  68. bool has_option(const String& p_key) const;
  69. void get_options(List<String> *r_options) const;
  70. ResourceImportMetadata();
  71. };
  72. class Resource : public Reference {
  73. OBJ_TYPE( Resource, Reference );
  74. OBJ_CATEGORY("Resources");
  75. RES_BASE_EXTENSION("res");
  76. Set<ObjectID> owners;
  77. friend class ResBase;
  78. friend class ResourceCache;
  79. String name;
  80. String path_cache;
  81. int subindex;
  82. virtual bool _use_builtin_script() const { return true; }
  83. #ifdef TOOLS_ENABLED
  84. Ref<ResourceImportMetadata> import_metadata;
  85. uint64_t last_modified_time;
  86. #endif
  87. protected:
  88. void emit_changed();
  89. void notify_change_to_owners();
  90. virtual void _resource_path_changed();
  91. static void _bind_methods();
  92. void _set_path(const String& p_path);
  93. void _take_over_path(const String& p_path);
  94. public:
  95. virtual bool can_reload_from_file();
  96. virtual void reload_from_file();
  97. void register_owner(Object *p_owner);
  98. void unregister_owner(Object *p_owner);
  99. void set_name(const String& p_name);
  100. String get_name() const;
  101. virtual void set_path(const String& p_path,bool p_take_over=false);
  102. String get_path() const;
  103. void set_subindex(int p_sub_index);
  104. int get_subindex() const;
  105. Ref<Resource> duplicate(bool p_subresources=false);
  106. void set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata);
  107. Ref<ResourceImportMetadata> get_import_metadata() const;
  108. #ifdef TOOLS_ENABLED
  109. uint32_t hash_edited_version() const;
  110. virtual void set_last_modified_time(uint64_t p_time) { last_modified_time=p_time; }
  111. uint64_t get_last_modified_time() const { return last_modified_time; }
  112. #endif
  113. virtual RID get_rid() const; // some resources may offer conversion to RID
  114. Resource();
  115. ~Resource();
  116. };
  117. typedef Ref<Resource> RES;
  118. class ResourceCache {
  119. friend class Resource;
  120. static HashMap<String,Resource*> resources;
  121. friend void unregister_core_types();
  122. static void clear();
  123. public:
  124. static void reload_externals();
  125. static bool has(const String& p_path);
  126. static Resource* get(const String& p_path);
  127. static void dump(const char* p_file=NULL,bool p_short=false);
  128. static void get_cached_resources(List<Ref<Resource> > *p_resources);
  129. static int get_cached_resource_count();
  130. };
  131. #endif