resource_format_binary.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access_compressed.h"
  34. #include "core/io/image.h"
  35. #include "core/io/marshalls.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_FLOAT = 4,
  45. VARIANT_STRING = 5,
  46. VARIANT_VECTOR2 = 10,
  47. VARIANT_RECT2 = 11,
  48. VARIANT_VECTOR3 = 12,
  49. VARIANT_PLANE = 13,
  50. VARIANT_QUATERNION = 14,
  51. VARIANT_AABB = 15,
  52. VARIANT_BASIS = 16,
  53. VARIANT_TRANSFORM3D = 17,
  54. VARIANT_TRANSFORM2D = 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_PACKED_BYTE_ARRAY = 31,
  63. VARIANT_PACKED_INT32_ARRAY = 32,
  64. VARIANT_PACKED_FLOAT32_ARRAY = 33,
  65. VARIANT_PACKED_STRING_ARRAY = 34,
  66. VARIANT_PACKED_VECTOR3_ARRAY = 35,
  67. VARIANT_PACKED_COLOR_ARRAY = 36,
  68. VARIANT_PACKED_VECTOR2_ARRAY = 37,
  69. VARIANT_INT64 = 40,
  70. VARIANT_DOUBLE = 41,
  71. VARIANT_CALLABLE = 42,
  72. VARIANT_SIGNAL = 43,
  73. VARIANT_STRING_NAME = 44,
  74. VARIANT_VECTOR2I = 45,
  75. VARIANT_RECT2I = 46,
  76. VARIANT_VECTOR3I = 47,
  77. VARIANT_PACKED_INT64_ARRAY = 48,
  78. VARIANT_PACKED_FLOAT64_ARRAY = 49,
  79. OBJECT_EMPTY = 0,
  80. OBJECT_EXTERNAL_RESOURCE = 1,
  81. OBJECT_INTERNAL_RESOURCE = 2,
  82. OBJECT_EXTERNAL_RESOURCE_INDEX = 3,
  83. // Version 2: added 64 bits support for float and int.
  84. // Version 3: changed nodepath encoding.
  85. // Version 4: new string ID for ext/subresources, breaks forward compat.
  86. FORMAT_VERSION = 4,
  87. FORMAT_VERSION_CAN_RENAME_DEPS = 1,
  88. FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3,
  89. };
  90. void ResourceLoaderBinary::_advance_padding(uint32_t p_len) {
  91. uint32_t extra = 4 - (p_len % 4);
  92. if (extra < 4) {
  93. for (uint32_t i = 0; i < extra; i++) {
  94. f->get_8(); //pad to 32
  95. }
  96. }
  97. }
  98. StringName ResourceLoaderBinary::_get_string() {
  99. uint32_t id = f->get_32();
  100. if (id & 0x80000000) {
  101. uint32_t len = id & 0x7FFFFFFF;
  102. if ((int)len > str_buf.size()) {
  103. str_buf.resize(len);
  104. }
  105. if (len == 0) {
  106. return StringName();
  107. }
  108. f->get_buffer((uint8_t *)&str_buf[0], len);
  109. String s;
  110. s.parse_utf8(&str_buf[0]);
  111. return s;
  112. }
  113. return string_map[id];
  114. }
  115. Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
  116. uint32_t type = f->get_32();
  117. print_bl("find property of type: " + itos(type));
  118. switch (type) {
  119. case VARIANT_NIL: {
  120. r_v = Variant();
  121. } break;
  122. case VARIANT_BOOL: {
  123. r_v = bool(f->get_32());
  124. } break;
  125. case VARIANT_INT: {
  126. r_v = int(f->get_32());
  127. } break;
  128. case VARIANT_INT64: {
  129. r_v = int64_t(f->get_64());
  130. } break;
  131. case VARIANT_FLOAT: {
  132. r_v = f->get_real();
  133. } break;
  134. case VARIANT_DOUBLE: {
  135. r_v = f->get_double();
  136. } break;
  137. case VARIANT_STRING: {
  138. r_v = get_unicode_string();
  139. } break;
  140. case VARIANT_VECTOR2: {
  141. Vector2 v;
  142. v.x = f->get_real();
  143. v.y = f->get_real();
  144. r_v = v;
  145. } break;
  146. case VARIANT_VECTOR2I: {
  147. Vector2i v;
  148. v.x = f->get_32();
  149. v.y = f->get_32();
  150. r_v = v;
  151. } break;
  152. case VARIANT_RECT2: {
  153. Rect2 v;
  154. v.position.x = f->get_real();
  155. v.position.y = f->get_real();
  156. v.size.x = f->get_real();
  157. v.size.y = f->get_real();
  158. r_v = v;
  159. } break;
  160. case VARIANT_RECT2I: {
  161. Rect2i v;
  162. v.position.x = f->get_32();
  163. v.position.y = f->get_32();
  164. v.size.x = f->get_32();
  165. v.size.y = f->get_32();
  166. r_v = v;
  167. } break;
  168. case VARIANT_VECTOR3: {
  169. Vector3 v;
  170. v.x = f->get_real();
  171. v.y = f->get_real();
  172. v.z = f->get_real();
  173. r_v = v;
  174. } break;
  175. case VARIANT_VECTOR3I: {
  176. Vector3i v;
  177. v.x = f->get_32();
  178. v.y = f->get_32();
  179. v.z = f->get_32();
  180. r_v = v;
  181. } break;
  182. case VARIANT_PLANE: {
  183. Plane v;
  184. v.normal.x = f->get_real();
  185. v.normal.y = f->get_real();
  186. v.normal.z = f->get_real();
  187. v.d = f->get_real();
  188. r_v = v;
  189. } break;
  190. case VARIANT_QUATERNION: {
  191. Quaternion v;
  192. v.x = f->get_real();
  193. v.y = f->get_real();
  194. v.z = f->get_real();
  195. v.w = f->get_real();
  196. r_v = v;
  197. } break;
  198. case VARIANT_AABB: {
  199. AABB v;
  200. v.position.x = f->get_real();
  201. v.position.y = f->get_real();
  202. v.position.z = f->get_real();
  203. v.size.x = f->get_real();
  204. v.size.y = f->get_real();
  205. v.size.z = f->get_real();
  206. r_v = v;
  207. } break;
  208. case VARIANT_TRANSFORM2D: {
  209. Transform2D v;
  210. v.elements[0].x = f->get_real();
  211. v.elements[0].y = f->get_real();
  212. v.elements[1].x = f->get_real();
  213. v.elements[1].y = f->get_real();
  214. v.elements[2].x = f->get_real();
  215. v.elements[2].y = f->get_real();
  216. r_v = v;
  217. } break;
  218. case VARIANT_BASIS: {
  219. Basis v;
  220. v.elements[0].x = f->get_real();
  221. v.elements[0].y = f->get_real();
  222. v.elements[0].z = f->get_real();
  223. v.elements[1].x = f->get_real();
  224. v.elements[1].y = f->get_real();
  225. v.elements[1].z = f->get_real();
  226. v.elements[2].x = f->get_real();
  227. v.elements[2].y = f->get_real();
  228. v.elements[2].z = f->get_real();
  229. r_v = v;
  230. } break;
  231. case VARIANT_TRANSFORM3D: {
  232. Transform3D v;
  233. v.basis.elements[0].x = f->get_real();
  234. v.basis.elements[0].y = f->get_real();
  235. v.basis.elements[0].z = f->get_real();
  236. v.basis.elements[1].x = f->get_real();
  237. v.basis.elements[1].y = f->get_real();
  238. v.basis.elements[1].z = f->get_real();
  239. v.basis.elements[2].x = f->get_real();
  240. v.basis.elements[2].y = f->get_real();
  241. v.basis.elements[2].z = f->get_real();
  242. v.origin.x = f->get_real();
  243. v.origin.y = f->get_real();
  244. v.origin.z = f->get_real();
  245. r_v = v;
  246. } break;
  247. case VARIANT_COLOR: {
  248. Color v; // Colors should always be in single-precision.
  249. v.r = f->get_float();
  250. v.g = f->get_float();
  251. v.b = f->get_float();
  252. v.a = f->get_float();
  253. r_v = v;
  254. } break;
  255. case VARIANT_STRING_NAME: {
  256. r_v = StringName(get_unicode_string());
  257. } break;
  258. case VARIANT_NODE_PATH: {
  259. Vector<StringName> names;
  260. Vector<StringName> subnames;
  261. bool absolute;
  262. int name_count = f->get_16();
  263. uint32_t subname_count = f->get_16();
  264. absolute = subname_count & 0x8000;
  265. subname_count &= 0x7FFF;
  266. if (ver_format < FORMAT_VERSION_NO_NODEPATH_PROPERTY) {
  267. subname_count += 1; // has a property field, so we should count it as well
  268. }
  269. for (int i = 0; i < name_count; i++) {
  270. names.push_back(_get_string());
  271. }
  272. for (uint32_t i = 0; i < subname_count; i++) {
  273. subnames.push_back(_get_string());
  274. }
  275. NodePath np = NodePath(names, subnames, absolute);
  276. r_v = np;
  277. } break;
  278. case VARIANT_RID: {
  279. r_v = f->get_32();
  280. } break;
  281. case VARIANT_OBJECT: {
  282. uint32_t objtype = f->get_32();
  283. switch (objtype) {
  284. case OBJECT_EMPTY: {
  285. //do none
  286. } break;
  287. case OBJECT_INTERNAL_RESOURCE: {
  288. uint32_t index = f->get_32();
  289. String path;
  290. if (using_named_scene_ids) { // New format.
  291. ERR_FAIL_INDEX_V((int)index, internal_resources.size(), ERR_PARSE_ERROR);
  292. path = internal_resources[index].path;
  293. } else {
  294. path += res_path + "::" + itos(index);
  295. }
  296. //always use internal cache for loading internal resources
  297. if (!internal_index_cache.has(path)) {
  298. WARN_PRINT(String("Couldn't load resource (no cache): " + path).utf8().get_data());
  299. r_v = Variant();
  300. } else {
  301. r_v = internal_index_cache[path];
  302. }
  303. } break;
  304. case OBJECT_EXTERNAL_RESOURCE: {
  305. //old file format, still around for compatibility
  306. String exttype = get_unicode_string();
  307. String path = get_unicode_string();
  308. if (!path.contains("://") && path.is_relative_path()) {
  309. // path is relative to file being loaded, so convert to a resource path
  310. path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  311. }
  312. if (remaps.find(path)) {
  313. path = remaps[path];
  314. }
  315. RES res = ResourceLoader::load(path, exttype);
  316. if (res.is_null()) {
  317. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  318. }
  319. r_v = res;
  320. } break;
  321. case OBJECT_EXTERNAL_RESOURCE_INDEX: {
  322. //new file format, just refers to an index in the external list
  323. int erindex = f->get_32();
  324. if (erindex < 0 || erindex >= external_resources.size()) {
  325. WARN_PRINT("Broken external resource! (index out of size)");
  326. r_v = Variant();
  327. } else {
  328. if (external_resources[erindex].cache.is_null()) {
  329. //cache not here yet, wait for it?
  330. if (use_sub_threads) {
  331. Error err;
  332. external_resources.write[erindex].cache = ResourceLoader::load_threaded_get(external_resources[erindex].path, &err);
  333. if (err != OK || external_resources[erindex].cache.is_null()) {
  334. if (!ResourceLoader::get_abort_on_missing_resources()) {
  335. ResourceLoader::notify_dependency_error(local_path, external_resources[erindex].path, external_resources[erindex].type);
  336. } else {
  337. error = ERR_FILE_MISSING_DEPENDENCIES;
  338. ERR_FAIL_V_MSG(error, "Can't load dependency: " + external_resources[erindex].path + ".");
  339. }
  340. }
  341. }
  342. }
  343. r_v = external_resources[erindex].cache;
  344. }
  345. } break;
  346. default: {
  347. ERR_FAIL_V(ERR_FILE_CORRUPT);
  348. } break;
  349. }
  350. } break;
  351. case VARIANT_CALLABLE: {
  352. r_v = Callable();
  353. } break;
  354. case VARIANT_SIGNAL: {
  355. r_v = Signal();
  356. } break;
  357. case VARIANT_DICTIONARY: {
  358. uint32_t len = f->get_32();
  359. Dictionary d; //last bit means shared
  360. len &= 0x7FFFFFFF;
  361. for (uint32_t i = 0; i < len; i++) {
  362. Variant key;
  363. Error err = parse_variant(key);
  364. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant.");
  365. Variant value;
  366. err = parse_variant(value);
  367. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant.");
  368. d[key] = value;
  369. }
  370. r_v = d;
  371. } break;
  372. case VARIANT_ARRAY: {
  373. uint32_t len = f->get_32();
  374. Array a; //last bit means shared
  375. len &= 0x7FFFFFFF;
  376. a.resize(len);
  377. for (uint32_t i = 0; i < len; i++) {
  378. Variant val;
  379. Error err = parse_variant(val);
  380. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant.");
  381. a[i] = val;
  382. }
  383. r_v = a;
  384. } break;
  385. case VARIANT_PACKED_BYTE_ARRAY: {
  386. uint32_t len = f->get_32();
  387. Vector<uint8_t> array;
  388. array.resize(len);
  389. uint8_t *w = array.ptrw();
  390. f->get_buffer(w, len);
  391. _advance_padding(len);
  392. r_v = array;
  393. } break;
  394. case VARIANT_PACKED_INT32_ARRAY: {
  395. uint32_t len = f->get_32();
  396. Vector<int32_t> array;
  397. array.resize(len);
  398. int32_t *w = array.ptrw();
  399. f->get_buffer((uint8_t *)w, len * sizeof(int32_t));
  400. #ifdef BIG_ENDIAN_ENABLED
  401. {
  402. uint32_t *ptr = (uint32_t *)w.ptr();
  403. for (int i = 0; i < len; i++) {
  404. ptr[i] = BSWAP32(ptr[i]);
  405. }
  406. }
  407. #endif
  408. r_v = array;
  409. } break;
  410. case VARIANT_PACKED_INT64_ARRAY: {
  411. uint32_t len = f->get_32();
  412. Vector<int64_t> array;
  413. array.resize(len);
  414. int64_t *w = array.ptrw();
  415. f->get_buffer((uint8_t *)w, len * sizeof(int64_t));
  416. #ifdef BIG_ENDIAN_ENABLED
  417. {
  418. uint64_t *ptr = (uint64_t *)w.ptr();
  419. for (int i = 0; i < len; i++) {
  420. ptr[i] = BSWAP64(ptr[i]);
  421. }
  422. }
  423. #endif
  424. r_v = array;
  425. } break;
  426. case VARIANT_PACKED_FLOAT32_ARRAY: {
  427. uint32_t len = f->get_32();
  428. Vector<float> array;
  429. array.resize(len);
  430. float *w = array.ptrw();
  431. f->get_buffer((uint8_t *)w, len * sizeof(float));
  432. #ifdef BIG_ENDIAN_ENABLED
  433. {
  434. uint32_t *ptr = (uint32_t *)w.ptr();
  435. for (int i = 0; i < len; i++) {
  436. ptr[i] = BSWAP32(ptr[i]);
  437. }
  438. }
  439. #endif
  440. r_v = array;
  441. } break;
  442. case VARIANT_PACKED_FLOAT64_ARRAY: {
  443. uint32_t len = f->get_32();
  444. Vector<double> array;
  445. array.resize(len);
  446. double *w = array.ptrw();
  447. f->get_buffer((uint8_t *)w, len * sizeof(double));
  448. #ifdef BIG_ENDIAN_ENABLED
  449. {
  450. uint64_t *ptr = (uint64_t *)w.ptr();
  451. for (int i = 0; i < len; i++) {
  452. ptr[i] = BSWAP64(ptr[i]);
  453. }
  454. }
  455. #endif
  456. r_v = array;
  457. } break;
  458. case VARIANT_PACKED_STRING_ARRAY: {
  459. uint32_t len = f->get_32();
  460. Vector<String> array;
  461. array.resize(len);
  462. String *w = array.ptrw();
  463. for (uint32_t i = 0; i < len; i++) {
  464. w[i] = get_unicode_string();
  465. }
  466. r_v = array;
  467. } break;
  468. case VARIANT_PACKED_VECTOR2_ARRAY: {
  469. uint32_t len = f->get_32();
  470. Vector<Vector2> array;
  471. array.resize(len);
  472. Vector2 *w = array.ptrw();
  473. if (sizeof(Vector2) == 8) {
  474. f->get_buffer((uint8_t *)w, len * sizeof(real_t) * 2);
  475. #ifdef BIG_ENDIAN_ENABLED
  476. {
  477. uint32_t *ptr = (uint32_t *)w.ptr();
  478. for (int i = 0; i < len * 2; i++) {
  479. ptr[i] = BSWAP32(ptr[i]);
  480. }
  481. }
  482. #endif
  483. } else {
  484. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Vector2 size is NOT 8!");
  485. }
  486. r_v = array;
  487. } break;
  488. case VARIANT_PACKED_VECTOR3_ARRAY: {
  489. uint32_t len = f->get_32();
  490. Vector<Vector3> array;
  491. array.resize(len);
  492. Vector3 *w = array.ptrw();
  493. if (sizeof(Vector3) == 12) {
  494. f->get_buffer((uint8_t *)w, len * sizeof(real_t) * 3);
  495. #ifdef BIG_ENDIAN_ENABLED
  496. {
  497. uint32_t *ptr = (uint32_t *)w.ptr();
  498. for (int i = 0; i < len * 3; i++) {
  499. ptr[i] = BSWAP32(ptr[i]);
  500. }
  501. }
  502. #endif
  503. } else {
  504. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Vector3 size is NOT 12!");
  505. }
  506. r_v = array;
  507. } break;
  508. case VARIANT_PACKED_COLOR_ARRAY: {
  509. uint32_t len = f->get_32();
  510. Vector<Color> array;
  511. array.resize(len);
  512. Color *w = array.ptrw();
  513. if (sizeof(Color) == 16) {
  514. f->get_buffer((uint8_t *)w, len * sizeof(real_t) * 4);
  515. #ifdef BIG_ENDIAN_ENABLED
  516. {
  517. uint32_t *ptr = (uint32_t *)w.ptr();
  518. for (int i = 0; i < len * 4; i++) {
  519. ptr[i] = BSWAP32(ptr[i]);
  520. }
  521. }
  522. #endif
  523. } else {
  524. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Color size is NOT 16!");
  525. }
  526. r_v = array;
  527. } break;
  528. default: {
  529. ERR_FAIL_V(ERR_FILE_CORRUPT);
  530. } break;
  531. }
  532. return OK; //never reach anyway
  533. }
  534. void ResourceLoaderBinary::set_local_path(const String &p_local_path) {
  535. res_path = p_local_path;
  536. }
  537. Ref<Resource> ResourceLoaderBinary::get_resource() {
  538. return resource;
  539. }
  540. Error ResourceLoaderBinary::load() {
  541. if (error != OK) {
  542. return error;
  543. }
  544. int stage = 0;
  545. for (int i = 0; i < external_resources.size(); i++) {
  546. String path = external_resources[i].path;
  547. if (remaps.has(path)) {
  548. path = remaps[path];
  549. }
  550. if (!path.contains("://") && path.is_relative_path()) {
  551. // path is relative to file being loaded, so convert to a resource path
  552. path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().plus_file(external_resources[i].path));
  553. }
  554. external_resources.write[i].path = path; //remap happens here, not on load because on load it can actually be used for filesystem dock resource remap
  555. if (!use_sub_threads) {
  556. external_resources.write[i].cache = ResourceLoader::load(path, external_resources[i].type);
  557. if (external_resources[i].cache.is_null()) {
  558. if (!ResourceLoader::get_abort_on_missing_resources()) {
  559. ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type);
  560. } else {
  561. error = ERR_FILE_MISSING_DEPENDENCIES;
  562. ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + ".");
  563. }
  564. }
  565. } else {
  566. Error err = ResourceLoader::load_threaded_request(path, external_resources[i].type, use_sub_threads, ResourceFormatLoader::CACHE_MODE_REUSE, local_path);
  567. if (err != OK) {
  568. if (!ResourceLoader::get_abort_on_missing_resources()) {
  569. ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type);
  570. } else {
  571. error = ERR_FILE_MISSING_DEPENDENCIES;
  572. ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + ".");
  573. }
  574. }
  575. }
  576. stage++;
  577. }
  578. for (int i = 0; i < internal_resources.size(); i++) {
  579. bool main = i == (internal_resources.size() - 1);
  580. //maybe it is loaded already
  581. String path;
  582. String id;
  583. if (!main) {
  584. path = internal_resources[i].path;
  585. if (path.begins_with("local://")) {
  586. path = path.replace_first("local://", "");
  587. id = path;
  588. path = res_path + "::" + path;
  589. internal_resources.write[i].path = path; // Update path.
  590. }
  591. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE && ResourceCache::has(path)) {
  592. RES cached = ResourceCache::get(path);
  593. if (cached.is_valid()) {
  594. //already loaded, don't do anything
  595. stage++;
  596. error = OK;
  597. internal_index_cache[path] = cached;
  598. continue;
  599. }
  600. }
  601. } else {
  602. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE && !ResourceCache::has(res_path)) {
  603. path = res_path;
  604. }
  605. }
  606. uint64_t offset = internal_resources[i].offset;
  607. f->seek(offset);
  608. String t = get_unicode_string();
  609. RES res;
  610. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) {
  611. //use the existing one
  612. Resource *r = ResourceCache::get(path);
  613. if (r->get_class() == t) {
  614. r->reset_state();
  615. res = Ref<Resource>(r);
  616. }
  617. }
  618. if (res.is_null()) {
  619. //did not replace
  620. Object *obj = ClassDB::instantiate(t);
  621. if (!obj) {
  622. error = ERR_FILE_CORRUPT;
  623. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource of unrecognized type in file: " + t + ".");
  624. }
  625. Resource *r = Object::cast_to<Resource>(obj);
  626. if (!r) {
  627. String obj_class = obj->get_class();
  628. error = ERR_FILE_CORRUPT;
  629. memdelete(obj); //bye
  630. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource type in resource field not a resource, type is: " + obj_class + ".");
  631. }
  632. res = RES(r);
  633. if (!path.is_empty() && cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  634. r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); //if got here because the resource with same path has different type, replace it
  635. }
  636. r->set_scene_unique_id(id);
  637. }
  638. if (!main) {
  639. internal_index_cache[path] = res;
  640. }
  641. int pc = f->get_32();
  642. //set properties
  643. for (int j = 0; j < pc; j++) {
  644. StringName name = _get_string();
  645. if (name == StringName()) {
  646. error = ERR_FILE_CORRUPT;
  647. ERR_FAIL_V(ERR_FILE_CORRUPT);
  648. }
  649. Variant value;
  650. error = parse_variant(value);
  651. if (error) {
  652. return error;
  653. }
  654. res->set(name, value);
  655. }
  656. #ifdef TOOLS_ENABLED
  657. res->set_edited(false);
  658. #endif
  659. stage++;
  660. if (progress) {
  661. *progress = (i + 1) / float(internal_resources.size());
  662. }
  663. resource_cache.push_back(res);
  664. if (main) {
  665. f->close();
  666. resource = res;
  667. resource->set_as_translation_remapped(translation_remapped);
  668. error = OK;
  669. return OK;
  670. }
  671. }
  672. return ERR_FILE_EOF;
  673. }
  674. void ResourceLoaderBinary::set_translation_remapped(bool p_remapped) {
  675. translation_remapped = p_remapped;
  676. }
  677. static void save_ustring(FileAccess *f, const String &p_string) {
  678. CharString utf8 = p_string.utf8();
  679. f->store_32(utf8.length() + 1);
  680. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  681. }
  682. static String get_ustring(FileAccess *f) {
  683. int len = f->get_32();
  684. Vector<char> str_buf;
  685. str_buf.resize(len);
  686. f->get_buffer((uint8_t *)&str_buf[0], len);
  687. String s;
  688. s.parse_utf8(&str_buf[0]);
  689. return s;
  690. }
  691. String ResourceLoaderBinary::get_unicode_string() {
  692. int len = f->get_32();
  693. if (len > str_buf.size()) {
  694. str_buf.resize(len);
  695. }
  696. if (len == 0) {
  697. return String();
  698. }
  699. f->get_buffer((uint8_t *)&str_buf[0], len);
  700. String s;
  701. s.parse_utf8(&str_buf[0]);
  702. return s;
  703. }
  704. void ResourceLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) {
  705. open(p_f, false, true);
  706. if (error) {
  707. return;
  708. }
  709. for (int i = 0; i < external_resources.size(); i++) {
  710. String dep;
  711. if (external_resources[i].uid != ResourceUID::INVALID_ID) {
  712. dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid);
  713. } else {
  714. dep = external_resources[i].path;
  715. }
  716. if (p_add_types && !external_resources[i].type.is_empty()) {
  717. dep += "::" + external_resources[i].type;
  718. }
  719. p_dependencies->push_back(dep);
  720. }
  721. }
  722. void ResourceLoaderBinary::open(FileAccess *p_f, bool p_no_resources, bool p_keep_uuid_paths) {
  723. error = OK;
  724. f = p_f;
  725. uint8_t header[4];
  726. f->get_buffer(header, 4);
  727. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  728. // Compressed.
  729. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  730. error = fac->open_after_magic(f);
  731. if (error != OK) {
  732. memdelete(fac);
  733. f->close();
  734. ERR_FAIL_MSG("Failed to open binary resource file: " + local_path + ".");
  735. }
  736. f = fac;
  737. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  738. // Not normal.
  739. error = ERR_FILE_UNRECOGNIZED;
  740. f->close();
  741. ERR_FAIL_MSG("Unrecognized binary resource file: " + local_path + ".");
  742. }
  743. bool big_endian = f->get_32();
  744. bool use_real64 = f->get_32();
  745. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  746. uint32_t ver_major = f->get_32();
  747. uint32_t ver_minor = f->get_32();
  748. ver_format = f->get_32();
  749. print_bl("big endian: " + itos(big_endian));
  750. #ifdef BIG_ENDIAN_ENABLED
  751. print_bl("endian swap: " + itos(!big_endian));
  752. #else
  753. print_bl("endian swap: " + itos(big_endian));
  754. #endif
  755. print_bl("real64: " + itos(use_real64));
  756. print_bl("major: " + itos(ver_major));
  757. print_bl("minor: " + itos(ver_minor));
  758. print_bl("format: " + itos(ver_format));
  759. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  760. f->close();
  761. ERR_FAIL_MSG(vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).",
  762. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  763. }
  764. type = get_unicode_string();
  765. print_bl("type: " + type);
  766. importmd_ofs = f->get_64();
  767. uint32_t flags = f->get_32();
  768. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) {
  769. using_named_scene_ids = true;
  770. }
  771. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) {
  772. using_uids = true;
  773. }
  774. f->real_is_double = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_REAL_T_IS_DOUBLE) != 0;
  775. if (using_uids) {
  776. uid = f->get_64();
  777. } else {
  778. f->get_64(); // skip over uid field
  779. uid = ResourceUID::INVALID_ID;
  780. }
  781. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  782. f->get_32(); //skip a few reserved fields
  783. }
  784. if (p_no_resources) {
  785. return;
  786. }
  787. uint32_t string_table_size = f->get_32();
  788. string_map.resize(string_table_size);
  789. for (uint32_t i = 0; i < string_table_size; i++) {
  790. StringName s = get_unicode_string();
  791. string_map.write[i] = s;
  792. }
  793. print_bl("strings: " + itos(string_table_size));
  794. uint32_t ext_resources_size = f->get_32();
  795. for (uint32_t i = 0; i < ext_resources_size; i++) {
  796. ExtResource er;
  797. er.type = get_unicode_string();
  798. er.path = get_unicode_string();
  799. if (using_uids) {
  800. er.uid = f->get_64();
  801. if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) {
  802. if (ResourceUID::get_singleton()->has_id(er.uid)) {
  803. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  804. er.path = ResourceUID::get_singleton()->get_id_path(er.uid);
  805. } else {
  806. WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data());
  807. }
  808. }
  809. }
  810. external_resources.push_back(er);
  811. }
  812. print_bl("ext resources: " + itos(ext_resources_size));
  813. uint32_t int_resources_size = f->get_32();
  814. for (uint32_t i = 0; i < int_resources_size; i++) {
  815. IntResource ir;
  816. ir.path = get_unicode_string();
  817. ir.offset = f->get_64();
  818. internal_resources.push_back(ir);
  819. }
  820. print_bl("int resources: " + itos(int_resources_size));
  821. if (f->eof_reached()) {
  822. error = ERR_FILE_CORRUPT;
  823. f->close();
  824. ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + ".");
  825. }
  826. }
  827. String ResourceLoaderBinary::recognize(FileAccess *p_f) {
  828. error = OK;
  829. f = p_f;
  830. uint8_t header[4];
  831. f->get_buffer(header, 4);
  832. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  833. // Compressed.
  834. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  835. error = fac->open_after_magic(f);
  836. if (error != OK) {
  837. memdelete(fac);
  838. f->close();
  839. return "";
  840. }
  841. f = fac;
  842. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  843. // Not normal.
  844. error = ERR_FILE_UNRECOGNIZED;
  845. f->close();
  846. return "";
  847. }
  848. bool big_endian = f->get_32();
  849. f->get_32(); // use_real64
  850. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  851. uint32_t ver_major = f->get_32();
  852. f->get_32(); // ver_minor
  853. uint32_t ver_format = f->get_32();
  854. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  855. f->close();
  856. return "";
  857. }
  858. String type = get_unicode_string();
  859. return type;
  860. }
  861. ResourceLoaderBinary::~ResourceLoaderBinary() {
  862. if (f) {
  863. memdelete(f);
  864. }
  865. }
  866. RES ResourceFormatLoaderBinary::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  867. if (r_error) {
  868. *r_error = ERR_FILE_CANT_OPEN;
  869. }
  870. Error err;
  871. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  872. ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot open file '" + p_path + "'.");
  873. ResourceLoaderBinary loader;
  874. loader.cache_mode = p_cache_mode;
  875. loader.use_sub_threads = p_use_sub_threads;
  876. loader.progress = r_progress;
  877. String path = !p_original_path.is_empty() ? p_original_path : p_path;
  878. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  879. loader.res_path = loader.local_path;
  880. loader.open(f);
  881. err = loader.load();
  882. if (r_error) {
  883. *r_error = err;
  884. }
  885. if (err) {
  886. return RES();
  887. }
  888. return loader.resource;
  889. }
  890. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  891. if (p_type.is_empty()) {
  892. get_recognized_extensions(p_extensions);
  893. return;
  894. }
  895. List<String> extensions;
  896. ClassDB::get_extensions_for_type(p_type, &extensions);
  897. extensions.sort();
  898. for (const String &E : extensions) {
  899. String ext = E.to_lower();
  900. p_extensions->push_back(ext);
  901. }
  902. }
  903. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  904. List<String> extensions;
  905. ClassDB::get_resource_base_extensions(&extensions);
  906. extensions.sort();
  907. for (const String &E : extensions) {
  908. String ext = E.to_lower();
  909. p_extensions->push_back(ext);
  910. }
  911. }
  912. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  913. return true; //handles all
  914. }
  915. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  916. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  917. ERR_FAIL_COND_MSG(!f, "Cannot open file '" + p_path + "'.");
  918. ResourceLoaderBinary loader;
  919. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  920. loader.res_path = loader.local_path;
  921. loader.get_dependencies(f, p_dependencies, p_add_types);
  922. }
  923. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  924. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  925. ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  926. FileAccess *fw = nullptr;
  927. String local_path = p_path.get_base_dir();
  928. uint8_t header[4];
  929. f->get_buffer(header, 4);
  930. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  931. // Compressed.
  932. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  933. Error err = fac->open_after_magic(f);
  934. if (err != OK) {
  935. memdelete(fac);
  936. memdelete(f);
  937. ERR_FAIL_V_MSG(err, "Cannot open file '" + p_path + "'.");
  938. }
  939. f = fac;
  940. FileAccessCompressed *facw = memnew(FileAccessCompressed);
  941. facw->configure("RSCC");
  942. err = facw->_open(p_path + ".depren", FileAccess::WRITE);
  943. if (err) {
  944. memdelete(fac);
  945. memdelete(facw);
  946. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'.");
  947. }
  948. fw = facw;
  949. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  950. // Not normal.
  951. memdelete(f);
  952. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'.");
  953. } else {
  954. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  955. if (!fw) {
  956. memdelete(f);
  957. }
  958. ERR_FAIL_COND_V_MSG(!fw, ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'.");
  959. uint8_t magic[4] = { 'R', 'S', 'R', 'C' };
  960. fw->store_buffer(magic, 4);
  961. }
  962. bool big_endian = f->get_32();
  963. bool use_real64 = f->get_32();
  964. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  965. #ifdef BIG_ENDIAN_ENABLED
  966. fw->store_32(!big_endian);
  967. #else
  968. fw->store_32(big_endian);
  969. #endif
  970. fw->set_big_endian(big_endian != 0);
  971. fw->store_32(use_real64); //use real64
  972. uint32_t ver_major = f->get_32();
  973. uint32_t ver_minor = f->get_32();
  974. uint32_t ver_format = f->get_32();
  975. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  976. memdelete(f);
  977. memdelete(fw);
  978. {
  979. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  980. da->remove(p_path + ".depren");
  981. }
  982. // Use the old approach.
  983. WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'.");
  984. Error err;
  985. f = FileAccess::open(p_path, FileAccess::READ, &err);
  986. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  987. ResourceLoaderBinary loader;
  988. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  989. loader.res_path = loader.local_path;
  990. loader.remaps = p_map;
  991. loader.open(f);
  992. err = loader.load();
  993. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  994. RES res = loader.get_resource();
  995. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  996. return ResourceFormatSaverBinary::singleton->save(p_path, res);
  997. }
  998. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  999. memdelete(f);
  1000. memdelete(fw);
  1001. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  1002. vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).",
  1003. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  1004. }
  1005. // Since we're not actually converting the file contents, leave the version
  1006. // numbers in the file untouched.
  1007. fw->store_32(ver_major);
  1008. fw->store_32(ver_minor);
  1009. fw->store_32(ver_format);
  1010. save_ustring(fw, get_ustring(f)); //type
  1011. uint64_t md_ofs = f->get_position();
  1012. uint64_t importmd_ofs = f->get_64();
  1013. fw->store_64(0); //metadata offset
  1014. uint32_t flags = f->get_32();
  1015. bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
  1016. uint64_t uid_data = f->get_64();
  1017. fw->store_32(flags);
  1018. fw->store_64(uid_data);
  1019. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1020. fw->store_32(0); // reserved
  1021. f->get_32();
  1022. }
  1023. //string table
  1024. uint32_t string_table_size = f->get_32();
  1025. fw->store_32(string_table_size);
  1026. for (uint32_t i = 0; i < string_table_size; i++) {
  1027. String s = get_ustring(f);
  1028. save_ustring(fw, s);
  1029. }
  1030. //external resources
  1031. uint32_t ext_resources_size = f->get_32();
  1032. fw->store_32(ext_resources_size);
  1033. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1034. String type = get_ustring(f);
  1035. String path = get_ustring(f);
  1036. if (using_uids) {
  1037. ResourceUID::ID uid = f->get_64();
  1038. if (uid != ResourceUID::INVALID_ID) {
  1039. if (ResourceUID::get_singleton()->has_id(uid)) {
  1040. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  1041. path = ResourceUID::get_singleton()->get_id_path(uid);
  1042. }
  1043. }
  1044. }
  1045. bool relative = false;
  1046. if (!path.begins_with("res://")) {
  1047. path = local_path.plus_file(path).simplify_path();
  1048. relative = true;
  1049. }
  1050. if (p_map.has(path)) {
  1051. String np = p_map[path];
  1052. path = np;
  1053. }
  1054. String full_path = path;
  1055. if (relative) {
  1056. //restore relative
  1057. path = local_path.path_to_file(path);
  1058. }
  1059. save_ustring(fw, type);
  1060. save_ustring(fw, path);
  1061. if (using_uids) {
  1062. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
  1063. fw->store_64(uid);
  1064. }
  1065. }
  1066. int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position();
  1067. //internal resources
  1068. uint32_t int_resources_size = f->get_32();
  1069. fw->store_32(int_resources_size);
  1070. for (uint32_t i = 0; i < int_resources_size; i++) {
  1071. String path = get_ustring(f);
  1072. uint64_t offset = f->get_64();
  1073. save_ustring(fw, path);
  1074. fw->store_64(offset + size_diff);
  1075. }
  1076. //rest of file
  1077. uint8_t b = f->get_8();
  1078. while (!f->eof_reached()) {
  1079. fw->store_8(b);
  1080. b = f->get_8();
  1081. }
  1082. bool all_ok = fw->get_error() == OK;
  1083. fw->seek(md_ofs);
  1084. fw->store_64(importmd_ofs + size_diff);
  1085. memdelete(f);
  1086. memdelete(fw);
  1087. if (!all_ok) {
  1088. return ERR_CANT_CREATE;
  1089. }
  1090. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1091. da->remove(p_path);
  1092. da->rename(p_path + ".depren", p_path);
  1093. return OK;
  1094. }
  1095. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1096. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1097. if (!f) {
  1098. return ""; //could not read
  1099. }
  1100. ResourceLoaderBinary loader;
  1101. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1102. loader.res_path = loader.local_path;
  1103. String r = loader.recognize(f);
  1104. return ClassDB::get_compatibility_remapped_class(r);
  1105. }
  1106. ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const {
  1107. String ext = p_path.get_extension().to_lower();
  1108. if (!ClassDB::is_resource_extension(ext)) {
  1109. return ResourceUID::INVALID_ID;
  1110. }
  1111. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1112. if (!f) {
  1113. return ResourceUID::INVALID_ID; //could not read
  1114. }
  1115. ResourceLoaderBinary loader;
  1116. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1117. loader.res_path = loader.local_path;
  1118. loader.open(f, true);
  1119. if (loader.error != OK) {
  1120. return ResourceUID::INVALID_ID; //could not read
  1121. }
  1122. return loader.uid;
  1123. }
  1124. ///////////////////////////////////////////////////////////
  1125. ///////////////////////////////////////////////////////////
  1126. ///////////////////////////////////////////////////////////
  1127. void ResourceFormatSaverBinaryInstance::_pad_buffer(FileAccess *f, int p_bytes) {
  1128. int extra = 4 - (p_bytes % 4);
  1129. if (extra < 4) {
  1130. for (int i = 0; i < extra; i++) {
  1131. f->store_8(0); //pad to 32
  1132. }
  1133. }
  1134. }
  1135. void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Variant &p_property, Map<RES, int> &resource_map, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint) {
  1136. switch (p_property.get_type()) {
  1137. case Variant::NIL: {
  1138. f->store_32(VARIANT_NIL);
  1139. // don't store anything
  1140. } break;
  1141. case Variant::BOOL: {
  1142. f->store_32(VARIANT_BOOL);
  1143. bool val = p_property;
  1144. f->store_32(val);
  1145. } break;
  1146. case Variant::INT: {
  1147. int64_t val = p_property;
  1148. if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) {
  1149. f->store_32(VARIANT_INT64);
  1150. f->store_64(val);
  1151. } else {
  1152. f->store_32(VARIANT_INT);
  1153. f->store_32(int32_t(p_property));
  1154. }
  1155. } break;
  1156. case Variant::FLOAT: {
  1157. double d = p_property;
  1158. float fl = d;
  1159. if (double(fl) != d) {
  1160. f->store_32(VARIANT_DOUBLE);
  1161. f->store_double(d);
  1162. } else {
  1163. f->store_32(VARIANT_FLOAT);
  1164. f->store_real(fl);
  1165. }
  1166. } break;
  1167. case Variant::STRING: {
  1168. f->store_32(VARIANT_STRING);
  1169. String val = p_property;
  1170. save_unicode_string(f, val);
  1171. } break;
  1172. case Variant::VECTOR2: {
  1173. f->store_32(VARIANT_VECTOR2);
  1174. Vector2 val = p_property;
  1175. f->store_real(val.x);
  1176. f->store_real(val.y);
  1177. } break;
  1178. case Variant::VECTOR2I: {
  1179. f->store_32(VARIANT_VECTOR2I);
  1180. Vector2i val = p_property;
  1181. f->store_32(val.x);
  1182. f->store_32(val.y);
  1183. } break;
  1184. case Variant::RECT2: {
  1185. f->store_32(VARIANT_RECT2);
  1186. Rect2 val = p_property;
  1187. f->store_real(val.position.x);
  1188. f->store_real(val.position.y);
  1189. f->store_real(val.size.x);
  1190. f->store_real(val.size.y);
  1191. } break;
  1192. case Variant::RECT2I: {
  1193. f->store_32(VARIANT_RECT2I);
  1194. Rect2i val = p_property;
  1195. f->store_32(val.position.x);
  1196. f->store_32(val.position.y);
  1197. f->store_32(val.size.x);
  1198. f->store_32(val.size.y);
  1199. } break;
  1200. case Variant::VECTOR3: {
  1201. f->store_32(VARIANT_VECTOR3);
  1202. Vector3 val = p_property;
  1203. f->store_real(val.x);
  1204. f->store_real(val.y);
  1205. f->store_real(val.z);
  1206. } break;
  1207. case Variant::VECTOR3I: {
  1208. f->store_32(VARIANT_VECTOR3I);
  1209. Vector3i val = p_property;
  1210. f->store_32(val.x);
  1211. f->store_32(val.y);
  1212. f->store_32(val.z);
  1213. } break;
  1214. case Variant::PLANE: {
  1215. f->store_32(VARIANT_PLANE);
  1216. Plane val = p_property;
  1217. f->store_real(val.normal.x);
  1218. f->store_real(val.normal.y);
  1219. f->store_real(val.normal.z);
  1220. f->store_real(val.d);
  1221. } break;
  1222. case Variant::QUATERNION: {
  1223. f->store_32(VARIANT_QUATERNION);
  1224. Quaternion val = p_property;
  1225. f->store_real(val.x);
  1226. f->store_real(val.y);
  1227. f->store_real(val.z);
  1228. f->store_real(val.w);
  1229. } break;
  1230. case Variant::AABB: {
  1231. f->store_32(VARIANT_AABB);
  1232. AABB val = p_property;
  1233. f->store_real(val.position.x);
  1234. f->store_real(val.position.y);
  1235. f->store_real(val.position.z);
  1236. f->store_real(val.size.x);
  1237. f->store_real(val.size.y);
  1238. f->store_real(val.size.z);
  1239. } break;
  1240. case Variant::TRANSFORM2D: {
  1241. f->store_32(VARIANT_TRANSFORM2D);
  1242. Transform2D val = p_property;
  1243. f->store_real(val.elements[0].x);
  1244. f->store_real(val.elements[0].y);
  1245. f->store_real(val.elements[1].x);
  1246. f->store_real(val.elements[1].y);
  1247. f->store_real(val.elements[2].x);
  1248. f->store_real(val.elements[2].y);
  1249. } break;
  1250. case Variant::BASIS: {
  1251. f->store_32(VARIANT_BASIS);
  1252. Basis val = p_property;
  1253. f->store_real(val.elements[0].x);
  1254. f->store_real(val.elements[0].y);
  1255. f->store_real(val.elements[0].z);
  1256. f->store_real(val.elements[1].x);
  1257. f->store_real(val.elements[1].y);
  1258. f->store_real(val.elements[1].z);
  1259. f->store_real(val.elements[2].x);
  1260. f->store_real(val.elements[2].y);
  1261. f->store_real(val.elements[2].z);
  1262. } break;
  1263. case Variant::TRANSFORM3D: {
  1264. f->store_32(VARIANT_TRANSFORM3D);
  1265. Transform3D val = p_property;
  1266. f->store_real(val.basis.elements[0].x);
  1267. f->store_real(val.basis.elements[0].y);
  1268. f->store_real(val.basis.elements[0].z);
  1269. f->store_real(val.basis.elements[1].x);
  1270. f->store_real(val.basis.elements[1].y);
  1271. f->store_real(val.basis.elements[1].z);
  1272. f->store_real(val.basis.elements[2].x);
  1273. f->store_real(val.basis.elements[2].y);
  1274. f->store_real(val.basis.elements[2].z);
  1275. f->store_real(val.origin.x);
  1276. f->store_real(val.origin.y);
  1277. f->store_real(val.origin.z);
  1278. } break;
  1279. case Variant::COLOR: {
  1280. f->store_32(VARIANT_COLOR);
  1281. Color val = p_property;
  1282. f->store_real(val.r);
  1283. f->store_real(val.g);
  1284. f->store_real(val.b);
  1285. f->store_real(val.a);
  1286. } break;
  1287. case Variant::STRING_NAME: {
  1288. f->store_32(VARIANT_STRING_NAME);
  1289. String val = p_property;
  1290. save_unicode_string(f, val);
  1291. } break;
  1292. case Variant::NODE_PATH: {
  1293. f->store_32(VARIANT_NODE_PATH);
  1294. NodePath np = p_property;
  1295. f->store_16(np.get_name_count());
  1296. uint16_t snc = np.get_subname_count();
  1297. if (np.is_absolute()) {
  1298. snc |= 0x8000;
  1299. }
  1300. f->store_16(snc);
  1301. for (int i = 0; i < np.get_name_count(); i++) {
  1302. if (string_map.has(np.get_name(i))) {
  1303. f->store_32(string_map[np.get_name(i)]);
  1304. } else {
  1305. save_unicode_string(f, np.get_name(i), true);
  1306. }
  1307. }
  1308. for (int i = 0; i < np.get_subname_count(); i++) {
  1309. if (string_map.has(np.get_subname(i))) {
  1310. f->store_32(string_map[np.get_subname(i)]);
  1311. } else {
  1312. save_unicode_string(f, np.get_subname(i), true);
  1313. }
  1314. }
  1315. } break;
  1316. case Variant::RID: {
  1317. f->store_32(VARIANT_RID);
  1318. WARN_PRINT("Can't save RIDs.");
  1319. RID val = p_property;
  1320. f->store_32(val.get_id());
  1321. } break;
  1322. case Variant::OBJECT: {
  1323. f->store_32(VARIANT_OBJECT);
  1324. RES res = p_property;
  1325. if (res.is_null()) {
  1326. f->store_32(OBJECT_EMPTY);
  1327. return; // don't save it
  1328. }
  1329. if (!res->is_built_in()) {
  1330. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1331. f->store_32(external_resources[res]);
  1332. } else {
  1333. if (!resource_map.has(res)) {
  1334. f->store_32(OBJECT_EMPTY);
  1335. ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference.");
  1336. }
  1337. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1338. f->store_32(resource_map[res]);
  1339. //internal resource
  1340. }
  1341. } break;
  1342. case Variant::CALLABLE: {
  1343. f->store_32(VARIANT_CALLABLE);
  1344. WARN_PRINT("Can't save Callables.");
  1345. } break;
  1346. case Variant::SIGNAL: {
  1347. f->store_32(VARIANT_SIGNAL);
  1348. WARN_PRINT("Can't save Signals.");
  1349. } break;
  1350. case Variant::DICTIONARY: {
  1351. f->store_32(VARIANT_DICTIONARY);
  1352. Dictionary d = p_property;
  1353. f->store_32(uint32_t(d.size()));
  1354. List<Variant> keys;
  1355. d.get_key_list(&keys);
  1356. for (const Variant &E : keys) {
  1357. write_variant(f, E, resource_map, external_resources, string_map);
  1358. write_variant(f, d[E], resource_map, external_resources, string_map);
  1359. }
  1360. } break;
  1361. case Variant::ARRAY: {
  1362. f->store_32(VARIANT_ARRAY);
  1363. Array a = p_property;
  1364. f->store_32(uint32_t(a.size()));
  1365. for (int i = 0; i < a.size(); i++) {
  1366. write_variant(f, a[i], resource_map, external_resources, string_map);
  1367. }
  1368. } break;
  1369. case Variant::PACKED_BYTE_ARRAY: {
  1370. f->store_32(VARIANT_PACKED_BYTE_ARRAY);
  1371. Vector<uint8_t> arr = p_property;
  1372. int len = arr.size();
  1373. f->store_32(len);
  1374. const uint8_t *r = arr.ptr();
  1375. f->store_buffer(r, len);
  1376. _pad_buffer(f, len);
  1377. } break;
  1378. case Variant::PACKED_INT32_ARRAY: {
  1379. f->store_32(VARIANT_PACKED_INT32_ARRAY);
  1380. Vector<int32_t> arr = p_property;
  1381. int len = arr.size();
  1382. f->store_32(len);
  1383. const int32_t *r = arr.ptr();
  1384. for (int i = 0; i < len; i++) {
  1385. f->store_32(r[i]);
  1386. }
  1387. } break;
  1388. case Variant::PACKED_INT64_ARRAY: {
  1389. f->store_32(VARIANT_PACKED_INT64_ARRAY);
  1390. Vector<int64_t> arr = p_property;
  1391. int len = arr.size();
  1392. f->store_32(len);
  1393. const int64_t *r = arr.ptr();
  1394. for (int i = 0; i < len; i++) {
  1395. f->store_64(r[i]);
  1396. }
  1397. } break;
  1398. case Variant::PACKED_FLOAT32_ARRAY: {
  1399. f->store_32(VARIANT_PACKED_FLOAT32_ARRAY);
  1400. Vector<float> arr = p_property;
  1401. int len = arr.size();
  1402. f->store_32(len);
  1403. const float *r = arr.ptr();
  1404. for (int i = 0; i < len; i++) {
  1405. f->store_real(r[i]);
  1406. }
  1407. } break;
  1408. case Variant::PACKED_FLOAT64_ARRAY: {
  1409. f->store_32(VARIANT_PACKED_FLOAT64_ARRAY);
  1410. Vector<double> arr = p_property;
  1411. int len = arr.size();
  1412. f->store_32(len);
  1413. const double *r = arr.ptr();
  1414. for (int i = 0; i < len; i++) {
  1415. f->store_double(r[i]);
  1416. }
  1417. } break;
  1418. case Variant::PACKED_STRING_ARRAY: {
  1419. f->store_32(VARIANT_PACKED_STRING_ARRAY);
  1420. Vector<String> arr = p_property;
  1421. int len = arr.size();
  1422. f->store_32(len);
  1423. const String *r = arr.ptr();
  1424. for (int i = 0; i < len; i++) {
  1425. save_unicode_string(f, r[i]);
  1426. }
  1427. } break;
  1428. case Variant::PACKED_VECTOR3_ARRAY: {
  1429. f->store_32(VARIANT_PACKED_VECTOR3_ARRAY);
  1430. Vector<Vector3> arr = p_property;
  1431. int len = arr.size();
  1432. f->store_32(len);
  1433. const Vector3 *r = arr.ptr();
  1434. for (int i = 0; i < len; i++) {
  1435. f->store_real(r[i].x);
  1436. f->store_real(r[i].y);
  1437. f->store_real(r[i].z);
  1438. }
  1439. } break;
  1440. case Variant::PACKED_VECTOR2_ARRAY: {
  1441. f->store_32(VARIANT_PACKED_VECTOR2_ARRAY);
  1442. Vector<Vector2> arr = p_property;
  1443. int len = arr.size();
  1444. f->store_32(len);
  1445. const Vector2 *r = arr.ptr();
  1446. for (int i = 0; i < len; i++) {
  1447. f->store_real(r[i].x);
  1448. f->store_real(r[i].y);
  1449. }
  1450. } break;
  1451. case Variant::PACKED_COLOR_ARRAY: {
  1452. f->store_32(VARIANT_PACKED_COLOR_ARRAY);
  1453. Vector<Color> arr = p_property;
  1454. int len = arr.size();
  1455. f->store_32(len);
  1456. const Color *r = arr.ptr();
  1457. for (int i = 0; i < len; i++) {
  1458. f->store_real(r[i].r);
  1459. f->store_real(r[i].g);
  1460. f->store_real(r[i].b);
  1461. f->store_real(r[i].a);
  1462. }
  1463. } break;
  1464. default: {
  1465. ERR_FAIL_MSG("Invalid variant.");
  1466. }
  1467. }
  1468. }
  1469. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1470. switch (p_variant.get_type()) {
  1471. case Variant::OBJECT: {
  1472. RES res = p_variant;
  1473. if (res.is_null() || external_resources.has(res)) {
  1474. return;
  1475. }
  1476. if (!p_main && (!bundle_resources) && !res->is_built_in()) {
  1477. if (res->get_path() == path) {
  1478. ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
  1479. return;
  1480. }
  1481. int idx = external_resources.size();
  1482. external_resources[res] = idx;
  1483. return;
  1484. }
  1485. if (resource_set.has(res)) {
  1486. return;
  1487. }
  1488. List<PropertyInfo> property_list;
  1489. res->get_property_list(&property_list);
  1490. for (const PropertyInfo &E : property_list) {
  1491. if (E.usage & PROPERTY_USAGE_STORAGE) {
  1492. Variant value = res->get(E.name);
  1493. if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1494. RES sres = value;
  1495. if (sres.is_valid()) {
  1496. NonPersistentKey npk;
  1497. npk.base = res;
  1498. npk.property = E.name;
  1499. non_persistent_map[npk] = sres;
  1500. resource_set.insert(sres);
  1501. saved_resources.push_back(sres);
  1502. }
  1503. } else {
  1504. _find_resources(value);
  1505. }
  1506. }
  1507. }
  1508. resource_set.insert(res);
  1509. saved_resources.push_back(res);
  1510. } break;
  1511. case Variant::ARRAY: {
  1512. Array varray = p_variant;
  1513. int len = varray.size();
  1514. for (int i = 0; i < len; i++) {
  1515. const Variant &v = varray.get(i);
  1516. _find_resources(v);
  1517. }
  1518. } break;
  1519. case Variant::DICTIONARY: {
  1520. Dictionary d = p_variant;
  1521. List<Variant> keys;
  1522. d.get_key_list(&keys);
  1523. for (const Variant &E : keys) {
  1524. _find_resources(E);
  1525. Variant v = d[E];
  1526. _find_resources(v);
  1527. }
  1528. } break;
  1529. case Variant::NODE_PATH: {
  1530. //take the chance and save node path strings
  1531. NodePath np = p_variant;
  1532. for (int i = 0; i < np.get_name_count(); i++) {
  1533. get_string_index(np.get_name(i));
  1534. }
  1535. for (int i = 0; i < np.get_subname_count(); i++) {
  1536. get_string_index(np.get_subname(i));
  1537. }
  1538. } break;
  1539. default: {
  1540. }
  1541. }
  1542. }
  1543. void ResourceFormatSaverBinaryInstance::save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len) {
  1544. CharString utf8 = p_string.utf8();
  1545. if (p_bit_on_len) {
  1546. f->store_32((utf8.length() + 1) | 0x80000000);
  1547. } else {
  1548. f->store_32(utf8.length() + 1);
  1549. }
  1550. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1551. }
  1552. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1553. StringName s = p_string;
  1554. if (string_map.has(s)) {
  1555. return string_map[s];
  1556. }
  1557. string_map[s] = strings.size();
  1558. strings.push_back(s);
  1559. return strings.size() - 1;
  1560. }
  1561. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1562. Error err;
  1563. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1564. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1565. fac->configure("RSCC");
  1566. f = fac;
  1567. err = fac->_open(p_path, FileAccess::WRITE);
  1568. if (err) {
  1569. memdelete(f);
  1570. }
  1571. } else {
  1572. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1573. }
  1574. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'.");
  1575. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1576. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1577. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1578. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1579. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1580. if (!p_path.begins_with("res://")) {
  1581. takeover_paths = false;
  1582. }
  1583. local_path = p_path.get_base_dir();
  1584. path = ProjectSettings::get_singleton()->localize_path(p_path);
  1585. _find_resources(p_resource, true);
  1586. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1587. //save header compressed
  1588. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1589. f->store_buffer(header, 4);
  1590. }
  1591. if (big_endian) {
  1592. f->store_32(1);
  1593. f->set_big_endian(true);
  1594. } else {
  1595. f->store_32(0);
  1596. }
  1597. f->store_32(0); //64 bits file, false for now
  1598. f->store_32(VERSION_MAJOR);
  1599. f->store_32(VERSION_MINOR);
  1600. f->store_32(FORMAT_VERSION);
  1601. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1602. f->close();
  1603. memdelete(f);
  1604. return ERR_CANT_CREATE;
  1605. }
  1606. save_unicode_string(f, p_resource->get_class());
  1607. f->store_64(0); //offset to import metadata
  1608. {
  1609. uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS;
  1610. #ifdef REAL_T_IS_DOUBLE
  1611. format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE;
  1612. #endif
  1613. f->store_32(format_flags);
  1614. }
  1615. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true);
  1616. f->store_64(uid);
  1617. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1618. f->store_32(0); // reserved
  1619. }
  1620. List<ResourceData> resources;
  1621. {
  1622. for (const RES &E : saved_resources) {
  1623. ResourceData &rd = resources.push_back(ResourceData())->get();
  1624. rd.type = E->get_class();
  1625. List<PropertyInfo> property_list;
  1626. E->get_property_list(&property_list);
  1627. for (const PropertyInfo &F : property_list) {
  1628. if (skip_editor && F.name.begins_with("__editor")) {
  1629. continue;
  1630. }
  1631. if ((F.usage & PROPERTY_USAGE_STORAGE)) {
  1632. Property p;
  1633. p.name_idx = get_string_index(F.name);
  1634. if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1635. NonPersistentKey npk;
  1636. npk.base = E;
  1637. npk.property = F.name;
  1638. if (non_persistent_map.has(npk)) {
  1639. p.value = non_persistent_map[npk];
  1640. }
  1641. } else {
  1642. p.value = E->get(F.name);
  1643. }
  1644. Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name);
  1645. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) {
  1646. continue;
  1647. }
  1648. p.pi = F;
  1649. rd.properties.push_back(p);
  1650. }
  1651. }
  1652. }
  1653. }
  1654. f->store_32(strings.size()); //string table size
  1655. for (int i = 0; i < strings.size(); i++) {
  1656. save_unicode_string(f, strings[i]);
  1657. }
  1658. // save external resource table
  1659. f->store_32(external_resources.size()); //amount of external resources
  1660. Vector<RES> save_order;
  1661. save_order.resize(external_resources.size());
  1662. for (const KeyValue<RES, int> &E : external_resources) {
  1663. save_order.write[E.value] = E.key;
  1664. }
  1665. for (int i = 0; i < save_order.size(); i++) {
  1666. save_unicode_string(f, save_order[i]->get_save_class());
  1667. String path = save_order[i]->get_path();
  1668. path = relative_paths ? local_path.path_to_file(path) : path;
  1669. save_unicode_string(f, path);
  1670. ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false);
  1671. f->store_64(ruid);
  1672. }
  1673. // save internal resource table
  1674. f->store_32(saved_resources.size()); //amount of internal resources
  1675. Vector<uint64_t> ofs_pos;
  1676. Set<String> used_unique_ids;
  1677. for (RES &r : saved_resources) {
  1678. if (r->is_built_in()) {
  1679. if (!r->get_scene_unique_id().is_empty()) {
  1680. if (used_unique_ids.has(r->get_scene_unique_id())) {
  1681. r->set_scene_unique_id("");
  1682. } else {
  1683. used_unique_ids.insert(r->get_scene_unique_id());
  1684. }
  1685. }
  1686. }
  1687. }
  1688. Map<RES, int> resource_map;
  1689. int res_index = 0;
  1690. for (RES &r : saved_resources) {
  1691. if (r->is_built_in()) {
  1692. if (r->get_scene_unique_id().is_empty()) {
  1693. String new_id;
  1694. while (true) {
  1695. new_id = r->get_class() + "_" + Resource::generate_scene_unique_id();
  1696. if (!used_unique_ids.has(new_id)) {
  1697. break;
  1698. }
  1699. }
  1700. r->set_scene_unique_id(new_id);
  1701. used_unique_ids.insert(new_id);
  1702. }
  1703. save_unicode_string(f, "local://" + r->get_scene_unique_id());
  1704. if (takeover_paths) {
  1705. r->set_path(p_path + "::" + r->get_scene_unique_id(), true);
  1706. }
  1707. #ifdef TOOLS_ENABLED
  1708. r->set_edited(false);
  1709. #endif
  1710. } else {
  1711. save_unicode_string(f, r->get_path()); //actual external
  1712. }
  1713. ofs_pos.push_back(f->get_position());
  1714. f->store_64(0); //offset in 64 bits
  1715. resource_map[r] = res_index++;
  1716. }
  1717. Vector<uint64_t> ofs_table;
  1718. //now actually save the resources
  1719. for (const ResourceData &rd : resources) {
  1720. ofs_table.push_back(f->get_position());
  1721. save_unicode_string(f, rd.type);
  1722. f->store_32(rd.properties.size());
  1723. for (const Property &p : rd.properties) {
  1724. f->store_32(p.name_idx);
  1725. write_variant(f, p.value, resource_map, external_resources, string_map, p.pi);
  1726. }
  1727. }
  1728. for (int i = 0; i < ofs_table.size(); i++) {
  1729. f->seek(ofs_pos[i]);
  1730. f->store_64(ofs_table[i]);
  1731. }
  1732. f->seek_end();
  1733. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1734. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1735. f->close();
  1736. memdelete(f);
  1737. return ERR_CANT_CREATE;
  1738. }
  1739. f->close();
  1740. memdelete(f);
  1741. return OK;
  1742. }
  1743. Error ResourceFormatSaverBinary::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1744. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1745. ResourceFormatSaverBinaryInstance saver;
  1746. return saver.save(local_path, p_resource, p_flags);
  1747. }
  1748. bool ResourceFormatSaverBinary::recognize(const RES &p_resource) const {
  1749. return true; //all recognized
  1750. }
  1751. void ResourceFormatSaverBinary::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1752. String base = p_resource->get_base_extension().to_lower();
  1753. p_extensions->push_back(base);
  1754. if (base != "res") {
  1755. p_extensions->push_back("res");
  1756. }
  1757. }
  1758. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr;
  1759. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  1760. singleton = this;
  1761. }