packed_scene.cpp 50 KB

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