resource_format_binary.cpp 66 KB

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