resource_format_xml.cpp 60 KB

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