scene_format_text.cpp 33 KB

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