Debian Codegraph @

core/io/resource.h

raw symbols SCIP
1 /**************************************************************************/
2 /* resource.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31 #pragma once
32
33 #include "core/io/resource_uid.h"
34 #include "core/object/class_db.h"
35 #include "core/object/gdvirtual.gen.inc"
36 #include "core/object/ref_counted.h"
37 #include "core/templates/safe_refcount.h"
38 #include "core/templates/self_list.h"
39
40 class Node;
41
42 #define RES_BASE_EXTENSION(m_ext) \
43 public: \
44 static void register_custom_data_to_otdb() { \
45 ClassDB::add_resource_base_extension(m_ext, get_class_static()); \
46 } \
47 virtual String get_base_extension() const override { \
48 return m_ext; \
49 } \
50 \
51 private:
52
53 class Resource : public RefCounted {
54 GDCLASS(Resource, RefCounted);
55
56 public:
57 static constexpr AncestralClass static_ancestral_class = AncestralClass::RESOURCE;
58
59 static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); }
60 virtual String get_base_extension() const { return "res"; }
61
62 protected:
63 struct DuplicateParams {
64 bool deep = false;
65 ResourceDeepDuplicateMode subres_mode = RESOURCE_DEEP_DUPLICATE_MAX;
66 Node *local_scene = nullptr;
67 };
68
69 private:
70 friend class ResBase;
71 friend class ResourceCache;
72
73 String name;
74 String path_cache;
75 String scene_unique_id;
76
77 #ifdef TOOLS_ENABLED
78 uint64_t last_modified_time = 0;
79 uint64_t import_last_modified_time = 0;
80 String import_path;
81 #endif
82
83 enum EmitChangedState {
84 EMIT_CHANGED_UNBLOCKED,
85 EMIT_CHANGED_BLOCKED,
86 EMIT_CHANGED_BLOCKED_PENDING_EMIT,
87 };
88 EmitChangedState emit_changed_state = EMIT_CHANGED_UNBLOCKED;
89 bool local_to_scene = false;
90 friend class SceneState;
91 Node *local_scene = nullptr;
92
93 SelfList<Resource> remapped_list;
94
95 using DuplicateRemapCacheT = HashMap<Ref<Resource>, Ref<Resource>>;
96 static thread_local inline DuplicateRemapCacheT *thread_duplicate_remap_cache = nullptr;
97 static thread_local inline bool thread_duplicate_remap_cache_needs_deallocation = true;
98
99 Variant _duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage = 0) const;
100 void _find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found);
101
102 // Only for binding the deep duplicate method, so it doesn't need actual members.
103 enum DeepDuplicateMode : int;
104
105 _ALWAYS_INLINE_ Ref<Resource> _duplicate_deep_bind(DeepDuplicateMode p_deep_subresources_mode) const;
106
107 protected:
108 virtual void _resource_path_changed();
109 static void _bind_methods();
110
111 void _block_emit_changed();
112 void _unblock_emit_changed();
113
114 void _set_path(const String &p_path);
115 void _take_over_path(const String &p_path);
116
117 virtual void reset_local_to_scene();
118 GDVIRTUAL0(_setup_local_to_scene);
119
120 GDVIRTUAL0RC(RID, _get_rid);
121
122 GDVIRTUAL1C(_set_path_cache, String);
123 GDVIRTUAL0(_reset_state);
124
125 virtual Ref<Resource> _duplicate(const DuplicateParams &p_params) const;
126 virtual String _to_string() override;
127
128 public:
129 static Node *(*_get_local_scene_func)(); // Used by the editor.
130 static void (*_update_configuration_warning)(); // Used by the editor.
131
132 void update_configuration_warning();
133 virtual bool editor_can_reload_from_file();
134 virtual void reset_state(); // For resources that store state in non-exposed properties, such as via _validate_property or _get_property_list, this function must be implemented to clear them.
135 virtual Error copy_from(const Ref<Resource> &p_resource);
136 virtual void reload_from_file();
137
138 void emit_changed();
139 void connect_changed(const Callable &p_callable, uint32_t p_flags = 0);
140 void disconnect_changed(const Callable &p_callable);
141
142 void set_name(const String &p_name);
143 String get_name() const;
144
145 virtual void set_path(const String &p_path, bool p_take_over = false);
146 String get_path() const;
147 virtual void set_path_cache(const String &p_path); // Set raw path without involving resource cache.
148 _FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.contains("::") || path_cache.begins_with("local://"); }
149
150 static void seed_scene_unique_id(uint32_t p_seed);
151 static String generate_scene_unique_id();
152 void set_scene_unique_id(const String &p_id);
153 String get_scene_unique_id() const;
154
155 Ref<Resource> duplicate(bool p_deep = false) const;
156 Ref<Resource> duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
157 Ref<Resource> _duplicate_from_variant(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int p_recursion_count) const;
158 static void _teardown_duplicate_from_variant();
159 Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) const;
160 void configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache);
161
162 void set_local_to_scene(bool p_enable);
163 bool is_local_to_scene() const;
164 virtual void setup_local_to_scene();
165
166 Node *get_local_scene() const;
167
168 #ifdef TOOLS_ENABLED
169
170 virtual uint32_t hash_edited_version_for_preview() const;
171
172 virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
173 uint64_t get_last_modified_time() const { return last_modified_time; }
174
175 virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; }
176 uint64_t get_import_last_modified_time() const { return import_last_modified_time; }
177
178 void set_import_path(const String &p_path) { import_path = p_path; }
179 String get_import_path() const { return import_path; }
180
181 #endif
182
183 void set_as_translation_remapped(bool p_remapped);
184
185 virtual RID get_rid() const; // Some resources may offer conversion to RID.
186
187 // Helps keep IDs the same when loading/saving scenes. An empty ID clears the entry, and an empty ID is returned when not found.
188 static void set_resource_id_for_path(const String &p_referrer_path, const String &p_resource_path, const String &p_id);
189 void set_id_for_path(const String &p_referrer_path, const String &p_id) { set_resource_id_for_path(p_referrer_path, get_path(), p_id); }
190 String get_id_for_path(const String &p_referrer_path) const;
191
192 Resource();
193 ~Resource();
194 };
195
196 VARIANT_ENUM_CAST(Resource::DeepDuplicateMode);
197
198 class ResourceCache {
199 friend class Resource;
200 friend class ResourceLoader; // Need the lock.
201 static Mutex lock;
202 static HashMap<String, Resource *> resources;
203 #ifdef TOOLS_ENABLED
204 static HashMap<String, HashMap<String, String>> resource_path_cache; // Each tscn has a set of resource paths and IDs.
205 static RWLock path_cache_lock;
206 #endif // TOOLS_ENABLED
207 friend void unregister_core_types();
208 static void clear();
209 friend void register_core_types();
210
211 public:
212 static bool has(const String &p_path);
213 static Ref<Resource> get_ref(const String &p_path);
214 static void get_cached_resources(List<Ref<Resource>> *p_resources);
215 static int get_cached_resource_count();
216 };