resource_format_binary.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*************************************************************************/
  2. /* resource_format_binary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "version.h"
  30. #include "resource_format_binary.h"
  31. #include "globals.h"
  32. #include "io/file_access_compressed.h"
  33. #include "io/marshalls.h"
  34. //#define print_bl(m_what) print_line(m_what)
  35. #define print_bl(m_what)
  36. enum {
  37. //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization)
  38. VARIANT_NIL=1,
  39. VARIANT_BOOL=2,
  40. VARIANT_INT=3,
  41. VARIANT_REAL=4,
  42. VARIANT_STRING=5,
  43. VARIANT_VECTOR2=10,
  44. VARIANT_RECT2=11,
  45. VARIANT_VECTOR3=12,
  46. VARIANT_PLANE=13,
  47. VARIANT_QUAT=14,
  48. VARIANT_AABB=15,
  49. VARIANT_MATRIX3=16,
  50. VARIANT_TRANSFORM=17,
  51. VARIANT_MATRIX32=18,
  52. VARIANT_COLOR=20,
  53. VARIANT_IMAGE=21,
  54. VARIANT_NODE_PATH=22,
  55. VARIANT_RID=23,
  56. VARIANT_OBJECT=24,
  57. VARIANT_INPUT_EVENT=25,
  58. VARIANT_DICTIONARY=26,
  59. VARIANT_ARRAY=30,
  60. VARIANT_RAW_ARRAY=31,
  61. VARIANT_INT_ARRAY=32,
  62. VARIANT_REAL_ARRAY=33,
  63. VARIANT_STRING_ARRAY=34,
  64. VARIANT_VECTOR3_ARRAY=35,
  65. VARIANT_COLOR_ARRAY=36,
  66. VARIANT_VECTOR2_ARRAY=37,
  67. IMAGE_ENCODING_EMPTY=0,
  68. IMAGE_ENCODING_RAW=1,
  69. IMAGE_ENCODING_LOSSLESS=2,
  70. IMAGE_ENCODING_LOSSY=3,
  71. IMAGE_FORMAT_GRAYSCALE=0,
  72. IMAGE_FORMAT_INTENSITY=1,
  73. IMAGE_FORMAT_GRAYSCALE_ALPHA=2,
  74. IMAGE_FORMAT_RGB=3,
  75. IMAGE_FORMAT_RGBA=4,
  76. IMAGE_FORMAT_INDEXED=5,
  77. IMAGE_FORMAT_INDEXED_ALPHA=6,
  78. IMAGE_FORMAT_BC1=7,
  79. IMAGE_FORMAT_BC2=8,
  80. IMAGE_FORMAT_BC3=9,
  81. IMAGE_FORMAT_BC4=10,
  82. IMAGE_FORMAT_BC5=11,
  83. IMAGE_FORMAT_PVRTC2=12,
  84. IMAGE_FORMAT_PVRTC2_ALPHA=13,
  85. IMAGE_FORMAT_PVRTC4=14,
  86. IMAGE_FORMAT_PVRTC4_ALPHA=15,
  87. IMAGE_FORMAT_ETC=16,
  88. IMAGE_FORMAT_ATC=17,
  89. IMAGE_FORMAT_ATC_ALPHA_EXPLICIT=18,
  90. IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED=19,
  91. IMAGE_FORMAT_CUSTOM=30,
  92. OBJECT_EMPTY=0,
  93. OBJECT_EXTERNAL_RESOURCE=1,
  94. OBJECT_INTERNAL_RESOURCE=2,
  95. FORMAT_VERSION=0
  96. };
  97. void ResourceInteractiveLoaderBinary::_advance_padding(uint32_t p_len) {
  98. uint32_t extra = 4-(p_len%4);
  99. if (extra<4) {
  100. for(uint32_t i=0;i<extra;i++)
  101. f->get_8(); //pad to 32
  102. }
  103. }
  104. Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) {
  105. uint32_t type = f->get_32();
  106. print_bl("find property of type: "+itos(type));
  107. switch(type) {
  108. case VARIANT_NIL: {
  109. r_v=Variant();
  110. } break;
  111. case VARIANT_BOOL: {
  112. r_v=bool(f->get_32());
  113. } break;
  114. case VARIANT_INT: {
  115. r_v=int(f->get_32());
  116. } break;
  117. case VARIANT_REAL: {
  118. r_v=f->get_real();
  119. } break;
  120. case VARIANT_STRING: {
  121. r_v=get_unicode_string();
  122. } break;
  123. case VARIANT_VECTOR2: {
  124. Vector2 v;
  125. v.x=f->get_real();
  126. v.y=f->get_real();
  127. r_v=v;
  128. } break;
  129. case VARIANT_RECT2: {
  130. Rect2 v;
  131. v.pos.x=f->get_real();
  132. v.pos.y=f->get_real();
  133. v.size.x=f->get_real();
  134. v.size.y=f->get_real();
  135. r_v=v;
  136. } break;
  137. case VARIANT_VECTOR3: {
  138. Vector3 v;
  139. v.x=f->get_real();
  140. v.y=f->get_real();
  141. v.z=f->get_real();
  142. r_v=v;
  143. } break;
  144. case VARIANT_PLANE: {
  145. Plane v;
  146. v.normal.x=f->get_real();
  147. v.normal.y=f->get_real();
  148. v.normal.z=f->get_real();
  149. v.d=f->get_real();
  150. r_v=v;
  151. } break;
  152. case VARIANT_QUAT: {
  153. Quat v;
  154. v.x=f->get_real();
  155. v.y=f->get_real();
  156. v.z=f->get_real();
  157. v.w=f->get_real();
  158. r_v=v;
  159. } break;
  160. case VARIANT_AABB: {
  161. AABB v;
  162. v.pos.x=f->get_real();
  163. v.pos.y=f->get_real();
  164. v.pos.z=f->get_real();
  165. v.size.x=f->get_real();
  166. v.size.y=f->get_real();
  167. v.size.z=f->get_real();
  168. r_v=v;
  169. } break;
  170. case VARIANT_MATRIX32: {
  171. Matrix32 v;
  172. v.elements[0].x=f->get_real();
  173. v.elements[0].y=f->get_real();
  174. v.elements[1].x=f->get_real();
  175. v.elements[1].y=f->get_real();
  176. v.elements[2].x=f->get_real();
  177. v.elements[2].y=f->get_real();
  178. r_v=v;
  179. } break;
  180. case VARIANT_MATRIX3: {
  181. Matrix3 v;
  182. v.elements[0].x=f->get_real();
  183. v.elements[0].y=f->get_real();
  184. v.elements[0].z=f->get_real();
  185. v.elements[1].x=f->get_real();
  186. v.elements[1].y=f->get_real();
  187. v.elements[1].z=f->get_real();
  188. v.elements[2].x=f->get_real();
  189. v.elements[2].y=f->get_real();
  190. v.elements[2].z=f->get_real();
  191. r_v=v;
  192. } break;
  193. case VARIANT_TRANSFORM: {
  194. Transform v;
  195. v.basis.elements[0].x=f->get_real();
  196. v.basis.elements[0].y=f->get_real();
  197. v.basis.elements[0].z=f->get_real();
  198. v.basis.elements[1].x=f->get_real();
  199. v.basis.elements[1].y=f->get_real();
  200. v.basis.elements[1].z=f->get_real();
  201. v.basis.elements[2].x=f->get_real();
  202. v.basis.elements[2].y=f->get_real();
  203. v.basis.elements[2].z=f->get_real();
  204. v.origin.x=f->get_real();
  205. v.origin.y=f->get_real();
  206. v.origin.z=f->get_real();
  207. r_v=v;
  208. } break;
  209. case VARIANT_COLOR: {
  210. Color v;
  211. v.r=f->get_real();
  212. v.g=f->get_real();
  213. v.b=f->get_real();
  214. v.a=f->get_real();
  215. r_v=v;
  216. } break;
  217. case VARIANT_IMAGE: {
  218. uint32_t encoding = f->get_32();
  219. if (encoding==IMAGE_ENCODING_EMPTY) {
  220. r_v=Variant();
  221. break;
  222. } else if (encoding==IMAGE_ENCODING_RAW) {
  223. uint32_t width = f->get_32();
  224. uint32_t height = f->get_32();
  225. uint32_t mipmaps = f->get_32();
  226. uint32_t format = f->get_32();
  227. Image::Format fmt;
  228. switch(format) {
  229. case IMAGE_FORMAT_GRAYSCALE: { fmt=Image::FORMAT_GRAYSCALE; } break;
  230. case IMAGE_FORMAT_INTENSITY: { fmt=Image::FORMAT_INTENSITY; } break;
  231. case IMAGE_FORMAT_GRAYSCALE_ALPHA: { fmt=Image::FORMAT_GRAYSCALE_ALPHA; } break;
  232. case IMAGE_FORMAT_RGB: { fmt=Image::FORMAT_RGB; } break;
  233. case IMAGE_FORMAT_RGBA: { fmt=Image::FORMAT_RGBA; } break;
  234. case IMAGE_FORMAT_INDEXED: { fmt=Image::FORMAT_INDEXED; } break;
  235. case IMAGE_FORMAT_INDEXED_ALPHA: { fmt=Image::FORMAT_INDEXED_ALPHA; } break;
  236. case IMAGE_FORMAT_BC1: { fmt=Image::FORMAT_BC1; } break;
  237. case IMAGE_FORMAT_BC2: { fmt=Image::FORMAT_BC2; } break;
  238. case IMAGE_FORMAT_BC3: { fmt=Image::FORMAT_BC3; } break;
  239. case IMAGE_FORMAT_BC4: { fmt=Image::FORMAT_BC4; } break;
  240. case IMAGE_FORMAT_BC5: { fmt=Image::FORMAT_BC5; } break;
  241. case IMAGE_FORMAT_PVRTC2: { fmt=Image::FORMAT_PVRTC2; } break;
  242. case IMAGE_FORMAT_PVRTC2_ALPHA: { fmt=Image::FORMAT_PVRTC2_ALPHA; } break;
  243. case IMAGE_FORMAT_PVRTC4: { fmt=Image::FORMAT_PVRTC4; } break;
  244. case IMAGE_FORMAT_PVRTC4_ALPHA: { fmt=Image::FORMAT_PVRTC4_ALPHA; } break;
  245. case IMAGE_FORMAT_ETC: { fmt=Image::FORMAT_ETC; } break;
  246. case IMAGE_FORMAT_ATC: { fmt=Image::FORMAT_ATC; } break;
  247. case IMAGE_FORMAT_ATC_ALPHA_EXPLICIT: { fmt=Image::FORMAT_ATC_ALPHA_EXPLICIT; } break;
  248. case IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED: { fmt=Image::FORMAT_ATC_ALPHA_INTERPOLATED; } break;
  249. case IMAGE_FORMAT_CUSTOM: { fmt=Image::FORMAT_CUSTOM; } break;
  250. default: {
  251. ERR_FAIL_V(ERR_FILE_CORRUPT);
  252. }
  253. }
  254. uint32_t datalen = f->get_32();
  255. DVector<uint8_t> imgdata;
  256. imgdata.resize(datalen);
  257. DVector<uint8_t>::Write w = imgdata.write();
  258. f->get_buffer(w.ptr(),datalen);
  259. _advance_padding(datalen);
  260. w=DVector<uint8_t>::Write();
  261. r_v=Image(width,height,mipmaps,fmt,imgdata);
  262. } else {
  263. //compressed
  264. DVector<uint8_t> data;
  265. data.resize(f->get_32());
  266. DVector<uint8_t>::Write w = data.write();
  267. f->get_buffer(w.ptr(),data.size());
  268. w = DVector<uint8_t>::Write();
  269. Image img;
  270. if (encoding==IMAGE_ENCODING_LOSSY && Image::lossy_unpacker) {
  271. img = Image::lossy_unpacker(data);
  272. } else if (encoding==IMAGE_ENCODING_LOSSLESS && Image::lossless_unpacker) {
  273. img = Image::lossless_unpacker(data);
  274. }
  275. _advance_padding(data.size());
  276. r_v=img;
  277. }
  278. } break;
  279. case VARIANT_NODE_PATH: {
  280. Vector<StringName> names;
  281. Vector<StringName> subnames;
  282. StringName property;
  283. bool absolute;
  284. int name_count = f->get_16();
  285. uint32_t subname_count = f->get_16();
  286. absolute=subname_count&0x8000;
  287. subname_count&=0x7FFF;
  288. for(int i=0;i<name_count;i++)
  289. names.push_back(string_map[f->get_32()]);
  290. for(uint32_t i=0;i<subname_count;i++)
  291. subnames.push_back(string_map[f->get_32()]);
  292. property=string_map[f->get_32()];
  293. NodePath np = NodePath(names,subnames,absolute,property);
  294. //print_line("got path: "+String(np));
  295. r_v=np;
  296. } break;
  297. case VARIANT_RID: {
  298. r_v=f->get_32();
  299. } break;
  300. case VARIANT_OBJECT: {
  301. uint32_t type=f->get_32();
  302. switch(type) {
  303. case OBJECT_EMPTY: {
  304. //do none
  305. } break;
  306. case OBJECT_INTERNAL_RESOURCE: {
  307. uint32_t index=f->get_32();
  308. String path = res_path+"::"+itos(index);
  309. RES res = ResourceLoader::load(path);
  310. if (res.is_null()) {
  311. WARN_PRINT(String("Couldn't load resource: "+path).utf8().get_data());
  312. }
  313. r_v=res;
  314. } break;
  315. case OBJECT_EXTERNAL_RESOURCE: {
  316. String type = get_unicode_string();
  317. String path = get_unicode_string();
  318. if (path.find("://")==-1 && path.is_rel_path()) {
  319. // path is relative to file being loaded, so convert to a resource path
  320. path=Globals::get_singleton()->localize_path(res_path.get_base_dir()+"/"+path);
  321. }
  322. RES res=ResourceLoader::load(path,type);
  323. if (res.is_null()) {
  324. WARN_PRINT(String("Couldn't load resource: "+path).utf8().get_data());
  325. }
  326. r_v=res;
  327. } break;
  328. default: {
  329. ERR_FAIL_V(ERR_FILE_CORRUPT);
  330. } break;
  331. }
  332. } break;
  333. case VARIANT_INPUT_EVENT: {
  334. } break;
  335. case VARIANT_DICTIONARY: {
  336. uint32_t len=f->get_32();
  337. Dictionary d(len&0x80000000); //last bit means shared
  338. len&=0x7FFFFFFF;
  339. for(uint32_t i=0;i<len;i++) {
  340. Variant key;
  341. Error err = parse_variant(key);
  342. ERR_FAIL_COND_V(err,ERR_FILE_CORRUPT);
  343. Variant value;
  344. err = parse_variant(value);
  345. ERR_FAIL_COND_V(err,ERR_FILE_CORRUPT);
  346. d[key]=value;
  347. }
  348. r_v=d;
  349. } break;
  350. case VARIANT_ARRAY: {
  351. uint32_t len=f->get_32();
  352. Array a(len&0x80000000); //last bit means shared
  353. len&=0x7FFFFFFF;
  354. a.resize(len);
  355. for(uint32_t i=0;i<len;i++) {
  356. Variant val;
  357. Error err = parse_variant(val);
  358. ERR_FAIL_COND_V(err,ERR_FILE_CORRUPT);
  359. a[i]=val;
  360. }
  361. r_v=a;
  362. } break;
  363. case VARIANT_RAW_ARRAY: {
  364. uint32_t len = f->get_32();
  365. DVector<uint8_t> array;
  366. array.resize(len);
  367. DVector<uint8_t>::Write w = array.write();
  368. f->get_buffer(w.ptr(),len);
  369. _advance_padding(len);
  370. w=DVector<uint8_t>::Write();
  371. r_v=array;
  372. } break;
  373. case VARIANT_INT_ARRAY: {
  374. uint32_t len = f->get_32();
  375. DVector<int> array;
  376. array.resize(len);
  377. DVector<int>::Write w = array.write();
  378. f->get_buffer((uint8_t*)w.ptr(),len*4);
  379. #ifdef BIG_ENDIAN_ENABLED
  380. {
  381. uint32_t *ptr=(uint32_t*)w.ptr();
  382. for(int i=0;i<len;i++) {
  383. ptr[i]=BSWAP32(ptr[i]);
  384. }
  385. }
  386. #endif
  387. w=DVector<int>::Write();
  388. r_v=array;
  389. } break;
  390. case VARIANT_REAL_ARRAY: {
  391. uint32_t len = f->get_32();
  392. DVector<real_t> array;
  393. array.resize(len);
  394. DVector<real_t>::Write w = array.write();
  395. f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t));
  396. #ifdef BIG_ENDIAN_ENABLED
  397. {
  398. uint32_t *ptr=(uint32_t*)w.ptr();
  399. for(int i=0;i<len;i++) {
  400. ptr[i]=BSWAP32(ptr[i]);
  401. }
  402. }
  403. #endif
  404. w=DVector<real_t>::Write();
  405. r_v=array;
  406. } break;
  407. case VARIANT_STRING_ARRAY: {
  408. uint32_t len = f->get_32();
  409. DVector<String> array;
  410. array.resize(len);
  411. DVector<String>::Write w = array.write();
  412. for(uint32_t i=0;i<len;i++)
  413. w[i]=get_unicode_string();
  414. w=DVector<String>::Write();
  415. r_v=array;
  416. } break;
  417. case VARIANT_VECTOR2_ARRAY: {
  418. uint32_t len = f->get_32();
  419. DVector<Vector2> array;
  420. array.resize(len);
  421. DVector<Vector2>::Write w = array.write();
  422. if (sizeof(Vector2)==8) {
  423. f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*2);
  424. #ifdef BIG_ENDIAN_ENABLED
  425. {
  426. uint32_t *ptr=(uint32_t*)w.ptr();
  427. for(int i=0;i<len*2;i++) {
  428. ptr[i]=BSWAP32(ptr[i]);
  429. }
  430. }
  431. #endif
  432. } else {
  433. ERR_EXPLAIN("Vector2 size is NOT 8!");
  434. ERR_FAIL_V(ERR_UNAVAILABLE);
  435. }
  436. w=DVector<Vector2>::Write();
  437. r_v=array;
  438. } break;
  439. case VARIANT_VECTOR3_ARRAY: {
  440. uint32_t len = f->get_32();
  441. DVector<Vector3> array;
  442. array.resize(len);
  443. DVector<Vector3>::Write w = array.write();
  444. if (sizeof(Vector3)==12) {
  445. f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*3);
  446. #ifdef BIG_ENDIAN_ENABLED
  447. {
  448. uint32_t *ptr=(uint32_t*)w.ptr();
  449. for(int i=0;i<len*3;i++) {
  450. ptr[i]=BSWAP32(ptr[i]);
  451. }
  452. }
  453. #endif
  454. } else {
  455. ERR_EXPLAIN("Vector3 size is NOT 12!");
  456. ERR_FAIL_V(ERR_UNAVAILABLE);
  457. }
  458. w=DVector<Vector3>::Write();
  459. r_v=array;
  460. } break;
  461. case VARIANT_COLOR_ARRAY: {
  462. uint32_t len = f->get_32();
  463. DVector<Color> array;
  464. array.resize(len);
  465. DVector<Color>::Write w = array.write();
  466. if (sizeof(Color)==16) {
  467. f->get_buffer((uint8_t*)w.ptr(),len*sizeof(real_t)*4);
  468. #ifdef BIG_ENDIAN_ENABLED
  469. {
  470. uint32_t *ptr=(uint32_t*)w.ptr();
  471. for(int i=0;i<len*4;i++) {
  472. ptr[i]=BSWAP32(ptr[i]);
  473. }
  474. }
  475. #endif
  476. } else {
  477. ERR_EXPLAIN("Color size is NOT 16!");
  478. ERR_FAIL_V(ERR_UNAVAILABLE);
  479. }
  480. w=DVector<Color>::Write();
  481. r_v=array;
  482. } break;
  483. default: {
  484. ERR_FAIL_V(ERR_FILE_CORRUPT);
  485. } break;
  486. }
  487. return OK; //never reach anyway
  488. }
  489. void ResourceInteractiveLoaderBinary::set_local_path(const String& p_local_path) {
  490. res_path=p_local_path;
  491. }
  492. Ref<Resource> ResourceInteractiveLoaderBinary::get_resource(){
  493. return resource;
  494. }
  495. Error ResourceInteractiveLoaderBinary::poll(){
  496. if (error!=OK)
  497. return error;
  498. int s = stage;
  499. if (s<external_resources.size()) {
  500. RES res = ResourceLoader::load(external_resources[s].path,external_resources[s].type);
  501. if (res.is_null()) {
  502. if (!ResourceLoader::get_abort_on_missing_resources()) {
  503. ResourceLoader::notify_load_error("Resource Not Found: "+external_resources[s].path);
  504. } else {
  505. error=ERR_FILE_CORRUPT;
  506. ERR_EXPLAIN("Can't load dependency: "+external_resources[s].path);
  507. ERR_FAIL_V(error);
  508. }
  509. } else {
  510. resource_cache.push_back(res);
  511. }
  512. stage++;
  513. return OK;
  514. }
  515. s-=external_resources.size();
  516. if (s>=internal_resources.size()) {
  517. error=ERR_BUG;
  518. ERR_FAIL_COND_V(s>=internal_resources.size(),error);
  519. }
  520. bool main = s==(internal_resources.size()-1);
  521. //maybe it is loaded already
  522. String path;
  523. if (!main) {
  524. path=internal_resources[s].path;
  525. if (path.begins_with("local://"))
  526. path=path.replace("local://",res_path+"::");
  527. if (ResourceCache::has(path)) {
  528. //already loaded, don't do anything
  529. stage++;
  530. error=OK;
  531. return error;
  532. }
  533. } else {
  534. path=res_path;
  535. }
  536. uint64_t offset = internal_resources[s].offset;
  537. f->seek(offset);
  538. String t = get_unicode_string();
  539. Object *obj = ObjectTypeDB::instance(t);
  540. if (!obj) {
  541. error=ERR_FILE_CORRUPT;
  542. ERR_EXPLAIN(local_path+":Resource of unrecognized type in file: "+t);
  543. }
  544. ERR_FAIL_COND_V(!obj,ERR_FILE_CORRUPT);
  545. Resource *r = obj->cast_to<Resource>();
  546. if (!r) {
  547. error=ERR_FILE_CORRUPT;
  548. memdelete(obj); //bye
  549. ERR_EXPLAIN(local_path+":Resoucre type in resource field not a resource, type is: "+obj->get_type());
  550. ERR_FAIL_COND_V(!r,ERR_FILE_CORRUPT);
  551. }
  552. RES res = RES( r );
  553. r->set_path(path);
  554. int pc = f->get_32();
  555. //set properties
  556. for(int i=0;i<pc;i++) {
  557. uint32_t name_idx = f->get_32();
  558. if (name_idx>=(uint32_t)string_map.size()) {
  559. error=ERR_FILE_CORRUPT;
  560. ERR_FAIL_V(ERR_FILE_CORRUPT);
  561. }
  562. Variant value;
  563. error = parse_variant(value);
  564. if (error)
  565. return error;
  566. res->set(string_map[name_idx],value);
  567. }
  568. #ifdef TOOLS_ENABLED
  569. res->set_edited(false);
  570. #endif
  571. stage++;
  572. resource_cache.push_back(res);
  573. if (main) {
  574. if (importmd_ofs) {
  575. f->seek(importmd_ofs);
  576. Ref<ResourceImportMetadata> imd = memnew( ResourceImportMetadata );
  577. imd->set_editor(get_unicode_string());
  578. int sc = f->get_32();
  579. for(int i=0;i<sc;i++) {
  580. String src = get_unicode_string();
  581. String md5 = get_unicode_string();
  582. imd->add_source(src,md5);
  583. }
  584. int pc = f->get_32();
  585. for(int i=0;i<pc;i++) {
  586. String name = get_unicode_string();
  587. Variant val;
  588. parse_variant(val);
  589. imd->set_option(name,val);
  590. }
  591. res->set_import_metadata(imd);
  592. }
  593. f->close();
  594. resource=res;
  595. error=ERR_FILE_EOF;
  596. } else {
  597. error=OK;
  598. }
  599. return OK;
  600. }
  601. int ResourceInteractiveLoaderBinary::get_stage() const{
  602. return stage;
  603. }
  604. int ResourceInteractiveLoaderBinary::get_stage_count() const {
  605. return external_resources.size()+internal_resources.size();
  606. }
  607. String ResourceInteractiveLoaderBinary::get_unicode_string() {
  608. int len = f->get_32();
  609. if (len>str_buf.size()) {
  610. str_buf.resize(len);
  611. }
  612. f->get_buffer((uint8_t*)&str_buf[0],len);
  613. String s;
  614. s.parse_utf8(&str_buf[0]);
  615. return s;
  616. }
  617. void ResourceInteractiveLoaderBinary::get_dependencies(FileAccess *p_f,List<String> *p_dependencies) {
  618. open(p_f);
  619. if (error)
  620. return;
  621. for(int i=0;i<external_resources.size();i++) {
  622. p_dependencies->push_back(external_resources[i].path);
  623. }
  624. }
  625. void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) {
  626. error=OK;
  627. f=p_f;
  628. uint8_t header[4];
  629. f->get_buffer(header,4);
  630. if (header[0]=='R' && header[1]=='S' && header[2]=='C' && header[3]=='C') {
  631. //compressed
  632. FileAccessCompressed *fac = memnew( FileAccessCompressed );
  633. fac->open_after_magic(f);
  634. f=fac;
  635. } else if (header[0]!='R' || header[1]!='S' || header[2]!='R' || header[3]!='C') {
  636. //not normal
  637. error=ERR_FILE_UNRECOGNIZED;
  638. ERR_EXPLAIN("Unrecognized binary resource file: "+local_path);
  639. ERR_FAIL_V();
  640. }
  641. bool big_endian = f->get_32();
  642. #ifdef BIG_ENDIAN_ENABLED
  643. endian_swap = !big_endian;
  644. #else
  645. bool endian_swap = big_endian;
  646. #endif
  647. bool use_real64 = f->get_32();
  648. f->set_endian_swap(big_endian!=0); //read big endian if saved as big endian
  649. uint32_t ver_major=f->get_32();
  650. uint32_t ver_minor=f->get_32();
  651. uint32_t ver_format=f->get_32();
  652. print_bl("big endian: "+itos(big_endian));
  653. print_bl("endian swap: "+itos(endian_swap));
  654. print_bl("real64: "+itos(use_real64));
  655. print_bl("major: "+itos(ver_major));
  656. print_bl("minor: "+itos(ver_minor));
  657. print_bl("format: "+itos(ver_format));
  658. if (ver_format<FORMAT_VERSION || ver_major>VERSION_MAJOR || (ver_major==VERSION_MAJOR && ver_minor>VERSION_MINOR)) {
  659. f->close();
  660. ERR_EXPLAIN("File Format '"+itos(FORMAT_VERSION)+"."+itos(ver_major)+"."+itos(ver_minor)+"' is too new! Please upgrade to a a new engine version: "+local_path);
  661. ERR_FAIL();
  662. }
  663. type=get_unicode_string();
  664. print_bl("type: "+type);
  665. importmd_ofs = f->get_64();
  666. for(int i=0;i<14;i++)
  667. f->get_32(); //skip a few reserved fields
  668. uint32_t string_table_size=f->get_32();
  669. string_map.resize(string_table_size);
  670. for(uint32_t i=0;i<string_table_size;i++) {
  671. StringName s = get_unicode_string();
  672. string_map[i]=s;
  673. }
  674. print_bl("strings: "+itos(string_table_size));
  675. uint32_t ext_resources_size=f->get_32();
  676. for(uint32_t i=0;i<ext_resources_size;i++) {
  677. ExtResoucre er;
  678. er.type=get_unicode_string();
  679. er.path=get_unicode_string();
  680. external_resources.push_back(er);
  681. }
  682. print_bl("ext resources: "+itos(ext_resources_size));
  683. uint32_t int_resources_size=f->get_32();
  684. for(uint32_t i=0;i<int_resources_size;i++) {
  685. IntResoucre ir;
  686. ir.path=get_unicode_string();
  687. ir.offset=f->get_64();
  688. internal_resources.push_back(ir);
  689. }
  690. print_bl("int resources: "+itos(int_resources_size));
  691. if (f->eof_reached()) {
  692. error=ERR_FILE_CORRUPT;
  693. ERR_EXPLAIN("Premature End Of File: "+local_path);
  694. ERR_FAIL();
  695. }
  696. }
  697. String ResourceInteractiveLoaderBinary::recognize(FileAccess *p_f) {
  698. error=OK;
  699. f=p_f;
  700. uint8_t header[4];
  701. f->get_buffer(header,4);
  702. if (header[0]=='R' && header[1]=='S' && header[2]=='C' && header[3]=='C') {
  703. //compressed
  704. FileAccessCompressed *fac = memnew( FileAccessCompressed );
  705. fac->open_after_magic(f);
  706. f=fac;
  707. } else if (header[0]!='R' || header[1]!='S' || header[2]!='R' || header[3]!='C') {
  708. //not normal
  709. return "";
  710. }
  711. bool big_endian = f->get_32();
  712. #ifdef BIG_ENDIAN_ENABLED
  713. endian_swap = !big_endian;
  714. #else
  715. bool endian_swap = big_endian;
  716. #endif
  717. bool use_real64 = f->get_32();
  718. f->set_endian_swap(big_endian!=0); //read big endian if saved as big endian
  719. uint32_t ver_major=f->get_32();
  720. uint32_t ver_minor=f->get_32();
  721. uint32_t ver_format=f->get_32();
  722. if (ver_format<FORMAT_VERSION || ver_major>VERSION_MAJOR || (ver_major==VERSION_MAJOR && ver_minor>VERSION_MINOR)) {
  723. f->close();
  724. return "";
  725. }
  726. String type=get_unicode_string();
  727. return type;
  728. }
  729. ResourceInteractiveLoaderBinary::ResourceInteractiveLoaderBinary() {
  730. f=NULL;
  731. stage=0;
  732. endian_swap=false;
  733. use_real64=false;
  734. error=OK;
  735. }
  736. ResourceInteractiveLoaderBinary::~ResourceInteractiveLoaderBinary() {
  737. if (f)
  738. memdelete(f);
  739. }
  740. Ref<ResourceInteractiveLoader> ResourceFormatLoaderBinary::load_interactive(const String &p_path) {
  741. Error err;
  742. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  743. if (err!=OK) {
  744. ERR_FAIL_COND_V(err!=OK,Ref<ResourceInteractiveLoader>());
  745. }
  746. Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary );
  747. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  748. ria->res_path=ria->local_path;
  749. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  750. ria->open(f);
  751. return ria;
  752. }
  753. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const {
  754. if (p_type=="") {
  755. get_recognized_extensions(p_extensions);
  756. return;
  757. }
  758. List<String> extensions;
  759. ObjectTypeDB::get_extensions_for_type(p_type,&extensions);
  760. extensions.sort();
  761. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  762. String ext = E->get().to_lower();
  763. if (ext=="res")
  764. continue;
  765. // p_extensions->push_back("x"+ext);
  766. p_extensions->push_back(ext);
  767. }
  768. p_extensions->push_back("res");
  769. }
  770. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const{
  771. List<String> extensions;
  772. ObjectTypeDB::get_resource_base_extensions(&extensions);
  773. extensions.sort();
  774. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  775. String ext = E->get().to_lower();
  776. if (ext=="res")
  777. continue;
  778. p_extensions->push_back(ext);
  779. }
  780. p_extensions->push_back("res");
  781. }
  782. bool ResourceFormatLoaderBinary::handles_type(const String& p_type) const{
  783. return true; //handles all
  784. }
  785. Error ResourceFormatLoaderBinary::load_import_metadata(const String &p_path, Ref<ResourceImportMetadata>& r_var) const {
  786. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  787. if (!f) {
  788. return ERR_FILE_CANT_OPEN;
  789. }
  790. Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary );
  791. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  792. ria->res_path=ria->local_path;
  793. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  794. ria->recognize(f);
  795. if(ria->error!=OK)
  796. return ERR_FILE_UNRECOGNIZED;
  797. f=ria->f;
  798. uint64_t imp_ofs = f->get_64();
  799. if (imp_ofs==0)
  800. return ERR_UNAVAILABLE;
  801. f->seek(imp_ofs);
  802. Ref<ResourceImportMetadata> imd = memnew( ResourceImportMetadata );
  803. imd->set_editor(ria->get_unicode_string());
  804. int sc = f->get_32();
  805. for(int i=0;i<sc;i++) {
  806. String src = ria->get_unicode_string();
  807. String md5 = ria->get_unicode_string();
  808. imd->add_source(src,md5);
  809. }
  810. int pc = f->get_32();
  811. for(int i=0;i<pc;i++) {
  812. String name = ria->get_unicode_string();
  813. Variant val;
  814. ria->parse_variant(val);
  815. imd->set_option(name,val);
  816. }
  817. r_var=imd;
  818. return OK;
  819. }
  820. void ResourceFormatLoaderBinary::get_dependencies(const String& p_path,List<String> *p_dependencies) {
  821. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  822. ERR_FAIL_COND(!f);
  823. Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary );
  824. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  825. ria->res_path=ria->local_path;
  826. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  827. ria->get_dependencies(f,p_dependencies);
  828. }
  829. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  830. FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
  831. if (!f) {
  832. return ""; //could not rwead
  833. }
  834. Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary );
  835. ria->local_path=Globals::get_singleton()->localize_path(p_path);
  836. ria->res_path=ria->local_path;
  837. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  838. String r = ria->recognize(f);
  839. return r;
  840. }
  841. ///////////////////////////////////////////////////////////
  842. ///////////////////////////////////////////////////////////
  843. ///////////////////////////////////////////////////////////
  844. void ResourceFormatSaverBinaryInstance::_pad_buffer(int p_bytes) {
  845. int extra = 4-(p_bytes%4);
  846. if (extra<4) {
  847. for(int i=0;i<extra;i++)
  848. f->store_8(0); //pad to 32
  849. }
  850. }
  851. void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property,const PropertyInfo& p_hint) {
  852. switch(p_property.get_type()) {
  853. case Variant::NIL: {
  854. f->store_32(VARIANT_NIL);
  855. // don't store anything
  856. } break;
  857. case Variant::BOOL: {
  858. f->store_32(VARIANT_BOOL);
  859. bool val=p_property;
  860. f->store_32(val);
  861. } break;
  862. case Variant::INT: {
  863. f->store_32(VARIANT_INT);
  864. int val=p_property;
  865. f->store_32(val);
  866. } break;
  867. case Variant::REAL: {
  868. f->store_32(VARIANT_REAL);
  869. real_t val=p_property;
  870. f->store_real(val);
  871. } break;
  872. case Variant::STRING: {
  873. f->store_32(VARIANT_STRING);
  874. String val=p_property;
  875. save_unicode_string(val);
  876. } break;
  877. case Variant::VECTOR2: {
  878. f->store_32(VARIANT_VECTOR2);
  879. Vector2 val=p_property;
  880. f->store_real(val.x);
  881. f->store_real(val.y);
  882. } break;
  883. case Variant::RECT2: {
  884. f->store_32(VARIANT_RECT2);
  885. Rect2 val=p_property;
  886. f->store_real(val.pos.x);
  887. f->store_real(val.pos.y);
  888. f->store_real(val.size.x);
  889. f->store_real(val.size.y);
  890. } break;
  891. case Variant::VECTOR3: {
  892. f->store_32(VARIANT_VECTOR3);
  893. Vector3 val=p_property;
  894. f->store_real(val.x);
  895. f->store_real(val.y);
  896. f->store_real(val.z);
  897. } break;
  898. case Variant::PLANE: {
  899. f->store_32(VARIANT_PLANE);
  900. Plane val=p_property;
  901. f->store_real(val.normal.x);
  902. f->store_real(val.normal.y);
  903. f->store_real(val.normal.z);
  904. f->store_real(val.d);
  905. } break;
  906. case Variant::QUAT: {
  907. f->store_32(VARIANT_QUAT);
  908. Quat val=p_property;
  909. f->store_real(val.x);
  910. f->store_real(val.y);
  911. f->store_real(val.z);
  912. f->store_real(val.w);
  913. } break;
  914. case Variant::_AABB: {
  915. f->store_32(VARIANT_AABB);
  916. AABB val=p_property;
  917. f->store_real(val.pos.x);
  918. f->store_real(val.pos.y);
  919. f->store_real(val.pos.z);
  920. f->store_real(val.size.x);
  921. f->store_real(val.size.y);
  922. f->store_real(val.size.z);
  923. } break;
  924. case Variant::MATRIX32: {
  925. f->store_32(VARIANT_MATRIX32);
  926. Matrix32 val=p_property;
  927. f->store_real(val.elements[0].x);
  928. f->store_real(val.elements[0].y);
  929. f->store_real(val.elements[1].x);
  930. f->store_real(val.elements[1].y);
  931. f->store_real(val.elements[2].x);
  932. f->store_real(val.elements[2].y);
  933. } break;
  934. case Variant::MATRIX3: {
  935. f->store_32(VARIANT_MATRIX3);
  936. Matrix3 val=p_property;
  937. f->store_real(val.elements[0].x);
  938. f->store_real(val.elements[0].y);
  939. f->store_real(val.elements[0].z);
  940. f->store_real(val.elements[1].x);
  941. f->store_real(val.elements[1].y);
  942. f->store_real(val.elements[1].z);
  943. f->store_real(val.elements[2].x);
  944. f->store_real(val.elements[2].y);
  945. f->store_real(val.elements[2].z);
  946. } break;
  947. case Variant::TRANSFORM: {
  948. f->store_32(VARIANT_TRANSFORM);
  949. Transform val=p_property;
  950. f->store_real(val.basis.elements[0].x);
  951. f->store_real(val.basis.elements[0].y);
  952. f->store_real(val.basis.elements[0].z);
  953. f->store_real(val.basis.elements[1].x);
  954. f->store_real(val.basis.elements[1].y);
  955. f->store_real(val.basis.elements[1].z);
  956. f->store_real(val.basis.elements[2].x);
  957. f->store_real(val.basis.elements[2].y);
  958. f->store_real(val.basis.elements[2].z);
  959. f->store_real(val.origin.x);
  960. f->store_real(val.origin.y);
  961. f->store_real(val.origin.z);
  962. } break;
  963. case Variant::COLOR: {
  964. f->store_32(VARIANT_COLOR);
  965. Color val=p_property;
  966. f->store_real(val.r);
  967. f->store_real(val.g);
  968. f->store_real(val.b);
  969. f->store_real(val.a);
  970. } break;
  971. case Variant::IMAGE: {
  972. f->store_32(VARIANT_IMAGE);
  973. Image val =p_property;
  974. if (val.empty()) {
  975. f->store_32(IMAGE_ENCODING_EMPTY);
  976. break;
  977. }
  978. int encoding=IMAGE_ENCODING_RAW;
  979. float quality=0.7;
  980. if (val.get_format() <= Image::FORMAT_INDEXED_ALPHA) {
  981. //can only compress uncompressed stuff
  982. if (p_hint.hint==PROPERTY_HINT_IMAGE_COMPRESS_LOSSY && Image::lossy_packer) {
  983. encoding=IMAGE_ENCODING_LOSSY;
  984. float qs=p_hint.hint_string.to_double();
  985. if (qs!=0.0)
  986. quality=qs;
  987. } else if (p_hint.hint==PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS && Image::lossless_packer) {
  988. encoding=IMAGE_ENCODING_LOSSLESS;
  989. }
  990. }
  991. f->store_32(encoding); //raw encoding
  992. if (encoding==IMAGE_ENCODING_RAW) {
  993. f->store_32(val.get_width());
  994. f->store_32(val.get_height());
  995. f->store_32(val.get_mipmaps());
  996. switch(val.get_format()) {
  997. case Image::FORMAT_GRAYSCALE: f->store_32(IMAGE_FORMAT_GRAYSCALE ); break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  998. case Image::FORMAT_INTENSITY: f->store_32(IMAGE_FORMAT_INTENSITY ); break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  999. case Image::FORMAT_GRAYSCALE_ALPHA: f->store_32(IMAGE_FORMAT_GRAYSCALE_ALPHA ); break; ///< two bytes per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255. alpha 0-255
  1000. case Image::FORMAT_RGB: f->store_32(IMAGE_FORMAT_RGB ); break; ///< one byte R: f->store_32(IMAGE_FORMAT_ ); break; one byte G: f->store_32(IMAGE_FORMAT_ ); break; one byte B
  1001. case Image::FORMAT_RGBA: f->store_32(IMAGE_FORMAT_RGBA ); break; ///< one byte R: f->store_32(IMAGE_FORMAT_ ); break; one byte G: f->store_32(IMAGE_FORMAT_ ); break; one byte B: f->store_32(IMAGE_FORMAT_ ); break; one byte A
  1002. case Image::FORMAT_INDEXED: f->store_32(IMAGE_FORMAT_INDEXED ); break; ///< index byte 0-256: f->store_32(IMAGE_FORMAT_ ); break; and after image end: f->store_32(IMAGE_FORMAT_ ); break; 256*3 bytes of palette
  1003. case Image::FORMAT_INDEXED_ALPHA: f->store_32(IMAGE_FORMAT_INDEXED_ALPHA ); break; ///< index byte 0-256: f->store_32(IMAGE_FORMAT_ ); break; and after image end: f->store_32(IMAGE_FORMAT_ ); break; 256*4 bytes of palette (alpha)
  1004. case Image::FORMAT_BC1: f->store_32(IMAGE_FORMAT_BC1 ); break; // DXT1
  1005. case Image::FORMAT_BC2: f->store_32(IMAGE_FORMAT_BC2 ); break; // DXT3
  1006. case Image::FORMAT_BC3: f->store_32(IMAGE_FORMAT_BC3 ); break; // DXT5
  1007. case Image::FORMAT_BC4: f->store_32(IMAGE_FORMAT_BC4 ); break; // ATI1
  1008. case Image::FORMAT_BC5: f->store_32(IMAGE_FORMAT_BC5 ); break; // ATI2
  1009. case Image::FORMAT_PVRTC2: f->store_32(IMAGE_FORMAT_PVRTC2 ); break;
  1010. case Image::FORMAT_PVRTC2_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC2_ALPHA ); break;
  1011. case Image::FORMAT_PVRTC4: f->store_32(IMAGE_FORMAT_PVRTC4 ); break;
  1012. case Image::FORMAT_PVRTC4_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC4_ALPHA ); break;
  1013. case Image::FORMAT_ETC: f->store_32(IMAGE_FORMAT_ETC); break;
  1014. case Image::FORMAT_ATC: f->store_32(IMAGE_FORMAT_ATC); break;
  1015. case Image::FORMAT_ATC_ALPHA_EXPLICIT: f->store_32(IMAGE_FORMAT_ATC_ALPHA_EXPLICIT); break;
  1016. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: f->store_32(IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED); break;
  1017. case Image::FORMAT_CUSTOM: f->store_32(IMAGE_FORMAT_CUSTOM ); break;
  1018. default: {}
  1019. }
  1020. int dlen = val.get_data().size();
  1021. f->store_32(dlen);
  1022. DVector<uint8_t>::Read r = val.get_data().read();
  1023. f->store_buffer(r.ptr(),dlen);
  1024. _pad_buffer(dlen);
  1025. } else {
  1026. DVector<uint8_t> data;
  1027. if (encoding==IMAGE_ENCODING_LOSSY) {
  1028. data=Image::lossy_packer(val,quality);
  1029. } else if (encoding==IMAGE_ENCODING_LOSSLESS) {
  1030. data=Image::lossless_packer(val);
  1031. }
  1032. int ds=data.size();
  1033. f->store_32(ds);
  1034. if (ds>0) {
  1035. DVector<uint8_t>::Read r = data.read();
  1036. f->store_buffer(r.ptr(),ds);
  1037. _pad_buffer(ds);
  1038. }
  1039. }
  1040. } break;
  1041. case Variant::NODE_PATH: {
  1042. f->store_32(VARIANT_NODE_PATH);
  1043. NodePath np=p_property;
  1044. f->store_16(np.get_name_count());
  1045. uint16_t snc = np.get_subname_count();
  1046. if (np.is_absolute())
  1047. snc|=0x8000;
  1048. f->store_16(snc);
  1049. for(int i=0;i<np.get_name_count();i++)
  1050. f->store_32(get_string_index(np.get_name(i)));
  1051. for(int i=0;i<np.get_subname_count();i++)
  1052. f->store_32(get_string_index(np.get_subname(i)));
  1053. f->store_32(get_string_index(np.get_property()));
  1054. } break;
  1055. case Variant::_RID: {
  1056. f->store_32(VARIANT_RID);
  1057. WARN_PRINT("Can't save RIDs");
  1058. RID val = p_property;
  1059. f->store_32(val.get_id());
  1060. } break;
  1061. case Variant::OBJECT: {
  1062. f->store_32(VARIANT_OBJECT);
  1063. RES res = p_property;
  1064. if (res.is_null()) {
  1065. f->store_32(OBJECT_EMPTY);
  1066. return; // don't save it
  1067. }
  1068. if (res->get_path().length() && res->get_path().find("::")==-1) {
  1069. f->store_32(OBJECT_EXTERNAL_RESOURCE);
  1070. save_unicode_string(res->get_save_type());
  1071. String path=relative_paths?local_path.path_to_file(res->get_path()):res->get_path();
  1072. if (no_extensions)
  1073. path=path.basename()+".*";
  1074. save_unicode_string(path);
  1075. } else {
  1076. if (!resource_map.has(res)) {
  1077. f->store_32(OBJECT_EMPTY);
  1078. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1079. ERR_FAIL();
  1080. }
  1081. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1082. f->store_32(resource_map[res]);
  1083. //internal resource
  1084. }
  1085. } break;
  1086. case Variant::INPUT_EVENT: {
  1087. f->store_32(VARIANT_INPUT_EVENT);
  1088. WARN_PRINT("Can't save InputEvent (maybe it could..)");
  1089. } break;
  1090. case Variant::DICTIONARY: {
  1091. f->store_32(VARIANT_DICTIONARY);
  1092. Dictionary d = p_property;
  1093. f->store_32(uint32_t(d.size())|(d.is_shared()?0x80000000:0));
  1094. List<Variant> keys;
  1095. d.get_key_list(&keys);
  1096. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1097. //if (!_check_type(dict[E->get()]))
  1098. // continue;
  1099. write_variant(E->get());
  1100. write_variant(d[E->get()]);
  1101. }
  1102. } break;
  1103. case Variant::ARRAY: {
  1104. f->store_32(VARIANT_ARRAY);
  1105. Array a=p_property;
  1106. f->store_32(uint32_t(a.size())|(a.is_shared()?0x80000000:0));
  1107. for(int i=0;i<a.size();i++) {
  1108. write_variant(a[i]);
  1109. }
  1110. } break;
  1111. case Variant::RAW_ARRAY: {
  1112. f->store_32(VARIANT_RAW_ARRAY);
  1113. DVector<uint8_t> arr = p_property;
  1114. int len=arr.size();
  1115. f->store_32(len);
  1116. DVector<uint8_t>::Read r = arr.read();
  1117. f->store_buffer(r.ptr(),len);
  1118. _pad_buffer(len);
  1119. } break;
  1120. case Variant::INT_ARRAY: {
  1121. f->store_32(VARIANT_INT_ARRAY);
  1122. DVector<int> arr = p_property;
  1123. int len=arr.size();
  1124. f->store_32(len);
  1125. DVector<int>::Read r = arr.read();
  1126. for(int i=0;i<len;i++)
  1127. f->store_32(r[i]);
  1128. } break;
  1129. case Variant::REAL_ARRAY: {
  1130. f->store_32(VARIANT_REAL_ARRAY);
  1131. DVector<real_t> arr = p_property;
  1132. int len=arr.size();
  1133. f->store_32(len);
  1134. DVector<real_t>::Read r = arr.read();
  1135. for(int i=0;i<len;i++) {
  1136. f->store_real(r[i]);
  1137. }
  1138. } break;
  1139. case Variant::STRING_ARRAY: {
  1140. f->store_32(VARIANT_STRING_ARRAY);
  1141. DVector<String> arr = p_property;
  1142. int len=arr.size();
  1143. f->store_32(len);
  1144. DVector<String>::Read r = arr.read();
  1145. for(int i=0;i<len;i++) {
  1146. save_unicode_string(r[i]);
  1147. }
  1148. } break;
  1149. case Variant::VECTOR3_ARRAY: {
  1150. f->store_32(VARIANT_VECTOR3_ARRAY);
  1151. DVector<Vector3> arr = p_property;
  1152. int len=arr.size();
  1153. f->store_32(len);
  1154. DVector<Vector3>::Read r = arr.read();
  1155. for(int i=0;i<len;i++) {
  1156. f->store_real(r[i].x);
  1157. f->store_real(r[i].y);
  1158. f->store_real(r[i].z);
  1159. }
  1160. } break;
  1161. case Variant::VECTOR2_ARRAY: {
  1162. f->store_32(VARIANT_VECTOR2_ARRAY);
  1163. DVector<Vector2> arr = p_property;
  1164. int len=arr.size();
  1165. f->store_32(len);
  1166. DVector<Vector2>::Read r = arr.read();
  1167. for(int i=0;i<len;i++) {
  1168. f->store_real(r[i].x);
  1169. f->store_real(r[i].y);
  1170. }
  1171. } break;
  1172. case Variant::COLOR_ARRAY: {
  1173. f->store_32(VARIANT_COLOR_ARRAY);
  1174. DVector<Color> arr = p_property;
  1175. int len=arr.size();
  1176. f->store_32(len);
  1177. DVector<Color>::Read r = arr.read();
  1178. for(int i=0;i<len;i++) {
  1179. f->store_real(r[i].r);
  1180. f->store_real(r[i].g);
  1181. f->store_real(r[i].b);
  1182. f->store_real(r[i].a);
  1183. }
  1184. } break;
  1185. default: {
  1186. ERR_EXPLAIN("Invalid variant");
  1187. ERR_FAIL();
  1188. }
  1189. }
  1190. }
  1191. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant& p_variant,bool p_main) {
  1192. switch(p_variant.get_type()) {
  1193. case Variant::OBJECT: {
  1194. RES res = p_variant.operator RefPtr();
  1195. if (res.is_null())
  1196. return;
  1197. if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) {
  1198. external_resources.insert(res);
  1199. return;
  1200. }
  1201. if (resource_map.has(res))
  1202. return;
  1203. List<PropertyInfo> property_list;
  1204. res->get_property_list(&property_list);
  1205. for(List<PropertyInfo>::Element *E=property_list.front();E;E=E->next()) {
  1206. if (E->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1207. _find_resources(res->get(E->get().name));
  1208. }
  1209. }
  1210. resource_map[ res ] = saved_resources.size();
  1211. saved_resources.push_back(res);
  1212. } break;
  1213. case Variant::ARRAY: {
  1214. Array varray=p_variant;
  1215. int len=varray.size();
  1216. for(int i=0;i<len;i++) {
  1217. Variant v=varray.get(i);
  1218. _find_resources(v);
  1219. }
  1220. } break;
  1221. case Variant::DICTIONARY: {
  1222. Dictionary d=p_variant;
  1223. List<Variant> keys;
  1224. d.get_key_list(&keys);
  1225. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1226. _find_resources(E->get());
  1227. Variant v = d[E->get()];
  1228. _find_resources(v);
  1229. }
  1230. } break;
  1231. case Variant::NODE_PATH: {
  1232. //take the chance and save node path strings
  1233. NodePath np = p_variant;
  1234. for(int i=0;i<np.get_name_count();i++)
  1235. get_string_index(np.get_name(i));
  1236. for(int i=0;i<np.get_subname_count();i++)
  1237. get_string_index(np.get_subname(i));
  1238. get_string_index(np.get_property());
  1239. } break;
  1240. default: {}
  1241. }
  1242. }
  1243. #if 0
  1244. Error ResourceFormatSaverBinary::_save_obj(const Object *p_object,SavedObject *so) {
  1245. //use classic way
  1246. List<PropertyInfo> property_list;
  1247. p_object->get_property_list( &property_list );
  1248. for(List<PropertyInfo>::Element *E=property_list.front();E;E=E->next()) {
  1249. if (skip_editor && E->get().name.begins_with("__editor"))
  1250. continue;
  1251. if (E->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1252. SavedObject::SavedProperty sp;
  1253. sp.name_idx=get_string_index(E->get().name);
  1254. sp.value = p_object->get(E->get().name);
  1255. _find_resources(sp.value);
  1256. so->properties.push_back(sp);
  1257. }
  1258. }
  1259. return OK;
  1260. }
  1261. Error ResourceFormatSaverBinary::save(const Object *p_object,const Variant &p_meta) {
  1262. ERR_FAIL_COND_V(!f,ERR_UNCONFIGURED);
  1263. ERR_EXPLAIN("write_object should supply either an object, a meta, or both");
  1264. ERR_FAIL_COND_V(!p_object && p_meta.get_type()==Variant::NIL, ERR_INVALID_PARAMETER);
  1265. SavedObject *so = memnew( SavedObject );
  1266. if (p_object)
  1267. so->type=p_object->get_type();
  1268. _find_resources(p_meta);
  1269. so->meta=p_meta;
  1270. Error err = _save_obj(p_object,so);
  1271. ERR_FAIL_COND_V( err, ERR_INVALID_DATA );
  1272. saved_objects.push_back(so);
  1273. return OK;
  1274. }
  1275. #endif
  1276. void ResourceFormatSaverBinaryInstance::save_unicode_string(const String& p_string) {
  1277. CharString utf8 = p_string.utf8();
  1278. f->store_32(utf8.length()+1);
  1279. f->store_buffer((const uint8_t*)utf8.get_data(),utf8.length()+1);
  1280. }
  1281. int ResourceFormatSaverBinaryInstance::get_string_index(const String& p_string) {
  1282. StringName s=p_string;
  1283. if (string_map.has(s))
  1284. return string_map[s];
  1285. string_map[s]=strings.size();
  1286. strings.push_back(s);
  1287. return strings.size()-1;
  1288. }
  1289. Error ResourceFormatSaverBinaryInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1290. Error err;
  1291. if (p_flags&ResourceSaver::FLAG_COMPRESS) {
  1292. FileAccessCompressed *fac = memnew( FileAccessCompressed );
  1293. fac->configure("RSCC");
  1294. f=fac;
  1295. err = fac->_open(p_path,FileAccess::WRITE);
  1296. if (err)
  1297. memdelete(f);
  1298. } else {
  1299. f=FileAccess::open(p_path,FileAccess::WRITE,&err);
  1300. }
  1301. ERR_FAIL_COND_V(err,err);
  1302. FileAccessRef _fref(f);
  1303. relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS;
  1304. skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1305. bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1306. big_endian=p_flags&ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1307. no_extensions=p_flags&ResourceSaver::FLAG_NO_EXTENSION;
  1308. local_path=p_path.get_base_dir();
  1309. //bin_meta_idx = get_string_index("__bin_meta__"); //is often used, so create
  1310. _find_resources(p_resource,true);
  1311. if (!(p_flags&ResourceSaver::FLAG_COMPRESS)) {
  1312. //save header compressed
  1313. static const uint8_t header[4]={'R','S','R','C'};
  1314. f->store_buffer(header,4);
  1315. }
  1316. if (big_endian) {
  1317. f->store_32(1);
  1318. f->set_endian_swap(true);
  1319. } else
  1320. f->store_32(0);
  1321. f->store_32(0); //64 bits file, false for now
  1322. f->store_32(VERSION_MAJOR);
  1323. f->store_32(VERSION_MINOR);
  1324. f->store_32(FORMAT_VERSION);
  1325. //f->store_32(saved_resources.size()+external_resources.size()); // load steps -not needed
  1326. save_unicode_string(p_resource->get_type());
  1327. uint64_t md_at = f->get_pos();
  1328. f->store_64(0); //offset to impoty metadata
  1329. for(int i=0;i<14;i++)
  1330. f->store_32(0); // reserved
  1331. List<ResourceData> resources;
  1332. {
  1333. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  1334. ResourceData &rd = resources.push_back(ResourceData())->get();
  1335. rd.type=E->get()->get_type();
  1336. List<PropertyInfo> property_list;
  1337. E->get()->get_property_list( &property_list );
  1338. for(List<PropertyInfo>::Element *F=property_list.front();F;F=F->next()) {
  1339. if (skip_editor && F->get().name.begins_with("__editor"))
  1340. continue;
  1341. if (F->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && F->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1342. Property p;
  1343. p.name_idx=get_string_index(F->get().name);
  1344. p.value=E->get()->get(F->get().name);
  1345. if (F->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && p.value.is_zero())
  1346. continue;
  1347. p.pi=F->get();
  1348. rd.properties.push_back(p);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. f->store_32(strings.size()); //string table size
  1354. for(int i=0;i<strings.size();i++) {
  1355. //print_bl("saving string: "+strings[i]);
  1356. save_unicode_string(strings[i]);
  1357. }
  1358. // save external resource table
  1359. f->store_32(external_resources.size()); //amount of external resources
  1360. for(Set<RES>::Element *E=external_resources.front();E;E=E->next()) {
  1361. save_unicode_string(E->get()->get_save_type());
  1362. String path = E->get()->get_path();
  1363. if (no_extensions)
  1364. path=path.basename()+".*";
  1365. save_unicode_string(path);
  1366. }
  1367. // save internal resource table
  1368. f->store_32(saved_resources.size()); //amount of internal resources
  1369. Vector<uint64_t> ofs_pos;
  1370. for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) {
  1371. RES r = E->get();
  1372. if (r->get_path()=="" || r->get_path().find("::")!=-1)
  1373. save_unicode_string("local://"+itos(ofs_pos.size()));
  1374. else
  1375. save_unicode_string(r->get_path()); //actual external
  1376. ofs_pos.push_back(f->get_pos());
  1377. f->store_64(0); //offset in 64 bits
  1378. }
  1379. Vector<uint64_t> ofs_table;
  1380. // int saved_idx=0;
  1381. //now actually save the resources
  1382. for(List<ResourceData>::Element *E=resources.front();E;E=E->next()) {
  1383. ResourceData & rd = E->get();
  1384. ofs_table.push_back(f->get_pos());
  1385. save_unicode_string(rd.type);
  1386. f->store_32(rd.properties.size());
  1387. for (List<Property>::Element *F=rd.properties.front();F;F=F->next()) {
  1388. Property &p=F->get();
  1389. f->store_32(p.name_idx);
  1390. write_variant(p.value,F->get().pi);
  1391. }
  1392. }
  1393. for(int i=0;i<ofs_table.size();i++) {
  1394. f->seek(ofs_pos[i]);
  1395. f->store_64(ofs_table[i]);
  1396. }
  1397. f->seek_end();
  1398. if (p_resource->get_import_metadata().is_valid()) {
  1399. uint64_t md_pos = f->get_pos();
  1400. Ref<ResourceImportMetadata> imd=p_resource->get_import_metadata();
  1401. save_unicode_string(imd->get_editor());
  1402. f->store_32(imd->get_source_count());
  1403. for(int i=0;i<imd->get_source_count();i++) {
  1404. save_unicode_string(imd->get_source_path(i));
  1405. save_unicode_string(imd->get_source_md5(i));
  1406. }
  1407. List<String> options;
  1408. imd->get_options(&options);
  1409. f->store_32(options.size());
  1410. for(List<String>::Element *E=options.front();E;E=E->next()) {
  1411. save_unicode_string(E->get());
  1412. write_variant(imd->get_option(E->get()));
  1413. }
  1414. f->seek(md_at);
  1415. f->store_64(md_pos);
  1416. f->seek_end();
  1417. }
  1418. f->store_buffer((const uint8_t*)"RSRC",4); //magic at end
  1419. f->close();
  1420. return OK;
  1421. }
  1422. Error ResourceFormatSaverBinary::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1423. String local_path = Globals::get_singleton()->localize_path(p_path);
  1424. ResourceFormatSaverBinaryInstance saver;
  1425. return saver.save(local_path,p_resource,p_flags);
  1426. }
  1427. bool ResourceFormatSaverBinary::recognize(const RES& p_resource) const {
  1428. return true; //all recognized
  1429. }
  1430. void ResourceFormatSaverBinary::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1431. //here comes the sun, lalalala
  1432. String base = p_resource->get_base_extension().to_lower();
  1433. if (base!="res") {
  1434. p_extensions->push_back(base);
  1435. }
  1436. p_extensions->push_back("res");
  1437. }