resource_format_xml.cpp 65 KB

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