resource.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* resource.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 <reduzio@gmail.com>
  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. virtual bool _use_builtin_script() const { return true; }
  82. #ifdef TOOLS_ENABLED
  83. Ref<ResourceImportMetadata> import_metadata;
  84. uint64_t last_modified_time;
  85. #endif
  86. protected:
  87. void emit_changed();
  88. void notify_change_to_owners();
  89. virtual void _resource_path_changed();
  90. static void _bind_methods();
  91. void _set_path(const String& p_path);
  92. void _take_over_path(const String& p_path);
  93. public:
  94. virtual bool can_reload_from_file();
  95. virtual void reload_from_file();
  96. void register_owner(Object *p_owner);
  97. void unregister_owner(Object *p_owner);
  98. void set_name(const String& p_name);
  99. String get_name() const;
  100. void set_path(const String& p_path,bool p_take_over=false);
  101. String get_path() const;
  102. Ref<Resource> duplicate(bool p_subresources=false);
  103. void set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata);
  104. Ref<ResourceImportMetadata> get_import_metadata() const;
  105. #ifdef TOOLS_ENABLED
  106. void set_last_modified_time(uint64_t p_time) { last_modified_time=p_time; }
  107. uint64_t get_last_modified_time() const { return last_modified_time; }
  108. #endif
  109. virtual RID get_rid() const; // some resources may offer conversion to RID
  110. Resource();
  111. ~Resource();
  112. };
  113. typedef Ref<Resource> RES;
  114. class ResourceCache {
  115. friend class Resource;
  116. static HashMap<String,Resource*> resources;
  117. friend void unregister_core_types();
  118. static void clear();
  119. public:
  120. static void reload_externals();
  121. static bool has(const String& p_path);
  122. static Resource* get(const String& p_path);
  123. static void dump(const char* p_file=NULL,bool p_short=false);
  124. static void get_cached_resources(List<Ref<Resource> > *p_resources);
  125. static int get_cached_resource_count();
  126. };
  127. #endif