resource_format_binary.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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, 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->close();
  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(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(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(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(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. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  751. error = fac->open_after_magic(f);
  752. if (error != OK) {
  753. memdelete(fac);
  754. f->close();
  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->close();
  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->close();
  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. f->close();
  845. ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + ".");
  846. }
  847. }
  848. String ResourceLoaderBinary::recognize(FileAccess *p_f) {
  849. error = OK;
  850. f = p_f;
  851. uint8_t header[4];
  852. f->get_buffer(header, 4);
  853. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  854. // Compressed.
  855. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  856. error = fac->open_after_magic(f);
  857. if (error != OK) {
  858. memdelete(fac);
  859. f->close();
  860. return "";
  861. }
  862. f = fac;
  863. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  864. // Not normal.
  865. error = ERR_FILE_UNRECOGNIZED;
  866. f->close();
  867. return "";
  868. }
  869. bool big_endian = f->get_32();
  870. f->get_32(); // use_real64
  871. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  872. uint32_t ver_major = f->get_32();
  873. f->get_32(); // ver_minor
  874. uint32_t ver_format = f->get_32();
  875. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  876. f->close();
  877. return "";
  878. }
  879. String type = get_unicode_string();
  880. return type;
  881. }
  882. ResourceLoaderBinary::~ResourceLoaderBinary() {
  883. if (f) {
  884. memdelete(f);
  885. }
  886. }
  887. 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) {
  888. if (r_error) {
  889. *r_error = ERR_FILE_CANT_OPEN;
  890. }
  891. Error err;
  892. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  893. ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot open file '" + p_path + "'.");
  894. ResourceLoaderBinary loader;
  895. loader.cache_mode = p_cache_mode;
  896. loader.use_sub_threads = p_use_sub_threads;
  897. loader.progress = r_progress;
  898. String path = !p_original_path.is_empty() ? p_original_path : p_path;
  899. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  900. loader.res_path = loader.local_path;
  901. loader.open(f);
  902. err = loader.load();
  903. if (r_error) {
  904. *r_error = err;
  905. }
  906. if (err) {
  907. return RES();
  908. }
  909. return loader.resource;
  910. }
  911. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  912. if (p_type.is_empty()) {
  913. get_recognized_extensions(p_extensions);
  914. return;
  915. }
  916. List<String> extensions;
  917. ClassDB::get_extensions_for_type(p_type, &extensions);
  918. extensions.sort();
  919. for (const String &E : extensions) {
  920. String ext = E.to_lower();
  921. p_extensions->push_back(ext);
  922. }
  923. }
  924. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  925. List<String> extensions;
  926. ClassDB::get_resource_base_extensions(&extensions);
  927. extensions.sort();
  928. for (const String &E : extensions) {
  929. String ext = E.to_lower();
  930. p_extensions->push_back(ext);
  931. }
  932. }
  933. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  934. return true; //handles all
  935. }
  936. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  937. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  938. ERR_FAIL_COND_MSG(!f, "Cannot open file '" + p_path + "'.");
  939. ResourceLoaderBinary loader;
  940. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  941. loader.res_path = loader.local_path;
  942. loader.get_dependencies(f, p_dependencies, p_add_types);
  943. }
  944. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  945. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  946. ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  947. FileAccess *fw = nullptr;
  948. String local_path = p_path.get_base_dir();
  949. uint8_t header[4];
  950. f->get_buffer(header, 4);
  951. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  952. // Compressed.
  953. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  954. Error err = fac->open_after_magic(f);
  955. if (err != OK) {
  956. memdelete(fac);
  957. memdelete(f);
  958. ERR_FAIL_V_MSG(err, "Cannot open file '" + p_path + "'.");
  959. }
  960. f = fac;
  961. FileAccessCompressed *facw = memnew(FileAccessCompressed);
  962. facw->configure("RSCC");
  963. err = facw->_open(p_path + ".depren", FileAccess::WRITE);
  964. if (err) {
  965. memdelete(fac);
  966. memdelete(facw);
  967. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'.");
  968. }
  969. fw = facw;
  970. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  971. // Not normal.
  972. memdelete(f);
  973. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'.");
  974. } else {
  975. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  976. if (!fw) {
  977. memdelete(f);
  978. }
  979. ERR_FAIL_COND_V_MSG(!fw, ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'.");
  980. uint8_t magic[4] = { 'R', 'S', 'R', 'C' };
  981. fw->store_buffer(magic, 4);
  982. }
  983. bool big_endian = f->get_32();
  984. bool use_real64 = f->get_32();
  985. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  986. #ifdef BIG_ENDIAN_ENABLED
  987. fw->store_32(!big_endian);
  988. #else
  989. fw->store_32(big_endian);
  990. #endif
  991. fw->set_big_endian(big_endian != 0);
  992. fw->store_32(use_real64); //use real64
  993. uint32_t ver_major = f->get_32();
  994. uint32_t ver_minor = f->get_32();
  995. uint32_t ver_format = f->get_32();
  996. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  997. memdelete(f);
  998. memdelete(fw);
  999. {
  1000. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1001. da->remove(p_path + ".depren");
  1002. }
  1003. // Use the old approach.
  1004. WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'.");
  1005. Error err;
  1006. f = FileAccess::open(p_path, FileAccess::READ, &err);
  1007. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1008. ResourceLoaderBinary loader;
  1009. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1010. loader.res_path = loader.local_path;
  1011. loader.remaps = p_map;
  1012. loader.open(f);
  1013. err = loader.load();
  1014. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  1015. RES res = loader.get_resource();
  1016. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  1017. return ResourceFormatSaverBinary::singleton->save(p_path, res);
  1018. }
  1019. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1020. memdelete(f);
  1021. memdelete(fw);
  1022. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  1023. 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).",
  1024. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  1025. }
  1026. // Since we're not actually converting the file contents, leave the version
  1027. // numbers in the file untouched.
  1028. fw->store_32(ver_major);
  1029. fw->store_32(ver_minor);
  1030. fw->store_32(ver_format);
  1031. save_ustring(fw, get_ustring(f)); //type
  1032. uint64_t md_ofs = f->get_position();
  1033. uint64_t importmd_ofs = f->get_64();
  1034. fw->store_64(0); //metadata offset
  1035. uint32_t flags = f->get_32();
  1036. bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
  1037. uint64_t uid_data = f->get_64();
  1038. fw->store_32(flags);
  1039. fw->store_64(uid_data);
  1040. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1041. fw->store_32(0); // reserved
  1042. f->get_32();
  1043. }
  1044. //string table
  1045. uint32_t string_table_size = f->get_32();
  1046. fw->store_32(string_table_size);
  1047. for (uint32_t i = 0; i < string_table_size; i++) {
  1048. String s = get_ustring(f);
  1049. save_ustring(fw, s);
  1050. }
  1051. //external resources
  1052. uint32_t ext_resources_size = f->get_32();
  1053. fw->store_32(ext_resources_size);
  1054. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1055. String type = get_ustring(f);
  1056. String path = get_ustring(f);
  1057. if (using_uids) {
  1058. ResourceUID::ID uid = f->get_64();
  1059. if (uid != ResourceUID::INVALID_ID) {
  1060. if (ResourceUID::get_singleton()->has_id(uid)) {
  1061. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  1062. path = ResourceUID::get_singleton()->get_id_path(uid);
  1063. }
  1064. }
  1065. }
  1066. bool relative = false;
  1067. if (!path.begins_with("res://")) {
  1068. path = local_path.plus_file(path).simplify_path();
  1069. relative = true;
  1070. }
  1071. if (p_map.has(path)) {
  1072. String np = p_map[path];
  1073. path = np;
  1074. }
  1075. String full_path = path;
  1076. if (relative) {
  1077. //restore relative
  1078. path = local_path.path_to_file(path);
  1079. }
  1080. save_ustring(fw, type);
  1081. save_ustring(fw, path);
  1082. if (using_uids) {
  1083. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
  1084. fw->store_64(uid);
  1085. }
  1086. }
  1087. int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position();
  1088. //internal resources
  1089. uint32_t int_resources_size = f->get_32();
  1090. fw->store_32(int_resources_size);
  1091. for (uint32_t i = 0; i < int_resources_size; i++) {
  1092. String path = get_ustring(f);
  1093. uint64_t offset = f->get_64();
  1094. save_ustring(fw, path);
  1095. fw->store_64(offset + size_diff);
  1096. }
  1097. //rest of file
  1098. uint8_t b = f->get_8();
  1099. while (!f->eof_reached()) {
  1100. fw->store_8(b);
  1101. b = f->get_8();
  1102. }
  1103. bool all_ok = fw->get_error() == OK;
  1104. fw->seek(md_ofs);
  1105. fw->store_64(importmd_ofs + size_diff);
  1106. memdelete(f);
  1107. memdelete(fw);
  1108. if (!all_ok) {
  1109. return ERR_CANT_CREATE;
  1110. }
  1111. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1112. da->remove(p_path);
  1113. da->rename(p_path + ".depren", p_path);
  1114. return OK;
  1115. }
  1116. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1117. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1118. if (!f) {
  1119. return ""; //could not read
  1120. }
  1121. ResourceLoaderBinary loader;
  1122. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1123. loader.res_path = loader.local_path;
  1124. String r = loader.recognize(f);
  1125. return ClassDB::get_compatibility_remapped_class(r);
  1126. }
  1127. ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const {
  1128. String ext = p_path.get_extension().to_lower();
  1129. if (!ClassDB::is_resource_extension(ext)) {
  1130. return ResourceUID::INVALID_ID;
  1131. }
  1132. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1133. if (!f) {
  1134. return ResourceUID::INVALID_ID; //could not read
  1135. }
  1136. ResourceLoaderBinary loader;
  1137. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1138. loader.res_path = loader.local_path;
  1139. loader.open(f, true);
  1140. if (loader.error != OK) {
  1141. return ResourceUID::INVALID_ID; //could not read
  1142. }
  1143. return loader.uid;
  1144. }
  1145. ///////////////////////////////////////////////////////////
  1146. ///////////////////////////////////////////////////////////
  1147. ///////////////////////////////////////////////////////////
  1148. void ResourceFormatSaverBinaryInstance::_pad_buffer(FileAccess *f, int p_bytes) {
  1149. int extra = 4 - (p_bytes % 4);
  1150. if (extra < 4) {
  1151. for (int i = 0; i < extra; i++) {
  1152. f->store_8(0); //pad to 32
  1153. }
  1154. }
  1155. }
  1156. void ResourceFormatSaverBinaryInstance::write_variant(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) {
  1157. switch (p_property.get_type()) {
  1158. case Variant::NIL: {
  1159. f->store_32(VARIANT_NIL);
  1160. // don't store anything
  1161. } break;
  1162. case Variant::BOOL: {
  1163. f->store_32(VARIANT_BOOL);
  1164. bool val = p_property;
  1165. f->store_32(val);
  1166. } break;
  1167. case Variant::INT: {
  1168. int64_t val = p_property;
  1169. if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) {
  1170. f->store_32(VARIANT_INT64);
  1171. f->store_64(val);
  1172. } else {
  1173. f->store_32(VARIANT_INT);
  1174. f->store_32(int32_t(p_property));
  1175. }
  1176. } break;
  1177. case Variant::FLOAT: {
  1178. double d = p_property;
  1179. float fl = d;
  1180. if (double(fl) != d) {
  1181. f->store_32(VARIANT_DOUBLE);
  1182. f->store_double(d);
  1183. } else {
  1184. f->store_32(VARIANT_FLOAT);
  1185. f->store_real(fl);
  1186. }
  1187. } break;
  1188. case Variant::STRING: {
  1189. f->store_32(VARIANT_STRING);
  1190. String val = p_property;
  1191. save_unicode_string(f, val);
  1192. } break;
  1193. case Variant::VECTOR2: {
  1194. f->store_32(VARIANT_VECTOR2);
  1195. Vector2 val = p_property;
  1196. f->store_real(val.x);
  1197. f->store_real(val.y);
  1198. } break;
  1199. case Variant::VECTOR2I: {
  1200. f->store_32(VARIANT_VECTOR2I);
  1201. Vector2i val = p_property;
  1202. f->store_32(val.x);
  1203. f->store_32(val.y);
  1204. } break;
  1205. case Variant::RECT2: {
  1206. f->store_32(VARIANT_RECT2);
  1207. Rect2 val = p_property;
  1208. f->store_real(val.position.x);
  1209. f->store_real(val.position.y);
  1210. f->store_real(val.size.x);
  1211. f->store_real(val.size.y);
  1212. } break;
  1213. case Variant::RECT2I: {
  1214. f->store_32(VARIANT_RECT2I);
  1215. Rect2i val = p_property;
  1216. f->store_32(val.position.x);
  1217. f->store_32(val.position.y);
  1218. f->store_32(val.size.x);
  1219. f->store_32(val.size.y);
  1220. } break;
  1221. case Variant::VECTOR3: {
  1222. f->store_32(VARIANT_VECTOR3);
  1223. Vector3 val = p_property;
  1224. f->store_real(val.x);
  1225. f->store_real(val.y);
  1226. f->store_real(val.z);
  1227. } break;
  1228. case Variant::VECTOR3I: {
  1229. f->store_32(VARIANT_VECTOR3I);
  1230. Vector3i val = p_property;
  1231. f->store_32(val.x);
  1232. f->store_32(val.y);
  1233. f->store_32(val.z);
  1234. } break;
  1235. case Variant::PLANE: {
  1236. f->store_32(VARIANT_PLANE);
  1237. Plane val = p_property;
  1238. f->store_real(val.normal.x);
  1239. f->store_real(val.normal.y);
  1240. f->store_real(val.normal.z);
  1241. f->store_real(val.d);
  1242. } break;
  1243. case Variant::QUATERNION: {
  1244. f->store_32(VARIANT_QUATERNION);
  1245. Quaternion val = p_property;
  1246. f->store_real(val.x);
  1247. f->store_real(val.y);
  1248. f->store_real(val.z);
  1249. f->store_real(val.w);
  1250. } break;
  1251. case Variant::AABB: {
  1252. f->store_32(VARIANT_AABB);
  1253. AABB val = p_property;
  1254. f->store_real(val.position.x);
  1255. f->store_real(val.position.y);
  1256. f->store_real(val.position.z);
  1257. f->store_real(val.size.x);
  1258. f->store_real(val.size.y);
  1259. f->store_real(val.size.z);
  1260. } break;
  1261. case Variant::TRANSFORM2D: {
  1262. f->store_32(VARIANT_TRANSFORM2D);
  1263. Transform2D val = p_property;
  1264. f->store_real(val.elements[0].x);
  1265. f->store_real(val.elements[0].y);
  1266. f->store_real(val.elements[1].x);
  1267. f->store_real(val.elements[1].y);
  1268. f->store_real(val.elements[2].x);
  1269. f->store_real(val.elements[2].y);
  1270. } break;
  1271. case Variant::BASIS: {
  1272. f->store_32(VARIANT_BASIS);
  1273. Basis val = p_property;
  1274. f->store_real(val.elements[0].x);
  1275. f->store_real(val.elements[0].y);
  1276. f->store_real(val.elements[0].z);
  1277. f->store_real(val.elements[1].x);
  1278. f->store_real(val.elements[1].y);
  1279. f->store_real(val.elements[1].z);
  1280. f->store_real(val.elements[2].x);
  1281. f->store_real(val.elements[2].y);
  1282. f->store_real(val.elements[2].z);
  1283. } break;
  1284. case Variant::TRANSFORM3D: {
  1285. f->store_32(VARIANT_TRANSFORM3D);
  1286. Transform3D val = p_property;
  1287. f->store_real(val.basis.elements[0].x);
  1288. f->store_real(val.basis.elements[0].y);
  1289. f->store_real(val.basis.elements[0].z);
  1290. f->store_real(val.basis.elements[1].x);
  1291. f->store_real(val.basis.elements[1].y);
  1292. f->store_real(val.basis.elements[1].z);
  1293. f->store_real(val.basis.elements[2].x);
  1294. f->store_real(val.basis.elements[2].y);
  1295. f->store_real(val.basis.elements[2].z);
  1296. f->store_real(val.origin.x);
  1297. f->store_real(val.origin.y);
  1298. f->store_real(val.origin.z);
  1299. } break;
  1300. case Variant::COLOR: {
  1301. f->store_32(VARIANT_COLOR);
  1302. Color val = p_property;
  1303. f->store_real(val.r);
  1304. f->store_real(val.g);
  1305. f->store_real(val.b);
  1306. f->store_real(val.a);
  1307. } break;
  1308. case Variant::STRING_NAME: {
  1309. f->store_32(VARIANT_STRING_NAME);
  1310. String val = p_property;
  1311. save_unicode_string(f, val);
  1312. } break;
  1313. case Variant::NODE_PATH: {
  1314. f->store_32(VARIANT_NODE_PATH);
  1315. NodePath np = p_property;
  1316. f->store_16(np.get_name_count());
  1317. uint16_t snc = np.get_subname_count();
  1318. if (np.is_absolute()) {
  1319. snc |= 0x8000;
  1320. }
  1321. f->store_16(snc);
  1322. for (int i = 0; i < np.get_name_count(); i++) {
  1323. if (string_map.has(np.get_name(i))) {
  1324. f->store_32(string_map[np.get_name(i)]);
  1325. } else {
  1326. save_unicode_string(f, np.get_name(i), true);
  1327. }
  1328. }
  1329. for (int i = 0; i < np.get_subname_count(); i++) {
  1330. if (string_map.has(np.get_subname(i))) {
  1331. f->store_32(string_map[np.get_subname(i)]);
  1332. } else {
  1333. save_unicode_string(f, np.get_subname(i), true);
  1334. }
  1335. }
  1336. } break;
  1337. case Variant::RID: {
  1338. f->store_32(VARIANT_RID);
  1339. WARN_PRINT("Can't save RIDs.");
  1340. RID val = p_property;
  1341. f->store_32(val.get_id());
  1342. } break;
  1343. case Variant::OBJECT: {
  1344. f->store_32(VARIANT_OBJECT);
  1345. RES res = p_property;
  1346. if (res.is_null()) {
  1347. f->store_32(OBJECT_EMPTY);
  1348. return; // don't save it
  1349. }
  1350. if (!res->is_built_in()) {
  1351. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1352. f->store_32(external_resources[res]);
  1353. } else {
  1354. if (!resource_map.has(res)) {
  1355. f->store_32(OBJECT_EMPTY);
  1356. ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference.");
  1357. }
  1358. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1359. f->store_32(resource_map[res]);
  1360. //internal resource
  1361. }
  1362. } break;
  1363. case Variant::CALLABLE: {
  1364. f->store_32(VARIANT_CALLABLE);
  1365. WARN_PRINT("Can't save Callables.");
  1366. } break;
  1367. case Variant::SIGNAL: {
  1368. f->store_32(VARIANT_SIGNAL);
  1369. WARN_PRINT("Can't save Signals.");
  1370. } break;
  1371. case Variant::DICTIONARY: {
  1372. f->store_32(VARIANT_DICTIONARY);
  1373. Dictionary d = p_property;
  1374. f->store_32(uint32_t(d.size()));
  1375. List<Variant> keys;
  1376. d.get_key_list(&keys);
  1377. for (const Variant &E : keys) {
  1378. write_variant(f, E, resource_map, external_resources, string_map);
  1379. write_variant(f, d[E], resource_map, external_resources, string_map);
  1380. }
  1381. } break;
  1382. case Variant::ARRAY: {
  1383. f->store_32(VARIANT_ARRAY);
  1384. Array a = p_property;
  1385. f->store_32(uint32_t(a.size()));
  1386. for (int i = 0; i < a.size(); i++) {
  1387. write_variant(f, a[i], resource_map, external_resources, string_map);
  1388. }
  1389. } break;
  1390. case Variant::PACKED_BYTE_ARRAY: {
  1391. f->store_32(VARIANT_PACKED_BYTE_ARRAY);
  1392. Vector<uint8_t> arr = p_property;
  1393. int len = arr.size();
  1394. f->store_32(len);
  1395. const uint8_t *r = arr.ptr();
  1396. f->store_buffer(r, len);
  1397. _pad_buffer(f, len);
  1398. } break;
  1399. case Variant::PACKED_INT32_ARRAY: {
  1400. f->store_32(VARIANT_PACKED_INT32_ARRAY);
  1401. Vector<int32_t> arr = p_property;
  1402. int len = arr.size();
  1403. f->store_32(len);
  1404. const int32_t *r = arr.ptr();
  1405. for (int i = 0; i < len; i++) {
  1406. f->store_32(r[i]);
  1407. }
  1408. } break;
  1409. case Variant::PACKED_INT64_ARRAY: {
  1410. f->store_32(VARIANT_PACKED_INT64_ARRAY);
  1411. Vector<int64_t> arr = p_property;
  1412. int len = arr.size();
  1413. f->store_32(len);
  1414. const int64_t *r = arr.ptr();
  1415. for (int i = 0; i < len; i++) {
  1416. f->store_64(r[i]);
  1417. }
  1418. } break;
  1419. case Variant::PACKED_FLOAT32_ARRAY: {
  1420. f->store_32(VARIANT_PACKED_FLOAT32_ARRAY);
  1421. Vector<float> arr = p_property;
  1422. int len = arr.size();
  1423. f->store_32(len);
  1424. const float *r = arr.ptr();
  1425. for (int i = 0; i < len; i++) {
  1426. f->store_real(r[i]);
  1427. }
  1428. } break;
  1429. case Variant::PACKED_FLOAT64_ARRAY: {
  1430. f->store_32(VARIANT_PACKED_FLOAT64_ARRAY);
  1431. Vector<double> arr = p_property;
  1432. int len = arr.size();
  1433. f->store_32(len);
  1434. const double *r = arr.ptr();
  1435. for (int i = 0; i < len; i++) {
  1436. f->store_double(r[i]);
  1437. }
  1438. } break;
  1439. case Variant::PACKED_STRING_ARRAY: {
  1440. f->store_32(VARIANT_PACKED_STRING_ARRAY);
  1441. Vector<String> arr = p_property;
  1442. int len = arr.size();
  1443. f->store_32(len);
  1444. const String *r = arr.ptr();
  1445. for (int i = 0; i < len; i++) {
  1446. save_unicode_string(f, r[i]);
  1447. }
  1448. } break;
  1449. case Variant::PACKED_VECTOR3_ARRAY: {
  1450. f->store_32(VARIANT_PACKED_VECTOR3_ARRAY);
  1451. Vector<Vector3> arr = p_property;
  1452. int len = arr.size();
  1453. f->store_32(len);
  1454. const Vector3 *r = arr.ptr();
  1455. for (int i = 0; i < len; i++) {
  1456. f->store_real(r[i].x);
  1457. f->store_real(r[i].y);
  1458. f->store_real(r[i].z);
  1459. }
  1460. } break;
  1461. case Variant::PACKED_VECTOR2_ARRAY: {
  1462. f->store_32(VARIANT_PACKED_VECTOR2_ARRAY);
  1463. Vector<Vector2> arr = p_property;
  1464. int len = arr.size();
  1465. f->store_32(len);
  1466. const Vector2 *r = arr.ptr();
  1467. for (int i = 0; i < len; i++) {
  1468. f->store_real(r[i].x);
  1469. f->store_real(r[i].y);
  1470. }
  1471. } break;
  1472. case Variant::PACKED_COLOR_ARRAY: {
  1473. f->store_32(VARIANT_PACKED_COLOR_ARRAY);
  1474. Vector<Color> arr = p_property;
  1475. int len = arr.size();
  1476. f->store_32(len);
  1477. const Color *r = arr.ptr();
  1478. for (int i = 0; i < len; i++) {
  1479. f->store_real(r[i].r);
  1480. f->store_real(r[i].g);
  1481. f->store_real(r[i].b);
  1482. f->store_real(r[i].a);
  1483. }
  1484. } break;
  1485. default: {
  1486. ERR_FAIL_MSG("Invalid variant.");
  1487. }
  1488. }
  1489. }
  1490. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1491. switch (p_variant.get_type()) {
  1492. case Variant::OBJECT: {
  1493. RES res = p_variant;
  1494. if (res.is_null() || external_resources.has(res)) {
  1495. return;
  1496. }
  1497. if (!p_main && (!bundle_resources) && !res->is_built_in()) {
  1498. if (res->get_path() == path) {
  1499. ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
  1500. return;
  1501. }
  1502. int idx = external_resources.size();
  1503. external_resources[res] = idx;
  1504. return;
  1505. }
  1506. if (resource_set.has(res)) {
  1507. return;
  1508. }
  1509. List<PropertyInfo> property_list;
  1510. res->get_property_list(&property_list);
  1511. for (const PropertyInfo &E : property_list) {
  1512. if (E.usage & PROPERTY_USAGE_STORAGE) {
  1513. Variant value = res->get(E.name);
  1514. if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1515. RES sres = value;
  1516. if (sres.is_valid()) {
  1517. NonPersistentKey npk;
  1518. npk.base = res;
  1519. npk.property = E.name;
  1520. non_persistent_map[npk] = sres;
  1521. resource_set.insert(sres);
  1522. saved_resources.push_back(sres);
  1523. }
  1524. } else {
  1525. _find_resources(value);
  1526. }
  1527. }
  1528. }
  1529. resource_set.insert(res);
  1530. saved_resources.push_back(res);
  1531. } break;
  1532. case Variant::ARRAY: {
  1533. Array varray = p_variant;
  1534. int len = varray.size();
  1535. for (int i = 0; i < len; i++) {
  1536. const Variant &v = varray.get(i);
  1537. _find_resources(v);
  1538. }
  1539. } break;
  1540. case Variant::DICTIONARY: {
  1541. Dictionary d = p_variant;
  1542. List<Variant> keys;
  1543. d.get_key_list(&keys);
  1544. for (const Variant &E : keys) {
  1545. _find_resources(E);
  1546. Variant v = d[E];
  1547. _find_resources(v);
  1548. }
  1549. } break;
  1550. case Variant::NODE_PATH: {
  1551. //take the chance and save node path strings
  1552. NodePath np = p_variant;
  1553. for (int i = 0; i < np.get_name_count(); i++) {
  1554. get_string_index(np.get_name(i));
  1555. }
  1556. for (int i = 0; i < np.get_subname_count(); i++) {
  1557. get_string_index(np.get_subname(i));
  1558. }
  1559. } break;
  1560. default: {
  1561. }
  1562. }
  1563. }
  1564. void ResourceFormatSaverBinaryInstance::save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len) {
  1565. CharString utf8 = p_string.utf8();
  1566. if (p_bit_on_len) {
  1567. f->store_32((utf8.length() + 1) | 0x80000000);
  1568. } else {
  1569. f->store_32(utf8.length() + 1);
  1570. }
  1571. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1572. }
  1573. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1574. StringName s = p_string;
  1575. if (string_map.has(s)) {
  1576. return string_map[s];
  1577. }
  1578. string_map[s] = strings.size();
  1579. strings.push_back(s);
  1580. return strings.size() - 1;
  1581. }
  1582. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1583. Error err;
  1584. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1585. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1586. fac->configure("RSCC");
  1587. f = fac;
  1588. err = fac->_open(p_path, FileAccess::WRITE);
  1589. if (err) {
  1590. memdelete(f);
  1591. }
  1592. } else {
  1593. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1594. }
  1595. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'.");
  1596. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1597. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1598. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1599. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1600. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1601. if (!p_path.begins_with("res://")) {
  1602. takeover_paths = false;
  1603. }
  1604. local_path = p_path.get_base_dir();
  1605. path = ProjectSettings::get_singleton()->localize_path(p_path);
  1606. _find_resources(p_resource, true);
  1607. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1608. //save header compressed
  1609. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1610. f->store_buffer(header, 4);
  1611. }
  1612. if (big_endian) {
  1613. f->store_32(1);
  1614. f->set_big_endian(true);
  1615. } else {
  1616. f->store_32(0);
  1617. }
  1618. f->store_32(0); //64 bits file, false for now
  1619. f->store_32(VERSION_MAJOR);
  1620. f->store_32(VERSION_MINOR);
  1621. f->store_32(FORMAT_VERSION);
  1622. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1623. f->close();
  1624. memdelete(f);
  1625. return ERR_CANT_CREATE;
  1626. }
  1627. save_unicode_string(f, p_resource->get_class());
  1628. f->store_64(0); //offset to import metadata
  1629. {
  1630. uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS;
  1631. #ifdef REAL_T_IS_DOUBLE
  1632. format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE;
  1633. #endif
  1634. f->store_32(format_flags);
  1635. }
  1636. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true);
  1637. f->store_64(uid);
  1638. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1639. f->store_32(0); // reserved
  1640. }
  1641. List<ResourceData> resources;
  1642. {
  1643. for (const RES &E : saved_resources) {
  1644. ResourceData &rd = resources.push_back(ResourceData())->get();
  1645. rd.type = E->get_class();
  1646. List<PropertyInfo> property_list;
  1647. E->get_property_list(&property_list);
  1648. for (const PropertyInfo &F : property_list) {
  1649. if (skip_editor && F.name.begins_with("__editor")) {
  1650. continue;
  1651. }
  1652. if ((F.usage & PROPERTY_USAGE_STORAGE)) {
  1653. Property p;
  1654. p.name_idx = get_string_index(F.name);
  1655. if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1656. NonPersistentKey npk;
  1657. npk.base = E;
  1658. npk.property = F.name;
  1659. if (non_persistent_map.has(npk)) {
  1660. p.value = non_persistent_map[npk];
  1661. }
  1662. } else {
  1663. p.value = E->get(F.name);
  1664. }
  1665. Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name);
  1666. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) {
  1667. continue;
  1668. }
  1669. p.pi = F;
  1670. rd.properties.push_back(p);
  1671. }
  1672. }
  1673. }
  1674. }
  1675. f->store_32(strings.size()); //string table size
  1676. for (int i = 0; i < strings.size(); i++) {
  1677. save_unicode_string(f, strings[i]);
  1678. }
  1679. // save external resource table
  1680. f->store_32(external_resources.size()); //amount of external resources
  1681. Vector<RES> save_order;
  1682. save_order.resize(external_resources.size());
  1683. for (const KeyValue<RES, int> &E : external_resources) {
  1684. save_order.write[E.value] = E.key;
  1685. }
  1686. for (int i = 0; i < save_order.size(); i++) {
  1687. save_unicode_string(f, save_order[i]->get_save_class());
  1688. String path = save_order[i]->get_path();
  1689. path = relative_paths ? local_path.path_to_file(path) : path;
  1690. save_unicode_string(f, path);
  1691. ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false);
  1692. f->store_64(ruid);
  1693. }
  1694. // save internal resource table
  1695. f->store_32(saved_resources.size()); //amount of internal resources
  1696. Vector<uint64_t> ofs_pos;
  1697. Set<String> used_unique_ids;
  1698. for (RES &r : saved_resources) {
  1699. if (r->is_built_in()) {
  1700. if (!r->get_scene_unique_id().is_empty()) {
  1701. if (used_unique_ids.has(r->get_scene_unique_id())) {
  1702. r->set_scene_unique_id("");
  1703. } else {
  1704. used_unique_ids.insert(r->get_scene_unique_id());
  1705. }
  1706. }
  1707. }
  1708. }
  1709. Map<RES, int> resource_map;
  1710. int res_index = 0;
  1711. for (RES &r : saved_resources) {
  1712. if (r->is_built_in()) {
  1713. if (r->get_scene_unique_id().is_empty()) {
  1714. String new_id;
  1715. while (true) {
  1716. new_id = r->get_class() + "_" + Resource::generate_scene_unique_id();
  1717. if (!used_unique_ids.has(new_id)) {
  1718. break;
  1719. }
  1720. }
  1721. r->set_scene_unique_id(new_id);
  1722. used_unique_ids.insert(new_id);
  1723. }
  1724. save_unicode_string(f, "local://" + r->get_scene_unique_id());
  1725. if (takeover_paths) {
  1726. r->set_path(p_path + "::" + r->get_scene_unique_id(), true);
  1727. }
  1728. #ifdef TOOLS_ENABLED
  1729. r->set_edited(false);
  1730. #endif
  1731. } else {
  1732. save_unicode_string(f, r->get_path()); //actual external
  1733. }
  1734. ofs_pos.push_back(f->get_position());
  1735. f->store_64(0); //offset in 64 bits
  1736. resource_map[r] = res_index++;
  1737. }
  1738. Vector<uint64_t> ofs_table;
  1739. //now actually save the resources
  1740. for (const ResourceData &rd : resources) {
  1741. ofs_table.push_back(f->get_position());
  1742. save_unicode_string(f, rd.type);
  1743. f->store_32(rd.properties.size());
  1744. for (const Property &p : rd.properties) {
  1745. f->store_32(p.name_idx);
  1746. write_variant(f, p.value, resource_map, external_resources, string_map, p.pi);
  1747. }
  1748. }
  1749. for (int i = 0; i < ofs_table.size(); i++) {
  1750. f->seek(ofs_pos[i]);
  1751. f->store_64(ofs_table[i]);
  1752. }
  1753. f->seek_end();
  1754. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1755. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1756. f->close();
  1757. memdelete(f);
  1758. return ERR_CANT_CREATE;
  1759. }
  1760. f->close();
  1761. memdelete(f);
  1762. return OK;
  1763. }
  1764. Error ResourceFormatSaverBinary::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1765. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1766. ResourceFormatSaverBinaryInstance saver;
  1767. return saver.save(local_path, p_resource, p_flags);
  1768. }
  1769. bool ResourceFormatSaverBinary::recognize(const RES &p_resource) const {
  1770. return true; //all recognized
  1771. }
  1772. void ResourceFormatSaverBinary::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1773. String base = p_resource->get_base_extension().to_lower();
  1774. p_extensions->push_back(base);
  1775. if (base != "res") {
  1776. p_extensions->push_back("res");
  1777. }
  1778. }
  1779. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr;
  1780. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  1781. singleton = this;
  1782. }