resource_format_xml.cpp 59 KB

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