resource_format_binary.cpp 70 KB

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