packed_scene.cpp 48 KB

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