resource_format_xml.cpp 57 KB

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