packed_scene.cpp 73 KB

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