scene_format_text.cpp 46 KB

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