resource_format_binary.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335
  1. /*************************************************************************/
  2. /* resource_format_binary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "resource_format_binary.h"
  31. #include "globals.h"
  32. #include "io/file_access_compressed.h"
  33. #include "io/marshalls.h"
  34. #include "os/dir_access.h"
  35. #include "version.h"
  36. //#define print_bl(m_what) print_line(m_what)
  37. #define print_bl(m_what)
  38. enum {
  39. //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization)
  40. VARIANT_NIL = 1,
  41. VARIANT_BOOL = 2,
  42. VARIANT_INT = 3,
  43. VARIANT_REAL = 4,
  44. VARIANT_STRING = 5,
  45. VARIANT_VECTOR2 = 10,
  46. VARIANT_RECT2 = 11,
  47. VARIANT_VECTOR3 = 12,
  48. VARIANT_PLANE = 13,
  49. VARIANT_QUAT = 14,
  50. VARIANT_AABB = 15,
  51. VARIANT_MATRIX3 = 16,
  52. VARIANT_TRANSFORM = 17,
  53. VARIANT_MATRIX32 = 18,
  54. VARIANT_COLOR = 20,
  55. VARIANT_IMAGE = 21,
  56. VARIANT_NODE_PATH = 22,
  57. VARIANT_RID = 23,
  58. VARIANT_OBJECT = 24,
  59. VARIANT_INPUT_EVENT = 25,
  60. VARIANT_DICTIONARY = 26,
  61. VARIANT_ARRAY = 30,
  62. VARIANT_RAW_ARRAY = 31,
  63. VARIANT_INT_ARRAY = 32,
  64. VARIANT_REAL_ARRAY = 33,
  65. VARIANT_STRING_ARRAY = 34,
  66. VARIANT_VECTOR3_ARRAY = 35,
  67. VARIANT_COLOR_ARRAY = 36,
  68. VARIANT_VECTOR2_ARRAY = 37,
  69. IMAGE_ENCODING_EMPTY = 0,
  70. IMAGE_ENCODING_RAW = 1,
  71. IMAGE_ENCODING_LOSSLESS = 2,
  72. IMAGE_ENCODING_LOSSY = 3,
  73. IMAGE_FORMAT_GRAYSCALE = 0,
  74. IMAGE_FORMAT_INTENSITY = 1,
  75. IMAGE_FORMAT_GRAYSCALE_ALPHA = 2,
  76. IMAGE_FORMAT_RGB = 3,
  77. IMAGE_FORMAT_RGBA = 4,
  78. IMAGE_FORMAT_INDEXED = 5,
  79. IMAGE_FORMAT_INDEXED_ALPHA = 6,
  80. IMAGE_FORMAT_BC1 = 7,
  81. IMAGE_FORMAT_BC2 = 8,
  82. IMAGE_FORMAT_BC3 = 9,
  83. IMAGE_FORMAT_BC4 = 10,
  84. IMAGE_FORMAT_BC5 = 11,
  85. IMAGE_FORMAT_PVRTC2 = 12,
  86. IMAGE_FORMAT_PVRTC2_ALPHA = 13,
  87. IMAGE_FORMAT_PVRTC4 = 14,
  88. IMAGE_FORMAT_PVRTC4_ALPHA = 15,
  89. IMAGE_FORMAT_ETC = 16,
  90. IMAGE_FORMAT_ATC = 17,
  91. IMAGE_FORMAT_ATC_ALPHA_EXPLICIT = 18,
  92. IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED = 19,
  93. IMAGE_FORMAT_CUSTOM = 30,
  94. OBJECT_EMPTY = 0,
  95. OBJECT_EXTERNAL_RESOURCE = 1,
  96. OBJECT_INTERNAL_RESOURCE = 2,
  97. OBJECT_EXTERNAL_RESOURCE_INDEX = 3,
  98. FORMAT_VERSION = 1,
  99. FORMAT_VERSION_CAN_RENAME_DEPS = 1
  100. };
  101. void ResourceInteractiveLoaderBinary::_advance_padding(uint32_t p_len) {
  102. uint32_t extra = 4 - (p_len % 4);
  103. if (extra < 4) {
  104. for (uint32_t i = 0; i < extra; i++)
  105. f->get_8(); //pad to 32
  106. }
  107. }
  108. Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v, bool p_for_export_data) {
  109. uint32_t type = f->get_32();
  110. print_bl("find property of type: " + itos(type));
  111. switch (type) {
  112. case VARIANT_NIL: {
  113. r_v = Variant();
  114. } break;
  115. case VARIANT_BOOL: {
  116. r_v = bool(f->get_32());
  117. } break;
  118. case VARIANT_INT: {
  119. r_v = int(f->get_32());
  120. } break;
  121. case VARIANT_REAL: {
  122. r_v = f->get_real();
  123. } break;
  124. case VARIANT_STRING: {
  125. r_v = get_unicode_string();
  126. } break;
  127. case VARIANT_VECTOR2: {
  128. Vector2 v;
  129. v.x = f->get_real();
  130. v.y = f->get_real();
  131. r_v = v;
  132. } break;
  133. case VARIANT_RECT2: {
  134. Rect2 v;
  135. v.pos.x = f->get_real();
  136. v.pos.y = f->get_real();
  137. v.size.x = f->get_real();
  138. v.size.y = f->get_real();
  139. r_v = v;
  140. } break;
  141. case VARIANT_VECTOR3: {
  142. Vector3 v;
  143. v.x = f->get_real();
  144. v.y = f->get_real();
  145. v.z = f->get_real();
  146. r_v = v;
  147. } break;
  148. case VARIANT_PLANE: {
  149. Plane v;
  150. v.normal.x = f->get_real();
  151. v.normal.y = f->get_real();
  152. v.normal.z = f->get_real();
  153. v.d = f->get_real();
  154. r_v = v;
  155. } break;
  156. case VARIANT_QUAT: {
  157. Quat v;
  158. v.x = f->get_real();
  159. v.y = f->get_real();
  160. v.z = f->get_real();
  161. v.w = f->get_real();
  162. r_v = v;
  163. } break;
  164. case VARIANT_AABB: {
  165. AABB v;
  166. v.pos.x = f->get_real();
  167. v.pos.y = f->get_real();
  168. v.pos.z = f->get_real();
  169. v.size.x = f->get_real();
  170. v.size.y = f->get_real();
  171. v.size.z = f->get_real();
  172. r_v = v;
  173. } break;
  174. case VARIANT_MATRIX32: {
  175. Matrix32 v;
  176. v.elements[0].x = f->get_real();
  177. v.elements[0].y = f->get_real();
  178. v.elements[1].x = f->get_real();
  179. v.elements[1].y = f->get_real();
  180. v.elements[2].x = f->get_real();
  181. v.elements[2].y = f->get_real();
  182. r_v = v;
  183. } break;
  184. case VARIANT_MATRIX3: {
  185. Matrix3 v;
  186. v.elements[0].x = f->get_real();
  187. v.elements[0].y = f->get_real();
  188. v.elements[0].z = f->get_real();
  189. v.elements[1].x = f->get_real();
  190. v.elements[1].y = f->get_real();
  191. v.elements[1].z = f->get_real();
  192. v.elements[2].x = f->get_real();
  193. v.elements[2].y = f->get_real();
  194. v.elements[2].z = f->get_real();
  195. r_v = v;
  196. } break;
  197. case VARIANT_TRANSFORM: {
  198. Transform v;
  199. v.basis.elements[0].x = f->get_real();
  200. v.basis.elements[0].y = f->get_real();
  201. v.basis.elements[0].z = f->get_real();
  202. v.basis.elements[1].x = f->get_real();
  203. v.basis.elements[1].y = f->get_real();
  204. v.basis.elements[1].z = f->get_real();
  205. v.basis.elements[2].x = f->get_real();
  206. v.basis.elements[2].y = f->get_real();
  207. v.basis.elements[2].z = f->get_real();
  208. v.origin.x = f->get_real();
  209. v.origin.y = f->get_real();
  210. v.origin.z = f->get_real();
  211. r_v = v;
  212. } break;
  213. case VARIANT_COLOR: {
  214. Color v;
  215. v.r = f->get_real();
  216. v.g = f->get_real();
  217. v.b = f->get_real();
  218. v.a = f->get_real();
  219. r_v = v;
  220. } break;
  221. case VARIANT_IMAGE: {
  222. uint32_t encoding = f->get_32();
  223. if (encoding == IMAGE_ENCODING_EMPTY) {
  224. r_v = Variant();
  225. break;
  226. } else if (encoding == IMAGE_ENCODING_RAW) {
  227. uint32_t width = f->get_32();
  228. uint32_t height = f->get_32();
  229. uint32_t mipmaps = f->get_32();
  230. uint32_t format = f->get_32();
  231. Image::Format fmt;
  232. switch (format) {
  233. case IMAGE_FORMAT_GRAYSCALE: {
  234. fmt = Image::FORMAT_GRAYSCALE;
  235. } break;
  236. case IMAGE_FORMAT_INTENSITY: {
  237. fmt = Image::FORMAT_INTENSITY;
  238. } break;
  239. case IMAGE_FORMAT_GRAYSCALE_ALPHA: {
  240. fmt = Image::FORMAT_GRAYSCALE_ALPHA;
  241. } break;
  242. case IMAGE_FORMAT_RGB: {
  243. fmt = Image::FORMAT_RGB;
  244. } break;
  245. case IMAGE_FORMAT_RGBA: {
  246. fmt = Image::FORMAT_RGBA;
  247. } break;
  248. case IMAGE_FORMAT_INDEXED: {
  249. fmt = Image::FORMAT_INDEXED;
  250. } break;
  251. case IMAGE_FORMAT_INDEXED_ALPHA: {
  252. fmt = Image::FORMAT_INDEXED_ALPHA;
  253. } break;
  254. case IMAGE_FORMAT_BC1: {
  255. fmt = Image::FORMAT_BC1;
  256. } break;
  257. case IMAGE_FORMAT_BC2: {
  258. fmt = Image::FORMAT_BC2;
  259. } break;
  260. case IMAGE_FORMAT_BC3: {
  261. fmt = Image::FORMAT_BC3;
  262. } break;
  263. case IMAGE_FORMAT_BC4: {
  264. fmt = Image::FORMAT_BC4;
  265. } break;
  266. case IMAGE_FORMAT_BC5: {
  267. fmt = Image::FORMAT_BC5;
  268. } break;
  269. case IMAGE_FORMAT_PVRTC2: {
  270. fmt = Image::FORMAT_PVRTC2;
  271. } break;
  272. case IMAGE_FORMAT_PVRTC2_ALPHA: {
  273. fmt = Image::FORMAT_PVRTC2_ALPHA;
  274. } break;
  275. case IMAGE_FORMAT_PVRTC4: {
  276. fmt = Image::FORMAT_PVRTC4;
  277. } break;
  278. case IMAGE_FORMAT_PVRTC4_ALPHA: {
  279. fmt = Image::FORMAT_PVRTC4_ALPHA;
  280. } break;
  281. case IMAGE_FORMAT_ETC: {
  282. fmt = Image::FORMAT_ETC;
  283. } break;
  284. case IMAGE_FORMAT_ATC: {
  285. fmt = Image::FORMAT_ATC;
  286. } break;
  287. case IMAGE_FORMAT_ATC_ALPHA_EXPLICIT: {
  288. fmt = Image::FORMAT_ATC_ALPHA_EXPLICIT;
  289. } break;
  290. case IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED: {
  291. fmt = Image::FORMAT_ATC_ALPHA_INTERPOLATED;
  292. } break;
  293. case IMAGE_FORMAT_CUSTOM: {
  294. fmt = Image::FORMAT_CUSTOM;
  295. } break;
  296. default: {
  297. ERR_FAIL_V(ERR_FILE_CORRUPT);
  298. }
  299. }
  300. uint32_t datalen = f->get_32();
  301. DVector<uint8_t> imgdata;
  302. imgdata.resize(datalen);
  303. DVector<uint8_t>::Write w = imgdata.write();
  304. f->get_buffer(w.ptr(), datalen);
  305. _advance_padding(datalen);
  306. w = DVector<uint8_t>::Write();
  307. r_v = Image(width, height, mipmaps, fmt, imgdata);
  308. } else {
  309. //compressed
  310. DVector<uint8_t> data;
  311. data.resize(f->get_32());
  312. DVector<uint8_t>::Write w = data.write();
  313. f->get_buffer(w.ptr(), data.size());
  314. w = DVector<uint8_t>::Write();
  315. Image img;
  316. if (encoding == IMAGE_ENCODING_LOSSY && Image::lossy_unpacker) {
  317. img = Image::lossy_unpacker(data);
  318. } else if (encoding == IMAGE_ENCODING_LOSSLESS && Image::lossless_unpacker) {
  319. img = Image::lossless_unpacker(data);
  320. }
  321. _advance_padding(data.size());
  322. r_v = img;
  323. }
  324. } break;
  325. case VARIANT_NODE_PATH: {
  326. Vector<StringName> names;
  327. Vector<StringName> subnames;
  328. StringName property;
  329. bool absolute;
  330. int name_count = f->get_16();
  331. uint32_t subname_count = f->get_16();
  332. absolute = subname_count & 0x8000;
  333. subname_count &= 0x7FFF;
  334. for (int i = 0; i < name_count; i++)
  335. names.push_back(string_map[f->get_32()]);
  336. for (uint32_t i = 0; i < subname_count; i++)
  337. subnames.push_back(string_map[f->get_32()]);
  338. property = string_map[f->get_32()];
  339. NodePath np = NodePath(names, subnames, absolute, property);
  340. //print_line("got path: "+String(np));
  341. r_v = np;
  342. } break;
  343. case VARIANT_RID: {
  344. r_v = f->get_32();
  345. } break;
  346. case VARIANT_OBJECT: {
  347. uint32_t type = f->get_32();
  348. switch (type) {
  349. case OBJECT_EMPTY: {
  350. //do none
  351. } break;
  352. case OBJECT_INTERNAL_RESOURCE: {
  353. uint32_t index = f->get_32();
  354. if (p_for_export_data) {
  355. r_v = "@RESLOCAL:" + itos(index);
  356. } else {
  357. String path = res_path + "::" + itos(index);
  358. RES res = ResourceLoader::load(path);
  359. if (res.is_null()) {
  360. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  361. }
  362. r_v = res;
  363. }
  364. } break;
  365. case OBJECT_EXTERNAL_RESOURCE: {
  366. //old file format, still around for compatibility
  367. String type = get_unicode_string();
  368. String path = get_unicode_string();
  369. if (p_for_export_data) {
  370. r_v = "@RESPATH:" + type + ":" + path;
  371. } else {
  372. if (path.find("://") == -1 && path.is_rel_path()) {
  373. // path is relative to file being loaded, so convert to a resource path
  374. path = Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  375. }
  376. if (remaps.find(path)) {
  377. path = remaps[path];
  378. }
  379. RES res = ResourceLoader::load(path, type);
  380. if (res.is_null()) {
  381. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  382. }
  383. r_v = res;
  384. }
  385. } break;
  386. case OBJECT_EXTERNAL_RESOURCE_INDEX: {
  387. //new file format, just refers to an index in the external list
  388. uint32_t erindex = f->get_32();
  389. if (p_for_export_data) {
  390. r_v = "@RESEXTERNAL:" + itos(erindex);
  391. } else {
  392. if (erindex >= external_resources.size()) {
  393. WARN_PRINT("Broken external resource! (index out of size");
  394. r_v = Variant();
  395. } else {
  396. String type = external_resources[erindex].type;
  397. String path = external_resources[erindex].path;
  398. if (path.find("://") == -1 && path.is_rel_path()) {
  399. // path is relative to file being loaded, so convert to a resource path
  400. path = Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  401. }
  402. RES res = ResourceLoader::load(path, type);
  403. if (res.is_null()) {
  404. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  405. }
  406. r_v = res;
  407. }
  408. }
  409. } break;
  410. default: {
  411. ERR_FAIL_V(ERR_FILE_CORRUPT);
  412. } break;
  413. }
  414. } break;
  415. case VARIANT_INPUT_EVENT: {
  416. } break;
  417. case VARIANT_DICTIONARY: {
  418. uint32_t len = f->get_32();
  419. Dictionary d(len & 0x80000000); //last bit means shared
  420. len &= 0x7FFFFFFF;
  421. for (uint32_t i = 0; i < len; i++) {
  422. Variant key;
  423. Error err = parse_variant(key, p_for_export_data);
  424. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  425. Variant value;
  426. err = parse_variant(value, p_for_export_data);
  427. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  428. d[key] = value;
  429. }
  430. r_v = d;
  431. } break;
  432. case VARIANT_ARRAY: {
  433. uint32_t len = f->get_32();
  434. Array a(len & 0x80000000); //last bit means shared
  435. len &= 0x7FFFFFFF;
  436. a.resize(len);
  437. for (uint32_t i = 0; i < len; i++) {
  438. Variant val;
  439. Error err = parse_variant(val, p_for_export_data);
  440. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  441. a[i] = val;
  442. }
  443. r_v = a;
  444. } break;
  445. case VARIANT_RAW_ARRAY: {
  446. uint32_t len = f->get_32();
  447. DVector<uint8_t> array;
  448. array.resize(len);
  449. DVector<uint8_t>::Write w = array.write();
  450. f->get_buffer(w.ptr(), len);
  451. _advance_padding(len);
  452. w = DVector<uint8_t>::Write();
  453. r_v = array;
  454. } break;
  455. case VARIANT_INT_ARRAY: {
  456. uint32_t len = f->get_32();
  457. DVector<int> array;
  458. array.resize(len);
  459. DVector<int>::Write w = array.write();
  460. f->get_buffer((uint8_t *)w.ptr(), len * 4);
  461. #ifdef BIG_ENDIAN_ENABLED
  462. {
  463. uint32_t *ptr = (uint32_t *)w.ptr();
  464. for (int i = 0; i < len; i++) {
  465. ptr[i] = BSWAP32(ptr[i]);
  466. }
  467. }
  468. #endif
  469. w = DVector<int>::Write();
  470. r_v = array;
  471. } break;
  472. case VARIANT_REAL_ARRAY: {
  473. uint32_t len = f->get_32();
  474. DVector<real_t> array;
  475. array.resize(len);
  476. DVector<real_t>::Write w = array.write();
  477. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t));
  478. #ifdef BIG_ENDIAN_ENABLED
  479. {
  480. uint32_t *ptr = (uint32_t *)w.ptr();
  481. for (int i = 0; i < len; i++) {
  482. ptr[i] = BSWAP32(ptr[i]);
  483. }
  484. }
  485. #endif
  486. w = DVector<real_t>::Write();
  487. r_v = array;
  488. } break;
  489. case VARIANT_STRING_ARRAY: {
  490. uint32_t len = f->get_32();
  491. DVector<String> array;
  492. array.resize(len);
  493. DVector<String>::Write w = array.write();
  494. for (uint32_t i = 0; i < len; i++)
  495. w[i] = get_unicode_string();
  496. w = DVector<String>::Write();
  497. r_v = array;
  498. } break;
  499. case VARIANT_VECTOR2_ARRAY: {
  500. uint32_t len = f->get_32();
  501. DVector<Vector2> array;
  502. array.resize(len);
  503. DVector<Vector2>::Write w = array.write();
  504. if (sizeof(Vector2) == 8) {
  505. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 2);
  506. #ifdef BIG_ENDIAN_ENABLED
  507. {
  508. uint32_t *ptr = (uint32_t *)w.ptr();
  509. for (int i = 0; i < len * 2; i++) {
  510. ptr[i] = BSWAP32(ptr[i]);
  511. }
  512. }
  513. #endif
  514. } else {
  515. ERR_EXPLAIN("Vector2 size is NOT 8!");
  516. ERR_FAIL_V(ERR_UNAVAILABLE);
  517. }
  518. w = DVector<Vector2>::Write();
  519. r_v = array;
  520. } break;
  521. case VARIANT_VECTOR3_ARRAY: {
  522. uint32_t len = f->get_32();
  523. DVector<Vector3> array;
  524. array.resize(len);
  525. DVector<Vector3>::Write w = array.write();
  526. if (sizeof(Vector3) == 12) {
  527. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 3);
  528. #ifdef BIG_ENDIAN_ENABLED
  529. {
  530. uint32_t *ptr = (uint32_t *)w.ptr();
  531. for (int i = 0; i < len * 3; i++) {
  532. ptr[i] = BSWAP32(ptr[i]);
  533. }
  534. }
  535. #endif
  536. } else {
  537. ERR_EXPLAIN("Vector3 size is NOT 12!");
  538. ERR_FAIL_V(ERR_UNAVAILABLE);
  539. }
  540. w = DVector<Vector3>::Write();
  541. r_v = array;
  542. } break;
  543. case VARIANT_COLOR_ARRAY: {
  544. uint32_t len = f->get_32();
  545. DVector<Color> array;
  546. array.resize(len);
  547. DVector<Color>::Write w = array.write();
  548. if (sizeof(Color) == 16) {
  549. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 4);
  550. #ifdef BIG_ENDIAN_ENABLED
  551. {
  552. uint32_t *ptr = (uint32_t *)w.ptr();
  553. for (int i = 0; i < len * 4; i++) {
  554. ptr[i] = BSWAP32(ptr[i]);
  555. }
  556. }
  557. #endif
  558. } else {
  559. ERR_EXPLAIN("Color size is NOT 16!");
  560. ERR_FAIL_V(ERR_UNAVAILABLE);
  561. }
  562. w = DVector<Color>::Write();
  563. r_v = array;
  564. } break;
  565. default: {
  566. ERR_FAIL_V(ERR_FILE_CORRUPT);
  567. } break;
  568. }
  569. return OK; //never reach anyway
  570. }
  571. void ResourceInteractiveLoaderBinary::set_local_path(const String &p_local_path) {
  572. res_path = p_local_path;
  573. }
  574. Ref<Resource> ResourceInteractiveLoaderBinary::get_resource() {
  575. return resource;
  576. }
  577. Error ResourceInteractiveLoaderBinary::poll() {
  578. if (error != OK)
  579. return error;
  580. int s = stage;
  581. if (s < external_resources.size()) {
  582. String path = external_resources[s].path;
  583. if (remaps.has(path)) {
  584. path = remaps[path];
  585. }
  586. RES res = ResourceLoader::load(path, external_resources[s].type);
  587. if (res.is_null()) {
  588. if (!ResourceLoader::get_abort_on_missing_resources()) {
  589. ResourceLoader::notify_dependency_error(local_path, path, external_resources[s].type);
  590. } else {
  591. error = ERR_FILE_MISSING_DEPENDENCIES;
  592. ERR_EXPLAIN("Can't load dependency: " + path);
  593. ERR_FAIL_V(error);
  594. }
  595. } else {
  596. resource_cache.push_back(res);
  597. }
  598. stage++;
  599. return error;
  600. }
  601. s -= external_resources.size();
  602. if (s >= internal_resources.size()) {
  603. error = ERR_BUG;
  604. ERR_FAIL_COND_V(s >= internal_resources.size(), error);
  605. }
  606. bool main = s == (internal_resources.size() - 1);
  607. //maybe it is loaded already
  608. String path;
  609. int subindex = 0;
  610. if (!main) {
  611. path = internal_resources[s].path;
  612. if (path.begins_with("local://")) {
  613. path = path.replace_first("local://", "");
  614. subindex = path.to_int();
  615. path = res_path + "::" + path;
  616. }
  617. if (ResourceCache::has(path)) {
  618. //already loaded, don't do anything
  619. stage++;
  620. error = OK;
  621. return error;
  622. }
  623. } else {
  624. if (!ResourceCache::has(res_path))
  625. path = res_path;
  626. }
  627. uint64_t offset = internal_resources[s].offset;
  628. f->seek(offset);
  629. String t = get_unicode_string();
  630. Object *obj = ObjectTypeDB::instance(t);
  631. if (!obj) {
  632. error = ERR_FILE_CORRUPT;
  633. ERR_EXPLAIN(local_path + ":Resource of unrecognized type in file: " + t);
  634. }
  635. ERR_FAIL_COND_V(!obj, ERR_FILE_CORRUPT);
  636. Resource *r = obj->cast_to<Resource>();
  637. if (!r) {
  638. error = ERR_FILE_CORRUPT;
  639. memdelete(obj); //bye
  640. ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_type());
  641. ERR_FAIL_COND_V(!r, ERR_FILE_CORRUPT);
  642. }
  643. RES res = RES(r);
  644. r->set_path(path);
  645. r->set_subindex(subindex);
  646. int pc = f->get_32();
  647. //set properties
  648. for (int i = 0; i < pc; i++) {
  649. uint32_t name_idx = f->get_32();
  650. if (name_idx >= (uint32_t)string_map.size()) {
  651. error = ERR_FILE_CORRUPT;
  652. ERR_FAIL_V(ERR_FILE_CORRUPT);
  653. }
  654. Variant value;
  655. error = parse_variant(value);
  656. if (error)
  657. return error;
  658. res->set(string_map[name_idx], value);
  659. }
  660. #ifdef TOOLS_ENABLED
  661. res->set_edited(false);
  662. #endif
  663. stage++;
  664. resource_cache.push_back(res);
  665. if (main) {
  666. if (importmd_ofs) {
  667. f->seek(importmd_ofs);
  668. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  669. imd->set_editor(get_unicode_string());
  670. int sc = f->get_32();
  671. for (int i = 0; i < sc; i++) {
  672. String src = get_unicode_string();
  673. String md5 = get_unicode_string();
  674. imd->add_source(src, md5);
  675. }
  676. int pc = f->get_32();
  677. for (int i = 0; i < pc; i++) {
  678. String name = get_unicode_string();
  679. Variant val;
  680. parse_variant(val);
  681. imd->set_option(name, val);
  682. }
  683. res->set_import_metadata(imd);
  684. }
  685. f->close();
  686. resource = res;
  687. error = ERR_FILE_EOF;
  688. } else {
  689. error = OK;
  690. }
  691. return OK;
  692. }
  693. int ResourceInteractiveLoaderBinary::get_stage() const {
  694. return stage;
  695. }
  696. int ResourceInteractiveLoaderBinary::get_stage_count() const {
  697. return external_resources.size() + internal_resources.size();
  698. }
  699. static void save_ustring(FileAccess *f, const String &p_string) {
  700. CharString utf8 = p_string.utf8();
  701. f->store_32(utf8.length() + 1);
  702. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  703. }
  704. static String get_ustring(FileAccess *f) {
  705. int len = f->get_32();
  706. Vector<char> str_buf;
  707. str_buf.resize(len);
  708. f->get_buffer((uint8_t *)&str_buf[0], len);
  709. String s;
  710. s.parse_utf8(&str_buf[0]);
  711. return s;
  712. }
  713. String ResourceInteractiveLoaderBinary::get_unicode_string() {
  714. int len = f->get_32();
  715. if (len > str_buf.size()) {
  716. str_buf.resize(len);
  717. }
  718. if (len == 0)
  719. return String();
  720. f->get_buffer((uint8_t *)&str_buf[0], len);
  721. String s;
  722. s.parse_utf8(&str_buf[0]);
  723. return s;
  724. }
  725. Error ResourceInteractiveLoaderBinary::get_export_data(ExportData &r_export_data) {
  726. for (int i = 0; i < external_resources.size(); i++) {
  727. ExportData::Dependency dep;
  728. dep.path = external_resources[i].path;
  729. dep.type = external_resources[i].type;
  730. r_export_data.dependencies[i] = dep;
  731. }
  732. for (int i = 0; i < internal_resources.size(); i++) {
  733. bool main = i == (internal_resources.size() - 1);
  734. //maybe it is loaded already
  735. r_export_data.resources.resize(r_export_data.resources.size() + 1);
  736. ExportData::ResourceData &res_data = r_export_data.resources[r_export_data.resources.size() - 1];
  737. res_data.index = -1;
  738. if (!main) {
  739. String path = internal_resources[i].path;
  740. if (path.begins_with("local://")) {
  741. path = path.replace_first("local://", "");
  742. res_data.index = path.to_int();
  743. }
  744. } else {
  745. }
  746. uint64_t offset = internal_resources[i].offset;
  747. f->seek(offset);
  748. String t = get_unicode_string();
  749. res_data.type = t;
  750. int pc = f->get_32();
  751. //set properties
  752. for (int i = 0; i < pc; i++) {
  753. uint32_t name_idx = f->get_32();
  754. if (name_idx >= (uint32_t)string_map.size()) {
  755. error = ERR_FILE_CORRUPT;
  756. ERR_FAIL_V(ERR_FILE_CORRUPT);
  757. }
  758. Variant value;
  759. error = parse_variant(value, true);
  760. if (error)
  761. return error;
  762. ExportData::PropertyData pdata;
  763. pdata.name = string_map[name_idx];
  764. pdata.value = value;
  765. res_data.properties.push_back(pdata);
  766. }
  767. }
  768. return OK;
  769. }
  770. void ResourceInteractiveLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) {
  771. open(p_f);
  772. if (error)
  773. return;
  774. for (int i = 0; i < external_resources.size(); i++) {
  775. String dep = external_resources[i].path;
  776. if (dep.ends_with("*")) {
  777. dep = ResourceLoader::guess_full_filename(dep, external_resources[i].type);
  778. }
  779. if (p_add_types && external_resources[i].type != String()) {
  780. dep += "::" + external_resources[i].type;
  781. }
  782. p_dependencies->push_back(dep);
  783. }
  784. }
  785. void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) {
  786. error = OK;
  787. f = p_f;
  788. uint8_t header[4];
  789. f->get_buffer(header, 4);
  790. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  791. //compressed
  792. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  793. fac->open_after_magic(f);
  794. f = fac;
  795. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  796. //not normal
  797. error = ERR_FILE_UNRECOGNIZED;
  798. ERR_EXPLAIN("Unrecognized binary resource file: " + local_path);
  799. ERR_FAIL();
  800. }
  801. bool big_endian = f->get_32();
  802. #ifdef BIG_ENDIAN_ENABLED
  803. endian_swap = !big_endian;
  804. #else
  805. bool endian_swap = big_endian;
  806. #endif
  807. bool use_real64 = f->get_32();
  808. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  809. uint32_t ver_major = f->get_32();
  810. uint32_t ver_minor = f->get_32();
  811. uint32_t ver_format = f->get_32();
  812. print_bl("big endian: " + itos(big_endian));
  813. print_bl("endian swap: " + itos(endian_swap));
  814. print_bl("real64: " + itos(use_real64));
  815. print_bl("major: " + itos(ver_major));
  816. print_bl("minor: " + itos(ver_minor));
  817. print_bl("format: " + itos(ver_format));
  818. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  819. f->close();
  820. 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);
  821. ERR_FAIL();
  822. }
  823. type = get_unicode_string();
  824. print_bl("type: " + type);
  825. importmd_ofs = f->get_64();
  826. for (int i = 0; i < 14; i++)
  827. f->get_32(); //skip a few reserved fields
  828. uint32_t string_table_size = f->get_32();
  829. string_map.resize(string_table_size);
  830. for (uint32_t i = 0; i < string_table_size; i++) {
  831. StringName s = get_unicode_string();
  832. string_map[i] = s;
  833. }
  834. print_bl("strings: " + itos(string_table_size));
  835. uint32_t ext_resources_size = f->get_32();
  836. for (uint32_t i = 0; i < ext_resources_size; i++) {
  837. ExtResource er;
  838. er.type = get_unicode_string();
  839. er.path = get_unicode_string();
  840. external_resources.push_back(er);
  841. }
  842. //see if the exporter has different set of external resources for more efficient loading
  843. /*
  844. String preload_depts = "deps/"+res_path.md5_text();
  845. if (Globals::get_singleton()->has(preload_depts)) {
  846. external_resources.clear();
  847. //ignore external resources and use these
  848. NodePath depts=Globals::get_singleton()->get(preload_depts);
  849. external_resources.resize(depts.get_name_count());
  850. for(int i=0;i<depts.get_name_count();i++) {
  851. external_resources[i].path=depts.get_name(i);
  852. }
  853. print_line(res_path+" - EXTERNAL RESOURCES: "+itos(external_resources.size()));
  854. }*/
  855. print_bl("ext resources: " + itos(ext_resources_size));
  856. uint32_t int_resources_size = f->get_32();
  857. for (uint32_t i = 0; i < int_resources_size; i++) {
  858. IntResource ir;
  859. ir.path = get_unicode_string();
  860. ir.offset = f->get_64();
  861. internal_resources.push_back(ir);
  862. }
  863. print_bl("int resources: " + itos(int_resources_size));
  864. if (f->eof_reached()) {
  865. error = ERR_FILE_CORRUPT;
  866. ERR_EXPLAIN("Premature End Of File: " + local_path);
  867. ERR_FAIL();
  868. }
  869. }
  870. String ResourceInteractiveLoaderBinary::recognize(FileAccess *p_f) {
  871. error = OK;
  872. f = p_f;
  873. uint8_t header[4];
  874. f->get_buffer(header, 4);
  875. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  876. //compressed
  877. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  878. fac->open_after_magic(f);
  879. f = fac;
  880. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  881. //not normal
  882. error = ERR_FILE_UNRECOGNIZED;
  883. return "";
  884. }
  885. bool big_endian = f->get_32();
  886. #ifdef BIG_ENDIAN_ENABLED
  887. endian_swap = !big_endian;
  888. #else
  889. bool endian_swap = big_endian;
  890. #endif
  891. bool use_real64 = f->get_32();
  892. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  893. uint32_t ver_major = f->get_32();
  894. uint32_t ver_minor = f->get_32();
  895. uint32_t ver_format = f->get_32();
  896. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  897. f->close();
  898. return "";
  899. }
  900. String type = get_unicode_string();
  901. return type;
  902. }
  903. ResourceInteractiveLoaderBinary::ResourceInteractiveLoaderBinary() {
  904. f = NULL;
  905. stage = 0;
  906. endian_swap = false;
  907. use_real64 = false;
  908. error = OK;
  909. }
  910. ResourceInteractiveLoaderBinary::~ResourceInteractiveLoaderBinary() {
  911. if (f)
  912. memdelete(f);
  913. }
  914. Ref<ResourceInteractiveLoader> ResourceFormatLoaderBinary::load_interactive(const String &p_path, Error *r_error) {
  915. if (r_error)
  916. *r_error = ERR_FILE_CANT_OPEN;
  917. Error err;
  918. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  919. if (err != OK) {
  920. ERR_FAIL_COND_V(err != OK, Ref<ResourceInteractiveLoader>());
  921. }
  922. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  923. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  924. ria->res_path = ria->local_path;
  925. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  926. ria->open(f);
  927. return ria;
  928. }
  929. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  930. if (p_type == "") {
  931. get_recognized_extensions(p_extensions);
  932. return;
  933. }
  934. List<String> extensions;
  935. ObjectTypeDB::get_extensions_for_type(p_type, &extensions);
  936. extensions.sort();
  937. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  938. String ext = E->get().to_lower();
  939. p_extensions->push_back(ext);
  940. }
  941. }
  942. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  943. List<String> extensions;
  944. ObjectTypeDB::get_resource_base_extensions(&extensions);
  945. extensions.sort();
  946. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  947. String ext = E->get().to_lower();
  948. p_extensions->push_back(ext);
  949. }
  950. }
  951. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  952. return true; //handles all
  953. }
  954. Error ResourceFormatLoaderBinary::load_import_metadata(const String &p_path, Ref<ResourceImportMetadata> &r_var) const {
  955. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  956. if (!f) {
  957. return ERR_FILE_CANT_OPEN;
  958. }
  959. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  960. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  961. ria->res_path = ria->local_path;
  962. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  963. ria->recognize(f);
  964. if (ria->error != OK)
  965. return ERR_FILE_UNRECOGNIZED;
  966. f = ria->f;
  967. uint64_t imp_ofs = f->get_64();
  968. if (imp_ofs == 0)
  969. return ERR_UNAVAILABLE;
  970. f->seek(imp_ofs);
  971. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  972. imd->set_editor(ria->get_unicode_string());
  973. int sc = f->get_32();
  974. for (int i = 0; i < sc; i++) {
  975. String src = ria->get_unicode_string();
  976. String md5 = ria->get_unicode_string();
  977. imd->add_source(src, md5);
  978. }
  979. int pc = f->get_32();
  980. for (int i = 0; i < pc; i++) {
  981. String name = ria->get_unicode_string();
  982. Variant val;
  983. ria->parse_variant(val);
  984. imd->set_option(name, val);
  985. }
  986. r_var = imd;
  987. return OK;
  988. }
  989. ResourceFormatLoaderBinary *ResourceFormatLoaderBinary::singleton = NULL;
  990. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  991. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  992. ERR_FAIL_COND(!f);
  993. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  994. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  995. ria->res_path = ria->local_path;
  996. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  997. ria->get_dependencies(f, p_dependencies, p_add_types);
  998. }
  999. Error ResourceFormatLoaderBinary::get_export_data(const String &p_path, ExportData &r_export_data) {
  1000. Error err;
  1001. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  1002. if (err != OK) {
  1003. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  1004. }
  1005. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1006. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1007. ria->res_path = ria->local_path;
  1008. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1009. ria->open(f);
  1010. return ria->get_export_data(r_export_data);
  1011. }
  1012. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  1013. // Error error=OK;
  1014. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1015. ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
  1016. FileAccess *fw = NULL; //=FileAccess::open(p_path+".depren");
  1017. String local_path = p_path.get_base_dir();
  1018. uint8_t header[4];
  1019. f->get_buffer(header, 4);
  1020. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  1021. //compressed
  1022. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1023. fac->open_after_magic(f);
  1024. f = fac;
  1025. FileAccessCompressed *facw = memnew(FileAccessCompressed);
  1026. facw->configure("RSCC");
  1027. Error err = facw->_open(p_path + ".depren", FileAccess::WRITE);
  1028. if (err) {
  1029. memdelete(fac);
  1030. memdelete(facw);
  1031. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  1032. }
  1033. fw = facw;
  1034. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1035. //not normal
  1036. //error=ERR_FILE_UNRECOGNIZED;
  1037. memdelete(f);
  1038. ERR_EXPLAIN("Unrecognized binary resource file: " + local_path);
  1039. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  1040. } else {
  1041. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  1042. if (!fw) {
  1043. memdelete(f);
  1044. }
  1045. ERR_FAIL_COND_V(!fw, ERR_CANT_CREATE);
  1046. }
  1047. bool big_endian = f->get_32();
  1048. #ifdef BIG_ENDIAN_ENABLED
  1049. endian_swap = !big_endian;
  1050. #else
  1051. bool endian_swap = big_endian;
  1052. #endif
  1053. bool use_real64 = f->get_32();
  1054. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  1055. fw->store_32(endian_swap);
  1056. fw->set_endian_swap(big_endian != 0);
  1057. fw->store_32(use_real64); //use real64
  1058. uint32_t ver_major = f->get_32();
  1059. uint32_t ver_minor = f->get_32();
  1060. uint32_t ver_format = f->get_32();
  1061. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  1062. memdelete(f);
  1063. memdelete(fw);
  1064. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1065. da->remove(p_path + ".depren");
  1066. memdelete(da);
  1067. //fuck it, use the old approach;
  1068. WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: " + p_path).utf8().get_data());
  1069. Error err;
  1070. f = FileAccess::open(p_path, FileAccess::READ, &err);
  1071. if (err != OK) {
  1072. ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN);
  1073. }
  1074. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1075. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1076. ria->res_path = ria->local_path;
  1077. ria->remaps = p_map;
  1078. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1079. ria->open(f);
  1080. err = ria->poll();
  1081. while (err == OK) {
  1082. err = ria->poll();
  1083. }
  1084. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  1085. RES res = ria->get_resource();
  1086. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  1087. return ResourceFormatSaverBinary::singleton->save(p_path, res);
  1088. }
  1089. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1090. memdelete(f);
  1091. memdelete(fw);
  1092. 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);
  1093. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  1094. }
  1095. fw->store_32(VERSION_MAJOR); //current version
  1096. fw->store_32(VERSION_MINOR);
  1097. fw->store_32(FORMAT_VERSION);
  1098. save_ustring(fw, get_ustring(f)); //type
  1099. size_t md_ofs = f->get_pos();
  1100. size_t importmd_ofs = f->get_64();
  1101. fw->store_64(0); //metadata offset
  1102. for (int i = 0; i < 14; i++) {
  1103. fw->store_32(0);
  1104. f->get_32();
  1105. }
  1106. //string table
  1107. uint32_t string_table_size = f->get_32();
  1108. fw->store_32(string_table_size);
  1109. for (uint32_t i = 0; i < string_table_size; i++) {
  1110. String s = get_ustring(f);
  1111. save_ustring(fw, s);
  1112. }
  1113. //external resources
  1114. uint32_t ext_resources_size = f->get_32();
  1115. fw->store_32(ext_resources_size);
  1116. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1117. String type = get_ustring(f);
  1118. String path = get_ustring(f);
  1119. bool relative = false;
  1120. if (!path.begins_with("res://")) {
  1121. path = local_path.plus_file(path).simplify_path();
  1122. relative = true;
  1123. }
  1124. if (p_map.has(path)) {
  1125. String np = p_map[path];
  1126. path = np;
  1127. }
  1128. if (relative) {
  1129. //restore relative
  1130. path = local_path.path_to_file(path);
  1131. }
  1132. save_ustring(fw, type);
  1133. save_ustring(fw, path);
  1134. }
  1135. int64_t size_diff = (int64_t)fw->get_pos() - (int64_t)f->get_pos();
  1136. //internal resources
  1137. uint32_t int_resources_size = f->get_32();
  1138. fw->store_32(int_resources_size);
  1139. for (uint32_t i = 0; i < int_resources_size; i++) {
  1140. String path = get_ustring(f);
  1141. uint64_t offset = f->get_64();
  1142. save_ustring(fw, path);
  1143. fw->store_64(offset + size_diff);
  1144. }
  1145. //rest of file
  1146. uint8_t b = f->get_8();
  1147. while (!f->eof_reached()) {
  1148. fw->store_8(b);
  1149. b = f->get_8();
  1150. }
  1151. bool all_ok = fw->get_error() == OK;
  1152. fw->seek(md_ofs);
  1153. fw->store_64(importmd_ofs + size_diff);
  1154. memdelete(f);
  1155. memdelete(fw);
  1156. if (!all_ok) {
  1157. return ERR_CANT_CREATE;
  1158. }
  1159. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1160. da->remove(p_path);
  1161. da->rename(p_path + ".depren", p_path);
  1162. memdelete(da);
  1163. return OK;
  1164. }
  1165. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1166. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1167. if (!f) {
  1168. return ""; //could not rwead
  1169. }
  1170. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1171. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1172. ria->res_path = ria->local_path;
  1173. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1174. String r = ria->recognize(f);
  1175. return r;
  1176. }
  1177. ///////////////////////////////////////////////////////////
  1178. ///////////////////////////////////////////////////////////
  1179. ///////////////////////////////////////////////////////////
  1180. void ResourceFormatSaverBinaryInstance::_pad_buffer(int p_bytes) {
  1181. int extra = 4 - (p_bytes % 4);
  1182. if (extra < 4) {
  1183. for (int i = 0; i < extra; i++)
  1184. f->store_8(0); //pad to 32
  1185. }
  1186. }
  1187. void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, const PropertyInfo &p_hint) {
  1188. switch (p_property.get_type()) {
  1189. case Variant::NIL: {
  1190. f->store_32(VARIANT_NIL);
  1191. // don't store anything
  1192. } break;
  1193. case Variant::BOOL: {
  1194. f->store_32(VARIANT_BOOL);
  1195. bool val = p_property;
  1196. f->store_32(val);
  1197. } break;
  1198. case Variant::INT: {
  1199. f->store_32(VARIANT_INT);
  1200. int val = p_property;
  1201. f->store_32(val);
  1202. } break;
  1203. case Variant::REAL: {
  1204. f->store_32(VARIANT_REAL);
  1205. real_t val = p_property;
  1206. f->store_real(val);
  1207. } break;
  1208. case Variant::STRING: {
  1209. f->store_32(VARIANT_STRING);
  1210. String val = p_property;
  1211. save_unicode_string(val);
  1212. } break;
  1213. case Variant::VECTOR2: {
  1214. f->store_32(VARIANT_VECTOR2);
  1215. Vector2 val = p_property;
  1216. f->store_real(val.x);
  1217. f->store_real(val.y);
  1218. } break;
  1219. case Variant::RECT2: {
  1220. f->store_32(VARIANT_RECT2);
  1221. Rect2 val = p_property;
  1222. f->store_real(val.pos.x);
  1223. f->store_real(val.pos.y);
  1224. f->store_real(val.size.x);
  1225. f->store_real(val.size.y);
  1226. } break;
  1227. case Variant::VECTOR3: {
  1228. f->store_32(VARIANT_VECTOR3);
  1229. Vector3 val = p_property;
  1230. f->store_real(val.x);
  1231. f->store_real(val.y);
  1232. f->store_real(val.z);
  1233. } break;
  1234. case Variant::PLANE: {
  1235. f->store_32(VARIANT_PLANE);
  1236. Plane val = p_property;
  1237. f->store_real(val.normal.x);
  1238. f->store_real(val.normal.y);
  1239. f->store_real(val.normal.z);
  1240. f->store_real(val.d);
  1241. } break;
  1242. case Variant::QUAT: {
  1243. f->store_32(VARIANT_QUAT);
  1244. Quat val = p_property;
  1245. f->store_real(val.x);
  1246. f->store_real(val.y);
  1247. f->store_real(val.z);
  1248. f->store_real(val.w);
  1249. } break;
  1250. case Variant::_AABB: {
  1251. f->store_32(VARIANT_AABB);
  1252. AABB val = p_property;
  1253. f->store_real(val.pos.x);
  1254. f->store_real(val.pos.y);
  1255. f->store_real(val.pos.z);
  1256. f->store_real(val.size.x);
  1257. f->store_real(val.size.y);
  1258. f->store_real(val.size.z);
  1259. } break;
  1260. case Variant::MATRIX32: {
  1261. f->store_32(VARIANT_MATRIX32);
  1262. Matrix32 val = p_property;
  1263. f->store_real(val.elements[0].x);
  1264. f->store_real(val.elements[0].y);
  1265. f->store_real(val.elements[1].x);
  1266. f->store_real(val.elements[1].y);
  1267. f->store_real(val.elements[2].x);
  1268. f->store_real(val.elements[2].y);
  1269. } break;
  1270. case Variant::MATRIX3: {
  1271. f->store_32(VARIANT_MATRIX3);
  1272. Matrix3 val = p_property;
  1273. f->store_real(val.elements[0].x);
  1274. f->store_real(val.elements[0].y);
  1275. f->store_real(val.elements[0].z);
  1276. f->store_real(val.elements[1].x);
  1277. f->store_real(val.elements[1].y);
  1278. f->store_real(val.elements[1].z);
  1279. f->store_real(val.elements[2].x);
  1280. f->store_real(val.elements[2].y);
  1281. f->store_real(val.elements[2].z);
  1282. } break;
  1283. case Variant::TRANSFORM: {
  1284. f->store_32(VARIANT_TRANSFORM);
  1285. Transform val = p_property;
  1286. f->store_real(val.basis.elements[0].x);
  1287. f->store_real(val.basis.elements[0].y);
  1288. f->store_real(val.basis.elements[0].z);
  1289. f->store_real(val.basis.elements[1].x);
  1290. f->store_real(val.basis.elements[1].y);
  1291. f->store_real(val.basis.elements[1].z);
  1292. f->store_real(val.basis.elements[2].x);
  1293. f->store_real(val.basis.elements[2].y);
  1294. f->store_real(val.basis.elements[2].z);
  1295. f->store_real(val.origin.x);
  1296. f->store_real(val.origin.y);
  1297. f->store_real(val.origin.z);
  1298. } break;
  1299. case Variant::COLOR: {
  1300. f->store_32(VARIANT_COLOR);
  1301. Color val = p_property;
  1302. f->store_real(val.r);
  1303. f->store_real(val.g);
  1304. f->store_real(val.b);
  1305. f->store_real(val.a);
  1306. } break;
  1307. case Variant::IMAGE: {
  1308. f->store_32(VARIANT_IMAGE);
  1309. Image val = p_property;
  1310. if (val.empty()) {
  1311. f->store_32(IMAGE_ENCODING_EMPTY);
  1312. break;
  1313. }
  1314. int encoding = IMAGE_ENCODING_RAW;
  1315. float quality = 0.7;
  1316. if (val.get_format() <= Image::FORMAT_INDEXED_ALPHA) {
  1317. //can only compress uncompressed stuff
  1318. if (p_hint.hint == PROPERTY_HINT_IMAGE_COMPRESS_LOSSY && Image::lossy_packer) {
  1319. encoding = IMAGE_ENCODING_LOSSY;
  1320. float qs = p_hint.hint_string.to_double();
  1321. if (qs != 0.0)
  1322. quality = qs;
  1323. } else if (p_hint.hint == PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS && Image::lossless_packer) {
  1324. encoding = IMAGE_ENCODING_LOSSLESS;
  1325. }
  1326. }
  1327. f->store_32(encoding); //raw encoding
  1328. if (encoding == IMAGE_ENCODING_RAW) {
  1329. f->store_32(val.get_width());
  1330. f->store_32(val.get_height());
  1331. f->store_32(val.get_mipmaps());
  1332. switch (val.get_format()) {
  1333. case Image::FORMAT_GRAYSCALE:
  1334. f->store_32(IMAGE_FORMAT_GRAYSCALE);
  1335. break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  1336. case Image::FORMAT_INTENSITY:
  1337. f->store_32(IMAGE_FORMAT_INTENSITY);
  1338. break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  1339. case Image::FORMAT_GRAYSCALE_ALPHA:
  1340. f->store_32(IMAGE_FORMAT_GRAYSCALE_ALPHA);
  1341. break; ///< two bytes per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255. alpha 0-255
  1342. case Image::FORMAT_RGB:
  1343. f->store_32(IMAGE_FORMAT_RGB);
  1344. break; ///< one byte R: f->store_32(IMAGE_FORMAT_ ); break; one byte G: f->store_32(IMAGE_FORMAT_ ); break; one byte B
  1345. case Image::FORMAT_RGBA:
  1346. f->store_32(IMAGE_FORMAT_RGBA);
  1347. 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
  1348. case Image::FORMAT_INDEXED:
  1349. f->store_32(IMAGE_FORMAT_INDEXED);
  1350. 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
  1351. case Image::FORMAT_INDEXED_ALPHA:
  1352. f->store_32(IMAGE_FORMAT_INDEXED_ALPHA);
  1353. 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)
  1354. case Image::FORMAT_BC1:
  1355. f->store_32(IMAGE_FORMAT_BC1);
  1356. break; // DXT1
  1357. case Image::FORMAT_BC2:
  1358. f->store_32(IMAGE_FORMAT_BC2);
  1359. break; // DXT3
  1360. case Image::FORMAT_BC3:
  1361. f->store_32(IMAGE_FORMAT_BC3);
  1362. break; // DXT5
  1363. case Image::FORMAT_BC4:
  1364. f->store_32(IMAGE_FORMAT_BC4);
  1365. break; // ATI1
  1366. case Image::FORMAT_BC5:
  1367. f->store_32(IMAGE_FORMAT_BC5);
  1368. break; // ATI2
  1369. case Image::FORMAT_PVRTC2: f->store_32(IMAGE_FORMAT_PVRTC2); break;
  1370. case Image::FORMAT_PVRTC2_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC2_ALPHA); break;
  1371. case Image::FORMAT_PVRTC4: f->store_32(IMAGE_FORMAT_PVRTC4); break;
  1372. case Image::FORMAT_PVRTC4_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC4_ALPHA); break;
  1373. case Image::FORMAT_ETC: f->store_32(IMAGE_FORMAT_ETC); break;
  1374. case Image::FORMAT_ATC: f->store_32(IMAGE_FORMAT_ATC); break;
  1375. case Image::FORMAT_ATC_ALPHA_EXPLICIT: f->store_32(IMAGE_FORMAT_ATC_ALPHA_EXPLICIT); break;
  1376. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: f->store_32(IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED); break;
  1377. case Image::FORMAT_CUSTOM: f->store_32(IMAGE_FORMAT_CUSTOM); break;
  1378. default: {}
  1379. }
  1380. int dlen = val.get_data().size();
  1381. f->store_32(dlen);
  1382. DVector<uint8_t>::Read r = val.get_data().read();
  1383. f->store_buffer(r.ptr(), dlen);
  1384. _pad_buffer(dlen);
  1385. } else {
  1386. DVector<uint8_t> data;
  1387. if (encoding == IMAGE_ENCODING_LOSSY) {
  1388. data = Image::lossy_packer(val, quality);
  1389. } else if (encoding == IMAGE_ENCODING_LOSSLESS) {
  1390. data = Image::lossless_packer(val);
  1391. }
  1392. int ds = data.size();
  1393. f->store_32(ds);
  1394. if (ds > 0) {
  1395. DVector<uint8_t>::Read r = data.read();
  1396. f->store_buffer(r.ptr(), ds);
  1397. _pad_buffer(ds);
  1398. }
  1399. }
  1400. } break;
  1401. case Variant::NODE_PATH: {
  1402. f->store_32(VARIANT_NODE_PATH);
  1403. NodePath np = p_property;
  1404. f->store_16(np.get_name_count());
  1405. uint16_t snc = np.get_subname_count();
  1406. if (np.is_absolute())
  1407. snc |= 0x8000;
  1408. f->store_16(snc);
  1409. for (int i = 0; i < np.get_name_count(); i++)
  1410. f->store_32(get_string_index(np.get_name(i)));
  1411. for (int i = 0; i < np.get_subname_count(); i++)
  1412. f->store_32(get_string_index(np.get_subname(i)));
  1413. f->store_32(get_string_index(np.get_property()));
  1414. } break;
  1415. case Variant::_RID: {
  1416. f->store_32(VARIANT_RID);
  1417. WARN_PRINT("Can't save RIDs");
  1418. RID val = p_property;
  1419. f->store_32(val.get_id());
  1420. } break;
  1421. case Variant::OBJECT: {
  1422. f->store_32(VARIANT_OBJECT);
  1423. RES res = p_property;
  1424. if (res.is_null()) {
  1425. f->store_32(OBJECT_EMPTY);
  1426. return; // don't save it
  1427. }
  1428. if (res->get_path().length() && res->get_path().find("::") == -1) {
  1429. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1430. f->store_32(external_resources[res]);
  1431. } else {
  1432. if (!resource_set.has(res)) {
  1433. f->store_32(OBJECT_EMPTY);
  1434. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1435. ERR_FAIL();
  1436. }
  1437. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1438. f->store_32(res->get_subindex());
  1439. //internal resource
  1440. }
  1441. } break;
  1442. case Variant::INPUT_EVENT: {
  1443. f->store_32(VARIANT_INPUT_EVENT);
  1444. WARN_PRINT("Can't save InputEvent (maybe it could..)");
  1445. } break;
  1446. case Variant::DICTIONARY: {
  1447. f->store_32(VARIANT_DICTIONARY);
  1448. Dictionary d = p_property;
  1449. f->store_32(uint32_t(d.size()) | (d.is_shared() ? 0x80000000 : 0));
  1450. List<Variant> keys;
  1451. d.get_key_list(&keys);
  1452. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1453. //if (!_check_type(dict[E->get()]))
  1454. // continue;
  1455. write_variant(E->get());
  1456. write_variant(d[E->get()]);
  1457. }
  1458. } break;
  1459. case Variant::ARRAY: {
  1460. f->store_32(VARIANT_ARRAY);
  1461. Array a = p_property;
  1462. f->store_32(uint32_t(a.size()) | (a.is_shared() ? 0x80000000 : 0));
  1463. for (int i = 0; i < a.size(); i++) {
  1464. write_variant(a[i]);
  1465. }
  1466. } break;
  1467. case Variant::RAW_ARRAY: {
  1468. f->store_32(VARIANT_RAW_ARRAY);
  1469. DVector<uint8_t> arr = p_property;
  1470. int len = arr.size();
  1471. f->store_32(len);
  1472. DVector<uint8_t>::Read r = arr.read();
  1473. f->store_buffer(r.ptr(), len);
  1474. _pad_buffer(len);
  1475. } break;
  1476. case Variant::INT_ARRAY: {
  1477. f->store_32(VARIANT_INT_ARRAY);
  1478. DVector<int> arr = p_property;
  1479. int len = arr.size();
  1480. f->store_32(len);
  1481. DVector<int>::Read r = arr.read();
  1482. for (int i = 0; i < len; i++)
  1483. f->store_32(r[i]);
  1484. } break;
  1485. case Variant::REAL_ARRAY: {
  1486. f->store_32(VARIANT_REAL_ARRAY);
  1487. DVector<real_t> arr = p_property;
  1488. int len = arr.size();
  1489. f->store_32(len);
  1490. DVector<real_t>::Read r = arr.read();
  1491. for (int i = 0; i < len; i++) {
  1492. f->store_real(r[i]);
  1493. }
  1494. } break;
  1495. case Variant::STRING_ARRAY: {
  1496. f->store_32(VARIANT_STRING_ARRAY);
  1497. DVector<String> arr = p_property;
  1498. int len = arr.size();
  1499. f->store_32(len);
  1500. DVector<String>::Read r = arr.read();
  1501. for (int i = 0; i < len; i++) {
  1502. save_unicode_string(r[i]);
  1503. }
  1504. } break;
  1505. case Variant::VECTOR3_ARRAY: {
  1506. f->store_32(VARIANT_VECTOR3_ARRAY);
  1507. DVector<Vector3> arr = p_property;
  1508. int len = arr.size();
  1509. f->store_32(len);
  1510. DVector<Vector3>::Read r = arr.read();
  1511. for (int i = 0; i < len; i++) {
  1512. f->store_real(r[i].x);
  1513. f->store_real(r[i].y);
  1514. f->store_real(r[i].z);
  1515. }
  1516. } break;
  1517. case Variant::VECTOR2_ARRAY: {
  1518. f->store_32(VARIANT_VECTOR2_ARRAY);
  1519. DVector<Vector2> arr = p_property;
  1520. int len = arr.size();
  1521. f->store_32(len);
  1522. DVector<Vector2>::Read r = arr.read();
  1523. for (int i = 0; i < len; i++) {
  1524. f->store_real(r[i].x);
  1525. f->store_real(r[i].y);
  1526. }
  1527. } break;
  1528. case Variant::COLOR_ARRAY: {
  1529. f->store_32(VARIANT_COLOR_ARRAY);
  1530. DVector<Color> arr = p_property;
  1531. int len = arr.size();
  1532. f->store_32(len);
  1533. DVector<Color>::Read r = arr.read();
  1534. for (int i = 0; i < len; i++) {
  1535. f->store_real(r[i].r);
  1536. f->store_real(r[i].g);
  1537. f->store_real(r[i].b);
  1538. f->store_real(r[i].a);
  1539. }
  1540. } break;
  1541. default: {
  1542. ERR_EXPLAIN("Invalid variant");
  1543. ERR_FAIL();
  1544. }
  1545. }
  1546. }
  1547. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1548. switch (p_variant.get_type()) {
  1549. case Variant::OBJECT: {
  1550. RES res = p_variant.operator RefPtr();
  1551. if (res.is_null() || external_resources.has(res))
  1552. return;
  1553. if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
  1554. int idx = external_resources.size();
  1555. external_resources[res] = idx;
  1556. return;
  1557. }
  1558. if (resource_set.has(res))
  1559. return;
  1560. List<PropertyInfo> property_list;
  1561. res->get_property_list(&property_list);
  1562. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  1563. if (E->get().usage & PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage & PROPERTY_USAGE_BUNDLE)) {
  1564. _find_resources(res->get(E->get().name));
  1565. }
  1566. }
  1567. resource_set.insert(res);
  1568. saved_resources.push_back(res);
  1569. } break;
  1570. case Variant::ARRAY: {
  1571. Array varray = p_variant;
  1572. int len = varray.size();
  1573. for (int i = 0; i < len; i++) {
  1574. Variant v = varray.get(i);
  1575. _find_resources(v);
  1576. }
  1577. } break;
  1578. case Variant::DICTIONARY: {
  1579. Dictionary d = p_variant;
  1580. List<Variant> keys;
  1581. d.get_key_list(&keys);
  1582. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1583. _find_resources(E->get());
  1584. Variant v = d[E->get()];
  1585. _find_resources(v);
  1586. }
  1587. } break;
  1588. case Variant::NODE_PATH: {
  1589. //take the chance and save node path strings
  1590. NodePath np = p_variant;
  1591. for (int i = 0; i < np.get_name_count(); i++)
  1592. get_string_index(np.get_name(i));
  1593. for (int i = 0; i < np.get_subname_count(); i++)
  1594. get_string_index(np.get_subname(i));
  1595. get_string_index(np.get_property());
  1596. } break;
  1597. default: {}
  1598. }
  1599. }
  1600. #if 0
  1601. Error ResourceFormatSaverBinary::_save_obj(const Object *p_object,SavedObject *so) {
  1602. //use classic way
  1603. List<PropertyInfo> property_list;
  1604. p_object->get_property_list( &property_list );
  1605. for(List<PropertyInfo>::Element *E=property_list.front();E;E=E->next()) {
  1606. if (skip_editor && E->get().name.begins_with("__editor"))
  1607. continue;
  1608. if (E->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1609. SavedObject::SavedProperty sp;
  1610. sp.name_idx=get_string_index(E->get().name);
  1611. sp.value = p_object->get(E->get().name);
  1612. _find_resources(sp.value);
  1613. so->properties.push_back(sp);
  1614. }
  1615. }
  1616. return OK;
  1617. }
  1618. Error ResourceFormatSaverBinary::save(const Object *p_object,const Variant &p_meta) {
  1619. ERR_FAIL_COND_V(!f,ERR_UNCONFIGURED);
  1620. ERR_EXPLAIN("write_object should supply either an object, a meta, or both");
  1621. ERR_FAIL_COND_V(!p_object && p_meta.get_type()==Variant::NIL, ERR_INVALID_PARAMETER);
  1622. SavedObject *so = memnew( SavedObject );
  1623. if (p_object)
  1624. so->type=p_object->get_type();
  1625. _find_resources(p_meta);
  1626. so->meta=p_meta;
  1627. Error err = _save_obj(p_object,so);
  1628. ERR_FAIL_COND_V( err, ERR_INVALID_DATA );
  1629. saved_objects.push_back(so);
  1630. return OK;
  1631. }
  1632. #endif
  1633. void ResourceFormatSaverBinaryInstance::save_unicode_string(const String &p_string) {
  1634. CharString utf8 = p_string.utf8();
  1635. f->store_32(utf8.length() + 1);
  1636. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1637. }
  1638. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1639. StringName s = p_string;
  1640. if (string_map.has(s))
  1641. return string_map[s];
  1642. string_map[s] = strings.size();
  1643. strings.push_back(s);
  1644. return strings.size() - 1;
  1645. }
  1646. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1647. Error err;
  1648. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1649. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1650. fac->configure("RSCC");
  1651. f = fac;
  1652. err = fac->_open(p_path, FileAccess::WRITE);
  1653. if (err)
  1654. memdelete(f);
  1655. } else {
  1656. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1657. }
  1658. ERR_FAIL_COND_V(err, err);
  1659. FileAccessRef _fref(f);
  1660. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1661. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1662. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1663. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1664. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1665. if (!p_path.begins_with("res://"))
  1666. takeover_paths = false;
  1667. local_path = p_path.get_base_dir();
  1668. //bin_meta_idx = get_string_index("__bin_meta__"); //is often used, so create
  1669. _find_resources(p_resource, true);
  1670. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1671. //save header compressed
  1672. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1673. f->store_buffer(header, 4);
  1674. }
  1675. if (big_endian) {
  1676. f->store_32(1);
  1677. f->set_endian_swap(true);
  1678. } else
  1679. f->store_32(0);
  1680. f->store_32(0); //64 bits file, false for now
  1681. f->store_32(VERSION_MAJOR);
  1682. f->store_32(VERSION_MINOR);
  1683. f->store_32(FORMAT_VERSION);
  1684. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1685. f->close();
  1686. return ERR_CANT_CREATE;
  1687. }
  1688. //f->store_32(saved_resources.size()+external_resources.size()); // load steps -not needed
  1689. save_unicode_string(p_resource->get_type());
  1690. uint64_t md_at = f->get_pos();
  1691. f->store_64(0); //offset to impoty metadata
  1692. for (int i = 0; i < 14; i++)
  1693. f->store_32(0); // reserved
  1694. List<ResourceData> resources;
  1695. {
  1696. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1697. ResourceData &rd = resources.push_back(ResourceData())->get();
  1698. rd.type = E->get()->get_type();
  1699. List<PropertyInfo> property_list;
  1700. E->get()->get_property_list(&property_list);
  1701. for (List<PropertyInfo>::Element *F = property_list.front(); F; F = F->next()) {
  1702. if (skip_editor && F->get().name.begins_with("__editor"))
  1703. continue;
  1704. if (F->get().usage & PROPERTY_USAGE_STORAGE || (bundle_resources && F->get().usage & PROPERTY_USAGE_BUNDLE)) {
  1705. Property p;
  1706. p.name_idx = get_string_index(F->get().name);
  1707. p.value = E->get()->get(F->get().name);
  1708. if ((F->get().usage & PROPERTY_USAGE_STORE_IF_NONZERO && p.value.is_zero()) || (F->get().usage & PROPERTY_USAGE_STORE_IF_NONONE && p.value.is_one()))
  1709. continue;
  1710. p.pi = F->get();
  1711. rd.properties.push_back(p);
  1712. }
  1713. }
  1714. }
  1715. }
  1716. f->store_32(strings.size()); //string table size
  1717. for (int i = 0; i < strings.size(); i++) {
  1718. //print_bl("saving string: "+strings[i]);
  1719. save_unicode_string(strings[i]);
  1720. }
  1721. // save external resource table
  1722. f->store_32(external_resources.size()); //amount of external resources
  1723. Vector<RES> save_order;
  1724. save_order.resize(external_resources.size());
  1725. for (Map<RES, int>::Element *E = external_resources.front(); E; E = E->next()) {
  1726. save_order[E->get()] = E->key();
  1727. }
  1728. for (int i = 0; i < save_order.size(); i++) {
  1729. save_unicode_string(save_order[i]->get_save_type());
  1730. String path = save_order[i]->get_path();
  1731. path = relative_paths ? local_path.path_to_file(path) : path;
  1732. save_unicode_string(path);
  1733. }
  1734. // save internal resource table
  1735. f->store_32(saved_resources.size()); //amount of internal resources
  1736. Vector<uint64_t> ofs_pos;
  1737. Set<int> used_indices;
  1738. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1739. RES r = E->get();
  1740. if (r->get_path() == "" || r->get_path().find("::") != -1) {
  1741. if (r->get_subindex() != 0) {
  1742. if (used_indices.has(r->get_subindex())) {
  1743. r->set_subindex(0); //repeated
  1744. } else {
  1745. used_indices.insert(r->get_subindex());
  1746. }
  1747. }
  1748. }
  1749. }
  1750. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1751. RES r = E->get();
  1752. if (r->get_path() == "" || r->get_path().find("::") != -1) {
  1753. if (r->get_subindex() == 0) {
  1754. int new_subindex = 1;
  1755. if (used_indices.size()) {
  1756. new_subindex = used_indices.back()->get() + 1;
  1757. }
  1758. r->set_subindex(new_subindex);
  1759. used_indices.insert(new_subindex);
  1760. }
  1761. save_unicode_string("local://" + itos(r->get_subindex()));
  1762. if (takeover_paths) {
  1763. r->set_path(p_path + "::" + itos(r->get_subindex()), true);
  1764. }
  1765. } else {
  1766. save_unicode_string(r->get_path()); //actual external
  1767. }
  1768. ofs_pos.push_back(f->get_pos());
  1769. f->store_64(0); //offset in 64 bits
  1770. }
  1771. Vector<uint64_t> ofs_table;
  1772. // int saved_idx=0;
  1773. //now actually save the resources
  1774. for (List<ResourceData>::Element *E = resources.front(); E; E = E->next()) {
  1775. ResourceData &rd = E->get();
  1776. ofs_table.push_back(f->get_pos());
  1777. save_unicode_string(rd.type);
  1778. f->store_32(rd.properties.size());
  1779. for (List<Property>::Element *F = rd.properties.front(); F; F = F->next()) {
  1780. Property &p = F->get();
  1781. f->store_32(p.name_idx);
  1782. write_variant(p.value, F->get().pi);
  1783. }
  1784. }
  1785. for (int i = 0; i < ofs_table.size(); i++) {
  1786. f->seek(ofs_pos[i]);
  1787. f->store_64(ofs_table[i]);
  1788. }
  1789. f->seek_end();
  1790. print_line("Saving: " + p_path);
  1791. if (p_resource->get_import_metadata().is_valid()) {
  1792. uint64_t md_pos = f->get_pos();
  1793. Ref<ResourceImportMetadata> imd = p_resource->get_import_metadata();
  1794. save_unicode_string(imd->get_editor());
  1795. f->store_32(imd->get_source_count());
  1796. for (int i = 0; i < imd->get_source_count(); i++) {
  1797. save_unicode_string(imd->get_source_path(i));
  1798. save_unicode_string(imd->get_source_md5(i));
  1799. }
  1800. List<String> options;
  1801. imd->get_options(&options);
  1802. f->store_32(options.size());
  1803. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1804. save_unicode_string(E->get());
  1805. write_variant(imd->get_option(E->get()));
  1806. }
  1807. f->seek(md_at);
  1808. f->store_64(md_pos);
  1809. f->seek_end();
  1810. }
  1811. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1812. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1813. f->close();
  1814. return ERR_CANT_CREATE;
  1815. }
  1816. f->close();
  1817. return OK;
  1818. }
  1819. Error ResourceFormatSaverBinary::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1820. String local_path = Globals::get_singleton()->localize_path(p_path);
  1821. ResourceFormatSaverBinaryInstance saver;
  1822. return saver.save(local_path, p_resource, p_flags);
  1823. }
  1824. bool ResourceFormatSaverBinary::recognize(const RES &p_resource) const {
  1825. return true; //all recognized
  1826. }
  1827. void ResourceFormatSaverBinary::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1828. String base = p_resource->get_base_extension().to_lower();
  1829. p_extensions->push_back(base);
  1830. }
  1831. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = NULL;
  1832. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  1833. singleton = this;
  1834. }