scene_format_text.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/dir_access.h"
  32. #include "project_settings.h"
  33. #include "version.h"
  34. //version 2: changed names for basis, rect3, poolvectors, etc.
  35. #define FORMAT_VERSION 2
  36. #include "os/dir_access.h"
  37. #include "version.h"
  38. #define _printerr() ERR_PRINT(String(res_path + ":" + itos(lines) + " - Parse Error: " + error_text).utf8().get_data());
  39. ///
  40. void ResourceInteractiveLoaderText::set_local_path(const String &p_local_path) {
  41. res_path = p_local_path;
  42. }
  43. Ref<Resource> ResourceInteractiveLoaderText::get_resource() {
  44. return resource;
  45. }
  46. Error ResourceInteractiveLoaderText::_parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  47. VariantParser::Token token;
  48. VariantParser::get_token(p_stream, token, line, r_err_str);
  49. if (token.type != VariantParser::TK_NUMBER) {
  50. r_err_str = "Expected number (sub-resource index)";
  51. return ERR_PARSE_ERROR;
  52. }
  53. int index = token.value;
  54. String path = local_path + "::" + itos(index);
  55. if (!ignore_resource_parsing) {
  56. if (!ResourceCache::has(path)) {
  57. r_err_str = "Can't load cached sub-resource: " + path;
  58. return ERR_PARSE_ERROR;
  59. }
  60. r_res = RES(ResourceCache::get(path));
  61. } else {
  62. r_res = RES();
  63. }
  64. VariantParser::get_token(p_stream, token, line, r_err_str);
  65. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  66. r_err_str = "Expected ')'";
  67. return ERR_PARSE_ERROR;
  68. }
  69. return OK;
  70. }
  71. Error ResourceInteractiveLoaderText::_parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  72. VariantParser::Token token;
  73. VariantParser::get_token(p_stream, token, line, r_err_str);
  74. if (token.type != VariantParser::TK_NUMBER) {
  75. r_err_str = "Expected number (sub-resource index)";
  76. return ERR_PARSE_ERROR;
  77. }
  78. int id = token.value;
  79. if (!ignore_resource_parsing) {
  80. if (!ext_resources.has(id)) {
  81. r_err_str = "Can't load cached ext-resource #" + itos(id);
  82. return ERR_PARSE_ERROR;
  83. }
  84. String path = ext_resources[id].path;
  85. String type = ext_resources[id].type;
  86. if (path.find("://") == -1 && path.is_rel_path()) {
  87. // path is relative to file being loaded, so convert to a resource path
  88. path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  89. }
  90. r_res = ResourceLoader::load(path, type);
  91. if (r_res.is_null()) {
  92. WARN_PRINT(String("Couldn't load external resource: " + path).utf8().get_data());
  93. }
  94. } else {
  95. r_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::poll() {
  105. if (error != OK)
  106. return error;
  107. if (next_tag.name == "ext_resource") {
  108. if (!next_tag.fields.has("path")) {
  109. error = ERR_FILE_CORRUPT;
  110. error_text = "Missing 'path' in external resource tag";
  111. _printerr();
  112. return error;
  113. }
  114. if (!next_tag.fields.has("type")) {
  115. error = ERR_FILE_CORRUPT;
  116. error_text = "Missing 'type' in external resource tag";
  117. _printerr();
  118. return error;
  119. }
  120. if (!next_tag.fields.has("id")) {
  121. error = ERR_FILE_CORRUPT;
  122. error_text = "Missing 'id' in external resource tag";
  123. _printerr();
  124. return error;
  125. }
  126. String path = next_tag.fields["path"];
  127. String type = next_tag.fields["type"];
  128. int index = next_tag.fields["id"];
  129. if (path.find("://") == -1 && path.is_rel_path()) {
  130. // path is relative to file being loaded, so convert to a resource path
  131. path = ProjectSettings::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  132. }
  133. if (remaps.has(path)) {
  134. path = remaps[path];
  135. }
  136. RES res = ResourceLoader::load(path, type);
  137. if (res.is_null()) {
  138. if (ResourceLoader::get_abort_on_missing_resources()) {
  139. error = ERR_FILE_CORRUPT;
  140. error_text = "[ext_resource] referenced nonexistent resource at: " + path;
  141. _printerr();
  142. return error;
  143. } else {
  144. ResourceLoader::notify_dependency_error(local_path, path, type);
  145. }
  146. } else {
  147. resource_cache.push_back(res);
  148. }
  149. ExtResource er;
  150. er.path = path;
  151. er.type = type;
  152. ext_resources[index] = er;
  153. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  154. if (error) {
  155. _printerr();
  156. }
  157. resource_current++;
  158. return error;
  159. } else if (next_tag.name == "sub_resource") {
  160. if (!next_tag.fields.has("type")) {
  161. error = ERR_FILE_CORRUPT;
  162. error_text = "Missing 'type' in external resource tag";
  163. _printerr();
  164. return error;
  165. }
  166. if (!next_tag.fields.has("id")) {
  167. error = ERR_FILE_CORRUPT;
  168. error_text = "Missing 'index' in external resource tag";
  169. _printerr();
  170. return error;
  171. }
  172. String type = next_tag.fields["type"];
  173. int id = next_tag.fields["id"];
  174. String path = local_path + "::" + itos(id);
  175. //bool exists=ResourceCache::has(path);
  176. Ref<Resource> res;
  177. if (!ResourceCache::has(path)) { //only if it doesn't exist
  178. Object *obj = ClassDB::instance(type);
  179. if (!obj) {
  180. error_text += "Can't create sub resource of type: " + type;
  181. _printerr();
  182. error = ERR_FILE_CORRUPT;
  183. return error;
  184. }
  185. Resource *r = Object::cast_to<Resource>(obj);
  186. if (!r) {
  187. error_text += "Can't create sub resource of type, because not a resource: " + type;
  188. _printerr();
  189. error = ERR_FILE_CORRUPT;
  190. return error;
  191. }
  192. res = Ref<Resource>(r);
  193. resource_cache.push_back(res);
  194. res->set_path(path);
  195. }
  196. resource_current++;
  197. while (true) {
  198. String assign;
  199. Variant value;
  200. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  201. if (error) {
  202. _printerr();
  203. return error;
  204. }
  205. if (assign != String()) {
  206. if (res.is_valid()) {
  207. res->set(assign, value);
  208. }
  209. //it's assignment
  210. } else if (next_tag.name != String()) {
  211. error = OK;
  212. break;
  213. } else {
  214. error = ERR_FILE_CORRUPT;
  215. error_text = "Premature end of file while parsing [sub_resource]";
  216. _printerr();
  217. return error;
  218. }
  219. }
  220. return OK;
  221. } else if (next_tag.name == "resource") {
  222. if (is_scene) {
  223. error_text += "found the 'resource' tag on a scene file!";
  224. _printerr();
  225. error = ERR_FILE_CORRUPT;
  226. return error;
  227. }
  228. Object *obj = ClassDB::instance(res_type);
  229. if (!obj) {
  230. error_text += "Can't create sub resource of type: " + res_type;
  231. _printerr();
  232. error = ERR_FILE_CORRUPT;
  233. return error;
  234. }
  235. Resource *r = Object::cast_to<Resource>(obj);
  236. if (!r) {
  237. error_text += "Can't create sub resource of type, because not a resource: " + res_type;
  238. _printerr();
  239. error = ERR_FILE_CORRUPT;
  240. return error;
  241. }
  242. resource = Ref<Resource>(r);
  243. resource_current++;
  244. while (true) {
  245. String assign;
  246. Variant value;
  247. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  248. if (error) {
  249. if (error != ERR_FILE_EOF) {
  250. _printerr();
  251. } else {
  252. if (!ResourceCache::has(res_path)) {
  253. resource->set_path(res_path);
  254. }
  255. resource->set_as_translation_remapped(translation_remapped);
  256. }
  257. return error;
  258. }
  259. if (assign != String()) {
  260. resource->set(assign, value);
  261. //it's assignment
  262. } else if (next_tag.name != String()) {
  263. error = ERR_FILE_CORRUPT;
  264. error_text = "Extra tag found when parsing main resource file";
  265. _printerr();
  266. return error;
  267. } else {
  268. error = ERR_FILE_EOF;
  269. return error;
  270. }
  271. }
  272. return OK;
  273. } else if (next_tag.name == "node") {
  274. if (!is_scene) {
  275. error_text += "found the 'node' tag on a resource file!";
  276. _printerr();
  277. error = ERR_FILE_CORRUPT;
  278. return error;
  279. }
  280. /*
  281. int add_name(const StringName& p_name);
  282. int add_value(const Variant& p_value);
  283. int add_node_path(const NodePath& p_path);
  284. int add_node(int p_parent,int p_owner,int p_type,int p_name, int p_instance);
  285. void add_node_property(int p_node,int p_name,int p_value);
  286. void add_node_group(int p_node,int p_group);
  287. void set_base_scene(int p_idx);
  288. void add_connection(int p_from,int p_to, int p_signal, int p_method, int p_flags,const Vector<int>& p_binds);
  289. void add_editable_instance(const NodePath& p_path);
  290. */
  291. int parent = -1;
  292. int owner = -1;
  293. int type = -1;
  294. int name = -1;
  295. int instance = -1;
  296. //int base_scene=-1;
  297. if (next_tag.fields.has("name")) {
  298. name = packed_scene->get_state()->add_name(next_tag.fields["name"]);
  299. }
  300. if (next_tag.fields.has("parent")) {
  301. NodePath np = next_tag.fields["parent"];
  302. np.prepend_period(); //compatible to how it manages paths internally
  303. parent = packed_scene->get_state()->add_node_path(np);
  304. }
  305. if (next_tag.fields.has("type")) {
  306. type = packed_scene->get_state()->add_name(next_tag.fields["type"]);
  307. } else {
  308. type = SceneState::TYPE_INSTANCED; //no type? assume this was instanced
  309. }
  310. if (next_tag.fields.has("instance")) {
  311. instance = packed_scene->get_state()->add_value(next_tag.fields["instance"]);
  312. if (packed_scene->get_state()->get_node_count() == 0 && parent == -1) {
  313. packed_scene->get_state()->set_base_scene(instance);
  314. instance = -1;
  315. }
  316. }
  317. if (next_tag.fields.has("instance_placeholder")) {
  318. String path = next_tag.fields["instance_placeholder"];
  319. int path_v = packed_scene->get_state()->add_value(path);
  320. if (packed_scene->get_state()->get_node_count() == 0) {
  321. error = ERR_FILE_CORRUPT;
  322. error_text = "Instance Placeholder can't be used for inheritance.";
  323. _printerr();
  324. return error;
  325. }
  326. instance = path_v | SceneState::FLAG_INSTANCE_IS_PLACEHOLDER;
  327. }
  328. if (next_tag.fields.has("owner")) {
  329. owner = packed_scene->get_state()->add_node_path(next_tag.fields["owner"]);
  330. } else {
  331. if (parent != -1 && !(type == SceneState::TYPE_INSTANCED && instance == -1))
  332. owner = 0; //if no owner, owner is root
  333. }
  334. int node_id = packed_scene->get_state()->add_node(parent, owner, type, name, instance);
  335. if (next_tag.fields.has("groups")) {
  336. Array groups = next_tag.fields["groups"];
  337. for (int i = 0; i < groups.size(); i++) {
  338. packed_scene->get_state()->add_node_group(node_id, packed_scene->get_state()->add_name(groups[i]));
  339. }
  340. }
  341. while (true) {
  342. String assign;
  343. Variant value;
  344. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  345. if (error) {
  346. if (error != ERR_FILE_EOF) {
  347. _printerr();
  348. } else {
  349. resource = packed_scene;
  350. if (!ResourceCache::has(res_path)) {
  351. packed_scene->set_path(res_path);
  352. }
  353. }
  354. return error;
  355. }
  356. if (assign != String()) {
  357. int nameidx = packed_scene->get_state()->add_name(assign);
  358. int valueidx = packed_scene->get_state()->add_value(value);
  359. packed_scene->get_state()->add_node_property(node_id, nameidx, valueidx);
  360. //it's assignment
  361. } else if (next_tag.name != String()) {
  362. error = OK;
  363. return error;
  364. } else {
  365. resource = packed_scene;
  366. error = ERR_FILE_EOF;
  367. return error;
  368. }
  369. }
  370. return OK;
  371. } else if (next_tag.name == "connection") {
  372. if (!is_scene) {
  373. error_text += "found the 'connection' tag on a resource file!";
  374. _printerr();
  375. error = ERR_FILE_CORRUPT;
  376. return error;
  377. }
  378. if (!next_tag.fields.has("from")) {
  379. error = ERR_FILE_CORRUPT;
  380. error_text = "missing 'from' field fron connection tag";
  381. return error;
  382. }
  383. if (!next_tag.fields.has("to")) {
  384. error = ERR_FILE_CORRUPT;
  385. error_text = "missing 'to' field fron connection tag";
  386. return error;
  387. }
  388. if (!next_tag.fields.has("signal")) {
  389. error = ERR_FILE_CORRUPT;
  390. error_text = "missing 'signal' field fron connection tag";
  391. return error;
  392. }
  393. if (!next_tag.fields.has("method")) {
  394. error = ERR_FILE_CORRUPT;
  395. error_text = "missing 'method' field fron connection tag";
  396. return error;
  397. }
  398. NodePath from = next_tag.fields["from"];
  399. NodePath to = next_tag.fields["to"];
  400. StringName method = next_tag.fields["method"];
  401. StringName signal = next_tag.fields["signal"];
  402. int flags = CONNECT_PERSIST;
  403. Array binds;
  404. if (next_tag.fields.has("flags")) {
  405. flags = next_tag.fields["flags"];
  406. }
  407. if (next_tag.fields.has("binds")) {
  408. binds = next_tag.fields["binds"];
  409. }
  410. Vector<int> bind_ints;
  411. for (int i = 0; i < binds.size(); i++) {
  412. bind_ints.push_back(packed_scene->get_state()->add_value(binds[i]));
  413. }
  414. packed_scene->get_state()->add_connection(
  415. packed_scene->get_state()->add_node_path(from.simplified()),
  416. packed_scene->get_state()->add_node_path(to.simplified()),
  417. packed_scene->get_state()->add_name(signal),
  418. packed_scene->get_state()->add_name(method),
  419. flags,
  420. bind_ints);
  421. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  422. if (error) {
  423. if (error != ERR_FILE_EOF) {
  424. _printerr();
  425. } else {
  426. resource = packed_scene;
  427. }
  428. }
  429. return error;
  430. } else if (next_tag.name == "editable") {
  431. if (!is_scene) {
  432. error_text += "found the 'editable' tag on a resource file!";
  433. _printerr();
  434. error = ERR_FILE_CORRUPT;
  435. return error;
  436. }
  437. if (!next_tag.fields.has("path")) {
  438. error = ERR_FILE_CORRUPT;
  439. error_text = "missing 'path' field fron connection tag";
  440. _printerr();
  441. return error;
  442. }
  443. NodePath path = next_tag.fields["path"];
  444. packed_scene->get_state()->add_editable_instance(path.simplified());
  445. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  446. if (error) {
  447. if (error != ERR_FILE_EOF) {
  448. _printerr();
  449. } else {
  450. resource = packed_scene;
  451. }
  452. }
  453. return error;
  454. } else {
  455. error_text += "Unknown tag in file: " + next_tag.name;
  456. _printerr();
  457. error = ERR_FILE_CORRUPT;
  458. return error;
  459. }
  460. return OK;
  461. }
  462. int ResourceInteractiveLoaderText::get_stage() const {
  463. return resource_current;
  464. }
  465. int ResourceInteractiveLoaderText::get_stage_count() const {
  466. return resources_total; //+ext_resources;
  467. }
  468. void ResourceInteractiveLoaderText::set_translation_remapped(bool p_remapped) {
  469. translation_remapped = p_remapped;
  470. }
  471. ResourceInteractiveLoaderText::ResourceInteractiveLoaderText() {
  472. translation_remapped = false;
  473. }
  474. ResourceInteractiveLoaderText::~ResourceInteractiveLoaderText() {
  475. memdelete(f);
  476. }
  477. void ResourceInteractiveLoaderText::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) {
  478. open(p_f);
  479. ignore_resource_parsing = true;
  480. ERR_FAIL_COND(error != OK);
  481. while (next_tag.name == "ext_resource") {
  482. if (!next_tag.fields.has("type")) {
  483. error = ERR_FILE_CORRUPT;
  484. error_text = "Missing 'type' in external resource tag";
  485. _printerr();
  486. return;
  487. }
  488. if (!next_tag.fields.has("id")) {
  489. error = ERR_FILE_CORRUPT;
  490. error_text = "Missing 'index' in external resource tag";
  491. _printerr();
  492. return;
  493. }
  494. String path = next_tag.fields["path"];
  495. String type = next_tag.fields["type"];
  496. if (path.find("://") == -1 && path.is_rel_path()) {
  497. // path is relative to file being loaded, so convert to a resource path
  498. path = ProjectSettings::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  499. }
  500. if (p_add_types) {
  501. path += "::" + type;
  502. }
  503. p_dependencies->push_back(path);
  504. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  505. if (err) {
  506. print_line(error_text + " - " + itos(lines));
  507. error_text = "Unexpected end of file";
  508. _printerr();
  509. error = ERR_FILE_CORRUPT;
  510. }
  511. }
  512. }
  513. Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map) {
  514. open(p_f, true);
  515. ERR_FAIL_COND_V(error != OK, error);
  516. ignore_resource_parsing = true;
  517. //FileAccess
  518. FileAccess *fw = NULL;
  519. String base_path = local_path.get_base_dir();
  520. uint64_t tag_end = f->get_position();
  521. while (true) {
  522. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  523. if (err != OK) {
  524. if (fw) {
  525. memdelete(fw);
  526. }
  527. error = ERR_FILE_CORRUPT;
  528. ERR_FAIL_V(error);
  529. }
  530. if (next_tag.name != "ext_resource") {
  531. //nothing was done
  532. if (!fw)
  533. return OK;
  534. break;
  535. } else {
  536. if (!fw) {
  537. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  538. if (is_scene) {
  539. fw->store_line("[gd_scene load_steps=" + itos(resources_total) + " format=" + itos(FORMAT_VERSION) + "]\n");
  540. } else {
  541. fw->store_line("[gd_resource type=\"" + res_type + "\" load_steps=" + itos(resources_total) + " format=" + itos(FORMAT_VERSION) + "]\n");
  542. }
  543. }
  544. if (!next_tag.fields.has("path") || !next_tag.fields.has("id") || !next_tag.fields.has("type")) {
  545. memdelete(fw);
  546. error = ERR_FILE_CORRUPT;
  547. ERR_FAIL_V(error);
  548. }
  549. String path = next_tag.fields["path"];
  550. int index = next_tag.fields["id"];
  551. String type = next_tag.fields["type"];
  552. bool relative = false;
  553. if (!path.begins_with("res://")) {
  554. path = base_path.plus_file(path).simplify_path();
  555. relative = true;
  556. }
  557. if (p_map.has(path)) {
  558. String np = p_map[path];
  559. path = np;
  560. }
  561. if (relative) {
  562. //restore relative
  563. path = base_path.path_to_file(path);
  564. }
  565. fw->store_line("[ext_resource path=\"" + path + "\" type=\"" + type + "\" id=" + itos(index) + "]");
  566. tag_end = f->get_position();
  567. }
  568. }
  569. f->seek(tag_end);
  570. uint8_t c = f->get_8();
  571. while (!f->eof_reached()) {
  572. fw->store_8(c);
  573. c = f->get_8();
  574. }
  575. f->close();
  576. bool all_ok = fw->get_error() == OK;
  577. memdelete(fw);
  578. if (!all_ok) {
  579. return ERR_CANT_CREATE;
  580. }
  581. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  582. da->remove(p_path);
  583. da->rename(p_path + ".depren", p_path);
  584. memdelete(da);
  585. return OK;
  586. }
  587. void ResourceInteractiveLoaderText::open(FileAccess *p_f, bool p_skip_first_tag) {
  588. error = OK;
  589. lines = 1;
  590. f = p_f;
  591. stream.f = f;
  592. is_scene = false;
  593. ignore_resource_parsing = false;
  594. resource_current = 0;
  595. VariantParser::Tag tag;
  596. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  597. if (err) {
  598. error = err;
  599. _printerr();
  600. return;
  601. }
  602. if (tag.fields.has("format")) {
  603. int fmt = tag.fields["format"];
  604. if (fmt > FORMAT_VERSION) {
  605. error_text = "Saved with newer format version";
  606. _printerr();
  607. error = ERR_PARSE_ERROR;
  608. return;
  609. }
  610. }
  611. if (tag.name == "gd_scene") {
  612. is_scene = true;
  613. packed_scene.instance();
  614. } else if (tag.name == "gd_resource") {
  615. if (!tag.fields.has("type")) {
  616. error_text = "Missing 'type' field in 'gd_resource' tag";
  617. _printerr();
  618. error = ERR_PARSE_ERROR;
  619. return;
  620. }
  621. res_type = tag.fields["type"];
  622. } else {
  623. error_text = "Unrecognized file type: " + tag.name;
  624. _printerr();
  625. error = ERR_PARSE_ERROR;
  626. return;
  627. }
  628. if (tag.fields.has("load_steps")) {
  629. resources_total = tag.fields["load_steps"];
  630. } else {
  631. resources_total = 0;
  632. }
  633. if (!p_skip_first_tag) {
  634. err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  635. if (err) {
  636. error_text = "Unexpected end of file";
  637. _printerr();
  638. error = ERR_FILE_CORRUPT;
  639. }
  640. }
  641. rp.ext_func = _parse_ext_resources;
  642. rp.sub_func = _parse_sub_resources;
  643. rp.func = NULL;
  644. rp.userdata = this;
  645. }
  646. String ResourceInteractiveLoaderText::recognize(FileAccess *p_f) {
  647. error = OK;
  648. lines = 1;
  649. f = p_f;
  650. stream.f = f;
  651. ignore_resource_parsing = true;
  652. VariantParser::Tag tag;
  653. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  654. if (err) {
  655. _printerr();
  656. return "";
  657. }
  658. if (tag.fields.has("format")) {
  659. int fmt = tag.fields["format"];
  660. if (fmt > FORMAT_VERSION) {
  661. error_text = "Saved with newer format version";
  662. _printerr();
  663. return "";
  664. }
  665. }
  666. if (tag.name == "gd_scene")
  667. return "PackedScene";
  668. if (tag.name != "gd_resource")
  669. return "";
  670. if (!tag.fields.has("type")) {
  671. error_text = "Missing 'type' field in 'gd_resource' tag";
  672. _printerr();
  673. return "";
  674. }
  675. return tag.fields["type"];
  676. }
  677. /////////////////////
  678. Ref<ResourceInteractiveLoader> ResourceFormatLoaderText::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
  679. if (r_error)
  680. *r_error = ERR_CANT_OPEN;
  681. Error err;
  682. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  683. if (err != OK) {
  684. ERR_FAIL_COND_V(err != OK, Ref<ResourceInteractiveLoader>());
  685. }
  686. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  687. String path = p_original_path != "" ? p_original_path : p_path;
  688. ria->local_path = ProjectSettings::get_singleton()->localize_path(path);
  689. ria->res_path = ria->local_path;
  690. //ria->set_local_path( ProjectSettings::get_singleton()->localize_path(p_path) );
  691. ria->open(f);
  692. return ria;
  693. }
  694. void ResourceFormatLoaderText::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  695. if (p_type == "") {
  696. get_recognized_extensions(p_extensions);
  697. return;
  698. }
  699. if (p_type == "PackedScene")
  700. p_extensions->push_back("tscn");
  701. else
  702. p_extensions->push_back("tres");
  703. }
  704. void ResourceFormatLoaderText::get_recognized_extensions(List<String> *p_extensions) const {
  705. p_extensions->push_back("tscn");
  706. p_extensions->push_back("tres");
  707. }
  708. bool ResourceFormatLoaderText::handles_type(const String &p_type) const {
  709. return true;
  710. }
  711. String ResourceFormatLoaderText::get_resource_type(const String &p_path) const {
  712. String ext = p_path.get_extension().to_lower();
  713. if (ext == "tscn")
  714. return "PackedScene";
  715. else if (ext != "tres")
  716. return String();
  717. //for anyhting else must test..
  718. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  719. if (!f) {
  720. return ""; //could not rwead
  721. }
  722. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  723. ria->local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  724. ria->res_path = ria->local_path;
  725. //ria->set_local_path( ProjectSettings::get_singleton()->localize_path(p_path) );
  726. String r = ria->recognize(f);
  727. return r;
  728. }
  729. void ResourceFormatLoaderText::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  730. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  731. if (!f) {
  732. ERR_FAIL();
  733. }
  734. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  735. ria->local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  736. ria->res_path = ria->local_path;
  737. //ria->set_local_path( ProjectSettings::get_singleton()->localize_path(p_path) );
  738. ria->get_dependencies(f, p_dependencies, p_add_types);
  739. }
  740. Error ResourceFormatLoaderText::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  741. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  742. if (!f) {
  743. ERR_FAIL_V(ERR_CANT_OPEN);
  744. }
  745. Ref<ResourceInteractiveLoaderText> ria = memnew(ResourceInteractiveLoaderText);
  746. ria->local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  747. ria->res_path = ria->local_path;
  748. //ria->set_local_path( ProjectSettings::get_singleton()->localize_path(p_path) );
  749. return ria->rename_dependencies(f, p_path, p_map);
  750. }
  751. /*****************************************************************************************************/
  752. /*****************************************************************************************************/
  753. /*****************************************************************************************************/
  754. /*****************************************************************************************************/
  755. /*****************************************************************************************************/
  756. /*****************************************************************************************************/
  757. /*****************************************************************************************************/
  758. /*****************************************************************************************************/
  759. /*****************************************************************************************************/
  760. /*****************************************************************************************************/
  761. String ResourceFormatSaverTextInstance::_write_resources(void *ud, const RES &p_resource) {
  762. ResourceFormatSaverTextInstance *rsi = (ResourceFormatSaverTextInstance *)ud;
  763. return rsi->_write_resource(p_resource);
  764. }
  765. String ResourceFormatSaverTextInstance::_write_resource(const RES &res) {
  766. if (external_resources.has(res)) {
  767. return "ExtResource( " + itos(external_resources[res] + 1) + " )";
  768. } else {
  769. if (internal_resources.has(res)) {
  770. return "SubResource( " + itos(internal_resources[res]) + " )";
  771. } else if (res->get_path().length() && res->get_path().find("::") == -1) {
  772. //external resource
  773. String path = relative_paths ? local_path.path_to_file(res->get_path()) : res->get_path();
  774. return "Resource( \"" + path + "\" )";
  775. } else {
  776. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  777. ERR_FAIL_V("null");
  778. //internal resource
  779. }
  780. }
  781. return "null";
  782. }
  783. void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant, bool p_main) {
  784. switch (p_variant.get_type()) {
  785. case Variant::OBJECT: {
  786. RES res = p_variant.operator RefPtr();
  787. if (res.is_null() || external_resources.has(res))
  788. return;
  789. if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
  790. int index = external_resources.size();
  791. external_resources[res] = index;
  792. return;
  793. }
  794. if (resource_set.has(res))
  795. return;
  796. List<PropertyInfo> property_list;
  797. res->get_property_list(&property_list);
  798. property_list.sort();
  799. List<PropertyInfo>::Element *I = property_list.front();
  800. while (I) {
  801. PropertyInfo pi = I->get();
  802. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  803. Variant v = res->get(I->get().name);
  804. _find_resources(v);
  805. }
  806. I = I->next();
  807. }
  808. resource_set.insert(res); //saved after, so the childs it needs are available when loaded
  809. saved_resources.push_back(res);
  810. } break;
  811. case Variant::ARRAY: {
  812. Array varray = p_variant;
  813. int len = varray.size();
  814. for (int i = 0; i < len; i++) {
  815. Variant v = varray.get(i);
  816. _find_resources(v);
  817. }
  818. } break;
  819. case Variant::DICTIONARY: {
  820. Dictionary d = p_variant;
  821. List<Variant> keys;
  822. d.get_key_list(&keys);
  823. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  824. Variant v = d[E->get()];
  825. _find_resources(v);
  826. }
  827. } break;
  828. default: {}
  829. }
  830. }
  831. static String _valprop(const String &p_name) {
  832. if (p_name.find("\"") != -1 || p_name.find("=") != -1 || p_name.find(" ") != -1)
  833. return "\"" + p_name.c_escape_multiline() + "\"";
  834. return p_name;
  835. }
  836. Error ResourceFormatSaverTextInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  837. if (p_path.ends_with(".tscn")) {
  838. packed_scene = p_resource;
  839. }
  840. Error err;
  841. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  842. ERR_FAIL_COND_V(err, ERR_CANT_OPEN);
  843. FileAccessRef _fref(f);
  844. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  845. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  846. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  847. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  848. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  849. if (!p_path.begins_with("res://")) {
  850. takeover_paths = false;
  851. }
  852. // save resources
  853. _find_resources(p_resource, true);
  854. if (packed_scene.is_valid()) {
  855. //add instances to external resources if saving a packed scene
  856. for (int i = 0; i < packed_scene->get_state()->get_node_count(); i++) {
  857. if (packed_scene->get_state()->is_node_instance_placeholder(i))
  858. continue;
  859. Ref<PackedScene> instance = packed_scene->get_state()->get_node_instance(i);
  860. if (instance.is_valid() && !external_resources.has(instance)) {
  861. int index = external_resources.size();
  862. external_resources[instance] = index;
  863. }
  864. }
  865. }
  866. ERR_FAIL_COND_V(err != OK, err);
  867. {
  868. String title = packed_scene.is_valid() ? "[gd_scene " : "[gd_resource ";
  869. if (packed_scene.is_null())
  870. title += "type=\"" + p_resource->get_class() + "\" ";
  871. int load_steps = saved_resources.size() + external_resources.size();
  872. /*
  873. if (packed_scene.is_valid()) {
  874. load_steps+=packed_scene->get_node_count();
  875. }
  876. //no, better to not use load steps from nodes, no point to that
  877. */
  878. if (load_steps > 1) {
  879. title += "load_steps=" + itos(load_steps) + " ";
  880. }
  881. title += "format=" + itos(FORMAT_VERSION) + "";
  882. //title+="engine_version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\"";
  883. f->store_string(title);
  884. f->store_line("]\n"); //one empty line
  885. }
  886. Vector<RES> sorted_er;
  887. sorted_er.resize(external_resources.size());
  888. for (Map<RES, int>::Element *E = external_resources.front(); E; E = E->next()) {
  889. sorted_er[E->get()] = E->key();
  890. }
  891. for (int i = 0; i < sorted_er.size(); i++) {
  892. String p = sorted_er[i]->get_path();
  893. f->store_string("[ext_resource path=\"" + p + "\" type=\"" + sorted_er[i]->get_save_class() + "\" id=" + itos(i + 1) + "]\n"); //bundled
  894. }
  895. if (external_resources.size())
  896. f->store_line(String()); //separate
  897. Set<int> used_indices;
  898. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  899. RES res = E->get();
  900. if (E->next() && (res->get_path() == "" || res->get_path().find("::") != -1)) {
  901. if (res->get_subindex() != 0) {
  902. if (used_indices.has(res->get_subindex())) {
  903. res->set_subindex(0); //repeated
  904. } else {
  905. used_indices.insert(res->get_subindex());
  906. }
  907. }
  908. }
  909. }
  910. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  911. RES res = E->get();
  912. ERR_CONTINUE(!resource_set.has(res));
  913. bool main = (E->next() == NULL);
  914. if (main && packed_scene.is_valid())
  915. break; //save as a scene
  916. if (main) {
  917. f->store_line("[resource]\n");
  918. } else {
  919. String line = "[sub_resource ";
  920. if (res->get_subindex() == 0) {
  921. int new_subindex = 1;
  922. if (used_indices.size()) {
  923. new_subindex = used_indices.back()->get() + 1;
  924. }
  925. res->set_subindex(new_subindex);
  926. used_indices.insert(new_subindex);
  927. }
  928. int idx = res->get_subindex();
  929. line += "type=\"" + res->get_class() + "\" id=" + itos(idx);
  930. f->store_line(line + "]\n");
  931. if (takeover_paths) {
  932. res->set_path(p_path + "::" + itos(idx), true);
  933. }
  934. internal_resources[res] = idx;
  935. #ifdef TOOLS_ENABLED
  936. res->set_edited(false);
  937. #endif
  938. }
  939. List<PropertyInfo> property_list;
  940. res->get_property_list(&property_list);
  941. //property_list.sort();
  942. for (List<PropertyInfo>::Element *PE = property_list.front(); PE; PE = PE->next()) {
  943. if (skip_editor && PE->get().name.begins_with("__editor"))
  944. continue;
  945. if (PE->get().usage & PROPERTY_USAGE_STORAGE) {
  946. String name = PE->get().name;
  947. Variant value = res->get(name);
  948. if ((PE->get().usage & PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero()) || (PE->get().usage & PROPERTY_USAGE_STORE_IF_NONONE && value.is_one()))
  949. continue;
  950. if (PE->get().type == Variant::OBJECT && value.is_zero() && !(PE->get().usage & PROPERTY_USAGE_STORE_IF_NULL))
  951. continue;
  952. String vars;
  953. VariantWriter::write_to_string(value, vars, _write_resources, this);
  954. f->store_string(_valprop(name) + " = " + vars + "\n");
  955. }
  956. }
  957. f->store_string("\n");
  958. }
  959. if (packed_scene.is_valid()) {
  960. //if this is a scene, save nodes and connections!
  961. Ref<SceneState> state = packed_scene->get_state();
  962. for (int i = 0; i < state->get_node_count(); i++) {
  963. StringName type = state->get_node_type(i);
  964. StringName name = state->get_node_name(i);
  965. NodePath path = state->get_node_path(i, true);
  966. NodePath owner = state->get_node_owner_path(i);
  967. Ref<PackedScene> instance = state->get_node_instance(i);
  968. String instance_placeholder = state->get_node_instance_placeholder(i);
  969. Vector<StringName> groups = state->get_node_groups(i);
  970. String header = "[node";
  971. header += " name=\"" + String(name) + "\"";
  972. if (type != StringName()) {
  973. header += " type=\"" + String(type) + "\"";
  974. }
  975. if (path != NodePath()) {
  976. header += " parent=\"" + String(path.simplified()) + "\"";
  977. }
  978. if (owner != NodePath() && owner != NodePath(".")) {
  979. header += " owner=\"" + String(owner.simplified()) + "\"";
  980. }
  981. if (groups.size()) {
  982. String sgroups = " groups=[\n";
  983. for (int j = 0; j < groups.size(); j++) {
  984. sgroups += "\"" + String(groups[j]).c_escape() + "\",\n";
  985. }
  986. sgroups += "]";
  987. header += sgroups;
  988. }
  989. f->store_string(header);
  990. if (instance_placeholder != String()) {
  991. String vars;
  992. f->store_string(" instance_placeholder=");
  993. VariantWriter::write_to_string(instance_placeholder, vars, _write_resources, this);
  994. f->store_string(vars);
  995. }
  996. if (instance.is_valid()) {
  997. String vars;
  998. f->store_string(" instance=");
  999. VariantWriter::write_to_string(instance, vars, _write_resources, this);
  1000. f->store_string(vars);
  1001. }
  1002. f->store_line("]\n");
  1003. for (int j = 0; j < state->get_node_property_count(i); j++) {
  1004. String vars;
  1005. VariantWriter::write_to_string(state->get_node_property_value(i, j), vars, _write_resources, this);
  1006. f->store_string(_valprop(String(state->get_node_property_name(i, j))) + " = " + vars + "\n");
  1007. }
  1008. if (state->get_node_property_count(i)) {
  1009. //add space
  1010. f->store_line(String());
  1011. }
  1012. }
  1013. for (int i = 0; i < state->get_connection_count(); i++) {
  1014. String connstr = "[connection";
  1015. connstr += " signal=\"" + String(state->get_connection_signal(i)) + "\"";
  1016. connstr += " from=\"" + String(state->get_connection_source(i).simplified()) + "\"";
  1017. connstr += " to=\"" + String(state->get_connection_target(i).simplified()) + "\"";
  1018. connstr += " method=\"" + String(state->get_connection_method(i)) + "\"";
  1019. int flags = state->get_connection_flags(i);
  1020. if (flags != Object::CONNECT_PERSIST) {
  1021. connstr += " flags=" + itos(flags);
  1022. }
  1023. Array binds = state->get_connection_binds(i);
  1024. f->store_string(connstr);
  1025. if (binds.size()) {
  1026. String vars;
  1027. VariantWriter::write_to_string(binds, vars, _write_resources, this);
  1028. f->store_string(" binds= " + vars);
  1029. }
  1030. f->store_line("]\n");
  1031. }
  1032. f->store_line(String());
  1033. Vector<NodePath> editable_instances = state->get_editable_instances();
  1034. for (int i = 0; i < editable_instances.size(); i++) {
  1035. f->store_line("[editable path=\"" + editable_instances[i].operator String() + "\"]");
  1036. }
  1037. }
  1038. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1039. f->close();
  1040. return ERR_CANT_CREATE;
  1041. }
  1042. f->close();
  1043. //memdelete(f);
  1044. return OK;
  1045. }
  1046. Error ResourceFormatSaverText::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1047. if (p_path.ends_with(".sct") && p_resource->get_class() != "PackedScene") {
  1048. return ERR_FILE_UNRECOGNIZED;
  1049. }
  1050. ResourceFormatSaverTextInstance saver;
  1051. return saver.save(p_path, p_resource, p_flags);
  1052. }
  1053. bool ResourceFormatSaverText::recognize(const RES &p_resource) const {
  1054. return true; // all recognized!
  1055. }
  1056. void ResourceFormatSaverText::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1057. if (p_resource->get_class() == "PackedScene")
  1058. p_extensions->push_back("tscn"); //text scene
  1059. else
  1060. p_extensions->push_back("tres"); //text resource
  1061. }
  1062. ResourceFormatSaverText *ResourceFormatSaverText::singleton = NULL;
  1063. ResourceFormatSaverText::ResourceFormatSaverText() {
  1064. singleton = this;
  1065. }