packed_scene.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. #include "packed_scene.h"
  31. #include "core/core_string_names.h"
  32. #include "core/engine.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/project_settings.h"
  35. #include "editor/editor_inspector.h"
  36. #include "scene/2d/node_2d.h"
  37. #include "scene/3d/spatial.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/main/instance_placeholder.h"
  40. #include "scene/property_utils.h"
  41. #define PACKED_SCENE_VERSION 2
  42. bool SceneState::can_instance() const {
  43. return nodes.size() > 0;
  44. }
  45. static Array _sanitize_node_pinned_properties(Node *p_node) {
  46. if (!p_node->has_meta("_edit_pinned_properties_")) {
  47. return Array();
  48. }
  49. Array pinned = p_node->get_meta("_edit_pinned_properties_");
  50. if (pinned.empty()) {
  51. return Array();
  52. }
  53. Set<StringName> storable_properties;
  54. p_node->get_storable_properties(storable_properties);
  55. int i = 0;
  56. do {
  57. if (storable_properties.has(pinned[i])) {
  58. i++;
  59. } else {
  60. pinned.remove(i);
  61. }
  62. } while (i < pinned.size());
  63. if (pinned.empty()) {
  64. p_node->remove_meta("_edit_pinned_properties_");
  65. }
  66. return pinned;
  67. }
  68. Node *SceneState::instance(GenEditState p_edit_state) const {
  69. // nodes where instancing failed (because something is missing)
  70. List<Node *> stray_instances;
  71. #define NODE_FROM_ID(p_name, p_id) \
  72. Node *p_name; \
  73. if (p_id & FLAG_ID_IS_PATH) { \
  74. NodePath np = node_paths[p_id & FLAG_MASK]; \
  75. p_name = ret_nodes[0]->get_node_or_null(np); \
  76. } else { \
  77. ERR_FAIL_INDEX_V(p_id &FLAG_MASK, nc, NULL); \
  78. p_name = ret_nodes[p_id & FLAG_MASK]; \
  79. }
  80. int nc = nodes.size();
  81. ERR_FAIL_COND_V(nc == 0, nullptr);
  82. const StringName *snames = nullptr;
  83. int sname_count = names.size();
  84. if (sname_count) {
  85. snames = &names[0];
  86. }
  87. const Variant *props = nullptr;
  88. int prop_count = variants.size();
  89. if (prop_count) {
  90. props = &variants[0];
  91. }
  92. //Vector<Variant> properties;
  93. const NodeData *nd = &nodes[0];
  94. Node **ret_nodes = (Node **)alloca(sizeof(Node *) * nc);
  95. bool gen_node_path_cache = p_edit_state != GEN_EDIT_STATE_DISABLED && node_path_cache.empty();
  96. Map<Ref<Resource>, Ref<Resource>> resources_local_to_scene;
  97. for (int i = 0; i < nc; i++) {
  98. const NodeData &n = nd[i];
  99. Node *parent = nullptr;
  100. String old_parent_path;
  101. if (i > 0) {
  102. ERR_FAIL_COND_V_MSG(n.parent == -1, nullptr, vformat("Invalid scene: node %s does not specify its parent node.", snames[n.name]));
  103. NODE_FROM_ID(nparent, n.parent);
  104. #ifdef DEBUG_ENABLED
  105. if (!nparent && (n.parent & FLAG_ID_IS_PATH)) {
  106. WARN_PRINT(String("Parent path '" + String(node_paths[n.parent & FLAG_MASK]) + "' for node '" + String(snames[n.name]) + "' has vanished when instancing: '" + get_path() + "'.").ascii().get_data());
  107. old_parent_path = String(node_paths[n.parent & FLAG_MASK]).trim_prefix("./").replace("/", "@");
  108. nparent = ret_nodes[0];
  109. }
  110. #endif
  111. parent = nparent;
  112. } else {
  113. // i == 0 is root node.
  114. ERR_FAIL_COND_V_MSG(n.parent != -1, nullptr, vformat("Invalid scene: root node %s cannot specify a parent node.", snames[n.name]));
  115. ERR_FAIL_COND_V_MSG(n.type == TYPE_INSTANCED && base_scene_idx < 0, nullptr, vformat("Invalid scene: root node %s in an instance, but there's no base scene.", snames[n.name]));
  116. }
  117. Node *node = nullptr;
  118. if (i == 0 && base_scene_idx >= 0) {
  119. //scene inheritance on root node
  120. Ref<PackedScene> sdata = props[base_scene_idx];
  121. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  122. node = sdata->instance(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE); //only main gets main edit state
  123. ERR_FAIL_COND_V(!node, nullptr);
  124. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  125. node->set_scene_inherited_state(sdata->get_state());
  126. }
  127. } else if (n.instance >= 0) {
  128. //instance a scene into this node
  129. if (n.instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  130. String path = props[n.instance & FLAG_MASK];
  131. if (disable_placeholders) {
  132. Ref<PackedScene> sdata = ResourceLoader::load(path, "PackedScene");
  133. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  134. node = sdata->instance(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  135. ERR_FAIL_COND_V(!node, nullptr);
  136. } else {
  137. InstancePlaceholder *ip = memnew(InstancePlaceholder);
  138. ip->set_instance_path(path);
  139. node = ip;
  140. }
  141. node->set_scene_instance_load_placeholder(true);
  142. } else {
  143. Ref<PackedScene> sdata = props[n.instance & FLAG_MASK];
  144. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  145. node = sdata->instance(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  146. ERR_FAIL_COND_V(!node, nullptr);
  147. }
  148. } else if (n.type == TYPE_INSTANCED) {
  149. //get the node from somewhere, it likely already exists from another instance
  150. if (parent) {
  151. node = parent->_get_child_by_name(snames[n.name]);
  152. #ifdef DEBUG_ENABLED
  153. if (!node) {
  154. 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());
  155. }
  156. #endif
  157. }
  158. } else {
  159. //node belongs to this scene and must be created
  160. Object *obj = ClassDB::instance(snames[n.type]);
  161. node = Object::cast_to<Node>(obj);
  162. if (!node) {
  163. if (obj) {
  164. memdelete(obj);
  165. obj = nullptr;
  166. }
  167. 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());
  168. if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
  169. if (Object::cast_to<Spatial>(ret_nodes[n.parent])) {
  170. obj = memnew(Spatial);
  171. } else if (Object::cast_to<Control>(ret_nodes[n.parent])) {
  172. obj = memnew(Control);
  173. } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
  174. obj = memnew(Node2D);
  175. }
  176. }
  177. if (!obj) {
  178. obj = memnew(Node);
  179. }
  180. node = Object::cast_to<Node>(obj);
  181. }
  182. }
  183. if (node) {
  184. // may not have found the node (part of instanced scene and removed)
  185. // if found all is good, otherwise ignore
  186. //properties
  187. int nprop_count = n.properties.size();
  188. if (nprop_count) {
  189. const NodeData::Property *nprops = &n.properties[0];
  190. for (int j = 0; j < nprop_count; j++) {
  191. bool valid;
  192. ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);
  193. ERR_FAIL_INDEX_V(nprops[j].value, prop_count, nullptr);
  194. if (snames[nprops[j].name] == CoreStringNames::get_singleton()->_script) {
  195. //work around to avoid old script variables from disappearing, should be the proper fix to:
  196. //https://github.com/godotengine/godot/issues/2958
  197. //store old state
  198. List<Pair<StringName, Variant>> old_state;
  199. if (node->get_script_instance()) {
  200. node->get_script_instance()->get_property_state(old_state);
  201. }
  202. node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
  203. //restore old state for new script, if exists
  204. for (List<Pair<StringName, Variant>>::Element *E = old_state.front(); E; E = E->next()) {
  205. node->set(E->get().first, E->get().second);
  206. }
  207. } else {
  208. Variant value = props[nprops[j].value];
  209. if (value.get_type() == Variant::OBJECT) {
  210. //handle resources that are local to scene by duplicating them if needed
  211. Ref<Resource> res = value;
  212. if (res.is_valid()) {
  213. if (res->is_local_to_scene()) {
  214. Map<Ref<Resource>, Ref<Resource>>::Element *E = resources_local_to_scene.find(res);
  215. if (E) {
  216. value = E->get();
  217. } else {
  218. Node *base = i == 0 ? node : ret_nodes[0];
  219. if (p_edit_state == GEN_EDIT_STATE_MAIN || p_edit_state == GEN_EDIT_STATE_MAIN_INHERITED) {
  220. //for the main scene, use the resource as is
  221. res->configure_for_local_scene(base, resources_local_to_scene);
  222. resources_local_to_scene[res] = res;
  223. } else {
  224. //for instances, a copy must be made
  225. Node *base2 = i == 0 ? node : ret_nodes[0];
  226. Ref<Resource> local_dupe = res->duplicate_for_local_scene(base2, resources_local_to_scene);
  227. resources_local_to_scene[res] = local_dupe;
  228. res = local_dupe;
  229. value = local_dupe;
  230. }
  231. }
  232. //must make a copy, because this res is local to scene
  233. }
  234. }
  235. } else if (p_edit_state == GEN_EDIT_STATE_INSTANCE) {
  236. value = value.duplicate(true); // Duplicate arrays and dictionaries for the editor
  237. }
  238. node->set(snames[nprops[j].name], value, &valid);
  239. }
  240. }
  241. }
  242. //name
  243. //groups
  244. for (int j = 0; j < n.groups.size(); j++) {
  245. ERR_FAIL_INDEX_V(n.groups[j], sname_count, nullptr);
  246. node->add_to_group(snames[n.groups[j]], true);
  247. }
  248. if (n.instance >= 0 || n.type != TYPE_INSTANCED || i == 0) {
  249. //if node was not part of instance, must set its name, parenthood and ownership
  250. if (i > 0) {
  251. if (parent) {
  252. parent->_add_child_nocheck(node, snames[n.name]);
  253. if (n.index >= 0 && n.index < parent->get_child_count() - 1) {
  254. parent->move_child(node, n.index);
  255. }
  256. } else {
  257. //it may be possible that an instanced scene has changed
  258. //and the node has nowhere to go anymore
  259. stray_instances.push_back(node); //can't be added, go to stray list
  260. }
  261. } else {
  262. if (Engine::get_singleton()->is_editor_hint()) {
  263. //validate name if using editor, to avoid broken
  264. node->set_name(snames[n.name]);
  265. } else {
  266. node->_set_name_nocheck(snames[n.name]);
  267. }
  268. }
  269. }
  270. if (!old_parent_path.empty()) {
  271. node->_set_name_nocheck(old_parent_path + "@" + node->get_name());
  272. }
  273. if (n.owner >= 0) {
  274. NODE_FROM_ID(owner, n.owner);
  275. if (owner) {
  276. node->_set_owner_nocheck(owner);
  277. if (node->data.unique_name_in_owner) {
  278. node->_acquire_unique_name_in_owner();
  279. }
  280. }
  281. }
  282. // we only want to deal with pinned flag if instancing as pure main (no instance, no inheriting)
  283. if (p_edit_state == GEN_EDIT_STATE_MAIN) {
  284. _sanitize_node_pinned_properties(node);
  285. } else {
  286. node->remove_meta("_edit_pinned_properties_");
  287. }
  288. }
  289. ret_nodes[i] = node;
  290. if (node && gen_node_path_cache && ret_nodes[0]) {
  291. NodePath n2 = ret_nodes[0]->get_path_to(node);
  292. node_path_cache[n2] = i;
  293. }
  294. }
  295. for (Map<Ref<Resource>, Ref<Resource>>::Element *E = resources_local_to_scene.front(); E; E = E->next()) {
  296. E->get()->setup_local_to_scene();
  297. }
  298. //do connections
  299. int cc = connections.size();
  300. const ConnectionData *cdata = connections.ptr();
  301. for (int i = 0; i < cc; i++) {
  302. const ConnectionData &c = cdata[i];
  303. //ERR_FAIL_INDEX_V( c.from, nc, NULL );
  304. //ERR_FAIL_INDEX_V( c.to, nc, NULL );
  305. NODE_FROM_ID(cfrom, c.from);
  306. NODE_FROM_ID(cto, c.to);
  307. if (!cfrom || !cto) {
  308. continue;
  309. }
  310. Vector<Variant> binds;
  311. if (c.binds.size()) {
  312. binds.resize(c.binds.size());
  313. for (int j = 0; j < c.binds.size(); j++) {
  314. binds.write[j] = props[c.binds[j]];
  315. }
  316. }
  317. cfrom->connect(snames[c.signal], cto, snames[c.method], binds, CONNECT_PERSIST | c.flags);
  318. }
  319. //Node *s = ret_nodes[0];
  320. //remove nodes that could not be added, likely as a result that
  321. while (stray_instances.size()) {
  322. memdelete(stray_instances.front()->get());
  323. stray_instances.pop_front();
  324. }
  325. for (int i = 0; i < editable_instances.size(); i++) {
  326. Node *ei = ret_nodes[0]->get_node_or_null(editable_instances[i]);
  327. if (ei) {
  328. ret_nodes[0]->set_editable_instance(ei, true);
  329. }
  330. }
  331. return ret_nodes[0];
  332. }
  333. static int _nm_get_string(const String &p_string, Map<StringName, int> &name_map) {
  334. if (name_map.has(p_string)) {
  335. return name_map[p_string];
  336. }
  337. int idx = name_map.size();
  338. name_map[p_string] = idx;
  339. return idx;
  340. }
  341. static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) {
  342. if (variant_map.has(p_variant)) {
  343. return variant_map[p_variant];
  344. }
  345. int idx = variant_map.size();
  346. variant_map[p_variant] = idx;
  347. return idx;
  348. }
  349. Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, Map<Node *, int> &node_map, Map<Node *, int> &nodepath_map) {
  350. // this function handles all the work related to properly packing scenes, be it
  351. // instanced or inherited.
  352. // given the complexity of this process, an attempt will be made to properly
  353. // document it. if you fail to understand something, please ask!
  354. //discard nodes that do not belong to be processed
  355. if (p_node != p_owner && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  356. return OK;
  357. }
  358. bool is_editable_instance = false;
  359. // save the child instantiated scenes that are chosen as editable, so they can be restored
  360. // upon load back
  361. if (p_node != p_owner && p_node->get_filename() != String() && p_owner->is_editable_instance(p_node)) {
  362. editable_instances.push_back(p_owner->get_path_to(p_node));
  363. // Node is the root of an editable instance.
  364. is_editable_instance = true;
  365. } else if (p_node->get_owner() && p_owner->is_a_parent_of(p_node->get_owner()) && p_owner->is_editable_instance(p_node->get_owner())) {
  366. // Node is part of an editable instance.
  367. is_editable_instance = true;
  368. }
  369. NodeData nd;
  370. nd.name = _nm_get_string(p_node->get_name(), name_map);
  371. nd.instance = -1; //not instanced by default
  372. //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
  373. 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)))) {
  374. //do not save index, because it belongs to saved scene and scene is not inherited
  375. nd.index = -1;
  376. } else if (p_node == p_owner) {
  377. //This (hopefully) happens if the node is a scene root, so its index is irrelevant.
  378. nd.index = -1;
  379. } else {
  380. //part of an inherited scene, or parent is from an instanced scene
  381. nd.index = p_node->get_index();
  382. }
  383. // if this node is part of an instanced scene or sub-instanced scene
  384. // we need to get the corresponding instance states.
  385. // with the instance states, we can query for identical properties/groups
  386. // and only save what has changed
  387. bool instanced_by_owner = false;
  388. Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instanced_by_owner);
  389. if (p_node->get_filename() != String() && p_node->get_owner() == p_owner && instanced_by_owner) {
  390. if (p_node->get_scene_instance_load_placeholder()) {
  391. //it's a placeholder, use the placeholder path
  392. nd.instance = _vm_get_variant(p_node->get_filename(), variant_map);
  393. nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
  394. } else {
  395. //must instance ourselves
  396. Ref<PackedScene> instance = ResourceLoader::load(p_node->get_filename());
  397. if (!instance.is_valid()) {
  398. return ERR_CANT_OPEN;
  399. }
  400. nd.instance = _vm_get_variant(instance, variant_map);
  401. }
  402. }
  403. // all setup, we then proceed to check all properties for the node
  404. // and save the ones that are worth saving
  405. List<PropertyInfo> plist;
  406. p_node->get_property_list(&plist);
  407. Array pinned_props = _sanitize_node_pinned_properties(p_node);
  408. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  409. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  410. continue;
  411. }
  412. Variant forced_value;
  413. // If instance or inheriting, not saving if property requested so, or it's meta
  414. if (states_stack.size()) {
  415. if ((E->get().usage & PROPERTY_USAGE_NO_INSTANCE_STATE)) {
  416. continue;
  417. }
  418. // Meta is normally not saved in instances/inherited (see GH-12838), but we need to save the pinned list
  419. if (E->get().name == "__meta__") {
  420. if (pinned_props.size()) {
  421. Dictionary meta_override;
  422. meta_override["_edit_pinned_properties_"] = pinned_props;
  423. forced_value = meta_override;
  424. }
  425. }
  426. }
  427. String name = E->get().name;
  428. Variant value = forced_value.get_type() == Variant::NIL ? p_node->get(name) : forced_value;
  429. if (!pinned_props.has(name) && forced_value.get_type() == Variant::NIL) {
  430. bool is_valid_default = false;
  431. Variant default_value = PropertyUtils::get_property_default_value(p_node, name, &is_valid_default, &states_stack, true);
  432. if (is_valid_default && !PropertyUtils::is_property_value_different(value, default_value)) {
  433. continue;
  434. }
  435. }
  436. NodeData::Property prop;
  437. prop.name = _nm_get_string(name, name_map);
  438. prop.value = _vm_get_variant(value, variant_map);
  439. nd.properties.push_back(prop);
  440. }
  441. // save the groups this node is into
  442. // discard groups that come from the original scene
  443. List<Node::GroupInfo> groups;
  444. p_node->get_groups(&groups);
  445. for (List<Node::GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  446. Node::GroupInfo &gi = E->get();
  447. if (!gi.persistent) {
  448. continue;
  449. }
  450. /*
  451. if (instance_state_node>=0 && instance_state->is_node_in_group(instance_state_node,gi.name))
  452. continue; //group was instanced, don't add here
  453. */
  454. bool skip = false;
  455. for (int i = 0; i < states_stack.size(); ++i) {
  456. const auto &ia = states_stack[i];
  457. //check all levels of pack to see if the group was added somewhere
  458. if (ia.state->is_node_in_group(ia.node, gi.name)) {
  459. skip = true;
  460. break;
  461. }
  462. }
  463. if (skip) {
  464. continue;
  465. }
  466. nd.groups.push_back(_nm_get_string(gi.name, name_map));
  467. }
  468. // save the right owner
  469. // for the saved scene root this is -1
  470. // for nodes of the saved scene this is 0
  471. // for nodes of instanced scenes this is >0
  472. if (p_node == p_owner) {
  473. //saved scene root
  474. nd.owner = -1;
  475. } else if (p_node->get_owner() == p_owner) {
  476. //part of saved scene
  477. nd.owner = 0;
  478. } else {
  479. nd.owner = -1;
  480. }
  481. // Save the right type. If this node was created by an instance
  482. // then flag that the node should not be created but reused
  483. if (states_stack.empty() && !is_editable_instance) {
  484. //this node is not part of an instancing process, so save the type
  485. nd.type = _nm_get_string(p_node->get_class(), name_map);
  486. } else {
  487. // this node is part of an instanced process, so do not save the type.
  488. // instead, save that it was instanced
  489. nd.type = TYPE_INSTANCED;
  490. }
  491. // determine whether to save this node or not
  492. // if this node is part of an instanced sub-scene, we can skip storing it if basically
  493. // no properties changed and no groups were added to it.
  494. // below condition is true for all nodes of the scene being saved, and ones in subscenes
  495. // that hold changes
  496. bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
  497. save_node = save_node || p_node == p_owner; // owner is always saved
  498. save_node = save_node || (p_node->get_owner() == p_owner && instanced_by_owner); //part of scene and not instanced
  499. int idx = nodes.size();
  500. int parent_node = NO_PARENT_SAVED;
  501. if (save_node) {
  502. //don't save the node if nothing and subscene
  503. node_map[p_node] = idx;
  504. //ok validate parent node
  505. if (p_parent_idx == NO_PARENT_SAVED) {
  506. int sidx;
  507. if (nodepath_map.has(p_node->get_parent())) {
  508. sidx = nodepath_map[p_node->get_parent()];
  509. } else {
  510. sidx = nodepath_map.size();
  511. nodepath_map[p_node->get_parent()] = sidx;
  512. }
  513. nd.parent = FLAG_ID_IS_PATH | sidx;
  514. } else {
  515. nd.parent = p_parent_idx;
  516. }
  517. parent_node = idx;
  518. nodes.push_back(nd);
  519. }
  520. for (int i = 0; i < p_node->get_child_count(); i++) {
  521. Node *c = p_node->get_child(i);
  522. Error err = _parse_node(p_owner, c, parent_node, name_map, variant_map, node_map, nodepath_map);
  523. if (err) {
  524. return err;
  525. }
  526. }
  527. return OK;
  528. }
  529. Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, Map<Node *, int> &node_map, Map<Node *, int> &nodepath_map) {
  530. if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  531. return OK;
  532. }
  533. List<MethodInfo> _signals;
  534. p_node->get_signal_list(&_signals);
  535. _signals.sort();
  536. //ERR_FAIL_COND_V( !node_map.has(p_node), ERR_BUG);
  537. //NodeData &nd = nodes[node_map[p_node]];
  538. for (List<MethodInfo>::Element *E = _signals.front(); E; E = E->next()) {
  539. List<Node::Connection> conns;
  540. p_node->get_signal_connection_list(E->get().name, &conns);
  541. conns.sort();
  542. for (List<Node::Connection>::Element *F = conns.front(); F; F = F->next()) {
  543. const Node::Connection &c = F->get();
  544. if (!(c.flags & CONNECT_PERSIST)) { //only persistent connections get saved
  545. continue;
  546. }
  547. // only connections that originate or end into main saved scene are saved
  548. // everything else is discarded
  549. Node *target = Object::cast_to<Node>(c.target);
  550. if (!target) {
  551. continue;
  552. }
  553. //find if this connection already exists
  554. Node *common_parent = target->find_common_parent_with(p_node);
  555. ERR_CONTINUE(!common_parent);
  556. if (common_parent != p_owner && common_parent->get_filename() == String()) {
  557. common_parent = common_parent->get_owner();
  558. }
  559. bool exists = false;
  560. //go through ownership chain to see if this exists
  561. while (common_parent) {
  562. Ref<SceneState> ps;
  563. if (common_parent == p_owner) {
  564. ps = common_parent->get_scene_inherited_state();
  565. } else {
  566. ps = common_parent->get_scene_instance_state();
  567. }
  568. if (ps.is_valid()) {
  569. NodePath signal_from = common_parent->get_path_to(p_node);
  570. NodePath signal_to = common_parent->get_path_to(target);
  571. if (ps->has_connection(signal_from, c.signal, signal_to, c.method)) {
  572. exists = true;
  573. break;
  574. }
  575. }
  576. if (common_parent == p_owner) {
  577. break;
  578. } else {
  579. common_parent = common_parent->get_owner();
  580. }
  581. }
  582. if (exists) { //already exists (comes from instance or inheritance), so don't save
  583. continue;
  584. }
  585. {
  586. Node *nl = p_node;
  587. bool exists2 = false;
  588. while (nl) {
  589. if (nl == p_owner) {
  590. Ref<SceneState> state = nl->get_scene_inherited_state();
  591. if (state.is_valid()) {
  592. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  593. int to_node = state->find_node_by_path(nl->get_path_to(target));
  594. if (from_node >= 0 && to_node >= 0) {
  595. //this one has state for this node, save
  596. if (state->is_connection(from_node, c.signal, to_node, c.method)) {
  597. exists2 = true;
  598. break;
  599. }
  600. }
  601. }
  602. nl = nullptr;
  603. } else {
  604. if (nl->get_filename() != String()) {
  605. //is an instance
  606. Ref<SceneState> state = nl->get_scene_instance_state();
  607. if (state.is_valid()) {
  608. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  609. int to_node = state->find_node_by_path(nl->get_path_to(target));
  610. if (from_node >= 0 && to_node >= 0) {
  611. //this one has state for this node, save
  612. if (state->is_connection(from_node, c.signal, to_node, c.method)) {
  613. exists2 = true;
  614. break;
  615. }
  616. }
  617. }
  618. }
  619. nl = nl->get_owner();
  620. }
  621. }
  622. if (exists2) {
  623. continue;
  624. }
  625. }
  626. int src_id;
  627. if (node_map.has(p_node)) {
  628. src_id = node_map[p_node];
  629. } else {
  630. if (nodepath_map.has(p_node)) {
  631. src_id = FLAG_ID_IS_PATH | nodepath_map[p_node];
  632. } else {
  633. int sidx = nodepath_map.size();
  634. nodepath_map[p_node] = sidx;
  635. src_id = FLAG_ID_IS_PATH | sidx;
  636. }
  637. }
  638. int target_id;
  639. if (node_map.has(target)) {
  640. target_id = node_map[target];
  641. } else {
  642. if (nodepath_map.has(target)) {
  643. target_id = FLAG_ID_IS_PATH | nodepath_map[target];
  644. } else {
  645. int sidx = nodepath_map.size();
  646. nodepath_map[target] = sidx;
  647. target_id = FLAG_ID_IS_PATH | sidx;
  648. }
  649. }
  650. ConnectionData cd;
  651. cd.from = src_id;
  652. cd.to = target_id;
  653. cd.method = _nm_get_string(c.method, name_map);
  654. cd.signal = _nm_get_string(c.signal, name_map);
  655. cd.flags = c.flags;
  656. for (int i = 0; i < c.binds.size(); i++) {
  657. cd.binds.push_back(_vm_get_variant(c.binds[i], variant_map));
  658. }
  659. connections.push_back(cd);
  660. }
  661. }
  662. for (int i = 0; i < p_node->get_child_count(); i++) {
  663. Node *c = p_node->get_child(i);
  664. Error err = _parse_connections(p_owner, c, name_map, variant_map, node_map, nodepath_map);
  665. if (err) {
  666. return err;
  667. }
  668. }
  669. return OK;
  670. }
  671. Error SceneState::pack(Node *p_scene) {
  672. ERR_FAIL_NULL_V(p_scene, ERR_INVALID_PARAMETER);
  673. clear();
  674. Node *scene = p_scene;
  675. Map<StringName, int> name_map;
  676. HashMap<Variant, int, VariantHasher, VariantComparator> variant_map;
  677. Map<Node *, int> node_map;
  678. Map<Node *, int> nodepath_map;
  679. // If using scene inheritance, pack the scene it inherits from.
  680. if (scene->get_scene_inherited_state().is_valid()) {
  681. String path = scene->get_scene_inherited_state()->get_path();
  682. Ref<PackedScene> instance = ResourceLoader::load(path);
  683. if (instance.is_valid()) {
  684. base_scene_idx = _vm_get_variant(instance, variant_map);
  685. }
  686. }
  687. // Instanced, only direct sub-scenes are supported of course.
  688. Error err = _parse_node(scene, scene, -1, name_map, variant_map, node_map, nodepath_map);
  689. if (err) {
  690. clear();
  691. ERR_FAIL_V(err);
  692. }
  693. err = _parse_connections(scene, scene, name_map, variant_map, node_map, nodepath_map);
  694. if (err) {
  695. clear();
  696. ERR_FAIL_V(err);
  697. }
  698. names.resize(name_map.size());
  699. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  700. names.write[E->get()] = E->key();
  701. }
  702. variants.resize(variant_map.size());
  703. const Variant *K = nullptr;
  704. while ((K = variant_map.next(K))) {
  705. int idx = variant_map[*K];
  706. variants.write[idx] = *K;
  707. }
  708. node_paths.resize(nodepath_map.size());
  709. for (Map<Node *, int>::Element *E = nodepath_map.front(); E; E = E->next()) {
  710. node_paths.write[E->get()] = scene->get_path_to(E->key());
  711. }
  712. if (Engine::get_singleton()->is_editor_hint()) {
  713. // Build node path cache
  714. for (Map<Node *, int>::Element *E = node_map.front(); E; E = E->next()) {
  715. node_path_cache[scene->get_path_to(E->key())] = E->get();
  716. }
  717. }
  718. return OK;
  719. }
  720. void SceneState::set_path(const String &p_path) {
  721. path = p_path;
  722. }
  723. String SceneState::get_path() const {
  724. return path;
  725. }
  726. void SceneState::clear() {
  727. names.clear();
  728. variants.clear();
  729. nodes.clear();
  730. connections.clear();
  731. node_path_cache.clear();
  732. node_paths.clear();
  733. editable_instances.clear();
  734. base_scene_idx = -1;
  735. }
  736. Ref<SceneState> SceneState::get_base_scene_state() const {
  737. if (base_scene_idx >= 0) {
  738. Ref<PackedScene> ps = variants[base_scene_idx];
  739. if (ps.is_valid()) {
  740. return ps->get_state();
  741. }
  742. }
  743. return Ref<SceneState>();
  744. }
  745. int SceneState::find_node_by_path(const NodePath &p_node) const {
  746. ERR_FAIL_COND_V_MSG(node_path_cache.size() == 0, -1, "This operation requires the node cache to have been built.");
  747. if (!node_path_cache.has(p_node)) {
  748. if (get_base_scene_state().is_valid()) {
  749. int idx = get_base_scene_state()->find_node_by_path(p_node);
  750. if (idx != -1) {
  751. int rkey = _find_base_scene_node_remap_key(idx);
  752. if (rkey == -1) {
  753. rkey = nodes.size() + base_scene_node_remap.size();
  754. base_scene_node_remap[rkey] = idx;
  755. }
  756. return rkey;
  757. }
  758. }
  759. return -1;
  760. }
  761. int nid = node_path_cache[p_node];
  762. if (get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
  763. //for nodes that _do_ exist in current scene, still try to look for
  764. //the node in the instanced scene, as a property may be missing
  765. //from the local one
  766. int idx = get_base_scene_state()->find_node_by_path(p_node);
  767. if (idx != -1) {
  768. base_scene_node_remap[nid] = idx;
  769. }
  770. }
  771. return nid;
  772. }
  773. int SceneState::_find_base_scene_node_remap_key(int p_idx) const {
  774. for (Map<int, int>::Element *E = base_scene_node_remap.front(); E; E = E->next()) {
  775. if (E->value() == p_idx) {
  776. return E->key();
  777. }
  778. }
  779. return -1;
  780. }
  781. Variant SceneState::get_property_value(int p_node, const StringName &p_property, bool &found) const {
  782. found = false;
  783. ERR_FAIL_COND_V(p_node < 0, Variant());
  784. if (p_node < nodes.size()) {
  785. //find in built-in nodes
  786. int pc = nodes[p_node].properties.size();
  787. const StringName *namep = names.ptr();
  788. const NodeData::Property *p = nodes[p_node].properties.ptr();
  789. for (int i = 0; i < pc; i++) {
  790. if (p_property == namep[p[i].name]) {
  791. found = true;
  792. return variants[p[i].value];
  793. }
  794. }
  795. }
  796. //property not found, try on instance
  797. if (base_scene_node_remap.has(p_node)) {
  798. return get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
  799. }
  800. return Variant();
  801. }
  802. bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
  803. ERR_FAIL_COND_V(p_node < 0, false);
  804. if (p_node < nodes.size()) {
  805. const StringName *namep = names.ptr();
  806. for (int i = 0; i < nodes[p_node].groups.size(); i++) {
  807. if (namep[nodes[p_node].groups[i]] == p_group) {
  808. return true;
  809. }
  810. }
  811. }
  812. if (base_scene_node_remap.has(p_node)) {
  813. return get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
  814. }
  815. return false;
  816. }
  817. bool SceneState::disable_placeholders = false;
  818. void SceneState::set_disable_placeholders(bool p_disable) {
  819. disable_placeholders = p_disable;
  820. }
  821. bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const {
  822. ERR_FAIL_COND_V(p_node < 0, false);
  823. ERR_FAIL_COND_V(p_to_node < 0, false);
  824. if (p_node < nodes.size() && p_to_node < nodes.size()) {
  825. int signal_idx = -1;
  826. int method_idx = -1;
  827. for (int i = 0; i < names.size(); i++) {
  828. if (names[i] == p_signal) {
  829. signal_idx = i;
  830. } else if (names[i] == p_to_method) {
  831. method_idx = i;
  832. }
  833. }
  834. if (signal_idx >= 0 && method_idx >= 0) {
  835. //signal and method strings are stored..
  836. for (int i = 0; i < connections.size(); i++) {
  837. if (connections[i].from == p_node && connections[i].to == p_to_node && connections[i].signal == signal_idx && connections[i].method == method_idx) {
  838. return true;
  839. }
  840. }
  841. }
  842. }
  843. if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
  844. 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);
  845. }
  846. return false;
  847. }
  848. void SceneState::set_bundled_scene(const Dictionary &p_dictionary) {
  849. ERR_FAIL_COND(!p_dictionary.has("names"));
  850. ERR_FAIL_COND(!p_dictionary.has("variants"));
  851. ERR_FAIL_COND(!p_dictionary.has("node_count"));
  852. ERR_FAIL_COND(!p_dictionary.has("nodes"));
  853. ERR_FAIL_COND(!p_dictionary.has("conn_count"));
  854. ERR_FAIL_COND(!p_dictionary.has("conns"));
  855. //ERR_FAIL_COND( !p_dictionary.has("path"));
  856. int version = 1;
  857. if (p_dictionary.has("version")) {
  858. version = p_dictionary["version"];
  859. }
  860. ERR_FAIL_COND_MSG(version > PACKED_SCENE_VERSION, "Save format version too new.");
  861. const int node_count = p_dictionary["node_count"];
  862. const PoolVector<int> snodes = p_dictionary["nodes"];
  863. ERR_FAIL_COND(snodes.size() < node_count);
  864. const int conn_count = p_dictionary["conn_count"];
  865. const PoolVector<int> sconns = p_dictionary["conns"];
  866. ERR_FAIL_COND(sconns.size() < conn_count);
  867. PoolVector<String> snames = p_dictionary["names"];
  868. if (snames.size()) {
  869. int namecount = snames.size();
  870. names.resize(namecount);
  871. PoolVector<String>::Read r = snames.read();
  872. for (int i = 0; i < names.size(); i++) {
  873. names.write[i] = r[i];
  874. }
  875. }
  876. Array svariants = p_dictionary["variants"];
  877. if (svariants.size()) {
  878. int varcount = svariants.size();
  879. variants.resize(varcount);
  880. for (int i = 0; i < varcount; i++) {
  881. variants.write[i] = svariants[i];
  882. }
  883. } else {
  884. variants.clear();
  885. }
  886. nodes.resize(node_count);
  887. if (node_count) {
  888. PoolVector<int>::Read r = snodes.read();
  889. int idx = 0;
  890. for (int i = 0; i < node_count; i++) {
  891. NodeData &nd = nodes.write[i];
  892. nd.parent = r[idx++];
  893. nd.owner = r[idx++];
  894. nd.type = r[idx++];
  895. uint32_t name_index = r[idx++];
  896. nd.name = name_index & ((1 << NAME_INDEX_BITS) - 1);
  897. nd.index = (name_index >> NAME_INDEX_BITS);
  898. nd.index--; //0 is invalid, stored as 1
  899. nd.instance = r[idx++];
  900. nd.properties.resize(r[idx++]);
  901. for (int j = 0; j < nd.properties.size(); j++) {
  902. nd.properties.write[j].name = r[idx++];
  903. nd.properties.write[j].value = r[idx++];
  904. }
  905. nd.groups.resize(r[idx++]);
  906. for (int j = 0; j < nd.groups.size(); j++) {
  907. nd.groups.write[j] = r[idx++];
  908. }
  909. }
  910. }
  911. connections.resize(conn_count);
  912. if (conn_count) {
  913. PoolVector<int>::Read r = sconns.read();
  914. int idx = 0;
  915. for (int i = 0; i < conn_count; i++) {
  916. ConnectionData &cd = connections.write[i];
  917. cd.from = r[idx++];
  918. cd.to = r[idx++];
  919. cd.signal = r[idx++];
  920. cd.method = r[idx++];
  921. cd.flags = r[idx++];
  922. cd.binds.resize(r[idx++]);
  923. for (int j = 0; j < cd.binds.size(); j++) {
  924. cd.binds.write[j] = r[idx++];
  925. }
  926. }
  927. }
  928. Array np;
  929. if (p_dictionary.has("node_paths")) {
  930. np = p_dictionary["node_paths"];
  931. }
  932. node_paths.resize(np.size());
  933. for (int i = 0; i < np.size(); i++) {
  934. node_paths.write[i] = np[i];
  935. }
  936. Array ei;
  937. if (p_dictionary.has("editable_instances")) {
  938. ei = p_dictionary["editable_instances"];
  939. }
  940. if (p_dictionary.has("base_scene")) {
  941. base_scene_idx = p_dictionary["base_scene"];
  942. }
  943. editable_instances.resize(ei.size());
  944. for (int i = 0; i < editable_instances.size(); i++) {
  945. editable_instances.write[i] = ei[i];
  946. }
  947. //path=p_dictionary["path"];
  948. }
  949. Dictionary SceneState::get_bundled_scene() const {
  950. PoolVector<String> rnames;
  951. rnames.resize(names.size());
  952. if (names.size()) {
  953. PoolVector<String>::Write r = rnames.write();
  954. for (int i = 0; i < names.size(); i++) {
  955. r[i] = names[i];
  956. }
  957. }
  958. Dictionary d;
  959. d["names"] = rnames;
  960. d["variants"] = variants;
  961. Vector<int> rnodes;
  962. d["node_count"] = nodes.size();
  963. for (int i = 0; i < nodes.size(); i++) {
  964. const NodeData &nd = nodes[i];
  965. rnodes.push_back(nd.parent);
  966. rnodes.push_back(nd.owner);
  967. rnodes.push_back(nd.type);
  968. uint32_t name_index = nd.name;
  969. if (nd.index < (1 << (32 - NAME_INDEX_BITS)) - 1) { //save if less than 16k children
  970. name_index |= uint32_t(nd.index + 1) << NAME_INDEX_BITS; //for backwards compatibility, index 0 is no index
  971. }
  972. rnodes.push_back(name_index);
  973. rnodes.push_back(nd.instance);
  974. rnodes.push_back(nd.properties.size());
  975. for (int j = 0; j < nd.properties.size(); j++) {
  976. rnodes.push_back(nd.properties[j].name);
  977. rnodes.push_back(nd.properties[j].value);
  978. }
  979. rnodes.push_back(nd.groups.size());
  980. for (int j = 0; j < nd.groups.size(); j++) {
  981. rnodes.push_back(nd.groups[j]);
  982. }
  983. }
  984. d["nodes"] = rnodes;
  985. Vector<int> rconns;
  986. d["conn_count"] = connections.size();
  987. for (int i = 0; i < connections.size(); i++) {
  988. const ConnectionData &cd = connections[i];
  989. rconns.push_back(cd.from);
  990. rconns.push_back(cd.to);
  991. rconns.push_back(cd.signal);
  992. rconns.push_back(cd.method);
  993. rconns.push_back(cd.flags);
  994. rconns.push_back(cd.binds.size());
  995. for (int j = 0; j < cd.binds.size(); j++) {
  996. rconns.push_back(cd.binds[j]);
  997. }
  998. }
  999. d["conns"] = rconns;
  1000. Array rnode_paths;
  1001. rnode_paths.resize(node_paths.size());
  1002. for (int i = 0; i < node_paths.size(); i++) {
  1003. rnode_paths[i] = node_paths[i];
  1004. }
  1005. d["node_paths"] = rnode_paths;
  1006. Array reditable_instances;
  1007. reditable_instances.resize(editable_instances.size());
  1008. for (int i = 0; i < editable_instances.size(); i++) {
  1009. reditable_instances[i] = editable_instances[i];
  1010. }
  1011. d["editable_instances"] = reditable_instances;
  1012. if (base_scene_idx >= 0) {
  1013. d["base_scene"] = base_scene_idx;
  1014. }
  1015. d["version"] = PACKED_SCENE_VERSION;
  1016. return d;
  1017. }
  1018. int SceneState::get_node_count() const {
  1019. return nodes.size();
  1020. }
  1021. StringName SceneState::get_node_type(int p_idx) const {
  1022. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1023. if (nodes[p_idx].type == TYPE_INSTANCED) {
  1024. return StringName();
  1025. }
  1026. return names[nodes[p_idx].type];
  1027. }
  1028. StringName SceneState::get_node_name(int p_idx) const {
  1029. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1030. return names[nodes[p_idx].name];
  1031. }
  1032. int SceneState::get_node_index(int p_idx) const {
  1033. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1034. return nodes[p_idx].index;
  1035. }
  1036. bool SceneState::is_node_instance_placeholder(int p_idx) const {
  1037. ERR_FAIL_INDEX_V(p_idx, nodes.size(), false);
  1038. return nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER);
  1039. }
  1040. Ref<PackedScene> SceneState::get_node_instance(int p_idx) const {
  1041. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Ref<PackedScene>());
  1042. if (nodes[p_idx].instance >= 0) {
  1043. if (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  1044. return Ref<PackedScene>();
  1045. } else {
  1046. return variants[nodes[p_idx].instance & FLAG_MASK];
  1047. }
  1048. } else if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1049. if (base_scene_idx >= 0) {
  1050. return variants[base_scene_idx];
  1051. }
  1052. }
  1053. return Ref<PackedScene>();
  1054. }
  1055. String SceneState::get_node_instance_placeholder(int p_idx) const {
  1056. ERR_FAIL_INDEX_V(p_idx, nodes.size(), String());
  1057. if (nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
  1058. return variants[nodes[p_idx].instance & FLAG_MASK];
  1059. }
  1060. return String();
  1061. }
  1062. Vector<StringName> SceneState::get_node_groups(int p_idx) const {
  1063. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Vector<StringName>());
  1064. Vector<StringName> groups;
  1065. for (int i = 0; i < nodes[p_idx].groups.size(); i++) {
  1066. groups.push_back(names[nodes[p_idx].groups[i]]);
  1067. }
  1068. return groups;
  1069. }
  1070. NodePath SceneState::get_node_path(int p_idx, bool p_for_parent) const {
  1071. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1072. if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1073. if (p_for_parent) {
  1074. return NodePath();
  1075. } else {
  1076. return NodePath(".");
  1077. }
  1078. }
  1079. Vector<StringName> sub_path;
  1080. NodePath base_path;
  1081. int nidx = p_idx;
  1082. while (true) {
  1083. if (nodes[nidx].parent == NO_PARENT_SAVED || nodes[nidx].parent < 0) {
  1084. sub_path.insert(0, ".");
  1085. break;
  1086. }
  1087. if (!p_for_parent || p_idx != nidx) {
  1088. sub_path.insert(0, names[nodes[nidx].name]);
  1089. }
  1090. if (nodes[nidx].parent & FLAG_ID_IS_PATH) {
  1091. base_path = node_paths[nodes[nidx].parent & FLAG_MASK];
  1092. break;
  1093. } else {
  1094. nidx = nodes[nidx].parent & FLAG_MASK;
  1095. }
  1096. }
  1097. for (int i = base_path.get_name_count() - 1; i >= 0; i--) {
  1098. sub_path.insert(0, base_path.get_name(i));
  1099. }
  1100. if (sub_path.empty()) {
  1101. return NodePath(".");
  1102. }
  1103. return NodePath(sub_path, false);
  1104. }
  1105. int SceneState::get_node_property_count(int p_idx) const {
  1106. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1107. return nodes[p_idx].properties.size();
  1108. }
  1109. StringName SceneState::get_node_property_name(int p_idx, int p_prop) const {
  1110. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1111. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), StringName());
  1112. return names[nodes[p_idx].properties[p_prop].name];
  1113. }
  1114. Variant SceneState::get_node_property_value(int p_idx, int p_prop) const {
  1115. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Variant());
  1116. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), Variant());
  1117. return variants[nodes[p_idx].properties[p_prop].value];
  1118. }
  1119. NodePath SceneState::get_node_owner_path(int p_idx) const {
  1120. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1121. if (nodes[p_idx].owner < 0 || nodes[p_idx].owner == NO_PARENT_SAVED) {
  1122. return NodePath(); //root likely
  1123. }
  1124. if (nodes[p_idx].owner & FLAG_ID_IS_PATH) {
  1125. return node_paths[nodes[p_idx].owner & FLAG_MASK];
  1126. } else {
  1127. return get_node_path(nodes[p_idx].owner & FLAG_MASK);
  1128. }
  1129. }
  1130. int SceneState::get_connection_count() const {
  1131. return connections.size();
  1132. }
  1133. NodePath SceneState::get_connection_source(int p_idx) const {
  1134. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1135. if (connections[p_idx].from & FLAG_ID_IS_PATH) {
  1136. return node_paths[connections[p_idx].from & FLAG_MASK];
  1137. } else {
  1138. return get_node_path(connections[p_idx].from & FLAG_MASK);
  1139. }
  1140. }
  1141. StringName SceneState::get_connection_signal(int p_idx) const {
  1142. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1143. return names[connections[p_idx].signal];
  1144. }
  1145. NodePath SceneState::get_connection_target(int p_idx) const {
  1146. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1147. if (connections[p_idx].to & FLAG_ID_IS_PATH) {
  1148. return node_paths[connections[p_idx].to & FLAG_MASK];
  1149. } else {
  1150. return get_node_path(connections[p_idx].to & FLAG_MASK);
  1151. }
  1152. }
  1153. StringName SceneState::get_connection_method(int p_idx) const {
  1154. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1155. return names[connections[p_idx].method];
  1156. }
  1157. int SceneState::get_connection_flags(int p_idx) const {
  1158. ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
  1159. return connections[p_idx].flags;
  1160. }
  1161. Array SceneState::get_connection_binds(int p_idx) const {
  1162. ERR_FAIL_INDEX_V(p_idx, connections.size(), Array());
  1163. Array binds;
  1164. for (int i = 0; i < connections[p_idx].binds.size(); i++) {
  1165. binds.push_back(variants[connections[p_idx].binds[i]]);
  1166. }
  1167. return binds;
  1168. }
  1169. bool SceneState::has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method) {
  1170. // this method cannot be const because of this
  1171. Ref<SceneState> ss = this;
  1172. do {
  1173. for (int i = 0; i < ss->connections.size(); i++) {
  1174. const ConnectionData &c = ss->connections[i];
  1175. NodePath np_from;
  1176. if (c.from & FLAG_ID_IS_PATH) {
  1177. np_from = ss->node_paths[c.from & FLAG_MASK];
  1178. } else {
  1179. np_from = ss->get_node_path(c.from);
  1180. }
  1181. NodePath np_to;
  1182. if (c.to & FLAG_ID_IS_PATH) {
  1183. np_to = ss->node_paths[c.to & FLAG_MASK];
  1184. } else {
  1185. np_to = ss->get_node_path(c.to);
  1186. }
  1187. StringName sn_signal = ss->names[c.signal];
  1188. StringName sn_method = ss->names[c.method];
  1189. if (np_from == p_node_from && sn_signal == p_signal && np_to == p_node_to && sn_method == p_method) {
  1190. return true;
  1191. }
  1192. }
  1193. ss = ss->get_base_scene_state();
  1194. } while (ss.is_valid());
  1195. return false;
  1196. }
  1197. Vector<NodePath> SceneState::get_editable_instances() const {
  1198. return editable_instances;
  1199. }
  1200. //add
  1201. int SceneState::add_name(const StringName &p_name) {
  1202. names.push_back(p_name);
  1203. return names.size() - 1;
  1204. }
  1205. int SceneState::find_name(const StringName &p_name) const {
  1206. for (int i = 0; i < names.size(); i++) {
  1207. if (names[i] == p_name) {
  1208. return i;
  1209. }
  1210. }
  1211. return -1;
  1212. }
  1213. int SceneState::add_value(const Variant &p_value) {
  1214. variants.push_back(p_value);
  1215. return variants.size() - 1;
  1216. }
  1217. int SceneState::add_node_path(const NodePath &p_path) {
  1218. node_paths.push_back(p_path);
  1219. return (node_paths.size() - 1) | FLAG_ID_IS_PATH;
  1220. }
  1221. int SceneState::add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index) {
  1222. NodeData nd;
  1223. nd.parent = p_parent;
  1224. nd.owner = p_owner;
  1225. nd.type = p_type;
  1226. nd.name = p_name;
  1227. nd.instance = p_instance;
  1228. nd.index = p_index;
  1229. nodes.push_back(nd);
  1230. return nodes.size() - 1;
  1231. }
  1232. void SceneState::add_node_property(int p_node, int p_name, int p_value) {
  1233. ERR_FAIL_INDEX(p_node, nodes.size());
  1234. ERR_FAIL_INDEX(p_name, names.size());
  1235. ERR_FAIL_INDEX(p_value, variants.size());
  1236. NodeData::Property prop;
  1237. prop.name = p_name;
  1238. prop.value = p_value;
  1239. nodes.write[p_node].properties.push_back(prop);
  1240. }
  1241. void SceneState::add_node_group(int p_node, int p_group) {
  1242. ERR_FAIL_INDEX(p_node, nodes.size());
  1243. ERR_FAIL_INDEX(p_group, names.size());
  1244. nodes.write[p_node].groups.push_back(p_group);
  1245. }
  1246. void SceneState::set_base_scene(int p_idx) {
  1247. ERR_FAIL_INDEX(p_idx, variants.size());
  1248. base_scene_idx = p_idx;
  1249. }
  1250. void SceneState::add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, const Vector<int> &p_binds) {
  1251. ERR_FAIL_INDEX(p_signal, names.size());
  1252. ERR_FAIL_INDEX(p_method, names.size());
  1253. for (int i = 0; i < p_binds.size(); i++) {
  1254. ERR_FAIL_INDEX(p_binds[i], variants.size());
  1255. }
  1256. ConnectionData c;
  1257. c.from = p_from;
  1258. c.to = p_to;
  1259. c.signal = p_signal;
  1260. c.method = p_method;
  1261. c.flags = p_flags;
  1262. c.binds = p_binds;
  1263. connections.push_back(c);
  1264. }
  1265. void SceneState::add_editable_instance(const NodePath &p_path) {
  1266. editable_instances.push_back(p_path);
  1267. }
  1268. PoolVector<String> SceneState::_get_node_groups(int p_idx) const {
  1269. Vector<StringName> groups = get_node_groups(p_idx);
  1270. PoolVector<String> ret;
  1271. for (int i = 0; i < groups.size(); i++) {
  1272. ret.push_back(groups[i]);
  1273. }
  1274. return ret;
  1275. }
  1276. void SceneState::_bind_methods() {
  1277. //unbuild API
  1278. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneState::get_node_count);
  1279. ClassDB::bind_method(D_METHOD("get_node_type", "idx"), &SceneState::get_node_type);
  1280. ClassDB::bind_method(D_METHOD("get_node_name", "idx"), &SceneState::get_node_name);
  1281. ClassDB::bind_method(D_METHOD("get_node_path", "idx", "for_parent"), &SceneState::get_node_path, DEFVAL(false));
  1282. ClassDB::bind_method(D_METHOD("get_node_owner_path", "idx"), &SceneState::get_node_owner_path);
  1283. ClassDB::bind_method(D_METHOD("is_node_instance_placeholder", "idx"), &SceneState::is_node_instance_placeholder);
  1284. ClassDB::bind_method(D_METHOD("get_node_instance_placeholder", "idx"), &SceneState::get_node_instance_placeholder);
  1285. ClassDB::bind_method(D_METHOD("get_node_instance", "idx"), &SceneState::get_node_instance);
  1286. ClassDB::bind_method(D_METHOD("get_node_groups", "idx"), &SceneState::_get_node_groups);
  1287. ClassDB::bind_method(D_METHOD("get_node_index", "idx"), &SceneState::get_node_index);
  1288. ClassDB::bind_method(D_METHOD("get_node_property_count", "idx"), &SceneState::get_node_property_count);
  1289. ClassDB::bind_method(D_METHOD("get_node_property_name", "idx", "prop_idx"), &SceneState::get_node_property_name);
  1290. ClassDB::bind_method(D_METHOD("get_node_property_value", "idx", "prop_idx"), &SceneState::get_node_property_value);
  1291. ClassDB::bind_method(D_METHOD("get_connection_count"), &SceneState::get_connection_count);
  1292. ClassDB::bind_method(D_METHOD("get_connection_source", "idx"), &SceneState::get_connection_source);
  1293. ClassDB::bind_method(D_METHOD("get_connection_signal", "idx"), &SceneState::get_connection_signal);
  1294. ClassDB::bind_method(D_METHOD("get_connection_target", "idx"), &SceneState::get_connection_target);
  1295. ClassDB::bind_method(D_METHOD("get_connection_method", "idx"), &SceneState::get_connection_method);
  1296. ClassDB::bind_method(D_METHOD("get_connection_flags", "idx"), &SceneState::get_connection_flags);
  1297. ClassDB::bind_method(D_METHOD("get_connection_binds", "idx"), &SceneState::get_connection_binds);
  1298. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1299. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1300. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1301. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1302. }
  1303. SceneState::SceneState() {
  1304. base_scene_idx = -1;
  1305. last_modified_time = 0;
  1306. }
  1307. ////////////////
  1308. void PackedScene::_set_bundled_scene(const Dictionary &p_scene) {
  1309. state->set_bundled_scene(p_scene);
  1310. }
  1311. Dictionary PackedScene::_get_bundled_scene() const {
  1312. return state->get_bundled_scene();
  1313. }
  1314. Error PackedScene::pack(Node *p_scene) {
  1315. return state->pack(p_scene);
  1316. }
  1317. void PackedScene::clear() {
  1318. state->clear();
  1319. }
  1320. bool PackedScene::can_instance() const {
  1321. return state->can_instance();
  1322. }
  1323. Node *PackedScene::instance(GenEditState p_edit_state) const {
  1324. #ifndef TOOLS_ENABLED
  1325. ERR_FAIL_COND_V_MSG(p_edit_state != GEN_EDIT_STATE_DISABLED, NULL, "Edit state is only for editors, does not work without tools compiled.");
  1326. #endif
  1327. Node *s = state->instance((SceneState::GenEditState)p_edit_state);
  1328. if (!s) {
  1329. return nullptr;
  1330. }
  1331. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  1332. s->set_scene_instance_state(state);
  1333. }
  1334. if (get_path() != "" && get_path().find("::") == -1) {
  1335. s->set_filename(get_path());
  1336. }
  1337. s->notification(Node::NOTIFICATION_INSTANCED);
  1338. return s;
  1339. }
  1340. void PackedScene::replace_state(Ref<SceneState> p_by) {
  1341. state = p_by;
  1342. state->set_path(get_path());
  1343. #ifdef TOOLS_ENABLED
  1344. state->set_last_modified_time(get_last_modified_time());
  1345. #endif
  1346. }
  1347. void PackedScene::recreate_state() {
  1348. state = Ref<SceneState>(memnew(SceneState));
  1349. state->set_path(get_path());
  1350. #ifdef TOOLS_ENABLED
  1351. state->set_last_modified_time(get_last_modified_time());
  1352. #endif
  1353. }
  1354. Ref<SceneState> PackedScene::get_state() {
  1355. return state;
  1356. }
  1357. void PackedScene::set_path(const String &p_path, bool p_take_over) {
  1358. state->set_path(p_path);
  1359. Resource::set_path(p_path, p_take_over);
  1360. }
  1361. void PackedScene::_bind_methods() {
  1362. ClassDB::bind_method(D_METHOD("pack", "path"), &PackedScene::pack);
  1363. ClassDB::bind_method(D_METHOD("instance", "edit_state"), &PackedScene::instance, DEFVAL(GEN_EDIT_STATE_DISABLED));
  1364. ClassDB::bind_method(D_METHOD("can_instance"), &PackedScene::can_instance);
  1365. ClassDB::bind_method(D_METHOD("_set_bundled_scene"), &PackedScene::_set_bundled_scene);
  1366. ClassDB::bind_method(D_METHOD("_get_bundled_scene"), &PackedScene::_get_bundled_scene);
  1367. ClassDB::bind_method(D_METHOD("get_state"), &PackedScene::get_state);
  1368. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled"), "_set_bundled_scene", "_get_bundled_scene");
  1369. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1370. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1371. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1372. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1373. }
  1374. PackedScene::PackedScene() {
  1375. state = Ref<SceneState>(memnew(SceneState));
  1376. }