resource_format_xml.cpp 59 KB

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