resource_format_xml.cpp 60 KB

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