2
0

packed_scene.cpp 50 KB

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