resource.h
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 
32 #include "object.h"
33 #include "safe_refcount.h"
34 #include "ref_ptr.h"
35 #include "reference.h"
36 #include "object_type_db.h"
37 
42 #define RES_BASE_EXTENSION(m_ext)\
43 public:\
44 static void register_custom_data_to_otdb() { ObjectTypeDB::add_resource_base_extension(m_ext,get_type_static()); }\
45 virtual String get_base_extension() const { return m_ext; }\
46 private:
47 
48 
50 
51  OBJ_TYPE( ResourceImportMetadata, Reference );
52 
53  struct Source {
54  String path;
55  String md5;
56  };
57 
58  Vector<Source> sources;
59  String editor;
60 
61  Map<String,Variant> options;
62 
63  StringArray _get_options() const;
64 protected:
65  virtual bool _use_builtin_script() const { return false; }
66  static void _bind_methods();
67 public:
68 
69  void set_editor(const String& p_editor);
70  String get_editor() const;
71 
72  void add_source(const String& p_path,const String& p_md5="");
73  String get_source_path(int p_idx) const;
74  String get_source_md5(int p_idx) const;
75  void set_source_md5(int p_idx,const String& p_md5);
76  void remove_source(int p_idx);
77  int get_source_count() const;
78 
79  void set_option(const String& p_key, const Variant& p_value);
80  Variant get_option(const String& p_key) const;
81  bool has_option(const String& p_key) const;
82 
83  void get_options(List<String> *r_options) const;
84 
86 };
87 
88 
89 class Resource : public Reference {
90 
91  OBJ_TYPE( Resource, Reference );
92  OBJ_CATEGORY("Resources");
93  RES_BASE_EXTENSION("res");
94 
95  Set<ObjectID> owners;
96 
97 friend class ResBase;
98 friend class ResourceCache;
99 
100  String name;
101  String path_cache;
102  int subindex;
103 
104  virtual bool _use_builtin_script() const { return true; }
105 
106 #ifdef TOOLS_ENABLED
107  Ref<ResourceImportMetadata> import_metadata;
108  uint64_t last_modified_time;
109 #endif
110 
111 protected:
112 
113  void emit_changed();
114 
115  void notify_change_to_owners();
116 
117  virtual void _resource_path_changed();
118  static void _bind_methods();
119 
120  void _set_path(const String& p_path);
121  void _take_over_path(const String& p_path);
122 public:
123 
124  virtual bool can_reload_from_file();
125  virtual void reload_from_file();
126 
127  void register_owner(Object *p_owner);
128  void unregister_owner(Object *p_owner);
129 
130  void set_name(const String& p_name);
131  String get_name() const;
132 
133  virtual void set_path(const String& p_path,bool p_take_over=false);
134  String get_path() const;
135 
136  void set_subindex(int p_sub_index);
137  int get_subindex() const;
138 
139  Ref<Resource> duplicate(bool p_subresources=false);
140 
141  void set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata);
142  Ref<ResourceImportMetadata> get_import_metadata() const;
143 
144 
145 #ifdef TOOLS_ENABLED
146 
147  virtual void set_last_modified_time(uint64_t p_time) { last_modified_time=p_time; }
148  uint64_t get_last_modified_time() const { return last_modified_time; }
149 
150 #endif
151 
152  virtual RID get_rid() const; // some resources may offer conversion to RID
153 
154  Resource();
155  ~Resource();
156 
157 };
158 
159 
160 typedef Ref<Resource> RES;
161 
163 friend class Resource;
164  static HashMap<String,Resource*> resources;
165 friend void unregister_core_types();
166  static void clear();
167 public:
168 
169  static void reload_externals();
170  static bool has(const String& p_path);
171  static Resource* get(const String& p_path);
172  static void dump(const char* p_file=NULL,bool p_short=false);
173  static void get_cached_resources(List<Ref<Resource> > *p_resources);
174  static int get_cached_resource_count();
175 
176 };
177 
178 #endif
Definition: reference.h:78
Definition: variant.h:74
Definition: resource.h:49
Definition: reference.h:40
Definition: resource.h:89
Definition: rid.h:47
Definition: resource.h:162
Definition: dvector.h:43
Definition: ustring.h:64
Definition: object.h:317