resource_format_xml.cpp 65 KB

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