resource_format_binary.cpp 57 KB

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