Debian Codegraph @

scene/resources/packed_scene.h

raw symbols SCIP
1 /**************************************************************************/
2 /* packed_scene.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.h"
34 #include "scene/main/node.h"
35
36 class SceneState : public RefCounted {
37 GDCLASS(SceneState, RefCounted);
38
39 Vector<StringName> names;
40 Vector<Variant> variants;
41 Vector<NodePath> node_paths;
42 Vector<PackedInt32Array> id_paths;
43 mutable PackedInt32Array ids;
44 Vector<NodePath> editable_instances;
45 mutable HashMap<NodePath, int> node_path_cache;
46 mutable HashMap<int, int> base_scene_node_remap;
47
48 int base_scene_idx = -1;
49
50 enum {
51 NO_PARENT_SAVED = 0x7FFFFFFF,
52 NAME_INDEX_BITS = 18,
53 NAME_MASK = (1 << NAME_INDEX_BITS) - 1,
54 };
55
56 struct NodeData {
57 int parent = 0;
58 int owner = 0;
59 int type = 0;
60 int name = 0;
61 int instance = 0;
62 int index = 0;
63
64 struct Property {
65 int name = 0;
66 int value = 0;
67 };
68
69 Vector<Property> properties;
70 Vector<int> groups;
71 };
72
73 struct DeferredNodePathProperties {
74 ObjectID base;
75 StringName property;
76 Variant value;
77 };
78
79 Vector<NodeData> nodes;
80
81 struct ConnectionData {
82 int from = 0;
83 int to = 0;
84 int signal = 0;
85 int method = 0;
86 int flags = 0;
87 int unbinds = 0;
88 Vector<int> binds;
89 };
90
91 Vector<ConnectionData> connections;
92
93 Error _parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map, HashSet<int32_t> &ids_saved);
94 Error _parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map);
95
96 String path;
97
98 uint64_t last_modified_time = 0;
99
100 static bool disable_placeholders;
101
102 Vector<String> _get_node_groups(int p_idx) const;
103
104 int _find_base_scene_node_remap_key(int p_idx) const;
105
106 Node *_recover_node_path_index(Node *p_base, int p_idx) const;
107
108 #ifdef TOOLS_ENABLED
109 public:
110 typedef void (*InstantiationWarningNotify)(const String &p_warning);
111
112 private:
113 static InstantiationWarningNotify instantiation_warn_notify;
114 #endif
115
116 protected:
117 static void _bind_methods();
118
119 public:
120 enum {
121 FLAG_ID_IS_PATH = (1 << 30),
122 TYPE_INSTANTIATED = 0x7FFFFFFF,
123 FLAG_INSTANCE_IS_PLACEHOLDER = (1 << 30),
124 FLAG_PATH_PROPERTY_IS_NODE = (1 << 30),
125 FLAG_PROP_NAME_MASK = FLAG_PATH_PROPERTY_IS_NODE - 1,
126 FLAG_MASK = (1 << 24) - 1,
127 };
128
129 enum GenEditState {
130 GEN_EDIT_STATE_DISABLED,
131 GEN_EDIT_STATE_INSTANCE,
132 GEN_EDIT_STATE_MAIN,
133 GEN_EDIT_STATE_MAIN_INHERITED,
134 };
135
136 struct PackState {
137 Ref<SceneState> state;
138 int node = -1;
139 };
140
141 static void set_disable_placeholders(bool p_disable);
142 static Ref<Resource> get_remap_resource(const Ref<Resource> &p_resource, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &remap_cache, const Ref<Resource> &p_fallback, Node *p_for_scene);
143
144 int find_node_by_path(const NodePath &p_node) const;
145 Variant get_property_value(int p_node, const StringName &p_property, bool &r_found, bool &r_node_deferred) const;
146 bool is_node_in_group(int p_node, const StringName &p_group) const;
147 bool is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const;
148
149 void set_bundled_scene(const Dictionary &p_dictionary);
150 Dictionary get_bundled_scene() const;
151
152 Error pack(Node *p_scene);
153
154 void set_path(const String &p_path);
155 String get_path() const;
156
157 void clear();
158 Error copy_from(const Ref<SceneState> &p_scene_state);
159
160 bool can_instantiate() const;
161 Node *instantiate(GenEditState p_edit_state) const;
162
163 Array setup_resources_in_array(Array &array_to_scan, const SceneState::NodeData &n, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resources_local_to_scenes, Node *node, const StringName sname, int i, Node **ret_nodes, SceneState::GenEditState p_edit_state) const;
164 Dictionary setup_resources_in_dictionary(Dictionary &p_dictionary_to_scan, const SceneState::NodeData &p_n, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resources_local_to_scenes, Node *p_node, const StringName p_sname, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const;
165 Variant make_local_resource(Variant &value, const SceneState::NodeData &p_node_data, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resources_local_to_scenes, Node *p_node, const StringName p_sname, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const;
166 bool has_local_resource(const Array &p_array) const;
167
168 Ref<SceneState> get_base_scene_state() const;
169
170 void update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene);
171
172 //unbuild API
173
174 int get_node_count() const;
175 StringName get_node_type(int p_idx) const;
176 StringName get_node_name(int p_idx) const;
177 NodePath get_node_path(int p_idx, bool p_for_parent = false) const;
178 int32_t get_node_unique_id(int p_idx) const;
179 PackedInt32Array get_node_id_path(int p_idx) const;
180 PackedInt32Array get_node_parent_id_path(int p_idx) const;
181 NodePath get_node_owner_path(int p_idx) const;
182 PackedInt32Array get_node_owner_id_path(int p_idx) const;
183 Ref<PackedScene> get_node_instance(int p_idx) const;
184 String get_node_instance_placeholder(int p_idx) const;
185 bool is_node_instance_placeholder(int p_idx) const;
186 Vector<StringName> get_node_groups(int p_idx) const;
187 int get_node_index(int p_idx) const;
188
189 int get_node_property_count(int p_idx) const;
190 StringName get_node_property_name(int p_idx, int p_prop) const;
191 Variant get_node_property_value(int p_idx, int p_prop) const;
192 Vector<String> get_node_deferred_nodepath_properties(int p_idx) const;
193
194 int get_connection_count() const;
195 NodePath get_connection_source(int p_idx) const;
196 StringName get_connection_signal(int p_idx) const;
197 NodePath get_connection_target(int p_idx) const;
198 StringName get_connection_method(int p_idx) const;
199
200 PackedInt32Array get_connection_source_id_path(int p_idx) const;
201 PackedInt32Array get_connection_target_id_path(int p_idx) const;
202
203 int get_connection_flags(int p_idx) const;
204 int get_connection_unbinds(int p_idx) const;
205 Array get_connection_binds(int p_idx) const;
206
207 bool has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance = false);
208
209 Vector<NodePath> get_editable_instances() const;
210 Ref<Resource> get_sub_resource(const String &p_path);
211 Vector<Ref<Resource>> get_sub_resources();
212
213 //build API
214
215 int add_name(const StringName &p_name);
216 int add_value(const Variant &p_value);
217 int add_node_path(const NodePath &p_path, const PackedInt32Array &p_uid_path);
218 int add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index, int32_t p_unique_id);
219 void add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path = false);
220 void add_node_group(int p_node, int p_group);
221 void set_base_scene(int p_idx);
222 void add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, int p_unbinds, const Vector<int> &p_binds);
223 void add_editable_instance(const NodePath &p_path);
224
225 bool remove_group_references(const StringName &p_name);
226 bool rename_group_references(const StringName &p_old_name, const StringName &p_new_name);
227 HashSet<StringName> get_all_groups();
228
229 virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
230 uint64_t get_last_modified_time() const { return last_modified_time; }
231
232 // Used when saving pointers (saves a path property instead).
233 static String get_meta_pointer_property(const String &p_property);
234
235 #ifdef TOOLS_ENABLED
236 static void set_instantiation_warning_notify_func(InstantiationWarningNotify p_warn_notify) { instantiation_warn_notify = p_warn_notify; }
237 #endif
238
239 SceneState();
240 };
241
242 VARIANT_ENUM_CAST(SceneState::GenEditState)
243
244 class PackedScene : public Resource {
245 GDCLASS(PackedScene, Resource);
246 RES_BASE_EXTENSION("scn");
247
248 Ref<SceneState> state;
249
250 void _set_bundled_scene(const Dictionary &p_scene);
251 Dictionary _get_bundled_scene() const;
252
253 protected:
254 virtual bool editor_can_reload_from_file() override { return false; } // this is handled by editor better
255 static void _bind_methods();
256 virtual void reset_state() override;
257
258 public:
259 enum GenEditState {
260 GEN_EDIT_STATE_DISABLED,
261 GEN_EDIT_STATE_INSTANCE,
262 GEN_EDIT_STATE_MAIN,
263 GEN_EDIT_STATE_MAIN_INHERITED,
264 };
265
266 Error pack(Node *p_scene);
267
268 void clear();
269
270 bool can_instantiate() const;
271 Node *instantiate(GenEditState p_edit_state = GEN_EDIT_STATE_DISABLED) const;
272
273 void recreate_state();
274 void replace_state(Ref<SceneState> p_by);
275
276 virtual void reload_from_file() override;
277
278 virtual void set_path(const String &p_path, bool p_take_over = false) override;
279 virtual void set_path_cache(const String &p_path) override;
280
281 #ifdef TOOLS_ENABLED
282 virtual void set_last_modified_time(uint64_t p_time) override {
283 Resource::set_last_modified_time(p_time);
284 state->set_last_modified_time(p_time);
285 }
286
287 static HashSet<StringName> get_scene_groups(const String &p_path);
288 #endif
289 Ref<SceneState> get_state() const;
290
291 PackedScene();
292 };
293
294 VARIANT_ENUM_CAST(PackedScene::GenEditState)