scene_format_text.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*************************************************************************/
  2. /* scene_format_text.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "scene_format_text.h"
  31. #include "globals.h"
  32. #include "os/dir_access.h"
  33. #include "version.h"
  34. #define FORMAT_VERSION 1
  35. #include "os/dir_access.h"
  36. #include "version.h"
  37. #define _printerr() ERR_PRINT(String(res_path + ":" + itos(lines) + " - Parse Error: " + error_text).utf8().get_data());
  38. ///
  39. void ResourceInteractiveLoaderText::set_local_path(const String &p_local_path) {
  40. res_path = p_local_path;
  41. }
  42. Ref<Resource> ResourceInteractiveLoaderText::get_resource() {
  43. return resource;
  44. }
  45. Error ResourceInteractiveLoaderText::_parse_sub_resource(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) {
  46. VariantParser::Token token;
  47. VariantParser::get_token(p_stream, token, line, r_err_str);
  48. if (token.type != VariantParser::TK_NUMBER) {
  49. r_err_str = "Expected number (sub-resource index)";
  50. return ERR_PARSE_ERROR;
  51. }
  52. int index = token.value;
  53. String path = local_path + "::" + itos(index);
  54. if (!ignore_resource_parsing) {
  55. if (!ResourceCache::has(path)) {
  56. r_err_str = "Can't load cached sub-resource: " + path;
  57. return ERR_PARSE_ERROR;
  58. }
  59. r_value_res = RES(ResourceCache::get(path));
  60. } else {
  61. r_value_res = RES();
  62. }
  63. VariantParser::get_token(p_stream, token, line, r_err_str);
  64. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  65. r_err_str = "Expected ')'";
  66. return ERR_PARSE_ERROR;
  67. }
  68. return OK;
  69. }
  70. Error ResourceInteractiveLoaderText::_parse_ext_resource(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) {
  71. VariantParser::Token token;
  72. VariantParser::get_token(p_stream, token, line, r_err_str);
  73. if (token.type != VariantParser::TK_NUMBER) {
  74. r_err_str = "Expected number (sub-resource index)";
  75. return ERR_PARSE_ERROR;
  76. }
  77. int id = token.value;
  78. if (!ignore_resource_parsing) {
  79. if (!ext_resources.has(id)) {
  80. r_err_str = "Can't load cached ext-resource #" + itos(id);
  81. return ERR_PARSE_ERROR;
  82. }
  83. String path = ext_resources[id].path;
  84. String type = ext_resources[id].type;
  85. if (path.find("://") == -1 && path.is_rel_path()) {
  86. // path is relative to file being loaded, so convert to a resource path
  87. path = Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  88. }
  89. RES res = ResourceLoader::load(path, type);
  90. if (res.is_null()) {
  91. WARN_PRINT(String("Couldn't load external resource: " + path).utf8().get_data());
  92. }
  93. r_value_res = res;
  94. } else {
  95. r_value_res = RES();
  96. }
  97. VariantParser::get_token(p_stream, token, line, r_err_str);
  98. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  99. r_err_str = "Expected ')'";
  100. return ERR_PARSE_ERROR;
  101. }
  102. return OK;
  103. }
  104. Error ResourceInteractiveLoaderText::_parse_sub_resource_export(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) {
  105. VariantParser::Token token;
  106. VariantParser::get_token(p_stream, token, line, r_err_str);
  107. if (token.type != VariantParser::TK_NUMBER) {
  108. r_err_str = "Expected number (sub-resource index)";
  109. return ERR_PARSE_ERROR;
  110. }
  111. int index = token.value;
  112. r_value_res = "@RESLOCAL:" + itos(index);
  113. VariantParser::get_token(p_stream, token, line, r_err_str);
  114. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  115. r_err_str = "Expected ')'";
  116. return ERR_PARSE_ERROR;
  117. }
  118. return OK;
  119. }
  120. Error ResourceInteractiveLoaderText::_parse_ext_resource_export(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) {
  121. VariantParser::Token token;
  122. VariantParser::get_token(p_stream, token, line, r_err_str);
  123. if (token.type != VariantParser::TK_NUMBER) {
  124. r_err_str = "Expected number (sub-resource index)";
  125. return ERR_PARSE_ERROR;
  126. }
  127. int id = token.value;
  128. {
  129. r_value_res = "@RESEXTERNAL:" + itos(id);
  130. }
  131. VariantParser::get_token(p_stream, token, line, r_err_str);
  132. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  133. r_err_str = "Expected ')'";
  134. return ERR_PARSE_ERROR;
  135. }
  136. return OK;
  137. }
  138. Error ResourceInteractiveLoaderText::poll() {
  139. if (error != OK)
  140. return error;
  141. if (next_tag.name == "ext_resource") {
  142. if (!next_tag.fields.has("path")) {
  143. error = ERR_FILE_CORRUPT;
  144. error_text = "Missing 'path' in external resource tag";
  145. _printerr();
  146. return error;
  147. }
  148. if (!next_tag.fields.has("type")) {
  149. error = ERR_FILE_CORRUPT;
  150. error_text = "Missing 'type' in external resource tag";
  151. _printerr();
  152. return error;
  153. }
  154. if (!next_tag.fields.has("id")) {
  155. error = ERR_FILE_CORRUPT;
  156. error_text = "Missing 'id' in external resource tag";
  157. _printerr();
  158. return error;
  159. }
  160. String path = next_tag.fields["path"];
  161. String type = next_tag.fields["type"];
  162. int index = next_tag.fields["id"];
  163. if (path.find("://") == -1 && path.is_rel_path()) {
  164. // path is relative to file being loaded, so convert to a resource path
  165. path = Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  166. }
  167. if (remaps.has(path)) {
  168. path = remaps[path];
  169. }
  170. RES res = ResourceLoader::load(path, type);
  171. if (res.is_null()) {
  172. if (ResourceLoader::get_abort_on_missing_resources()) {
  173. error = ERR_FILE_CORRUPT;
  174. error_text = "[ext_resource] referenced nonexistent resource at: " + path;
  175. _printerr();
  176. return error;
  177. } else {
  178. ResourceLoader::notify_dependency_error(local_path, path, type);
  179. }
  180. } else {
  181. resource_cache.push_back(res);
  182. }
  183. ExtResource er;
  184. er.path = path;
  185. er.type = type;
  186. ext_resources[index] = er;
  187. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  188. if (error) {
  189. _printerr();
  190. }
  191. resource_current++;
  192. return error;
  193. } else if (next_tag.name == "sub_resource") {
  194. if (!next_tag.fields.has("type")) {
  195. error = ERR_FILE_CORRUPT;
  196. error_text = "Missing 'type' in external resource tag";
  197. _printerr();
  198. return error;
  199. }
  200. if (!next_tag.fields.has("id")) {
  201. error = ERR_FILE_CORRUPT;
  202. error_text = "Missing 'index' in external resource tag";
  203. _printerr();
  204. return error;
  205. }
  206. String type = next_tag.fields["type"];
  207. int id = next_tag.fields["id"];
  208. String path = local_path + "::" + itos(id);
  209. //bool exists=ResourceCache::has(path);
  210. Ref<Resource> res;
  211. if (!ResourceCache::has(path)) { //only if it doesn't exist
  212. Object *obj = ObjectTypeDB::instance(type);
  213. if (!obj) {
  214. error_text += "Can't create sub resource of type: " + type;
  215. _printerr();
  216. error = ERR_FILE_CORRUPT;
  217. return error;
  218. }
  219. Resource *r = obj->cast_to<Resource>();
  220. if (!r) {
  221. error_text += "Can't create sub resource of type, because not a resource: " + type;
  222. _printerr();
  223. error = ERR_FILE_CORRUPT;
  224. return error;
  225. }
  226. res = Ref<Resource>(r);
  227. resource_cache.push_back(res);
  228. res->set_path(path);
  229. }
  230. resource_current++;
  231. while (true) {
  232. String assign;
  233. Variant value;
  234. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  235. if (error) {
  236. _printerr();
  237. return error;
  238. }
  239. if (assign != String()) {
  240. if (res.is_valid()) {
  241. res->set(assign, value);
  242. }
  243. //it's assignment
  244. } else if (next_tag.name != String()) {
  245. error = OK;
  246. break;
  247. } else {
  248. error = ERR_FILE_CORRUPT;
  249. error_text = "Premature end of file while parsing [sub_resource]";
  250. _printerr();
  251. return error;
  252. }
  253. }
  254. return OK;
  255. } else if (next_tag.name == "resource") {
  256. if (is_scene) {
  257. error_text += "found the 'resource' tag on a scene file!";
  258. _printerr();
  259. error = ERR_FILE_CORRUPT;
  260. return error;
  261. }
  262. Object *obj = ObjectTypeDB::instance(res_type);
  263. if (!obj) {
  264. error_text += "Can't create sub resource of type: " + res_type;
  265. _printerr();
  266. error = ERR_FILE_CORRUPT;
  267. return error;
  268. }
  269. Resource *r = obj->cast_to<Resource>();
  270. if (!r) {
  271. error_text += "Can't create sub resource of type, because not a resource: " + res_type;
  272. _printerr();
  273. error = ERR_FILE_CORRUPT;
  274. return error;
  275. }
  276. resource = Ref<Resource>(r);
  277. resource_current++;
  278. while (true) {
  279. String assign;
  280. Variant value;
  281. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  282. if (error) {
  283. if (error != ERR_FILE_EOF) {
  284. _printerr();
  285. } else {
  286. if (!ResourceCache::has(res_path)) {
  287. resource->set_path(res_path);
  288. }
  289. }
  290. return error;
  291. }
  292. if (assign != String()) {
  293. resource->set(assign, value);
  294. //it's assignment
  295. } else if (next_tag.name != String()) {
  296. error = ERR_FILE_CORRUPT;
  297. error_text = "Extra tag found when parsing main resource file";
  298. _printerr();
  299. return error;
  300. } else {
  301. error = ERR_FILE_EOF;
  302. return error;
  303. }
  304. }
  305. return OK;
  306. } else if (next_tag.name == "node") {
  307. if (!is_scene) {
  308. error_text += "found the 'node' tag on a resource file!";
  309. _printerr();
  310. error = ERR_FILE_CORRUPT;
  311. return error;
  312. }
  313. /*
  314. int add_name(const StringName& p_name);
  315. int add_value(const Variant& p_value);
  316. int add_node_path(const NodePath& p_path);
  317. int add_node(int p_parent,int p_owner,int p_type,int p_name, int p_instance);
  318. void add_node_property(int p_node,int p_name,int p_value);
  319. void add_node_group(int p_node,int p_group);
  320. void set_base_scene(int p_idx);
  321. void add_connection(int p_from,int p_to, int p_signal, int p_method, int p_flags,const Vector<int>& p_binds);
  322. void add_editable_instance(const NodePath& p_path);
  323. */
  324. int parent = -1;
  325. int owner = -1;
  326. int type = -1;
  327. int name = -1;
  328. int instance = -1;
  329. // int base_scene=-1;
  330. if (next_tag.fields.has("name")) {
  331. name = packed_scene->get_state()->add_name(next_tag.fields["name"]);
  332. }
  333. if (next_tag.fields.has("parent")) {
  334. NodePath np = next_tag.fields["parent"];
  335. np.prepend_period(); //compatible to how it manages paths internally
  336. parent = packed_scene->get_state()->add_node_path(np);
  337. }
  338. if (next_tag.fields.has("type")) {
  339. type = packed_scene->get_state()->add_name(next_tag.fields["type"]);
  340. } else {
  341. type = SceneState::TYPE_INSTANCED; //no type? assume this was instanced
  342. }
  343. if (next_tag.fields.has("instance")) {
  344. instance = packed_scene->get_state()->add_value(next_tag.fields["instance"]);
  345. if (packed_scene->get_state()->get_node_count() == 0 && parent == -1) {
  346. packed_scene->get_state()->set_base_scene(instance);
  347. instance = -1;
  348. }
  349. }
  350. if (next_tag.fields.has("instance_placeholder")) {
  351. String path = next_tag.fields["instance_placeholder"];
  352. int path_v = packed_scene->get_state()->add_value(path);
  353. if (packed_scene->get_state()->get_node_count() == 0) {
  354. error = ERR_FILE_CORRUPT;
  355. error_text = "Instance Placeholder can't be used for inheritance.";
  356. _printerr();
  357. return error;
  358. }
  359. instance = path_v | SceneState::FLAG_INSTANCE_IS_PLACEHOLDER;
  360. }
  361. if (next_tag.fields.has("owner")) {
  362. owner = packed_scene->get_state()->add_node_path(next_tag.fields["owner"]);
  363. } else {
  364. if (parent != -1 && !(type == SceneState::TYPE_INSTANCED && instance == -1))
  365. owner = 0; //if no owner, owner is root
  366. }
  367. int node_id = packed_scene->get_state()->add_node(parent, owner, type, name, instance);
  368. if (next_tag.fields.has("groups")) {
  369. Array groups = next_tag.fields["groups"];
  370. for (int i = 0; i < groups.size(); i++) {
  371. packed_scene->get_state()->add_node_group(node_id, packed_scene->get_state()->add_name(groups[i]));
  372. }
  373. }
  374. while (true) {
  375. String assign;
  376. Variant value;
  377. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  378. if (error) {
  379. if (error != ERR_FILE_EOF) {
  380. _printerr();
  381. } else {
  382. resource = packed_scene;
  383. if (!ResourceCache::has(res_path)) {
  384. packed_scene->set_path(res_path);
  385. }
  386. }
  387. return error;
  388. }
  389. if (assign != String()) {
  390. int nameidx = packed_scene->get_state()->add_name(assign);
  391. int valueidx = packed_scene->get_state()->add_value(value);
  392. packed_scene->get_state()->add_node_property(node_id, nameidx, valueidx);
  393. //it's assignment
  394. } else if (next_tag.name != String()) {
  395. error = OK;
  396. return error;
  397. } else {
  398. resource = packed_scene;
  399. error = ERR_FILE_EOF;
  400. return error;
  401. }
  402. }
  403. return OK;
  404. } else if (next_tag.name == "connection") {
  405. if (!is_scene) {
  406. error_text += "found the 'connection' tag on a resource file!";
  407. _printerr();
  408. error = ERR_FILE_CORRUPT;
  409. return error;
  410. }
  411. if (!next_tag.fields.has("from")) {
  412. error = ERR_FILE_CORRUPT;
  413. error_text = "missing 'from' field fron connection tag";
  414. return error;
  415. }
  416. if (!next_tag.fields.has("to")) {
  417. error = ERR_FILE_CORRUPT;
  418. error_text = "missing 'to' field fron connection tag";
  419. return error;
  420. }
  421. if (!next_tag.fields.has("signal")) {
  422. error = ERR_FILE_CORRUPT;
  423. error_text = "missing 'signal' field fron connection tag";
  424. return error;
  425. }
  426. if (!next_tag.fields.has("method")) {
  427. error = ERR_FILE_CORRUPT;
  428. error_text = "missing 'method' field fron connection tag";
  429. return error;
  430. }
  431. NodePath from = next_tag.fields["from"];
  432. NodePath to = next_tag.fields["to"];
  433. StringName method = next_tag.fields["method"];
  434. StringName signal = next_tag.fields["signal"];
  435. int flags = CONNECT_PERSIST;
  436. Array binds;
  437. if (next_tag.fields.has("flags")) {
  438. flags = next_tag.fields["flags"];
  439. }
  440. if (next_tag.fields.has("binds")) {
  441. binds = next_tag.fields["binds"];
  442. }
  443. Vector<int> bind_ints;
  444. for (int i = 0; i < binds.size(); i++) {
  445. bind_ints.push_back(packed_scene->get_state()->add_value(binds[i]));
  446. }
  447. packed_scene->get_state()->add_connection(
  448. packed_scene->get_state()->add_node_path(from.simplified()),
  449. packed_scene->get_state()->add_node_path(to.simplified()),
  450. packed_scene->get_state()->add_name(signal),
  451. packed_scene->get_state()->add_name(method),
  452. flags,
  453. bind_ints);
  454. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  455. if (error) {
  456. if (error != ERR_FILE_EOF) {
  457. _printerr();
  458. } else {
  459. resource = packed_scene;
  460. }
  461. }
  462. return error;
  463. } else if (next_tag.name == "editable") {
  464. if (!is_scene) {
  465. error_text += "found the 'editable' tag on a resource file!";
  466. _printerr();
  467. error = ERR_FILE_CORRUPT;
  468. return error;
  469. }
  470. if (!next_tag.fields.has("path")) {
  471. error = ERR_FILE_CORRUPT;
  472. error_text = "missing 'path' field fron connection tag";
  473. _printerr();
  474. return error;
  475. }
  476. NodePath path = next_tag.fields["path"];
  477. packed_scene->get_state()->add_editable_instance(path.simplified());
  478. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  479. if (error) {
  480. if (error != ERR_FILE_EOF) {
  481. _printerr();
  482. } else {
  483. resource = packed_scene;
  484. }
  485. }
  486. return error;
  487. } else {
  488. error_text += "Unknown tag in file: " + next_tag.name;
  489. _printerr();
  490. error = ERR_FILE_CORRUPT;
  491. return error;
  492. }
  493. return OK;
  494. }
  495. int ResourceInteractiveLoaderText::get_stage() const {
  496. return resource_current;
  497. }
  498. int ResourceInteractiveLoaderText::get_stage_count() const {
  499. return resources_total; //+ext_resources;
  500. }
  501. ResourceInteractiveLoaderText::~ResourceInteractiveLoaderText() {
  502. memdelete(f);
  503. }
  504. void ResourceInteractiveLoaderText::get_dependencies(FileAccess *f, List<String> *p_dependencies, bool p_add_types) {
  505. open(f);
  506. ignore_resource_parsing = true;
  507. ERR_FAIL_COND(error != OK);
  508. while (next_tag.name == "ext_resource") {
  509. if (!next_tag.fields.has("type")) {
  510. error = ERR_FILE_CORRUPT;
  511. error_text = "Missing 'type' in external resource tag";
  512. _printerr();
  513. return;
  514. }
  515. if (!next_tag.fields.has("id")) {
  516. error = ERR_FILE_CORRUPT;
  517. error_text = "Missing 'index' in external resource tag";
  518. _printerr();
  519. return;
  520. }
  521. String path = next_tag.fields["path"];
  522. String type = next_tag.fields["type"];
  523. if (path.find("://") == -1 && path.is_rel_path()) {
  524. // path is relative to file being loaded, so convert to a resource path
  525. path = Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  526. }
  527. if (p_add_types) {
  528. path += "::" + type;
  529. }
  530. p_dependencies->push_back(path);
  531. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  532. if (err) {
  533. print_line(error_text + " - " + itos(lines));
  534. error_text = "Unexpected end of file";
  535. _printerr();
  536. error = ERR_FILE_CORRUPT;
  537. }
  538. }
  539. }
  540. Error ResourceInteractiveLoaderText::get_export_data(FileAccess *p_f, ExportData &r_export_data) {
  541. open(p_f);
  542. ERR_FAIL_COND_V(error != OK, error);
  543. while (true) {
  544. if (next_tag.name == "ext_resource") {
  545. if (!next_tag.fields.has("path")) {
  546. error = ERR_FILE_CORRUPT;
  547. error_text = "Missing 'path' in external resource tag";
  548. _printerr();
  549. return error;
  550. }
  551. if (!next_tag.fields.has("type")) {
  552. error = ERR_FILE_CORRUPT;
  553. error_text = "Missing 'type' in external resource tag";
  554. _printerr();
  555. return error;
  556. }
  557. if (!next_tag.fields.has("id")) {
  558. error = ERR_FILE_CORRUPT;
  559. error_text = "Missing 'id' in external resource tag";
  560. _printerr();
  561. return error;
  562. }
  563. String path = next_tag.fields["path"];
  564. String type = next_tag.fields["type"];
  565. int index = next_tag.fields["id"];
  566. ExportData::Dependency dep;
  567. dep.path = path;
  568. dep.type = type;
  569. r_export_data.dependencies[index] = dep;
  570. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_export);
  571. if (error) {
  572. _printerr();
  573. return error;
  574. }
  575. } else if (next_tag.name == "sub_resource") {
  576. if (!next_tag.fields.has("type")) {
  577. error = ERR_FILE_CORRUPT;
  578. error_text = "Missing 'type' in external resource tag";
  579. _printerr();
  580. return error;
  581. }
  582. if (!next_tag.fields.has("id")) {
  583. error = ERR_FILE_CORRUPT;
  584. error_text = "Missing 'index' in external resource tag";
  585. _printerr();
  586. return error;
  587. }
  588. String type = next_tag.fields["type"];
  589. int id = next_tag.fields["id"];
  590. r_export_data.resources.resize(r_export_data.resources.size() + 1);
  591. ExportData::ResourceData &res_data = r_export_data.resources[r_export_data.resources.size() - 1];
  592. res_data.type = type;
  593. res_data.index = id;
  594. while (true) {
  595. String assign;
  596. Variant value;
  597. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_export);
  598. if (error) {
  599. _printerr();
  600. return error;
  601. }
  602. if (assign != String()) {
  603. ExportData::PropertyData pdata;
  604. pdata.name = assign;
  605. pdata.value = value;
  606. res_data.properties.push_back(pdata);
  607. //it's assignment
  608. } else if (next_tag.name != String()) {
  609. error = OK;
  610. break;
  611. } else {
  612. error = ERR_FILE_CORRUPT;
  613. error_text = "Premature end of file while parsing [sub_resource]";
  614. _printerr();
  615. return error;
  616. }
  617. }
  618. } else if (next_tag.name == "resource") {
  619. if (is_scene) {
  620. error_text += "found the 'resource' tag on a scene file!";
  621. _printerr();
  622. error = ERR_FILE_CORRUPT;
  623. return error;
  624. }
  625. r_export_data.resources.resize(r_export_data.resources.size() + 1);
  626. ExportData::ResourceData &res_data = r_export_data.resources[r_export_data.resources.size() - 1];
  627. res_data.index = -1;
  628. res_data.type = res_type;
  629. while (true) {
  630. String assign;
  631. Variant value;
  632. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_export);
  633. if (error) {
  634. if (error != ERR_FILE_EOF) {
  635. _printerr();
  636. } else {
  637. return OK;
  638. }
  639. return error;
  640. }
  641. if (assign != String()) {
  642. ExportData::PropertyData pdata;
  643. pdata.name = assign;
  644. pdata.value = value;
  645. res_data.properties.push_back(pdata);
  646. //it's assignment
  647. } else if (next_tag.name != String()) {
  648. error = ERR_FILE_CORRUPT;
  649. error_text = "Extra tag found when parsing main resource file";
  650. _printerr();
  651. return error;
  652. } else {
  653. return OK;
  654. }
  655. }
  656. return OK;
  657. } else if (next_tag.name == "node") {
  658. if (!is_scene) {
  659. error_text += "found the 'node' tag on a resource file!";
  660. _printerr();
  661. error = ERR_FILE_CORRUPT;
  662. return error;
  663. }
  664. r_export_data.nodes.resize(r_export_data.nodes.size() + 1);
  665. ExportData::NodeData &node_data = r_export_data.nodes[r_export_data.nodes.size() - 1];
  666. if (next_tag.fields.has("name")) {
  667. node_data.name = next_tag.fields["name"];
  668. }
  669. if (next_tag.fields.has("parent")) {
  670. node_data.parent = next_tag.fields["parent"];
  671. }
  672. if (next_tag.fields.has("type")) {
  673. node_data.type = next_tag.fields["type"];
  674. }
  675. if (next_tag.fields.has("instance")) {
  676. node_data.instance = next_tag.fields["instance"];
  677. }
  678. if (next_tag.fields.has("instance_placeholder")) {
  679. node_data.instance_placeholder = next_tag.fields["instance_placeholder"];
  680. }
  681. if (next_tag.fields.has("owner")) {
  682. node_data.owner = next_tag.fields["owner"];
  683. }
  684. if (next_tag.fields.has("groups")) {
  685. Array groups = next_tag.fields["groups"];
  686. for (int i = 0; i < groups.size(); i++) {
  687. node_data.groups.push_back(groups[i]);
  688. }
  689. }
  690. while (true) {
  691. String assign;
  692. Variant value;
  693. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_export);
  694. if (error) {
  695. if (error != ERR_FILE_EOF) {
  696. _printerr();
  697. return error;
  698. } else {
  699. return OK;
  700. }
  701. }
  702. if (assign != String()) {
  703. ExportData::PropertyData pdata;
  704. pdata.name = assign;
  705. pdata.value = value;
  706. node_data.properties.push_back(pdata);
  707. //it's assignment
  708. } else if (next_tag.name != String()) {
  709. break; //something else
  710. } else {
  711. //all done?
  712. return OK;
  713. }
  714. }
  715. } else if (next_tag.name == "connection") {
  716. if (!is_scene) {
  717. error_text += "found the 'connection' tag on a resource file!";
  718. _printerr();
  719. error = ERR_FILE_CORRUPT;
  720. return error;
  721. }
  722. if (!next_tag.fields.has("from")) {
  723. error = ERR_FILE_CORRUPT;
  724. error_text = "missing 'from' field fron connection tag";
  725. return error;
  726. }
  727. if (!next_tag.fields.has("to")) {
  728. error = ERR_FILE_CORRUPT;
  729. error_text = "missing 'to' field fron connection tag";
  730. return error;
  731. }
  732. if (!next_tag.fields.has("signal")) {
  733. error = ERR_FILE_CORRUPT;
  734. error_text = "missing 'signal' field fron connection tag";
  735. return error;
  736. }
  737. if (!next_tag.fields.has("method")) {
  738. error = ERR_FILE_CORRUPT;
  739. error_text = "missing 'method' field fron connection tag";
  740. return error;
  741. }
  742. NodePath from = next_tag.fields["from"];
  743. NodePath to = next_tag.fields["to"];
  744. StringName method = next_tag.fields["method"];
  745. StringName signal = next_tag.fields["signal"];
  746. int flags = CONNECT_PERSIST;
  747. Array binds;
  748. if (next_tag.fields.has("flags")) {
  749. flags = next_tag.fields["flags"];
  750. }
  751. if (next_tag.fields.has("binds")) {
  752. binds = next_tag.fields["binds"];
  753. }
  754. ExportData::Connection conn;
  755. conn.from = from;
  756. conn.to = to;
  757. conn.method = method;
  758. conn.signal = signal;
  759. conn.binds = binds;
  760. conn.flags = flags;
  761. r_export_data.connections.push_back(conn);
  762. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_export);
  763. if (error) {
  764. if (error != ERR_FILE_EOF) {
  765. _printerr();
  766. return error;
  767. } else {
  768. return OK;
  769. }
  770. }
  771. continue;
  772. } else if (next_tag.name == "editable") {
  773. if (!is_scene) {
  774. error_text += "found the 'editable' tag on a resource file!";
  775. _printerr();
  776. error = ERR_FILE_CORRUPT;
  777. return error;
  778. }
  779. if (!next_tag.fields.has("path")) {
  780. error = ERR_FILE_CORRUPT;
  781. error_text = "missing 'path' field fron connection tag";
  782. _printerr();
  783. return error;
  784. }
  785. NodePath path = next_tag.fields["path"];
  786. r_export_data.editables.push_back(path);
  787. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_export);
  788. if (error) {
  789. if (error != ERR_FILE_EOF) {
  790. _printerr();
  791. return error;
  792. } else {
  793. return OK;
  794. }
  795. }
  796. continue;
  797. } else {
  798. error_text += "Unknown tag in file: " + next_tag.name;
  799. _printerr();
  800. error = ERR_FILE_CORRUPT;
  801. return error;
  802. }
  803. }
  804. ERR_FAIL_V(ERR_BUG);
  805. }
  806. Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map) {
  807. open(p_f, true);
  808. ERR_FAIL_COND_V(error != OK, error);
  809. ignore_resource_parsing = true;
  810. //FileAccess
  811. FileAccess *fw = NULL;
  812. String base_path = local_path.get_base_dir();
  813. uint64_t tag_end = f->get_pos();
  814. while (true) {
  815. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  816. if (err != OK) {
  817. if (fw) {
  818. memdelete(fw);
  819. }
  820. error = ERR_FILE_CORRUPT;
  821. ERR_FAIL_V(error);
  822. }
  823. if (next_tag.name != "ext_resource") {
  824. //nothing was done
  825. if (!fw)
  826. return OK;
  827. break;
  828. } else {
  829. if (!fw) {
  830. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  831. if (is_scene) {
  832. fw->store_line("[gd_scene load_steps=" + itos(resources_total) + " format=" + itos(FORMAT_VERSION) + "]\n");
  833. } else {
  834. fw->store_line("[gd_resource type=\"" + res_type + "\" load_steps=" + itos(resources_total) + " format=" + itos(FORMAT_VERSION) + "]\n");
  835. }
  836. }
  837. if (!next_tag.fields.has("path") || !next_tag.fields.has("id") || !next_tag.fields.has("type")) {
  838. memdelete(fw);
  839. error = ERR_FILE_CORRUPT;
  840. ERR_FAIL_V(error);
  841. }
  842. String path = next_tag.fields["path"];
  843. int index = next_tag.fields["id"];
  844. String type = next_tag.fields["type"];
  845. bool relative = false;
  846. if (!path.begins_with("res://")) {
  847. path = base_path.plus_file(path).simplify_path();
  848. relative = true;
  849. }
  850. if (p_map.has(path)) {
  851. String np = p_map[path];
  852. path = np;
  853. }
  854. if (relative) {
  855. //restore relative
  856. path = base_path.path_to_file(path);
  857. }
  858. fw->store_line("[ext_resource path=\"" + path + "\" type=\"" + type + "\" id=" + itos(index) + "]");
  859. tag_end = f->get_pos();
  860. }
  861. }
  862. f->seek(tag_end);
  863. uint8_t c = f->get_8();
  864. while (!f->eof_reached()) {
  865. fw->store_8(c);
  866. c = f->get_8();
  867. }
  868. f->close();
  869. bool all_ok = fw->get_error() == OK;
  870. memdelete(fw);
  871. if (!all_ok) {
  872. return ERR_CANT_CREATE;
  873. }
  874. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  875. da->remove(p_path);
  876. da->rename(p_path + ".depren", p_path);
  877. memdelete(da);
  878. return OK;
  879. }
  880. void ResourceInteractiveLoaderText::open(FileAccess *p_f, bool p_skip_first_tag) {
  881. error = OK;
  882. lines = 1;
  883. f = p_f;
  884. stream.f = f;
  885. is_scene = false;
  886. ignore_resource_parsing = false;
  887. resource_current = 0;
  888. VariantParser::Tag tag;
  889. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  890. if (err) {
  891. error = err;
  892. _printerr();
  893. return;
  894. }
  895. if (tag.fields.has("format")) {
  896. int fmt = tag.fields["format"];
  897. if (fmt > FORMAT_VERSION) {
  898. error_text = "Saved with newer format version";
  899. _printerr();
  900. error = ERR_PARSE_ERROR;
  901. return;
  902. }
  903. }
  904. if (tag.name == "gd_scene") {
  905. is_scene = true;
  906. packed_scene.instance();
  907. } else if (tag.name == "gd_resource") {
  908. if (!tag.fields.has("type")) {
  909. error_text = "Missing 'type' field in 'gd_resource' tag";
  910. _printerr();
  911. error = ERR_PARSE_ERROR;
  912. return;
  913. }
  914. res_type = tag.fields["type"];
  915. } else {
  916. error_text = "Unrecognized file type: " + tag.name;
  917. _printerr();
  918. error = ERR_PARSE_ERROR;
  919. return;
  920. }
  921. if (tag.fields.has("load_steps")) {
  922. resources_total = tag.fields["load_steps"];
  923. } else {
  924. resources_total = 0;
  925. }
  926. if (!p_skip_first_tag) {
  927. err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  928. if (err) {
  929. error_text = "Unexpected end of file";
  930. _printerr();
  931. error = ERR_FILE_CORRUPT;
  932. }
  933. }
  934. rp.ext_func = _parse_ext_resources;
  935. rp.sub_func = _parse_sub_resources;
  936. rp.func = NULL;
  937. rp.userdata = this;
  938. rp_export.ext_func = _parse_ext_resources_export;
  939. rp_export.sub_func = _parse_sub_resources_export;
  940. rp_export.func = NULL;
  941. rp_export.userdata = this;
  942. }
  943. String ResourceInteractiveLoaderText::recognize(FileAccess *p_f) {
  944. error = OK;
  945. lines = 1;
  946. f = p_f;
  947. stream.f = f;
  948. ignore_resource_parsing = true;
  949. VariantParser::Tag tag;
  950. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  951. if (err) {
  952. _printerr();
  953. return "";
  954. }
  955. if (tag.fields.has("format")) {
  956. int fmt = tag.fields["format"];
  957. if (fmt > FORMAT_VERSION) {
  958. error_text = "Saved with newer format version";
  959. _printerr();
  960. return "";
  961. }
  962. }
  963. if (tag.name == "gd_scene")
  964. return "PackedScene";
  965. if (tag.name != "gd_resource")
  966. return "";
  967. if (!tag.fields.has("type")) {
  968. error_text = "Missing 'type' field in 'gd_resource' tag";
  969. _printerr();
  970. return "";
  971. }
  972. return tag.fields["type"];
  973. }
  974. /////////////////////
  975. Ref<ResourceInteractiveLoader> ResourceFormatLoaderText::load_interactive(const String &p_path, Error *r_error) {
  976. if (r_error)
  977. *r_error = ERR_CANT_OPEN;
  978. Error err;
  979. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  980. if (err != OK) {
  981. ERR_FAIL_COND_V(err != OK, Ref<ResourceInteractiveLoader>());
  982. }
  983. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  984. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  985. ria->res_path = ria->local_path;
  986. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  987. ria->open(f);
  988. return ria;
  989. }
  990. void ResourceFormatLoaderText::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  991. if (p_type == "") {
  992. get_recognized_extensions(p_extensions);
  993. return;
  994. }
  995. if (p_type == "PackedScene")
  996. p_extensions->push_back("tscn");
  997. else
  998. p_extensions->push_back("tres");
  999. }
  1000. void ResourceFormatLoaderText::get_recognized_extensions(List<String> *p_extensions) const {
  1001. p_extensions->push_back("tscn");
  1002. p_extensions->push_back("tres");
  1003. }
  1004. bool ResourceFormatLoaderText::handles_type(const String &p_type) const {
  1005. return true;
  1006. }
  1007. String ResourceFormatLoaderText::get_resource_type(const String &p_path) const {
  1008. String ext = p_path.extension().to_lower();
  1009. if (ext == "tscn")
  1010. return "PackedScene";
  1011. else if (ext != "tres")
  1012. return String();
  1013. //for anyhting else must test..
  1014. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1015. if (!f) {
  1016. return ""; //could not rwead
  1017. }
  1018. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  1019. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1020. ria->res_path = ria->local_path;
  1021. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1022. String r = ria->recognize(f);
  1023. return r;
  1024. }
  1025. void ResourceFormatLoaderText::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  1026. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1027. if (!f) {
  1028. ERR_FAIL();
  1029. }
  1030. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  1031. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1032. ria->res_path = ria->local_path;
  1033. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1034. ria->get_dependencies(f, p_dependencies, p_add_types);
  1035. }
  1036. ResourceFormatLoaderText *ResourceFormatLoaderText::singleton = NULL;
  1037. Error ResourceFormatLoaderText::get_export_data(const String &p_path, ExportData &r_export_data) {
  1038. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1039. if (!f) {
  1040. ERR_FAIL_V(ERR_CANT_OPEN);
  1041. }
  1042. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  1043. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1044. ria->res_path = ria->local_path;
  1045. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1046. Error err = ria->get_export_data(f, r_export_data);
  1047. if (err != OK) {
  1048. ERR_PRINTS("ERROR: " + ria->error_text);
  1049. }
  1050. return err;
  1051. }
  1052. Error ResourceFormatLoaderText::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  1053. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1054. if (!f) {
  1055. ERR_FAIL_V(ERR_CANT_OPEN);
  1056. }
  1057. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  1058. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1059. ria->res_path = ria->local_path;
  1060. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1061. return ria->rename_dependencies(f, p_path, p_map);
  1062. }
  1063. /*****************************************************************************************************/
  1064. /*****************************************************************************************************/
  1065. /*****************************************************************************************************/
  1066. /*****************************************************************************************************/
  1067. /*****************************************************************************************************/
  1068. /*****************************************************************************************************/
  1069. /*****************************************************************************************************/
  1070. /*****************************************************************************************************/
  1071. /*****************************************************************************************************/
  1072. /*****************************************************************************************************/
  1073. String ResourceFormatSaverTextInstance::_write_resources(void *ud, const RES &p_resource) {
  1074. ResourceFormatSaverTextInstance *rsi = (ResourceFormatSaverTextInstance *)ud;
  1075. return rsi->_write_resource(p_resource);
  1076. }
  1077. String ResourceFormatSaverTextInstance::_write_resource(const RES &res) {
  1078. if (external_resources.has(res)) {
  1079. return "ExtResource( " + itos(external_resources[res] + 1) + " )";
  1080. } else {
  1081. if (internal_resources.has(res)) {
  1082. return "SubResource( " + itos(internal_resources[res]) + " )";
  1083. } else if (res->get_path().length() && res->get_path().find("::") == -1) {
  1084. //external resource
  1085. String path = relative_paths ? local_path.path_to_file(res->get_path()) : res->get_path();
  1086. return "Resource( \"" + path + "\" )";
  1087. } else {
  1088. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1089. ERR_FAIL_V("null");
  1090. //internal resource
  1091. }
  1092. }
  1093. return "null";
  1094. }
  1095. void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1096. switch (p_variant.get_type()) {
  1097. case Variant::OBJECT: {
  1098. RES res = p_variant.operator RefPtr();
  1099. if (res.is_null() || external_resources.has(res))
  1100. return;
  1101. if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
  1102. int index = external_resources.size();
  1103. external_resources[res] = index;
  1104. return;
  1105. }
  1106. if (resource_set.has(res))
  1107. return;
  1108. List<PropertyInfo> property_list;
  1109. res->get_property_list(&property_list);
  1110. property_list.sort();
  1111. List<PropertyInfo>::Element *I = property_list.front();
  1112. while (I) {
  1113. PropertyInfo pi = I->get();
  1114. if (pi.usage & PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage & PROPERTY_USAGE_BUNDLE)) {
  1115. Variant v = res->get(I->get().name);
  1116. _find_resources(v);
  1117. }
  1118. I = I->next();
  1119. }
  1120. resource_set.insert(res); //saved after, so the childs it needs are available when loaded
  1121. saved_resources.push_back(res);
  1122. } break;
  1123. case Variant::ARRAY: {
  1124. Array varray = p_variant;
  1125. int len = varray.size();
  1126. for (int i = 0; i < len; i++) {
  1127. Variant v = varray.get(i);
  1128. _find_resources(v);
  1129. }
  1130. } break;
  1131. case Variant::DICTIONARY: {
  1132. Dictionary d = p_variant;
  1133. List<Variant> keys;
  1134. d.get_key_list(&keys);
  1135. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1136. Variant v = d[E->get()];
  1137. _find_resources(v);
  1138. }
  1139. } break;
  1140. default: {
  1141. }
  1142. }
  1143. }
  1144. static String _valprop(const String &p_name) {
  1145. // Escape and quote strings with extended ASCII or further Unicode characters
  1146. // as well as '"', '=' or ' ' (32)
  1147. const CharType *cstr = p_name.c_str();
  1148. for (int i = 0; cstr[i]; i++) {
  1149. if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] < 33 || cstr[i] > 126) {
  1150. return "\"" + p_name.c_escape_multiline() + "\"";
  1151. }
  1152. }
  1153. // Keep as is
  1154. return p_name;
  1155. }
  1156. Error ResourceFormatSaverTextInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1157. if (p_path.ends_with(".tscn")) {
  1158. packed_scene = p_resource;
  1159. }
  1160. Error err;
  1161. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1162. ERR_FAIL_COND_V(err, ERR_CANT_OPEN);
  1163. FileAccessRef _fref(f);
  1164. local_path = Globals::get_singleton()->localize_path(p_path);
  1165. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1166. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1167. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1168. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1169. if (!p_path.begins_with("res://")) {
  1170. takeover_paths = false;
  1171. }
  1172. // save resources
  1173. _find_resources(p_resource, true);
  1174. if (packed_scene.is_valid()) {
  1175. //add instances to external resources if saving a packed scene
  1176. for (int i = 0; i < packed_scene->get_state()->get_node_count(); i++) {
  1177. if (packed_scene->get_state()->is_node_instance_placeholder(i))
  1178. continue;
  1179. Ref<PackedScene> instance = packed_scene->get_state()->get_node_instance(i);
  1180. if (instance.is_valid() && !external_resources.has(instance)) {
  1181. int index = external_resources.size();
  1182. external_resources[instance] = index;
  1183. }
  1184. }
  1185. }
  1186. ERR_FAIL_COND_V(err != OK, err);
  1187. {
  1188. String title = packed_scene.is_valid() ? "[gd_scene " : "[gd_resource ";
  1189. if (packed_scene.is_null())
  1190. title += "type=\"" + p_resource->get_type() + "\" ";
  1191. int load_steps = saved_resources.size() + external_resources.size();
  1192. //if (packed_scene.is_valid()) {
  1193. // load_steps+=packed_scene->get_node_count();
  1194. //}
  1195. //no, better to not use load steps from nodes, no point to that
  1196. if (load_steps > 1) {
  1197. title += "load_steps=" + itos(load_steps) + " ";
  1198. }
  1199. title += "format=" + itos(FORMAT_VERSION) + "";
  1200. //title+="engine_version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\"";
  1201. f->store_string(title);
  1202. f->store_line("]\n"); //one empty line
  1203. }
  1204. Vector<RES> sorted_er;
  1205. sorted_er.resize(external_resources.size());
  1206. for (Map<RES, int>::Element *E = external_resources.front(); E; E = E->next()) {
  1207. sorted_er[E->get()] = E->key();
  1208. }
  1209. for (int i = 0; i < sorted_er.size(); i++) {
  1210. String p = sorted_er[i]->get_path();
  1211. f->store_string("[ext_resource path=\"" + p + "\" type=\"" + sorted_er[i]->get_save_type() + "\" id=" + itos(i + 1) + "]\n"); //bundled
  1212. }
  1213. if (external_resources.size())
  1214. f->store_line(String()); //separate
  1215. Set<int> used_indices;
  1216. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1217. RES res = E->get();
  1218. if (E->next() && (res->get_path() == "" || res->get_path().find("::") != -1)) {
  1219. if (res->get_subindex() != 0) {
  1220. if (used_indices.has(res->get_subindex())) {
  1221. res->set_subindex(0); //repeated
  1222. } else {
  1223. used_indices.insert(res->get_subindex());
  1224. }
  1225. }
  1226. }
  1227. }
  1228. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1229. RES res = E->get();
  1230. ERR_CONTINUE(!resource_set.has(res));
  1231. bool main = (E->next() == NULL);
  1232. if (main && packed_scene.is_valid())
  1233. break; //save as a scene
  1234. if (main) {
  1235. f->store_line("[resource]\n");
  1236. } else {
  1237. String line = "[sub_resource ";
  1238. if (res->get_subindex() == 0) {
  1239. int new_subindex = 1;
  1240. if (used_indices.size()) {
  1241. new_subindex = used_indices.back()->get() + 1;
  1242. }
  1243. res->set_subindex(new_subindex);
  1244. used_indices.insert(new_subindex);
  1245. }
  1246. int idx = res->get_subindex();
  1247. line += "type=\"" + res->get_type() + "\" id=" + itos(idx);
  1248. f->store_line(line + "]\n");
  1249. if (takeover_paths) {
  1250. res->set_path(p_path + "::" + itos(idx), true);
  1251. }
  1252. internal_resources[res] = idx;
  1253. }
  1254. List<PropertyInfo> property_list;
  1255. res->get_property_list(&property_list);
  1256. // property_list.sort();
  1257. for (List<PropertyInfo>::Element *PE = property_list.front(); PE; PE = PE->next()) {
  1258. if (skip_editor && PE->get().name.begins_with("__editor"))
  1259. continue;
  1260. if (PE->get().usage & PROPERTY_USAGE_STORAGE || (bundle_resources && PE->get().usage & PROPERTY_USAGE_BUNDLE)) {
  1261. String name = PE->get().name;
  1262. Variant value = res->get(name);
  1263. if ((PE->get().usage & PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero()) || (PE->get().usage & PROPERTY_USAGE_STORE_IF_NONONE && value.is_one()))
  1264. continue;
  1265. if (PE->get().type == Variant::OBJECT && value.is_zero() && !(PE->get().usage & PROPERTY_USAGE_STORE_IF_NULL))
  1266. continue;
  1267. String vars;
  1268. VariantWriter::write_to_string(value, vars, _write_resources, this);
  1269. f->store_string(_valprop(name) + " = " + vars + "\n");
  1270. }
  1271. }
  1272. f->store_string("\n");
  1273. }
  1274. if (packed_scene.is_valid()) {
  1275. //if this is a scene, save nodes and connections!
  1276. Ref<SceneState> state = packed_scene->get_state();
  1277. for (int i = 0; i < state->get_node_count(); i++) {
  1278. StringName type = state->get_node_type(i);
  1279. StringName name = state->get_node_name(i);
  1280. NodePath path = state->get_node_path(i, true);
  1281. NodePath owner = state->get_node_owner_path(i);
  1282. Ref<PackedScene> instance = state->get_node_instance(i);
  1283. String instance_placeholder = state->get_node_instance_placeholder(i);
  1284. Vector<StringName> groups = state->get_node_groups(i);
  1285. String header = "[node";
  1286. header += " name=\"" + String(name) + "\"";
  1287. if (type != StringName()) {
  1288. header += " type=\"" + String(type) + "\"";
  1289. }
  1290. if (path != NodePath()) {
  1291. header += " parent=\"" + String(path.simplified()) + "\"";
  1292. }
  1293. if (owner != NodePath() && owner != NodePath(".")) {
  1294. header += " owner=\"" + String(owner.simplified()) + "\"";
  1295. }
  1296. if (groups.size()) {
  1297. String sgroups = " groups=[\n";
  1298. for (int j = 0; j < groups.size(); j++) {
  1299. sgroups += "\"" + String(groups[j]).c_escape() + "\",\n";
  1300. }
  1301. sgroups += "]";
  1302. header += sgroups;
  1303. }
  1304. f->store_string(header);
  1305. if (instance_placeholder != String()) {
  1306. String vars;
  1307. f->store_string(" instance_placeholder=");
  1308. VariantWriter::write_to_string(instance_placeholder, vars, _write_resources, this);
  1309. f->store_string(vars);
  1310. }
  1311. if (instance.is_valid()) {
  1312. String vars;
  1313. f->store_string(" instance=");
  1314. VariantWriter::write_to_string(instance, vars, _write_resources, this);
  1315. f->store_string(vars);
  1316. }
  1317. f->store_line("]\n");
  1318. for (int j = 0; j < state->get_node_property_count(i); j++) {
  1319. String vars;
  1320. VariantWriter::write_to_string(state->get_node_property_value(i, j), vars, _write_resources, this);
  1321. f->store_string(_valprop(String(state->get_node_property_name(i, j))) + " = " + vars + "\n");
  1322. }
  1323. if (state->get_node_property_count(i)) {
  1324. //add space
  1325. f->store_line(String());
  1326. }
  1327. }
  1328. for (int i = 0; i < state->get_connection_count(); i++) {
  1329. String connstr = "[connection";
  1330. connstr += " signal=\"" + String(state->get_connection_signal(i)) + "\"";
  1331. connstr += " from=\"" + String(state->get_connection_source(i).simplified()) + "\"";
  1332. connstr += " to=\"" + String(state->get_connection_target(i).simplified()) + "\"";
  1333. connstr += " method=\"" + String(state->get_connection_method(i)) + "\"";
  1334. int flags = state->get_connection_flags(i);
  1335. if (flags != Object::CONNECT_PERSIST) {
  1336. connstr += " flags=" + itos(flags);
  1337. }
  1338. Array binds = state->get_connection_binds(i);
  1339. f->store_string(connstr);
  1340. if (binds.size()) {
  1341. String vars;
  1342. VariantWriter::write_to_string(binds, vars, _write_resources, this);
  1343. f->store_string(" binds= " + vars);
  1344. }
  1345. f->store_line("]\n");
  1346. }
  1347. f->store_line(String());
  1348. Vector<NodePath> editable_instances = state->get_editable_instances();
  1349. for (int i = 0; i < editable_instances.size(); i++) {
  1350. f->store_line("[editable path=\"" + editable_instances[i].operator String() + "\"]");
  1351. }
  1352. }
  1353. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1354. f->close();
  1355. return ERR_CANT_CREATE;
  1356. }
  1357. f->close();
  1358. //memdelete(f);
  1359. return OK;
  1360. }
  1361. Error ResourceFormatSaverText::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1362. if (p_path.ends_with(".sct") && p_resource->get_type() != "PackedScene") {
  1363. return ERR_FILE_UNRECOGNIZED;
  1364. }
  1365. ResourceFormatSaverTextInstance saver;
  1366. return saver.save(p_path, p_resource, p_flags);
  1367. }
  1368. bool ResourceFormatSaverText::recognize(const RES &p_resource) const {
  1369. return true; // all recognized!
  1370. }
  1371. void ResourceFormatSaverText::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1372. if (p_resource->get_type() == "PackedScene")
  1373. p_extensions->push_back("tscn"); //text scene
  1374. else
  1375. p_extensions->push_back("tres"); //text resource
  1376. }
  1377. ResourceFormatSaverText *ResourceFormatSaverText::singleton = NULL;
  1378. ResourceFormatSaverText::ResourceFormatSaverText() {
  1379. singleton = this;
  1380. }