resource_format_binary.cpp 69 KB

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