resource_format_binary.cpp 60 KB

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