resource_format_xml.cpp 65 KB

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