resource.h 5.2 KB

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