resource_format_xml.cpp 68 KB

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