resource_format_xml.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634
  1. /*************************************************************************/
  2. /* resource_format_xml.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "resource_format_xml.h"
  30. #include "globals.h"
  31. #include "version.h"
  32. ResourceInteractiveLoaderXML::Tag* ResourceInteractiveLoaderXML::parse_tag(bool *r_exit,bool p_printerr) {
  33. while(get_char()!='<' && !f->eof_reached()) {}
  34. if (f->eof_reached()) {
  35. return NULL;
  36. }
  37. Tag tag;
  38. bool exit=false;
  39. if (r_exit)
  40. *r_exit=false;
  41. bool complete=false;
  42. while(!f->eof_reached()) {
  43. CharType c=get_char();
  44. if (c<33 && tag.name.length() && !exit) {
  45. break;
  46. } else if (c=='>') {
  47. complete=true;
  48. break;
  49. } else if (c=='/') {
  50. exit=true;
  51. } else {
  52. tag.name+=c;
  53. }
  54. }
  55. if (f->eof_reached()) {
  56. return NULL;
  57. }
  58. if (exit) {
  59. if (!tag_stack.size()) {
  60. if (!p_printerr)
  61. return NULL;
  62. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unmatched exit tag </"+tag.name+">");
  63. ERR_FAIL_COND_V(!tag_stack.size(),NULL);
  64. }
  65. if (tag_stack.back()->get().name!=tag.name) {
  66. if (!p_printerr)
  67. return NULL;
  68. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Mismatched exit tag. Got </"+tag.name+">, expected </"+tag_stack.back()->get().name+">");
  69. ERR_FAIL_COND_V(tag_stack.back()->get().name!=tag.name,NULL);
  70. }
  71. if (!complete) {
  72. while(get_char()!='>' && !f->eof_reached()) {}
  73. if (f->eof_reached())
  74. return NULL;
  75. }
  76. if (r_exit)
  77. *r_exit=true;
  78. tag_stack.pop_back();
  79. return NULL;
  80. }
  81. if (!complete) {
  82. String name;
  83. CharString r_value;
  84. bool reading_value=false;
  85. while(!f->eof_reached()) {
  86. CharType c=get_char();
  87. if (c=='>') {
  88. if (r_value.size()) {
  89. r_value.push_back(0);
  90. tag.args[name].parse_utf8(r_value.get_data());
  91. }
  92. break;
  93. } else if ( ((!reading_value && (c<33)) || c=='=' || c=='"' || c=='\'') && tag.name.length()) {
  94. if (!reading_value && name.length()) {
  95. reading_value=true;
  96. } else if (reading_value && r_value.size()) {
  97. r_value.push_back(0);
  98. tag.args[name].parse_utf8(r_value.get_data());
  99. name="";
  100. r_value.clear();
  101. reading_value=false;
  102. }
  103. } else if (reading_value) {
  104. r_value.push_back(c);
  105. } else {
  106. name+=c;
  107. }
  108. }
  109. if (f->eof_reached())
  110. return NULL;
  111. }
  112. tag_stack.push_back(tag);
  113. return &tag_stack.back()->get();
  114. }
  115. Error ResourceInteractiveLoaderXML::close_tag(const String& p_name) {
  116. int level=0;
  117. bool inside_tag=false;
  118. while(true) {
  119. if (f->eof_reached()) {
  120. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": EOF found while attempting to find </"+p_name+">");
  121. ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT );
  122. }
  123. uint8_t c = get_char();
  124. if (c == '<') {
  125. if (inside_tag) {
  126. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML. Already inside Tag.");
  127. ERR_FAIL_COND_V(inside_tag,ERR_FILE_CORRUPT);
  128. }
  129. inside_tag=true;
  130. c = get_char();
  131. if (c == '/') {
  132. --level;
  133. } else {
  134. ++level;
  135. };
  136. } else if (c == '>') {
  137. if (!inside_tag) {
  138. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML. Already outside Tag");
  139. ERR_FAIL_COND_V(!inside_tag,ERR_FILE_CORRUPT);
  140. }
  141. inside_tag=false;
  142. if (level == -1) {
  143. tag_stack.pop_back();
  144. return OK;
  145. };
  146. };
  147. }
  148. return OK;
  149. }
  150. void ResourceInteractiveLoaderXML::unquote(String& p_str) {
  151. p_str=p_str.strip_edges().replace("\"","").xml_unescape();
  152. /*p_str=p_str.strip_edges();
  153. p_str=p_str.replace("\"","");
  154. p_str=p_str.replace("&gt;","<");
  155. p_str=p_str.replace("&lt;",">");
  156. p_str=p_str.replace("&apos;","'");
  157. p_str=p_str.replace("&quot;","\"");
  158. for (int i=1;i<32;i++) {
  159. char chr[2]={i,0};
  160. p_str=p_str.replace("&#"+String::num(i)+";",chr);
  161. }
  162. p_str=p_str.replace("&amp;","&");
  163. */
  164. //p_str.parse_utf8( p_str.ascii(true).get_data() );
  165. }
  166. Error ResourceInteractiveLoaderXML::goto_end_of_tag() {
  167. uint8_t c;
  168. while(true) {
  169. c=get_char();
  170. if (c=='>') //closetag
  171. break;
  172. if (f->eof_reached()) {
  173. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": EOF found while attempting to find close tag.");
  174. ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT );
  175. }
  176. }
  177. tag_stack.pop_back();
  178. return OK;
  179. }
  180. Error ResourceInteractiveLoaderXML::parse_property_data(String &r_data) {
  181. r_data="";
  182. CharString cs;
  183. while(true) {
  184. CharType c=get_char();
  185. if (c=='<')
  186. break;
  187. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  188. cs.push_back(c);
  189. }
  190. cs.push_back(0);
  191. r_data.parse_utf8(cs.get_data());
  192. while(get_char()!='>' && !f->eof_reached()) {}
  193. if (f->eof_reached()) {
  194. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Malformed XML.");
  195. ERR_FAIL_COND_V( f->eof_reached(), ERR_FILE_CORRUPT );
  196. }
  197. r_data=r_data.strip_edges();
  198. tag_stack.pop_back();
  199. return OK;
  200. }
  201. Error ResourceInteractiveLoaderXML::_parse_array_element(Vector<char> &buff,bool p_number_only,FileAccess *f,bool *end) {
  202. if (buff.empty())
  203. buff.resize(32); // optimi
  204. int buff_max=buff.size();
  205. int buff_size=0;
  206. *end=false;
  207. char *buffptr=&buff[0];
  208. bool found=false;
  209. bool quoted=false;
  210. while(true) {
  211. char c=get_char();
  212. if (c==0) {
  213. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (zero found).");
  214. ERR_FAIL_V(ERR_FILE_CORRUPT);
  215. } else if (c=='"') {
  216. quoted=!quoted;
  217. } else if ((!quoted && ((p_number_only && c<33) || c==',')) || c=='<') {
  218. if (c=='<') {
  219. *end=true;
  220. break;
  221. }
  222. if (c<32 && f->eof_reached()) {
  223. *end=true;
  224. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (unexpected EOF).");
  225. ERR_FAIL_V(ERR_FILE_CORRUPT);
  226. }
  227. if (found)
  228. break;
  229. } else {
  230. found=true;
  231. if (buff_size>=buff_max) {
  232. buff_max++;
  233. buff.resize(buff_max);
  234. buffptr=buff.ptr();
  235. }
  236. buffptr[buff_size]=c;
  237. buff_size++;
  238. }
  239. }
  240. if (buff_size>=buff_max) {
  241. buff_max++;
  242. buff.resize(buff_max);
  243. }
  244. buff[buff_size]=0;
  245. buff_size++;
  246. return OK;
  247. }
  248. Error ResourceInteractiveLoaderXML::parse_property(Variant& r_v, String &r_name) {
  249. bool exit;
  250. Tag *tag = parse_tag(&exit);
  251. if (!tag) {
  252. if (exit) // shouldn't have exited
  253. return ERR_FILE_EOF;
  254. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": File corrupt (No Property Tag).");
  255. ERR_FAIL_V(ERR_FILE_CORRUPT);
  256. }
  257. r_v=Variant();
  258. r_name="";
  259. //ERR_FAIL_COND_V(tag->name!="property",ERR_FILE_CORRUPT);
  260. //ERR_FAIL_COND_V(!tag->args.has("name"),ERR_FILE_CORRUPT);
  261. // ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT);
  262. //String name=tag->args["name"];
  263. //ERR_FAIL_COND_V(name=="",ERR_FILE_CORRUPT);
  264. String type=tag->name;
  265. String name=tag->args["name"];
  266. if (type=="") {
  267. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": 'type' field is empty.");
  268. ERR_FAIL_COND_V(type=="",ERR_FILE_CORRUPT);
  269. }
  270. if (type=="dictionary") {
  271. Dictionary d( tag->args.has("shared") && (String(tag->args["shared"])=="true" || String(tag->args["shared"])=="1"));
  272. while(true) {
  273. Error err;
  274. String tagname;
  275. Variant key;
  276. int dictline = get_current_line();
  277. err=parse_property(key,tagname);
  278. if (err && err!=ERR_FILE_EOF) {
  279. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error parsing dictionary: "+name+" (from line "+itos(dictline)+")");
  280. ERR_FAIL_COND_V(err && err!=ERR_FILE_EOF,err);
  281. }
  282. //ERR_FAIL_COND_V(tagname!="key",ERR_FILE_CORRUPT);
  283. if (err)
  284. break;
  285. Variant value;
  286. err=parse_property(value,tagname);
  287. if (err) {
  288. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error parsing dictionary: "+name+" (from line "+itos(dictline)+")");
  289. }
  290. ERR_FAIL_COND_V(err,err);
  291. //ERR_FAIL_COND_V(tagname!="value",ERR_FILE_CORRUPT);
  292. d[key]=value;
  293. }
  294. //err=parse_property_data(name); // skip the rest
  295. //ERR_FAIL_COND_V(err,err);
  296. r_name=name;
  297. r_v=d;
  298. return OK;
  299. } else if (type=="array") {
  300. if (!tag->args.has("len")) {
  301. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  302. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  303. }
  304. int len=tag->args["len"].to_int();
  305. bool shared = tag->args.has("shared") && (String(tag->args["shared"])=="true" || String(tag->args["shared"])=="1");
  306. Array array(shared);
  307. array.resize(len);
  308. Error err;
  309. Variant v;
  310. String tagname;
  311. int idx=0;
  312. while( (err=parse_property(v,tagname))==OK ) {
  313. ERR_CONTINUE( idx <0 || idx >=len );
  314. array.set(idx,v);
  315. idx++;
  316. }
  317. if (idx!=len) {
  318. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array (size mismatch): "+name);
  319. ERR_FAIL_COND_V(idx!=len,err);
  320. }
  321. if (err!=ERR_FILE_EOF) {
  322. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array: "+name);
  323. ERR_FAIL_COND_V(err!=ERR_FILE_EOF,err);
  324. }
  325. //err=parse_property_data(name); // skip the rest
  326. //ERR_FAIL_COND_V(err,err);
  327. r_name=name;
  328. r_v=array;
  329. return OK;
  330. } else if (type=="resource") {
  331. if (tag->args.has("path")) {
  332. String path=tag->args["path"];
  333. String hint;
  334. if (tag->args.has("resource_type"))
  335. hint=tag->args["resource_type"];
  336. if (path.begins_with("local://"))
  337. path=path.replace("local://",local_path+"::");
  338. else if (path.find("://")==-1 && path.is_rel_path()) {
  339. // path is relative to file being loaded, so convert to a resource path
  340. path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  341. }
  342. //take advantage of the resource loader cache. The resource is cached on it, even if
  343. RES res=ResourceLoader::load(path,hint);
  344. if (res.is_null()) {
  345. WARN_PRINT(String("Couldn't load resource: "+path).ascii().get_data());
  346. }
  347. r_v=res.get_ref_ptr();
  348. }
  349. Error err=goto_end_of_tag();
  350. if (err) {
  351. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error closing <resource> tag.");
  352. ERR_FAIL_COND_V(err,err);
  353. }
  354. r_name=name;
  355. return OK;
  356. } else if (type=="image") {
  357. if (!tag->args.has("encoding")) {
  358. //empty image
  359. r_v=Image();
  360. String sdfsdfg;
  361. Error err=parse_property_data(sdfsdfg);
  362. return OK;
  363. }
  364. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'encoding' field.");
  365. ERR_FAIL_COND_V( !tag->args.has("encoding"), ERR_FILE_CORRUPT );
  366. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'width' field.");
  367. ERR_FAIL_COND_V( !tag->args.has("width"), ERR_FILE_CORRUPT );
  368. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'height' field.");
  369. ERR_FAIL_COND_V( !tag->args.has("height"), ERR_FILE_CORRUPT );
  370. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Image missing 'format' field.");
  371. ERR_FAIL_COND_V( !tag->args.has("format"), ERR_FILE_CORRUPT );
  372. String encoding=tag->args["encoding"];
  373. if (encoding=="raw") {
  374. String width=tag->args["width"];
  375. String height=tag->args["height"];
  376. String format=tag->args["format"];
  377. int mipmaps=tag->args.has("mipmaps")?int(tag->args["mipmaps"].to_int()):int(0);
  378. int custom_size = tag->args.has("custom_size")?int(tag->args["custom_size"].to_int()):int(0);
  379. r_name=name;
  380. Image::Format imgformat;
  381. if (format=="grayscale") {
  382. imgformat=Image::FORMAT_GRAYSCALE;
  383. } else if (format=="intensity") {
  384. imgformat=Image::FORMAT_INTENSITY;
  385. } else if (format=="grayscale_alpha") {
  386. imgformat=Image::FORMAT_GRAYSCALE_ALPHA;
  387. } else if (format=="rgb") {
  388. imgformat=Image::FORMAT_RGB;
  389. } else if (format=="rgba") {
  390. imgformat=Image::FORMAT_RGBA;
  391. } else if (format=="indexed") {
  392. imgformat=Image::FORMAT_INDEXED;
  393. } else if (format=="indexed_alpha") {
  394. imgformat=Image::FORMAT_INDEXED_ALPHA;
  395. } else if (format=="bc1") {
  396. imgformat=Image::FORMAT_BC1;
  397. } else if (format=="bc2") {
  398. imgformat=Image::FORMAT_BC2;
  399. } else if (format=="bc3") {
  400. imgformat=Image::FORMAT_BC3;
  401. } else if (format=="bc4") {
  402. imgformat=Image::FORMAT_BC4;
  403. } else if (format=="bc5") {
  404. imgformat=Image::FORMAT_BC5;
  405. } else if (format=="pvrtc2") {
  406. imgformat=Image::FORMAT_PVRTC2;
  407. } else if (format=="pvrtc2a") {
  408. imgformat=Image::FORMAT_PVRTC2_ALPHA;
  409. } else if (format=="pvrtc4") {
  410. imgformat=Image::FORMAT_PVRTC4;
  411. } else if (format=="pvrtc4a") {
  412. imgformat=Image::FORMAT_PVRTC4_ALPHA;
  413. } else if (format=="etc") {
  414. imgformat=Image::FORMAT_ETC;
  415. } else if (format=="atc") {
  416. imgformat=Image::FORMAT_ATC;
  417. } else if (format=="atcai") {
  418. imgformat=Image::FORMAT_ATC_ALPHA_INTERPOLATED;
  419. } else if (format=="atcae") {
  420. imgformat=Image::FORMAT_ATC_ALPHA_EXPLICIT;
  421. } else if (format=="custom") {
  422. imgformat=Image::FORMAT_CUSTOM;
  423. } else {
  424. ERR_FAIL_V( ERR_FILE_CORRUPT );
  425. }
  426. int datasize;
  427. int w=width.to_int();
  428. int h=height.to_int();
  429. if (w == 0 && h == 0) {
  430. //r_v = Image(w, h, imgformat);
  431. r_v=Image();
  432. String sdfsdfg;
  433. Error err=parse_property_data(sdfsdfg);
  434. return OK;
  435. };
  436. if (imgformat==Image::FORMAT_CUSTOM) {
  437. datasize=custom_size;
  438. } else {
  439. datasize = Image::get_image_data_size(h,w,imgformat,mipmaps);
  440. }
  441. if (datasize==0) {
  442. //r_v = Image(w, h, imgformat);
  443. r_v=Image();
  444. String sdfsdfg;
  445. Error err=parse_property_data(sdfsdfg);
  446. return OK;
  447. };
  448. DVector<uint8_t> pixels;
  449. pixels.resize(datasize);
  450. DVector<uint8_t>::Write wb = pixels.write();
  451. int idx=0;
  452. uint8_t byte;
  453. while( idx<datasize*2) {
  454. CharType c=get_char();
  455. ERR_FAIL_COND_V(c=='<',ERR_FILE_CORRUPT);
  456. if ( (c>='0' && c<='9') || (c>='A' && c<='F') || (c>='a' && c<='f') ) {
  457. if (idx&1) {
  458. byte|=HEX2CHR(c);
  459. wb[idx>>1]=byte;
  460. } else {
  461. byte=HEX2CHR(c)<<4;
  462. }
  463. idx++;
  464. }
  465. }
  466. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  467. wb=DVector<uint8_t>::Write();
  468. r_v=Image(w,h,mipmaps,imgformat,pixels);
  469. String sdfsdfg;
  470. Error err=parse_property_data(sdfsdfg);
  471. ERR_FAIL_COND_V(err,err);
  472. return OK;
  473. }
  474. ERR_FAIL_V(ERR_FILE_CORRUPT);
  475. } else if (type=="raw_array") {
  476. if (!tag->args.has("len")) {
  477. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": RawArray missing 'len' field: "+name);
  478. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  479. }
  480. int len=tag->args["len"].to_int();
  481. DVector<uint8_t> bytes;
  482. bytes.resize(len);
  483. DVector<uint8_t>::Write w=bytes.write();
  484. uint8_t *bytesptr=w.ptr();
  485. int idx=0;
  486. uint8_t byte;
  487. while( idx<len*2) {
  488. CharType c=get_char();
  489. if (c<=32)
  490. continue;
  491. if (idx&1) {
  492. byte|=HEX2CHR(c);
  493. bytesptr[idx>>1]=byte;
  494. //printf("%x\n",int(byte));
  495. } else {
  496. byte=HEX2CHR(c)<<4;
  497. }
  498. idx++;
  499. }
  500. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  501. w=DVector<uint8_t>::Write();
  502. r_v=bytes;
  503. String sdfsdfg;
  504. Error err=parse_property_data(sdfsdfg);
  505. ERR_FAIL_COND_V(err,err);
  506. r_name=name;
  507. return OK;
  508. } else if (type=="int_array") {
  509. if (!tag->args.has("len")) {
  510. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  511. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  512. }
  513. int len=tag->args["len"].to_int();
  514. DVector<int> ints;
  515. ints.resize(len);
  516. DVector<int>::Write w=ints.write();
  517. int *intsptr=w.ptr();
  518. int idx=0;
  519. String str;
  520. #if 0
  521. while( idx<len ) {
  522. CharType c=get_char();
  523. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  524. if (c<33 || c==',' || c=='<') {
  525. if (str.length()) {
  526. intsptr[idx]=str.to_int();
  527. str="";
  528. idx++;
  529. }
  530. if (c=='<') {
  531. while(get_char()!='>' && !f->eof_reached()) {}
  532. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  533. break;
  534. }
  535. } else {
  536. str+=c;
  537. }
  538. }
  539. #else
  540. Vector<char> tmpdata;
  541. while( idx<len ) {
  542. bool end=false;
  543. Error err = _parse_array_element(tmpdata,true,f,&end);
  544. ERR_FAIL_COND_V(err,err);
  545. intsptr[idx]=String::to_int(&tmpdata[0]);
  546. idx++;
  547. if (end)
  548. break;
  549. }
  550. #endif
  551. w=DVector<int>::Write();
  552. r_v=ints;
  553. Error err=goto_end_of_tag();
  554. ERR_FAIL_COND_V(err,err);
  555. r_name=name;
  556. return OK;
  557. } else if (type=="real_array") {
  558. if (!tag->args.has("len")) {
  559. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  560. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  561. }
  562. int len=tag->args["len"].to_int();;
  563. DVector<real_t> reals;
  564. reals.resize(len);
  565. DVector<real_t>::Write w=reals.write();
  566. real_t *realsptr=w.ptr();
  567. int idx=0;
  568. String str;
  569. #if 0
  570. while( idx<len ) {
  571. CharType c=get_char();
  572. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  573. if (c<33 || c==',' || c=='<') {
  574. if (str.length()) {
  575. realsptr[idx]=str.to_double();
  576. str="";
  577. idx++;
  578. }
  579. if (c=='<') {
  580. while(get_char()!='>' && !f->eof_reached()) {}
  581. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  582. break;
  583. }
  584. } else {
  585. str+=c;
  586. }
  587. }
  588. #else
  589. Vector<char> tmpdata;
  590. while( idx<len ) {
  591. bool end=false;
  592. Error err = _parse_array_element(tmpdata,true,f,&end);
  593. ERR_FAIL_COND_V(err,err);
  594. realsptr[idx]=String::to_double(&tmpdata[0]);
  595. idx++;
  596. if (end)
  597. break;
  598. }
  599. #endif
  600. w=DVector<real_t>::Write();
  601. r_v=reals;
  602. Error err=goto_end_of_tag();
  603. ERR_FAIL_COND_V(err,err);
  604. r_name=name;
  605. return OK;
  606. } else if (type=="string_array") {
  607. #if 0
  608. if (!tag->args.has("len")) {
  609. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  610. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  611. }
  612. int len=tag->args["len"].to_int();
  613. DVector<String> strings;
  614. strings.resize(len);
  615. DVector<String>::Write w=strings.write();
  616. String *stringsptr=w.ptr();
  617. int idx=0;
  618. String str;
  619. bool inside_str=false;
  620. CharString cs;
  621. while( idx<len ) {
  622. CharType c=get_char();
  623. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  624. if (c=='"') {
  625. if (inside_str) {
  626. cs.push_back(0);
  627. String str;
  628. str.parse_utf8(cs.get_data());
  629. unquote(str);
  630. stringsptr[idx]=str;
  631. cs.clear();
  632. idx++;
  633. inside_str=false;
  634. } else {
  635. inside_str=true;
  636. }
  637. } else if (c=='<') {
  638. while(get_char()!='>' && !f->eof_reached()) {}
  639. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  640. break;
  641. } else if (inside_str){
  642. cs.push_back(c);
  643. }
  644. }
  645. w=DVector<String>::Write();
  646. r_v=strings;
  647. String sdfsdfg;
  648. Error err=parse_property_data(sdfsdfg);
  649. ERR_FAIL_COND_V(err,err);
  650. r_name=name;
  651. return OK;
  652. #endif
  653. if (!tag->args.has("len")) {
  654. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": String Array missing 'len' field: "+name);
  655. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  656. }
  657. int len=tag->args["len"].to_int();
  658. StringArray array;
  659. array.resize(len);
  660. DVector<String>::Write w = array.write();
  661. Error err;
  662. Variant v;
  663. String tagname;
  664. int idx=0;
  665. while( (err=parse_property(v,tagname))==OK ) {
  666. ERR_CONTINUE( idx <0 || idx >=len );
  667. String str = v; //convert back to string
  668. w[idx]=str;
  669. idx++;
  670. }
  671. if (idx!=len) {
  672. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array (size mismatch): "+name);
  673. ERR_FAIL_COND_V(idx!=len,err);
  674. }
  675. if (err!=ERR_FILE_EOF) {
  676. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Error loading array: "+name);
  677. ERR_FAIL_COND_V(err!=ERR_FILE_EOF,err);
  678. }
  679. //err=parse_property_data(name); // skip the rest
  680. //ERR_FAIL_COND_V(err,err);
  681. r_name=name;
  682. r_v=array;
  683. return OK;
  684. } else if (type=="vector3_array") {
  685. if (!tag->args.has("len")) {
  686. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  687. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  688. }
  689. int len=tag->args["len"].to_int();;
  690. DVector<Vector3> vectors;
  691. vectors.resize(len);
  692. DVector<Vector3>::Write w=vectors.write();
  693. Vector3 *vectorsptr=w.ptr();
  694. int idx=0;
  695. int subidx=0;
  696. Vector3 auxvec;
  697. String str;
  698. // uint64_t tbegin = OS::get_singleton()->get_ticks_usec();
  699. #if 0
  700. while( idx<len ) {
  701. CharType c=get_char();
  702. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  703. if (c<33 || c==',' || c=='<') {
  704. if (str.length()) {
  705. auxvec[subidx]=str.to_double();
  706. subidx++;
  707. str="";
  708. if (subidx==3) {
  709. vectorsptr[idx]=auxvec;
  710. idx++;
  711. subidx=0;
  712. }
  713. }
  714. if (c=='<') {
  715. while(get_char()!='>' && !f->eof_reached()) {}
  716. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  717. break;
  718. }
  719. } else {
  720. str+=c;
  721. }
  722. }
  723. #else
  724. Vector<char> tmpdata;
  725. while( idx<len ) {
  726. bool end=false;
  727. Error err = _parse_array_element(tmpdata,true,f,&end);
  728. ERR_FAIL_COND_V(err,err);
  729. auxvec[subidx]=String::to_double(&tmpdata[0]);
  730. subidx++;
  731. if (subidx==3) {
  732. vectorsptr[idx]=auxvec;
  733. idx++;
  734. subidx=0;
  735. }
  736. if (end)
  737. break;
  738. }
  739. #endif
  740. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Premature end of vector3 array");
  741. ERR_FAIL_COND_V(idx<len,ERR_FILE_CORRUPT);
  742. // double time_taken = (OS::get_singleton()->get_ticks_usec() - tbegin)/1000000.0;
  743. w=DVector<Vector3>::Write();
  744. r_v=vectors;
  745. String sdfsdfg;
  746. Error err=goto_end_of_tag();
  747. ERR_FAIL_COND_V(err,err);
  748. r_name=name;
  749. return OK;
  750. } else if (type=="vector2_array") {
  751. if (!tag->args.has("len")) {
  752. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  753. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  754. }
  755. int len=tag->args["len"].to_int();;
  756. DVector<Vector2> vectors;
  757. vectors.resize(len);
  758. DVector<Vector2>::Write w=vectors.write();
  759. Vector2 *vectorsptr=w.ptr();
  760. int idx=0;
  761. int subidx=0;
  762. Vector2 auxvec;
  763. String str;
  764. // uint64_t tbegin = OS::get_singleton()->get_ticks_usec();
  765. #if 0
  766. while( idx<len ) {
  767. CharType c=get_char();
  768. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  769. if (c<22 || c==',' || c=='<') {
  770. if (str.length()) {
  771. auxvec[subidx]=str.to_double();
  772. subidx++;
  773. str="";
  774. if (subidx==2) {
  775. vectorsptr[idx]=auxvec;
  776. idx++;
  777. subidx=0;
  778. }
  779. }
  780. if (c=='<') {
  781. while(get_char()!='>' && !f->eof_reached()) {}
  782. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  783. break;
  784. }
  785. } else {
  786. str+=c;
  787. }
  788. }
  789. #else
  790. Vector<char> tmpdata;
  791. while( idx<len ) {
  792. bool end=false;
  793. Error err = _parse_array_element(tmpdata,true,f,&end);
  794. ERR_FAIL_COND_V(err,err);
  795. auxvec[subidx]=String::to_double(&tmpdata[0]);
  796. subidx++;
  797. if (subidx==2) {
  798. vectorsptr[idx]=auxvec;
  799. idx++;
  800. subidx=0;
  801. }
  802. if (end)
  803. break;
  804. }
  805. #endif
  806. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Premature end of vector2 array");
  807. ERR_FAIL_COND_V(idx<len,ERR_FILE_CORRUPT);
  808. // double time_taken = (OS::get_singleton()->get_ticks_usec() - tbegin)/1000000.0;
  809. w=DVector<Vector2>::Write();
  810. r_v=vectors;
  811. String sdfsdfg;
  812. Error err=goto_end_of_tag();
  813. ERR_FAIL_COND_V(err,err);
  814. r_name=name;
  815. return OK;
  816. } else if (type=="color_array") {
  817. if (!tag->args.has("len")) {
  818. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Array missing 'len' field: "+name);
  819. ERR_FAIL_COND_V(!tag->args.has("len"),ERR_FILE_CORRUPT);
  820. }
  821. int len=tag->args["len"].to_int();;
  822. DVector<Color> colors;
  823. colors.resize(len);
  824. DVector<Color>::Write w=colors.write();
  825. Color *colorsptr=w.ptr();
  826. int idx=0;
  827. int subidx=0;
  828. Color auxcol;
  829. String str;
  830. while( idx<len ) {
  831. CharType c=get_char();
  832. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  833. if (c<33 || c==',' || c=='<') {
  834. if (str.length()) {
  835. auxcol[subidx]=str.to_double();
  836. subidx++;
  837. str="";
  838. if (subidx==4) {
  839. colorsptr[idx]=auxcol;
  840. idx++;
  841. subidx=0;
  842. }
  843. }
  844. if (c=='<') {
  845. while(get_char()!='>' && !f->eof_reached()) {}
  846. ERR_FAIL_COND_V(f->eof_reached(),ERR_FILE_CORRUPT);
  847. break;
  848. }
  849. } else {
  850. str+=c;
  851. }
  852. }
  853. w=DVector<Color>::Write();
  854. r_v=colors;
  855. String sdfsdfg;
  856. Error err=parse_property_data(sdfsdfg);
  857. ERR_FAIL_COND_V(err,err);
  858. r_name=name;
  859. return OK;
  860. }
  861. String data;
  862. Error err = parse_property_data(data);
  863. ERR_FAIL_COND_V(err!=OK,err);
  864. if (type=="nil") {
  865. // uh do nothing
  866. } else if (type=="bool") {
  867. // uh do nothing
  868. if (data.nocasecmp_to("true")==0 || data.to_int()!=0)
  869. r_v=true;
  870. else
  871. r_v=false;
  872. } else if (type=="int") {
  873. r_v=data.to_int();
  874. } else if (type=="real") {
  875. r_v=data.to_double();
  876. } else if (type=="string") {
  877. String str=data;
  878. unquote(str);
  879. r_v=str;
  880. } else if (type=="vector3") {
  881. r_v=Vector3(
  882. data.get_slice(",",0).to_double(),
  883. data.get_slice(",",1).to_double(),
  884. data.get_slice(",",2).to_double()
  885. );
  886. } else if (type=="vector2") {
  887. r_v=Vector2(
  888. data.get_slice(",",0).to_double(),
  889. data.get_slice(",",1).to_double()
  890. );
  891. } else if (type=="plane") {
  892. r_v=Plane(
  893. data.get_slice(",",0).to_double(),
  894. data.get_slice(",",1).to_double(),
  895. data.get_slice(",",2).to_double(),
  896. data.get_slice(",",3).to_double()
  897. );
  898. } else if (type=="quaternion") {
  899. r_v=Quat(
  900. data.get_slice(",",0).to_double(),
  901. data.get_slice(",",1).to_double(),
  902. data.get_slice(",",2).to_double(),
  903. data.get_slice(",",3).to_double()
  904. );
  905. } else if (type=="rect2") {
  906. r_v=Rect2(
  907. Vector2(
  908. data.get_slice(",",0).to_double(),
  909. data.get_slice(",",1).to_double()
  910. ),
  911. Vector2(
  912. data.get_slice(",",2).to_double(),
  913. data.get_slice(",",3).to_double()
  914. )
  915. );
  916. } else if (type=="aabb") {
  917. r_v=AABB(
  918. Vector3(
  919. data.get_slice(",",0).to_double(),
  920. data.get_slice(",",1).to_double(),
  921. data.get_slice(",",2).to_double()
  922. ),
  923. Vector3(
  924. data.get_slice(",",3).to_double(),
  925. data.get_slice(",",4).to_double(),
  926. data.get_slice(",",5).to_double()
  927. )
  928. );
  929. } else if (type=="matrix32") {
  930. Matrix32 m3;
  931. for (int i=0;i<3;i++) {
  932. for (int j=0;j<2;j++) {
  933. m3.elements[i][j]=data.get_slice(",",i*2+j).to_double();
  934. }
  935. }
  936. r_v=m3;
  937. } else if (type=="matrix3") {
  938. Matrix3 m3;
  939. for (int i=0;i<3;i++) {
  940. for (int j=0;j<3;j++) {
  941. m3.elements[i][j]=data.get_slice(",",i*3+j).to_double();
  942. }
  943. }
  944. r_v=m3;
  945. } else if (type=="transform") {
  946. Transform tr;
  947. for (int i=0;i<3;i++) {
  948. for (int j=0;j<3;j++) {
  949. tr.basis.elements[i][j]=data.get_slice(",",i*3+j).to_double();
  950. }
  951. }
  952. tr.origin=Vector3(
  953. data.get_slice(",",9).to_double(),
  954. data.get_slice(",",10).to_double(),
  955. data.get_slice(",",11).to_double()
  956. );
  957. r_v=tr;
  958. } else if (type=="color") {
  959. r_v=Color(
  960. data.get_slice(",",0).to_double(),
  961. data.get_slice(",",1).to_double(),
  962. data.get_slice(",",2).to_double(),
  963. data.get_slice(",",3).to_double()
  964. );
  965. } else if (type=="node_path") {
  966. String str=data;
  967. unquote(str);
  968. r_v=NodePath( str );
  969. } else if (type=="input_event") {
  970. // ?
  971. } else {
  972. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unrecognized tag in file: "+type);
  973. ERR_FAIL_V(ERR_FILE_CORRUPT);
  974. }
  975. r_name=name;
  976. return OK;
  977. }
  978. int ResourceInteractiveLoaderXML::get_current_line() const {
  979. return lines;
  980. }
  981. uint8_t ResourceInteractiveLoaderXML::get_char() const {
  982. uint8_t c = f->get_8();
  983. if (c=='\n')
  984. lines++;
  985. return c;
  986. }
  987. ///
  988. void ResourceInteractiveLoaderXML::set_local_path(const String& p_local_path) {
  989. res_path=p_local_path;
  990. }
  991. Ref<Resource> ResourceInteractiveLoaderXML::get_resource() {
  992. return resource;
  993. }
  994. Error ResourceInteractiveLoaderXML::poll() {
  995. if (error!=OK)
  996. return error;
  997. if (ext_resources.size()) {
  998. error=ERR_FILE_CORRUPT;
  999. String path=ext_resources.front()->get();
  1000. RES res = ResourceLoader::load(path);
  1001. if (res.is_null()) {
  1002. if (ResourceLoader::get_abort_on_missing_resources()) {
  1003. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": editor exported nonexistent resource at: "+path);
  1004. ERR_FAIL_V(error);
  1005. } else {
  1006. ResourceLoader::notify_load_error("Resource Not Found: "+path);
  1007. }
  1008. } else {
  1009. resource_cache.push_back(res);
  1010. }
  1011. error=OK;
  1012. ext_resources.pop_front();
  1013. resource_current++;
  1014. return error;
  1015. }
  1016. bool exit;
  1017. Tag *tag = parse_tag(&exit);
  1018. if (!tag) {
  1019. error=ERR_FILE_CORRUPT;
  1020. if (!exit) // shouldn't have exited
  1021. ERR_FAIL_V(error);
  1022. error=ERR_FILE_EOF;
  1023. return error;
  1024. }
  1025. RES res;
  1026. //Object *obj=NULL;
  1027. bool main;
  1028. if (tag->name=="ext_resource") {
  1029. error=ERR_FILE_CORRUPT;
  1030. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field.");
  1031. ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT);
  1032. String type;
  1033. if (tag->args.has("type"))
  1034. type=tag->args["type"];
  1035. String path = tag->args["path"];
  1036. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?.");
  1037. ERR_FAIL_COND_V(path.begins_with("local://"),ERR_FILE_CORRUPT);
  1038. if (path.find("://")==-1 && path.is_rel_path()) {
  1039. // path is relative to file being loaded, so convert to a resource path
  1040. path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  1041. }
  1042. RES res = ResourceLoader::load(path,type);
  1043. if (res.is_null()) {
  1044. if (ResourceLoader::get_abort_on_missing_resources()) {
  1045. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> referenced nonexistent resource at: "+path);
  1046. ERR_FAIL_V(error);
  1047. } else {
  1048. ResourceLoader::notify_load_error("Resource Not Found: "+path);
  1049. }
  1050. } else {
  1051. resource_cache.push_back(res);
  1052. }
  1053. Error err = close_tag("ext_resource");
  1054. if (err)
  1055. return error;
  1056. error=OK;
  1057. resource_current++;
  1058. return error;
  1059. } else if (tag->name=="resource") {
  1060. main=false;
  1061. } else if (tag->name=="main_resource") {
  1062. main=true;
  1063. } else {
  1064. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": unexpected main tag: "+tag->name);
  1065. error=ERR_FILE_CORRUPT;
  1066. ERR_FAIL_V(error);
  1067. }
  1068. String type;
  1069. String path;
  1070. if (!main) {
  1071. //loading resource
  1072. error=ERR_FILE_CORRUPT;
  1073. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'len' field.");
  1074. ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT);
  1075. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'type' field.");
  1076. ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT);
  1077. path=tag->args["path"];
  1078. error=OK;
  1079. if (path.begins_with("local://")) {
  1080. //built-in resource (but really external)
  1081. path=path.replace("local://",local_path+"::");
  1082. }
  1083. if (ResourceCache::has(path)) {
  1084. Error err = close_tag(tag->name);
  1085. if (err) {
  1086. error=ERR_FILE_CORRUPT;
  1087. }
  1088. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unable to close <resource> tag.");
  1089. ERR_FAIL_COND_V( err, err );
  1090. resource_current++;
  1091. error=OK;
  1092. return OK;
  1093. }
  1094. type = tag->args["type"];
  1095. } else {
  1096. type=resource_type;
  1097. }
  1098. Object *obj = ObjectTypeDB::instance(type);
  1099. if (!obj) {
  1100. error=ERR_FILE_CORRUPT;
  1101. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object of unrecognized type in file: "+type);
  1102. }
  1103. ERR_FAIL_COND_V(!obj,ERR_FILE_CORRUPT);
  1104. Resource *r = obj->cast_to<Resource>();
  1105. if (!r) {
  1106. error=ERR_FILE_CORRUPT;
  1107. memdelete(obj); //bye
  1108. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object type in resource field not a resource, type is: "+obj->get_type());
  1109. ERR_FAIL_COND_V(!r,ERR_FILE_CORRUPT);
  1110. }
  1111. res = RES( r );
  1112. if (path!="")
  1113. r->set_path(path);
  1114. //load properties
  1115. while(true) {
  1116. String name;
  1117. Variant v;
  1118. Error err;
  1119. err = parse_property(v,name);
  1120. if (err==ERR_FILE_EOF) //tag closed
  1121. break;
  1122. if (err!=OK) {
  1123. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": XML Parsing aborted.");
  1124. ERR_FAIL_COND_V(err!=OK,ERR_FILE_CORRUPT);
  1125. }
  1126. obj->set(name,v);
  1127. }
  1128. #ifdef TOOLS_ENABLED
  1129. res->set_edited(false);
  1130. #endif
  1131. resource_cache.push_back(res); //keep it in mem until finished loading
  1132. resource_current++;
  1133. if (main) {
  1134. f->close();
  1135. resource=res;
  1136. resource->set_path(res_path);
  1137. error=ERR_FILE_EOF;
  1138. return error;
  1139. }
  1140. error=OK;
  1141. return OK;
  1142. }
  1143. int ResourceInteractiveLoaderXML::get_stage() const {
  1144. return resource_current;
  1145. }
  1146. int ResourceInteractiveLoaderXML::get_stage_count() const {
  1147. return resources_total+ext_resources.size();
  1148. }
  1149. ResourceInteractiveLoaderXML::~ResourceInteractiveLoaderXML() {
  1150. memdelete(f);
  1151. }
  1152. void ResourceInteractiveLoaderXML::get_dependencies(FileAccess *f,List<String> *p_dependencies) {
  1153. open(f);
  1154. ERR_FAIL_COND(error!=OK);
  1155. while(true) {
  1156. bool exit;
  1157. Tag *tag = parse_tag(&exit);
  1158. if (!tag) {
  1159. error=ERR_FILE_CORRUPT;
  1160. ERR_FAIL_COND(!exit);
  1161. error=ERR_FILE_EOF;
  1162. return;
  1163. }
  1164. if (tag->name!="ext_resource") {
  1165. return;
  1166. }
  1167. error=ERR_FILE_CORRUPT;
  1168. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field.");
  1169. ERR_FAIL_COND(!tag->args.has("path"));
  1170. String path = tag->args["path"];
  1171. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?.");
  1172. ERR_FAIL_COND(path.begins_with("local://"));
  1173. if (path.find("://")==-1 && path.is_rel_path()) {
  1174. // path is relative to file being loaded, so convert to a resource path
  1175. path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  1176. }
  1177. if (path.ends_with("*")) {
  1178. ERR_FAIL_COND(!tag->args.has("type"));
  1179. String type = tag->args["type"];
  1180. path = ResourceLoader::guess_full_filename(path,type);
  1181. }
  1182. p_dependencies->push_back(path);
  1183. Error err = close_tag("ext_resource");
  1184. if (err)
  1185. return;
  1186. error=OK;
  1187. }
  1188. }
  1189. void ResourceInteractiveLoaderXML::open(FileAccess *p_f) {
  1190. error=OK;
  1191. lines=1;
  1192. f=p_f;
  1193. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1194. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1195. error=ERR_FILE_CORRUPT;
  1196. ResourceLoader::notify_load_error("XML is invalid (missing header tags)");
  1197. ERR_EXPLAIN("Not a XML:UTF-8 File: "+local_path);
  1198. ERR_FAIL();
  1199. }
  1200. tag_stack.clear();
  1201. tag = parse_tag();
  1202. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1203. ResourceLoader::notify_load_error(local_path+": XML is not a valid resource file.");
  1204. error=ERR_FILE_CORRUPT;
  1205. ERR_EXPLAIN("Unrecognized XML File: "+local_path);
  1206. ERR_FAIL();
  1207. }
  1208. if (tag->args.has("subresource_count"))
  1209. resources_total=tag->args["subresource_count"].to_int();
  1210. resource_current=0;
  1211. resource_type=tag->args["type"];
  1212. String version = tag->args["version"];
  1213. if (version.get_slice_count(".")!=2) {
  1214. error=ERR_FILE_CORRUPT;
  1215. ResourceLoader::notify_load_error(local_path+":XML version string is invalid: "+version);
  1216. ERR_EXPLAIN("Invalid Version String '"+version+"'' in file: "+local_path);
  1217. ERR_FAIL();
  1218. }
  1219. int major = version.get_slice(".",0).to_int();
  1220. int minor = version.get_slice(".",1).to_int();
  1221. if (major>VERSION_MAJOR) {
  1222. error=ERR_FILE_UNRECOGNIZED;
  1223. ResourceLoader::notify_load_error(local_path+": File Format '"+version+"' is too new. Please upgrade to a newer engine version.");
  1224. ERR_EXPLAIN("File Format '"+version+"' is too new! Please upgrade to a a new engine version: "+local_path);
  1225. ERR_FAIL();
  1226. }
  1227. String preload_depts = "deps/"+local_path.md5_text();
  1228. if (Globals::get_singleton()->has(preload_depts)) {
  1229. ext_resources.clear();
  1230. //ignore external resources and use these
  1231. NodePath depts=Globals::get_singleton()->get(preload_depts);
  1232. for(int i=0;i<depts.get_name_count();i++) {
  1233. ext_resources.push_back(depts.get_name(i));
  1234. }
  1235. print_line(local_path+" - EXTERNAL RESOURCES: "+itos(ext_resources.size()));
  1236. }
  1237. }
  1238. String ResourceInteractiveLoaderXML::recognize(FileAccess *p_f) {
  1239. error=OK;
  1240. lines=1;
  1241. f=p_f;
  1242. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1243. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1244. return ""; //unrecognized
  1245. }
  1246. tag_stack.clear();
  1247. tag = parse_tag();
  1248. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1249. return ""; //unrecognized
  1250. }
  1251. return tag->args["type"];
  1252. }
  1253. /////////////////////
  1254. Ref<ResourceInteractiveLoader> ResourceFormatLoaderXML::load_interactive(const String &p_path) {
  1255. Error err;
  1256. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  1257. if (err!=OK) {
  1258. ERR_FAIL_COND_V(err!=OK,Ref<ResourceInteractiveLoader>());
  1259. }
  1260. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1261. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1262. ria->res_path=ria->local_path;
  1263. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1264. ria->open(f);
  1265. return ria;
  1266. }
  1267. void ResourceFormatLoaderXML::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const {
  1268. if (p_type=="") {
  1269. get_recognized_extensions(p_extensions);
  1270. return;
  1271. }
  1272. List<String> extensions;
  1273. ObjectTypeDB::get_extensions_for_type(p_type,&extensions);
  1274. extensions.sort();
  1275. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1276. String ext = E->get().to_lower();
  1277. if (ext=="res")
  1278. continue;
  1279. p_extensions->push_back("x"+ext);
  1280. p_extensions->push_back(ext);
  1281. }
  1282. p_extensions->push_back("xml");
  1283. }
  1284. void ResourceFormatLoaderXML::get_recognized_extensions(List<String> *p_extensions) const{
  1285. List<String> extensions;
  1286. ObjectTypeDB::get_resource_base_extensions(&extensions);
  1287. extensions.sort();
  1288. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1289. String ext = E->get().to_lower();
  1290. if (ext=="res")
  1291. continue;
  1292. p_extensions->push_back("x"+ext);
  1293. }
  1294. p_extensions->push_back("xml");
  1295. }
  1296. bool ResourceFormatLoaderXML::handles_type(const String& p_type) const{
  1297. return true;
  1298. }
  1299. String ResourceFormatLoaderXML::get_resource_type(const String &p_path) const{
  1300. String ext=p_path.extension().to_lower();
  1301. if (!ext.begins_with("x")) //a lie but..
  1302. return "";
  1303. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1304. if (!f) {
  1305. return ""; //could not rwead
  1306. }
  1307. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1308. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1309. ria->res_path=ria->local_path;
  1310. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1311. String r = ria->recognize(f);
  1312. return r;
  1313. }
  1314. void ResourceFormatLoaderXML::get_dependencies(const String& p_path,List<String> *p_dependencies) {
  1315. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1316. if (!f) {
  1317. ERR_FAIL();
  1318. }
  1319. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1320. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1321. ria->res_path=ria->local_path;
  1322. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1323. ria->get_dependencies(f,p_dependencies);
  1324. }
  1325. /****************************************************************************************/
  1326. /****************************************************************************************/
  1327. /****************************************************************************************/
  1328. /****************************************************************************************/
  1329. /****************************************************************************************/
  1330. /****************************************************************************************/
  1331. /****************************************************************************************/
  1332. /****************************************************************************************/
  1333. /****************************************************************************************/
  1334. /****************************************************************************************/
  1335. /****************************************************************************************/
  1336. /****************************************************************************************/
  1337. /****************************************************************************************/
  1338. void ResourceFormatSaverXMLInstance::escape(String& p_str) {
  1339. p_str=p_str.replace("&","&amp;");
  1340. p_str=p_str.replace("<","&gt;");
  1341. p_str=p_str.replace(">","&lt;");
  1342. p_str=p_str.replace("'","&apos;");
  1343. p_str=p_str.replace("\"","&quot;");
  1344. for (char i=1;i<32;i++) {
  1345. char chr[2]={i,0};
  1346. const char hexn[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  1347. const char hex[8]={'&','#','0','0',hexn[i>>4],hexn[i&0xf],';',0};
  1348. p_str=p_str.replace(chr,hex);
  1349. }
  1350. }
  1351. void ResourceFormatSaverXMLInstance::write_tabs(int p_diff) {
  1352. for (int i=0;i<depth+p_diff;i++) {
  1353. f->store_8('\t');
  1354. }
  1355. }
  1356. void ResourceFormatSaverXMLInstance::write_string(String p_str,bool p_escape) {
  1357. /* write an UTF8 string */
  1358. if (p_escape)
  1359. escape(p_str);
  1360. f->store_string(p_str);;
  1361. /*
  1362. CharString cs=p_str.utf8();
  1363. const char *data=cs.get_data();
  1364. while (*data) {
  1365. f->store_8(*data);
  1366. data++;
  1367. }*/
  1368. }
  1369. void ResourceFormatSaverXMLInstance::enter_tag(const char* p_tag,const String& p_args) {
  1370. f->store_8('<');
  1371. int cc = 0;
  1372. const char *c=p_tag;
  1373. while(*c) {
  1374. cc++;
  1375. c++;
  1376. }
  1377. f->store_buffer((const uint8_t*)p_tag,cc);
  1378. if (p_args.length()) {
  1379. f->store_8(' ');
  1380. f->store_string(p_args);
  1381. }
  1382. f->store_8('>');
  1383. depth++;
  1384. }
  1385. void ResourceFormatSaverXMLInstance::exit_tag(const char* p_tag) {
  1386. depth--;
  1387. f->store_8('<');
  1388. f->store_8('/');
  1389. int cc = 0;
  1390. const char *c=p_tag;
  1391. while(*c) {
  1392. cc++;
  1393. c++;
  1394. }
  1395. f->store_buffer((const uint8_t*)p_tag,cc);
  1396. f->store_8('>');
  1397. }
  1398. /*
  1399. static bool _check_type(const Variant& p_property) {
  1400. if (p_property.get_type()==Variant::_RID)
  1401. return false;
  1402. if (p_property.get_type()==Variant::OBJECT) {
  1403. RES res = p_property;
  1404. if (res.is_null())
  1405. return false;
  1406. }
  1407. return true;
  1408. }*/
  1409. void ResourceFormatSaverXMLInstance::write_property(const String& p_name,const Variant& p_property,bool *r_ok) {
  1410. if (r_ok)
  1411. *r_ok=false;
  1412. const char* type;
  1413. String params;
  1414. bool oneliner=true;
  1415. switch( p_property.get_type() ) {
  1416. case Variant::NIL: type="nil"; break;
  1417. case Variant::BOOL: type="bool"; break;
  1418. case Variant::INT: type="int"; break;
  1419. case Variant::REAL: type="real"; break;
  1420. case Variant::STRING: type="string"; break;
  1421. case Variant::VECTOR2: type="vector2"; break;
  1422. case Variant::RECT2: type="rect2"; break;
  1423. case Variant::VECTOR3: type="vector3"; break;
  1424. case Variant::PLANE: type="plane"; break;
  1425. case Variant::_AABB: type="aabb"; break;
  1426. case Variant::QUAT: type="quaternion"; break;
  1427. case Variant::MATRIX32: type="matrix32"; break;
  1428. case Variant::MATRIX3: type="matrix3"; break;
  1429. case Variant::TRANSFORM: type="transform"; break;
  1430. case Variant::COLOR: type="color"; break;
  1431. case Variant::IMAGE: {
  1432. type="image";
  1433. Image img=p_property;
  1434. if (img.empty()) {
  1435. write_tabs();
  1436. enter_tag(type,"name=\""+p_name+"\"");
  1437. exit_tag(type);
  1438. if (r_ok)
  1439. *r_ok=true;
  1440. return;
  1441. }
  1442. params+="encoding=\"raw\"";
  1443. params+=" width=\""+itos(img.get_width())+"\"";
  1444. params+=" height=\""+itos(img.get_height())+"\"";
  1445. params+=" mipmaps=\""+itos(img.get_mipmaps())+"\"";
  1446. switch(img.get_format()) {
  1447. case Image::FORMAT_GRAYSCALE: params+=" format=\"grayscale\""; break;
  1448. case Image::FORMAT_INTENSITY: params+=" format=\"intensity\""; break;
  1449. case Image::FORMAT_GRAYSCALE_ALPHA: params+=" format=\"grayscale_alpha\""; break;
  1450. case Image::FORMAT_RGB: params+=" format=\"rgb\""; break;
  1451. case Image::FORMAT_RGBA: params+=" format=\"rgba\""; break;
  1452. case Image::FORMAT_INDEXED : params+=" format=\"indexed\""; break;
  1453. case Image::FORMAT_INDEXED_ALPHA: params+=" format=\"indexed_alpha\""; break;
  1454. case Image::FORMAT_BC1: params+=" format=\"bc1\""; break;
  1455. case Image::FORMAT_BC2: params+=" format=\"bc2\""; break;
  1456. case Image::FORMAT_BC3: params+=" format=\"bc3\""; break;
  1457. case Image::FORMAT_BC4: params+=" format=\"bc4\""; break;
  1458. case Image::FORMAT_BC5: params+=" format=\"bc5\""; break;
  1459. case Image::FORMAT_PVRTC2: params+=" format=\"pvrtc2\""; break;
  1460. case Image::FORMAT_PVRTC2_ALPHA: params+=" format=\"pvrtc2a\""; break;
  1461. case Image::FORMAT_PVRTC4: params+=" format=\"pvrtc4\""; break;
  1462. case Image::FORMAT_PVRTC4_ALPHA: params+=" format=\"pvrtc4a\""; break;
  1463. case Image::FORMAT_ETC: params+=" format=\"etc\""; break;
  1464. case Image::FORMAT_ATC: params+=" format=\"atc\""; break;
  1465. case Image::FORMAT_ATC_ALPHA_EXPLICIT: params+=" format=\"atcae\""; break;
  1466. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: params+=" format=\"atcai\""; break;
  1467. case Image::FORMAT_CUSTOM: params+=" format=\"custom\" custom_size=\""+itos(img.get_data().size())+"\""; break;
  1468. default: {}
  1469. }
  1470. } break;
  1471. case Variant::NODE_PATH: type="node_path"; break;
  1472. case Variant::OBJECT: {
  1473. type="resource";
  1474. RES res = p_property;
  1475. if (res.is_null()) {
  1476. write_tabs();
  1477. enter_tag(type,"name=\""+p_name+"\"");
  1478. exit_tag(type);
  1479. if (r_ok)
  1480. *r_ok=true;
  1481. return; // don't save it
  1482. }
  1483. params="resource_type=\""+res->get_save_type()+"\"";
  1484. if (res->get_path().length() && res->get_path().find("::")==-1) {
  1485. //external resource
  1486. String path=relative_paths?local_path.path_to_file(res->get_path()):res->get_path();
  1487. escape(path);
  1488. params+=" path=\""+path+"\"";
  1489. } else {
  1490. //internal resource
  1491. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1492. ERR_FAIL_COND(!resource_map.has(res));
  1493. params+=" path=\"local://"+itos(resource_map[res])+"\"";
  1494. }
  1495. } break;
  1496. case Variant::INPUT_EVENT: type="input_event"; break;
  1497. case Variant::DICTIONARY: type="dictionary"; params="shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1498. case Variant::ARRAY: type="array"; params="len=\""+itos(p_property.operator Array().size())+"\" shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1499. case Variant::RAW_ARRAY: type="raw_array"; params="len=\""+itos(p_property.operator DVector < uint8_t >().size())+"\""; break;
  1500. case Variant::INT_ARRAY: type="int_array"; params="len=\""+itos(p_property.operator DVector < int >().size())+"\""; break;
  1501. case Variant::REAL_ARRAY: type="real_array"; params="len=\""+itos(p_property.operator DVector < real_t >().size())+"\""; break;
  1502. case Variant::STRING_ARRAY: oneliner=false; type="string_array"; params="len=\""+itos(p_property.operator DVector < String >().size())+"\""; break;
  1503. case Variant::VECTOR2_ARRAY: type="vector2_array"; params="len=\""+itos(p_property.operator DVector < Vector2 >().size())+"\""; break;
  1504. case Variant::VECTOR3_ARRAY: type="vector3_array"; params="len=\""+itos(p_property.operator DVector < Vector3 >().size())+"\""; break;
  1505. case Variant::COLOR_ARRAY: type="color_array"; params="len=\""+itos(p_property.operator DVector < Color >().size())+"\""; break;
  1506. default: {
  1507. ERR_PRINT("Unknown Variant type.");
  1508. ERR_FAIL();
  1509. }
  1510. }
  1511. write_tabs();
  1512. if (p_name!="") {
  1513. if (params.length())
  1514. enter_tag(type,"name=\""+p_name+"\" "+params);
  1515. else
  1516. enter_tag(type,"name=\""+p_name+"\"");
  1517. } else {
  1518. if (params.length())
  1519. enter_tag(type," "+params);
  1520. else
  1521. enter_tag(type,String());
  1522. }
  1523. if (!oneliner)
  1524. f->store_8('\n');
  1525. else
  1526. f->store_8(' ');
  1527. switch( p_property.get_type() ) {
  1528. case Variant::NIL: {
  1529. } break;
  1530. case Variant::BOOL: {
  1531. write_string( p_property.operator bool() ? "True":"False" );
  1532. } break;
  1533. case Variant::INT: {
  1534. write_string( itos(p_property.operator int()) );
  1535. } break;
  1536. case Variant::REAL: {
  1537. write_string( rtos(p_property.operator real_t()) );
  1538. } break;
  1539. case Variant::STRING: {
  1540. String str=p_property;
  1541. escape(str);
  1542. str="\""+str+"\"";
  1543. write_string( str,false );
  1544. } break;
  1545. case Variant::VECTOR2: {
  1546. Vector2 v = p_property;
  1547. write_string( rtoss(v.x) +", "+rtoss(v.y) );
  1548. } break;
  1549. case Variant::RECT2: {
  1550. Rect2 aabb = p_property;
  1551. write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) );
  1552. } break;
  1553. case Variant::VECTOR3: {
  1554. Vector3 v = p_property;
  1555. write_string( rtoss(v.x) +", "+rtoss(v.y)+", "+rtoss(v.z) );
  1556. } break;
  1557. case Variant::PLANE: {
  1558. Plane p = p_property;
  1559. write_string( rtoss(p.normal.x) +", "+rtoss(p.normal.y)+", "+rtoss(p.normal.z)+", "+rtoss(p.d) );
  1560. } break;
  1561. case Variant::_AABB: {
  1562. AABB aabb = p_property;
  1563. write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.pos.z) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) +", "+rtoss(aabb.size.z) );
  1564. } break;
  1565. case Variant::QUAT: {
  1566. Quat quat = p_property;
  1567. write_string( rtoss(quat.x)+", "+rtoss(quat.y)+", "+rtoss(quat.z)+", "+rtoss(quat.w)+", ");
  1568. } break;
  1569. case Variant::MATRIX32: {
  1570. String s;
  1571. Matrix32 m3 = p_property;
  1572. for (int i=0;i<3;i++) {
  1573. for (int j=0;j<2;j++) {
  1574. if (i!=0 || j!=0)
  1575. s+=", ";
  1576. s+=rtoss( m3.elements[i][j] );
  1577. }
  1578. }
  1579. write_string(s);
  1580. } break;
  1581. case Variant::MATRIX3: {
  1582. String s;
  1583. Matrix3 m3 = p_property;
  1584. for (int i=0;i<3;i++) {
  1585. for (int j=0;j<3;j++) {
  1586. if (i!=0 || j!=0)
  1587. s+=", ";
  1588. s+=rtoss( m3.elements[i][j] );
  1589. }
  1590. }
  1591. write_string(s);
  1592. } break;
  1593. case Variant::TRANSFORM: {
  1594. String s;
  1595. Transform t = p_property;
  1596. Matrix3 &m3 = t.basis;
  1597. for (int i=0;i<3;i++) {
  1598. for (int j=0;j<3;j++) {
  1599. if (i!=0 || j!=0)
  1600. s+=", ";
  1601. s+=rtoss( m3.elements[i][j] );
  1602. }
  1603. }
  1604. s=s+", "+rtoss(t.origin.x) +", "+rtoss(t.origin.y)+", "+rtoss(t.origin.z);
  1605. write_string(s);
  1606. } break;
  1607. // misc types
  1608. case Variant::COLOR: {
  1609. Color c = p_property;
  1610. write_string( rtoss(c.r) +", "+rtoss(c.g)+", "+rtoss(c.b)+", "+rtoss(c.a) );
  1611. } break;
  1612. case Variant::IMAGE: {
  1613. String s;
  1614. Image img = p_property;
  1615. DVector<uint8_t> data = img.get_data();
  1616. int len = data.size();
  1617. DVector<uint8_t>::Read r = data.read();
  1618. const uint8_t *ptr=r.ptr();;
  1619. for (int i=0;i<len;i++) {
  1620. uint8_t byte = ptr[i];
  1621. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1622. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1623. s+=str;
  1624. }
  1625. write_string(s);
  1626. } break;
  1627. case Variant::NODE_PATH: {
  1628. String str=p_property;
  1629. escape(str);
  1630. str="\""+str+"\"";
  1631. write_string( str,false);
  1632. } break;
  1633. case Variant::OBJECT: {
  1634. /* this saver does not save resources in here
  1635. RES res = p_property;
  1636. if (!res.is_null()) {
  1637. String path=res->get_path();
  1638. if (!res->is_shared() || !path.length()) {
  1639. // if no path, or path is from inside a scene
  1640. write_object( *res );
  1641. }
  1642. }
  1643. */
  1644. } break;
  1645. case Variant::INPUT_EVENT: {
  1646. write_string( p_property.operator String() );
  1647. } break;
  1648. case Variant::DICTIONARY: {
  1649. Dictionary dict = p_property;
  1650. List<Variant> keys;
  1651. dict.get_key_list(&keys);
  1652. keys.sort();
  1653. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1654. //if (!_check_type(dict[E->get()]))
  1655. // continue;
  1656. bool ok;
  1657. write_property("",E->get(),&ok);
  1658. ERR_CONTINUE(!ok);
  1659. write_property("",dict[E->get()],&ok);
  1660. if (!ok)
  1661. write_property("",Variant()); //at least make the file consistent..
  1662. }
  1663. } break;
  1664. case Variant::ARRAY: {
  1665. Array array = p_property;
  1666. int len=array.size();
  1667. for (int i=0;i<len;i++) {
  1668. write_property("",array[i]);
  1669. }
  1670. } break;
  1671. case Variant::RAW_ARRAY: {
  1672. String s;
  1673. DVector<uint8_t> data = p_property;
  1674. int len = data.size();
  1675. DVector<uint8_t>::Read r = data.read();
  1676. const uint8_t *ptr=r.ptr();;
  1677. for (int i=0;i<len;i++) {
  1678. uint8_t byte = ptr[i];
  1679. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1680. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1681. s+=str;
  1682. }
  1683. write_string(s,false);
  1684. } break;
  1685. case Variant::INT_ARRAY: {
  1686. DVector<int> data = p_property;
  1687. int len = data.size();
  1688. DVector<int>::Read r = data.read();
  1689. const int *ptr=r.ptr();;
  1690. write_tabs();
  1691. for (int i=0;i<len;i++) {
  1692. if (i>0)
  1693. write_string(", ",false);
  1694. write_string(itos(ptr[i]),false);
  1695. }
  1696. } break;
  1697. case Variant::REAL_ARRAY: {
  1698. DVector<real_t> data = p_property;
  1699. int len = data.size();
  1700. DVector<real_t>::Read r = data.read();
  1701. const real_t *ptr=r.ptr();;
  1702. write_tabs();
  1703. String cm=", " ;
  1704. for (int i=0;i<len;i++) {
  1705. if (i>0)
  1706. write_string(cm,false);
  1707. write_string(rtoss(ptr[i]),false);
  1708. }
  1709. } break;
  1710. case Variant::STRING_ARRAY: {
  1711. DVector<String> data = p_property;
  1712. int len = data.size();
  1713. DVector<String>::Read r = data.read();
  1714. const String *ptr=r.ptr();;
  1715. String s;
  1716. //write_string("\n");
  1717. for (int i=0;i<len;i++) {
  1718. write_tabs(0);
  1719. String str=ptr[i];
  1720. escape(str);
  1721. write_string("<string> \""+str+"\" </string>\n",false);
  1722. }
  1723. } break;
  1724. case Variant::VECTOR2_ARRAY: {
  1725. DVector<Vector2> data = p_property;
  1726. int len = data.size();
  1727. DVector<Vector2>::Read r = data.read();
  1728. const Vector2 *ptr=r.ptr();;
  1729. write_tabs();
  1730. for (int i=0;i<len;i++) {
  1731. if (i>0)
  1732. write_string(", ",false);
  1733. write_string(rtoss(ptr[i].x),false);
  1734. write_string(", "+rtoss(ptr[i].y),false);
  1735. }
  1736. } break;
  1737. case Variant::VECTOR3_ARRAY: {
  1738. DVector<Vector3> data = p_property;
  1739. int len = data.size();
  1740. DVector<Vector3>::Read r = data.read();
  1741. const Vector3 *ptr=r.ptr();;
  1742. write_tabs();
  1743. for (int i=0;i<len;i++) {
  1744. if (i>0)
  1745. write_string(", ",false);
  1746. write_string(rtoss(ptr[i].x),false);
  1747. write_string(", "+rtoss(ptr[i].y),false);
  1748. write_string(", "+rtoss(ptr[i].z),false);
  1749. }
  1750. } break;
  1751. case Variant::COLOR_ARRAY: {
  1752. DVector<Color> data = p_property;
  1753. int len = data.size();
  1754. DVector<Color>::Read r = data.read();
  1755. const Color *ptr=r.ptr();;
  1756. write_tabs();
  1757. for (int i=0;i<len;i++) {
  1758. if (i>0)
  1759. write_string(", ",false);
  1760. write_string(rtoss(ptr[i].r),false);
  1761. write_string(", "+rtoss(ptr[i].g),false);
  1762. write_string(", "+rtoss(ptr[i].b),false);
  1763. write_string(", "+rtoss(ptr[i].a),false);
  1764. }
  1765. } break;
  1766. default: {}
  1767. }
  1768. if (oneliner)
  1769. f->store_8(' ');
  1770. else
  1771. write_tabs(-1);
  1772. exit_tag(type);
  1773. f->store_8('\n');
  1774. if (r_ok)
  1775. *r_ok=true;
  1776. }
  1777. void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bool p_main) {
  1778. switch(p_variant.get_type()) {
  1779. case Variant::OBJECT: {
  1780. RES res = p_variant.operator RefPtr();
  1781. if (res.is_null())
  1782. return;
  1783. if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) {
  1784. external_resources.push_back(res);
  1785. return;
  1786. }
  1787. if (resource_map.has(res))
  1788. return;
  1789. List<PropertyInfo> property_list;
  1790. res->get_property_list( &property_list );
  1791. property_list.sort();
  1792. List<PropertyInfo>::Element *I=property_list.front();
  1793. while(I) {
  1794. PropertyInfo pi=I->get();
  1795. if (pi.usage&PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage&PROPERTY_USAGE_BUNDLE)) {
  1796. Variant v=res->get(I->get().name);
  1797. _find_resources(v);
  1798. }
  1799. I=I->next();
  1800. }
  1801. resource_map[ res ] = resource_map.size(); //saved after, so the childs it needs are available when loaded
  1802. saved_resources.push_back(res);
  1803. } break;
  1804. case Variant::ARRAY: {
  1805. Array varray=p_variant;
  1806. int len=varray.size();
  1807. for(int i=0;i<len;i++) {
  1808. Variant v=varray.get(i);
  1809. _find_resources(v);
  1810. }
  1811. } break;
  1812. case Variant::DICTIONARY: {
  1813. Dictionary d=p_variant;
  1814. List<Variant> keys;
  1815. d.get_key_list(&keys);
  1816. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1817. Variant v = d[E->get()];
  1818. _find_resources(v);
  1819. }
  1820. } break;
  1821. default: {}
  1822. }
  1823. }
  1824. Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1825. Error err;
  1826. f = FileAccess::open(p_path, FileAccess::WRITE,&err);
  1827. ERR_FAIL_COND_V( err, ERR_CANT_OPEN );
  1828. FileAccessRef _fref(f);
  1829. local_path = Globals::get_singleton()->localize_path(p_path);
  1830. relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS;
  1831. skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1832. bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1833. takeover_paths=p_flags&ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1834. if (!p_path.begins_with("res://")) {
  1835. takeover_paths=false;
  1836. }
  1837. depth=0;
  1838. // save resources
  1839. _find_resources(p_resource,true);
  1840. ERR_FAIL_COND_V(err!=OK,err);
  1841. write_string("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",false); //no escape
  1842. write_string("\n",false);
  1843. enter_tag("resource_file","type=\""+p_resource->get_type()+"\" subresource_count=\""+itos(saved_resources.size()+external_resources.size())+"\" version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\" version_name=\""+VERSION_FULL_NAME+"\"");
  1844. write_string("\n",false);
  1845. for(List<RES>::Element *E=external_resources.front();E;E=E->next()) {
  1846. write_tabs();
  1847. String p = E->get()->get_path();
  1848. enter_tag("ext_resource","path=\""+p+"\" type=\""+E->get()->get_save_type()+"\""); //bundled
  1849. exit_tag("ext_resource"); //bundled
  1850. write_string("\n",false);
  1851. }
  1852. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  1853. RES res = E->get();
  1854. ERR_CONTINUE(!resource_map.has(res));
  1855. bool main = (E->next()==NULL);
  1856. write_tabs();
  1857. if (main)
  1858. enter_tag("main_resource",""); //bundled
  1859. else if (res->get_path().length() && res->get_path().find("::") == -1 )
  1860. enter_tag("resource","type=\""+res->get_type()+"\" path=\""+res->get_path()+"\""); //bundled
  1861. else {
  1862. int idx = resource_map[res];
  1863. enter_tag("resource","type=\""+res->get_type()+"\" path=\"local://"+itos(idx)+"\"");
  1864. if (takeover_paths) {
  1865. res->set_path(p_path+"::"+itos(idx),true);
  1866. }
  1867. }
  1868. write_string("\n",false);
  1869. List<PropertyInfo> property_list;
  1870. res->get_property_list(&property_list);
  1871. // property_list.sort();
  1872. for(List<PropertyInfo>::Element *PE = property_list.front();PE;PE=PE->next()) {
  1873. if (skip_editor && PE->get().name.begins_with("__editor"))
  1874. continue;
  1875. if (PE->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && PE->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1876. String name = PE->get().name;
  1877. Variant value = res->get(name);
  1878. if (PE->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero())
  1879. continue;
  1880. write_property(name,value);
  1881. }
  1882. }
  1883. write_string("\n",false);
  1884. write_tabs(-1);
  1885. if (main)
  1886. exit_tag("main_resource");
  1887. else
  1888. exit_tag("resource");
  1889. write_string("\n",false);
  1890. }
  1891. exit_tag("resource_file");
  1892. if (f->get_error()!=OK && f->get_error()!=ERR_FILE_EOF) {
  1893. f->close();
  1894. return ERR_CANT_CREATE;
  1895. }
  1896. f->close();
  1897. //memdelete(f);
  1898. return OK;
  1899. }
  1900. Error ResourceFormatSaverXML::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1901. ResourceFormatSaverXMLInstance saver;
  1902. return saver.save(p_path,p_resource,p_flags);
  1903. }
  1904. bool ResourceFormatSaverXML::recognize(const RES& p_resource) const {
  1905. return true; // all recognized!
  1906. }
  1907. void ResourceFormatSaverXML::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1908. //here comes the sun, lalalala
  1909. String base = p_resource->get_base_extension().to_lower();
  1910. p_extensions->push_back("xml");
  1911. if (base!="res") {
  1912. p_extensions->push_back("x"+base);
  1913. }
  1914. }