resource_format_binary.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  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/version.h"
  38. //#define print_bl(m_what) print_line(m_what)
  39. #define print_bl(m_what) (void)(m_what)
  40. enum {
  41. //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization)
  42. VARIANT_NIL = 1,
  43. VARIANT_BOOL = 2,
  44. VARIANT_INT = 3,
  45. VARIANT_FLOAT = 4,
  46. VARIANT_STRING = 5,
  47. VARIANT_VECTOR2 = 10,
  48. VARIANT_RECT2 = 11,
  49. VARIANT_VECTOR3 = 12,
  50. VARIANT_PLANE = 13,
  51. VARIANT_QUATERNION = 14,
  52. VARIANT_AABB = 15,
  53. VARIANT_BASIS = 16,
  54. VARIANT_TRANSFORM3D = 17,
  55. VARIANT_TRANSFORM2D = 18,
  56. VARIANT_COLOR = 20,
  57. VARIANT_NODE_PATH = 22,
  58. VARIANT_RID = 23,
  59. VARIANT_OBJECT = 24,
  60. VARIANT_INPUT_EVENT = 25,
  61. VARIANT_DICTIONARY = 26,
  62. VARIANT_ARRAY = 30,
  63. VARIANT_PACKED_BYTE_ARRAY = 31,
  64. VARIANT_PACKED_INT32_ARRAY = 32,
  65. VARIANT_PACKED_FLOAT32_ARRAY = 33,
  66. VARIANT_PACKED_STRING_ARRAY = 34,
  67. VARIANT_PACKED_VECTOR3_ARRAY = 35,
  68. VARIANT_PACKED_COLOR_ARRAY = 36,
  69. VARIANT_PACKED_VECTOR2_ARRAY = 37,
  70. VARIANT_INT64 = 40,
  71. VARIANT_DOUBLE = 41,
  72. VARIANT_CALLABLE = 42,
  73. VARIANT_SIGNAL = 43,
  74. VARIANT_STRING_NAME = 44,
  75. VARIANT_VECTOR2I = 45,
  76. VARIANT_RECT2I = 46,
  77. VARIANT_VECTOR3I = 47,
  78. VARIANT_PACKED_INT64_ARRAY = 48,
  79. VARIANT_PACKED_FLOAT64_ARRAY = 49,
  80. VARIANT_VECTOR4 = 50,
  81. VARIANT_VECTOR4I = 51,
  82. VARIANT_PROJECTION = 52,
  83. OBJECT_EMPTY = 0,
  84. OBJECT_EXTERNAL_RESOURCE = 1,
  85. OBJECT_INTERNAL_RESOURCE = 2,
  86. OBJECT_EXTERNAL_RESOURCE_INDEX = 3,
  87. // Version 2: added 64 bits support for float and int.
  88. // Version 3: changed nodepath encoding.
  89. // Version 4: new string ID for ext/subresources, breaks forward compat.
  90. // Version 5: Ability to store script class in the header.
  91. FORMAT_VERSION = 5,
  92. FORMAT_VERSION_CAN_RENAME_DEPS = 1,
  93. FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3,
  94. };
  95. void ResourceLoaderBinary::_advance_padding(uint32_t p_len) {
  96. uint32_t extra = 4 - (p_len % 4);
  97. if (extra < 4) {
  98. for (uint32_t i = 0; i < extra; i++) {
  99. f->get_8(); //pad to 32
  100. }
  101. }
  102. }
  103. static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) {
  104. if (f->real_is_double) {
  105. if constexpr (sizeof(real_t) == 8) {
  106. // Ideal case with double-precision
  107. f->get_buffer((uint8_t *)dst, count * sizeof(double));
  108. #ifdef BIG_ENDIAN_ENABLED
  109. {
  110. uint64_t *dst = (uint64_t *)dst;
  111. for (size_t i = 0; i < count; i++) {
  112. dst[i] = BSWAP64(dst[i]);
  113. }
  114. }
  115. #endif
  116. } else if constexpr (sizeof(real_t) == 4) {
  117. // May be slower, but this is for compatibility. Eventually the data should be converted.
  118. for (size_t i = 0; i < count; ++i) {
  119. dst[i] = f->get_double();
  120. }
  121. } else {
  122. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!");
  123. }
  124. } else {
  125. if constexpr (sizeof(real_t) == 4) {
  126. // Ideal case with float-precision
  127. f->get_buffer((uint8_t *)dst, count * sizeof(float));
  128. #ifdef BIG_ENDIAN_ENABLED
  129. {
  130. uint32_t *dst = (uint32_t *)dst;
  131. for (size_t i = 0; i < count; i++) {
  132. dst[i] = BSWAP32(dst[i]);
  133. }
  134. }
  135. #endif
  136. } else if constexpr (sizeof(real_t) == 8) {
  137. for (size_t i = 0; i < count; ++i) {
  138. dst[i] = f->get_float();
  139. }
  140. } else {
  141. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!");
  142. }
  143. }
  144. return OK;
  145. }
  146. StringName ResourceLoaderBinary::_get_string() {
  147. uint32_t id = f->get_32();
  148. if (id & 0x80000000) {
  149. uint32_t len = id & 0x7FFFFFFF;
  150. if ((int)len > str_buf.size()) {
  151. str_buf.resize(len);
  152. }
  153. if (len == 0) {
  154. return StringName();
  155. }
  156. f->get_buffer((uint8_t *)&str_buf[0], len);
  157. String s;
  158. s.parse_utf8(&str_buf[0]);
  159. return s;
  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(String("Couldn't load resource (no cache): " + path).utf8().get_data());
  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);
  400. if (res.is_null()) {
  401. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  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, "Can't load dependency: " + 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. default: {
  592. ERR_FAIL_V(ERR_FILE_CORRUPT);
  593. } break;
  594. }
  595. return OK; //never reach anyway
  596. }
  597. Ref<Resource> ResourceLoaderBinary::get_resource() {
  598. return resource;
  599. }
  600. Error ResourceLoaderBinary::load() {
  601. if (error != OK) {
  602. return error;
  603. }
  604. for (int i = 0; i < external_resources.size(); i++) {
  605. String path = external_resources[i].path;
  606. if (remaps.has(path)) {
  607. path = remaps[path];
  608. }
  609. if (!path.contains("://") && path.is_relative_path()) {
  610. // path is relative to file being loaded, so convert to a resource path
  611. path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().path_join(external_resources[i].path));
  612. }
  613. 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
  614. 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, ResourceFormatLoader::CACHE_MODE_REUSE);
  615. if (!external_resources[i].load_token.is_valid()) {
  616. if (!ResourceLoader::get_abort_on_missing_resources()) {
  617. ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type);
  618. } else {
  619. error = ERR_FILE_MISSING_DEPENDENCIES;
  620. ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + ".");
  621. }
  622. }
  623. }
  624. for (int i = 0; i < internal_resources.size(); i++) {
  625. bool main = i == (internal_resources.size() - 1);
  626. //maybe it is loaded already
  627. String path;
  628. String id;
  629. if (!main) {
  630. path = internal_resources[i].path;
  631. if (path.begins_with("local://")) {
  632. path = path.replace_first("local://", "");
  633. id = path;
  634. path = res_path + "::" + path;
  635. internal_resources.write[i].path = path; // Update path.
  636. }
  637. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE && ResourceCache::has(path)) {
  638. Ref<Resource> cached = ResourceCache::get_ref(path);
  639. if (cached.is_valid()) {
  640. //already loaded, don't do anything
  641. error = OK;
  642. internal_index_cache[path] = cached;
  643. continue;
  644. }
  645. }
  646. } else {
  647. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE && !ResourceCache::has(res_path)) {
  648. path = res_path;
  649. }
  650. }
  651. uint64_t offset = internal_resources[i].offset;
  652. f->seek(offset);
  653. String t = get_unicode_string();
  654. Ref<Resource> res;
  655. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) {
  656. //use the existing one
  657. Ref<Resource> cached = ResourceCache::get_ref(path);
  658. if (cached->get_class() == t) {
  659. cached->reset_state();
  660. res = cached;
  661. }
  662. }
  663. MissingResource *missing_resource = nullptr;
  664. if (res.is_null()) {
  665. //did not replace
  666. Object *obj = ClassDB::instantiate(t);
  667. if (!obj) {
  668. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  669. //create a missing resource
  670. missing_resource = memnew(MissingResource);
  671. missing_resource->set_original_class(t);
  672. missing_resource->set_recording_properties(true);
  673. obj = missing_resource;
  674. } else {
  675. error = ERR_FILE_CORRUPT;
  676. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource of unrecognized type in file: " + t + ".");
  677. }
  678. }
  679. Resource *r = Object::cast_to<Resource>(obj);
  680. if (!r) {
  681. String obj_class = obj->get_class();
  682. error = ERR_FILE_CORRUPT;
  683. memdelete(obj); //bye
  684. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource type in resource field not a resource, type is: " + obj_class + ".");
  685. }
  686. res = Ref<Resource>(r);
  687. if (!path.is_empty() && cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  688. r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); //if got here because the resource with same path has different type, replace it
  689. }
  690. r->set_scene_unique_id(id);
  691. }
  692. if (!main) {
  693. internal_index_cache[path] = res;
  694. }
  695. int pc = f->get_32();
  696. //set properties
  697. Dictionary missing_resource_properties;
  698. for (int j = 0; j < pc; j++) {
  699. StringName name = _get_string();
  700. if (name == StringName()) {
  701. error = ERR_FILE_CORRUPT;
  702. ERR_FAIL_V(ERR_FILE_CORRUPT);
  703. }
  704. Variant value;
  705. error = parse_variant(value);
  706. if (error) {
  707. return error;
  708. }
  709. bool set_valid = true;
  710. if (value.get_type() == Variant::OBJECT && missing_resource != nullptr) {
  711. // If the property being set is a missing resource (and the parent is not),
  712. // then setting it will most likely not work.
  713. // Instead, save it as metadata.
  714. Ref<MissingResource> mr = value;
  715. if (mr.is_valid()) {
  716. missing_resource_properties[name] = mr;
  717. set_valid = false;
  718. }
  719. }
  720. if (value.get_type() == Variant::ARRAY) {
  721. Array set_array = value;
  722. bool is_get_valid = false;
  723. Variant get_value = res->get(name, &is_get_valid);
  724. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  725. Array get_array = get_value;
  726. if (!set_array.is_same_typed(get_array)) {
  727. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  728. }
  729. }
  730. }
  731. if (set_valid) {
  732. res->set(name, value);
  733. }
  734. }
  735. if (missing_resource) {
  736. missing_resource->set_recording_properties(false);
  737. }
  738. if (!missing_resource_properties.is_empty()) {
  739. res->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  740. }
  741. #ifdef TOOLS_ENABLED
  742. res->set_edited(false);
  743. #endif
  744. if (progress) {
  745. *progress = (i + 1) / float(internal_resources.size());
  746. }
  747. resource_cache.push_back(res);
  748. if (main) {
  749. f.unref();
  750. resource = res;
  751. resource->set_as_translation_remapped(translation_remapped);
  752. error = OK;
  753. return OK;
  754. }
  755. }
  756. return ERR_FILE_EOF;
  757. }
  758. void ResourceLoaderBinary::set_translation_remapped(bool p_remapped) {
  759. translation_remapped = p_remapped;
  760. }
  761. static void save_ustring(Ref<FileAccess> f, const String &p_string) {
  762. CharString utf8 = p_string.utf8();
  763. f->store_32(utf8.length() + 1);
  764. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  765. }
  766. static String get_ustring(Ref<FileAccess> f) {
  767. int len = f->get_32();
  768. Vector<char> str_buf;
  769. str_buf.resize(len);
  770. f->get_buffer((uint8_t *)&str_buf[0], len);
  771. String s;
  772. s.parse_utf8(&str_buf[0]);
  773. return s;
  774. }
  775. String ResourceLoaderBinary::get_unicode_string() {
  776. int len = f->get_32();
  777. if (len > str_buf.size()) {
  778. str_buf.resize(len);
  779. }
  780. if (len == 0) {
  781. return String();
  782. }
  783. f->get_buffer((uint8_t *)&str_buf[0], len);
  784. String s;
  785. s.parse_utf8(&str_buf[0]);
  786. return s;
  787. }
  788. void ResourceLoaderBinary::get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes) {
  789. open(p_f, false, true);
  790. if (error) {
  791. return;
  792. }
  793. for (int i = 0; i < internal_resources.size(); i++) {
  794. p_f->seek(internal_resources[i].offset);
  795. String t = get_unicode_string();
  796. ERR_FAIL_COND(p_f->get_error() != OK);
  797. if (t != String()) {
  798. p_classes->insert(t);
  799. }
  800. }
  801. }
  802. void ResourceLoaderBinary::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) {
  803. open(p_f, false, true);
  804. if (error) {
  805. return;
  806. }
  807. for (int i = 0; i < external_resources.size(); i++) {
  808. String dep;
  809. String fallback_path;
  810. if (external_resources[i].uid != ResourceUID::INVALID_ID) {
  811. dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid);
  812. fallback_path = external_resources[i].path; // Used by Dependency Editor, in case uid path fails.
  813. } else {
  814. dep = external_resources[i].path;
  815. }
  816. if (p_add_types && !external_resources[i].type.is_empty()) {
  817. dep += "::" + external_resources[i].type;
  818. }
  819. if (!fallback_path.is_empty()) {
  820. if (!p_add_types) {
  821. dep += "::"; // Ensure that path comes third, even if there is no type.
  822. }
  823. dep += "::" + fallback_path;
  824. }
  825. p_dependencies->push_back(dep);
  826. }
  827. }
  828. void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p_keep_uuid_paths) {
  829. error = OK;
  830. f = p_f;
  831. uint8_t header[4];
  832. f->get_buffer(header, 4);
  833. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  834. // Compressed.
  835. Ref<FileAccessCompressed> fac;
  836. fac.instantiate();
  837. error = fac->open_after_magic(f);
  838. if (error != OK) {
  839. f.unref();
  840. ERR_FAIL_MSG("Failed to open binary resource file: " + local_path + ".");
  841. }
  842. f = fac;
  843. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  844. // Not normal.
  845. error = ERR_FILE_UNRECOGNIZED;
  846. f.unref();
  847. ERR_FAIL_MSG("Unrecognized binary resource file: " + local_path + ".");
  848. }
  849. bool big_endian = f->get_32();
  850. bool use_real64 = f->get_32();
  851. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  852. uint32_t ver_major = f->get_32();
  853. uint32_t ver_minor = f->get_32();
  854. ver_format = f->get_32();
  855. print_bl("big endian: " + itos(big_endian));
  856. #ifdef BIG_ENDIAN_ENABLED
  857. print_bl("endian swap: " + itos(!big_endian));
  858. #else
  859. print_bl("endian swap: " + itos(big_endian));
  860. #endif
  861. print_bl("real64: " + itos(use_real64));
  862. print_bl("major: " + itos(ver_major));
  863. print_bl("minor: " + itos(ver_minor));
  864. print_bl("format: " + itos(ver_format));
  865. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  866. f.unref();
  867. 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).",
  868. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  869. }
  870. type = get_unicode_string();
  871. print_bl("type: " + type);
  872. importmd_ofs = f->get_64();
  873. uint32_t flags = f->get_32();
  874. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) {
  875. using_named_scene_ids = true;
  876. }
  877. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) {
  878. using_uids = true;
  879. }
  880. f->real_is_double = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_REAL_T_IS_DOUBLE) != 0;
  881. if (using_uids) {
  882. uid = f->get_64();
  883. } else {
  884. f->get_64(); // skip over uid field
  885. uid = ResourceUID::INVALID_ID;
  886. }
  887. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  888. script_class = get_unicode_string();
  889. }
  890. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  891. f->get_32(); //skip a few reserved fields
  892. }
  893. if (p_no_resources) {
  894. return;
  895. }
  896. uint32_t string_table_size = f->get_32();
  897. string_map.resize(string_table_size);
  898. for (uint32_t i = 0; i < string_table_size; i++) {
  899. StringName s = get_unicode_string();
  900. string_map.write[i] = s;
  901. }
  902. print_bl("strings: " + itos(string_table_size));
  903. uint32_t ext_resources_size = f->get_32();
  904. for (uint32_t i = 0; i < ext_resources_size; i++) {
  905. ExtResource er;
  906. er.type = get_unicode_string();
  907. er.path = get_unicode_string();
  908. if (using_uids) {
  909. er.uid = f->get_64();
  910. if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) {
  911. if (ResourceUID::get_singleton()->has_id(er.uid)) {
  912. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  913. er.path = ResourceUID::get_singleton()->get_id_path(er.uid);
  914. } else {
  915. #ifdef TOOLS_ENABLED
  916. // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated.
  917. if (ResourceLoader::get_resource_uid(res_path) != er.uid) {
  918. 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());
  919. }
  920. #else
  921. 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());
  922. #endif
  923. }
  924. }
  925. }
  926. external_resources.push_back(er);
  927. }
  928. print_bl("ext resources: " + itos(ext_resources_size));
  929. uint32_t int_resources_size = f->get_32();
  930. for (uint32_t i = 0; i < int_resources_size; i++) {
  931. IntResource ir;
  932. ir.path = get_unicode_string();
  933. ir.offset = f->get_64();
  934. internal_resources.push_back(ir);
  935. }
  936. print_bl("int resources: " + itos(int_resources_size));
  937. if (f->eof_reached()) {
  938. error = ERR_FILE_CORRUPT;
  939. f.unref();
  940. ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + ".");
  941. }
  942. }
  943. String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) {
  944. error = OK;
  945. f = p_f;
  946. uint8_t header[4];
  947. f->get_buffer(header, 4);
  948. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  949. // Compressed.
  950. Ref<FileAccessCompressed> fac;
  951. fac.instantiate();
  952. error = fac->open_after_magic(f);
  953. if (error != OK) {
  954. f.unref();
  955. return "";
  956. }
  957. f = fac;
  958. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  959. // Not normal.
  960. error = ERR_FILE_UNRECOGNIZED;
  961. f.unref();
  962. return "";
  963. }
  964. bool big_endian = f->get_32();
  965. f->get_32(); // use_real64
  966. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  967. uint32_t ver_major = f->get_32();
  968. f->get_32(); // ver_minor
  969. uint32_t ver_fmt = f->get_32();
  970. if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  971. f.unref();
  972. return "";
  973. }
  974. return get_unicode_string();
  975. }
  976. String ResourceLoaderBinary::recognize_script_class(Ref<FileAccess> p_f) {
  977. error = OK;
  978. f = p_f;
  979. uint8_t header[4];
  980. f->get_buffer(header, 4);
  981. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  982. // Compressed.
  983. Ref<FileAccessCompressed> fac;
  984. fac.instantiate();
  985. error = fac->open_after_magic(f);
  986. if (error != OK) {
  987. f.unref();
  988. return "";
  989. }
  990. f = fac;
  991. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  992. // Not normal.
  993. error = ERR_FILE_UNRECOGNIZED;
  994. f.unref();
  995. return "";
  996. }
  997. bool big_endian = f->get_32();
  998. f->get_32(); // use_real64
  999. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  1000. uint32_t ver_major = f->get_32();
  1001. f->get_32(); // ver_minor
  1002. uint32_t ver_fmt = f->get_32();
  1003. if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1004. f.unref();
  1005. return "";
  1006. }
  1007. get_unicode_string(); // type
  1008. f->get_64(); // Metadata offset
  1009. uint32_t flags = f->get_32();
  1010. f->get_64(); // UID
  1011. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  1012. return get_unicode_string();
  1013. } else {
  1014. return String();
  1015. }
  1016. }
  1017. 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) {
  1018. if (r_error) {
  1019. *r_error = ERR_FILE_CANT_OPEN;
  1020. }
  1021. Error err;
  1022. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  1023. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot open file '" + p_path + "'.");
  1024. ResourceLoaderBinary loader;
  1025. loader.cache_mode = p_cache_mode;
  1026. loader.use_sub_threads = p_use_sub_threads;
  1027. loader.progress = r_progress;
  1028. String path = !p_original_path.is_empty() ? p_original_path : p_path;
  1029. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  1030. loader.res_path = loader.local_path;
  1031. loader.open(f);
  1032. err = loader.load();
  1033. if (r_error) {
  1034. *r_error = err;
  1035. }
  1036. if (err) {
  1037. return Ref<Resource>();
  1038. }
  1039. return loader.resource;
  1040. }
  1041. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  1042. if (p_type.is_empty()) {
  1043. get_recognized_extensions(p_extensions);
  1044. return;
  1045. }
  1046. List<String> extensions;
  1047. ClassDB::get_extensions_for_type(p_type, &extensions);
  1048. extensions.sort();
  1049. for (const String &E : extensions) {
  1050. String ext = E.to_lower();
  1051. p_extensions->push_back(ext);
  1052. }
  1053. }
  1054. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  1055. List<String> extensions;
  1056. ClassDB::get_resource_base_extensions(&extensions);
  1057. extensions.sort();
  1058. for (const String &E : extensions) {
  1059. String ext = E.to_lower();
  1060. p_extensions->push_back(ext);
  1061. }
  1062. }
  1063. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  1064. return true; //handles all
  1065. }
  1066. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  1067. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1068. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'.");
  1069. ResourceLoaderBinary loader;
  1070. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1071. loader.res_path = loader.local_path;
  1072. loader.get_dependencies(f, p_dependencies, p_add_types);
  1073. }
  1074. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) {
  1075. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1076. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1077. Ref<FileAccess> fw;
  1078. String local_path = p_path.get_base_dir();
  1079. uint8_t header[4];
  1080. f->get_buffer(header, 4);
  1081. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  1082. // Compressed.
  1083. Ref<FileAccessCompressed> fac;
  1084. fac.instantiate();
  1085. Error err = fac->open_after_magic(f);
  1086. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  1087. f = fac;
  1088. Ref<FileAccessCompressed> facw;
  1089. facw.instantiate();
  1090. facw->configure("RSCC");
  1091. err = facw->open_internal(p_path + ".depren", FileAccess::WRITE);
  1092. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'.");
  1093. fw = facw;
  1094. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1095. // Not normal.
  1096. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'.");
  1097. } else {
  1098. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  1099. ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'.");
  1100. uint8_t magic[4] = { 'R', 'S', 'R', 'C' };
  1101. fw->store_buffer(magic, 4);
  1102. }
  1103. bool big_endian = f->get_32();
  1104. bool use_real64 = f->get_32();
  1105. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  1106. #ifdef BIG_ENDIAN_ENABLED
  1107. fw->store_32(!big_endian);
  1108. #else
  1109. fw->store_32(big_endian);
  1110. #endif
  1111. fw->set_big_endian(big_endian != 0);
  1112. fw->store_32(use_real64); //use real64
  1113. uint32_t ver_major = f->get_32();
  1114. uint32_t ver_minor = f->get_32();
  1115. uint32_t ver_format = f->get_32();
  1116. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  1117. fw.unref();
  1118. {
  1119. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1120. da->remove(p_path + ".depren");
  1121. }
  1122. // Use the old approach.
  1123. WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'.");
  1124. Error err;
  1125. f = FileAccess::open(p_path, FileAccess::READ, &err);
  1126. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1127. ResourceLoaderBinary loader;
  1128. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1129. loader.res_path = loader.local_path;
  1130. loader.remaps = p_map;
  1131. loader.open(f);
  1132. err = loader.load();
  1133. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  1134. Ref<Resource> res = loader.get_resource();
  1135. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  1136. return ResourceFormatSaverBinary::singleton->save(res, p_path);
  1137. }
  1138. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1139. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  1140. 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).",
  1141. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  1142. }
  1143. // Since we're not actually converting the file contents, leave the version
  1144. // numbers in the file untouched.
  1145. fw->store_32(ver_major);
  1146. fw->store_32(ver_minor);
  1147. fw->store_32(ver_format);
  1148. save_ustring(fw, get_ustring(f)); //type
  1149. uint64_t md_ofs = f->get_position();
  1150. uint64_t importmd_ofs = f->get_64();
  1151. fw->store_64(0); //metadata offset
  1152. uint32_t flags = f->get_32();
  1153. bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
  1154. uint64_t uid_data = f->get_64();
  1155. fw->store_32(flags);
  1156. fw->store_64(uid_data);
  1157. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  1158. save_ustring(fw, get_ustring(f));
  1159. }
  1160. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1161. fw->store_32(0); // reserved
  1162. f->get_32();
  1163. }
  1164. //string table
  1165. uint32_t string_table_size = f->get_32();
  1166. fw->store_32(string_table_size);
  1167. for (uint32_t i = 0; i < string_table_size; i++) {
  1168. String s = get_ustring(f);
  1169. save_ustring(fw, s);
  1170. }
  1171. //external resources
  1172. uint32_t ext_resources_size = f->get_32();
  1173. fw->store_32(ext_resources_size);
  1174. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1175. String type = get_ustring(f);
  1176. String path = get_ustring(f);
  1177. if (using_uids) {
  1178. ResourceUID::ID uid = f->get_64();
  1179. if (uid != ResourceUID::INVALID_ID) {
  1180. if (ResourceUID::get_singleton()->has_id(uid)) {
  1181. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  1182. path = ResourceUID::get_singleton()->get_id_path(uid);
  1183. }
  1184. }
  1185. }
  1186. bool relative = false;
  1187. if (!path.begins_with("res://")) {
  1188. path = local_path.path_join(path).simplify_path();
  1189. relative = true;
  1190. }
  1191. if (p_map.has(path)) {
  1192. String np = p_map[path];
  1193. path = np;
  1194. }
  1195. String full_path = path;
  1196. if (relative) {
  1197. //restore relative
  1198. path = local_path.path_to_file(path);
  1199. }
  1200. save_ustring(fw, type);
  1201. save_ustring(fw, path);
  1202. if (using_uids) {
  1203. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
  1204. fw->store_64(uid);
  1205. }
  1206. }
  1207. int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position();
  1208. //internal resources
  1209. uint32_t int_resources_size = f->get_32();
  1210. fw->store_32(int_resources_size);
  1211. for (uint32_t i = 0; i < int_resources_size; i++) {
  1212. String path = get_ustring(f);
  1213. uint64_t offset = f->get_64();
  1214. save_ustring(fw, path);
  1215. fw->store_64(offset + size_diff);
  1216. }
  1217. //rest of file
  1218. uint8_t b = f->get_8();
  1219. while (!f->eof_reached()) {
  1220. fw->store_8(b);
  1221. b = f->get_8();
  1222. }
  1223. f.unref();
  1224. bool all_ok = fw->get_error() == OK;
  1225. fw->seek(md_ofs);
  1226. fw->store_64(importmd_ofs + size_diff);
  1227. if (!all_ok) {
  1228. return ERR_CANT_CREATE;
  1229. }
  1230. fw.unref();
  1231. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1232. da->remove(p_path);
  1233. da->rename(p_path + ".depren", p_path);
  1234. return OK;
  1235. }
  1236. void ResourceFormatLoaderBinary::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  1237. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1238. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'.");
  1239. ResourceLoaderBinary loader;
  1240. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1241. loader.res_path = loader.local_path;
  1242. loader.get_classes_used(f, r_classes);
  1243. }
  1244. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1245. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1246. if (f.is_null()) {
  1247. return ""; //could not read
  1248. }
  1249. ResourceLoaderBinary loader;
  1250. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1251. loader.res_path = loader.local_path;
  1252. String r = loader.recognize(f);
  1253. return ClassDB::get_compatibility_remapped_class(r);
  1254. }
  1255. String ResourceFormatLoaderBinary::get_resource_script_class(const String &p_path) const {
  1256. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1257. if (f.is_null()) {
  1258. return ""; //could not read
  1259. }
  1260. ResourceLoaderBinary loader;
  1261. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1262. loader.res_path = loader.local_path;
  1263. return loader.recognize_script_class(f);
  1264. }
  1265. ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const {
  1266. String ext = p_path.get_extension().to_lower();
  1267. if (!ClassDB::is_resource_extension(ext)) {
  1268. return ResourceUID::INVALID_ID;
  1269. }
  1270. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1271. if (f.is_null()) {
  1272. return ResourceUID::INVALID_ID; //could not read
  1273. }
  1274. ResourceLoaderBinary loader;
  1275. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1276. loader.res_path = loader.local_path;
  1277. loader.open(f, true);
  1278. if (loader.error != OK) {
  1279. return ResourceUID::INVALID_ID; //could not read
  1280. }
  1281. return loader.uid;
  1282. }
  1283. ///////////////////////////////////////////////////////////
  1284. ///////////////////////////////////////////////////////////
  1285. ///////////////////////////////////////////////////////////
  1286. void ResourceFormatSaverBinaryInstance::_pad_buffer(Ref<FileAccess> f, int p_bytes) {
  1287. int extra = 4 - (p_bytes % 4);
  1288. if (extra < 4) {
  1289. for (int i = 0; i < extra; i++) {
  1290. f->store_8(0); //pad to 32
  1291. }
  1292. }
  1293. }
  1294. 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) {
  1295. switch (p_property.get_type()) {
  1296. case Variant::NIL: {
  1297. f->store_32(VARIANT_NIL);
  1298. // don't store anything
  1299. } break;
  1300. case Variant::BOOL: {
  1301. f->store_32(VARIANT_BOOL);
  1302. bool val = p_property;
  1303. f->store_32(val);
  1304. } break;
  1305. case Variant::INT: {
  1306. int64_t val = p_property;
  1307. if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) {
  1308. f->store_32(VARIANT_INT64);
  1309. f->store_64(val);
  1310. } else {
  1311. f->store_32(VARIANT_INT);
  1312. f->store_32(int32_t(p_property));
  1313. }
  1314. } break;
  1315. case Variant::FLOAT: {
  1316. double d = p_property;
  1317. float fl = d;
  1318. if (double(fl) != d) {
  1319. f->store_32(VARIANT_DOUBLE);
  1320. f->store_double(d);
  1321. } else {
  1322. f->store_32(VARIANT_FLOAT);
  1323. f->store_real(fl);
  1324. }
  1325. } break;
  1326. case Variant::STRING: {
  1327. f->store_32(VARIANT_STRING);
  1328. String val = p_property;
  1329. save_unicode_string(f, val);
  1330. } break;
  1331. case Variant::VECTOR2: {
  1332. f->store_32(VARIANT_VECTOR2);
  1333. Vector2 val = p_property;
  1334. f->store_real(val.x);
  1335. f->store_real(val.y);
  1336. } break;
  1337. case Variant::VECTOR2I: {
  1338. f->store_32(VARIANT_VECTOR2I);
  1339. Vector2i val = p_property;
  1340. f->store_32(val.x);
  1341. f->store_32(val.y);
  1342. } break;
  1343. case Variant::RECT2: {
  1344. f->store_32(VARIANT_RECT2);
  1345. Rect2 val = p_property;
  1346. f->store_real(val.position.x);
  1347. f->store_real(val.position.y);
  1348. f->store_real(val.size.x);
  1349. f->store_real(val.size.y);
  1350. } break;
  1351. case Variant::RECT2I: {
  1352. f->store_32(VARIANT_RECT2I);
  1353. Rect2i val = p_property;
  1354. f->store_32(val.position.x);
  1355. f->store_32(val.position.y);
  1356. f->store_32(val.size.x);
  1357. f->store_32(val.size.y);
  1358. } break;
  1359. case Variant::VECTOR3: {
  1360. f->store_32(VARIANT_VECTOR3);
  1361. Vector3 val = p_property;
  1362. f->store_real(val.x);
  1363. f->store_real(val.y);
  1364. f->store_real(val.z);
  1365. } break;
  1366. case Variant::VECTOR3I: {
  1367. f->store_32(VARIANT_VECTOR3I);
  1368. Vector3i val = p_property;
  1369. f->store_32(val.x);
  1370. f->store_32(val.y);
  1371. f->store_32(val.z);
  1372. } break;
  1373. case Variant::VECTOR4: {
  1374. f->store_32(VARIANT_VECTOR4);
  1375. Vector4 val = p_property;
  1376. f->store_real(val.x);
  1377. f->store_real(val.y);
  1378. f->store_real(val.z);
  1379. f->store_real(val.w);
  1380. } break;
  1381. case Variant::VECTOR4I: {
  1382. f->store_32(VARIANT_VECTOR4I);
  1383. Vector4i val = p_property;
  1384. f->store_32(val.x);
  1385. f->store_32(val.y);
  1386. f->store_32(val.z);
  1387. f->store_32(val.w);
  1388. } break;
  1389. case Variant::PLANE: {
  1390. f->store_32(VARIANT_PLANE);
  1391. Plane val = p_property;
  1392. f->store_real(val.normal.x);
  1393. f->store_real(val.normal.y);
  1394. f->store_real(val.normal.z);
  1395. f->store_real(val.d);
  1396. } break;
  1397. case Variant::QUATERNION: {
  1398. f->store_32(VARIANT_QUATERNION);
  1399. Quaternion val = p_property;
  1400. f->store_real(val.x);
  1401. f->store_real(val.y);
  1402. f->store_real(val.z);
  1403. f->store_real(val.w);
  1404. } break;
  1405. case Variant::AABB: {
  1406. f->store_32(VARIANT_AABB);
  1407. AABB val = p_property;
  1408. f->store_real(val.position.x);
  1409. f->store_real(val.position.y);
  1410. f->store_real(val.position.z);
  1411. f->store_real(val.size.x);
  1412. f->store_real(val.size.y);
  1413. f->store_real(val.size.z);
  1414. } break;
  1415. case Variant::TRANSFORM2D: {
  1416. f->store_32(VARIANT_TRANSFORM2D);
  1417. Transform2D val = p_property;
  1418. f->store_real(val.columns[0].x);
  1419. f->store_real(val.columns[0].y);
  1420. f->store_real(val.columns[1].x);
  1421. f->store_real(val.columns[1].y);
  1422. f->store_real(val.columns[2].x);
  1423. f->store_real(val.columns[2].y);
  1424. } break;
  1425. case Variant::BASIS: {
  1426. f->store_32(VARIANT_BASIS);
  1427. Basis val = p_property;
  1428. f->store_real(val.rows[0].x);
  1429. f->store_real(val.rows[0].y);
  1430. f->store_real(val.rows[0].z);
  1431. f->store_real(val.rows[1].x);
  1432. f->store_real(val.rows[1].y);
  1433. f->store_real(val.rows[1].z);
  1434. f->store_real(val.rows[2].x);
  1435. f->store_real(val.rows[2].y);
  1436. f->store_real(val.rows[2].z);
  1437. } break;
  1438. case Variant::TRANSFORM3D: {
  1439. f->store_32(VARIANT_TRANSFORM3D);
  1440. Transform3D val = p_property;
  1441. f->store_real(val.basis.rows[0].x);
  1442. f->store_real(val.basis.rows[0].y);
  1443. f->store_real(val.basis.rows[0].z);
  1444. f->store_real(val.basis.rows[1].x);
  1445. f->store_real(val.basis.rows[1].y);
  1446. f->store_real(val.basis.rows[1].z);
  1447. f->store_real(val.basis.rows[2].x);
  1448. f->store_real(val.basis.rows[2].y);
  1449. f->store_real(val.basis.rows[2].z);
  1450. f->store_real(val.origin.x);
  1451. f->store_real(val.origin.y);
  1452. f->store_real(val.origin.z);
  1453. } break;
  1454. case Variant::PROJECTION: {
  1455. f->store_32(VARIANT_PROJECTION);
  1456. Projection val = p_property;
  1457. f->store_real(val.columns[0].x);
  1458. f->store_real(val.columns[0].y);
  1459. f->store_real(val.columns[0].z);
  1460. f->store_real(val.columns[0].w);
  1461. f->store_real(val.columns[1].x);
  1462. f->store_real(val.columns[1].y);
  1463. f->store_real(val.columns[1].z);
  1464. f->store_real(val.columns[1].w);
  1465. f->store_real(val.columns[2].x);
  1466. f->store_real(val.columns[2].y);
  1467. f->store_real(val.columns[2].z);
  1468. f->store_real(val.columns[2].w);
  1469. f->store_real(val.columns[3].x);
  1470. f->store_real(val.columns[3].y);
  1471. f->store_real(val.columns[3].z);
  1472. f->store_real(val.columns[3].w);
  1473. } break;
  1474. case Variant::COLOR: {
  1475. f->store_32(VARIANT_COLOR);
  1476. Color val = p_property;
  1477. // Color are always floats
  1478. f->store_float(val.r);
  1479. f->store_float(val.g);
  1480. f->store_float(val.b);
  1481. f->store_float(val.a);
  1482. } break;
  1483. case Variant::STRING_NAME: {
  1484. f->store_32(VARIANT_STRING_NAME);
  1485. String val = p_property;
  1486. save_unicode_string(f, val);
  1487. } break;
  1488. case Variant::NODE_PATH: {
  1489. f->store_32(VARIANT_NODE_PATH);
  1490. NodePath np = p_property;
  1491. f->store_16(np.get_name_count());
  1492. uint16_t snc = np.get_subname_count();
  1493. if (np.is_absolute()) {
  1494. snc |= 0x8000;
  1495. }
  1496. f->store_16(snc);
  1497. for (int i = 0; i < np.get_name_count(); i++) {
  1498. if (string_map.has(np.get_name(i))) {
  1499. f->store_32(string_map[np.get_name(i)]);
  1500. } else {
  1501. save_unicode_string(f, np.get_name(i), true);
  1502. }
  1503. }
  1504. for (int i = 0; i < np.get_subname_count(); i++) {
  1505. if (string_map.has(np.get_subname(i))) {
  1506. f->store_32(string_map[np.get_subname(i)]);
  1507. } else {
  1508. save_unicode_string(f, np.get_subname(i), true);
  1509. }
  1510. }
  1511. } break;
  1512. case Variant::RID: {
  1513. f->store_32(VARIANT_RID);
  1514. WARN_PRINT("Can't save RIDs.");
  1515. RID val = p_property;
  1516. f->store_32(val.get_id());
  1517. } break;
  1518. case Variant::OBJECT: {
  1519. f->store_32(VARIANT_OBJECT);
  1520. Ref<Resource> res = p_property;
  1521. if (res.is_null()) {
  1522. f->store_32(OBJECT_EMPTY);
  1523. return; // don't save it
  1524. }
  1525. if (!res->is_built_in()) {
  1526. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1527. f->store_32(external_resources[res]);
  1528. } else {
  1529. if (!resource_map.has(res)) {
  1530. f->store_32(OBJECT_EMPTY);
  1531. ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference.");
  1532. }
  1533. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1534. f->store_32(resource_map[res]);
  1535. //internal resource
  1536. }
  1537. } break;
  1538. case Variant::CALLABLE: {
  1539. f->store_32(VARIANT_CALLABLE);
  1540. WARN_PRINT("Can't save Callables.");
  1541. } break;
  1542. case Variant::SIGNAL: {
  1543. f->store_32(VARIANT_SIGNAL);
  1544. WARN_PRINT("Can't save Signals.");
  1545. } break;
  1546. case Variant::DICTIONARY: {
  1547. f->store_32(VARIANT_DICTIONARY);
  1548. Dictionary d = p_property;
  1549. f->store_32(uint32_t(d.size()));
  1550. List<Variant> keys;
  1551. d.get_key_list(&keys);
  1552. for (const Variant &E : keys) {
  1553. write_variant(f, E, resource_map, external_resources, string_map);
  1554. write_variant(f, d[E], resource_map, external_resources, string_map);
  1555. }
  1556. } break;
  1557. case Variant::ARRAY: {
  1558. f->store_32(VARIANT_ARRAY);
  1559. Array a = p_property;
  1560. f->store_32(uint32_t(a.size()));
  1561. for (int i = 0; i < a.size(); i++) {
  1562. write_variant(f, a[i], resource_map, external_resources, string_map);
  1563. }
  1564. } break;
  1565. case Variant::PACKED_BYTE_ARRAY: {
  1566. f->store_32(VARIANT_PACKED_BYTE_ARRAY);
  1567. Vector<uint8_t> arr = p_property;
  1568. int len = arr.size();
  1569. f->store_32(len);
  1570. const uint8_t *r = arr.ptr();
  1571. f->store_buffer(r, len);
  1572. _pad_buffer(f, len);
  1573. } break;
  1574. case Variant::PACKED_INT32_ARRAY: {
  1575. f->store_32(VARIANT_PACKED_INT32_ARRAY);
  1576. Vector<int32_t> arr = p_property;
  1577. int len = arr.size();
  1578. f->store_32(len);
  1579. const int32_t *r = arr.ptr();
  1580. for (int i = 0; i < len; i++) {
  1581. f->store_32(r[i]);
  1582. }
  1583. } break;
  1584. case Variant::PACKED_INT64_ARRAY: {
  1585. f->store_32(VARIANT_PACKED_INT64_ARRAY);
  1586. Vector<int64_t> arr = p_property;
  1587. int len = arr.size();
  1588. f->store_32(len);
  1589. const int64_t *r = arr.ptr();
  1590. for (int i = 0; i < len; i++) {
  1591. f->store_64(r[i]);
  1592. }
  1593. } break;
  1594. case Variant::PACKED_FLOAT32_ARRAY: {
  1595. f->store_32(VARIANT_PACKED_FLOAT32_ARRAY);
  1596. Vector<float> arr = p_property;
  1597. int len = arr.size();
  1598. f->store_32(len);
  1599. const float *r = arr.ptr();
  1600. for (int i = 0; i < len; i++) {
  1601. f->store_float(r[i]);
  1602. }
  1603. } break;
  1604. case Variant::PACKED_FLOAT64_ARRAY: {
  1605. f->store_32(VARIANT_PACKED_FLOAT64_ARRAY);
  1606. Vector<double> arr = p_property;
  1607. int len = arr.size();
  1608. f->store_32(len);
  1609. const double *r = arr.ptr();
  1610. for (int i = 0; i < len; i++) {
  1611. f->store_double(r[i]);
  1612. }
  1613. } break;
  1614. case Variant::PACKED_STRING_ARRAY: {
  1615. f->store_32(VARIANT_PACKED_STRING_ARRAY);
  1616. Vector<String> arr = p_property;
  1617. int len = arr.size();
  1618. f->store_32(len);
  1619. const String *r = arr.ptr();
  1620. for (int i = 0; i < len; i++) {
  1621. save_unicode_string(f, r[i]);
  1622. }
  1623. } break;
  1624. case Variant::PACKED_VECTOR3_ARRAY: {
  1625. f->store_32(VARIANT_PACKED_VECTOR3_ARRAY);
  1626. Vector<Vector3> arr = p_property;
  1627. int len = arr.size();
  1628. f->store_32(len);
  1629. const Vector3 *r = arr.ptr();
  1630. for (int i = 0; i < len; i++) {
  1631. f->store_real(r[i].x);
  1632. f->store_real(r[i].y);
  1633. f->store_real(r[i].z);
  1634. }
  1635. } break;
  1636. case Variant::PACKED_VECTOR2_ARRAY: {
  1637. f->store_32(VARIANT_PACKED_VECTOR2_ARRAY);
  1638. Vector<Vector2> arr = p_property;
  1639. int len = arr.size();
  1640. f->store_32(len);
  1641. const Vector2 *r = arr.ptr();
  1642. for (int i = 0; i < len; i++) {
  1643. f->store_real(r[i].x);
  1644. f->store_real(r[i].y);
  1645. }
  1646. } break;
  1647. case Variant::PACKED_COLOR_ARRAY: {
  1648. f->store_32(VARIANT_PACKED_COLOR_ARRAY);
  1649. Vector<Color> arr = p_property;
  1650. int len = arr.size();
  1651. f->store_32(len);
  1652. const Color *r = arr.ptr();
  1653. for (int i = 0; i < len; i++) {
  1654. f->store_float(r[i].r);
  1655. f->store_float(r[i].g);
  1656. f->store_float(r[i].b);
  1657. f->store_float(r[i].a);
  1658. }
  1659. } break;
  1660. default: {
  1661. ERR_FAIL_MSG("Invalid variant.");
  1662. }
  1663. }
  1664. }
  1665. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1666. switch (p_variant.get_type()) {
  1667. case Variant::OBJECT: {
  1668. Ref<Resource> res = p_variant;
  1669. if (res.is_null() || external_resources.has(res)) {
  1670. return;
  1671. }
  1672. if (!p_main && (!bundle_resources) && !res->is_built_in()) {
  1673. if (res->get_path() == path) {
  1674. ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
  1675. return;
  1676. }
  1677. int idx = external_resources.size();
  1678. external_resources[res] = idx;
  1679. return;
  1680. }
  1681. if (resource_set.has(res)) {
  1682. return;
  1683. }
  1684. List<PropertyInfo> property_list;
  1685. res->get_property_list(&property_list);
  1686. for (const PropertyInfo &E : property_list) {
  1687. if (E.usage & PROPERTY_USAGE_STORAGE) {
  1688. Variant value = res->get(E.name);
  1689. if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1690. Ref<Resource> sres = value;
  1691. if (sres.is_valid()) {
  1692. NonPersistentKey npk;
  1693. npk.base = res;
  1694. npk.property = E.name;
  1695. non_persistent_map[npk] = sres;
  1696. resource_set.insert(sres);
  1697. saved_resources.push_back(sres);
  1698. }
  1699. } else {
  1700. _find_resources(value);
  1701. }
  1702. }
  1703. }
  1704. resource_set.insert(res);
  1705. saved_resources.push_back(res);
  1706. } break;
  1707. case Variant::ARRAY: {
  1708. Array varray = p_variant;
  1709. int len = varray.size();
  1710. for (int i = 0; i < len; i++) {
  1711. const Variant &v = varray.get(i);
  1712. _find_resources(v);
  1713. }
  1714. } break;
  1715. case Variant::DICTIONARY: {
  1716. Dictionary d = p_variant;
  1717. List<Variant> keys;
  1718. d.get_key_list(&keys);
  1719. for (const Variant &E : keys) {
  1720. _find_resources(E);
  1721. Variant v = d[E];
  1722. _find_resources(v);
  1723. }
  1724. } break;
  1725. case Variant::NODE_PATH: {
  1726. //take the chance and save node path strings
  1727. NodePath np = p_variant;
  1728. for (int i = 0; i < np.get_name_count(); i++) {
  1729. get_string_index(np.get_name(i));
  1730. }
  1731. for (int i = 0; i < np.get_subname_count(); i++) {
  1732. get_string_index(np.get_subname(i));
  1733. }
  1734. } break;
  1735. default: {
  1736. }
  1737. }
  1738. }
  1739. void ResourceFormatSaverBinaryInstance::save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len) {
  1740. CharString utf8 = p_string.utf8();
  1741. if (p_bit_on_len) {
  1742. p_f->store_32((utf8.length() + 1) | 0x80000000);
  1743. } else {
  1744. p_f->store_32(utf8.length() + 1);
  1745. }
  1746. p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1747. }
  1748. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1749. StringName s = p_string;
  1750. if (string_map.has(s)) {
  1751. return string_map[s];
  1752. }
  1753. string_map[s] = strings.size();
  1754. strings.push_back(s);
  1755. return strings.size() - 1;
  1756. }
  1757. static String _resource_get_class(Ref<Resource> p_resource) {
  1758. Ref<MissingResource> missing_resource = p_resource;
  1759. if (missing_resource.is_valid()) {
  1760. return missing_resource->get_original_class();
  1761. } else {
  1762. return p_resource->get_class();
  1763. }
  1764. }
  1765. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) {
  1766. Error err;
  1767. Ref<FileAccess> f;
  1768. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1769. Ref<FileAccessCompressed> fac;
  1770. fac.instantiate();
  1771. fac->configure("RSCC");
  1772. f = fac;
  1773. err = fac->open_internal(p_path, FileAccess::WRITE);
  1774. } else {
  1775. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1776. }
  1777. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'.");
  1778. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1779. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1780. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1781. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1782. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1783. if (!p_path.begins_with("res://")) {
  1784. takeover_paths = false;
  1785. }
  1786. local_path = p_path.get_base_dir();
  1787. path = ProjectSettings::get_singleton()->localize_path(p_path);
  1788. _find_resources(p_resource, true);
  1789. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1790. //save header compressed
  1791. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1792. f->store_buffer(header, 4);
  1793. }
  1794. if (big_endian) {
  1795. f->store_32(1);
  1796. f->set_big_endian(true);
  1797. } else {
  1798. f->store_32(0);
  1799. }
  1800. f->store_32(0); //64 bits file, false for now
  1801. f->store_32(VERSION_MAJOR);
  1802. f->store_32(VERSION_MINOR);
  1803. f->store_32(FORMAT_VERSION);
  1804. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1805. return ERR_CANT_CREATE;
  1806. }
  1807. save_unicode_string(f, _resource_get_class(p_resource));
  1808. f->store_64(0); //offset to import metadata
  1809. String script_class;
  1810. {
  1811. uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS;
  1812. #ifdef REAL_T_IS_DOUBLE
  1813. format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE;
  1814. #endif
  1815. if (!p_resource->is_class("PackedScene")) {
  1816. Ref<Script> s = p_resource->get_script();
  1817. if (s.is_valid()) {
  1818. script_class = s->get_global_name();
  1819. if (!script_class.is_empty()) {
  1820. format_flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS;
  1821. }
  1822. }
  1823. }
  1824. f->store_32(format_flags);
  1825. }
  1826. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true);
  1827. f->store_64(uid);
  1828. if (!script_class.is_empty()) {
  1829. save_unicode_string(f, script_class);
  1830. }
  1831. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1832. f->store_32(0); // reserved
  1833. }
  1834. List<ResourceData> resources;
  1835. Dictionary missing_resource_properties = p_resource->get_meta(META_MISSING_RESOURCES, Dictionary());
  1836. {
  1837. for (const Ref<Resource> &E : saved_resources) {
  1838. ResourceData &rd = resources.push_back(ResourceData())->get();
  1839. rd.type = _resource_get_class(E);
  1840. List<PropertyInfo> property_list;
  1841. E->get_property_list(&property_list);
  1842. for (const PropertyInfo &F : property_list) {
  1843. if (skip_editor && F.name.begins_with("__editor")) {
  1844. continue;
  1845. }
  1846. if (F.name == META_PROPERTY_MISSING_RESOURCES) {
  1847. continue;
  1848. }
  1849. if ((F.usage & PROPERTY_USAGE_STORAGE)) {
  1850. Property p;
  1851. p.name_idx = get_string_index(F.name);
  1852. if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1853. NonPersistentKey npk;
  1854. npk.base = E;
  1855. npk.property = F.name;
  1856. if (non_persistent_map.has(npk)) {
  1857. p.value = non_persistent_map[npk];
  1858. }
  1859. } else {
  1860. p.value = E->get(F.name);
  1861. }
  1862. if (p.pi.type == Variant::OBJECT && missing_resource_properties.has(F.name)) {
  1863. // Was this missing resource overridden? If so do not save the old value.
  1864. Ref<Resource> res = p.value;
  1865. if (res.is_null()) {
  1866. p.value = missing_resource_properties[F.name];
  1867. }
  1868. }
  1869. Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name);
  1870. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) {
  1871. continue;
  1872. }
  1873. p.pi = F;
  1874. rd.properties.push_back(p);
  1875. }
  1876. }
  1877. }
  1878. }
  1879. f->store_32(strings.size()); //string table size
  1880. for (int i = 0; i < strings.size(); i++) {
  1881. save_unicode_string(f, strings[i]);
  1882. }
  1883. // save external resource table
  1884. f->store_32(external_resources.size()); //amount of external resources
  1885. Vector<Ref<Resource>> save_order;
  1886. save_order.resize(external_resources.size());
  1887. for (const KeyValue<Ref<Resource>, int> &E : external_resources) {
  1888. save_order.write[E.value] = E.key;
  1889. }
  1890. for (int i = 0; i < save_order.size(); i++) {
  1891. save_unicode_string(f, save_order[i]->get_save_class());
  1892. String res_path = save_order[i]->get_path();
  1893. res_path = relative_paths ? local_path.path_to_file(res_path) : res_path;
  1894. save_unicode_string(f, res_path);
  1895. ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false);
  1896. f->store_64(ruid);
  1897. }
  1898. // save internal resource table
  1899. f->store_32(saved_resources.size()); //amount of internal resources
  1900. Vector<uint64_t> ofs_pos;
  1901. HashSet<String> used_unique_ids;
  1902. for (Ref<Resource> &r : saved_resources) {
  1903. if (r->is_built_in()) {
  1904. if (!r->get_scene_unique_id().is_empty()) {
  1905. if (used_unique_ids.has(r->get_scene_unique_id())) {
  1906. r->set_scene_unique_id("");
  1907. } else {
  1908. used_unique_ids.insert(r->get_scene_unique_id());
  1909. }
  1910. }
  1911. }
  1912. }
  1913. HashMap<Ref<Resource>, int> resource_map;
  1914. int res_index = 0;
  1915. for (Ref<Resource> &r : saved_resources) {
  1916. if (r->is_built_in()) {
  1917. if (r->get_scene_unique_id().is_empty()) {
  1918. String new_id;
  1919. while (true) {
  1920. new_id = _resource_get_class(r) + "_" + Resource::generate_scene_unique_id();
  1921. if (!used_unique_ids.has(new_id)) {
  1922. break;
  1923. }
  1924. }
  1925. r->set_scene_unique_id(new_id);
  1926. used_unique_ids.insert(new_id);
  1927. }
  1928. save_unicode_string(f, "local://" + r->get_scene_unique_id());
  1929. if (takeover_paths) {
  1930. r->set_path(p_path + "::" + r->get_scene_unique_id(), true);
  1931. }
  1932. #ifdef TOOLS_ENABLED
  1933. r->set_edited(false);
  1934. #endif
  1935. } else {
  1936. save_unicode_string(f, r->get_path()); //actual external
  1937. }
  1938. ofs_pos.push_back(f->get_position());
  1939. f->store_64(0); //offset in 64 bits
  1940. resource_map[r] = res_index++;
  1941. }
  1942. Vector<uint64_t> ofs_table;
  1943. //now actually save the resources
  1944. for (const ResourceData &rd : resources) {
  1945. ofs_table.push_back(f->get_position());
  1946. save_unicode_string(f, rd.type);
  1947. f->store_32(rd.properties.size());
  1948. for (const Property &p : rd.properties) {
  1949. f->store_32(p.name_idx);
  1950. write_variant(f, p.value, resource_map, external_resources, string_map, p.pi);
  1951. }
  1952. }
  1953. for (int i = 0; i < ofs_table.size(); i++) {
  1954. f->seek(ofs_pos[i]);
  1955. f->store_64(ofs_table[i]);
  1956. }
  1957. f->seek_end();
  1958. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1959. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1960. return ERR_CANT_CREATE;
  1961. }
  1962. return OK;
  1963. }
  1964. Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  1965. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1966. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1967. Ref<FileAccess> fw;
  1968. local_path = p_path.get_base_dir();
  1969. uint8_t header[4];
  1970. f->get_buffer(header, 4);
  1971. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  1972. // Compressed.
  1973. Ref<FileAccessCompressed> fac;
  1974. fac.instantiate();
  1975. Error err = fac->open_after_magic(f);
  1976. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  1977. f = fac;
  1978. Ref<FileAccessCompressed> facw;
  1979. facw.instantiate();
  1980. facw->configure("RSCC");
  1981. err = facw->open_internal(p_path + ".uidren", FileAccess::WRITE);
  1982. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".uidren'.");
  1983. fw = facw;
  1984. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1985. // Not a binary resource.
  1986. return ERR_FILE_UNRECOGNIZED;
  1987. } else {
  1988. fw = FileAccess::open(p_path + ".uidren", FileAccess::WRITE);
  1989. ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".uidren'.");
  1990. uint8_t magich[4] = { 'R', 'S', 'R', 'C' };
  1991. fw->store_buffer(magich, 4);
  1992. }
  1993. big_endian = f->get_32();
  1994. bool use_real64 = f->get_32();
  1995. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  1996. #ifdef BIG_ENDIAN_ENABLED
  1997. fw->store_32(!big_endian);
  1998. #else
  1999. fw->store_32(big_endian);
  2000. #endif
  2001. fw->set_big_endian(big_endian != 0);
  2002. fw->store_32(use_real64); //use real64
  2003. uint32_t ver_major = f->get_32();
  2004. uint32_t ver_minor = f->get_32();
  2005. uint32_t ver_format = f->get_32();
  2006. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  2007. fw.unref();
  2008. {
  2009. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2010. da->remove(p_path + ".uidren");
  2011. }
  2012. // Use the old approach.
  2013. WARN_PRINT("This file is old, so it does not support UIDs, opening and resaving '" + p_path + "'.");
  2014. return ERR_UNAVAILABLE;
  2015. }
  2016. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  2017. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  2018. 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).",
  2019. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  2020. }
  2021. // Since we're not actually converting the file contents, leave the version
  2022. // numbers in the file untouched.
  2023. fw->store_32(ver_major);
  2024. fw->store_32(ver_minor);
  2025. fw->store_32(ver_format);
  2026. save_ustring(fw, get_ustring(f)); //type
  2027. fw->store_64(f->get_64()); //metadata offset
  2028. uint32_t flags = f->get_32();
  2029. flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS;
  2030. f->get_64(); // Skip previous UID
  2031. fw->store_32(flags);
  2032. fw->store_64(p_uid);
  2033. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  2034. save_ustring(fw, get_ustring(f));
  2035. }
  2036. //rest of file
  2037. uint8_t b = f->get_8();
  2038. while (!f->eof_reached()) {
  2039. fw->store_8(b);
  2040. b = f->get_8();
  2041. }
  2042. f.unref();
  2043. bool all_ok = fw->get_error() == OK;
  2044. if (!all_ok) {
  2045. return ERR_CANT_CREATE;
  2046. }
  2047. fw.unref();
  2048. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2049. da->remove(p_path);
  2050. da->rename(p_path + ".uidren", p_path);
  2051. return OK;
  2052. }
  2053. Error ResourceFormatSaverBinary::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  2054. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  2055. ResourceFormatSaverBinaryInstance saver;
  2056. return saver.save(local_path, p_resource, p_flags);
  2057. }
  2058. Error ResourceFormatSaverBinary::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  2059. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  2060. ResourceFormatSaverBinaryInstance saver;
  2061. return saver.set_uid(local_path, p_uid);
  2062. }
  2063. bool ResourceFormatSaverBinary::recognize(const Ref<Resource> &p_resource) const {
  2064. return true; //all recognized
  2065. }
  2066. void ResourceFormatSaverBinary::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  2067. String base = p_resource->get_base_extension().to_lower();
  2068. p_extensions->push_back(base);
  2069. if (base != "res") {
  2070. p_extensions->push_back("res");
  2071. }
  2072. }
  2073. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr;
  2074. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  2075. singleton = this;
  2076. }