packed_scene.cpp 50 KB

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