resource_format_binary.cpp 66 KB

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