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-2017 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. if (!ResourceCache::has(res_path)) {
  1157. resource->set_path(res_path);
  1158. }
  1159. error=ERR_FILE_EOF;
  1160. return error;
  1161. }
  1162. error=OK;
  1163. return OK;
  1164. }
  1165. int ResourceInteractiveLoaderXML::get_stage() const {
  1166. return resource_current;
  1167. }
  1168. int ResourceInteractiveLoaderXML::get_stage_count() const {
  1169. return resources_total;//+ext_resources;
  1170. }
  1171. ResourceInteractiveLoaderXML::~ResourceInteractiveLoaderXML() {
  1172. memdelete(f);
  1173. }
  1174. void ResourceInteractiveLoaderXML::get_dependencies(FileAccess *f,List<String> *p_dependencies,bool p_add_types) {
  1175. open(f);
  1176. ERR_FAIL_COND(error!=OK);
  1177. while(true) {
  1178. bool exit;
  1179. Tag *tag = parse_tag(&exit);
  1180. if (!tag) {
  1181. error=ERR_FILE_CORRUPT;
  1182. ERR_FAIL_COND(!exit);
  1183. error=ERR_FILE_EOF;
  1184. return;
  1185. }
  1186. if (tag->name!="ext_resource") {
  1187. return;
  1188. }
  1189. error=ERR_FILE_CORRUPT;
  1190. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> missing 'path' field.");
  1191. ERR_FAIL_COND(!tag->args.has("path"));
  1192. String path = tag->args["path"];
  1193. ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <ext_resource> can't use a local path, this is a bug?.");
  1194. ERR_FAIL_COND(path.begins_with("local://"));
  1195. if (path.find("://")==-1 && path.is_rel_path()) {
  1196. // path is relative to file being loaded, so convert to a resource path
  1197. path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path));
  1198. }
  1199. if (path.ends_with("*")) {
  1200. ERR_FAIL_COND(!tag->args.has("type"));
  1201. String type = tag->args["type"];
  1202. path = ResourceLoader::guess_full_filename(path,type);
  1203. }
  1204. if (p_add_types && tag->args.has("type")) {
  1205. path+="::"+tag->args["type"];
  1206. }
  1207. p_dependencies->push_back(path);
  1208. Error err = close_tag("ext_resource");
  1209. if (err)
  1210. return;
  1211. error=OK;
  1212. }
  1213. }
  1214. Error ResourceInteractiveLoaderXML::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) {
  1215. open(p_f);
  1216. ERR_FAIL_COND_V(error!=OK,error);
  1217. //FileAccess
  1218. bool old_format=false;
  1219. FileAccess *fw = NULL;
  1220. String base_path=local_path.get_base_dir();
  1221. while(true) {
  1222. bool exit;
  1223. List<String> order;
  1224. Tag *tag = parse_tag(&exit,true,&order);
  1225. bool done=false;
  1226. if (!tag) {
  1227. if (fw) {
  1228. memdelete(fw);
  1229. }
  1230. error=ERR_FILE_CORRUPT;
  1231. ERR_FAIL_COND_V(!exit,error);
  1232. error=ERR_FILE_EOF;
  1233. return error;
  1234. }
  1235. if (tag->name=="ext_resource") {
  1236. if (!tag->args.has("index") || !tag->args.has("path") || !tag->args.has("type")) {
  1237. old_format=true;
  1238. break;
  1239. }
  1240. if (!fw) {
  1241. fw=FileAccess::open(p_path+".depren",FileAccess::WRITE);
  1242. fw->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); //no escape
  1243. 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+"\">");
  1244. }
  1245. String path = tag->args["path"];
  1246. String index = tag->args["index"];
  1247. String type = tag->args["type"];
  1248. bool relative=false;
  1249. if (!path.begins_with("res://")) {
  1250. path=base_path.plus_file(path).simplify_path();
  1251. relative=true;
  1252. }
  1253. if (p_map.has(path)) {
  1254. String np=p_map[path];
  1255. path=np;
  1256. }
  1257. if (relative) {
  1258. //restore relative
  1259. path=base_path.path_to_file(path);
  1260. }
  1261. tag->args["path"]=path;
  1262. tag->args["index"]=index;
  1263. tag->args["type"]=type;
  1264. } else {
  1265. done=true;
  1266. }
  1267. String tagt="\t<";
  1268. if (exit)
  1269. tagt+="/";
  1270. tagt+=tag->name;
  1271. for(List<String>::Element *E=order.front();E;E=E->next()) {
  1272. tagt+=" "+E->get()+"=\""+tag->args[E->get()]+"\"";
  1273. }
  1274. tagt+=">";
  1275. fw->store_line(tagt);
  1276. if (done)
  1277. break;
  1278. close_tag("ext_resource");
  1279. fw->store_line("\t</ext_resource>");
  1280. }
  1281. if (old_format) {
  1282. if (fw)
  1283. memdelete(fw);
  1284. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1285. da->remove(p_path+".depren");
  1286. memdelete(da);
  1287. //fuck it, use the old approach;
  1288. WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: "+p_path).utf8().get_data());
  1289. Error err;
  1290. FileAccess *f2 = FileAccess::open(p_path,FileAccess::READ,&err);
  1291. if (err!=OK) {
  1292. ERR_FAIL_COND_V(err!=OK,ERR_FILE_CANT_OPEN);
  1293. }
  1294. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1295. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1296. ria->res_path=ria->local_path;
  1297. ria->remaps=p_map;
  1298. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1299. ria->open(f2);
  1300. err = ria->poll();
  1301. while(err==OK) {
  1302. err=ria->poll();
  1303. }
  1304. ERR_FAIL_COND_V(err!=ERR_FILE_EOF,ERR_FILE_CORRUPT);
  1305. RES res = ria->get_resource();
  1306. ERR_FAIL_COND_V(!res.is_valid(),ERR_FILE_CORRUPT);
  1307. return ResourceFormatSaverXML::singleton->save(p_path,res);
  1308. }
  1309. if (!fw) {
  1310. return OK; //nothing to rename, do nothing
  1311. }
  1312. uint8_t c=f->get_8();
  1313. while(!f->eof_reached()) {
  1314. fw->store_8(c);
  1315. c=f->get_8();
  1316. }
  1317. f->close();
  1318. bool all_ok = fw->get_error()==OK;
  1319. memdelete(fw);
  1320. if (!all_ok) {
  1321. return ERR_CANT_CREATE;
  1322. }
  1323. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1324. da->remove(p_path);
  1325. da->rename(p_path+".depren",p_path);
  1326. memdelete(da);
  1327. return OK;
  1328. }
  1329. void ResourceInteractiveLoaderXML::open(FileAccess *p_f) {
  1330. error=OK;
  1331. lines=1;
  1332. f=p_f;
  1333. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1334. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1335. error=ERR_FILE_CORRUPT;
  1336. ResourceLoader::notify_load_error("XML is invalid (missing header tags)");
  1337. ERR_EXPLAIN("Not a XML:UTF-8 File: "+local_path);
  1338. ERR_FAIL();
  1339. }
  1340. tag_stack.clear();
  1341. tag = parse_tag();
  1342. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1343. ResourceLoader::notify_load_error(local_path+": XML is not a valid resource file.");
  1344. error=ERR_FILE_CORRUPT;
  1345. ERR_EXPLAIN("Unrecognized XML File: "+local_path);
  1346. ERR_FAIL();
  1347. }
  1348. if (tag->args.has("subresource_count"))
  1349. resources_total=tag->args["subresource_count"].to_int();
  1350. resource_current=0;
  1351. resource_type=tag->args["type"];
  1352. String version = tag->args["version"];
  1353. if (version.get_slice_count(".")!=2) {
  1354. error=ERR_FILE_CORRUPT;
  1355. ResourceLoader::notify_load_error(local_path+":XML version string is invalid: "+version);
  1356. ERR_EXPLAIN("Invalid Version String '"+version+"'' in file: "+local_path);
  1357. ERR_FAIL();
  1358. }
  1359. int major = version.get_slicec('.',0).to_int();
  1360. if (major>VERSION_MAJOR) {
  1361. error=ERR_FILE_UNRECOGNIZED;
  1362. ResourceLoader::notify_load_error(local_path+": File Format '"+version+"' is too new. Please upgrade to a newer engine version.");
  1363. ERR_EXPLAIN("File Format '"+version+"' is too new! Please upgrade to a a new engine version: "+local_path);
  1364. ERR_FAIL();
  1365. }
  1366. /*
  1367. String preload_depts = "deps/"+local_path.md5_text();
  1368. if (Globals::get_singleton()->has(preload_depts)) {
  1369. ext_resources.clear();
  1370. //ignore external resources and use these
  1371. NodePath depts=Globals::get_singleton()->get(preload_depts);
  1372. for(int i=0;i<depts.get_name_count();i++) {
  1373. ext_resources.push_back(depts.get_name(i));
  1374. }
  1375. print_line(local_path+" - EXTERNAL RESOURCES: "+itos(ext_resources.size()));
  1376. }
  1377. */
  1378. }
  1379. String ResourceInteractiveLoaderXML::recognize(FileAccess *p_f) {
  1380. error=OK;
  1381. lines=1;
  1382. f=p_f;
  1383. ResourceInteractiveLoaderXML::Tag *tag = parse_tag();
  1384. if (!tag || tag->name!="?xml" || !tag->args.has("version") || !tag->args.has("encoding") || tag->args["encoding"]!="UTF-8") {
  1385. return ""; //unrecognized
  1386. }
  1387. tag_stack.clear();
  1388. tag = parse_tag();
  1389. if (!tag || tag->name!="resource_file" || !tag->args.has("type") || !tag->args.has("version")) {
  1390. return ""; //unrecognized
  1391. }
  1392. return tag->args["type"];
  1393. }
  1394. /////////////////////
  1395. Ref<ResourceInteractiveLoader> ResourceFormatLoaderXML::load_interactive(const String &p_path, Error *r_error) {
  1396. if (r_error)
  1397. *r_error=ERR_CANT_OPEN;
  1398. Error err;
  1399. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  1400. if (err!=OK) {
  1401. ERR_FAIL_COND_V(err!=OK,Ref<ResourceInteractiveLoader>());
  1402. }
  1403. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1404. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1405. ria->res_path=ria->local_path;
  1406. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1407. ria->open(f);
  1408. return ria;
  1409. }
  1410. void ResourceFormatLoaderXML::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const {
  1411. if (p_type=="") {
  1412. get_recognized_extensions(p_extensions);
  1413. return;
  1414. }
  1415. List<String> extensions;
  1416. ObjectTypeDB::get_extensions_for_type(p_type,&extensions);
  1417. extensions.sort();
  1418. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1419. String ext = E->get().to_lower();
  1420. if (ext=="res")
  1421. continue;
  1422. p_extensions->push_back("x"+ext);
  1423. }
  1424. p_extensions->push_back("xml");
  1425. }
  1426. void ResourceFormatLoaderXML::get_recognized_extensions(List<String> *p_extensions) const{
  1427. List<String> extensions;
  1428. ObjectTypeDB::get_resource_base_extensions(&extensions);
  1429. extensions.sort();
  1430. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  1431. String ext = E->get().to_lower();
  1432. if (ext=="res")
  1433. continue;
  1434. p_extensions->push_back("x"+ext);
  1435. }
  1436. p_extensions->push_back("xml");
  1437. }
  1438. bool ResourceFormatLoaderXML::handles_type(const String& p_type) const{
  1439. return true;
  1440. }
  1441. String ResourceFormatLoaderXML::get_resource_type(const String &p_path) const{
  1442. String ext=p_path.extension().to_lower();
  1443. if (!ext.begins_with("x")) //a lie but..
  1444. return "";
  1445. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1446. if (!f) {
  1447. return ""; //could not rwead
  1448. }
  1449. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1450. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1451. ria->res_path=ria->local_path;
  1452. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1453. String r = ria->recognize(f);
  1454. return r;
  1455. }
  1456. void ResourceFormatLoaderXML::get_dependencies(const String& p_path,List<String> *p_dependencies,bool p_add_types) {
  1457. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1458. if (!f) {
  1459. ERR_FAIL();
  1460. }
  1461. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1462. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1463. ria->res_path=ria->local_path;
  1464. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1465. ria->get_dependencies(f,p_dependencies,p_add_types);
  1466. }
  1467. Error ResourceFormatLoaderXML::rename_dependencies(const String &p_path,const Map<String,String>& p_map) {
  1468. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  1469. if (!f) {
  1470. ERR_FAIL_V(ERR_CANT_OPEN);
  1471. }
  1472. Ref<ResourceInteractiveLoaderXML> ria = memnew( ResourceInteractiveLoaderXML );
  1473. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  1474. ria->res_path=ria->local_path;
  1475. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1476. return ria->rename_dependencies(f,p_path,p_map);
  1477. }
  1478. /****************************************************************************************/
  1479. /****************************************************************************************/
  1480. /****************************************************************************************/
  1481. /****************************************************************************************/
  1482. /****************************************************************************************/
  1483. /****************************************************************************************/
  1484. /****************************************************************************************/
  1485. /****************************************************************************************/
  1486. /****************************************************************************************/
  1487. /****************************************************************************************/
  1488. /****************************************************************************************/
  1489. /****************************************************************************************/
  1490. /****************************************************************************************/
  1491. void ResourceFormatSaverXMLInstance::escape(String& p_str) {
  1492. p_str=p_str.replace("&","&amp;");
  1493. p_str=p_str.replace("<","&lt;");
  1494. p_str=p_str.replace(">","&gt;");
  1495. p_str=p_str.replace("'","&apos;");
  1496. p_str=p_str.replace("\"","&quot;");
  1497. for (char i=1;i<32;i++) {
  1498. char chr[2]={i,0};
  1499. const char hexn[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  1500. const char hex[8]={'&','#','0','0',hexn[i>>4],hexn[i&0xf],';',0};
  1501. p_str=p_str.replace(chr,hex);
  1502. }
  1503. }
  1504. void ResourceFormatSaverXMLInstance::write_tabs(int p_diff) {
  1505. for (int i=0;i<depth+p_diff;i++) {
  1506. f->store_8('\t');
  1507. }
  1508. }
  1509. void ResourceFormatSaverXMLInstance::write_string(String p_str,bool p_escape) {
  1510. /* write an UTF8 string */
  1511. if (p_escape)
  1512. escape(p_str);
  1513. f->store_string(p_str);;
  1514. /*
  1515. CharString cs=p_str.utf8();
  1516. const char *data=cs.get_data();
  1517. while (*data) {
  1518. f->store_8(*data);
  1519. data++;
  1520. }*/
  1521. }
  1522. void ResourceFormatSaverXMLInstance::enter_tag(const char* p_tag,const String& p_args) {
  1523. f->store_8('<');
  1524. int cc = 0;
  1525. const char *c=p_tag;
  1526. while(*c) {
  1527. cc++;
  1528. c++;
  1529. }
  1530. f->store_buffer((const uint8_t*)p_tag,cc);
  1531. if (p_args.length()) {
  1532. f->store_8(' ');
  1533. f->store_string(p_args);
  1534. }
  1535. f->store_8('>');
  1536. depth++;
  1537. }
  1538. void ResourceFormatSaverXMLInstance::exit_tag(const char* p_tag) {
  1539. depth--;
  1540. f->store_8('<');
  1541. f->store_8('/');
  1542. int cc = 0;
  1543. const char *c=p_tag;
  1544. while(*c) {
  1545. cc++;
  1546. c++;
  1547. }
  1548. f->store_buffer((const uint8_t*)p_tag,cc);
  1549. f->store_8('>');
  1550. }
  1551. /*
  1552. static bool _check_type(const Variant& p_property) {
  1553. if (p_property.get_type()==Variant::_RID)
  1554. return false;
  1555. if (p_property.get_type()==Variant::OBJECT) {
  1556. RES res = p_property;
  1557. if (res.is_null())
  1558. return false;
  1559. }
  1560. return true;
  1561. }*/
  1562. void ResourceFormatSaverXMLInstance::write_property(const String& p_name,const Variant& p_property,bool *r_ok) {
  1563. if (r_ok)
  1564. *r_ok=false;
  1565. const char* type;
  1566. String params;
  1567. bool oneliner=true;
  1568. switch( p_property.get_type() ) {
  1569. case Variant::NIL: type="nil"; break;
  1570. case Variant::BOOL: type="bool"; break;
  1571. case Variant::INT: type="int"; break;
  1572. case Variant::REAL: type="real"; break;
  1573. case Variant::STRING: type="string"; break;
  1574. case Variant::VECTOR2: type="vector2"; break;
  1575. case Variant::RECT2: type="rect2"; break;
  1576. case Variant::VECTOR3: type="vector3"; break;
  1577. case Variant::PLANE: type="plane"; break;
  1578. case Variant::_AABB: type="aabb"; break;
  1579. case Variant::QUAT: type="quaternion"; break;
  1580. case Variant::MATRIX32: type="matrix32"; break;
  1581. case Variant::MATRIX3: type="matrix3"; break;
  1582. case Variant::TRANSFORM: type="transform"; break;
  1583. case Variant::COLOR: type="color"; break;
  1584. case Variant::IMAGE: {
  1585. type="image";
  1586. Image img=p_property;
  1587. if (img.empty()) {
  1588. write_tabs();
  1589. enter_tag(type,"name=\""+p_name+"\"");
  1590. exit_tag(type);
  1591. if (r_ok)
  1592. *r_ok=true;
  1593. return;
  1594. }
  1595. params+="encoding=\"raw\"";
  1596. params+=" width=\""+itos(img.get_width())+"\"";
  1597. params+=" height=\""+itos(img.get_height())+"\"";
  1598. params+=" mipmaps=\""+itos(img.get_mipmaps())+"\"";
  1599. switch(img.get_format()) {
  1600. case Image::FORMAT_GRAYSCALE: params+=" format=\"grayscale\""; break;
  1601. case Image::FORMAT_INTENSITY: params+=" format=\"intensity\""; break;
  1602. case Image::FORMAT_GRAYSCALE_ALPHA: params+=" format=\"grayscale_alpha\""; break;
  1603. case Image::FORMAT_RGB: params+=" format=\"rgb\""; break;
  1604. case Image::FORMAT_RGBA: params+=" format=\"rgba\""; break;
  1605. case Image::FORMAT_INDEXED : params+=" format=\"indexed\""; break;
  1606. case Image::FORMAT_INDEXED_ALPHA: params+=" format=\"indexed_alpha\""; break;
  1607. case Image::FORMAT_BC1: params+=" format=\"bc1\""; break;
  1608. case Image::FORMAT_BC2: params+=" format=\"bc2\""; break;
  1609. case Image::FORMAT_BC3: params+=" format=\"bc3\""; break;
  1610. case Image::FORMAT_BC4: params+=" format=\"bc4\""; break;
  1611. case Image::FORMAT_BC5: params+=" format=\"bc5\""; break;
  1612. case Image::FORMAT_PVRTC2: params+=" format=\"pvrtc2\""; break;
  1613. case Image::FORMAT_PVRTC2_ALPHA: params+=" format=\"pvrtc2a\""; break;
  1614. case Image::FORMAT_PVRTC4: params+=" format=\"pvrtc4\""; break;
  1615. case Image::FORMAT_PVRTC4_ALPHA: params+=" format=\"pvrtc4a\""; break;
  1616. case Image::FORMAT_ETC: params+=" format=\"etc\""; break;
  1617. case Image::FORMAT_ATC: params+=" format=\"atc\""; break;
  1618. case Image::FORMAT_ATC_ALPHA_EXPLICIT: params+=" format=\"atcae\""; break;
  1619. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: params+=" format=\"atcai\""; break;
  1620. case Image::FORMAT_CUSTOM: params+=" format=\"custom\" custom_size=\""+itos(img.get_data().size())+"\""; break;
  1621. default: {}
  1622. }
  1623. } break;
  1624. case Variant::NODE_PATH: type="node_path"; break;
  1625. case Variant::OBJECT: {
  1626. type="resource";
  1627. RES res = p_property;
  1628. if (res.is_null()) {
  1629. write_tabs();
  1630. enter_tag(type,"name=\""+p_name+"\"");
  1631. exit_tag(type);
  1632. if (r_ok)
  1633. *r_ok=true;
  1634. return; // don't save it
  1635. }
  1636. if (external_resources.has(res)) {
  1637. params="external=\""+itos(external_resources[res])+"\"";
  1638. } else {
  1639. params="resource_type=\""+res->get_save_type()+"\"";
  1640. if (res->get_path().length() && res->get_path().find("::")==-1) {
  1641. //external resource
  1642. String path=relative_paths?local_path.path_to_file(res->get_path()):res->get_path();
  1643. escape(path);
  1644. params+=" path=\""+path+"\"";
  1645. } else {
  1646. //internal resource
  1647. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1648. ERR_FAIL_COND(!resource_set.has(res));
  1649. params+=" path=\"local://"+itos(res->get_subindex())+"\"";
  1650. }
  1651. }
  1652. } break;
  1653. case Variant::INPUT_EVENT: type="input_event"; break;
  1654. case Variant::DICTIONARY: type="dictionary"; params="shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1655. case Variant::ARRAY: type="array"; params="len=\""+itos(p_property.operator Array().size())+"\" shared=\""+String(p_property.is_shared()?"true":"false")+"\""; oneliner=false; break;
  1656. case Variant::RAW_ARRAY: type="raw_array"; params="len=\""+itos(p_property.operator DVector < uint8_t >().size())+"\""; break;
  1657. case Variant::INT_ARRAY: type="int_array"; params="len=\""+itos(p_property.operator DVector < int >().size())+"\""; break;
  1658. case Variant::REAL_ARRAY: type="real_array"; params="len=\""+itos(p_property.operator DVector < real_t >().size())+"\""; break;
  1659. case Variant::STRING_ARRAY: oneliner=false; type="string_array"; params="len=\""+itos(p_property.operator DVector < String >().size())+"\""; break;
  1660. case Variant::VECTOR2_ARRAY: type="vector2_array"; params="len=\""+itos(p_property.operator DVector < Vector2 >().size())+"\""; break;
  1661. case Variant::VECTOR3_ARRAY: type="vector3_array"; params="len=\""+itos(p_property.operator DVector < Vector3 >().size())+"\""; break;
  1662. case Variant::COLOR_ARRAY: type="color_array"; params="len=\""+itos(p_property.operator DVector < Color >().size())+"\""; break;
  1663. default: {
  1664. ERR_PRINT("Unknown Variant type.");
  1665. ERR_FAIL();
  1666. }
  1667. }
  1668. write_tabs();
  1669. if (p_name!="") {
  1670. if (params.length())
  1671. enter_tag(type,"name=\""+p_name+"\" "+params);
  1672. else
  1673. enter_tag(type,"name=\""+p_name+"\"");
  1674. } else {
  1675. if (params.length())
  1676. enter_tag(type," "+params);
  1677. else
  1678. enter_tag(type,String());
  1679. }
  1680. if (!oneliner)
  1681. f->store_8('\n');
  1682. else
  1683. f->store_8(' ');
  1684. switch( p_property.get_type() ) {
  1685. case Variant::NIL: {
  1686. } break;
  1687. case Variant::BOOL: {
  1688. write_string( p_property.operator bool() ? "True":"False" );
  1689. } break;
  1690. case Variant::INT: {
  1691. write_string( itos(p_property.operator int()) );
  1692. } break;
  1693. case Variant::REAL: {
  1694. write_string( rtos(p_property.operator real_t()) );
  1695. } break;
  1696. case Variant::STRING: {
  1697. String str=p_property;
  1698. escape(str);
  1699. str="\""+str+"\"";
  1700. write_string( str,false );
  1701. } break;
  1702. case Variant::VECTOR2: {
  1703. Vector2 v = p_property;
  1704. write_string( rtoss(v.x) +", "+rtoss(v.y) );
  1705. } break;
  1706. case Variant::RECT2: {
  1707. Rect2 aabb = p_property;
  1708. write_string( rtoss(aabb.pos.x) +", "+rtoss(aabb.pos.y) +", "+rtoss(aabb.size.x) +", "+rtoss(aabb.size.y) );
  1709. } break;
  1710. case Variant::VECTOR3: {
  1711. Vector3 v = p_property;
  1712. write_string( rtoss(v.x) +", "+rtoss(v.y)+", "+rtoss(v.z) );
  1713. } break;
  1714. case Variant::PLANE: {
  1715. Plane p = p_property;
  1716. write_string( rtoss(p.normal.x) +", "+rtoss(p.normal.y)+", "+rtoss(p.normal.z)+", "+rtoss(p.d) );
  1717. } break;
  1718. case Variant::_AABB: {
  1719. AABB aabb = p_property;
  1720. 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) );
  1721. } break;
  1722. case Variant::QUAT: {
  1723. Quat quat = p_property;
  1724. write_string( rtoss(quat.x)+", "+rtoss(quat.y)+", "+rtoss(quat.z)+", "+rtoss(quat.w)+", ");
  1725. } break;
  1726. case Variant::MATRIX32: {
  1727. String s;
  1728. Matrix32 m3 = p_property;
  1729. for (int i=0;i<3;i++) {
  1730. for (int j=0;j<2;j++) {
  1731. if (i!=0 || j!=0)
  1732. s+=", ";
  1733. s+=rtoss( m3.elements[i][j] );
  1734. }
  1735. }
  1736. write_string(s);
  1737. } break;
  1738. case Variant::MATRIX3: {
  1739. String s;
  1740. Matrix3 m3 = p_property;
  1741. for (int i=0;i<3;i++) {
  1742. for (int j=0;j<3;j++) {
  1743. if (i!=0 || j!=0)
  1744. s+=", ";
  1745. s+=rtoss( m3.elements[i][j] );
  1746. }
  1747. }
  1748. write_string(s);
  1749. } break;
  1750. case Variant::TRANSFORM: {
  1751. String s;
  1752. Transform t = p_property;
  1753. Matrix3 &m3 = t.basis;
  1754. for (int i=0;i<3;i++) {
  1755. for (int j=0;j<3;j++) {
  1756. if (i!=0 || j!=0)
  1757. s+=", ";
  1758. s+=rtoss( m3.elements[i][j] );
  1759. }
  1760. }
  1761. s=s+", "+rtoss(t.origin.x) +", "+rtoss(t.origin.y)+", "+rtoss(t.origin.z);
  1762. write_string(s);
  1763. } break;
  1764. // misc types
  1765. case Variant::COLOR: {
  1766. Color c = p_property;
  1767. write_string( rtoss(c.r) +", "+rtoss(c.g)+", "+rtoss(c.b)+", "+rtoss(c.a) );
  1768. } break;
  1769. case Variant::IMAGE: {
  1770. String s;
  1771. Image img = p_property;
  1772. DVector<uint8_t> data = img.get_data();
  1773. int len = data.size();
  1774. DVector<uint8_t>::Read r = data.read();
  1775. const uint8_t *ptr=r.ptr();;
  1776. for (int i=0;i<len;i++) {
  1777. uint8_t byte = ptr[i];
  1778. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1779. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1780. s+=str;
  1781. }
  1782. write_string(s);
  1783. } break;
  1784. case Variant::NODE_PATH: {
  1785. String str=p_property;
  1786. escape(str);
  1787. str="\""+str+"\"";
  1788. write_string( str,false);
  1789. } break;
  1790. case Variant::OBJECT: {
  1791. /* this saver does not save resources in here
  1792. RES res = p_property;
  1793. if (!res.is_null()) {
  1794. String path=res->get_path();
  1795. if (!res->is_shared() || !path.length()) {
  1796. // if no path, or path is from inside a scene
  1797. write_object( *res );
  1798. }
  1799. }
  1800. */
  1801. } break;
  1802. case Variant::INPUT_EVENT: {
  1803. write_string( p_property.operator String() );
  1804. } break;
  1805. case Variant::DICTIONARY: {
  1806. Dictionary dict = p_property;
  1807. List<Variant> keys;
  1808. dict.get_key_list(&keys);
  1809. keys.sort();
  1810. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1811. //if (!_check_type(dict[E->get()]))
  1812. // continue;
  1813. bool ok;
  1814. write_property("",E->get(),&ok);
  1815. ERR_CONTINUE(!ok);
  1816. write_property("",dict[E->get()],&ok);
  1817. if (!ok)
  1818. write_property("",Variant()); //at least make the file consistent..
  1819. }
  1820. } break;
  1821. case Variant::ARRAY: {
  1822. Array array = p_property;
  1823. int len=array.size();
  1824. for (int i=0;i<len;i++) {
  1825. write_property("",array[i]);
  1826. }
  1827. } break;
  1828. case Variant::RAW_ARRAY: {
  1829. String s;
  1830. DVector<uint8_t> data = p_property;
  1831. int len = data.size();
  1832. DVector<uint8_t>::Read r = data.read();
  1833. const uint8_t *ptr=r.ptr();;
  1834. for (int i=0;i<len;i++) {
  1835. uint8_t byte = ptr[i];
  1836. const char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1837. char str[3]={ hex[byte>>4], hex[byte&0xF], 0};
  1838. s+=str;
  1839. }
  1840. write_string(s,false);
  1841. } break;
  1842. case Variant::INT_ARRAY: {
  1843. DVector<int> data = p_property;
  1844. int len = data.size();
  1845. DVector<int>::Read r = data.read();
  1846. const int *ptr=r.ptr();;
  1847. write_tabs();
  1848. for (int i=0;i<len;i++) {
  1849. if (i>0)
  1850. write_string(", ",false);
  1851. write_string(itos(ptr[i]),false);
  1852. }
  1853. } break;
  1854. case Variant::REAL_ARRAY: {
  1855. DVector<real_t> data = p_property;
  1856. int len = data.size();
  1857. DVector<real_t>::Read r = data.read();
  1858. const real_t *ptr=r.ptr();;
  1859. write_tabs();
  1860. String cm=", " ;
  1861. for (int i=0;i<len;i++) {
  1862. if (i>0)
  1863. write_string(cm,false);
  1864. write_string(rtoss(ptr[i]),false);
  1865. }
  1866. } break;
  1867. case Variant::STRING_ARRAY: {
  1868. DVector<String> data = p_property;
  1869. int len = data.size();
  1870. DVector<String>::Read r = data.read();
  1871. const String *ptr=r.ptr();;
  1872. String s;
  1873. //write_string("\n");
  1874. for (int i=0;i<len;i++) {
  1875. write_tabs(0);
  1876. String str=ptr[i];
  1877. escape(str);
  1878. write_string("<string> \""+str+"\" </string>\n",false);
  1879. }
  1880. } break;
  1881. case Variant::VECTOR2_ARRAY: {
  1882. DVector<Vector2> data = p_property;
  1883. int len = data.size();
  1884. DVector<Vector2>::Read r = data.read();
  1885. const Vector2 *ptr=r.ptr();;
  1886. write_tabs();
  1887. for (int i=0;i<len;i++) {
  1888. if (i>0)
  1889. write_string(", ",false);
  1890. write_string(rtoss(ptr[i].x),false);
  1891. write_string(", "+rtoss(ptr[i].y),false);
  1892. }
  1893. } break;
  1894. case Variant::VECTOR3_ARRAY: {
  1895. DVector<Vector3> data = p_property;
  1896. int len = data.size();
  1897. DVector<Vector3>::Read r = data.read();
  1898. const Vector3 *ptr=r.ptr();;
  1899. write_tabs();
  1900. for (int i=0;i<len;i++) {
  1901. if (i>0)
  1902. write_string(", ",false);
  1903. write_string(rtoss(ptr[i].x),false);
  1904. write_string(", "+rtoss(ptr[i].y),false);
  1905. write_string(", "+rtoss(ptr[i].z),false);
  1906. }
  1907. } break;
  1908. case Variant::COLOR_ARRAY: {
  1909. DVector<Color> data = p_property;
  1910. int len = data.size();
  1911. DVector<Color>::Read r = data.read();
  1912. const Color *ptr=r.ptr();;
  1913. write_tabs();
  1914. for (int i=0;i<len;i++) {
  1915. if (i>0)
  1916. write_string(", ",false);
  1917. write_string(rtoss(ptr[i].r),false);
  1918. write_string(", "+rtoss(ptr[i].g),false);
  1919. write_string(", "+rtoss(ptr[i].b),false);
  1920. write_string(", "+rtoss(ptr[i].a),false);
  1921. }
  1922. } break;
  1923. default: {}
  1924. }
  1925. if (oneliner)
  1926. f->store_8(' ');
  1927. else
  1928. write_tabs(-1);
  1929. exit_tag(type);
  1930. f->store_8('\n');
  1931. if (r_ok)
  1932. *r_ok=true;
  1933. }
  1934. void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bool p_main) {
  1935. switch(p_variant.get_type()) {
  1936. case Variant::OBJECT: {
  1937. RES res = p_variant.operator RefPtr();
  1938. if (res.is_null() || external_resources.has(res))
  1939. return;
  1940. if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) {
  1941. int index = external_resources.size();
  1942. external_resources[res]=index;
  1943. return;
  1944. }
  1945. if (resource_set.has(res))
  1946. return;
  1947. List<PropertyInfo> property_list;
  1948. res->get_property_list( &property_list );
  1949. property_list.sort();
  1950. List<PropertyInfo>::Element *I=property_list.front();
  1951. while(I) {
  1952. PropertyInfo pi=I->get();
  1953. if (pi.usage&PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage&PROPERTY_USAGE_BUNDLE)) {
  1954. Variant v=res->get(I->get().name);
  1955. _find_resources(v);
  1956. }
  1957. I=I->next();
  1958. }
  1959. resource_set.insert( res ); //saved after, so the childs it needs are available when loaded
  1960. saved_resources.push_back(res);
  1961. } break;
  1962. case Variant::ARRAY: {
  1963. Array varray=p_variant;
  1964. int len=varray.size();
  1965. for(int i=0;i<len;i++) {
  1966. Variant v=varray.get(i);
  1967. _find_resources(v);
  1968. }
  1969. } break;
  1970. case Variant::DICTIONARY: {
  1971. Dictionary d=p_variant;
  1972. List<Variant> keys;
  1973. d.get_key_list(&keys);
  1974. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1975. Variant v = d[E->get()];
  1976. _find_resources(v);
  1977. }
  1978. } break;
  1979. default: {}
  1980. }
  1981. }
  1982. Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1983. Error err;
  1984. f = FileAccess::open(p_path, FileAccess::WRITE,&err);
  1985. ERR_FAIL_COND_V( err, ERR_CANT_OPEN );
  1986. FileAccessRef _fref(f);
  1987. local_path = Globals::get_singleton()->localize_path(p_path);
  1988. relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS;
  1989. skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1990. bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1991. takeover_paths=p_flags&ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1992. if (!p_path.begins_with("res://")) {
  1993. takeover_paths=false;
  1994. }
  1995. depth=0;
  1996. // save resources
  1997. _find_resources(p_resource,true);
  1998. ERR_FAIL_COND_V(err!=OK,err);
  1999. write_string("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",false); //no escape
  2000. write_string("\n",false);
  2001. 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+"\"");
  2002. write_string("\n",false);
  2003. for(Map<RES,int>::Element *E=external_resources.front();E;E=E->next()) {
  2004. write_tabs();
  2005. String p = E->key()->get_path();
  2006. enter_tag("ext_resource","path=\""+p+"\" type=\""+E->key()->get_save_type()+"\" index=\""+itos(E->get())+"\""); //bundled
  2007. exit_tag("ext_resource"); //bundled
  2008. write_string("\n",false);
  2009. }
  2010. Set<int> used_indices;
  2011. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  2012. RES res = E->get();
  2013. if (E->next() && (res->get_path()=="" || res->get_path().find("::") != -1 )) {
  2014. if (res->get_subindex()!=0) {
  2015. if (used_indices.has(res->get_subindex())) {
  2016. res->set_subindex(0); //repeated
  2017. } else {
  2018. used_indices.insert(res->get_subindex());
  2019. }
  2020. }
  2021. }
  2022. }
  2023. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  2024. RES res = E->get();
  2025. ERR_CONTINUE(!resource_set.has(res));
  2026. bool main = (E->next()==NULL);
  2027. write_tabs();
  2028. if (main)
  2029. enter_tag("main_resource",""); //bundled
  2030. else if (res->get_path().length() && res->get_path().find("::") == -1 )
  2031. enter_tag("resource","type=\""+res->get_type()+"\" path=\""+res->get_path()+"\""); //bundled
  2032. else {
  2033. if (res->get_subindex()==0) {
  2034. int new_subindex=1;
  2035. if (used_indices.size()) {
  2036. new_subindex=used_indices.back()->get()+1;
  2037. }
  2038. res->set_subindex(new_subindex);
  2039. used_indices.insert(new_subindex);
  2040. }
  2041. int idx = res->get_subindex();
  2042. enter_tag("resource","type=\""+res->get_type()+"\" path=\"local://"+itos(idx)+"\"");
  2043. if (takeover_paths) {
  2044. res->set_path(p_path+"::"+itos(idx),true);
  2045. }
  2046. }
  2047. write_string("\n",false);
  2048. List<PropertyInfo> property_list;
  2049. res->get_property_list(&property_list);
  2050. // property_list.sort();
  2051. for(List<PropertyInfo>::Element *PE = property_list.front();PE;PE=PE->next()) {
  2052. if (skip_editor && PE->get().name.begins_with("__editor"))
  2053. continue;
  2054. if (PE->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && PE->get().usage&PROPERTY_USAGE_BUNDLE)) {
  2055. String name = PE->get().name;
  2056. Variant value = res->get(name);
  2057. if ((PE->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero())||(PE->get().usage&PROPERTY_USAGE_STORE_IF_NONONE && value.is_one()) )
  2058. continue;
  2059. write_property(name,value);
  2060. }
  2061. }
  2062. write_string("\n",false);
  2063. write_tabs(-1);
  2064. if (main)
  2065. exit_tag("main_resource");
  2066. else
  2067. exit_tag("resource");
  2068. write_string("\n",false);
  2069. }
  2070. exit_tag("resource_file");
  2071. if (f->get_error()!=OK && f->get_error()!=ERR_FILE_EOF) {
  2072. f->close();
  2073. return ERR_CANT_CREATE;
  2074. }
  2075. f->close();
  2076. //memdelete(f);
  2077. return OK;
  2078. }
  2079. Error ResourceFormatSaverXML::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  2080. ResourceFormatSaverXMLInstance saver;
  2081. return saver.save(p_path,p_resource,p_flags);
  2082. }
  2083. bool ResourceFormatSaverXML::recognize(const RES& p_resource) const {
  2084. return true; // all recognized!
  2085. }
  2086. void ResourceFormatSaverXML::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  2087. //here comes the sun, lalalala
  2088. String base = p_resource->get_base_extension().to_lower();
  2089. p_extensions->push_back("xml");
  2090. if (base!="res") {
  2091. p_extensions->push_back("x"+base);
  2092. }
  2093. }
  2094. ResourceFormatSaverXML* ResourceFormatSaverXML::singleton=NULL;
  2095. ResourceFormatSaverXML::ResourceFormatSaverXML() {
  2096. singleton=this;
  2097. }