packed_scene.cpp 55 KB

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