resource_format_binary.cpp 57 KB

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