resource_format_xml.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  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. bool exit;
  992. Tag *tag = parse_tag(&exit);
  993. if (!tag) {
  994. error=ERR_FILE_CORRUPT;
  995. if (!exit) // shouldn't have exited
  996. ERR_FAIL_V(error);
  997. error=ERR_FILE_EOF;
  998. return error;
  999. }
  1000. RES res;
  1001. //Object *obj=NULL;
  1002. bool main;
  1003. if (tag->name=="ext_resource") {
  1004. error=ERR_FILE_CORRUPT;
  1005. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field.");
  1006. ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT);
  1007. String type;
  1008. if (tag->args.has("type"))
  1009. type=tag->args["type"];
  1010. String path = tag->args["path"];
  1011. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?.");
  1012. ERR_FAIL_COND_V(path.begins_with("local://"),ERR_FILE_CORRUPT);
  1013. if (path.find("://")==-1 && path.is_rel_path()) {
  1014. // path is relative to file being loaded, so convert to a resource path
  1015. path=Globals::get_singleton()->localize_path(local_path.get_base_dir()+"/"+path);
  1016. }
  1017. RES res = ResourceLoader::load(path,type);
  1018. if (res.is_null()) {
  1019. if (ResourceLoader::get_abort_on_missing_resources()) {
  1020. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> referenced unexisting resource at: "+path);
  1021. ERR_FAIL_V(error);
  1022. } else {
  1023. ResourceLoader::notify_load_error("Resource Not Found: "+path);
  1024. }
  1025. } else {
  1026. resource_cache.push_back(res);
  1027. }
  1028. Error err = close_tag("ext_resource");
  1029. if (err)
  1030. return error;
  1031. error=OK;
  1032. resource_current++;
  1033. return error;
  1034. } else if (tag->name=="resource") {
  1035. main=false;
  1036. } else if (tag->name=="main_resource") {
  1037. main=true;
  1038. } else {
  1039. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": unexpected main tag: "+tag->name);
  1040. error=ERR_FILE_CORRUPT;
  1041. ERR_FAIL_V(error);
  1042. }
  1043. String type;
  1044. String path;
  1045. if (!main) {
  1046. //loading resource
  1047. error=ERR_FILE_CORRUPT;
  1048. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'len' field.");
  1049. ERR_FAIL_COND_V(!tag->args.has("path"),ERR_FILE_CORRUPT);
  1050. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'type' field.");
  1051. ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT);
  1052. path=tag->args["path"];
  1053. error=OK;
  1054. if (path.begins_with("local://")) {
  1055. //built-in resource (but really external)
  1056. path=path.replace("local://",local_path+"::");
  1057. }
  1058. if (ResourceCache::has(path)) {
  1059. Error err = close_tag(tag->name);
  1060. if (err) {
  1061. error=ERR_FILE_CORRUPT;
  1062. }
  1063. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Unable to close <resource> tag.");
  1064. ERR_FAIL_COND_V( err, err );
  1065. resource_current++;
  1066. error=OK;
  1067. return OK;
  1068. }
  1069. type = tag->args["type"];
  1070. } else {
  1071. type=resource_type;
  1072. }
  1073. Object *obj = ObjectTypeDB::instance(type);
  1074. if (!obj) {
  1075. error=ERR_FILE_CORRUPT;
  1076. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object of unrecognized type in file: "+type);
  1077. }
  1078. ERR_FAIL_COND_V(!obj,ERR_FILE_CORRUPT);
  1079. Resource *r = obj->cast_to<Resource>();
  1080. if (!r) {
  1081. error=ERR_FILE_CORRUPT;
  1082. memdelete(obj); //bye
  1083. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": Object type in resource field not a resource, type is: "+obj->get_type());
  1084. ERR_FAIL_COND_V(!r,ERR_FILE_CORRUPT);
  1085. }
  1086. res = RES( r );
  1087. if (path!="")
  1088. r->set_path(path);
  1089. //load properties
  1090. while(true) {
  1091. String name;
  1092. Variant v;
  1093. Error err;
  1094. err = parse_property(v,name);
  1095. if (err==ERR_FILE_EOF) //tag closed
  1096. break;
  1097. if (err!=OK) {
  1098. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": XML Parsing aborted.");
  1099. ERR_FAIL_COND_V(err!=OK,ERR_FILE_CORRUPT);
  1100. }
  1101. obj->set(name,v);
  1102. }
  1103. #ifdef TOOLS_ENABLED
  1104. res->set_edited(false);
  1105. #endif
  1106. resource_cache.push_back(res); //keep it in mem until finished loading
  1107. resource_current++;
  1108. if (main) {
  1109. f->close();
  1110. resource=res;
  1111. resource->set_path(res_path);
  1112. error=ERR_FILE_EOF;
  1113. return error;
  1114. }
  1115. error=OK;
  1116. return OK;
  1117. }
  1118. int ResourceInteractiveLoaderXML::get_stage() const {
  1119. return resource_current;
  1120. }
  1121. int ResourceInteractiveLoaderXML::get_stage_count() const {
  1122. return resources_total;
  1123. }
  1124. ResourceInteractiveLoaderXML::~ResourceInteractiveLoaderXML() {
  1125. memdelete(f);
  1126. }
  1127. void ResourceInteractiveLoaderXML::get_dependencies(FileAccess *f,List<String> *p_dependencies) {
  1128. open(f);
  1129. ERR_FAIL_COND(error!=OK);
  1130. while(true) {
  1131. bool exit;
  1132. Tag *tag = parse_tag(&exit);
  1133. if (!tag) {
  1134. error=ERR_FILE_CORRUPT;
  1135. ERR_FAIL_COND(!exit);
  1136. error=ERR_FILE_EOF;
  1137. return;
  1138. }
  1139. if (tag->name!="ext_resource") {
  1140. return;
  1141. }
  1142. error=ERR_FILE_CORRUPT;
  1143. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field.");
  1144. ERR_FAIL_COND(!tag->args.has("path"));
  1145. String path = tag->args["path"];
  1146. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?.");
  1147. ERR_FAIL_COND(path.begins_with("local://"));
  1148. if (path.find("://")==-1 && path.is_rel_path()) {
  1149. // path is relative to file being loaded, so convert to a resource path
  1150. path=Globals::get_singleton()->localize_path(local_path.get_base_dir()+"/"+path);
  1151. }
  1152. p_dependencies->push_back(path);
  1153. Error err = close_tag("ext_resource");
  1154. if (err)
  1155. return;
  1156. error=OK;
  1157. }
  1158. }
  1159. void ResourceInteractiveLoaderXML::open(FileAccess *p_f) {
  1160. error=OK;
  1161. lines=1;
  1162. f=p_f;
  1163. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1164. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1165. error=ERR_FILE_CORRUPT;
  1166. ResourceLoader::notify_load_error("XML is invalid (missing header tags)");
  1167. ERR_EXPLAIN("Not a XML:UTF-8 File: "+local_path);
  1168. ERR_FAIL();
  1169. }
  1170. tag_stack.clear();
  1171. tag = parse_tag();
  1172. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1173. ResourceLoader::notify_load_error(local_path+": XML is not a valid resource file.");
  1174. error=ERR_FILE_CORRUPT;
  1175. ERR_EXPLAIN("Unrecognized XML File: "+local_path);
  1176. ERR_FAIL();
  1177. }
  1178. if (tag->args.has("subresource_count"))
  1179. resources_total=tag->args["subresource_count"].to_int();
  1180. resource_current=0;
  1181. resource_type=tag->args["type"];
  1182. String version = tag->args["version"];
  1183. if (version.get_slice_count(".")!=2) {
  1184. error=ERR_FILE_CORRUPT;
  1185. ResourceLoader::notify_load_error(local_path+":XML version string is invalid: "+version);
  1186. ERR_EXPLAIN("Invalid Version String '"+version+"'' in file: "+local_path);
  1187. ERR_FAIL();
  1188. }
  1189. int major = version.get_slice(".",0).to_int();
  1190. int minor = version.get_slice(".",1).to_int();
  1191. if (major>VERSION_MAJOR || (major==VERSION_MAJOR && minor>VERSION_MINOR)) {
  1192. error=ERR_FILE_UNRECOGNIZED;
  1193. ResourceLoader::notify_load_error(local_path+": File Format '"+version+"' is too new. Please upgrade to a newer engine version.");
  1194. ERR_EXPLAIN("File Format '"+version+"' is too new! Please upgrade to a a new engine version: "+local_path);
  1195. ERR_FAIL();
  1196. }
  1197. }
  1198. String ResourceInteractiveLoaderXML::recognize(FileAccess *p_f) {
  1199. error=OK;
  1200. lines=1;
  1201. f=p_f;
  1202. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1203. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1204. return ""; //unrecognized
  1205. }
  1206. tag_stack.clear();
  1207. tag = parse_tag();
  1208. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1209. return ""; //unrecognized
  1210. }
  1211. return tag->args["type"];
  1212. }
  1213. /////////////////////
  1214. Ref<ResourceInteractiveLoader> ResourceFormatLoaderXML::load_interactive(const String &p_path) {
  1215. Error err;
  1216. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  1217. if (err!=OK) {
  1218. ERR_FAIL_COND_V(err!=OK,Ref<ResourceInteractiveLoader>());
  1219. }
  1220. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1221. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1222. ria->res_path=ria->local_path;
  1223. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1224. ria->open(f);
  1225. return ria;
  1226. }
  1227. void ResourceFormatLoaderXML::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const {
  1228. if (p_type=="") {
  1229. get_recognized_extensions(p_extensions);
  1230. return;
  1231. }
  1232. List<String> extensions;
  1233. ObjectTypeDB::get_extensions_for_type(p_type,&extensions);
  1234. extensions.sort();
  1235. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1236. String ext = E->get().to_lower();
  1237. if (ext=="res")
  1238. continue;
  1239. p_extensions->push_back("x"+ext);
  1240. p_extensions->push_back(ext);
  1241. }
  1242. p_extensions->push_back("xml");
  1243. }
  1244. void ResourceFormatLoaderXML::get_recognized_extensions(List<String> *p_extensions) const{
  1245. List<String> extensions;
  1246. ObjectTypeDB::get_resource_base_extensions(&extensions);
  1247. extensions.sort();
  1248. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1249. String ext = E->get().to_lower();
  1250. if (ext=="res")
  1251. continue;
  1252. p_extensions->push_back("x"+ext);
  1253. }
  1254. p_extensions->push_back("xml");
  1255. }
  1256. bool ResourceFormatLoaderXML::handles_type(const String& p_type) const{
  1257. return true;
  1258. }
  1259. String ResourceFormatLoaderXML::get_resource_type(const String &p_path) const{
  1260. String ext=p_path.extension().to_lower();
  1261. if (!ext.begins_with("x")) //a lie but..
  1262. return "";
  1263. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1264. if (!f) {
  1265. return ""; //could not rwead
  1266. }
  1267. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1268. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1269. ria->res_path=ria->local_path;
  1270. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1271. String r = ria->recognize(f);
  1272. return r;
  1273. }
  1274. void ResourceFormatLoaderXML::get_dependencies(const String& p_path,List<String> *p_dependencies) {
  1275. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1276. if (!f) {
  1277. ERR_FAIL();
  1278. }
  1279. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1280. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1281. ria->res_path=ria->local_path;
  1282. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1283. ria->get_dependencies(f,p_dependencies);
  1284. }
  1285. /****************************************************************************************/
  1286. /****************************************************************************************/
  1287. /****************************************************************************************/
  1288. /****************************************************************************************/
  1289. /****************************************************************************************/
  1290. /****************************************************************************************/
  1291. /****************************************************************************************/
  1292. /****************************************************************************************/
  1293. /****************************************************************************************/
  1294. /****************************************************************************************/
  1295. /****************************************************************************************/
  1296. /****************************************************************************************/
  1297. /****************************************************************************************/
  1298. void ResourceFormatSaverXMLInstance::escape(String& p_str) {
  1299. p_str=p_str.replace("&","&amp;");
  1300. p_str=p_str.replace("<","&gt;");
  1301. p_str=p_str.replace(">","&lt;");
  1302. p_str=p_str.replace("'","&apos;");
  1303. p_str=p_str.replace("\"","&quot;");
  1304. for (int i=1;i<32;i++) {
  1305. char chr[2]={i,0};
  1306. p_str=p_str.replace(chr,"&#"+String::num(i)+";");
  1307. }
  1308. }
  1309. void ResourceFormatSaverXMLInstance::write_tabs(int p_diff) {
  1310. for (int i=0;i<depth+p_diff;i++) {
  1311. f->store_8('\t');
  1312. }
  1313. }
  1314. void ResourceFormatSaverXMLInstance::write_string(String p_str,bool p_escape) {
  1315. /* write an UTF8 string */
  1316. if (p_escape)
  1317. escape(p_str);
  1318. f->store_string(p_str);;
  1319. /*
  1320. CharString cs=p_str.utf8();
  1321. const char *data=cs.get_data();
  1322. while (*data) {
  1323. f->store_8(*data);
  1324. data++;
  1325. }*/
  1326. }
  1327. void ResourceFormatSaverXMLInstance::enter_tag(const char* p_tag,const String& p_args) {
  1328. f->store_8('<');
  1329. int cc = 0;
  1330. const char *c=p_tag;
  1331. while(*c) {
  1332. cc++;
  1333. c++;
  1334. }
  1335. f->store_buffer((const uint8_t*)p_tag,cc);
  1336. if (p_args.length()) {
  1337. f->store_8(' ');
  1338. f->store_string(p_args);
  1339. }
  1340. f->store_8('>');
  1341. depth++;
  1342. }
  1343. void ResourceFormatSaverXMLInstance::exit_tag(const char* p_tag) {
  1344. depth--;
  1345. f->store_8('<');
  1346. f->store_8('/');
  1347. int cc = 0;
  1348. const char *c=p_tag;
  1349. while(*c) {
  1350. cc++;
  1351. c++;
  1352. }
  1353. f->store_buffer((const uint8_t*)p_tag,cc);
  1354. f->store_8('>');
  1355. }
  1356. /*
  1357. static bool _check_type(const Variant& p_property) {
  1358. if (p_property.get_type()==Variant::_RID)
  1359. return false;
  1360. if (p_property.get_type()==Variant::OBJECT) {
  1361. RES res = p_property;
  1362. if (res.is_null())
  1363. return false;
  1364. }
  1365. return true;
  1366. }*/
  1367. void ResourceFormatSaverXMLInstance::write_property(const String& p_name,const Variant& p_property,bool *r_ok) {
  1368. if (r_ok)
  1369. *r_ok=false;
  1370. const char* type;
  1371. String params;
  1372. bool oneliner=true;
  1373. switch( p_property.get_type() ) {
  1374. case Variant::NIL: type="nil"; break;
  1375. case Variant::BOOL: type="bool"; break;
  1376. case Variant::INT: type="int"; break;
  1377. case Variant::REAL: type="real"; break;
  1378. case Variant::STRING: type="string"; break;
  1379. case Variant::VECTOR2: type="vector2"; break;
  1380. case Variant::RECT2: type="rect2"; break;
  1381. case Variant::VECTOR3: type="vector3"; break;
  1382. case Variant::PLANE: type="plane"; break;
  1383. case Variant::_AABB: type="aabb"; break;
  1384. case Variant::QUAT: type="quaternion"; break;
  1385. case Variant::MATRIX32: type="matrix32"; break;
  1386. case Variant::MATRIX3: type="matrix3"; break;
  1387. case Variant::TRANSFORM: type="transform"; break;
  1388. case Variant::COLOR: type="color"; break;
  1389. case Variant::IMAGE: {
  1390. type="image";
  1391. Image img=p_property;
  1392. if (img.empty()) {
  1393. write_tabs();
  1394. enter_tag(type,"name=\""+p_name+"\"");
  1395. exit_tag(type);
  1396. if (r_ok)
  1397. *r_ok=true;
  1398. return;
  1399. }
  1400. params+="encoding=\"raw\"";
  1401. params+=" width=\""+itos(img.get_width())+"\"";
  1402. params+=" height=\""+itos(img.get_height())+"\"";
  1403. params+=" mipmaps=\""+itos(img.get_mipmaps())+"\"";
  1404. switch(img.get_format()) {
  1405. case Image::FORMAT_GRAYSCALE: params+=" format=\"grayscale\""; break;
  1406. case Image::FORMAT_INTENSITY: params+=" format=\"intensity\""; break;
  1407. case Image::FORMAT_GRAYSCALE_ALPHA: params+=" format=\"grayscale_alpha\""; break;
  1408. case Image::FORMAT_RGB: params+=" format=\"rgb\""; break;
  1409. case Image::FORMAT_RGBA: params+=" format=\"rgba\""; break;
  1410. case Image::FORMAT_INDEXED : params+=" format=\"indexed\""; break;
  1411. case Image::FORMAT_INDEXED_ALPHA: params+=" format=\"indexed_alpha\""; break;
  1412. case Image::FORMAT_BC1: params+=" format=\"bc1\""; break;
  1413. case Image::FORMAT_BC2: params+=" format=\"bc2\""; break;
  1414. case Image::FORMAT_BC3: params+=" format=\"bc3\""; break;
  1415. case Image::FORMAT_BC4: params+=" format=\"bc4\""; break;
  1416. case Image::FORMAT_BC5: params+=" format=\"bc5\""; break;
  1417. case Image::FORMAT_PVRTC2: params+=" format=\"pvrtc2\""; break;
  1418. case Image::FORMAT_PVRTC2_ALPHA: params+=" format=\"pvrtc2a\""; break;
  1419. case Image::FORMAT_PVRTC4: params+=" format=\"pvrtc4\""; break;
  1420. case Image::FORMAT_PVRTC4_ALPHA: params+=" format=\"pvrtc4a\""; break;
  1421. case Image::FORMAT_ETC: params+=" format=\"etc\""; break;
  1422. case Image::FORMAT_ATC: params+=" format=\"atc\""; break;
  1423. case Image::FORMAT_ATC_ALPHA_EXPLICIT: params+=" format=\"atcae\""; break;
  1424. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: params+=" format=\"atcai\""; break;
  1425. case Image::FORMAT_CUSTOM: params+=" format=\"custom\" custom_size=\""+itos(img.get_data().size())+"\""; break;
  1426. default: {}
  1427. }
  1428. } break;
  1429. case Variant::NODE_PATH: type="node_path"; break;
  1430. case Variant::OBJECT: {
  1431. type="resource";
  1432. RES res = p_property;
  1433. if (res.is_null()) {
  1434. write_tabs();
  1435. enter_tag(type,"name=\""+p_name+"\"");
  1436. exit_tag(type);
  1437. if (r_ok)
  1438. *r_ok=true;
  1439. return; // don't save it
  1440. }
  1441. params="resource_type=\""+res->get_save_type()+"\"";
  1442. if (res->get_path().length() && res->get_path().find("::")==-1) {
  1443. //external resource
  1444. String path=relative_paths?local_path.path_to_file(res->get_path()):res->get_path();
  1445. if (no_extension)
  1446. path=path.basename()+".*";
  1447. escape(path);
  1448. params+=" path=\""+path+"\"";
  1449. } else {
  1450. //internal resource
  1451. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1452. ERR_FAIL_COND(!resource_map.has(res));
  1453. params+=" path=\"local://"+itos(resource_map[res])+"\"";
  1454. }
  1455. } break;
  1456. case Variant::INPUT_EVENT: type="input_event"; break;
  1457. case Variant::DICTIONARY: type="dictionary"; params="shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1458. case Variant::ARRAY: type="array"; params="len=\""+itos(p_property.operator Array().size())+"\" shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1459. case Variant::RAW_ARRAY: type="raw_array"; params="len=\""+itos(p_property.operator DVector < uint8_t >().size())+"\""; break;
  1460. case Variant::INT_ARRAY: type="int_array"; params="len=\""+itos(p_property.operator DVector < int >().size())+"\""; break;
  1461. case Variant::REAL_ARRAY: type="real_array"; params="len=\""+itos(p_property.operator DVector < real_t >().size())+"\""; break;
  1462. case Variant::STRING_ARRAY: oneliner=false; type="string_array"; params="len=\""+itos(p_property.operator DVector < String >().size())+"\""; break;
  1463. case Variant::VECTOR2_ARRAY: type="vector2_array"; params="len=\""+itos(p_property.operator DVector < Vector2 >().size())+"\""; break;
  1464. case Variant::VECTOR3_ARRAY: type="vector3_array"; params="len=\""+itos(p_property.operator DVector < Vector3 >().size())+"\""; break;
  1465. case Variant::COLOR_ARRAY: type="color_array"; params="len=\""+itos(p_property.operator DVector < Color >().size())+"\""; break;
  1466. default: {
  1467. ERR_PRINT("Unknown Variant type.");
  1468. ERR_FAIL();
  1469. }
  1470. }
  1471. write_tabs();
  1472. if (p_name!="") {
  1473. if (params.length())
  1474. enter_tag(type,"name=\""+p_name+"\" "+params);
  1475. else
  1476. enter_tag(type,"name=\""+p_name+"\"");
  1477. } else {
  1478. if (params.length())
  1479. enter_tag(type," "+params);
  1480. else
  1481. enter_tag(type,String());
  1482. }
  1483. if (!oneliner)
  1484. f->store_8('\n');
  1485. else
  1486. f->store_8(' ');
  1487. switch( p_property.get_type() ) {
  1488. case Variant::NIL: {
  1489. } break;
  1490. case Variant::BOOL: {
  1491. write_string( p_property.operator bool() ? "True":"False" );
  1492. } break;
  1493. case Variant::INT: {
  1494. write_string( itos(p_property.operator int()) );
  1495. } break;
  1496. case Variant::REAL: {
  1497. write_string( rtos(p_property.operator real_t()) );
  1498. } break;
  1499. case Variant::STRING: {
  1500. String str=p_property;
  1501. escape(str);
  1502. str="\""+str+"\"";
  1503. write_string( str,false );
  1504. } break;
  1505. case Variant::VECTOR2: {
  1506. Vector2 v = p_property;
  1507. write_string( rtoss(v.x) +", "+rtoss(v.y) );
  1508. } break;
  1509. case Variant::RECT2: {
  1510. Rect2 aabb = p_property;
  1511. write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) );
  1512. } break;
  1513. case Variant::VECTOR3: {
  1514. Vector3 v = p_property;
  1515. write_string( rtoss(v.x) +", "+rtoss(v.y)+", "+rtoss(v.z) );
  1516. } break;
  1517. case Variant::PLANE: {
  1518. Plane p = p_property;
  1519. write_string( rtoss(p.normal.x) +", "+rtoss(p.normal.y)+", "+rtoss(p.normal.z)+", "+rtoss(p.d) );
  1520. } break;
  1521. case Variant::_AABB: {
  1522. AABB aabb = p_property;
  1523. 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) );
  1524. } break;
  1525. case Variant::QUAT: {
  1526. Quat quat = p_property;
  1527. write_string( rtoss(quat.x)+", "+rtoss(quat.y)+", "+rtoss(quat.z)+", "+rtoss(quat.w)+", ");
  1528. } break;
  1529. case Variant::MATRIX32: {
  1530. String s;
  1531. Matrix32 m3 = p_property;
  1532. for (int i=0;i<3;i++) {
  1533. for (int j=0;j<2;j++) {
  1534. if (i!=0 || j!=0)
  1535. s+=", ";
  1536. s+=rtoss( m3.elements[i][j] );
  1537. }
  1538. }
  1539. write_string(s);
  1540. } break;
  1541. case Variant::MATRIX3: {
  1542. String s;
  1543. Matrix3 m3 = p_property;
  1544. for (int i=0;i<3;i++) {
  1545. for (int j=0;j<3;j++) {
  1546. if (i!=0 || j!=0)
  1547. s+=", ";
  1548. s+=rtoss( m3.elements[i][j] );
  1549. }
  1550. }
  1551. write_string(s);
  1552. } break;
  1553. case Variant::TRANSFORM: {
  1554. String s;
  1555. Transform t = p_property;
  1556. Matrix3 &m3 = t.basis;
  1557. for (int i=0;i<3;i++) {
  1558. for (int j=0;j<3;j++) {
  1559. if (i!=0 || j!=0)
  1560. s+=", ";
  1561. s+=rtoss( m3.elements[i][j] );
  1562. }
  1563. }
  1564. s=s+", "+rtoss(t.origin.x) +", "+rtoss(t.origin.y)+", "+rtoss(t.origin.z);
  1565. write_string(s);
  1566. } break;
  1567. // misc types
  1568. case Variant::COLOR: {
  1569. Color c = p_property;
  1570. write_string( rtoss(c.r) +", "+rtoss(c.g)+", "+rtoss(c.b)+", "+rtoss(c.a) );
  1571. } break;
  1572. case Variant::IMAGE: {
  1573. String s;
  1574. Image img = p_property;
  1575. DVector<uint8_t> data = img.get_data();
  1576. int len = data.size();
  1577. DVector<uint8_t>::Read r = data.read();
  1578. const uint8_t *ptr=r.ptr();;
  1579. for (int i=0;i<len;i++) {
  1580. uint8_t byte = ptr[i];
  1581. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1582. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1583. s+=str;
  1584. }
  1585. write_string(s);
  1586. } break;
  1587. case Variant::NODE_PATH: {
  1588. String str=p_property;
  1589. escape(str);
  1590. str="\""+str+"\"";
  1591. write_string( str,false);
  1592. } break;
  1593. case Variant::OBJECT: {
  1594. /* this saver does not save resources in here
  1595. RES res = p_property;
  1596. if (!res.is_null()) {
  1597. String path=res->get_path();
  1598. if (!res->is_shared() || !path.length()) {
  1599. // if no path, or path is from inside a scene
  1600. write_object( *res );
  1601. }
  1602. }
  1603. */
  1604. } break;
  1605. case Variant::INPUT_EVENT: {
  1606. write_string( p_property.operator String() );
  1607. } break;
  1608. case Variant::DICTIONARY: {
  1609. Dictionary dict = p_property;
  1610. List<Variant> keys;
  1611. dict.get_key_list(&keys);
  1612. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1613. //if (!_check_type(dict[E->get()]))
  1614. // continue;
  1615. bool ok;
  1616. write_property("",E->get(),&ok);
  1617. ERR_CONTINUE(!ok);
  1618. write_property("",dict[E->get()],&ok);
  1619. if (!ok)
  1620. write_property("",Variant()); //at least make the file consistent..
  1621. }
  1622. } break;
  1623. case Variant::ARRAY: {
  1624. Array array = p_property;
  1625. int len=array.size();
  1626. for (int i=0;i<len;i++) {
  1627. write_property("",array[i]);
  1628. }
  1629. } break;
  1630. case Variant::RAW_ARRAY: {
  1631. String s;
  1632. DVector<uint8_t> data = p_property;
  1633. int len = data.size();
  1634. DVector<uint8_t>::Read r = data.read();
  1635. const uint8_t *ptr=r.ptr();;
  1636. for (int i=0;i<len;i++) {
  1637. uint8_t byte = ptr[i];
  1638. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1639. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1640. s+=str;
  1641. }
  1642. write_string(s,false);
  1643. } break;
  1644. case Variant::INT_ARRAY: {
  1645. DVector<int> data = p_property;
  1646. int len = data.size();
  1647. DVector<int>::Read r = data.read();
  1648. const int *ptr=r.ptr();;
  1649. write_tabs();
  1650. for (int i=0;i<len;i++) {
  1651. if (i>0)
  1652. write_string(", ",false);
  1653. write_string(itos(ptr[i]),false);
  1654. }
  1655. } break;
  1656. case Variant::REAL_ARRAY: {
  1657. DVector<real_t> data = p_property;
  1658. int len = data.size();
  1659. DVector<real_t>::Read r = data.read();
  1660. const real_t *ptr=r.ptr();;
  1661. write_tabs();
  1662. String cm=", " ;
  1663. for (int i=0;i<len;i++) {
  1664. if (i>0)
  1665. write_string(cm,false);
  1666. write_string(rtoss(ptr[i]),false);
  1667. }
  1668. } break;
  1669. case Variant::STRING_ARRAY: {
  1670. DVector<String> data = p_property;
  1671. int len = data.size();
  1672. DVector<String>::Read r = data.read();
  1673. const String *ptr=r.ptr();;
  1674. String s;
  1675. //write_string("\n");
  1676. for (int i=0;i<len;i++) {
  1677. write_tabs(0);
  1678. String str=ptr[i];
  1679. escape(str);
  1680. write_string("<string> \""+str+"\" </string>\n",false);
  1681. }
  1682. } break;
  1683. case Variant::VECTOR2_ARRAY: {
  1684. DVector<Vector2> data = p_property;
  1685. int len = data.size();
  1686. DVector<Vector2>::Read r = data.read();
  1687. const Vector2 *ptr=r.ptr();;
  1688. write_tabs();
  1689. for (int i=0;i<len;i++) {
  1690. if (i>0)
  1691. write_string(", ",false);
  1692. write_string(rtoss(ptr[i].x),false);
  1693. write_string(", "+rtoss(ptr[i].y),false);
  1694. }
  1695. } break;
  1696. case Variant::VECTOR3_ARRAY: {
  1697. DVector<Vector3> data = p_property;
  1698. int len = data.size();
  1699. DVector<Vector3>::Read r = data.read();
  1700. const Vector3 *ptr=r.ptr();;
  1701. write_tabs();
  1702. for (int i=0;i<len;i++) {
  1703. if (i>0)
  1704. write_string(", ",false);
  1705. write_string(rtoss(ptr[i].x),false);
  1706. write_string(", "+rtoss(ptr[i].y),false);
  1707. write_string(", "+rtoss(ptr[i].z),false);
  1708. }
  1709. } break;
  1710. case Variant::COLOR_ARRAY: {
  1711. DVector<Color> data = p_property;
  1712. int len = data.size();
  1713. DVector<Color>::Read r = data.read();
  1714. const Color *ptr=r.ptr();;
  1715. write_tabs();
  1716. for (int i=0;i<len;i++) {
  1717. if (i>0)
  1718. write_string(", ",false);
  1719. write_string(rtoss(ptr[i].r),false);
  1720. write_string(", "+rtoss(ptr[i].g),false);
  1721. write_string(", "+rtoss(ptr[i].b),false);
  1722. write_string(", "+rtoss(ptr[i].a),false);
  1723. }
  1724. } break;
  1725. default: {}
  1726. }
  1727. if (oneliner)
  1728. f->store_8(' ');
  1729. else
  1730. write_tabs(-1);
  1731. exit_tag(type);
  1732. f->store_8('\n');
  1733. if (r_ok)
  1734. *r_ok=true;
  1735. }
  1736. void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bool p_main) {
  1737. switch(p_variant.get_type()) {
  1738. case Variant::OBJECT: {
  1739. RES res = p_variant.operator RefPtr();
  1740. if (res.is_null())
  1741. return;
  1742. if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) {
  1743. external_resources.insert(res);
  1744. return;
  1745. }
  1746. if (resource_map.has(res))
  1747. return;
  1748. List<PropertyInfo> property_list;
  1749. res->get_property_list( &property_list );
  1750. List<PropertyInfo>::Element *I=property_list.front();
  1751. while(I) {
  1752. PropertyInfo pi=I->get();
  1753. if (pi.usage&PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage&PROPERTY_USAGE_BUNDLE)) {
  1754. Variant v=res->get(I->get().name);
  1755. _find_resources(v);
  1756. }
  1757. I=I->next();
  1758. }
  1759. resource_map[ res ] = resource_map.size(); //saved after, so the childs it needs are available when loaded
  1760. saved_resources.push_back(res);
  1761. } break;
  1762. case Variant::ARRAY: {
  1763. Array varray=p_variant;
  1764. int len=varray.size();
  1765. for(int i=0;i<len;i++) {
  1766. Variant v=varray.get(i);
  1767. _find_resources(v);
  1768. }
  1769. } break;
  1770. case Variant::DICTIONARY: {
  1771. Dictionary d=p_variant;
  1772. List<Variant> keys;
  1773. d.get_key_list(&keys);
  1774. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1775. Variant v = d[E->get()];
  1776. _find_resources(v);
  1777. }
  1778. } break;
  1779. default: {}
  1780. }
  1781. }
  1782. Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1783. Error err;
  1784. f = FileAccess::open(p_path, FileAccess::WRITE,&err);
  1785. ERR_FAIL_COND_V( err, ERR_CANT_OPEN );
  1786. FileAccessRef _fref(f);
  1787. local_path = Globals::get_singleton()->localize_path(p_path);
  1788. relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS;
  1789. skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1790. bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1791. no_extension=p_flags&ResourceSaver::FLAG_NO_EXTENSION;
  1792. depth=0;
  1793. // save resources
  1794. _find_resources(p_resource,true);
  1795. ERR_FAIL_COND_V(err!=OK,err);
  1796. write_string("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",false); //no escape
  1797. write_string("\n",false);
  1798. 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+"\"");
  1799. write_string("\n",false);
  1800. for(Set<RES>::Element *E=external_resources.front();E;E=E->next()) {
  1801. write_tabs();
  1802. String p = E->get()->get_path();
  1803. if (no_extension)
  1804. p=p.basename()+".*";
  1805. enter_tag("ext_resource","path=\""+p+"\" type=\""+E->get()->get_save_type()+"\""); //bundled
  1806. exit_tag("ext_resource"); //bundled
  1807. write_string("\n",false);
  1808. }
  1809. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  1810. RES res = E->get();
  1811. ERR_CONTINUE(!resource_map.has(res));
  1812. bool main = (E->next()==NULL);
  1813. write_tabs();
  1814. if (main)
  1815. enter_tag("main_resource",""); //bundled
  1816. else if (res->get_path().length() && res->get_path().find("::") == -1 )
  1817. enter_tag("resource","type=\""+res->get_type()+"\" path=\""+res->get_path()+"\""); //bundled
  1818. else
  1819. enter_tag("resource","type=\""+res->get_type()+"\" path=\"local://"+itos(resource_map[res])+"\"");
  1820. write_string("\n",false);
  1821. List<PropertyInfo> property_list;
  1822. res->get_property_list(&property_list);
  1823. for(List<PropertyInfo>::Element *PE = property_list.front();PE;PE=PE->next()) {
  1824. if (skip_editor && PE->get().name.begins_with("__editor"))
  1825. continue;
  1826. if (PE->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && PE->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1827. String name = PE->get().name;
  1828. Variant value = res->get(name);
  1829. if (PE->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero())
  1830. continue;
  1831. write_property(name,value);
  1832. }
  1833. }
  1834. write_string("\n",false);
  1835. write_tabs(-1);
  1836. if (main)
  1837. exit_tag("main_resource");
  1838. else
  1839. exit_tag("resource");
  1840. write_string("\n",false);
  1841. }
  1842. exit_tag("resource_file");
  1843. f->close();
  1844. //memdelete(f);
  1845. return OK;
  1846. }
  1847. Error ResourceFormatSaverXML::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1848. ResourceFormatSaverXMLInstance saver;
  1849. return saver.save(p_path,p_resource,p_flags);
  1850. }
  1851. bool ResourceFormatSaverXML::recognize(const RES& p_resource) const {
  1852. return true; // all recognized!
  1853. }
  1854. void ResourceFormatSaverXML::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1855. //here comes the sun, lalalala
  1856. String base = p_resource->get_base_extension().to_lower();
  1857. p_extensions->push_back("xml");
  1858. if (base!="res") {
  1859. p_extensions->push_back("x"+base);
  1860. }
  1861. }