2
0

packed_scene.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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_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_EXPLAIN(vformat("Invalid scene: node %s does not specify its parent node.", snames[n.name]))
  75. ERR_FAIL_COND_V(n.parent == -1, NULL)
  76. NODE_FROM_ID(nparent, n.parent);
  77. #ifdef DEBUG_ENABLED
  78. if (!nparent && (n.parent & FLAG_ID_IS_PATH)) {
  79. 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());
  80. }
  81. #endif
  82. parent = nparent;
  83. }
  84. Node *node = NULL;
  85. if (i == 0 && base_scene_idx >= 0) {
  86. //scene inheritance on root node
  87. Ref<PackedScene> sdata = props[base_scene_idx];
  88. ERR_FAIL_COND_V(!sdata.is_valid(), NULL);
  89. 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
  90. ERR_FAIL_COND_V(!node, NULL);
  91. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  92. node->set_scene_inherited_state(sdata->get_state());
  93. }
  94. } else if (n.instance >= 0) {
  95. //instance a scene into this node
  96. if (n.instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  97. String path = props[n.instance & FLAG_MASK];
  98. if (disable_placeholders) {
  99. Ref<PackedScene> sdata = ResourceLoader::load(path, "PackedScene");
  100. ERR_FAIL_COND_V(!sdata.is_valid(), NULL);
  101. node = sdata->instance(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  102. ERR_FAIL_COND_V(!node, NULL);
  103. } else {
  104. InstancePlaceholder *ip = memnew(InstancePlaceholder);
  105. ip->set_instance_path(path);
  106. node = ip;
  107. }
  108. node->set_scene_instance_load_placeholder(true);
  109. } else {
  110. Ref<PackedScene> sdata = props[n.instance & FLAG_MASK];
  111. ERR_FAIL_COND_V(!sdata.is_valid(), NULL);
  112. node = sdata->instance(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  113. ERR_FAIL_COND_V(!node, NULL);
  114. }
  115. } else if (n.type == TYPE_INSTANCED) {
  116. //get the node from somewhere, it likely already exists from another instance
  117. if (parent) {
  118. node = parent->_get_child_by_name(snames[n.name]);
  119. #ifdef DEBUG_ENABLED
  120. if (!node) {
  121. 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());
  122. }
  123. #endif
  124. }
  125. } else if (ClassDB::is_class_enabled(snames[n.type])) {
  126. //node belongs to this scene and must be created
  127. Object *obj = ClassDB::instance(snames[n.type]);
  128. if (!Object::cast_to<Node>(obj)) {
  129. if (obj) {
  130. memdelete(obj);
  131. obj = NULL;
  132. }
  133. WARN_PRINT(String("Warning node of type " + snames[n.type].operator String() + " does not exist.").ascii().get_data());
  134. if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
  135. if (Object::cast_to<Spatial>(ret_nodes[n.parent])) {
  136. obj = memnew(Spatial);
  137. } else if (Object::cast_to<Control>(ret_nodes[n.parent])) {
  138. obj = memnew(Control);
  139. } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
  140. obj = memnew(Node2D);
  141. }
  142. }
  143. if (!obj) {
  144. obj = memnew(Node);
  145. }
  146. }
  147. node = Object::cast_to<Node>(obj);
  148. } else {
  149. //print_line("Class is disabled for: " + itos(n.type));
  150. //print_line("name: " + String(snames[n.type]));
  151. }
  152. if (node) {
  153. // may not have found the node (part of instanced scene and removed)
  154. // if found all is good, otherwise ignore
  155. //properties
  156. int nprop_count = n.properties.size();
  157. if (nprop_count) {
  158. const NodeData::Property *nprops = &n.properties[0];
  159. for (int j = 0; j < nprop_count; j++) {
  160. bool valid;
  161. ERR_FAIL_INDEX_V(nprops[j].name, sname_count, NULL);
  162. ERR_FAIL_INDEX_V(nprops[j].value, prop_count, NULL);
  163. if (snames[nprops[j].name] == CoreStringNames::get_singleton()->_script) {
  164. //work around to avoid old script variables from disappearing, should be the proper fix to:
  165. //https://github.com/godotengine/godot/issues/2958
  166. //store old state
  167. List<Pair<StringName, Variant> > old_state;
  168. if (node->get_script_instance()) {
  169. node->get_script_instance()->get_property_state(old_state);
  170. }
  171. node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
  172. //restore old state for new script, if exists
  173. for (List<Pair<StringName, Variant> >::Element *E = old_state.front(); E; E = E->next()) {
  174. node->set(E->get().first, E->get().second);
  175. }
  176. } else {
  177. Variant value = props[nprops[j].value];
  178. if (value.get_type() == Variant::OBJECT) {
  179. //handle resources that are local to scene by duplicating them if needed
  180. Ref<Resource> res = value;
  181. if (res.is_valid()) {
  182. if (res->is_local_to_scene()) {
  183. Map<Ref<Resource>, Ref<Resource> >::Element *E = resources_local_to_scene.find(res);
  184. if (E) {
  185. value = E->get();
  186. } else {
  187. Node *base = i == 0 ? node : ret_nodes[0];
  188. if (p_edit_state == GEN_EDIT_STATE_MAIN) {
  189. //for the main scene, use the resource as is
  190. res->configure_for_local_scene(base, resources_local_to_scene);
  191. resources_local_to_scene[res] = res;
  192. } else {
  193. //for instances, a copy must be made
  194. Node *base2 = i == 0 ? node : ret_nodes[0];
  195. Ref<Resource> local_dupe = res->duplicate_for_local_scene(base2, resources_local_to_scene);
  196. resources_local_to_scene[res] = local_dupe;
  197. res = local_dupe;
  198. value = local_dupe;
  199. }
  200. }
  201. //must make a copy, because this res is local to scene
  202. }
  203. }
  204. } else if (p_edit_state == GEN_EDIT_STATE_INSTANCE) {
  205. value = value.duplicate(true); // Duplicate arrays and dictionaries for the editor
  206. }
  207. node->set(snames[nprops[j].name], value, &valid);
  208. }
  209. }
  210. }
  211. //name
  212. //groups
  213. for (int j = 0; j < n.groups.size(); j++) {
  214. ERR_FAIL_INDEX_V(n.groups[j], sname_count, NULL);
  215. node->add_to_group(snames[n.groups[j]], true);
  216. }
  217. if (n.instance >= 0 || n.type != TYPE_INSTANCED || i == 0) {
  218. //if node was not part of instance, must set its name, parenthood and ownership
  219. if (i > 0) {
  220. if (parent) {
  221. parent->_add_child_nocheck(node, snames[n.name]);
  222. if (n.index >= 0 && n.index < parent->get_child_count() - 1)
  223. parent->move_child(node, n.index);
  224. } else {
  225. //it may be possible that an instanced scene has changed
  226. //and the node has nowhere to go anymore
  227. stray_instances.push_back(node); //can't be added, go to stray list
  228. }
  229. } else {
  230. if (Engine::get_singleton()->is_editor_hint()) {
  231. //validate name if using editor, to avoid broken
  232. node->set_name(snames[n.name]);
  233. } else {
  234. node->_set_name_nocheck(snames[n.name]);
  235. }
  236. }
  237. }
  238. if (n.owner >= 0) {
  239. NODE_FROM_ID(owner, n.owner);
  240. if (owner)
  241. node->_set_owner_nocheck(owner);
  242. }
  243. }
  244. ret_nodes[i] = node;
  245. if (node && gen_node_path_cache && ret_nodes[0]) {
  246. NodePath n2 = ret_nodes[0]->get_path_to(node);
  247. node_path_cache[n2] = i;
  248. }
  249. }
  250. for (Map<Ref<Resource>, Ref<Resource> >::Element *E = resources_local_to_scene.front(); E; E = E->next()) {
  251. E->get()->setup_local_to_scene();
  252. }
  253. //do connections
  254. int cc = connections.size();
  255. const ConnectionData *cdata = connections.ptr();
  256. for (int i = 0; i < cc; i++) {
  257. const ConnectionData &c = cdata[i];
  258. //ERR_FAIL_INDEX_V( c.from, nc, NULL );
  259. //ERR_FAIL_INDEX_V( c.to, nc, NULL );
  260. NODE_FROM_ID(cfrom, c.from);
  261. NODE_FROM_ID(cto, c.to);
  262. if (!cfrom || !cto)
  263. continue;
  264. Vector<Variant> binds;
  265. if (c.binds.size()) {
  266. binds.resize(c.binds.size());
  267. for (int j = 0; j < c.binds.size(); j++)
  268. binds.write[j] = props[c.binds[j]];
  269. }
  270. cfrom->connect(snames[c.signal], cto, snames[c.method], binds, CONNECT_PERSIST | c.flags);
  271. }
  272. //Node *s = ret_nodes[0];
  273. //remove nodes that could not be added, likely as a result that
  274. while (stray_instances.size()) {
  275. memdelete(stray_instances.front()->get());
  276. stray_instances.pop_front();
  277. }
  278. for (int i = 0; i < editable_instances.size(); i++) {
  279. Node *ei = ret_nodes[0]->get_node_or_null(editable_instances[i]);
  280. if (ei) {
  281. ret_nodes[0]->set_editable_instance(ei, true);
  282. }
  283. }
  284. return ret_nodes[0];
  285. }
  286. static int _nm_get_string(const String &p_string, Map<StringName, int> &name_map) {
  287. if (name_map.has(p_string))
  288. return name_map[p_string];
  289. int idx = name_map.size();
  290. name_map[p_string] = idx;
  291. return idx;
  292. }
  293. static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) {
  294. if (variant_map.has(p_variant))
  295. return variant_map[p_variant];
  296. int idx = variant_map.size();
  297. variant_map[p_variant] = idx;
  298. return idx;
  299. }
  300. 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) {
  301. // this function handles all the work related to properly packing scenes, be it
  302. // instanced or inherited.
  303. // given the complexity of this process, an attempt will be made to properly
  304. // document it. if you fail to understand something, please ask!
  305. //discard nodes that do not belong to be processed
  306. if (p_node != p_owner && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner()))
  307. return OK;
  308. // save the child instanced scenes that are chosen as editable, so they can be restored
  309. // upon load back
  310. if (p_node != p_owner && p_node->get_filename() != String() && p_owner->is_editable_instance(p_node))
  311. editable_instances.push_back(p_owner->get_path_to(p_node));
  312. NodeData nd;
  313. nd.name = _nm_get_string(p_node->get_name(), name_map);
  314. nd.instance = -1; //not instanced by default
  315. //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
  316. 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)))) {
  317. //do not save index, because it belongs to saved scene and scene is not inherited
  318. nd.index = -1;
  319. } else {
  320. //part of an inherited scene, or parent is from an instanced scene
  321. nd.index = p_node->get_index();
  322. }
  323. // if this node is part of an instanced scene or sub-instanced scene
  324. // we need to get the corresponding instance states.
  325. // with the instance states, we can query for identical properties/groups
  326. // and only save what has changed
  327. List<PackState> pack_state_stack;
  328. bool instanced_by_owner = true;
  329. {
  330. Node *n = p_node;
  331. while (n) {
  332. if (n == p_owner) {
  333. Ref<SceneState> state = n->get_scene_inherited_state();
  334. if (state.is_valid()) {
  335. int node = state->find_node_by_path(n->get_path_to(p_node));
  336. if (node >= 0) {
  337. //this one has state for this node, save
  338. PackState ps;
  339. ps.node = node;
  340. ps.state = state;
  341. pack_state_stack.push_back(ps);
  342. instanced_by_owner = false;
  343. }
  344. }
  345. if (p_node->get_filename() != String() && p_node->get_owner() == p_owner && instanced_by_owner) {
  346. if (p_node->get_scene_instance_load_placeholder()) {
  347. //it's a placeholder, use the placeholder path
  348. nd.instance = _vm_get_variant(p_node->get_filename(), variant_map);
  349. nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
  350. } else {
  351. //must instance ourselves
  352. Ref<PackedScene> instance = ResourceLoader::load(p_node->get_filename());
  353. if (!instance.is_valid()) {
  354. return ERR_CANT_OPEN;
  355. }
  356. nd.instance = _vm_get_variant(instance, variant_map);
  357. }
  358. }
  359. n = NULL;
  360. } else {
  361. if (n->get_filename() != String()) {
  362. //is an instance
  363. Ref<SceneState> state = n->get_scene_instance_state();
  364. if (state.is_valid()) {
  365. int node = state->find_node_by_path(n->get_path_to(p_node));
  366. if (node >= 0) {
  367. //this one has state for this node, save
  368. PackState ps;
  369. ps.node = node;
  370. ps.state = state;
  371. pack_state_stack.push_back(ps);
  372. }
  373. }
  374. }
  375. n = n->get_owner();
  376. }
  377. }
  378. }
  379. // all setup, we then proceed to check all properties for the node
  380. // and save the ones that are worth saving
  381. List<PropertyInfo> plist;
  382. p_node->get_property_list(&plist);
  383. StringName type = p_node->get_class();
  384. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  385. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  386. continue;
  387. }
  388. String name = E->get().name;
  389. Variant value = p_node->get(E->get().name);
  390. bool isdefault = false;
  391. Variant default_value = ClassDB::class_get_default_property_value(type, name);
  392. if (default_value.get_type() != Variant::NIL) {
  393. isdefault = bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value));
  394. }
  395. Ref<Script> script = p_node->get_script();
  396. if (!isdefault && script.is_valid() && script->get_property_default_value(name, default_value)) {
  397. isdefault = bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value));
  398. }
  399. // the version above makes more sense, because it does not rely on placeholder or usage flag
  400. // in the script, just the default value function.
  401. // if (E->get().usage & PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE) {
  402. // isdefault = true; //is script default value
  403. // }
  404. if (pack_state_stack.size()) {
  405. // we are on part of an instanced subscene
  406. // or part of instanced scene.
  407. // only save what has been changed
  408. // only save changed properties in instance
  409. if ((E->get().usage & PROPERTY_USAGE_NO_INSTANCE_STATE) || E->get().name == "__meta__") {
  410. //property has requested that no instance state is saved, sorry
  411. //also, meta won't be overridden or saved
  412. continue;
  413. }
  414. bool exists = false;
  415. Variant original;
  416. for (List<PackState>::Element *F = pack_state_stack.back(); F; F = F->prev()) {
  417. //check all levels of pack to see if the property exists somewhere
  418. const PackState &ps = F->get();
  419. original = ps.state->get_property_value(ps.node, E->get().name, exists);
  420. if (exists) {
  421. break;
  422. }
  423. }
  424. if (exists) {
  425. //check if already exists and did not change
  426. if (value.get_type() == Variant::REAL && original.get_type() == Variant::REAL) {
  427. //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  428. float a = value;
  429. float b = original;
  430. if (Math::abs(a - b) < CMP_EPSILON)
  431. continue;
  432. } else if (bool(Variant::evaluate(Variant::OP_EQUAL, value, original))) {
  433. continue;
  434. }
  435. }
  436. if (!exists && isdefault) {
  437. //does not exist in original node, but it's the default value
  438. //so safe to skip too.
  439. continue;
  440. }
  441. } else {
  442. if (isdefault) {
  443. //it's the default value, no point in saving it
  444. continue;
  445. }
  446. }
  447. NodeData::Property prop;
  448. prop.name = _nm_get_string(name, name_map);
  449. prop.value = _vm_get_variant(value, variant_map);
  450. nd.properties.push_back(prop);
  451. }
  452. // save the groups this node is into
  453. // discard groups that come from the original scene
  454. List<Node::GroupInfo> groups;
  455. p_node->get_groups(&groups);
  456. for (List<Node::GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  457. Node::GroupInfo &gi = E->get();
  458. if (!gi.persistent)
  459. continue;
  460. /*
  461. if (instance_state_node>=0 && instance_state->is_node_in_group(instance_state_node,gi.name))
  462. continue; //group was instanced, don't add here
  463. */
  464. bool skip = false;
  465. for (List<PackState>::Element *F = pack_state_stack.front(); F; F = F->next()) {
  466. //check all levels of pack to see if the group was added somewhere
  467. const PackState &ps = F->get();
  468. if (ps.state->is_node_in_group(ps.node, gi.name)) {
  469. skip = true;
  470. break;
  471. }
  472. }
  473. if (skip)
  474. continue;
  475. nd.groups.push_back(_nm_get_string(gi.name, name_map));
  476. }
  477. // save the right owner
  478. // for the saved scene root this is -1
  479. // for nodes of the saved scene this is 0
  480. // for nodes of instanced scenes this is >0
  481. if (p_node == p_owner) {
  482. //saved scene root
  483. nd.owner = -1;
  484. } else if (p_node->get_owner() == p_owner) {
  485. //part of saved scene
  486. nd.owner = 0;
  487. } else {
  488. nd.owner = -1;
  489. }
  490. // Save the right type. If this node was created by an instance
  491. // then flag that the node should not be created but reused
  492. if (pack_state_stack.empty()) {
  493. //this node is not part of an instancing process, so save the type
  494. nd.type = _nm_get_string(p_node->get_class(), name_map);
  495. } else {
  496. // this node is part of an instanced process, so do not save the type.
  497. // instead, save that it was instanced
  498. nd.type = TYPE_INSTANCED;
  499. }
  500. // determine whether to save this node or not
  501. // if this node is part of an instanced sub-scene, we can skip storing it if basically
  502. // no properties changed and no groups were added to it.
  503. // below condition is true for all nodes of the scene being saved, and ones in subscenes
  504. // that hold changes
  505. bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
  506. save_node = save_node || p_node == p_owner; // owner is always saved
  507. save_node = save_node || (p_node->get_owner() == p_owner && instanced_by_owner); //part of scene and not instanced
  508. int idx = nodes.size();
  509. int parent_node = NO_PARENT_SAVED;
  510. if (save_node) {
  511. //don't save the node if nothing and subscene
  512. node_map[p_node] = idx;
  513. //ok validate parent node
  514. if (p_parent_idx == NO_PARENT_SAVED) {
  515. int sidx;
  516. if (nodepath_map.has(p_node->get_parent())) {
  517. sidx = nodepath_map[p_node->get_parent()];
  518. } else {
  519. sidx = nodepath_map.size();
  520. nodepath_map[p_node->get_parent()] = sidx;
  521. }
  522. nd.parent = FLAG_ID_IS_PATH | sidx;
  523. } else {
  524. nd.parent = p_parent_idx;
  525. }
  526. parent_node = idx;
  527. nodes.push_back(nd);
  528. }
  529. for (int i = 0; i < p_node->get_child_count(); i++) {
  530. Node *c = p_node->get_child(i);
  531. Error err = _parse_node(p_owner, c, parent_node, name_map, variant_map, node_map, nodepath_map);
  532. if (err)
  533. return err;
  534. }
  535. return OK;
  536. }
  537. 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) {
  538. if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner()))
  539. return OK;
  540. List<MethodInfo> _signals;
  541. p_node->get_signal_list(&_signals);
  542. _signals.sort();
  543. //ERR_FAIL_COND_V( !node_map.has(p_node), ERR_BUG);
  544. //NodeData &nd = nodes[node_map[p_node]];
  545. for (List<MethodInfo>::Element *E = _signals.front(); E; E = E->next()) {
  546. List<Node::Connection> conns;
  547. p_node->get_signal_connection_list(E->get().name, &conns);
  548. conns.sort();
  549. for (List<Node::Connection>::Element *F = conns.front(); F; F = F->next()) {
  550. const Node::Connection &c = F->get();
  551. if (!(c.flags & CONNECT_PERSIST)) //only persistent connections get saved
  552. continue;
  553. // only connections that originate or end into main saved scene are saved
  554. // everything else is discarded
  555. Node *target = Object::cast_to<Node>(c.target);
  556. if (!target) {
  557. continue;
  558. }
  559. //find if this connection already exists
  560. Node *common_parent = target->find_common_parent_with(p_node);
  561. ERR_CONTINUE(!common_parent);
  562. if (common_parent != p_owner && common_parent->get_filename() == String()) {
  563. common_parent = common_parent->get_owner();
  564. }
  565. bool exists = false;
  566. //go through ownership chain to see if this exists
  567. while (common_parent) {
  568. Ref<SceneState> ps;
  569. if (common_parent == p_owner)
  570. ps = common_parent->get_scene_inherited_state();
  571. else
  572. ps = common_parent->get_scene_instance_state();
  573. if (ps.is_valid()) {
  574. NodePath signal_from = common_parent->get_path_to(p_node);
  575. NodePath signal_to = common_parent->get_path_to(target);
  576. if (ps->has_connection(signal_from, c.signal, signal_to, c.method)) {
  577. exists = true;
  578. break;
  579. }
  580. }
  581. if (common_parent == p_owner)
  582. break;
  583. else
  584. common_parent = common_parent->get_owner();
  585. }
  586. if (exists) { //already exists (comes from instance or inheritance), so don't save
  587. continue;
  588. }
  589. {
  590. Node *nl = p_node;
  591. bool exists2 = false;
  592. while (nl) {
  593. if (nl == p_owner) {
  594. Ref<SceneState> state = nl->get_scene_inherited_state();
  595. if (state.is_valid()) {
  596. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  597. int to_node = state->find_node_by_path(nl->get_path_to(target));
  598. if (from_node >= 0 && to_node >= 0) {
  599. //this one has state for this node, save
  600. if (state->is_connection(from_node, c.signal, to_node, c.method)) {
  601. exists2 = true;
  602. break;
  603. }
  604. }
  605. }
  606. nl = NULL;
  607. } else {
  608. if (nl->get_filename() != String()) {
  609. //is an instance
  610. Ref<SceneState> state = nl->get_scene_instance_state();
  611. if (state.is_valid()) {
  612. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  613. int to_node = state->find_node_by_path(nl->get_path_to(target));
  614. if (from_node >= 0 && to_node >= 0) {
  615. //this one has state for this node, save
  616. if (state->is_connection(from_node, c.signal, to_node, c.method)) {
  617. exists2 = true;
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. nl = nl->get_owner();
  624. }
  625. }
  626. if (exists2) {
  627. continue;
  628. }
  629. }
  630. int src_id;
  631. if (node_map.has(p_node)) {
  632. src_id = node_map[p_node];
  633. } else {
  634. if (nodepath_map.has(p_node)) {
  635. src_id = FLAG_ID_IS_PATH | nodepath_map[p_node];
  636. } else {
  637. int sidx = nodepath_map.size();
  638. nodepath_map[p_node] = sidx;
  639. src_id = FLAG_ID_IS_PATH | sidx;
  640. }
  641. }
  642. int target_id;
  643. if (node_map.has(target)) {
  644. target_id = node_map[target];
  645. } else {
  646. if (nodepath_map.has(target)) {
  647. target_id = FLAG_ID_IS_PATH | nodepath_map[target];
  648. } else {
  649. int sidx = nodepath_map.size();
  650. nodepath_map[target] = sidx;
  651. target_id = FLAG_ID_IS_PATH | sidx;
  652. }
  653. }
  654. ConnectionData cd;
  655. cd.from = src_id;
  656. cd.to = target_id;
  657. cd.method = _nm_get_string(c.method, name_map);
  658. cd.signal = _nm_get_string(c.signal, name_map);
  659. cd.flags = c.flags;
  660. for (int i = 0; i < c.binds.size(); i++) {
  661. cd.binds.push_back(_vm_get_variant(c.binds[i], variant_map));
  662. }
  663. connections.push_back(cd);
  664. }
  665. }
  666. for (int i = 0; i < p_node->get_child_count(); i++) {
  667. Node *c = p_node->get_child(i);
  668. Error err = _parse_connections(p_owner, c, name_map, variant_map, node_map, nodepath_map);
  669. if (err)
  670. return err;
  671. }
  672. return OK;
  673. }
  674. Error SceneState::pack(Node *p_scene) {
  675. ERR_FAIL_NULL_V(p_scene, ERR_INVALID_PARAMETER);
  676. clear();
  677. Node *scene = p_scene;
  678. Map<StringName, int> name_map;
  679. HashMap<Variant, int, VariantHasher, VariantComparator> variant_map;
  680. Map<Node *, int> node_map;
  681. Map<Node *, int> nodepath_map;
  682. //if using scene inheritance, pack the scene it inherits from
  683. if (scene->get_scene_inherited_state().is_valid()) {
  684. String path = scene->get_scene_inherited_state()->get_path();
  685. Ref<PackedScene> instance = ResourceLoader::load(path);
  686. if (instance.is_valid()) {
  687. base_scene_idx = _vm_get_variant(instance, variant_map);
  688. }
  689. }
  690. //instanced, only direct sub-scnes are supported of course
  691. Error err = _parse_node(scene, scene, -1, name_map, variant_map, node_map, nodepath_map);
  692. if (err) {
  693. clear();
  694. ERR_FAIL_V(err);
  695. }
  696. err = _parse_connections(scene, scene, name_map, variant_map, node_map, nodepath_map);
  697. if (err) {
  698. clear();
  699. ERR_FAIL_V(err);
  700. }
  701. names.resize(name_map.size());
  702. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  703. names.write[E->get()] = E->key();
  704. }
  705. variants.resize(variant_map.size());
  706. const Variant *K = NULL;
  707. while ((K = variant_map.next(K))) {
  708. int idx = variant_map[*K];
  709. variants.write[idx] = *K;
  710. }
  711. node_paths.resize(nodepath_map.size());
  712. for (Map<Node *, int>::Element *E = nodepath_map.front(); E; E = E->next()) {
  713. node_paths.write[E->get()] = scene->get_path_to(E->key());
  714. }
  715. return OK;
  716. }
  717. void SceneState::set_path(const String &p_path) {
  718. path = p_path;
  719. }
  720. String SceneState::get_path() const {
  721. return path;
  722. }
  723. void SceneState::clear() {
  724. names.clear();
  725. variants.clear();
  726. nodes.clear();
  727. connections.clear();
  728. node_path_cache.clear();
  729. node_paths.clear();
  730. editable_instances.clear();
  731. base_scene_idx = -1;
  732. }
  733. Ref<SceneState> SceneState::_get_base_scene_state() const {
  734. if (base_scene_idx >= 0) {
  735. Ref<PackedScene> ps = variants[base_scene_idx];
  736. if (ps.is_valid()) {
  737. return ps->get_state();
  738. }
  739. }
  740. return Ref<SceneState>();
  741. }
  742. int SceneState::find_node_by_path(const NodePath &p_node) const {
  743. if (!node_path_cache.has(p_node)) {
  744. if (_get_base_scene_state().is_valid()) {
  745. int idx = _get_base_scene_state()->find_node_by_path(p_node);
  746. if (idx >= 0) {
  747. int rkey = _find_base_scene_node_remap_key(idx);
  748. if (rkey == -1) {
  749. rkey = nodes.size() + base_scene_node_remap.size();
  750. base_scene_node_remap[rkey] = idx;
  751. }
  752. return rkey;
  753. }
  754. }
  755. return -1;
  756. }
  757. int nid = node_path_cache[p_node];
  758. if (_get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
  759. //for nodes that _do_ exist in current scene, still try to look for
  760. //the node in the instanced scene, as a property may be missing
  761. //from the local one
  762. int idx = _get_base_scene_state()->find_node_by_path(p_node);
  763. if (idx != -1) {
  764. base_scene_node_remap[nid] = idx;
  765. }
  766. }
  767. return nid;
  768. }
  769. int SceneState::_find_base_scene_node_remap_key(int p_idx) const {
  770. for (Map<int, int>::Element *E = base_scene_node_remap.front(); E; E = E->next()) {
  771. if (E->value() == p_idx) {
  772. return E->key();
  773. }
  774. }
  775. return -1;
  776. }
  777. Variant SceneState::get_property_value(int p_node, const StringName &p_property, bool &found) const {
  778. found = false;
  779. ERR_FAIL_COND_V(p_node < 0, Variant());
  780. if (p_node < nodes.size()) {
  781. //find in built-in nodes
  782. int pc = nodes[p_node].properties.size();
  783. const StringName *namep = names.ptr();
  784. const NodeData::Property *p = nodes[p_node].properties.ptr();
  785. for (int i = 0; i < pc; i++) {
  786. if (p_property == namep[p[i].name]) {
  787. found = true;
  788. return variants[p[i].value];
  789. }
  790. }
  791. }
  792. //property not found, try on instance
  793. if (base_scene_node_remap.has(p_node)) {
  794. return _get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
  795. }
  796. return Variant();
  797. }
  798. bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
  799. ERR_FAIL_COND_V(p_node < 0, false);
  800. if (p_node < nodes.size()) {
  801. const StringName *namep = names.ptr();
  802. for (int i = 0; i < nodes[p_node].groups.size(); i++) {
  803. if (namep[nodes[p_node].groups[i]] == p_group)
  804. return true;
  805. }
  806. }
  807. if (base_scene_node_remap.has(p_node)) {
  808. return _get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
  809. }
  810. return false;
  811. }
  812. bool SceneState::disable_placeholders = false;
  813. void SceneState::set_disable_placeholders(bool p_disable) {
  814. disable_placeholders = p_disable;
  815. }
  816. bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const {
  817. ERR_FAIL_COND_V(p_node < 0, false);
  818. ERR_FAIL_COND_V(p_to_node < 0, false);
  819. if (p_node < nodes.size() && p_to_node < nodes.size()) {
  820. int signal_idx = -1;
  821. int method_idx = -1;
  822. for (int i = 0; i < names.size(); i++) {
  823. if (names[i] == p_signal) {
  824. signal_idx = i;
  825. } else if (names[i] == p_to_method) {
  826. method_idx = i;
  827. }
  828. }
  829. if (signal_idx >= 0 && method_idx >= 0) {
  830. //signal and method strings are stored..
  831. for (int i = 0; i < connections.size(); i++) {
  832. if (connections[i].from == p_node && connections[i].to == p_to_node && connections[i].signal == signal_idx && connections[i].method == method_idx) {
  833. return true;
  834. }
  835. }
  836. }
  837. }
  838. if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
  839. 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);
  840. }
  841. return false;
  842. }
  843. void SceneState::set_bundled_scene(const Dictionary &p_dictionary) {
  844. ERR_FAIL_COND(!p_dictionary.has("names"));
  845. ERR_FAIL_COND(!p_dictionary.has("variants"));
  846. ERR_FAIL_COND(!p_dictionary.has("node_count"));
  847. ERR_FAIL_COND(!p_dictionary.has("nodes"));
  848. ERR_FAIL_COND(!p_dictionary.has("conn_count"));
  849. ERR_FAIL_COND(!p_dictionary.has("conns"));
  850. //ERR_FAIL_COND( !p_dictionary.has("path"));
  851. int version = 1;
  852. if (p_dictionary.has("version"))
  853. version = p_dictionary["version"];
  854. if (version > PACK_VERSION) {
  855. ERR_EXPLAIN("Save format version too new!");
  856. ERR_FAIL();
  857. }
  858. PoolVector<String> snames = p_dictionary["names"];
  859. if (snames.size()) {
  860. int namecount = snames.size();
  861. names.resize(namecount);
  862. PoolVector<String>::Read r = snames.read();
  863. for (int i = 0; i < names.size(); i++)
  864. names.write[i] = r[i];
  865. }
  866. Array svariants = p_dictionary["variants"];
  867. if (svariants.size()) {
  868. int varcount = svariants.size();
  869. variants.resize(varcount);
  870. for (int i = 0; i < varcount; i++) {
  871. variants.write[i] = svariants[i];
  872. }
  873. } else {
  874. variants.clear();
  875. }
  876. nodes.resize(p_dictionary["node_count"]);
  877. int nc = nodes.size();
  878. if (nc) {
  879. PoolVector<int> snodes = p_dictionary["nodes"];
  880. PoolVector<int>::Read r = snodes.read();
  881. int idx = 0;
  882. for (int i = 0; i < nc; i++) {
  883. NodeData &nd = nodes.write[i];
  884. nd.parent = r[idx++];
  885. nd.owner = r[idx++];
  886. nd.type = r[idx++];
  887. uint32_t name_index = r[idx++];
  888. nd.name = name_index & ((1 << NAME_INDEX_BITS) - 1);
  889. nd.index = (name_index >> NAME_INDEX_BITS);
  890. nd.index--; //0 is invalid, stored as 1
  891. nd.instance = r[idx++];
  892. nd.properties.resize(r[idx++]);
  893. for (int j = 0; j < nd.properties.size(); j++) {
  894. nd.properties.write[j].name = r[idx++];
  895. nd.properties.write[j].value = r[idx++];
  896. }
  897. nd.groups.resize(r[idx++]);
  898. for (int j = 0; j < nd.groups.size(); j++) {
  899. nd.groups.write[j] = r[idx++];
  900. }
  901. }
  902. }
  903. connections.resize(p_dictionary["conn_count"]);
  904. int cc = connections.size();
  905. if (cc) {
  906. PoolVector<int> sconns = p_dictionary["conns"];
  907. PoolVector<int>::Read r = sconns.read();
  908. int idx = 0;
  909. for (int i = 0; i < cc; i++) {
  910. ConnectionData &cd = connections.write[i];
  911. cd.from = r[idx++];
  912. cd.to = r[idx++];
  913. cd.signal = r[idx++];
  914. cd.method = r[idx++];
  915. cd.flags = r[idx++];
  916. cd.binds.resize(r[idx++]);
  917. for (int j = 0; j < cd.binds.size(); j++) {
  918. cd.binds.write[j] = r[idx++];
  919. }
  920. }
  921. }
  922. Array np;
  923. if (p_dictionary.has("node_paths")) {
  924. np = p_dictionary["node_paths"];
  925. }
  926. node_paths.resize(np.size());
  927. for (int i = 0; i < np.size(); i++) {
  928. node_paths.write[i] = np[i];
  929. }
  930. Array ei;
  931. if (p_dictionary.has("editable_instances")) {
  932. ei = p_dictionary["editable_instances"];
  933. }
  934. if (p_dictionary.has("base_scene")) {
  935. base_scene_idx = p_dictionary["base_scene"];
  936. }
  937. editable_instances.resize(ei.size());
  938. for (int i = 0; i < editable_instances.size(); i++) {
  939. editable_instances.write[i] = ei[i];
  940. }
  941. //path=p_dictionary["path"];
  942. }
  943. Dictionary SceneState::get_bundled_scene() const {
  944. PoolVector<String> rnames;
  945. rnames.resize(names.size());
  946. if (names.size()) {
  947. PoolVector<String>::Write r = rnames.write();
  948. for (int i = 0; i < names.size(); i++)
  949. r[i] = names[i];
  950. }
  951. Dictionary d;
  952. d["names"] = rnames;
  953. d["variants"] = variants;
  954. Vector<int> rnodes;
  955. d["node_count"] = nodes.size();
  956. for (int i = 0; i < nodes.size(); i++) {
  957. const NodeData &nd = nodes[i];
  958. rnodes.push_back(nd.parent);
  959. rnodes.push_back(nd.owner);
  960. rnodes.push_back(nd.type);
  961. uint32_t name_index = nd.name;
  962. if (nd.index < (1 << (32 - NAME_INDEX_BITS)) - 1) { //save if less than 16k children
  963. name_index |= uint32_t(nd.index + 1) << NAME_INDEX_BITS; //for backwards compatibility, index 0 is no index
  964. }
  965. rnodes.push_back(name_index);
  966. rnodes.push_back(nd.instance);
  967. rnodes.push_back(nd.properties.size());
  968. for (int j = 0; j < nd.properties.size(); j++) {
  969. rnodes.push_back(nd.properties[j].name);
  970. rnodes.push_back(nd.properties[j].value);
  971. }
  972. rnodes.push_back(nd.groups.size());
  973. for (int j = 0; j < nd.groups.size(); j++) {
  974. rnodes.push_back(nd.groups[j]);
  975. }
  976. }
  977. d["nodes"] = rnodes;
  978. Vector<int> rconns;
  979. d["conn_count"] = connections.size();
  980. for (int i = 0; i < connections.size(); i++) {
  981. const ConnectionData &cd = connections[i];
  982. rconns.push_back(cd.from);
  983. rconns.push_back(cd.to);
  984. rconns.push_back(cd.signal);
  985. rconns.push_back(cd.method);
  986. rconns.push_back(cd.flags);
  987. rconns.push_back(cd.binds.size());
  988. for (int j = 0; j < cd.binds.size(); j++)
  989. rconns.push_back(cd.binds[j]);
  990. }
  991. d["conns"] = rconns;
  992. Array rnode_paths;
  993. rnode_paths.resize(node_paths.size());
  994. for (int i = 0; i < node_paths.size(); i++) {
  995. rnode_paths[i] = node_paths[i];
  996. }
  997. d["node_paths"] = rnode_paths;
  998. Array reditable_instances;
  999. reditable_instances.resize(editable_instances.size());
  1000. for (int i = 0; i < editable_instances.size(); i++) {
  1001. reditable_instances[i] = editable_instances[i];
  1002. }
  1003. d["editable_instances"] = reditable_instances;
  1004. if (base_scene_idx >= 0) {
  1005. d["base_scene"] = base_scene_idx;
  1006. }
  1007. d["version"] = PACK_VERSION;
  1008. //d["path"]=path;
  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. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  1313. ERR_EXPLAIN("Edit state is only for editors, does not work without tools compiled");
  1314. ERR_FAIL_COND_V(p_edit_state != GEN_EDIT_STATE_DISABLED, NULL);
  1315. }
  1316. #endif
  1317. Node *s = state->instance((SceneState::GenEditState)p_edit_state);
  1318. if (!s)
  1319. return NULL;
  1320. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  1321. s->set_scene_instance_state(state);
  1322. }
  1323. if (get_path() != "" && get_path().find("::") == -1)
  1324. s->set_filename(get_path());
  1325. s->notification(Node::NOTIFICATION_INSTANCED);
  1326. return s;
  1327. }
  1328. void PackedScene::replace_state(Ref<SceneState> p_by) {
  1329. state = p_by;
  1330. state->set_path(get_path());
  1331. #ifdef TOOLS_ENABLED
  1332. state->set_last_modified_time(get_last_modified_time());
  1333. #endif
  1334. }
  1335. void PackedScene::recreate_state() {
  1336. state = Ref<SceneState>(memnew(SceneState));
  1337. state->set_path(get_path());
  1338. #ifdef TOOLS_ENABLED
  1339. state->set_last_modified_time(get_last_modified_time());
  1340. #endif
  1341. }
  1342. Ref<SceneState> PackedScene::get_state() {
  1343. return state;
  1344. }
  1345. void PackedScene::set_path(const String &p_path, bool p_take_over) {
  1346. state->set_path(p_path);
  1347. Resource::set_path(p_path, p_take_over);
  1348. }
  1349. void PackedScene::_bind_methods() {
  1350. ClassDB::bind_method(D_METHOD("pack", "path"), &PackedScene::pack);
  1351. ClassDB::bind_method(D_METHOD("instance", "edit_state"), &PackedScene::instance, DEFVAL(GEN_EDIT_STATE_DISABLED));
  1352. ClassDB::bind_method(D_METHOD("can_instance"), &PackedScene::can_instance);
  1353. ClassDB::bind_method(D_METHOD("_set_bundled_scene"), &PackedScene::_set_bundled_scene);
  1354. ClassDB::bind_method(D_METHOD("_get_bundled_scene"), &PackedScene::_get_bundled_scene);
  1355. ClassDB::bind_method(D_METHOD("get_state"), &PackedScene::get_state);
  1356. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled"), "_set_bundled_scene", "_get_bundled_scene");
  1357. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1358. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1359. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1360. }
  1361. PackedScene::PackedScene() {
  1362. state = Ref<SceneState>(memnew(SceneState));
  1363. }