resource_format_binary.cpp 69 KB

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