1/**************************************************************************/
2/* packed_scene.cpp */
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#include "packed_scene.h"
32
33#include "core/config/engine.h"
34#include "core/config/project_settings.h"
35#include "core/core_string_names.h"
36#include "core/io/missing_resource.h"
37#include "core/io/resource_loader.h"
38#include "core/templates/local_vector.h"
39#include "scene/2d/node_2d.h"
40#include "scene/3d/node_3d.h"
41#include "scene/gui/control.h"
42#include "scene/main/instance_placeholder.h"
43#include "scene/main/missing_node.h"
44#include "scene/property_utils.h"
45
46#define PACKED_SCENE_VERSION 3
47
48#ifdef TOOLS_ENABLED
49SceneState::InstantiationWarningNotify SceneState::instantiation_warn_notify = nullptr;
50#endif
51
52bool SceneState::can_instantiate() const {
53 return nodes.size() > 0;
54}
55
56static Array _sanitize_node_pinned_properties(Node *p_node) {
57 Array pinned = p_node->get_meta("_edit_pinned_properties_", Array());
58 if (pinned.is_empty()) {
59 return Array();
60 }
61 HashSet<StringName> storable_properties;
62 p_node->get_storable_properties(storable_properties);
63 int i = 0;
64 do {
65 if (storable_properties.has(pinned[i])) {
66 i++;
67 } else {
68 pinned.remove_at(i);
69 }
70 } while (i < pinned.size());
71 if (pinned.is_empty()) {
72 p_node->remove_meta("_edit_pinned_properties_");
73 }
74 return pinned;
75}
76
77Ref<Resource> SceneState::get_remap_resource(const Ref<Resource> &p_resource, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache, const Ref<Resource> &p_fallback, Node *p_for_scene) {
78 ERR_FAIL_COND_V(p_resource.is_null(), Ref<Resource>());
79
80 Ref<Resource> remap_resource;
81
82 // Find the shared copy of the source resource.
83 HashMap<Ref<Resource>, Ref<Resource>>::Iterator R = remap_cache.find(p_resource);
84 if (R) {
85 remap_resource = R->value;
86 } else if (p_fallback.is_valid() && p_fallback->is_local_to_scene() && p_fallback->get_class() == p_resource->get_class()) {
87 // Simply copy the data from the source resource to update the fallback resource that was previously set.
88
89 p_fallback->reset_state(); // May want to reset state.
90
91 List<PropertyInfo> pi;
92 p_resource->get_property_list(&pi);
93 for (const PropertyInfo &E : pi) {
94 if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
95 continue;
96 }
97 if (E.name == "resource_path") {
98 continue; // Do not change path.
99 }
100
101 Variant value = p_resource->get(E.name);
102
103 // The local-to-scene subresource instance is preserved, thus maintaining the previous sharing relationship.
104 // This is mainly used when the sub-scene root is reset in the main scene.
105 Ref<Resource> sub_res_of_from = value;
106 if (sub_res_of_from.is_valid() && sub_res_of_from->is_local_to_scene()) {
107 value = get_remap_resource(sub_res_of_from, remap_cache, p_fallback->get(E.name), p_fallback->get_local_scene());
108 }
109
110 p_fallback->set(E.name, value);
111 }
112
113 p_fallback->set_scene_unique_id(p_resource->get_scene_unique_id()); // Get the id from the main scene, in case the id changes again when saving the scene.
114
115 remap_cache[p_resource] = p_fallback;
116 remap_resource = p_fallback;
117 } else { // A copy of the source resource is required to overwrite the previous one.
118 Ref<Resource> local_dupe = p_resource->duplicate_for_local_scene(p_for_scene, remap_cache);
119 remap_cache[p_resource] = local_dupe;
120 remap_resource = local_dupe;
121 }
122
123 return remap_resource;
124}
125
126Node *SceneState::instantiate(GenEditState p_edit_state) const {
127 // Nodes where instantiation failed (because something is missing.)
128 List<Node *> stray_instances;
129
130#define NODE_FROM_ID(p_name, p_id) \
131 Node *p_name; \
132 if (p_id & FLAG_ID_IS_PATH) { \
133 NodePath np = node_paths[p_id & FLAG_MASK]; \
134 p_name = ret_nodes[0]->get_node_or_null(np); \
135 } else { \
136 ERR_FAIL_INDEX_V(p_id &FLAG_MASK, nc, nullptr); \
137 p_name = ret_nodes[p_id & FLAG_MASK]; \
138 }
139
140 int nc = nodes.size();
141 ERR_FAIL_COND_V(nc == 0, nullptr);
142
143 const StringName *snames = nullptr;
144 int sname_count = names.size();
145 if (sname_count) {
146 snames = &names[0];
147 }
148
149 const Variant *props = nullptr;
150 int prop_count = variants.size();
151 if (prop_count) {
152 props = &variants[0];
153 }
154
155 //Vector<Variant> properties;
156
157 const NodeData *nd = &nodes[0];
158
159 Node **ret_nodes = (Node **)alloca(sizeof(Node *) * nc);
160
161 bool gen_node_path_cache = p_edit_state != GEN_EDIT_STATE_DISABLED && node_path_cache.is_empty();
162
163 HashMap<Ref<Resource>, Ref<Resource>> resources_local_to_scene;
164
165 LocalVector<DeferredNodePathProperties> deferred_node_paths;
166
167 for (int i = 0; i < nc; i++) {
168 const NodeData &n = nd[i];
169
170 Node *parent = nullptr;
171 String old_parent_path;
172
173 if (i > 0) {
174 ERR_FAIL_COND_V_MSG(n.parent == -1, nullptr, vformat("Invalid scene: node %s does not specify its parent node.", snames[n.name]));
175 NODE_FROM_ID(nparent, n.parent);
176#ifdef DEBUG_ENABLED
177 if (!nparent && (n.parent & FLAG_ID_IS_PATH)) {
178 WARN_PRINT(String("Parent path '" + String(node_paths[n.parent & FLAG_MASK]) + "' for node '" + String(snames[n.name]) + "' has vanished when instantiating: '" + get_path() + "'.").ascii().get_data());
179 old_parent_path = String(node_paths[n.parent & FLAG_MASK]).trim_prefix("./").replace("/", "@");
180 nparent = ret_nodes[0];
181 }
182#endif
183 parent = nparent;
184 } else {
185 // i == 0 is root node.
186 ERR_FAIL_COND_V_MSG(n.parent != -1, nullptr, vformat("Invalid scene: root node %s cannot specify a parent node.", snames[n.name]));
187 ERR_FAIL_COND_V_MSG(n.type == TYPE_INSTANTIATED && base_scene_idx < 0, nullptr, vformat("Invalid scene: root node %s in an instance, but there's no base scene.", snames[n.name]));
188 }
189
190 Node *node = nullptr;
191 MissingNode *missing_node = nullptr;
192
193 if (i == 0 && base_scene_idx >= 0) {
194 //scene inheritance on root node
195 Ref<PackedScene> sdata = props[base_scene_idx];
196 ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
197 node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE); //only main gets main edit state
198 ERR_FAIL_NULL_V(node, nullptr);
199 if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
200 node->set_scene_inherited_state(sdata->get_state());
201 }
202
203 } else if (n.instance >= 0) {
204 //instance a scene into this node
205 if (n.instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
206 String scene_path = props[n.instance & FLAG_MASK];
207 if (disable_placeholders) {
208 Ref<PackedScene> sdata = ResourceLoader::load(scene_path, "PackedScene");
209 ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
210 node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
211 ERR_FAIL_NULL_V(node, nullptr);
212 } else {
213 InstancePlaceholder *ip = memnew(InstancePlaceholder);
214 ip->set_instance_path(scene_path);
215 node = ip;
216 }
217 node->set_scene_instance_load_placeholder(true);
218 } else {
219 Ref<PackedScene> sdata = props[n.instance & FLAG_MASK];
220 ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
221 node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
222 ERR_FAIL_NULL_V(node, nullptr);
223 }
224
225 } else if (n.type == TYPE_INSTANTIATED) {
226 //get the node from somewhere, it likely already exists from another instance
227 if (parent) {
228 node = parent->_get_child_by_name(snames[n.name]);
229#ifdef DEBUG_ENABLED
230 if (!node) {
231 WARN_PRINT(String("Node '" + String(ret_nodes[0]->get_path_to(parent)) + "/" + String(snames[n.name]) + "' was modified from inside an instance, but it has vanished.").ascii().get_data());
232 }
233#endif
234 }
235 } else {
236 //node belongs to this scene and must be created
237 Object *obj = ClassDB::instantiate(snames[n.type]);
238
239 node = Object::cast_to<Node>(obj);
240
241 if (!node) {
242 if (obj) {
243 memdelete(obj);
244 obj = nullptr;
245 }
246
247 if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
248 missing_node = memnew(MissingNode);
249 missing_node->set_original_class(snames[n.type]);
250 missing_node->set_recording_properties(true);
251 node = missing_node;
252 obj = missing_node;
253 } else {
254 WARN_PRINT(vformat("Node %s of type %s cannot be created. A placeholder will be created instead.", snames[n.name], snames[n.type]).ascii().get_data());
255 if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
256 if (Object::cast_to<Control>(ret_nodes[n.parent])) {
257 obj = memnew(Control);
258 } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
259 obj = memnew(Node2D);
260#ifndef _3D_DISABLED
261 } else if (Object::cast_to<Node3D>(ret_nodes[n.parent])) {
262 obj = memnew(Node3D);
263#endif // _3D_DISABLED
264 }
265 }
266
267 if (!obj) {
268 obj = memnew(Node);
269 }
270
271 node = Object::cast_to<Node>(obj);
272 }
273 }
274 }
275
276 if (node) {
277 // may not have found the node (part of instantiated scene and removed)
278 // if found all is good, otherwise ignore
279
280 //properties
281 int nprop_count = n.properties.size();
282 if (nprop_count) {
283 const NodeData::Property *nprops = &n.properties[0];
284
285 Dictionary missing_resource_properties;
286 HashMap<Ref<Resource>, Ref<Resource>> resources_local_to_sub_scene; // Record the mappings in the sub-scene.
287
288 for (int j = 0; j < nprop_count; j++) {
289 bool valid;
290
291 ERR_FAIL_INDEX_V(nprops[j].value, prop_count, nullptr);
292
293 if (nprops[j].name & FLAG_PATH_PROPERTY_IS_NODE) {
294 uint32_t name_idx = nprops[j].name & (FLAG_PATH_PROPERTY_IS_NODE - 1);
295 ERR_FAIL_UNSIGNED_INDEX_V(name_idx, (uint32_t)sname_count, nullptr);
296
297 DeferredNodePathProperties dnp;
298 dnp.value = props[nprops[j].value];
299 dnp.base = node;
300 dnp.property = snames[name_idx];
301 deferred_node_paths.push_back(dnp);
302 continue;
303 }
304
305 ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);
306
307 if (snames[nprops[j].name] == CoreStringNames::get_singleton()->_script) {
308 //work around to avoid old script variables from disappearing, should be the proper fix to:
309 //https://github.com/godotengine/godot/issues/2958
310
311 //store old state
312 List<Pair<StringName, Variant>> old_state;
313 if (node->get_script_instance()) {
314 node->get_script_instance()->get_property_state(old_state);
315 }
316
317 node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
318
319 //restore old state for new script, if exists
320 for (const Pair<StringName, Variant> &E : old_state) {
321 node->set(E.first, E.second);
322 }
323 } else {
324 Variant value = props[nprops[j].value];
325
326 if (value.get_type() == Variant::OBJECT) {
327 //handle resources that are local to scene by duplicating them if needed
328 Ref<Resource> res = value;
329 if (res.is_valid()) {
330 if (res->is_local_to_scene()) {
331 if (n.instance >= 0) { // For the root node of a sub-scene, treat it as part of the sub-scene.
332 value = get_remap_resource(res, resources_local_to_sub_scene, node->get(snames[nprops[j].name]), node);
333 } else {
334 HashMap<Ref<Resource>, Ref<Resource>>::Iterator E = resources_local_to_scene.find(res);
335 Node *base = i == 0 ? node : ret_nodes[0];
336 if (E) {
337 value = E->value;
338 } else {
339 if (p_edit_state == GEN_EDIT_STATE_MAIN) {
340 //for the main scene, use the resource as is
341 res->configure_for_local_scene(base, resources_local_to_scene);
342 resources_local_to_scene[res] = res;
343 } else {
344 //for instances, a copy must be made
345 Ref<Resource> local_dupe = res->duplicate_for_local_scene(base, resources_local_to_scene);
346 resources_local_to_scene[res] = local_dupe;
347 value = local_dupe;
348 }
349 }
350 }
351 //must make a copy, because this res is local to scene
352 }
353 }
354 }
355 if (value.get_type() == Variant::ARRAY) {
356 Array set_array = value;
357 bool is_get_valid = false;
358 Variant get_value = node->get(snames[nprops[j].name], &is_get_valid);
359 if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
360 Array get_array = get_value;
361 if (!set_array.is_same_typed(get_array)) {
362 value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
363 }
364 }
365 }
366 if (p_edit_state == GEN_EDIT_STATE_INSTANCE && value.get_type() != Variant::OBJECT) {
367 value = value.duplicate(true); // Duplicate arrays and dictionaries for the editor
368 }
369
370 bool set_valid = true;
371 if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled() && value.get_type() == Variant::OBJECT) {
372 Ref<MissingResource> mr = value;
373 if (mr.is_valid()) {
374 missing_resource_properties[snames[nprops[j].name]] = mr;
375 set_valid = false;
376 }
377 }
378
379 if (set_valid) {
380 node->set(snames[nprops[j].name], value, &valid);
381 }
382 }
383 }
384 if (!missing_resource_properties.is_empty()) {
385 node->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
386 }
387
388 for (KeyValue<Ref<Resource>, Ref<Resource>> &E : resources_local_to_sub_scene) {
389 if (E.value->get_local_scene() == node) {
390 E.value->setup_local_to_scene(); // Setup may be required for the resource to work properly.
391 }
392 }
393 }
394
395 //name
396
397 //groups
398 for (int j = 0; j < n.groups.size(); j++) {
399 ERR_FAIL_INDEX_V(n.groups[j], sname_count, nullptr);
400 node->add_to_group(snames[n.groups[j]], true);
401 }
402
403 if (n.instance >= 0 || n.type != TYPE_INSTANTIATED || i == 0) {
404 //if node was not part of instance, must set its name, parenthood and ownership
405 if (i > 0) {
406 if (parent) {
407 bool pending_add = true;
408#ifdef TOOLS_ENABLED
409 if (Engine::get_singleton()->is_editor_hint()) {
410 Node *existing = parent->_get_child_by_name(snames[n.name]);
411 if (existing) {
412 // There's already a node in the same parent with the same name.
413 // This means that somehow the node was added both to the scene being
414 // loaded and another one instantiated in the former, maybe because of
415 // manual editing, or a bug in scene saving, or a loophole in the workflow
416 // (with any of the bugs possibly already fixed).
417 // Bring consistency back by letting it be assigned a non-clashing name.
418 // This simple workaround at least avoids leaks and helps the user realize
419 // something awkward has happened.
420 if (instantiation_warn_notify) {
421 instantiation_warn_notify(vformat(
422 TTR("An incoming node's name clashes with %s already in the scene (presumably, from a more nested instance).\nThe less nested node will be renamed. Please fix and re-save the scene."),
423 ret_nodes[0]->get_path_to(existing)));
424 }
425 node->set_name(snames[n.name]);
426 parent->add_child(node, true);
427 pending_add = false;
428 }
429 }
430#endif
431 if (pending_add) {
432 parent->_add_child_nocheck(node, snames[n.name]);
433 }
434 if (n.index >= 0 && n.index < parent->get_child_count() - 1) {
435 parent->move_child(node, n.index);
436 }
437 } else {
438 //it may be possible that an instantiated scene has changed
439 //and the node has nowhere to go anymore
440 stray_instances.push_back(node); //can't be added, go to stray list
441 }
442 } else {
443 if (Engine::get_singleton()->is_editor_hint()) {
444 //validate name if using editor, to avoid broken
445 node->set_name(snames[n.name]);
446 } else {
447 node->_set_name_nocheck(snames[n.name]);
448 }
449 }
450 }
451
452 if (!old_parent_path.is_empty()) {
453 node->_set_name_nocheck(old_parent_path + "@" + node->get_name());
454 }
455
456 if (n.owner >= 0) {
457 NODE_FROM_ID(owner, n.owner);
458 if (owner) {
459 node->_set_owner_nocheck(owner);
460 if (node->data.unique_name_in_owner) {
461 node->_acquire_unique_name_in_owner();
462 }
463 }
464 }
465
466 // We only want to deal with pinned flag if instantiating as pure main (no instance, no inheriting.)
467 if (p_edit_state == GEN_EDIT_STATE_MAIN) {
468 _sanitize_node_pinned_properties(node);
469 } else {
470 node->remove_meta("_edit_pinned_properties_");
471 }
472 }
473
474 if (missing_node) {
475 missing_node->set_recording_properties(false);
476 }
477
478 ret_nodes[i] = node;
479
480 if (node && gen_node_path_cache && ret_nodes[0]) {
481 NodePath n2 = ret_nodes[0]->get_path_to(node);
482 node_path_cache[n2] = i;
483 }
484 }
485
486 for (const DeferredNodePathProperties &dnp : deferred_node_paths) {
487 // Replace properties stored as NodePaths with actual Nodes.
488 if (dnp.value.get_type() == Variant::ARRAY) {
489 Array paths = dnp.value;
490
491 bool valid;
492 Array array = dnp.base->get(dnp.property, &valid);
493 ERR_CONTINUE(!valid);
494 array = array.duplicate();
495
496 array.resize(paths.size());
497 for (int i = 0; i < array.size(); i++) {
498 array.set(i, dnp.base->get_node_or_null(paths[i]));
499 }
500 dnp.base->set(dnp.property, array);
501 } else {
502 dnp.base->set(dnp.property, dnp.base->get_node_or_null(dnp.value));
503 }
504 }
505
506 for (KeyValue<Ref<Resource>, Ref<Resource>> &E : resources_local_to_scene) {
507 if (E.value->get_local_scene() == ret_nodes[0]) {
508 E.value->setup_local_to_scene();
509 }
510 }
511
512 //do connections
513
514 int cc = connections.size();
515 const ConnectionData *cdata = connections.ptr();
516
517 for (int i = 0; i < cc; i++) {
518 const ConnectionData &c = cdata[i];
519 //ERR_FAIL_INDEX_V( c.from, nc, nullptr );
520 //ERR_FAIL_INDEX_V( c.to, nc, nullptr );
521
522 NODE_FROM_ID(cfrom, c.from);
523 NODE_FROM_ID(cto, c.to);
524
525 if (!cfrom || !cto) {
526 continue;
527 }
528
529 Callable callable(cto, snames[c.method]);
530 if (c.unbinds > 0) {
531 callable = callable.unbind(c.unbinds);
532 } else if (!c.binds.is_empty()) {
533 Vector<Variant> binds;
534 if (c.binds.size()) {
535 binds.resize(c.binds.size());
536 for (int j = 0; j < c.binds.size(); j++) {
537 binds.write[j] = props[c.binds[j]];
538 }
539 }
540
541 const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
542 for (int j = 0; j < binds.size(); j++) {
543 argptrs[j] = &binds[j];
544 }
545 callable = callable.bindp(argptrs, binds.size());
546 }
547
548 cfrom->connect(snames[c.signal], callable, CONNECT_PERSIST | c.flags | (p_edit_state == GEN_EDIT_STATE_MAIN ? 0 : CONNECT_INHERITED));
549 }
550
551 //Node *s = ret_nodes[0];
552
553 //remove nodes that could not be added, likely as a result that
554 while (stray_instances.size()) {
555 memdelete(stray_instances.front()->get());
556 stray_instances.pop_front();
557 }
558
559 for (int i = 0; i < editable_instances.size(); i++) {
560 Node *ei = ret_nodes[0]->get_node_or_null(editable_instances[i]);
561 if (ei) {
562 ret_nodes[0]->set_editable_instance(ei, true);
563 }
564 }
565
566 return ret_nodes[0];
567}
568
569static int _nm_get_string(const String &p_string, HashMap<StringName, int> &name_map) {
570 if (name_map.has(p_string)) {
571 return name_map[p_string];
572 }
573
574 int idx = name_map.size();
575 name_map[p_string] = idx;
576 return idx;
577}
578
579static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) {
580 if (variant_map.has(p_variant)) {
581 return variant_map[p_variant];
582 }
583
584 int idx = variant_map.size();
585 variant_map[p_variant] = idx;
586 return idx;
587}
588
589Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) {
590 // this function handles all the work related to properly packing scenes, be it
591 // instantiated or inherited.
592 // given the complexity of this process, an attempt will be made to properly
593 // document it. if you fail to understand something, please ask!
594
595 //discard nodes that do not belong to be processed
596 if (p_node != p_owner && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
597 return OK;
598 }
599
600 bool is_editable_instance = false;
601
602 // save the child instantiated scenes that are chosen as editable, so they can be restored
603 // upon load back
604 if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
605 editable_instances.push_back(p_owner->get_path_to(p_node));
606 // Node is the root of an editable instance.
607 is_editable_instance = true;
608 } else if (p_node->get_owner() && p_owner->is_ancestor_of(p_node->get_owner()) && p_owner->is_editable_instance(p_node->get_owner())) {
609 // Node is part of an editable instance.
610 is_editable_instance = true;
611 }
612
613 NodeData nd;
614
615 nd.name = _nm_get_string(p_node->get_name(), name_map);
616 nd.instance = -1; //not instantiated by default
617
618 //really convoluted condition, but it basically checks that index is only saved when part of an inherited scene OR the node parent is from the edited scene
619 if (p_owner->get_scene_inherited_state().is_null() && (p_node == p_owner || (p_node->get_owner() == p_owner && (p_node->get_parent() == p_owner || p_node->get_parent()->get_owner() == p_owner)))) {
620 //do not save index, because it belongs to saved scene and scene is not inherited
621 nd.index = -1;
622 } else if (p_node == p_owner) {
623 //This (hopefully) happens if the node is a scene root, so its index is irrelevant.
624 nd.index = -1;
625 } else {
626 //part of an inherited scene, or parent is from an instantiated scene
627 nd.index = p_node->get_index();
628 }
629
630 // if this node is part of an instantiated scene or sub-instantiated scene
631 // we need to get the corresponding instance states.
632 // with the instance states, we can query for identical properties/groups
633 // and only save what has changed
634
635 bool instantiated_by_owner = false;
636 Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
637
638 if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
639 if (p_node->get_scene_instance_load_placeholder()) {
640 //it's a placeholder, use the placeholder path
641 nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
642 nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
643 } else {
644 //must instance ourselves
645 Ref<PackedScene> instance = ResourceLoader::load(p_node->get_scene_file_path());
646 if (!instance.is_valid()) {
647 return ERR_CANT_OPEN;
648 }
649
650 nd.instance = _vm_get_variant(instance, variant_map);
651 }
652 }
653
654 // all setup, we then proceed to check all properties for the node
655 // and save the ones that are worth saving
656
657 List<PropertyInfo> plist;
658 p_node->get_property_list(&plist);
659
660 Array pinned_props = _sanitize_node_pinned_properties(p_node);
661 Dictionary missing_resource_properties = p_node->get_meta(META_MISSING_RESOURCES, Dictionary());
662
663 for (const PropertyInfo &E : plist) {
664 if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
665 continue;
666 }
667
668 if (E.name == META_PROPERTY_MISSING_RESOURCES) {
669 continue; // Ignore this property when packing.
670 }
671
672 // If instance or inheriting, not saving if property requested so.
673 if (!states_stack.is_empty()) {
674 if ((E.usage & PROPERTY_USAGE_NO_INSTANCE_STATE)) {
675 continue;
676 }
677 }
678
679 StringName name = E.name;
680 Variant value = p_node->get(name);
681 bool use_deferred_node_path_bit = false;
682
683 if (E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_NODE_TYPE) {
684 if (value.get_type() == Variant::OBJECT) {
685 if (Node *n = Object::cast_to<Node>(value)) {
686 value = p_node->get_path_to(n);
687 }
688 use_deferred_node_path_bit = true;
689 }
690 if (value.get_type() != Variant::NODE_PATH) {
691 continue; //was never set, ignore.
692 }
693 } else if (E.type == Variant::OBJECT && missing_resource_properties.has(E.name)) {
694 // Was this missing resource overridden? If so do not save the old value.
695 Ref<Resource> ures = value;
696 if (ures.is_null()) {
697 value = missing_resource_properties[E.name];
698 }
699 } else if (E.type == Variant::ARRAY && E.hint == PROPERTY_HINT_TYPE_STRING) {
700 int hint_subtype_separator = E.hint_string.find(":");
701 if (hint_subtype_separator >= 0) {
702 String subtype_string = E.hint_string.substr(0, hint_subtype_separator);
703 int slash_pos = subtype_string.find("/");
704 PropertyHint subtype_hint = PropertyHint::PROPERTY_HINT_NONE;
705 if (slash_pos >= 0) {
706 subtype_hint = PropertyHint(subtype_string.get_slice("/", 1).to_int());
707 subtype_string = subtype_string.substr(0, slash_pos);
708 }
709 Variant::Type subtype = Variant::Type(subtype_string.to_int());
710
711 if (subtype == Variant::OBJECT && subtype_hint == PROPERTY_HINT_NODE_TYPE) {
712 use_deferred_node_path_bit = true;
713 Array array = value;
714 Array new_array;
715 for (int i = 0; i < array.size(); i++) {
716 Variant elem = array[i];
717 if (elem.get_type() == Variant::OBJECT) {
718 if (Node *n = Object::cast_to<Node>(elem)) {
719 new_array.push_back(p_node->get_path_to(n));
720 continue;
721 }
722 }
723 new_array.push_back(elem);
724 }
725 value = new_array;
726 }
727 }
728 }
729
730 if (!pinned_props.has(name)) {
731 bool is_valid_default = false;
732 Variant default_value = PropertyUtils::get_property_default_value(p_node, name, &is_valid_default, &states_stack, true);
733 if (is_valid_default && !PropertyUtils::is_property_value_different(value, default_value)) {
734 continue;
735 }
736 }
737
738 NodeData::Property prop;
739 prop.name = _nm_get_string(name, name_map);
740 prop.value = _vm_get_variant(value, variant_map);
741 if (use_deferred_node_path_bit) {
742 prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
743 }
744 nd.properties.push_back(prop);
745 }
746
747 // save the groups this node is into
748 // discard groups that come from the original scene
749
750 List<Node::GroupInfo> groups;
751 p_node->get_groups(&groups);
752 for (const Node::GroupInfo &gi : groups) {
753 if (!gi.persistent) {
754 continue;
755 }
756
757 bool skip = false;
758 for (const SceneState::PackState &ia : states_stack) {
759 //check all levels of pack to see if the group was added somewhere
760 if (ia.state->is_node_in_group(ia.node, gi.name)) {
761 skip = true;
762 break;
763 }
764 }
765
766 if (skip) {
767 continue;
768 }
769
770 nd.groups.push_back(_nm_get_string(gi.name, name_map));
771 }
772
773 // save the right owner
774 // for the saved scene root this is -1
775 // for nodes of the saved scene this is 0
776 // for nodes of instantiated scenes this is >0
777
778 if (p_node == p_owner) {
779 //saved scene root
780 nd.owner = -1;
781 } else if (p_node->get_owner() == p_owner) {
782 //part of saved scene
783 nd.owner = 0;
784 } else {
785 nd.owner = -1;
786 }
787
788 MissingNode *missing_node = Object::cast_to<MissingNode>(p_node);
789
790 // Save the right type. If this node was created by an instance
791 // then flag that the node should not be created but reused
792 if (states_stack.is_empty() && !is_editable_instance) {
793 //This node is not part of an instantiation process, so save the type.
794 if (missing_node != nullptr) {
795 // It's a missing node (type non existent on load).
796 nd.type = _nm_get_string(missing_node->get_original_class(), name_map);
797 } else {
798 nd.type = _nm_get_string(p_node->get_class(), name_map);
799 }
800 } else {
801 // this node is part of an instantiated process, so do not save the type.
802 // instead, save that it was instantiated
803 nd.type = TYPE_INSTANTIATED;
804 }
805
806 // determine whether to save this node or not
807 // if this node is part of an instantiated sub-scene, we can skip storing it if basically
808 // no properties changed and no groups were added to it.
809 // below condition is true for all nodes of the scene being saved, and ones in subscenes
810 // that hold changes
811
812 bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
813 save_node = save_node || p_node == p_owner; // owner is always saved
814 save_node = save_node || (p_node->get_owner() == p_owner && instantiated_by_owner); //part of scene and not instanced
815
816 int idx = nodes.size();
817 int parent_node = NO_PARENT_SAVED;
818
819 if (save_node) {
820 //don't save the node if nothing and subscene
821
822 node_map[p_node] = idx;
823
824 //ok validate parent node
825 if (p_parent_idx == NO_PARENT_SAVED) {
826 int sidx;
827 if (nodepath_map.has(p_node->get_parent())) {
828 sidx = nodepath_map[p_node->get_parent()];
829 } else {
830 sidx = nodepath_map.size();
831 nodepath_map[p_node->get_parent()] = sidx;
832 }
833
834 nd.parent = FLAG_ID_IS_PATH | sidx;
835 } else {
836 nd.parent = p_parent_idx;
837 }
838
839 parent_node = idx;
840 nodes.push_back(nd);
841 }
842
843 for (int i = 0; i < p_node->get_child_count(); i++) {
844 Node *c = p_node->get_child(i);
845 Error err = _parse_node(p_owner, c, parent_node, name_map, variant_map, node_map, nodepath_map);
846 if (err) {
847 return err;
848 }
849 }
850
851 return OK;
852}
853
854Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) {
855 if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
856 return OK;
857 }
858
859 List<MethodInfo> _signals;
860 p_node->get_signal_list(&_signals);
861 _signals.sort();
862
863 //ERR_FAIL_COND_V( !node_map.has(p_node), ERR_BUG);
864 //NodeData &nd = nodes[node_map[p_node]];
865
866 for (const MethodInfo &E : _signals) {
867 List<Node::Connection> conns;
868 p_node->get_signal_connection_list(E.name, &conns);
869
870 conns.sort();
871
872 for (const Node::Connection &F : conns) {
873 const Node::Connection &c = F;
874
875 if (!(c.flags & CONNECT_PERSIST)) { //only persistent connections get saved
876 continue;
877 }
878
879 // only connections that originate or end into main saved scene are saved
880 // everything else is discarded
881
882 Node *target = Object::cast_to<Node>(c.callable.get_object());
883
884 if (!target) {
885 continue;
886 }
887
888 Vector<Variant> binds;
889 int unbinds = 0;
890 Callable base_callable;
891
892 if (c.callable.is_custom()) {
893 CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(c.callable.get_custom());
894 if (ccb) {
895 binds = ccb->get_binds();
896 base_callable = ccb->get_callable();
897 }
898
899 CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(c.callable.get_custom());
900 if (ccu) {
901 unbinds = ccu->get_unbinds();
902 base_callable = ccu->get_callable();
903 }
904 } else {
905 base_callable = c.callable;
906 }
907
908 //find if this connection already exists
909 Node *common_parent = target->find_common_parent_with(p_node);
910
911 ERR_CONTINUE(!common_parent);
912
913 if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
914 common_parent = common_parent->get_owner();
915 }
916
917 bool exists = false;
918
919 //go through ownership chain to see if this exists
920 while (common_parent) {
921 Ref<SceneState> ps;
922
923 if (common_parent == p_owner) {
924 ps = common_parent->get_scene_inherited_state();
925 } else {
926 ps = common_parent->get_scene_instance_state();
927 }
928
929 if (ps.is_valid()) {
930 NodePath signal_from = common_parent->get_path_to(p_node);
931 NodePath signal_to = common_parent->get_path_to(target);
932
933 if (ps->has_connection(signal_from, c.signal.get_name(), signal_to, base_callable.get_method())) {
934 exists = true;
935 break;
936 }
937 }
938
939 if (common_parent == p_owner) {
940 break;
941 } else {
942 common_parent = common_parent->get_owner();
943 }
944 }
945
946 if (exists) { //already exists (comes from instance or inheritance), so don't save
947 continue;
948 }
949
950 {
951 Node *nl = p_node;
952
953 bool exists2 = false;
954
955 while (nl) {
956 if (nl == p_owner) {
957 Ref<SceneState> state = nl->get_scene_inherited_state();
958 if (state.is_valid()) {
959 int from_node = state->find_node_by_path(nl->get_path_to(p_node));
960 int to_node = state->find_node_by_path(nl->get_path_to(target));
961
962 if (from_node >= 0 && to_node >= 0) {
963 //this one has state for this node, save
964 if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
965 exists2 = true;
966 break;
967 }
968 }
969 }
970
971 nl = nullptr;
972 } else {
973 if (!nl->get_scene_file_path().is_empty()) {
974 //is an instance
975 Ref<SceneState> state = nl->get_scene_instance_state();
976 if (state.is_valid()) {
977 int from_node = state->find_node_by_path(nl->get_path_to(p_node));
978 int to_node = state->find_node_by_path(nl->get_path_to(target));
979
980 if (from_node >= 0 && to_node >= 0) {
981 //this one has state for this node, save
982 if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
983 exists2 = true;
984 break;
985 }
986 }
987 }
988 }
989 nl = nl->get_owner();
990 }
991 }
992
993 if (exists2) {
994 continue;
995 }
996 }
997
998 int src_id;
999
1000 if (node_map.has(p_node)) {
1001 src_id = node_map[p_node];
1002 } else {
1003 if (nodepath_map.has(p_node)) {
1004 src_id = FLAG_ID_IS_PATH | nodepath_map[p_node];
1005 } else {
1006 int sidx = nodepath_map.size();
1007 nodepath_map[p_node] = sidx;
1008 src_id = FLAG_ID_IS_PATH | sidx;
1009 }
1010 }
1011
1012 int target_id;
1013
1014 if (node_map.has(target)) {
1015 target_id = node_map[target];
1016 } else {
1017 if (nodepath_map.has(target)) {
1018 target_id = FLAG_ID_IS_PATH | nodepath_map[target];
1019 } else {
1020 int sidx = nodepath_map.size();
1021 nodepath_map[target] = sidx;
1022 target_id = FLAG_ID_IS_PATH | sidx;
1023 }
1024 }
1025
1026 ConnectionData cd;
1027 cd.from = src_id;
1028 cd.to = target_id;
1029 cd.method = _nm_get_string(base_callable.get_method(), name_map);
1030 cd.signal = _nm_get_string(c.signal.get_name(), name_map);
1031 cd.flags = c.flags;
1032 cd.unbinds = unbinds;
1033
1034 for (int i = 0; i < binds.size(); i++) {
1035 cd.binds.push_back(_vm_get_variant(binds[i], variant_map));
1036 }
1037 connections.push_back(cd);
1038 }
1039 }
1040
1041 for (int i = 0; i < p_node->get_child_count(); i++) {
1042 Node *c = p_node->get_child(i);
1043 Error err = _parse_connections(p_owner, c, name_map, variant_map, node_map, nodepath_map);
1044 if (err) {
1045 return err;
1046 }
1047 }
1048
1049 return OK;
1050}
1051
1052Error SceneState::pack(Node *p_scene) {
1053 ERR_FAIL_NULL_V(p_scene, ERR_INVALID_PARAMETER);
1054
1055 clear();
1056
1057 Node *scene = p_scene;
1058
1059 HashMap<StringName, int> name_map;
1060 HashMap<Variant, int, VariantHasher, VariantComparator> variant_map;
1061 HashMap<Node *, int> node_map;
1062 HashMap<Node *, int> nodepath_map;
1063
1064 // If using scene inheritance, pack the scene it inherits from.
1065 if (scene->get_scene_inherited_state().is_valid()) {
1066 String scene_path = scene->get_scene_inherited_state()->get_path();
1067 Ref<PackedScene> instance = ResourceLoader::load(scene_path);
1068 if (instance.is_valid()) {
1069 base_scene_idx = _vm_get_variant(instance, variant_map);
1070 }
1071 }
1072
1073 // Instanced, only direct sub-scenes are supported of course.
1074 Error err = _parse_node(scene, scene, -1, name_map, variant_map, node_map, nodepath_map);
1075 if (err) {
1076 clear();
1077 ERR_FAIL_V(err);
1078 }
1079
1080 err = _parse_connections(scene, scene, name_map, variant_map, node_map, nodepath_map);
1081 if (err) {
1082 clear();
1083 ERR_FAIL_V(err);
1084 }
1085
1086 names.resize(name_map.size());
1087
1088 for (const KeyValue<StringName, int> &E : name_map) {
1089 names.write[E.value] = E.key;
1090 }
1091
1092 variants.resize(variant_map.size());
1093
1094 for (const KeyValue<Variant, int> &E : variant_map) {
1095 int idx = E.value;
1096 variants.write[idx] = E.key;
1097 }
1098
1099 node_paths.resize(nodepath_map.size());
1100 for (const KeyValue<Node *, int> &E : nodepath_map) {
1101 node_paths.write[E.value] = scene->get_path_to(E.key);
1102 }
1103
1104 if (Engine::get_singleton()->is_editor_hint()) {
1105 // Build node path cache
1106 for (const KeyValue<Node *, int> &E : node_map) {
1107 node_path_cache[scene->get_path_to(E.key)] = E.value;
1108 }
1109 }
1110
1111 return OK;
1112}
1113
1114void SceneState::set_path(const String &p_path) {
1115 path = p_path;
1116}
1117
1118String SceneState::get_path() const {
1119 return path;
1120}
1121
1122void SceneState::clear() {
1123 names.clear();
1124 variants.clear();
1125 nodes.clear();
1126 connections.clear();
1127 node_path_cache.clear();
1128 node_paths.clear();
1129 editable_instances.clear();
1130 base_scene_idx = -1;
1131}
1132
1133Error SceneState::copy_from(const Ref<SceneState> &p_scene_state) {
1134 ERR_FAIL_COND_V(p_scene_state.is_null(), ERR_INVALID_PARAMETER);
1135
1136 clear();
1137
1138 for (const StringName &E : p_scene_state->names) {
1139 names.append(E);
1140 }
1141 for (const Variant &E : p_scene_state->variants) {
1142 variants.append(E);
1143 }
1144 for (const SceneState::NodeData &E : p_scene_state->nodes) {
1145 nodes.append(E);
1146 }
1147 for (const SceneState::ConnectionData &E : p_scene_state->connections) {
1148 connections.append(E);
1149 }
1150 for (KeyValue<NodePath, int> &E : p_scene_state->node_path_cache) {
1151 node_path_cache.insert(E.key, E.value);
1152 }
1153 for (const NodePath &E : p_scene_state->node_paths) {
1154 node_paths.append(E);
1155 }
1156 for (const NodePath &E : p_scene_state->editable_instances) {
1157 editable_instances.append(E);
1158 }
1159 base_scene_idx = p_scene_state->base_scene_idx;
1160
1161 return OK;
1162}
1163
1164Ref<SceneState> SceneState::get_base_scene_state() const {
1165 if (base_scene_idx >= 0) {
1166 Ref<PackedScene> ps = variants[base_scene_idx];
1167 if (ps.is_valid()) {
1168 return ps->get_state();
1169 }
1170 }
1171
1172 return Ref<SceneState>();
1173}
1174
1175void SceneState::update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene) {
1176 ERR_FAIL_COND(p_packed_scene.is_null());
1177
1178 for (const NodeData &nd : nodes) {
1179 if (nd.instance >= 0) {
1180 if (!(nd.instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
1181 int instance_id = nd.instance & FLAG_MASK;
1182 Ref<PackedScene> original_packed_scene = variants[instance_id];
1183 if (original_packed_scene.is_valid()) {
1184 if (original_packed_scene->get_path() == p_path) {
1185 variants.remove_at(instance_id);
1186 variants.insert(instance_id, p_packed_scene);
1187 }
1188 }
1189 }
1190 }
1191 }
1192}
1193
1194int SceneState::find_node_by_path(const NodePath &p_node) const {
1195 ERR_FAIL_COND_V_MSG(node_path_cache.size() == 0, -1, "This operation requires the node cache to have been built.");
1196
1197 if (!node_path_cache.has(p_node)) {
1198 if (get_base_scene_state().is_valid()) {
1199 int idx = get_base_scene_state()->find_node_by_path(p_node);
1200 if (idx != -1) {
1201 int rkey = _find_base_scene_node_remap_key(idx);
1202 if (rkey == -1) {
1203 rkey = nodes.size() + base_scene_node_remap.size();
1204 base_scene_node_remap[rkey] = idx;
1205 }
1206 return rkey;
1207 }
1208 }
1209 return -1;
1210 }
1211
1212 int nid = node_path_cache[p_node];
1213
1214 if (get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
1215 //for nodes that _do_ exist in current scene, still try to look for
1216 //the node in the instantiated scene, as a property may be missing
1217 //from the local one
1218 int idx = get_base_scene_state()->find_node_by_path(p_node);
1219 if (idx != -1) {
1220 base_scene_node_remap[nid] = idx;
1221 }
1222 }
1223
1224 return nid;
1225}
1226
1227int SceneState::_find_base_scene_node_remap_key(int p_idx) const {
1228 for (const KeyValue<int, int> &E : base_scene_node_remap) {
1229 if (E.value == p_idx) {
1230 return E.key;
1231 }
1232 }
1233 return -1;
1234}
1235
1236Variant SceneState::get_property_value(int p_node, const StringName &p_property, bool &found) const {
1237 found = false;
1238
1239 ERR_FAIL_COND_V(p_node < 0, Variant());
1240
1241 if (p_node < nodes.size()) {
1242 //find in built-in nodes
1243 int pc = nodes[p_node].properties.size();
1244 const StringName *namep = names.ptr();
1245
1246 const NodeData::Property *p = nodes[p_node].properties.ptr();
1247 for (int i = 0; i < pc; i++) {
1248 if (p_property == namep[p[i].name & FLAG_PROP_NAME_MASK]) {
1249 found = true;
1250 return variants[p[i].value];
1251 }
1252 }
1253 }
1254
1255 //property not found, try on instance
1256
1257 if (base_scene_node_remap.has(p_node)) {
1258 return get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
1259 }
1260
1261 return Variant();
1262}
1263
1264bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
1265 ERR_FAIL_COND_V(p_node < 0, false);
1266
1267 if (p_node < nodes.size()) {
1268 const StringName *namep = names.ptr();
1269 for (int i = 0; i < nodes[p_node].groups.size(); i++) {
1270 if (namep[nodes[p_node].groups[i]] == p_group) {
1271 return true;
1272 }
1273 }
1274 }
1275
1276 if (base_scene_node_remap.has(p_node)) {
1277 return get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
1278 }
1279
1280 return false;
1281}
1282
1283bool SceneState::disable_placeholders = false;
1284
1285void SceneState::set_disable_placeholders(bool p_disable) {
1286 disable_placeholders = p_disable;
1287}
1288
1289bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const {
1290 ERR_FAIL_COND_V(p_node < 0, false);
1291 ERR_FAIL_COND_V(p_to_node < 0, false);
1292
1293 if (p_node < nodes.size() && p_to_node < nodes.size()) {
1294 int signal_idx = -1;
1295 int method_idx = -1;
1296 for (int i = 0; i < names.size(); i++) {
1297 if (names[i] == p_signal) {
1298 signal_idx = i;
1299 } else if (names[i] == p_to_method) {
1300 method_idx = i;
1301 }
1302 }
1303
1304 if (signal_idx >= 0 && method_idx >= 0) {
1305 //signal and method strings are stored..
1306
1307 for (int i = 0; i < connections.size(); i++) {
1308 if (connections[i].from == p_node && connections[i].to == p_to_node && connections[i].signal == signal_idx && connections[i].method == method_idx) {
1309 return true;
1310 }
1311 }
1312 }
1313 }
1314
1315 if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
1316 return get_base_scene_state()->is_connection(base_scene_node_remap[p_node], p_signal, base_scene_node_remap[p_to_node], p_to_method);
1317 }
1318
1319 return false;
1320}
1321
1322void SceneState::set_bundled_scene(const Dictionary &p_dictionary) {
1323 ERR_FAIL_COND(!p_dictionary.has("names"));
1324 ERR_FAIL_COND(!p_dictionary.has("variants"));
1325 ERR_FAIL_COND(!p_dictionary.has("node_count"));
1326 ERR_FAIL_COND(!p_dictionary.has("nodes"));
1327 ERR_FAIL_COND(!p_dictionary.has("conn_count"));
1328 ERR_FAIL_COND(!p_dictionary.has("conns"));
1329 //ERR_FAIL_COND( !p_dictionary.has("path"));
1330
1331 int version = 1;
1332 if (p_dictionary.has("version")) {
1333 version = p_dictionary["version"];
1334 }
1335
1336 ERR_FAIL_COND_MSG(version > PACKED_SCENE_VERSION, "Save format version too new.");
1337
1338 const int node_count = p_dictionary["node_count"];
1339 const Vector<int> snodes = p_dictionary["nodes"];
1340 ERR_FAIL_COND(snodes.size() < node_count);
1341
1342 const int conn_count = p_dictionary["conn_count"];
1343 const Vector<int> sconns = p_dictionary["conns"];
1344 ERR_FAIL_COND(sconns.size() < conn_count);
1345
1346 Vector<String> snames = p_dictionary["names"];
1347 if (snames.size()) {
1348 int namecount = snames.size();
1349 names.resize(namecount);
1350 const String *r = snames.ptr();
1351 for (int i = 0; i < names.size(); i++) {
1352 names.write[i] = r[i];
1353 }
1354 }
1355
1356 Array svariants = p_dictionary["variants"];
1357
1358 if (svariants.size()) {
1359 int varcount = svariants.size();
1360 variants.resize(varcount);
1361 for (int i = 0; i < varcount; i++) {
1362 variants.write[i] = svariants[i];
1363 }
1364
1365 } else {
1366 variants.clear();
1367 }
1368
1369 nodes.resize(node_count);
1370 if (node_count) {
1371 const int *r = snodes.ptr();
1372 int idx = 0;
1373 for (int i = 0; i < node_count; i++) {
1374 NodeData &nd = nodes.write[i];
1375 nd.parent = r[idx++];
1376 nd.owner = r[idx++];
1377 nd.type = r[idx++];
1378 uint32_t name_index = r[idx++];
1379 nd.name = name_index & ((1 << NAME_INDEX_BITS) - 1);
1380 nd.index = (name_index >> NAME_INDEX_BITS);
1381 nd.index--; //0 is invalid, stored as 1
1382 nd.instance = r[idx++];
1383 nd.properties.resize(r[idx++]);
1384 for (int j = 0; j < nd.properties.size(); j++) {
1385 nd.properties.write[j].name = r[idx++];
1386 nd.properties.write[j].value = r[idx++];
1387 }
1388 nd.groups.resize(r[idx++]);
1389 for (int j = 0; j < nd.groups.size(); j++) {
1390 nd.groups.write[j] = r[idx++];
1391 }
1392 }
1393 }
1394
1395 connections.resize(conn_count);
1396 if (conn_count) {
1397 const int *r = sconns.ptr();
1398 int idx = 0;
1399 for (int i = 0; i < conn_count; i++) {
1400 ConnectionData &cd = connections.write[i];
1401 cd.from = r[idx++];
1402 cd.to = r[idx++];
1403 cd.signal = r[idx++];
1404 cd.method = r[idx++];
1405 cd.flags = r[idx++];
1406 cd.binds.resize(r[idx++]);
1407
1408 for (int j = 0; j < cd.binds.size(); j++) {
1409 cd.binds.write[j] = r[idx++];
1410 }
1411 if (version >= 3) {
1412 cd.unbinds = r[idx++];
1413 }
1414 }
1415 }
1416
1417 Array np;
1418 if (p_dictionary.has("node_paths")) {
1419 np = p_dictionary["node_paths"];
1420 }
1421 node_paths.resize(np.size());
1422 for (int i = 0; i < np.size(); i++) {
1423 node_paths.write[i] = np[i];
1424 }
1425
1426 Array ei;
1427 if (p_dictionary.has("editable_instances")) {
1428 ei = p_dictionary["editable_instances"];
1429 }
1430
1431 if (p_dictionary.has("base_scene")) {
1432 base_scene_idx = p_dictionary["base_scene"];
1433 }
1434
1435 editable_instances.resize(ei.size());
1436 for (int i = 0; i < editable_instances.size(); i++) {
1437 editable_instances.write[i] = ei[i];
1438 }
1439
1440 //path=p_dictionary["path"];
1441}
1442
1443Dictionary SceneState::get_bundled_scene() const {
1444 Vector<String> rnames;
1445 rnames.resize(names.size());
1446
1447 if (names.size()) {
1448 String *r = rnames.ptrw();
1449
1450 for (int i = 0; i < names.size(); i++) {
1451 r[i] = names[i];
1452 }
1453 }
1454
1455 Dictionary d;
1456 d["names"] = rnames;
1457 d["variants"] = variants;
1458
1459 Vector<int> rnodes;
1460 d["node_count"] = nodes.size();
1461
1462 for (int i = 0; i < nodes.size(); i++) {
1463 const NodeData &nd = nodes[i];
1464 rnodes.push_back(nd.parent);
1465 rnodes.push_back(nd.owner);
1466 rnodes.push_back(nd.type);
1467 uint32_t name_index = nd.name;
1468 if (nd.index < (1 << (32 - NAME_INDEX_BITS)) - 1) { //save if less than 16k children
1469 name_index |= uint32_t(nd.index + 1) << NAME_INDEX_BITS; //for backwards compatibility, index 0 is no index
1470 }
1471 rnodes.push_back(name_index);
1472 rnodes.push_back(nd.instance);
1473 rnodes.push_back(nd.properties.size());
1474 for (int j = 0; j < nd.properties.size(); j++) {
1475 rnodes.push_back(nd.properties[j].name);
1476 rnodes.push_back(nd.properties[j].value);
1477 }
1478 rnodes.push_back(nd.groups.size());
1479 for (int j = 0; j < nd.groups.size(); j++) {
1480 rnodes.push_back(nd.groups[j]);
1481 }
1482 }
1483
1484 d["nodes"] = rnodes;
1485
1486 Vector<int> rconns;
1487 d["conn_count"] = connections.size();
1488
1489 for (int i = 0; i < connections.size(); i++) {
1490 const ConnectionData &cd = connections[i];
1491 rconns.push_back(cd.from);
1492 rconns.push_back(cd.to);
1493 rconns.push_back(cd.signal);
1494 rconns.push_back(cd.method);
1495 rconns.push_back(cd.flags);
1496 rconns.push_back(cd.binds.size());
1497 for (int j = 0; j < cd.binds.size(); j++) {
1498 rconns.push_back(cd.binds[j]);
1499 }
1500 rconns.push_back(cd.unbinds);
1501 }
1502
1503 d["conns"] = rconns;
1504
1505 Array rnode_paths;
1506 rnode_paths.resize(node_paths.size());
1507 for (int i = 0; i < node_paths.size(); i++) {
1508 rnode_paths[i] = node_paths[i];
1509 }
1510 d["node_paths"] = rnode_paths;
1511
1512 Array reditable_instances;
1513 reditable_instances.resize(editable_instances.size());
1514 for (int i = 0; i < editable_instances.size(); i++) {
1515 reditable_instances[i] = editable_instances[i];
1516 }
1517 d["editable_instances"] = reditable_instances;
1518 if (base_scene_idx >= 0) {
1519 d["base_scene"] = base_scene_idx;
1520 }
1521
1522 d["version"] = PACKED_SCENE_VERSION;
1523
1524 return d;
1525}
1526
1527int SceneState::get_node_count() const {
1528 return nodes.size();
1529}
1530
1531StringName SceneState::get_node_type(int p_idx) const {
1532 ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
1533 if (nodes[p_idx].type == TYPE_INSTANTIATED) {
1534 return StringName();
1535 }
1536 return names[nodes[p_idx].type];
1537}
1538
1539StringName SceneState::get_node_name(int p_idx) const {
1540 ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
1541 return names[nodes[p_idx].name];
1542}
1543
1544int SceneState::get_node_index(int p_idx) const {
1545 ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
1546 return nodes[p_idx].index;
1547}
1548
1549bool SceneState::is_node_instance_placeholder(int p_idx) const {
1550 ERR_FAIL_INDEX_V(p_idx, nodes.size(), false);
1551
1552 return nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER);
1553}
1554
1555Ref<PackedScene> SceneState::get_node_instance(int p_idx) const {
1556 ERR_FAIL_INDEX_V(p_idx, nodes.size(), Ref<PackedScene>());
1557
1558 if (nodes[p_idx].instance >= 0) {
1559 if (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
1560 return Ref<PackedScene>();
1561 } else {
1562 return variants[nodes[p_idx].instance & FLAG_MASK];
1563 }
1564 } else if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
1565 if (base_scene_idx >= 0) {
1566 return variants[base_scene_idx];
1567 }
1568 }
1569
1570 return Ref<PackedScene>();
1571}
1572
1573String SceneState::get_node_instance_placeholder(int p_idx) const {
1574 ERR_FAIL_INDEX_V(p_idx, nodes.size(), String());
1575
1576 if (nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
1577 return variants[nodes[p_idx].instance & FLAG_MASK];
1578 }
1579
1580 return String();
1581}
1582
1583Vector<StringName> SceneState::get_node_groups(int p_idx) const {
1584 ERR_FAIL_INDEX_V(p_idx, nodes.size(), Vector<StringName>());
1585 Vector<StringName> groups;
1586 for (int i = 0; i < nodes[p_idx].groups.size(); i++) {
1587 groups.push_back(names[nodes[p_idx].groups[i]]);
1588 }
1589 return groups;
1590}
1591
1592NodePath SceneState::get_node_path(int p_idx, bool p_for_parent) const {
1593 ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
1594
1595 if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
1596 if (p_for_parent) {
1597 return NodePath();
1598 } else {
1599 return NodePath(".");
1600 }
1601 }
1602
1603 Vector<StringName> sub_path;
1604 NodePath base_path;
1605 int nidx = p_idx;
1606 while (true) {
1607 if (nodes[nidx].parent == NO_PARENT_SAVED || nodes[nidx].parent < 0) {
1608 sub_path.insert(0, ".");
1609 break;
1610 }
1611
1612 if (!p_for_parent || p_idx != nidx) {
1613 sub_path.insert(0, names[nodes[nidx].name]);
1614 }
1615
1616 if (nodes[nidx].parent & FLAG_ID_IS_PATH) {
1617 base_path = node_paths[nodes[nidx].parent & FLAG_MASK];
1618 break;
1619 } else {
1620 nidx = nodes[nidx].parent & FLAG_MASK;
1621 }
1622 }
1623
1624 for (int i = base_path.get_name_count() - 1; i >= 0; i--) {
1625 sub_path.insert(0, base_path.get_name(i));
1626 }
1627
1628 if (sub_path.is_empty()) {
1629 return NodePath(".");
1630 }
1631
1632 return NodePath(sub_path, false);
1633}
1634
1635int SceneState::get_node_property_count(int p_idx) const {
1636 ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
1637 return nodes[p_idx].properties.size();
1638}
1639
1640StringName SceneState::get_node_property_name(int p_idx, int p_prop) const {
1641 ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
1642 ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), StringName());
1643 return names[nodes[p_idx].properties[p_prop].name & FLAG_PROP_NAME_MASK];
1644}
1645
1646Vector<String> SceneState::get_node_deferred_nodepath_properties(int p_idx) const {
1647 Vector<String> ret;
1648 ERR_FAIL_INDEX_V(p_idx, nodes.size(), ret);
1649 for (int i = 0; i < nodes[p_idx].properties.size(); i++) {
1650 uint32_t idx = nodes[p_idx].properties[i].name;
1651 if (idx & FLAG_PATH_PROPERTY_IS_NODE) {
1652 ret.push_back(names[idx & FLAG_PROP_NAME_MASK]);
1653 }
1654 }
1655 return ret;
1656}
1657
1658Variant SceneState::get_node_property_value(int p_idx, int p_prop) const {
1659 ERR_FAIL_INDEX_V(p_idx, nodes.size(), Variant());
1660 ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), Variant());
1661
1662 return variants[nodes[p_idx].properties[p_prop].value];
1663}
1664
1665NodePath SceneState::get_node_owner_path(int p_idx) const {
1666 ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
1667 if (nodes[p_idx].owner < 0 || nodes[p_idx].owner == NO_PARENT_SAVED) {
1668 return NodePath(); //root likely
1669 }
1670 if (nodes[p_idx].owner & FLAG_ID_IS_PATH) {
1671 return node_paths[nodes[p_idx].owner & FLAG_MASK];
1672 } else {
1673 return get_node_path(nodes[p_idx].owner & FLAG_MASK);
1674 }
1675}
1676
1677int SceneState::get_connection_count() const {
1678 return connections.size();
1679}
1680
1681NodePath SceneState::get_connection_source(int p_idx) const {
1682 ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
1683 if (connections[p_idx].from & FLAG_ID_IS_PATH) {
1684 return node_paths[connections[p_idx].from & FLAG_MASK];
1685 } else {
1686 return get_node_path(connections[p_idx].from & FLAG_MASK);
1687 }
1688}
1689
1690StringName SceneState::get_connection_signal(int p_idx) const {
1691 ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
1692 return names[connections[p_idx].signal];
1693}
1694
1695NodePath SceneState::get_connection_target(int p_idx) const {
1696 ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
1697 if (connections[p_idx].to & FLAG_ID_IS_PATH) {
1698 return node_paths[connections[p_idx].to & FLAG_MASK];
1699 } else {
1700 return get_node_path(connections[p_idx].to & FLAG_MASK);
1701 }
1702}
1703
1704StringName SceneState::get_connection_method(int p_idx) const {
1705 ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
1706 return names[connections[p_idx].method];
1707}
1708
1709int SceneState::get_connection_flags(int p_idx) const {
1710 ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
1711 return connections[p_idx].flags;
1712}
1713
1714int SceneState::get_connection_unbinds(int p_idx) const {
1715 ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
1716 return connections[p_idx].unbinds;
1717}
1718
1719Array SceneState::get_connection_binds(int p_idx) const {
1720 ERR_FAIL_INDEX_V(p_idx, connections.size(), Array());
1721 Array binds;
1722 for (int i = 0; i < connections[p_idx].binds.size(); i++) {
1723 binds.push_back(variants[connections[p_idx].binds[i]]);
1724 }
1725 return binds;
1726}
1727
1728bool SceneState::has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance) {
1729 // this method cannot be const because of this
1730 Ref<SceneState> ss = this;
1731
1732 do {
1733 for (int i = 0; i < ss->connections.size(); i++) {
1734 const ConnectionData &c = ss->connections[i];
1735
1736 NodePath np_from;
1737
1738 if (c.from & FLAG_ID_IS_PATH) {
1739 np_from = ss->node_paths[c.from & FLAG_MASK];
1740 } else {
1741 np_from = ss->get_node_path(c.from);
1742 }
1743
1744 NodePath np_to;
1745
1746 if (c.to & FLAG_ID_IS_PATH) {
1747 np_to = ss->node_paths[c.to & FLAG_MASK];
1748 } else {
1749 np_to = ss->get_node_path(c.to);
1750 }
1751
1752 StringName sn_signal = ss->names[c.signal];
1753 StringName sn_method = ss->names[c.method];
1754
1755 if (np_from == p_node_from && sn_signal == p_signal && np_to == p_node_to && sn_method == p_method) {
1756 return true;
1757 }
1758 }
1759
1760 if (p_no_inheritance) {
1761 break;
1762 }
1763
1764 ss = ss->get_base_scene_state();
1765 } while (ss.is_valid());
1766
1767 return false;
1768}
1769
1770Vector<NodePath> SceneState::get_editable_instances() const {
1771 return editable_instances;
1772}
1773
1774//add
1775
1776int SceneState::add_name(const StringName &p_name) {
1777 names.push_back(p_name);
1778 return names.size() - 1;
1779}
1780
1781int SceneState::add_value(const Variant &p_value) {
1782 variants.push_back(p_value);
1783 return variants.size() - 1;
1784}
1785
1786int SceneState::add_node_path(const NodePath &p_path) {
1787 node_paths.push_back(p_path);
1788 return (node_paths.size() - 1) | FLAG_ID_IS_PATH;
1789}
1790
1791int SceneState::add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index) {
1792 NodeData nd;
1793 nd.parent = p_parent;
1794 nd.owner = p_owner;
1795 nd.type = p_type;
1796 nd.name = p_name;
1797 nd.instance = p_instance;
1798 nd.index = p_index;
1799
1800 nodes.push_back(nd);
1801
1802 return nodes.size() - 1;
1803}
1804
1805void SceneState::add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path) {
1806 ERR_FAIL_INDEX(p_node, nodes.size());
1807 ERR_FAIL_INDEX(p_name, names.size());
1808 ERR_FAIL_INDEX(p_value, variants.size());
1809
1810 NodeData::Property prop;
1811 prop.name = p_name;
1812 if (p_deferred_node_path) {
1813 prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
1814 }
1815 prop.value = p_value;
1816 nodes.write[p_node].properties.push_back(prop);
1817}
1818
1819void SceneState::add_node_group(int p_node, int p_group) {
1820 ERR_FAIL_INDEX(p_node, nodes.size());
1821 ERR_FAIL_INDEX(p_group, names.size());
1822 nodes.write[p_node].groups.push_back(p_group);
1823}
1824
1825void SceneState::set_base_scene(int p_idx) {
1826 ERR_FAIL_INDEX(p_idx, variants.size());
1827 base_scene_idx = p_idx;
1828}
1829
1830void SceneState::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) {
1831 ERR_FAIL_INDEX(p_signal, names.size());
1832 ERR_FAIL_INDEX(p_method, names.size());
1833
1834 for (int i = 0; i < p_binds.size(); i++) {
1835 ERR_FAIL_INDEX(p_binds[i], variants.size());
1836 }
1837 ConnectionData c;
1838 c.from = p_from;
1839 c.to = p_to;
1840 c.signal = p_signal;
1841 c.method = p_method;
1842 c.flags = p_flags;
1843 c.unbinds = p_unbinds;
1844 c.binds = p_binds;
1845 connections.push_back(c);
1846}
1847
1848void SceneState::add_editable_instance(const NodePath &p_path) {
1849 editable_instances.push_back(p_path);
1850}
1851
1852Vector<String> SceneState::_get_node_groups(int p_idx) const {
1853 Vector<StringName> groups = get_node_groups(p_idx);
1854 Vector<String> ret;
1855
1856 for (int i = 0; i < groups.size(); i++) {
1857 ret.push_back(groups[i]);
1858 }
1859
1860 return ret;
1861}
1862
1863void SceneState::_bind_methods() {
1864 //unbuild API
1865
1866 ClassDB::bind_method(D_METHOD("get_node_count"), &SceneState::get_node_count);
1867 ClassDB::bind_method(D_METHOD("get_node_type", "idx"), &SceneState::get_node_type);
1868 ClassDB::bind_method(D_METHOD("get_node_name", "idx"), &SceneState::get_node_name);
1869 ClassDB::bind_method(D_METHOD("get_node_path", "idx", "for_parent"), &SceneState::get_node_path, DEFVAL(false));
1870 ClassDB::bind_method(D_METHOD("get_node_owner_path", "idx"), &SceneState::get_node_owner_path);
1871 ClassDB::bind_method(D_METHOD("is_node_instance_placeholder", "idx"), &SceneState::is_node_instance_placeholder);
1872 ClassDB::bind_method(D_METHOD("get_node_instance_placeholder", "idx"), &SceneState::get_node_instance_placeholder);
1873 ClassDB::bind_method(D_METHOD("get_node_instance", "idx"), &SceneState::get_node_instance);
1874 ClassDB::bind_method(D_METHOD("get_node_groups", "idx"), &SceneState::_get_node_groups);
1875 ClassDB::bind_method(D_METHOD("get_node_index", "idx"), &SceneState::get_node_index);
1876 ClassDB::bind_method(D_METHOD("get_node_property_count", "idx"), &SceneState::get_node_property_count);
1877 ClassDB::bind_method(D_METHOD("get_node_property_name", "idx", "prop_idx"), &SceneState::get_node_property_name);
1878 ClassDB::bind_method(D_METHOD("get_node_property_value", "idx", "prop_idx"), &SceneState::get_node_property_value);
1879 ClassDB::bind_method(D_METHOD("get_connection_count"), &SceneState::get_connection_count);
1880 ClassDB::bind_method(D_METHOD("get_connection_source", "idx"), &SceneState::get_connection_source);
1881 ClassDB::bind_method(D_METHOD("get_connection_signal", "idx"), &SceneState::get_connection_signal);
1882 ClassDB::bind_method(D_METHOD("get_connection_target", "idx"), &SceneState::get_connection_target);
1883 ClassDB::bind_method(D_METHOD("get_connection_method", "idx"), &SceneState::get_connection_method);
1884 ClassDB::bind_method(D_METHOD("get_connection_flags", "idx"), &SceneState::get_connection_flags);
1885 ClassDB::bind_method(D_METHOD("get_connection_binds", "idx"), &SceneState::get_connection_binds);
1886 ClassDB::bind_method(D_METHOD("get_connection_unbinds", "idx"), &SceneState::get_connection_unbinds);
1887
1888 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
1889 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
1890 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
1891 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
1892}
1893
1894SceneState::SceneState() {
1895}
1896
1897////////////////
1898
1899void PackedScene::_set_bundled_scene(const Dictionary &p_scene) {
1900 state->set_bundled_scene(p_scene);
1901}
1902
1903Dictionary PackedScene::_get_bundled_scene() const {
1904 return state->get_bundled_scene();
1905}
1906
1907Error PackedScene::pack(Node *p_scene) {
1908 return state->pack(p_scene);
1909}
1910
1911void PackedScene::clear() {
1912 state->clear();
1913}
1914
1915void PackedScene::reload_from_file() {
1916 String path = get_path();
1917 if (!path.is_resource_file()) {
1918 return;
1919 }
1920
1921 Ref<PackedScene> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
1922 if (!s.is_valid()) {
1923 return;
1924 }
1925
1926 // Backup the loaded_state
1927 Ref<SceneState> loaded_state = s->get_state();
1928 // This assigns a new state to s->state
1929 // We do this because of the next step
1930 s->recreate_state();
1931 // This has a side-effect to clear s->state
1932 copy_from(s);
1933 // Then, we copy the backed-up loaded_state to state
1934 state->copy_from(loaded_state);
1935}
1936
1937bool PackedScene::can_instantiate() const {
1938 return state->can_instantiate();
1939}
1940
1941Node *PackedScene::instantiate(GenEditState p_edit_state) const {
1942#ifndef TOOLS_ENABLED
1943 ERR_FAIL_COND_V_MSG(p_edit_state != GEN_EDIT_STATE_DISABLED, nullptr, "Edit state is only for editors, does not work without tools compiled.");
1944#endif
1945
1946 Node *s = state->instantiate((SceneState::GenEditState)p_edit_state);
1947 if (!s) {
1948 return nullptr;
1949 }
1950
1951 if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
1952 s->set_scene_instance_state(state);
1953 }
1954
1955 if (!is_built_in()) {
1956 s->set_scene_file_path(get_path());
1957 }
1958
1959 s->notification(Node::NOTIFICATION_SCENE_INSTANTIATED);
1960
1961 return s;
1962}
1963
1964void PackedScene::replace_state(Ref<SceneState> p_by) {
1965 state = p_by;
1966 state->set_path(get_path());
1967#ifdef TOOLS_ENABLED
1968 state->set_last_modified_time(get_last_modified_time());
1969#endif
1970}
1971
1972void PackedScene::recreate_state() {
1973 state = Ref<SceneState>(memnew(SceneState));
1974 state->set_path(get_path());
1975#ifdef TOOLS_ENABLED
1976 state->set_last_modified_time(get_last_modified_time());
1977#endif
1978}
1979
1980Ref<SceneState> PackedScene::get_state() const {
1981 return state;
1982}
1983
1984void PackedScene::set_path(const String &p_path, bool p_take_over) {
1985 state->set_path(p_path);
1986 Resource::set_path(p_path, p_take_over);
1987}
1988
1989void PackedScene::reset_state() {
1990 clear();
1991}
1992void PackedScene::_bind_methods() {
1993 ClassDB::bind_method(D_METHOD("pack", "path"), &PackedScene::pack);
1994 ClassDB::bind_method(D_METHOD("instantiate", "edit_state"), &PackedScene::instantiate, DEFVAL(GEN_EDIT_STATE_DISABLED));
1995 ClassDB::bind_method(D_METHOD("can_instantiate"), &PackedScene::can_instantiate);
1996 ClassDB::bind_method(D_METHOD("_set_bundled_scene", "scene"), &PackedScene::_set_bundled_scene);
1997 ClassDB::bind_method(D_METHOD("_get_bundled_scene"), &PackedScene::_get_bundled_scene);
1998 ClassDB::bind_method(D_METHOD("get_state"), &PackedScene::get_state);
1999
2000 ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled"), "_set_bundled_scene", "_get_bundled_scene");
2001
2002 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
2003 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
2004 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
2005 BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
2006}
2007
2008PackedScene::PackedScene() {
2009 state = Ref<SceneState>(memnew(SceneState));
2010}
2011