packed_scene.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /**************************************************************************/
  2. /* packed_scene.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/io/missing_resource.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/templates/local_vector.h"
  35. #include "scene/2d/node_2d.h"
  36. #ifndef _3D_DISABLED
  37. #include "scene/3d/node_3d.h"
  38. #endif // _3D_DISABLED
  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 3
  44. #ifdef TOOLS_ENABLED
  45. SceneState::InstantiationWarningNotify SceneState::instantiation_warn_notify = nullptr;
  46. #endif
  47. bool SceneState::can_instantiate() const {
  48. return nodes.size() > 0;
  49. }
  50. static Array _sanitize_node_pinned_properties(Node *p_node) {
  51. Array pinned = p_node->get_meta("_edit_pinned_properties_", Array());
  52. if (pinned.is_empty()) {
  53. return Array();
  54. }
  55. HashSet<StringName> storable_properties;
  56. p_node->get_storable_properties(storable_properties);
  57. int i = 0;
  58. do {
  59. if (storable_properties.has(pinned[i])) {
  60. i++;
  61. } else {
  62. pinned.remove_at(i);
  63. }
  64. } while (i < pinned.size());
  65. if (pinned.is_empty()) {
  66. p_node->remove_meta("_edit_pinned_properties_");
  67. }
  68. return pinned;
  69. }
  70. Ref<Resource> SceneState::get_remap_resource(const Ref<Resource> &p_resource, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache, const Ref<Resource> &p_fallback, Node *p_for_scene) {
  71. ERR_FAIL_COND_V(p_resource.is_null(), Ref<Resource>());
  72. Ref<Resource> remap_resource;
  73. // Find the shared copy of the source resource.
  74. HashMap<Ref<Resource>, Ref<Resource>>::Iterator R = remap_cache.find(p_resource);
  75. if (R) {
  76. remap_resource = R->value;
  77. } else if (p_fallback.is_valid() && p_fallback->is_local_to_scene() && p_fallback->get_class() == p_resource->get_class()) {
  78. // Simply copy the data from the source resource to update the fallback resource that was previously set.
  79. p_fallback->reset_state(); // May want to reset state.
  80. List<PropertyInfo> pi;
  81. p_resource->get_property_list(&pi);
  82. for (const PropertyInfo &E : pi) {
  83. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  84. continue;
  85. }
  86. if (E.name == "resource_path") {
  87. continue; // Do not change path.
  88. }
  89. Variant value = p_resource->get(E.name);
  90. // The local-to-scene subresource instance is preserved, thus maintaining the previous sharing relationship.
  91. // This is mainly used when the sub-scene root is reset in the main scene.
  92. Ref<Resource> sub_res_of_from = value;
  93. if (sub_res_of_from.is_valid() && sub_res_of_from->is_local_to_scene()) {
  94. value = get_remap_resource(sub_res_of_from, remap_cache, p_fallback->get(E.name), p_fallback->get_local_scene());
  95. }
  96. p_fallback->set(E.name, value);
  97. }
  98. p_fallback->set_scene_unique_id(p_resource->get_scene_unique_id()); // Get the id from the main scene, in case the id changes again when saving the scene.
  99. remap_cache[p_resource] = p_fallback;
  100. remap_resource = p_fallback;
  101. } else { // A copy of the source resource is required to overwrite the previous one.
  102. Ref<Resource> local_dupe = p_resource->duplicate_for_local_scene(p_for_scene, remap_cache);
  103. remap_cache[p_resource] = local_dupe;
  104. remap_resource = local_dupe;
  105. }
  106. return remap_resource;
  107. }
  108. Node *SceneState::instantiate(GenEditState p_edit_state) const {
  109. // Nodes where instantiation failed (because something is missing.)
  110. List<Node *> stray_instances;
  111. #define NODE_FROM_ID(p_name, p_id) \
  112. Node *p_name; \
  113. if (p_id & FLAG_ID_IS_PATH) { \
  114. NodePath np = node_paths[p_id & FLAG_MASK]; \
  115. p_name = ret_nodes[0]->get_node_or_null(np); \
  116. } else { \
  117. ERR_FAIL_INDEX_V(p_id &FLAG_MASK, nc, nullptr); \
  118. p_name = ret_nodes[p_id & FLAG_MASK]; \
  119. }
  120. int nc = nodes.size();
  121. ERR_FAIL_COND_V_MSG(nc == 0, nullptr, vformat("Failed to instantiate scene state of \"%s\", node count is 0. Make sure the PackedScene resource is valid.", path));
  122. const StringName *snames = nullptr;
  123. int sname_count = names.size();
  124. if (sname_count) {
  125. snames = &names[0];
  126. }
  127. const Variant *props = nullptr;
  128. int prop_count = variants.size();
  129. if (prop_count) {
  130. props = &variants[0];
  131. }
  132. //Vector<Variant> properties;
  133. const NodeData *nd = &nodes[0];
  134. Node **ret_nodes = (Node **)alloca(sizeof(Node *) * nc);
  135. bool gen_node_path_cache = p_edit_state != GEN_EDIT_STATE_DISABLED && node_path_cache.is_empty();
  136. HashMap<Ref<Resource>, Ref<Resource>> resources_local_to_scene;
  137. LocalVector<DeferredNodePathProperties> deferred_node_paths;
  138. for (int i = 0; i < nc; i++) {
  139. const NodeData &n = nd[i];
  140. Node *parent = nullptr;
  141. String old_parent_path;
  142. if (i > 0) {
  143. ERR_FAIL_COND_V_MSG(n.parent == -1, nullptr, vformat("Invalid scene: node %s does not specify its parent node.", snames[n.name]));
  144. NODE_FROM_ID(nparent, n.parent);
  145. #ifdef DEBUG_ENABLED
  146. if (!nparent && (n.parent & FLAG_ID_IS_PATH)) {
  147. WARN_PRINT(String("Parent path '" + String(node_paths[n.parent & FLAG_MASK]) + "' for node '" + String(snames[n.name]) + "' has vanished when instantiating: '" + get_path() + "'.").ascii().get_data());
  148. old_parent_path = String(node_paths[n.parent & FLAG_MASK]).trim_prefix("./").replace("/", "@");
  149. nparent = ret_nodes[0];
  150. }
  151. #endif
  152. parent = nparent;
  153. } else {
  154. // i == 0 is root node.
  155. ERR_FAIL_COND_V_MSG(n.parent != -1, nullptr, vformat("Invalid scene: root node %s cannot specify a parent node.", snames[n.name]));
  156. ERR_FAIL_COND_V_MSG(n.type == TYPE_INSTANTIATED && base_scene_idx < 0, nullptr, vformat("Invalid scene: root node %s in an instance, but there's no base scene.", snames[n.name]));
  157. }
  158. Node *node = nullptr;
  159. MissingNode *missing_node = nullptr;
  160. bool is_inherited_scene = false;
  161. if (i == 0 && base_scene_idx >= 0) {
  162. // Scene inheritance on root node.
  163. Ref<PackedScene> sdata = props[base_scene_idx];
  164. ERR_FAIL_COND_V(sdata.is_null(), nullptr);
  165. 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
  166. ERR_FAIL_NULL_V(node, nullptr);
  167. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  168. node->set_scene_inherited_state(sdata->get_state());
  169. }
  170. is_inherited_scene = true;
  171. } else if (n.instance >= 0) {
  172. // Instance a scene into this node.
  173. if (n.instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  174. const String scene_path = props[n.instance & FLAG_MASK];
  175. if (disable_placeholders) {
  176. Ref<PackedScene> sdata = ResourceLoader::load(scene_path, "PackedScene");
  177. if (sdata.is_valid()) {
  178. node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  179. ERR_FAIL_NULL_V(node, nullptr);
  180. } else if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  181. missing_node = memnew(MissingNode);
  182. missing_node->set_original_scene(scene_path);
  183. missing_node->set_recording_properties(true);
  184. node = missing_node;
  185. } else {
  186. ERR_FAIL_V_MSG(nullptr, "Placeholder scene is missing.");
  187. }
  188. } else {
  189. InstancePlaceholder *ip = memnew(InstancePlaceholder);
  190. ip->set_instance_path(scene_path);
  191. node = ip;
  192. }
  193. node->set_scene_instance_load_placeholder(true);
  194. } else {
  195. Ref<Resource> res = props[n.instance & FLAG_MASK];
  196. Ref<PackedScene> sdata = res;
  197. if (sdata.is_valid()) {
  198. node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  199. ERR_FAIL_NULL_V_MSG(node, nullptr, vformat("Failed to load scene dependency: \"%s\". Make sure the required scene is valid.", sdata->get_path()));
  200. } else if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  201. missing_node = memnew(MissingNode);
  202. #ifdef TOOLS_ENABLED
  203. if (res.is_valid()) {
  204. missing_node->set_original_scene(res->get_meta("__load_path__", ""));
  205. }
  206. #endif
  207. missing_node->set_recording_properties(true);
  208. node = missing_node;
  209. } else {
  210. ERR_FAIL_V_MSG(nullptr, "Scene instance is missing.");
  211. }
  212. }
  213. } else if (n.type == TYPE_INSTANTIATED) {
  214. // Get the node from somewhere, it likely already exists from another instance.
  215. if (parent) {
  216. node = parent->_get_child_by_name(snames[n.name]);
  217. #ifdef DEBUG_ENABLED
  218. if (!node) {
  219. 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());
  220. }
  221. #endif
  222. }
  223. } else {
  224. // Node belongs to this scene and must be created.
  225. Object *obj = ClassDB::instantiate(snames[n.type]);
  226. node = Object::cast_to<Node>(obj);
  227. if (!node) {
  228. if (obj) {
  229. memdelete(obj);
  230. obj = nullptr;
  231. }
  232. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  233. missing_node = memnew(MissingNode);
  234. missing_node->set_original_class(snames[n.type]);
  235. missing_node->set_recording_properties(true);
  236. node = missing_node;
  237. obj = missing_node;
  238. } else {
  239. 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());
  240. if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
  241. if (Object::cast_to<Control>(ret_nodes[n.parent])) {
  242. obj = memnew(Control);
  243. } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
  244. obj = memnew(Node2D);
  245. #ifndef _3D_DISABLED
  246. } else if (Object::cast_to<Node3D>(ret_nodes[n.parent])) {
  247. obj = memnew(Node3D);
  248. #endif // _3D_DISABLED
  249. }
  250. }
  251. if (!obj) {
  252. obj = memnew(Node);
  253. }
  254. node = Object::cast_to<Node>(obj);
  255. }
  256. }
  257. }
  258. if (node) {
  259. // may not have found the node (part of instantiated scene and removed)
  260. // if found all is good, otherwise ignore
  261. //properties
  262. int nprop_count = n.properties.size();
  263. if (nprop_count) {
  264. const NodeData::Property *nprops = &n.properties[0];
  265. Dictionary missing_resource_properties;
  266. HashMap<Ref<Resource>, Ref<Resource>> resources_local_to_sub_scene; // Record the mappings in the sub-scene.
  267. for (int j = 0; j < nprop_count; j++) {
  268. bool valid;
  269. ERR_FAIL_INDEX_V(nprops[j].value, prop_count, nullptr);
  270. if (nprops[j].name & FLAG_PATH_PROPERTY_IS_NODE) {
  271. if (!Engine::get_singleton()->is_editor_hint() && node->get_scene_instance_load_placeholder()) {
  272. // We cannot know if the referenced nodes exist yet, so instead of deferring, we write the NodePaths directly.
  273. uint32_t name_idx = nprops[j].name & (FLAG_PATH_PROPERTY_IS_NODE - 1);
  274. ERR_FAIL_UNSIGNED_INDEX_V(name_idx, (uint32_t)sname_count, nullptr);
  275. node->set(snames[name_idx], props[nprops[j].value], &valid);
  276. continue;
  277. }
  278. uint32_t name_idx = nprops[j].name & (FLAG_PATH_PROPERTY_IS_NODE - 1);
  279. ERR_FAIL_UNSIGNED_INDEX_V(name_idx, (uint32_t)sname_count, nullptr);
  280. DeferredNodePathProperties dnp;
  281. dnp.value = props[nprops[j].value];
  282. dnp.base = node;
  283. dnp.property = snames[name_idx];
  284. deferred_node_paths.push_back(dnp);
  285. continue;
  286. }
  287. ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);
  288. if (snames[nprops[j].name] == CoreStringName(script)) {
  289. //work around to avoid old script variables from disappearing, should be the proper fix to:
  290. //https://github.com/godotengine/godot/issues/2958
  291. //store old state
  292. List<Pair<StringName, Variant>> old_state;
  293. if (node->get_script_instance()) {
  294. node->get_script_instance()->get_property_state(old_state);
  295. }
  296. node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
  297. //restore old state for new script, if exists
  298. for (const Pair<StringName, Variant> &E : old_state) {
  299. node->set(E.first, E.second);
  300. }
  301. } else {
  302. Variant value = props[nprops[j].value];
  303. // Making sure that instances of inherited scenes don't share the same
  304. // reference between them.
  305. if (is_inherited_scene) {
  306. value = value.duplicate(true);
  307. }
  308. if (value.get_type() == Variant::OBJECT) {
  309. //handle resources that are local to scene by duplicating them if needed
  310. Ref<Resource> res = value;
  311. if (res.is_valid()) {
  312. value = make_local_resource(value, n, resources_local_to_sub_scene, node, snames[nprops[j].name], resources_local_to_scene, i, ret_nodes, p_edit_state);
  313. }
  314. }
  315. if (value.get_type() == Variant::ARRAY) {
  316. Array set_array = value;
  317. value = setup_resources_in_array(set_array, n, resources_local_to_sub_scene, node, snames[nprops[j].name], resources_local_to_scene, i, ret_nodes, p_edit_state);
  318. bool is_get_valid = false;
  319. Variant get_value = node->get(snames[nprops[j].name], &is_get_valid);
  320. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  321. Array get_array = get_value;
  322. if (!set_array.is_same_typed(get_array)) {
  323. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  324. }
  325. }
  326. }
  327. if (value.get_type() == Variant::DICTIONARY) {
  328. Dictionary set_dict = value;
  329. value = setup_resources_in_dictionary(set_dict, n, resources_local_to_sub_scene, node, snames[nprops[j].name], resources_local_to_scene, i, ret_nodes, p_edit_state);
  330. bool is_get_valid = false;
  331. Variant get_value = node->get(snames[nprops[j].name], &is_get_valid);
  332. if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) {
  333. Dictionary get_dict = get_value;
  334. if (!set_dict.is_same_typed(get_dict)) {
  335. value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(),
  336. get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());
  337. }
  338. }
  339. }
  340. bool set_valid = true;
  341. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled() && value.get_type() == Variant::OBJECT) {
  342. Ref<MissingResource> mr = value;
  343. if (mr.is_valid()) {
  344. missing_resource_properties[snames[nprops[j].name]] = mr;
  345. set_valid = false;
  346. }
  347. }
  348. if (set_valid) {
  349. node->set(snames[nprops[j].name], value, &valid);
  350. }
  351. if (p_edit_state == GEN_EDIT_STATE_INSTANCE && value.get_type() != Variant::OBJECT) {
  352. value = value.duplicate(true); // Duplicate arrays and dictionaries for the editor.
  353. }
  354. }
  355. }
  356. if (!missing_resource_properties.is_empty()) {
  357. node->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  358. }
  359. for (KeyValue<Ref<Resource>, Ref<Resource>> &E : resources_local_to_sub_scene) {
  360. if (E.value->get_local_scene() == node) {
  361. E.value->setup_local_to_scene(); // Setup may be required for the resource to work properly.
  362. }
  363. }
  364. }
  365. //name
  366. //groups
  367. for (int j = 0; j < n.groups.size(); j++) {
  368. ERR_FAIL_INDEX_V(n.groups[j], sname_count, nullptr);
  369. node->add_to_group(snames[n.groups[j]], true);
  370. }
  371. if (n.instance >= 0 || n.type != TYPE_INSTANTIATED || i == 0) {
  372. //if node was not part of instance, must set its name, parenthood and ownership
  373. if (i > 0) {
  374. if (parent) {
  375. bool pending_add = true;
  376. #ifdef TOOLS_ENABLED
  377. if (Engine::get_singleton()->is_editor_hint()) {
  378. Node *existing = parent->_get_child_by_name(snames[n.name]);
  379. if (existing) {
  380. // There's already a node in the same parent with the same name.
  381. // This means that somehow the node was added both to the scene being
  382. // loaded and another one instantiated in the former, maybe because of
  383. // manual editing, or a bug in scene saving, or a loophole in the workflow
  384. // (with any of the bugs possibly already fixed).
  385. // Bring consistency back by letting it be assigned a non-clashing name.
  386. // This simple workaround at least avoids leaks and helps the user realize
  387. // something awkward has happened.
  388. if (instantiation_warn_notify) {
  389. instantiation_warn_notify(vformat(
  390. TTR("An incoming node's name clashes with %s already in the scene (presumably, from a more nested instance).\nThe less nested node will be renamed. Please fix and re-save the scene."),
  391. ret_nodes[0]->get_path_to(existing)));
  392. }
  393. node->set_name(snames[n.name]);
  394. parent->add_child(node, true);
  395. pending_add = false;
  396. }
  397. }
  398. #endif
  399. if (pending_add) {
  400. parent->_add_child_nocheck(node, snames[n.name]);
  401. }
  402. if (n.index >= 0 && n.index < parent->get_child_count() - 1) {
  403. parent->move_child(node, n.index);
  404. }
  405. } else {
  406. //it may be possible that an instantiated scene has changed
  407. //and the node has nowhere to go anymore
  408. stray_instances.push_back(node); //can't be added, go to stray list
  409. }
  410. } else {
  411. if (Engine::get_singleton()->is_editor_hint()) {
  412. //validate name if using editor, to avoid broken
  413. node->set_name(snames[n.name]);
  414. } else {
  415. node->_set_name_nocheck(snames[n.name]);
  416. }
  417. }
  418. }
  419. if (!old_parent_path.is_empty()) {
  420. node->set_name(old_parent_path + "#" + node->get_name());
  421. }
  422. if (n.owner >= 0) {
  423. NODE_FROM_ID(owner, n.owner);
  424. if (owner) {
  425. node->_set_owner_nocheck(owner);
  426. if (node->data.unique_name_in_owner) {
  427. node->_acquire_unique_name_in_owner();
  428. }
  429. }
  430. }
  431. // We only want to deal with pinned flag if instantiating as pure main (no instance, no inheriting.)
  432. if (p_edit_state == GEN_EDIT_STATE_MAIN) {
  433. _sanitize_node_pinned_properties(node);
  434. } else {
  435. node->remove_meta("_edit_pinned_properties_");
  436. }
  437. }
  438. if (missing_node) {
  439. missing_node->set_recording_properties(false);
  440. }
  441. ret_nodes[i] = node;
  442. if (node && gen_node_path_cache && ret_nodes[0]) {
  443. NodePath n2 = ret_nodes[0]->get_path_to(node);
  444. node_path_cache[n2] = i;
  445. }
  446. }
  447. for (const DeferredNodePathProperties &dnp : deferred_node_paths) {
  448. // Replace properties stored as NodePaths with actual Nodes.
  449. if (dnp.value.get_type() == Variant::ARRAY) {
  450. Array paths = dnp.value;
  451. bool valid;
  452. Array array = dnp.base->get(dnp.property, &valid);
  453. ERR_CONTINUE_EDMSG(!valid, vformat("Failed to get property '%s' from node '%s'.", dnp.property, dnp.base->get_name()));
  454. array = array.duplicate();
  455. array.resize(paths.size());
  456. for (int i = 0; i < array.size(); i++) {
  457. array.set(i, dnp.base->get_node_or_null(paths[i]));
  458. }
  459. dnp.base->set(dnp.property, array);
  460. } else if (dnp.value.get_type() == Variant::DICTIONARY) {
  461. Dictionary paths = dnp.value;
  462. bool valid;
  463. Dictionary dict = dnp.base->get(dnp.property, &valid);
  464. ERR_CONTINUE_EDMSG(!valid, vformat("Failed to get property '%s' from node '%s'.", dnp.property, dnp.base->get_name()));
  465. dict = dict.duplicate();
  466. bool convert_key = dict.get_typed_key_builtin() == Variant::OBJECT &&
  467. ClassDB::is_parent_class(dict.get_typed_key_class_name(), "Node");
  468. bool convert_value = dict.get_typed_value_builtin() == Variant::OBJECT &&
  469. ClassDB::is_parent_class(dict.get_typed_value_class_name(), "Node");
  470. for (int i = 0; i < paths.size(); i++) {
  471. Variant key = paths.get_key_at_index(i);
  472. if (convert_key) {
  473. key = dnp.base->get_node_or_null(key);
  474. }
  475. Variant value = paths.get_value_at_index(i);
  476. if (convert_value) {
  477. value = dnp.base->get_node_or_null(value);
  478. }
  479. dict[key] = value;
  480. }
  481. dnp.base->set(dnp.property, dict);
  482. } else {
  483. dnp.base->set(dnp.property, dnp.base->get_node_or_null(dnp.value));
  484. }
  485. }
  486. for (KeyValue<Ref<Resource>, Ref<Resource>> &E : resources_local_to_scene) {
  487. if (E.value->get_local_scene() == ret_nodes[0]) {
  488. E.value->setup_local_to_scene();
  489. }
  490. }
  491. //do connections
  492. int cc = connections.size();
  493. const ConnectionData *cdata = connections.ptr();
  494. for (int i = 0; i < cc; i++) {
  495. const ConnectionData &c = cdata[i];
  496. //ERR_FAIL_INDEX_V( c.from, nc, nullptr );
  497. //ERR_FAIL_INDEX_V( c.to, nc, nullptr );
  498. NODE_FROM_ID(cfrom, c.from);
  499. NODE_FROM_ID(cto, c.to);
  500. if (!cfrom || !cto) {
  501. continue;
  502. }
  503. Callable callable(cto, snames[c.method]);
  504. if (c.unbinds > 0) {
  505. callable = callable.unbind(c.unbinds);
  506. } else if (!c.binds.is_empty()) {
  507. Vector<Variant> binds;
  508. if (c.binds.size()) {
  509. binds.resize(c.binds.size());
  510. for (int j = 0; j < c.binds.size(); j++) {
  511. binds.write[j] = props[c.binds[j]];
  512. }
  513. }
  514. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
  515. for (int j = 0; j < binds.size(); j++) {
  516. argptrs[j] = &binds[j];
  517. }
  518. callable = callable.bindp(argptrs, binds.size());
  519. }
  520. cfrom->connect(snames[c.signal], callable, CONNECT_PERSIST | c.flags | (p_edit_state == GEN_EDIT_STATE_MAIN ? 0 : CONNECT_INHERITED));
  521. }
  522. //Node *s = ret_nodes[0];
  523. //remove nodes that could not be added, likely as a result that
  524. while (stray_instances.size()) {
  525. memdelete(stray_instances.front()->get());
  526. stray_instances.pop_front();
  527. }
  528. for (int i = 0; i < editable_instances.size(); i++) {
  529. Node *ei = ret_nodes[0]->get_node_or_null(editable_instances[i]);
  530. if (ei) {
  531. ret_nodes[0]->set_editable_instance(ei, true);
  532. }
  533. }
  534. return ret_nodes[0];
  535. }
  536. Variant SceneState::make_local_resource(Variant &p_value, const SceneState::NodeData &p_node_data, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_sub_scene, Node *p_node, const StringName p_sname, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_scene, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const {
  537. Ref<Resource> res = p_value;
  538. if (res.is_null() || !res->is_local_to_scene()) {
  539. return p_value;
  540. }
  541. if (p_node_data.instance >= 0) { // For the root node of a sub-scene, treat it as part of the sub-scene.
  542. return get_remap_resource(res, p_resources_local_to_sub_scene, p_node->get(p_sname), p_node);
  543. } else {
  544. HashMap<Ref<Resource>, Ref<Resource>>::Iterator E = p_resources_local_to_scene.find(res);
  545. Node *base = p_i == 0 ? p_node : p_ret_nodes[0];
  546. if (E) {
  547. return E->value;
  548. } else if (p_edit_state == GEN_EDIT_STATE_MAIN) { // For the main scene, use the resource as is
  549. res->configure_for_local_scene(base, p_resources_local_to_scene);
  550. p_resources_local_to_scene[res] = res;
  551. return res;
  552. } else { // For instances, a copy must be made.
  553. Ref<Resource> local_dupe = res->duplicate_for_local_scene(base, p_resources_local_to_scene);
  554. p_resources_local_to_scene[res] = local_dupe;
  555. return local_dupe;
  556. }
  557. }
  558. }
  559. Array SceneState::setup_resources_in_array(Array &p_array_to_scan, const SceneState::NodeData &p_n, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_sub_scene, Node *p_node, const StringName p_sname, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_scene, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const {
  560. for (int i = 0; i < p_array_to_scan.size(); i++) {
  561. if (p_array_to_scan[i].get_type() == Variant::OBJECT) {
  562. p_array_to_scan[i] = make_local_resource(p_array_to_scan[i], p_n, p_resources_local_to_sub_scene, p_node, p_sname, p_resources_local_to_scene, p_i, p_ret_nodes, p_edit_state);
  563. }
  564. }
  565. return p_array_to_scan;
  566. }
  567. Dictionary SceneState::setup_resources_in_dictionary(Dictionary &p_dictionary_to_scan, const SceneState::NodeData &p_n, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_sub_scene, Node *p_node, const StringName p_sname, HashMap<Ref<Resource>, Ref<Resource>> &p_resources_local_to_scene, int p_i, Node **p_ret_nodes, SceneState::GenEditState p_edit_state) const {
  568. const Array keys = p_dictionary_to_scan.keys();
  569. const Array values = p_dictionary_to_scan.values();
  570. if (has_local_resource(values) || has_local_resource(keys)) {
  571. Array duplicated_keys = keys.duplicate(true);
  572. Array duplicated_values = values.duplicate(true);
  573. duplicated_keys = setup_resources_in_array(duplicated_keys, p_n, p_resources_local_to_sub_scene, p_node, p_sname, p_resources_local_to_scene, p_i, p_ret_nodes, p_edit_state);
  574. duplicated_values = setup_resources_in_array(duplicated_values, p_n, p_resources_local_to_sub_scene, p_node, p_sname, p_resources_local_to_scene, p_i, p_ret_nodes, p_edit_state);
  575. p_dictionary_to_scan.clear();
  576. for (int i = 0; i < keys.size(); i++) {
  577. p_dictionary_to_scan[duplicated_keys[i]] = duplicated_values[i];
  578. }
  579. }
  580. return p_dictionary_to_scan;
  581. }
  582. bool SceneState::has_local_resource(const Array &p_array) const {
  583. for (int i = 0; i < p_array.size(); i++) {
  584. Ref<Resource> res = p_array[i];
  585. if (res.is_valid() && res->is_local_to_scene()) {
  586. return true;
  587. }
  588. }
  589. return false;
  590. }
  591. static int _nm_get_string(const String &p_string, HashMap<StringName, int> &name_map) {
  592. if (name_map.has(p_string)) {
  593. return name_map[p_string];
  594. }
  595. int idx = name_map.size();
  596. name_map[p_string] = idx;
  597. return idx;
  598. }
  599. static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) {
  600. if (variant_map.has(p_variant)) {
  601. return variant_map[p_variant];
  602. }
  603. int idx = variant_map.size();
  604. variant_map[p_variant] = idx;
  605. return idx;
  606. }
  607. 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) {
  608. // this function handles all the work related to properly packing scenes, be it
  609. // instantiated or inherited.
  610. // given the complexity of this process, an attempt will be made to properly
  611. // document it. if you fail to understand something, please ask!
  612. //discard nodes that do not belong to be processed
  613. if (p_node != p_owner && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  614. return OK;
  615. }
  616. bool is_editable_instance = false;
  617. // save the child instantiated scenes that are chosen as editable, so they can be restored
  618. // upon load back
  619. if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
  620. editable_instances.push_back(p_owner->get_path_to(p_node));
  621. // Node is the root of an editable instance.
  622. is_editable_instance = true;
  623. } else if (p_node->get_owner() && p_owner->is_ancestor_of(p_node->get_owner()) && p_owner->is_editable_instance(p_node->get_owner())) {
  624. // Node is part of an editable instance.
  625. is_editable_instance = true;
  626. }
  627. NodeData nd;
  628. nd.name = _nm_get_string(p_node->get_name(), name_map);
  629. nd.instance = -1; //not instantiated by default
  630. //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
  631. 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)))) {
  632. //do not save index, because it belongs to saved scene and scene is not inherited
  633. nd.index = -1;
  634. } else if (p_node == p_owner) {
  635. //This (hopefully) happens if the node is a scene root, so its index is irrelevant.
  636. nd.index = -1;
  637. } else {
  638. //part of an inherited scene, or parent is from an instantiated scene
  639. nd.index = p_node->get_index();
  640. }
  641. // if this node is part of an instantiated scene or sub-instantiated scene
  642. // we need to get the corresponding instance states.
  643. // with the instance states, we can query for identical properties/groups
  644. // and only save what has changed
  645. bool instantiated_by_owner = false;
  646. Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
  647. if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
  648. if (p_node->get_scene_instance_load_placeholder()) {
  649. //it's a placeholder, use the placeholder path
  650. nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
  651. nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
  652. } else {
  653. //must instance ourselves
  654. Ref<PackedScene> instance = ResourceLoader::load(p_node->get_scene_file_path());
  655. if (instance.is_null()) {
  656. return ERR_CANT_OPEN;
  657. }
  658. nd.instance = _vm_get_variant(instance, variant_map);
  659. }
  660. }
  661. // all setup, we then proceed to check all properties for the node
  662. // and save the ones that are worth saving
  663. List<PropertyInfo> plist;
  664. p_node->get_property_list(&plist);
  665. Array pinned_props = _sanitize_node_pinned_properties(p_node);
  666. Dictionary missing_resource_properties = p_node->get_meta(META_MISSING_RESOURCES, Dictionary());
  667. for (const PropertyInfo &E : plist) {
  668. if (!(E.usage & PROPERTY_USAGE_STORAGE) && !missing_resource_properties.has(E.name)) {
  669. continue;
  670. }
  671. if (E.name == META_PROPERTY_MISSING_RESOURCES) {
  672. continue; // Ignore this property when packing.
  673. }
  674. // If instance or inheriting, not saving if property requested so.
  675. if (!states_stack.is_empty()) {
  676. if ((E.usage & PROPERTY_USAGE_NO_INSTANCE_STATE)) {
  677. continue;
  678. }
  679. }
  680. StringName name = E.name;
  681. Variant value = p_node->get(name);
  682. bool use_deferred_node_path_bit = false;
  683. if (E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_NODE_TYPE) {
  684. if (value.get_type() == Variant::OBJECT) {
  685. if (Node *n = Object::cast_to<Node>(value)) {
  686. value = p_node->get_path_to(n);
  687. }
  688. use_deferred_node_path_bit = true;
  689. }
  690. if (value.get_type() != Variant::NODE_PATH) {
  691. continue; //was never set, ignore.
  692. }
  693. } else if (E.type == Variant::OBJECT && missing_resource_properties.has(E.name)) {
  694. // Was this missing resource overridden? If so do not save the old value.
  695. Ref<Resource> ures = value;
  696. if (ures.is_null()) {
  697. value = missing_resource_properties[E.name];
  698. }
  699. } else if (E.type == Variant::ARRAY && E.hint == PROPERTY_HINT_TYPE_STRING) {
  700. int hint_subtype_separator = E.hint_string.find_char(':');
  701. if (hint_subtype_separator >= 0) {
  702. String subtype_string = E.hint_string.substr(0, hint_subtype_separator);
  703. int slash_pos = subtype_string.find_char('/');
  704. PropertyHint subtype_hint = PropertyHint::PROPERTY_HINT_NONE;
  705. if (slash_pos >= 0) {
  706. subtype_hint = PropertyHint(subtype_string.get_slice("/", 1).to_int());
  707. subtype_string = subtype_string.substr(0, slash_pos);
  708. }
  709. Variant::Type subtype = Variant::Type(subtype_string.to_int());
  710. if (subtype == Variant::OBJECT && subtype_hint == PROPERTY_HINT_NODE_TYPE) {
  711. use_deferred_node_path_bit = true;
  712. Array array = value;
  713. Array new_array;
  714. for (int i = 0; i < array.size(); i++) {
  715. Variant elem = array[i];
  716. if (elem.get_type() == Variant::OBJECT) {
  717. if (Node *n = Object::cast_to<Node>(elem)) {
  718. new_array.push_back(p_node->get_path_to(n));
  719. continue;
  720. }
  721. }
  722. new_array.push_back(elem);
  723. }
  724. value = new_array;
  725. }
  726. }
  727. } else if (E.type == Variant::DICTIONARY && E.hint == PROPERTY_HINT_TYPE_STRING) {
  728. int key_value_separator = E.hint_string.find_char(';');
  729. if (key_value_separator >= 0) {
  730. int key_subtype_separator = E.hint_string.find_char(':');
  731. String key_subtype_string = E.hint_string.substr(0, key_subtype_separator);
  732. int key_slash_pos = key_subtype_string.find_char('/');
  733. PropertyHint key_subtype_hint = PropertyHint::PROPERTY_HINT_NONE;
  734. if (key_slash_pos >= 0) {
  735. key_subtype_hint = PropertyHint(key_subtype_string.get_slice("/", 1).to_int());
  736. key_subtype_string = key_subtype_string.substr(0, key_slash_pos);
  737. }
  738. Variant::Type key_subtype = Variant::Type(key_subtype_string.to_int());
  739. bool convert_key = key_subtype == Variant::OBJECT && key_subtype_hint == PROPERTY_HINT_NODE_TYPE;
  740. int value_subtype_separator = E.hint_string.find_char(':', key_value_separator) - (key_value_separator + 1);
  741. String value_subtype_string = E.hint_string.substr(key_value_separator + 1, value_subtype_separator);
  742. int value_slash_pos = value_subtype_string.find_char('/');
  743. PropertyHint value_subtype_hint = PropertyHint::PROPERTY_HINT_NONE;
  744. if (value_slash_pos >= 0) {
  745. value_subtype_hint = PropertyHint(value_subtype_string.get_slice("/", 1).to_int());
  746. value_subtype_string = value_subtype_string.substr(0, value_slash_pos);
  747. }
  748. Variant::Type value_subtype = Variant::Type(value_subtype_string.to_int());
  749. bool convert_value = value_subtype == Variant::OBJECT && value_subtype_hint == PROPERTY_HINT_NODE_TYPE;
  750. if (convert_key || convert_value) {
  751. use_deferred_node_path_bit = true;
  752. Dictionary dict = value;
  753. Dictionary new_dict;
  754. for (int i = 0; i < dict.size(); i++) {
  755. Variant new_key = dict.get_key_at_index(i);
  756. if (convert_key && new_key.get_type() == Variant::OBJECT) {
  757. if (Node *n = Object::cast_to<Node>(new_key)) {
  758. new_key = p_node->get_path_to(n);
  759. }
  760. }
  761. Variant new_value = dict.get_value_at_index(i);
  762. if (convert_value && new_value.get_type() == Variant::OBJECT) {
  763. if (Node *n = Object::cast_to<Node>(new_value)) {
  764. new_value = p_node->get_path_to(n);
  765. }
  766. }
  767. new_dict[new_key] = new_value;
  768. }
  769. value = new_dict;
  770. }
  771. }
  772. }
  773. if (!pinned_props.has(name)) {
  774. bool is_valid_default = false;
  775. Variant default_value = PropertyUtils::get_property_default_value(p_node, name, &is_valid_default, &states_stack, true);
  776. if (is_valid_default && !PropertyUtils::is_property_value_different(p_node, value, default_value)) {
  777. if (value.get_type() == Variant::ARRAY && has_local_resource(value)) {
  778. // Save anyway
  779. } else if (value.get_type() == Variant::DICTIONARY) {
  780. Dictionary dictionary = value;
  781. if (!has_local_resource(dictionary.values()) && !has_local_resource(dictionary.keys())) {
  782. continue;
  783. }
  784. } else {
  785. continue;
  786. }
  787. }
  788. }
  789. NodeData::Property prop;
  790. prop.name = _nm_get_string(name, name_map);
  791. prop.value = _vm_get_variant(value, variant_map);
  792. if (use_deferred_node_path_bit) {
  793. prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
  794. }
  795. nd.properties.push_back(prop);
  796. }
  797. // save the groups this node is into
  798. // discard groups that come from the original scene
  799. List<Node::GroupInfo> groups;
  800. p_node->get_groups(&groups);
  801. for (const Node::GroupInfo &gi : groups) {
  802. if (!gi.persistent) {
  803. continue;
  804. }
  805. bool skip = false;
  806. for (const SceneState::PackState &ia : states_stack) {
  807. //check all levels of pack to see if the group was added somewhere
  808. if (ia.state->is_node_in_group(ia.node, gi.name)) {
  809. skip = true;
  810. break;
  811. }
  812. }
  813. if (skip) {
  814. continue;
  815. }
  816. nd.groups.push_back(_nm_get_string(gi.name, name_map));
  817. }
  818. // save the right owner
  819. // for the saved scene root this is -1
  820. // for nodes of the saved scene this is 0
  821. // for nodes of instantiated scenes this is >0
  822. if (p_node == p_owner) {
  823. //saved scene root
  824. nd.owner = -1;
  825. } else if (p_node->get_owner() == p_owner) {
  826. //part of saved scene
  827. nd.owner = 0;
  828. } else {
  829. nd.owner = -1;
  830. }
  831. MissingNode *missing_node = Object::cast_to<MissingNode>(p_node);
  832. // Save the right type. If this node was created by an instance
  833. // then flag that the node should not be created but reused
  834. if (states_stack.is_empty() && !is_editable_instance) {
  835. //This node is not part of an instantiation process, so save the type.
  836. if (missing_node != nullptr) {
  837. // It's a missing node (type non existent on load).
  838. nd.type = _nm_get_string(missing_node->get_original_class(), name_map);
  839. } else {
  840. nd.type = _nm_get_string(p_node->get_class(), name_map);
  841. }
  842. } else {
  843. // this node is part of an instantiated process, so do not save the type.
  844. // instead, save that it was instantiated
  845. nd.type = TYPE_INSTANTIATED;
  846. }
  847. // determine whether to save this node or not
  848. // if this node is part of an instantiated sub-scene, we can skip storing it if basically
  849. // no properties changed and no groups were added to it.
  850. // below condition is true for all nodes of the scene being saved, and ones in subscenes
  851. // that hold changes
  852. bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
  853. save_node = save_node || p_node == p_owner; // owner is always saved
  854. save_node = save_node || (p_node->get_owner() == p_owner && instantiated_by_owner); //part of scene and not instanced
  855. int idx = nodes.size();
  856. int parent_node = NO_PARENT_SAVED;
  857. if (save_node) {
  858. //don't save the node if nothing and subscene
  859. node_map[p_node] = idx;
  860. //ok validate parent node
  861. if (p_parent_idx == NO_PARENT_SAVED) {
  862. int sidx;
  863. if (nodepath_map.has(p_node->get_parent())) {
  864. sidx = nodepath_map[p_node->get_parent()];
  865. } else {
  866. sidx = nodepath_map.size();
  867. nodepath_map[p_node->get_parent()] = sidx;
  868. }
  869. nd.parent = FLAG_ID_IS_PATH | sidx;
  870. } else {
  871. nd.parent = p_parent_idx;
  872. }
  873. parent_node = idx;
  874. nodes.push_back(nd);
  875. }
  876. for (int i = 0; i < p_node->get_child_count(); i++) {
  877. Node *c = p_node->get_child(i);
  878. Error err = _parse_node(p_owner, c, parent_node, name_map, variant_map, node_map, nodepath_map);
  879. if (err) {
  880. return err;
  881. }
  882. }
  883. return OK;
  884. }
  885. 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) {
  886. // Ignore nodes that are within a scene instance.
  887. if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  888. return OK;
  889. }
  890. List<MethodInfo> _signals;
  891. p_node->get_signal_list(&_signals);
  892. _signals.sort();
  893. //ERR_FAIL_COND_V( !node_map.has(p_node), ERR_BUG);
  894. //NodeData &nd = nodes[node_map[p_node]];
  895. for (const MethodInfo &E : _signals) {
  896. List<Node::Connection> conns;
  897. p_node->get_signal_connection_list(E.name, &conns);
  898. conns.sort();
  899. for (const Node::Connection &F : conns) {
  900. const Node::Connection &c = F;
  901. // Don't save connections that are not persistent.
  902. if (!(c.flags & CONNECT_PERSIST)) {
  903. continue;
  904. }
  905. // only connections that originate or end into main saved scene are saved
  906. // everything else is discarded
  907. Node *target = Object::cast_to<Node>(c.callable.get_object());
  908. if (!target) {
  909. continue;
  910. }
  911. Vector<Variant> binds;
  912. int unbinds = 0;
  913. Callable base_callable;
  914. if (c.callable.is_custom()) {
  915. CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(c.callable.get_custom());
  916. if (ccb) {
  917. binds = ccb->get_binds();
  918. base_callable = ccb->get_callable();
  919. }
  920. CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(c.callable.get_custom());
  921. if (ccu) {
  922. unbinds = ccu->get_unbinds();
  923. base_callable = ccu->get_callable();
  924. }
  925. } else {
  926. base_callable = c.callable;
  927. }
  928. //find if this connection already exists
  929. Node *common_parent = target->find_common_parent_with(p_node);
  930. ERR_CONTINUE(!common_parent);
  931. if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
  932. common_parent = common_parent->get_owner();
  933. }
  934. bool exists = false;
  935. //go through ownership chain to see if this exists
  936. while (common_parent) {
  937. Ref<SceneState> ps;
  938. if (common_parent == p_owner) {
  939. ps = common_parent->get_scene_inherited_state();
  940. } else {
  941. ps = common_parent->get_scene_instance_state();
  942. }
  943. if (ps.is_valid()) {
  944. NodePath signal_from = common_parent->get_path_to(p_node);
  945. NodePath signal_to = common_parent->get_path_to(target);
  946. if (ps->has_connection(signal_from, c.signal.get_name(), signal_to, base_callable.get_method())) {
  947. exists = true;
  948. break;
  949. }
  950. }
  951. if (common_parent == p_owner) {
  952. break;
  953. } else {
  954. common_parent = common_parent->get_owner();
  955. }
  956. }
  957. if (exists) { //already exists (comes from instance or inheritance), so don't save
  958. continue;
  959. }
  960. {
  961. Node *nl = p_node;
  962. bool exists2 = false;
  963. while (nl) {
  964. if (nl == p_owner) {
  965. Ref<SceneState> state = nl->get_scene_inherited_state();
  966. if (state.is_valid()) {
  967. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  968. int to_node = state->find_node_by_path(nl->get_path_to(target));
  969. if (from_node >= 0 && to_node >= 0) {
  970. //this one has state for this node, save
  971. if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
  972. exists2 = true;
  973. break;
  974. }
  975. }
  976. }
  977. nl = nullptr;
  978. } else {
  979. if (!nl->get_scene_file_path().is_empty()) {
  980. //is an instance
  981. Ref<SceneState> state = nl->get_scene_instance_state();
  982. if (state.is_valid()) {
  983. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  984. int to_node = state->find_node_by_path(nl->get_path_to(target));
  985. if (from_node >= 0 && to_node >= 0) {
  986. //this one has state for this node, save
  987. if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
  988. exists2 = true;
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. nl = nl->get_owner();
  995. }
  996. }
  997. if (exists2) {
  998. continue;
  999. }
  1000. }
  1001. int src_id;
  1002. if (node_map.has(p_node)) {
  1003. src_id = node_map[p_node];
  1004. } else {
  1005. if (nodepath_map.has(p_node)) {
  1006. src_id = FLAG_ID_IS_PATH | nodepath_map[p_node];
  1007. } else {
  1008. int sidx = nodepath_map.size();
  1009. nodepath_map[p_node] = sidx;
  1010. src_id = FLAG_ID_IS_PATH | sidx;
  1011. }
  1012. }
  1013. int target_id;
  1014. if (node_map.has(target)) {
  1015. target_id = node_map[target];
  1016. } else {
  1017. if (nodepath_map.has(target)) {
  1018. target_id = FLAG_ID_IS_PATH | nodepath_map[target];
  1019. } else {
  1020. int sidx = nodepath_map.size();
  1021. nodepath_map[target] = sidx;
  1022. target_id = FLAG_ID_IS_PATH | sidx;
  1023. }
  1024. }
  1025. ConnectionData cd;
  1026. cd.from = src_id;
  1027. cd.to = target_id;
  1028. cd.method = _nm_get_string(base_callable.get_method(), name_map);
  1029. cd.signal = _nm_get_string(c.signal.get_name(), name_map);
  1030. cd.flags = c.flags & ~CONNECT_INHERITED; // Do not store inherited.
  1031. cd.unbinds = unbinds;
  1032. for (int i = 0; i < binds.size(); i++) {
  1033. cd.binds.push_back(_vm_get_variant(binds[i], variant_map));
  1034. }
  1035. connections.push_back(cd);
  1036. }
  1037. }
  1038. // Recursively parse child connections.
  1039. for (int i = 0; i < p_node->get_child_count(); i++) {
  1040. Node *child = p_node->get_child(i);
  1041. Error err = _parse_connections(p_owner, child, name_map, variant_map, node_map, nodepath_map);
  1042. if (err) {
  1043. return err;
  1044. }
  1045. }
  1046. return OK;
  1047. }
  1048. Error SceneState::pack(Node *p_scene) {
  1049. ERR_FAIL_NULL_V(p_scene, ERR_INVALID_PARAMETER);
  1050. clear();
  1051. Node *scene = p_scene;
  1052. HashMap<StringName, int> name_map;
  1053. HashMap<Variant, int, VariantHasher, VariantComparator> variant_map;
  1054. HashMap<Node *, int> node_map;
  1055. HashMap<Node *, int> nodepath_map;
  1056. // If using scene inheritance, pack the scene it inherits from.
  1057. if (scene->get_scene_inherited_state().is_valid()) {
  1058. String scene_path = scene->get_scene_inherited_state()->get_path();
  1059. Ref<PackedScene> instance = ResourceLoader::load(scene_path);
  1060. if (instance.is_valid()) {
  1061. base_scene_idx = _vm_get_variant(instance, variant_map);
  1062. }
  1063. }
  1064. // Instanced, only direct sub-scenes are supported of course.
  1065. Error err = _parse_node(scene, scene, -1, name_map, variant_map, node_map, nodepath_map);
  1066. if (err) {
  1067. clear();
  1068. ERR_FAIL_V(err);
  1069. }
  1070. err = _parse_connections(scene, scene, name_map, variant_map, node_map, nodepath_map);
  1071. if (err) {
  1072. clear();
  1073. ERR_FAIL_V(err);
  1074. }
  1075. names.resize(name_map.size());
  1076. for (const KeyValue<StringName, int> &E : name_map) {
  1077. names.write[E.value] = E.key;
  1078. }
  1079. variants.resize(variant_map.size());
  1080. for (const KeyValue<Variant, int> &E : variant_map) {
  1081. int idx = E.value;
  1082. variants.write[idx] = E.key;
  1083. }
  1084. node_paths.resize(nodepath_map.size());
  1085. for (const KeyValue<Node *, int> &E : nodepath_map) {
  1086. node_paths.write[E.value] = scene->get_path_to(E.key);
  1087. }
  1088. if (Engine::get_singleton()->is_editor_hint()) {
  1089. // Build node path cache
  1090. for (const KeyValue<Node *, int> &E : node_map) {
  1091. node_path_cache[scene->get_path_to(E.key)] = E.value;
  1092. }
  1093. }
  1094. return OK;
  1095. }
  1096. void SceneState::set_path(const String &p_path) {
  1097. path = p_path;
  1098. }
  1099. String SceneState::get_path() const {
  1100. return path;
  1101. }
  1102. void SceneState::clear() {
  1103. names.clear();
  1104. variants.clear();
  1105. nodes.clear();
  1106. connections.clear();
  1107. node_path_cache.clear();
  1108. node_paths.clear();
  1109. editable_instances.clear();
  1110. base_scene_idx = -1;
  1111. }
  1112. Error SceneState::copy_from(const Ref<SceneState> &p_scene_state) {
  1113. ERR_FAIL_COND_V(p_scene_state.is_null(), ERR_INVALID_PARAMETER);
  1114. clear();
  1115. for (const StringName &E : p_scene_state->names) {
  1116. names.append(E);
  1117. }
  1118. for (const Variant &E : p_scene_state->variants) {
  1119. variants.append(E);
  1120. }
  1121. for (const SceneState::NodeData &E : p_scene_state->nodes) {
  1122. nodes.append(E);
  1123. }
  1124. for (const SceneState::ConnectionData &E : p_scene_state->connections) {
  1125. connections.append(E);
  1126. }
  1127. for (KeyValue<NodePath, int> &E : p_scene_state->node_path_cache) {
  1128. node_path_cache.insert(E.key, E.value);
  1129. }
  1130. for (const NodePath &E : p_scene_state->node_paths) {
  1131. node_paths.append(E);
  1132. }
  1133. for (const NodePath &E : p_scene_state->editable_instances) {
  1134. editable_instances.append(E);
  1135. }
  1136. base_scene_idx = p_scene_state->base_scene_idx;
  1137. return OK;
  1138. }
  1139. Ref<SceneState> SceneState::get_base_scene_state() const {
  1140. if (base_scene_idx >= 0) {
  1141. Ref<PackedScene> ps = variants[base_scene_idx];
  1142. if (ps.is_valid()) {
  1143. return ps->get_state();
  1144. }
  1145. }
  1146. return Ref<SceneState>();
  1147. }
  1148. int SceneState::find_node_by_path(const NodePath &p_node) const {
  1149. ERR_FAIL_COND_V_MSG(node_path_cache.is_empty(), -1, "This operation requires the node cache to have been built.");
  1150. if (!node_path_cache.has(p_node)) {
  1151. if (get_base_scene_state().is_valid()) {
  1152. int idx = get_base_scene_state()->find_node_by_path(p_node);
  1153. if (idx != -1) {
  1154. int rkey = _find_base_scene_node_remap_key(idx);
  1155. if (rkey == -1) {
  1156. rkey = nodes.size() + base_scene_node_remap.size();
  1157. base_scene_node_remap[rkey] = idx;
  1158. }
  1159. return rkey;
  1160. }
  1161. }
  1162. return -1;
  1163. }
  1164. int nid = node_path_cache[p_node];
  1165. if (get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
  1166. //for nodes that _do_ exist in current scene, still try to look for
  1167. //the node in the instantiated scene, as a property may be missing
  1168. //from the local one
  1169. int idx = get_base_scene_state()->find_node_by_path(p_node);
  1170. if (idx != -1) {
  1171. base_scene_node_remap[nid] = idx;
  1172. }
  1173. }
  1174. return nid;
  1175. }
  1176. int SceneState::_find_base_scene_node_remap_key(int p_idx) const {
  1177. for (const KeyValue<int, int> &E : base_scene_node_remap) {
  1178. if (E.value == p_idx) {
  1179. return E.key;
  1180. }
  1181. }
  1182. return -1;
  1183. }
  1184. Variant SceneState::get_property_value(int p_node, const StringName &p_property, bool &r_found, bool &r_node_deferred) const {
  1185. r_found = false;
  1186. r_node_deferred = false;
  1187. ERR_FAIL_COND_V(p_node < 0, Variant());
  1188. if (p_node < nodes.size()) {
  1189. // Find in built-in nodes.
  1190. int pc = nodes[p_node].properties.size();
  1191. const StringName *namep = names.ptr();
  1192. const NodeData::Property *p = nodes[p_node].properties.ptr();
  1193. for (int i = 0; i < pc; i++) {
  1194. if (p_property == namep[p[i].name & FLAG_PROP_NAME_MASK]) {
  1195. r_found = true;
  1196. r_node_deferred = p[i].name & FLAG_PATH_PROPERTY_IS_NODE;
  1197. return variants[p[i].value];
  1198. }
  1199. }
  1200. }
  1201. // Property not found, try on instance.
  1202. HashMap<int, int>::ConstIterator I = base_scene_node_remap.find(p_node);
  1203. if (I) {
  1204. return get_base_scene_state()->get_property_value(I->value, p_property, r_found, r_node_deferred);
  1205. }
  1206. return Variant();
  1207. }
  1208. bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
  1209. ERR_FAIL_COND_V(p_node < 0, false);
  1210. if (p_node < nodes.size()) {
  1211. const StringName *namep = names.ptr();
  1212. for (int i = 0; i < nodes[p_node].groups.size(); i++) {
  1213. if (namep[nodes[p_node].groups[i]] == p_group) {
  1214. return true;
  1215. }
  1216. }
  1217. }
  1218. if (base_scene_node_remap.has(p_node)) {
  1219. return get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
  1220. }
  1221. return false;
  1222. }
  1223. bool SceneState::disable_placeholders = false;
  1224. void SceneState::set_disable_placeholders(bool p_disable) {
  1225. disable_placeholders = p_disable;
  1226. }
  1227. bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const {
  1228. ERR_FAIL_COND_V(p_node < 0, false);
  1229. ERR_FAIL_COND_V(p_to_node < 0, false);
  1230. if (p_node < nodes.size() && p_to_node < nodes.size()) {
  1231. int signal_idx = -1;
  1232. int method_idx = -1;
  1233. for (int i = 0; i < names.size(); i++) {
  1234. if (names[i] == p_signal) {
  1235. signal_idx = i;
  1236. } else if (names[i] == p_to_method) {
  1237. method_idx = i;
  1238. }
  1239. }
  1240. if (signal_idx >= 0 && method_idx >= 0) {
  1241. //signal and method strings are stored..
  1242. for (int i = 0; i < connections.size(); i++) {
  1243. if (connections[i].from == p_node && connections[i].to == p_to_node && connections[i].signal == signal_idx && connections[i].method == method_idx) {
  1244. return true;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
  1250. 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);
  1251. }
  1252. return false;
  1253. }
  1254. void SceneState::set_bundled_scene(const Dictionary &p_dictionary) {
  1255. ERR_FAIL_COND(!p_dictionary.has("names"));
  1256. ERR_FAIL_COND(!p_dictionary.has("variants"));
  1257. ERR_FAIL_COND(!p_dictionary.has("node_count"));
  1258. ERR_FAIL_COND(!p_dictionary.has("nodes"));
  1259. ERR_FAIL_COND(!p_dictionary.has("conn_count"));
  1260. ERR_FAIL_COND(!p_dictionary.has("conns"));
  1261. //ERR_FAIL_COND( !p_dictionary.has("path"));
  1262. int version = 1;
  1263. if (p_dictionary.has("version")) {
  1264. version = p_dictionary["version"];
  1265. }
  1266. ERR_FAIL_COND_MSG(version > PACKED_SCENE_VERSION, "Save format version too new.");
  1267. const int node_count = p_dictionary["node_count"];
  1268. const Vector<int> snodes = p_dictionary["nodes"];
  1269. ERR_FAIL_COND(snodes.size() < node_count);
  1270. const int conn_count = p_dictionary["conn_count"];
  1271. const Vector<int> sconns = p_dictionary["conns"];
  1272. ERR_FAIL_COND(sconns.size() < conn_count);
  1273. Vector<String> snames = p_dictionary["names"];
  1274. if (snames.size()) {
  1275. int namecount = snames.size();
  1276. names.resize(namecount);
  1277. const String *r = snames.ptr();
  1278. for (int i = 0; i < names.size(); i++) {
  1279. names.write[i] = r[i];
  1280. }
  1281. }
  1282. Array svariants = p_dictionary["variants"];
  1283. if (svariants.size()) {
  1284. int varcount = svariants.size();
  1285. variants.resize(varcount);
  1286. for (int i = 0; i < varcount; i++) {
  1287. variants.write[i] = svariants[i];
  1288. }
  1289. } else {
  1290. variants.clear();
  1291. }
  1292. nodes.resize(node_count);
  1293. if (node_count) {
  1294. const int *r = snodes.ptr();
  1295. int idx = 0;
  1296. for (int i = 0; i < node_count; i++) {
  1297. NodeData &nd = nodes.write[i];
  1298. nd.parent = r[idx++];
  1299. nd.owner = r[idx++];
  1300. nd.type = r[idx++];
  1301. uint32_t name_index = r[idx++];
  1302. nd.name = name_index & ((1 << NAME_INDEX_BITS) - 1);
  1303. nd.index = (name_index >> NAME_INDEX_BITS);
  1304. nd.index--; //0 is invalid, stored as 1
  1305. nd.instance = r[idx++];
  1306. nd.properties.resize(r[idx++]);
  1307. for (int j = 0; j < nd.properties.size(); j++) {
  1308. nd.properties.write[j].name = r[idx++];
  1309. nd.properties.write[j].value = r[idx++];
  1310. }
  1311. nd.groups.resize(r[idx++]);
  1312. for (int j = 0; j < nd.groups.size(); j++) {
  1313. nd.groups.write[j] = r[idx++];
  1314. }
  1315. }
  1316. }
  1317. connections.resize(conn_count);
  1318. if (conn_count) {
  1319. const int *r = sconns.ptr();
  1320. int idx = 0;
  1321. for (int i = 0; i < conn_count; i++) {
  1322. ConnectionData &cd = connections.write[i];
  1323. cd.from = r[idx++];
  1324. cd.to = r[idx++];
  1325. cd.signal = r[idx++];
  1326. cd.method = r[idx++];
  1327. cd.flags = r[idx++];
  1328. cd.binds.resize(r[idx++]);
  1329. for (int j = 0; j < cd.binds.size(); j++) {
  1330. cd.binds.write[j] = r[idx++];
  1331. }
  1332. if (version >= 3) {
  1333. cd.unbinds = r[idx++];
  1334. }
  1335. }
  1336. }
  1337. Array np;
  1338. if (p_dictionary.has("node_paths")) {
  1339. np = p_dictionary["node_paths"];
  1340. }
  1341. node_paths.resize(np.size());
  1342. for (int i = 0; i < np.size(); i++) {
  1343. node_paths.write[i] = np[i];
  1344. }
  1345. Array ei;
  1346. if (p_dictionary.has("editable_instances")) {
  1347. ei = p_dictionary["editable_instances"];
  1348. }
  1349. if (p_dictionary.has("base_scene")) {
  1350. base_scene_idx = p_dictionary["base_scene"];
  1351. }
  1352. editable_instances.resize(ei.size());
  1353. for (int i = 0; i < editable_instances.size(); i++) {
  1354. editable_instances.write[i] = ei[i];
  1355. }
  1356. //path=p_dictionary["path"];
  1357. }
  1358. Dictionary SceneState::get_bundled_scene() const {
  1359. Vector<String> rnames;
  1360. rnames.resize(names.size());
  1361. if (names.size()) {
  1362. String *r = rnames.ptrw();
  1363. for (int i = 0; i < names.size(); i++) {
  1364. r[i] = names[i];
  1365. }
  1366. }
  1367. Dictionary d;
  1368. d["names"] = rnames;
  1369. d["variants"] = variants;
  1370. Vector<int> rnodes;
  1371. d["node_count"] = nodes.size();
  1372. for (int i = 0; i < nodes.size(); i++) {
  1373. const NodeData &nd = nodes[i];
  1374. rnodes.push_back(nd.parent);
  1375. rnodes.push_back(nd.owner);
  1376. rnodes.push_back(nd.type);
  1377. uint32_t name_index = nd.name;
  1378. if (nd.index < (1 << (32 - NAME_INDEX_BITS)) - 1) { //save if less than 16k children
  1379. name_index |= uint32_t(nd.index + 1) << NAME_INDEX_BITS; //for backwards compatibility, index 0 is no index
  1380. }
  1381. rnodes.push_back(name_index);
  1382. rnodes.push_back(nd.instance);
  1383. rnodes.push_back(nd.properties.size());
  1384. for (int j = 0; j < nd.properties.size(); j++) {
  1385. rnodes.push_back(nd.properties[j].name);
  1386. rnodes.push_back(nd.properties[j].value);
  1387. }
  1388. rnodes.push_back(nd.groups.size());
  1389. for (int j = 0; j < nd.groups.size(); j++) {
  1390. rnodes.push_back(nd.groups[j]);
  1391. }
  1392. }
  1393. d["nodes"] = rnodes;
  1394. Vector<int> rconns;
  1395. d["conn_count"] = connections.size();
  1396. for (int i = 0; i < connections.size(); i++) {
  1397. const ConnectionData &cd = connections[i];
  1398. rconns.push_back(cd.from);
  1399. rconns.push_back(cd.to);
  1400. rconns.push_back(cd.signal);
  1401. rconns.push_back(cd.method);
  1402. rconns.push_back(cd.flags);
  1403. rconns.push_back(cd.binds.size());
  1404. for (int j = 0; j < cd.binds.size(); j++) {
  1405. rconns.push_back(cd.binds[j]);
  1406. }
  1407. rconns.push_back(cd.unbinds);
  1408. }
  1409. d["conns"] = rconns;
  1410. Array rnode_paths;
  1411. rnode_paths.resize(node_paths.size());
  1412. for (int i = 0; i < node_paths.size(); i++) {
  1413. rnode_paths[i] = node_paths[i];
  1414. }
  1415. d["node_paths"] = rnode_paths;
  1416. Array reditable_instances;
  1417. reditable_instances.resize(editable_instances.size());
  1418. for (int i = 0; i < editable_instances.size(); i++) {
  1419. reditable_instances[i] = editable_instances[i];
  1420. }
  1421. d["editable_instances"] = reditable_instances;
  1422. if (base_scene_idx >= 0) {
  1423. d["base_scene"] = base_scene_idx;
  1424. }
  1425. d["version"] = PACKED_SCENE_VERSION;
  1426. return d;
  1427. }
  1428. int SceneState::get_node_count() const {
  1429. return nodes.size();
  1430. }
  1431. StringName SceneState::get_node_type(int p_idx) const {
  1432. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1433. if (nodes[p_idx].type == TYPE_INSTANTIATED) {
  1434. return StringName();
  1435. }
  1436. return names[nodes[p_idx].type];
  1437. }
  1438. StringName SceneState::get_node_name(int p_idx) const {
  1439. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1440. return names[nodes[p_idx].name];
  1441. }
  1442. int SceneState::get_node_index(int p_idx) const {
  1443. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1444. return nodes[p_idx].index;
  1445. }
  1446. bool SceneState::is_node_instance_placeholder(int p_idx) const {
  1447. ERR_FAIL_INDEX_V(p_idx, nodes.size(), false);
  1448. return nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER);
  1449. }
  1450. Ref<PackedScene> SceneState::get_node_instance(int p_idx) const {
  1451. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Ref<PackedScene>());
  1452. if (nodes[p_idx].instance >= 0) {
  1453. if (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  1454. return Ref<PackedScene>();
  1455. } else {
  1456. return variants[nodes[p_idx].instance & FLAG_MASK];
  1457. }
  1458. } else if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1459. if (base_scene_idx >= 0) {
  1460. return variants[base_scene_idx];
  1461. }
  1462. }
  1463. return Ref<PackedScene>();
  1464. }
  1465. String SceneState::get_node_instance_placeholder(int p_idx) const {
  1466. ERR_FAIL_INDEX_V(p_idx, nodes.size(), String());
  1467. if (nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
  1468. return variants[nodes[p_idx].instance & FLAG_MASK];
  1469. }
  1470. return String();
  1471. }
  1472. Vector<StringName> SceneState::get_node_groups(int p_idx) const {
  1473. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Vector<StringName>());
  1474. Vector<StringName> groups;
  1475. for (int i = 0; i < nodes[p_idx].groups.size(); i++) {
  1476. groups.push_back(names[nodes[p_idx].groups[i]]);
  1477. }
  1478. return groups;
  1479. }
  1480. NodePath SceneState::get_node_path(int p_idx, bool p_for_parent) const {
  1481. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1482. if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1483. if (p_for_parent) {
  1484. return NodePath();
  1485. } else {
  1486. return NodePath(".");
  1487. }
  1488. }
  1489. Vector<StringName> sub_path;
  1490. NodePath base_path;
  1491. int nidx = p_idx;
  1492. while (true) {
  1493. if (nodes[nidx].parent == NO_PARENT_SAVED || nodes[nidx].parent < 0) {
  1494. sub_path.insert(0, ".");
  1495. break;
  1496. }
  1497. if (!p_for_parent || p_idx != nidx) {
  1498. sub_path.insert(0, names[nodes[nidx].name]);
  1499. }
  1500. if (nodes[nidx].parent & FLAG_ID_IS_PATH) {
  1501. base_path = node_paths[nodes[nidx].parent & FLAG_MASK];
  1502. break;
  1503. } else {
  1504. nidx = nodes[nidx].parent & FLAG_MASK;
  1505. }
  1506. }
  1507. for (int i = base_path.get_name_count() - 1; i >= 0; i--) {
  1508. sub_path.insert(0, base_path.get_name(i));
  1509. }
  1510. if (sub_path.is_empty()) {
  1511. return NodePath(".");
  1512. }
  1513. return NodePath(sub_path, false);
  1514. }
  1515. int SceneState::get_node_property_count(int p_idx) const {
  1516. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1517. return nodes[p_idx].properties.size();
  1518. }
  1519. StringName SceneState::get_node_property_name(int p_idx, int p_prop) const {
  1520. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1521. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), StringName());
  1522. return names[nodes[p_idx].properties[p_prop].name & FLAG_PROP_NAME_MASK];
  1523. }
  1524. Vector<String> SceneState::get_node_deferred_nodepath_properties(int p_idx) const {
  1525. Vector<String> ret;
  1526. ERR_FAIL_COND_V(p_idx < 0, ret);
  1527. if (p_idx < nodes.size()) {
  1528. // Find in built-in nodes.
  1529. for (int i = 0; i < nodes[p_idx].properties.size(); i++) {
  1530. uint32_t idx = nodes[p_idx].properties[i].name;
  1531. if (idx & FLAG_PATH_PROPERTY_IS_NODE) {
  1532. ret.push_back(names[idx & FLAG_PROP_NAME_MASK]);
  1533. }
  1534. }
  1535. return ret;
  1536. }
  1537. // Property not found, try on instance.
  1538. HashMap<int, int>::ConstIterator I = base_scene_node_remap.find(p_idx);
  1539. if (I) {
  1540. return get_base_scene_state()->get_node_deferred_nodepath_properties(I->value);
  1541. }
  1542. return ret;
  1543. }
  1544. Variant SceneState::get_node_property_value(int p_idx, int p_prop) const {
  1545. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Variant());
  1546. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), Variant());
  1547. return variants[nodes[p_idx].properties[p_prop].value];
  1548. }
  1549. NodePath SceneState::get_node_owner_path(int p_idx) const {
  1550. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1551. if (nodes[p_idx].owner < 0 || nodes[p_idx].owner == NO_PARENT_SAVED) {
  1552. return NodePath(); //root likely
  1553. }
  1554. if (nodes[p_idx].owner & FLAG_ID_IS_PATH) {
  1555. return node_paths[nodes[p_idx].owner & FLAG_MASK];
  1556. } else {
  1557. return get_node_path(nodes[p_idx].owner & FLAG_MASK);
  1558. }
  1559. }
  1560. int SceneState::get_connection_count() const {
  1561. return connections.size();
  1562. }
  1563. NodePath SceneState::get_connection_source(int p_idx) const {
  1564. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1565. if (connections[p_idx].from & FLAG_ID_IS_PATH) {
  1566. return node_paths[connections[p_idx].from & FLAG_MASK];
  1567. } else {
  1568. return get_node_path(connections[p_idx].from & FLAG_MASK);
  1569. }
  1570. }
  1571. StringName SceneState::get_connection_signal(int p_idx) const {
  1572. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1573. return names[connections[p_idx].signal];
  1574. }
  1575. NodePath SceneState::get_connection_target(int p_idx) const {
  1576. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1577. if (connections[p_idx].to & FLAG_ID_IS_PATH) {
  1578. return node_paths[connections[p_idx].to & FLAG_MASK];
  1579. } else {
  1580. return get_node_path(connections[p_idx].to & FLAG_MASK);
  1581. }
  1582. }
  1583. StringName SceneState::get_connection_method(int p_idx) const {
  1584. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1585. return names[connections[p_idx].method];
  1586. }
  1587. int SceneState::get_connection_flags(int p_idx) const {
  1588. ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
  1589. return connections[p_idx].flags;
  1590. }
  1591. int SceneState::get_connection_unbinds(int p_idx) const {
  1592. ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
  1593. return connections[p_idx].unbinds;
  1594. }
  1595. Array SceneState::get_connection_binds(int p_idx) const {
  1596. ERR_FAIL_INDEX_V(p_idx, connections.size(), Array());
  1597. Array binds;
  1598. for (int i = 0; i < connections[p_idx].binds.size(); i++) {
  1599. binds.push_back(variants[connections[p_idx].binds[i]]);
  1600. }
  1601. return binds;
  1602. }
  1603. bool SceneState::has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance) {
  1604. // this method cannot be const because of this
  1605. Ref<SceneState> ss = this;
  1606. do {
  1607. for (int i = 0; i < ss->connections.size(); i++) {
  1608. const ConnectionData &c = ss->connections[i];
  1609. NodePath np_from;
  1610. if (c.from & FLAG_ID_IS_PATH) {
  1611. np_from = ss->node_paths[c.from & FLAG_MASK];
  1612. } else {
  1613. np_from = ss->get_node_path(c.from);
  1614. }
  1615. NodePath np_to;
  1616. if (c.to & FLAG_ID_IS_PATH) {
  1617. np_to = ss->node_paths[c.to & FLAG_MASK];
  1618. } else {
  1619. np_to = ss->get_node_path(c.to);
  1620. }
  1621. StringName sn_signal = ss->names[c.signal];
  1622. StringName sn_method = ss->names[c.method];
  1623. if (np_from == p_node_from && sn_signal == p_signal && np_to == p_node_to && sn_method == p_method) {
  1624. return true;
  1625. }
  1626. }
  1627. if (p_no_inheritance) {
  1628. break;
  1629. }
  1630. ss = ss->get_base_scene_state();
  1631. } while (ss.is_valid());
  1632. return false;
  1633. }
  1634. Vector<NodePath> SceneState::get_editable_instances() const {
  1635. return editable_instances;
  1636. }
  1637. Ref<Resource> SceneState::get_sub_resource(const String &p_path) {
  1638. for (const Variant &v : variants) {
  1639. const Ref<Resource> &res = v;
  1640. if (res.is_valid() && res->get_path() == p_path) {
  1641. return res;
  1642. }
  1643. }
  1644. return Ref<Resource>();
  1645. }
  1646. //add
  1647. int SceneState::add_name(const StringName &p_name) {
  1648. names.push_back(p_name);
  1649. return names.size() - 1;
  1650. }
  1651. int SceneState::add_value(const Variant &p_value) {
  1652. variants.push_back(p_value);
  1653. return variants.size() - 1;
  1654. }
  1655. int SceneState::add_node_path(const NodePath &p_path) {
  1656. node_paths.push_back(p_path);
  1657. return (node_paths.size() - 1) | FLAG_ID_IS_PATH;
  1658. }
  1659. int SceneState::add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index) {
  1660. NodeData nd;
  1661. nd.parent = p_parent;
  1662. nd.owner = p_owner;
  1663. nd.type = p_type;
  1664. nd.name = p_name;
  1665. nd.instance = p_instance;
  1666. nd.index = p_index;
  1667. nodes.push_back(nd);
  1668. return nodes.size() - 1;
  1669. }
  1670. void SceneState::add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path) {
  1671. ERR_FAIL_INDEX(p_node, nodes.size());
  1672. ERR_FAIL_INDEX(p_name, names.size());
  1673. ERR_FAIL_INDEX(p_value, variants.size());
  1674. NodeData::Property prop;
  1675. prop.name = p_name;
  1676. if (p_deferred_node_path) {
  1677. prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
  1678. }
  1679. prop.value = p_value;
  1680. nodes.write[p_node].properties.push_back(prop);
  1681. }
  1682. void SceneState::add_node_group(int p_node, int p_group) {
  1683. ERR_FAIL_INDEX(p_node, nodes.size());
  1684. ERR_FAIL_INDEX(p_group, names.size());
  1685. nodes.write[p_node].groups.push_back(p_group);
  1686. }
  1687. void SceneState::set_base_scene(int p_idx) {
  1688. ERR_FAIL_INDEX(p_idx, variants.size());
  1689. base_scene_idx = p_idx;
  1690. }
  1691. 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) {
  1692. ERR_FAIL_INDEX(p_signal, names.size());
  1693. ERR_FAIL_INDEX(p_method, names.size());
  1694. for (int i = 0; i < p_binds.size(); i++) {
  1695. ERR_FAIL_INDEX(p_binds[i], variants.size());
  1696. }
  1697. ConnectionData c;
  1698. c.from = p_from;
  1699. c.to = p_to;
  1700. c.signal = p_signal;
  1701. c.method = p_method;
  1702. c.flags = p_flags;
  1703. c.unbinds = p_unbinds;
  1704. c.binds = p_binds;
  1705. connections.push_back(c);
  1706. }
  1707. void SceneState::add_editable_instance(const NodePath &p_path) {
  1708. editable_instances.push_back(p_path);
  1709. }
  1710. bool SceneState::remove_group_references(const StringName &p_name) {
  1711. bool edited = false;
  1712. for (NodeData &node : nodes) {
  1713. for (const int &group : node.groups) {
  1714. if (names[group] == p_name) {
  1715. node.groups.erase(group);
  1716. edited = true;
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. return edited;
  1722. }
  1723. bool SceneState::rename_group_references(const StringName &p_old_name, const StringName &p_new_name) {
  1724. bool edited = false;
  1725. for (const NodeData &node : nodes) {
  1726. for (const int &group : node.groups) {
  1727. if (names[group] == p_old_name) {
  1728. names.write[group] = p_new_name;
  1729. edited = true;
  1730. break;
  1731. }
  1732. }
  1733. }
  1734. return edited;
  1735. }
  1736. HashSet<StringName> SceneState::get_all_groups() {
  1737. HashSet<StringName> ret;
  1738. for (const NodeData &node : nodes) {
  1739. for (const int &group : node.groups) {
  1740. ret.insert(names[group]);
  1741. }
  1742. }
  1743. return ret;
  1744. }
  1745. Vector<String> SceneState::_get_node_groups(int p_idx) const {
  1746. Vector<StringName> groups = get_node_groups(p_idx);
  1747. Vector<String> ret;
  1748. for (int i = 0; i < groups.size(); i++) {
  1749. ret.push_back(groups[i]);
  1750. }
  1751. return ret;
  1752. }
  1753. void SceneState::_bind_methods() {
  1754. //unbuild API
  1755. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneState::get_node_count);
  1756. ClassDB::bind_method(D_METHOD("get_node_type", "idx"), &SceneState::get_node_type);
  1757. ClassDB::bind_method(D_METHOD("get_node_name", "idx"), &SceneState::get_node_name);
  1758. ClassDB::bind_method(D_METHOD("get_node_path", "idx", "for_parent"), &SceneState::get_node_path, DEFVAL(false));
  1759. ClassDB::bind_method(D_METHOD("get_node_owner_path", "idx"), &SceneState::get_node_owner_path);
  1760. ClassDB::bind_method(D_METHOD("is_node_instance_placeholder", "idx"), &SceneState::is_node_instance_placeholder);
  1761. ClassDB::bind_method(D_METHOD("get_node_instance_placeholder", "idx"), &SceneState::get_node_instance_placeholder);
  1762. ClassDB::bind_method(D_METHOD("get_node_instance", "idx"), &SceneState::get_node_instance);
  1763. ClassDB::bind_method(D_METHOD("get_node_groups", "idx"), &SceneState::_get_node_groups);
  1764. ClassDB::bind_method(D_METHOD("get_node_index", "idx"), &SceneState::get_node_index);
  1765. ClassDB::bind_method(D_METHOD("get_node_property_count", "idx"), &SceneState::get_node_property_count);
  1766. ClassDB::bind_method(D_METHOD("get_node_property_name", "idx", "prop_idx"), &SceneState::get_node_property_name);
  1767. ClassDB::bind_method(D_METHOD("get_node_property_value", "idx", "prop_idx"), &SceneState::get_node_property_value);
  1768. ClassDB::bind_method(D_METHOD("get_connection_count"), &SceneState::get_connection_count);
  1769. ClassDB::bind_method(D_METHOD("get_connection_source", "idx"), &SceneState::get_connection_source);
  1770. ClassDB::bind_method(D_METHOD("get_connection_signal", "idx"), &SceneState::get_connection_signal);
  1771. ClassDB::bind_method(D_METHOD("get_connection_target", "idx"), &SceneState::get_connection_target);
  1772. ClassDB::bind_method(D_METHOD("get_connection_method", "idx"), &SceneState::get_connection_method);
  1773. ClassDB::bind_method(D_METHOD("get_connection_flags", "idx"), &SceneState::get_connection_flags);
  1774. ClassDB::bind_method(D_METHOD("get_connection_binds", "idx"), &SceneState::get_connection_binds);
  1775. ClassDB::bind_method(D_METHOD("get_connection_unbinds", "idx"), &SceneState::get_connection_unbinds);
  1776. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1777. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1778. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1779. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1780. }
  1781. SceneState::SceneState() {
  1782. }
  1783. ////////////////
  1784. void PackedScene::_set_bundled_scene(const Dictionary &p_scene) {
  1785. state->set_bundled_scene(p_scene);
  1786. }
  1787. Dictionary PackedScene::_get_bundled_scene() const {
  1788. return state->get_bundled_scene();
  1789. }
  1790. Error PackedScene::pack(Node *p_scene) {
  1791. return state->pack(p_scene);
  1792. }
  1793. void PackedScene::clear() {
  1794. state->clear();
  1795. }
  1796. void PackedScene::reload_from_file() {
  1797. String path = get_path();
  1798. if (!path.is_resource_file()) {
  1799. return;
  1800. }
  1801. Ref<PackedScene> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  1802. if (s.is_null()) {
  1803. return;
  1804. }
  1805. // Backup the loaded_state
  1806. Ref<SceneState> loaded_state = s->get_state();
  1807. // This assigns a new state to s->state
  1808. // We do this because of the next step
  1809. s->recreate_state();
  1810. // This has a side-effect to clear s->state
  1811. copy_from(s);
  1812. // Then, we copy the backed-up loaded_state to state
  1813. state->copy_from(loaded_state);
  1814. }
  1815. bool PackedScene::can_instantiate() const {
  1816. return state->can_instantiate();
  1817. }
  1818. Node *PackedScene::instantiate(GenEditState p_edit_state) const {
  1819. #ifndef TOOLS_ENABLED
  1820. 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.");
  1821. #endif
  1822. Node *s = state->instantiate((SceneState::GenEditState)p_edit_state);
  1823. if (!s) {
  1824. return nullptr;
  1825. }
  1826. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  1827. s->set_scene_instance_state(state);
  1828. }
  1829. if (!is_built_in()) {
  1830. s->set_scene_file_path(get_path());
  1831. }
  1832. s->notification(Node::NOTIFICATION_SCENE_INSTANTIATED);
  1833. return s;
  1834. }
  1835. void PackedScene::replace_state(Ref<SceneState> p_by) {
  1836. state = p_by;
  1837. state->set_path(get_path());
  1838. #ifdef TOOLS_ENABLED
  1839. state->set_last_modified_time(get_last_modified_time());
  1840. #endif
  1841. }
  1842. void PackedScene::recreate_state() {
  1843. state.instantiate();
  1844. state->set_path(get_path());
  1845. #ifdef TOOLS_ENABLED
  1846. state->set_last_modified_time(get_last_modified_time());
  1847. #endif
  1848. }
  1849. #ifdef TOOLS_ENABLED
  1850. HashSet<StringName> PackedScene::get_scene_groups(const String &p_path) {
  1851. {
  1852. Ref<PackedScene> packed_scene = ResourceCache::get_ref(p_path);
  1853. if (packed_scene.is_valid()) {
  1854. return packed_scene->get_state()->get_all_groups();
  1855. }
  1856. }
  1857. if (p_path.get_extension() == "tscn") {
  1858. Ref<FileAccess> scene_file = FileAccess::open(p_path, FileAccess::READ);
  1859. ERR_FAIL_COND_V(scene_file.is_null(), HashSet<StringName>());
  1860. HashSet<StringName> ret;
  1861. while (!scene_file->eof_reached()) {
  1862. const String line = scene_file->get_line();
  1863. if (!line.begins_with("[node")) {
  1864. continue;
  1865. }
  1866. int i = line.find("groups=[");
  1867. if (i == -1) {
  1868. continue;
  1869. }
  1870. int j = line.find_char(']', i);
  1871. while (i < j) {
  1872. i = line.find_char('"', i);
  1873. if (i == -1) {
  1874. break;
  1875. }
  1876. int k = line.find_char('"', i + 1);
  1877. if (k == -1) {
  1878. break;
  1879. }
  1880. ret.insert(line.substr(i + 1, k - i - 1));
  1881. i = k + 1;
  1882. }
  1883. }
  1884. return ret;
  1885. } else {
  1886. Ref<PackedScene> packed_scene = ResourceLoader::load(p_path);
  1887. ERR_FAIL_COND_V(packed_scene.is_null(), HashSet<StringName>());
  1888. return packed_scene->get_state()->get_all_groups();
  1889. }
  1890. }
  1891. #endif
  1892. Ref<SceneState> PackedScene::get_state() const {
  1893. return state;
  1894. }
  1895. void PackedScene::set_path(const String &p_path, bool p_take_over) {
  1896. state->set_path(p_path);
  1897. Resource::set_path(p_path, p_take_over);
  1898. }
  1899. void PackedScene::set_path_cache(const String &p_path) {
  1900. state->set_path(p_path);
  1901. Resource::set_path_cache(p_path);
  1902. }
  1903. void PackedScene::reset_state() {
  1904. clear();
  1905. }
  1906. void PackedScene::_bind_methods() {
  1907. ClassDB::bind_method(D_METHOD("pack", "path"), &PackedScene::pack);
  1908. ClassDB::bind_method(D_METHOD("instantiate", "edit_state"), &PackedScene::instantiate, DEFVAL(GEN_EDIT_STATE_DISABLED));
  1909. ClassDB::bind_method(D_METHOD("can_instantiate"), &PackedScene::can_instantiate);
  1910. ClassDB::bind_method(D_METHOD("_set_bundled_scene", "scene"), &PackedScene::_set_bundled_scene);
  1911. ClassDB::bind_method(D_METHOD("_get_bundled_scene"), &PackedScene::_get_bundled_scene);
  1912. ClassDB::bind_method(D_METHOD("get_state"), &PackedScene::get_state);
  1913. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "_set_bundled_scene", "_get_bundled_scene");
  1914. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1915. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1916. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1917. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1918. }
  1919. PackedScene::PackedScene() {
  1920. state.instantiate();
  1921. }