resource_format_binary.cpp 48 KB

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