resource_format_binary.cpp 55 KB

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