resource_format_binary.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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(String("Couldn't load resource (no cache): " + path).utf8().get_data());
  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(String("Couldn't load resource: " + path).utf8().get_data());
  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, "Can't load dependency: " + 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_valid()) {
  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, "Can't load dependency: " + 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. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) {
  669. //use the existing one
  670. Ref<Resource> cached = ResourceCache::get_ref(path);
  671. if (cached->get_class() == t) {
  672. cached->reset_state();
  673. res = cached;
  674. }
  675. }
  676. MissingResource *missing_resource = nullptr;
  677. if (res.is_null()) {
  678. //did not replace
  679. Object *obj = ClassDB::instantiate(t);
  680. if (!obj) {
  681. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  682. //create a missing resource
  683. missing_resource = memnew(MissingResource);
  684. missing_resource->set_original_class(t);
  685. missing_resource->set_recording_properties(true);
  686. obj = missing_resource;
  687. } else {
  688. error = ERR_FILE_CORRUPT;
  689. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource of unrecognized type in file: " + t + ".");
  690. }
  691. }
  692. Resource *r = Object::cast_to<Resource>(obj);
  693. if (!r) {
  694. String obj_class = obj->get_class();
  695. error = ERR_FILE_CORRUPT;
  696. memdelete(obj); //bye
  697. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource type in resource field not a resource, type is: " + obj_class + ".");
  698. }
  699. res = Ref<Resource>(r);
  700. if (!path.is_empty()) {
  701. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  702. r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); // If got here because the resource with same path has different type, replace it.
  703. } else {
  704. r->set_path_cache(path);
  705. }
  706. }
  707. r->set_scene_unique_id(id);
  708. }
  709. if (!main) {
  710. internal_index_cache[path] = res;
  711. }
  712. int pc = f->get_32();
  713. //set properties
  714. Dictionary missing_resource_properties;
  715. for (int j = 0; j < pc; j++) {
  716. StringName name = _get_string();
  717. if (name == StringName()) {
  718. error = ERR_FILE_CORRUPT;
  719. ERR_FAIL_V(ERR_FILE_CORRUPT);
  720. }
  721. Variant value;
  722. error = parse_variant(value);
  723. if (error) {
  724. return error;
  725. }
  726. bool set_valid = true;
  727. if (value.get_type() == Variant::OBJECT && missing_resource != nullptr) {
  728. // If the property being set is a missing resource (and the parent is not),
  729. // then setting it will most likely not work.
  730. // Instead, save it as metadata.
  731. Ref<MissingResource> mr = value;
  732. if (mr.is_valid()) {
  733. missing_resource_properties[name] = mr;
  734. set_valid = false;
  735. }
  736. }
  737. if (value.get_type() == Variant::ARRAY) {
  738. Array set_array = value;
  739. bool is_get_valid = false;
  740. Variant get_value = res->get(name, &is_get_valid);
  741. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  742. Array get_array = get_value;
  743. if (!set_array.is_same_typed(get_array)) {
  744. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  745. }
  746. }
  747. }
  748. if (set_valid) {
  749. res->set(name, value);
  750. }
  751. }
  752. if (missing_resource) {
  753. missing_resource->set_recording_properties(false);
  754. }
  755. if (!missing_resource_properties.is_empty()) {
  756. res->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  757. }
  758. #ifdef TOOLS_ENABLED
  759. res->set_edited(false);
  760. #endif
  761. if (progress) {
  762. *progress = (i + 1) / float(internal_resources.size());
  763. }
  764. resource_cache.push_back(res);
  765. if (main) {
  766. f.unref();
  767. resource = res;
  768. resource->set_as_translation_remapped(translation_remapped);
  769. error = OK;
  770. return OK;
  771. }
  772. }
  773. return ERR_FILE_EOF;
  774. }
  775. void ResourceLoaderBinary::set_translation_remapped(bool p_remapped) {
  776. translation_remapped = p_remapped;
  777. }
  778. static void save_ustring(Ref<FileAccess> f, const String &p_string) {
  779. CharString utf8 = p_string.utf8();
  780. f->store_32(utf8.length() + 1);
  781. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  782. }
  783. static String get_ustring(Ref<FileAccess> f) {
  784. int len = f->get_32();
  785. Vector<char> str_buf;
  786. str_buf.resize(len);
  787. f->get_buffer((uint8_t *)&str_buf[0], len);
  788. String s;
  789. s.parse_utf8(&str_buf[0]);
  790. return s;
  791. }
  792. String ResourceLoaderBinary::get_unicode_string() {
  793. int len = f->get_32();
  794. if (len > str_buf.size()) {
  795. str_buf.resize(len);
  796. }
  797. if (len == 0) {
  798. return String();
  799. }
  800. f->get_buffer((uint8_t *)&str_buf[0], len);
  801. String s;
  802. s.parse_utf8(&str_buf[0]);
  803. return s;
  804. }
  805. void ResourceLoaderBinary::get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes) {
  806. open(p_f, false, true);
  807. if (error) {
  808. return;
  809. }
  810. for (int i = 0; i < internal_resources.size(); i++) {
  811. p_f->seek(internal_resources[i].offset);
  812. String t = get_unicode_string();
  813. ERR_FAIL_COND(p_f->get_error() != OK);
  814. if (t != String()) {
  815. p_classes->insert(t);
  816. }
  817. }
  818. }
  819. void ResourceLoaderBinary::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) {
  820. open(p_f, false, true);
  821. if (error) {
  822. return;
  823. }
  824. for (int i = 0; i < external_resources.size(); i++) {
  825. String dep;
  826. String fallback_path;
  827. if (external_resources[i].uid != ResourceUID::INVALID_ID) {
  828. dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid);
  829. fallback_path = external_resources[i].path; // Used by Dependency Editor, in case uid path fails.
  830. } else {
  831. dep = external_resources[i].path;
  832. }
  833. if (p_add_types && !external_resources[i].type.is_empty()) {
  834. dep += "::" + external_resources[i].type;
  835. }
  836. if (!fallback_path.is_empty()) {
  837. if (!p_add_types) {
  838. dep += "::"; // Ensure that path comes third, even if there is no type.
  839. }
  840. dep += "::" + fallback_path;
  841. }
  842. p_dependencies->push_back(dep);
  843. }
  844. }
  845. void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p_keep_uuid_paths) {
  846. error = OK;
  847. f = p_f;
  848. uint8_t header[4];
  849. f->get_buffer(header, 4);
  850. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  851. // Compressed.
  852. Ref<FileAccessCompressed> fac;
  853. fac.instantiate();
  854. error = fac->open_after_magic(f);
  855. if (error != OK) {
  856. f.unref();
  857. ERR_FAIL_MSG("Failed to open binary resource file: " + local_path + ".");
  858. }
  859. f = fac;
  860. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  861. // Not normal.
  862. error = ERR_FILE_UNRECOGNIZED;
  863. f.unref();
  864. ERR_FAIL_MSG("Unrecognized binary resource file: " + local_path + ".");
  865. }
  866. bool big_endian = f->get_32();
  867. bool use_real64 = f->get_32();
  868. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  869. uint32_t ver_major = f->get_32();
  870. uint32_t ver_minor = f->get_32();
  871. ver_format = f->get_32();
  872. print_bl("big endian: " + itos(big_endian));
  873. #ifdef BIG_ENDIAN_ENABLED
  874. print_bl("endian swap: " + itos(!big_endian));
  875. #else
  876. print_bl("endian swap: " + itos(big_endian));
  877. #endif
  878. print_bl("real64: " + itos(use_real64));
  879. print_bl("major: " + itos(ver_major));
  880. print_bl("minor: " + itos(ver_minor));
  881. print_bl("format: " + itos(ver_format));
  882. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  883. f.unref();
  884. 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).",
  885. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  886. }
  887. type = get_unicode_string();
  888. print_bl("type: " + type);
  889. importmd_ofs = f->get_64();
  890. uint32_t flags = f->get_32();
  891. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) {
  892. using_named_scene_ids = true;
  893. }
  894. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) {
  895. using_uids = true;
  896. }
  897. f->real_is_double = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_REAL_T_IS_DOUBLE) != 0;
  898. if (using_uids) {
  899. uid = f->get_64();
  900. } else {
  901. f->get_64(); // skip over uid field
  902. uid = ResourceUID::INVALID_ID;
  903. }
  904. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  905. script_class = get_unicode_string();
  906. }
  907. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  908. f->get_32(); //skip a few reserved fields
  909. }
  910. if (p_no_resources) {
  911. return;
  912. }
  913. uint32_t string_table_size = f->get_32();
  914. string_map.resize(string_table_size);
  915. for (uint32_t i = 0; i < string_table_size; i++) {
  916. StringName s = get_unicode_string();
  917. string_map.write[i] = s;
  918. }
  919. print_bl("strings: " + itos(string_table_size));
  920. uint32_t ext_resources_size = f->get_32();
  921. for (uint32_t i = 0; i < ext_resources_size; i++) {
  922. ExtResource er;
  923. er.type = get_unicode_string();
  924. er.path = get_unicode_string();
  925. if (using_uids) {
  926. er.uid = f->get_64();
  927. if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) {
  928. if (ResourceUID::get_singleton()->has_id(er.uid)) {
  929. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  930. er.path = ResourceUID::get_singleton()->get_id_path(er.uid);
  931. } else {
  932. #ifdef TOOLS_ENABLED
  933. // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated.
  934. if (ResourceLoader::get_resource_uid(res_path) != er.uid) {
  935. 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());
  936. }
  937. #else
  938. 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());
  939. #endif
  940. }
  941. }
  942. }
  943. external_resources.push_back(er);
  944. }
  945. print_bl("ext resources: " + itos(ext_resources_size));
  946. uint32_t int_resources_size = f->get_32();
  947. for (uint32_t i = 0; i < int_resources_size; i++) {
  948. IntResource ir;
  949. ir.path = get_unicode_string();
  950. ir.offset = f->get_64();
  951. internal_resources.push_back(ir);
  952. }
  953. print_bl("int resources: " + itos(int_resources_size));
  954. if (f->eof_reached()) {
  955. error = ERR_FILE_CORRUPT;
  956. f.unref();
  957. ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + ".");
  958. }
  959. }
  960. String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) {
  961. error = OK;
  962. f = p_f;
  963. uint8_t header[4];
  964. f->get_buffer(header, 4);
  965. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  966. // Compressed.
  967. Ref<FileAccessCompressed> fac;
  968. fac.instantiate();
  969. error = fac->open_after_magic(f);
  970. if (error != OK) {
  971. f.unref();
  972. return "";
  973. }
  974. f = fac;
  975. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  976. // Not normal.
  977. error = ERR_FILE_UNRECOGNIZED;
  978. f.unref();
  979. return "";
  980. }
  981. bool big_endian = f->get_32();
  982. f->get_32(); // use_real64
  983. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  984. uint32_t ver_major = f->get_32();
  985. f->get_32(); // ver_minor
  986. uint32_t ver_fmt = f->get_32();
  987. if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  988. f.unref();
  989. return "";
  990. }
  991. return get_unicode_string();
  992. }
  993. String ResourceLoaderBinary::recognize_script_class(Ref<FileAccess> p_f) {
  994. error = OK;
  995. f = p_f;
  996. uint8_t header[4];
  997. f->get_buffer(header, 4);
  998. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  999. // Compressed.
  1000. Ref<FileAccessCompressed> fac;
  1001. fac.instantiate();
  1002. error = fac->open_after_magic(f);
  1003. if (error != OK) {
  1004. f.unref();
  1005. return "";
  1006. }
  1007. f = fac;
  1008. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1009. // Not normal.
  1010. error = ERR_FILE_UNRECOGNIZED;
  1011. f.unref();
  1012. return "";
  1013. }
  1014. bool big_endian = f->get_32();
  1015. f->get_32(); // use_real64
  1016. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  1017. uint32_t ver_major = f->get_32();
  1018. f->get_32(); // ver_minor
  1019. uint32_t ver_fmt = f->get_32();
  1020. if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1021. f.unref();
  1022. return "";
  1023. }
  1024. get_unicode_string(); // type
  1025. f->get_64(); // Metadata offset
  1026. uint32_t flags = f->get_32();
  1027. f->get_64(); // UID
  1028. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  1029. return get_unicode_string();
  1030. } else {
  1031. return String();
  1032. }
  1033. }
  1034. 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) {
  1035. if (r_error) {
  1036. *r_error = ERR_FILE_CANT_OPEN;
  1037. }
  1038. Error err;
  1039. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  1040. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot open file '" + p_path + "'.");
  1041. ResourceLoaderBinary loader;
  1042. switch (p_cache_mode) {
  1043. case CACHE_MODE_IGNORE:
  1044. case CACHE_MODE_REUSE:
  1045. case CACHE_MODE_REPLACE:
  1046. loader.cache_mode = p_cache_mode;
  1047. loader.cache_mode_for_external = CACHE_MODE_REUSE;
  1048. break;
  1049. case CACHE_MODE_IGNORE_DEEP:
  1050. loader.cache_mode = CACHE_MODE_IGNORE;
  1051. loader.cache_mode_for_external = p_cache_mode;
  1052. break;
  1053. case CACHE_MODE_REPLACE_DEEP:
  1054. loader.cache_mode = CACHE_MODE_REPLACE;
  1055. loader.cache_mode_for_external = p_cache_mode;
  1056. break;
  1057. }
  1058. loader.use_sub_threads = p_use_sub_threads;
  1059. loader.progress = r_progress;
  1060. String path = !p_original_path.is_empty() ? p_original_path : p_path;
  1061. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  1062. loader.res_path = loader.local_path;
  1063. loader.open(f);
  1064. err = loader.load();
  1065. if (r_error) {
  1066. *r_error = err;
  1067. }
  1068. if (err) {
  1069. return Ref<Resource>();
  1070. }
  1071. return loader.resource;
  1072. }
  1073. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  1074. if (p_type.is_empty()) {
  1075. get_recognized_extensions(p_extensions);
  1076. return;
  1077. }
  1078. List<String> extensions;
  1079. ClassDB::get_extensions_for_type(p_type, &extensions);
  1080. extensions.sort();
  1081. for (const String &E : extensions) {
  1082. String ext = E.to_lower();
  1083. p_extensions->push_back(ext);
  1084. }
  1085. }
  1086. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  1087. List<String> extensions;
  1088. ClassDB::get_resource_base_extensions(&extensions);
  1089. extensions.sort();
  1090. for (const String &E : extensions) {
  1091. String ext = E.to_lower();
  1092. p_extensions->push_back(ext);
  1093. }
  1094. }
  1095. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  1096. return true; //handles all
  1097. }
  1098. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  1099. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1100. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'.");
  1101. ResourceLoaderBinary loader;
  1102. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1103. loader.res_path = loader.local_path;
  1104. loader.get_dependencies(f, p_dependencies, p_add_types);
  1105. }
  1106. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) {
  1107. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1108. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1109. Ref<FileAccess> fw;
  1110. String local_path = p_path.get_base_dir();
  1111. uint8_t header[4];
  1112. f->get_buffer(header, 4);
  1113. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  1114. // Compressed.
  1115. Ref<FileAccessCompressed> fac;
  1116. fac.instantiate();
  1117. Error err = fac->open_after_magic(f);
  1118. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  1119. f = fac;
  1120. Ref<FileAccessCompressed> facw;
  1121. facw.instantiate();
  1122. facw->configure("RSCC");
  1123. err = facw->open_internal(p_path + ".depren", FileAccess::WRITE);
  1124. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'.");
  1125. fw = facw;
  1126. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1127. // Not normal.
  1128. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'.");
  1129. } else {
  1130. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  1131. ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'.");
  1132. uint8_t magic[4] = { 'R', 'S', 'R', 'C' };
  1133. fw->store_buffer(magic, 4);
  1134. }
  1135. bool big_endian = f->get_32();
  1136. bool use_real64 = f->get_32();
  1137. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  1138. #ifdef BIG_ENDIAN_ENABLED
  1139. fw->store_32(!big_endian);
  1140. #else
  1141. fw->store_32(big_endian);
  1142. #endif
  1143. fw->set_big_endian(big_endian != 0);
  1144. fw->store_32(use_real64); //use real64
  1145. uint32_t ver_major = f->get_32();
  1146. uint32_t ver_minor = f->get_32();
  1147. uint32_t ver_format = f->get_32();
  1148. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  1149. fw.unref();
  1150. {
  1151. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1152. da->remove(p_path + ".depren");
  1153. }
  1154. // Use the old approach.
  1155. WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'.");
  1156. Error err;
  1157. f = FileAccess::open(p_path, FileAccess::READ, &err);
  1158. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  1159. ResourceLoaderBinary loader;
  1160. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1161. loader.res_path = loader.local_path;
  1162. loader.remaps = p_map;
  1163. loader.open(f);
  1164. err = loader.load();
  1165. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  1166. Ref<Resource> res = loader.get_resource();
  1167. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  1168. return ResourceFormatSaverBinary::singleton->save(res, p_path);
  1169. }
  1170. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1171. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  1172. 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).",
  1173. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  1174. }
  1175. // Since we're not actually converting the file contents, leave the version
  1176. // numbers in the file untouched.
  1177. fw->store_32(ver_major);
  1178. fw->store_32(ver_minor);
  1179. fw->store_32(ver_format);
  1180. save_ustring(fw, get_ustring(f)); //type
  1181. uint64_t md_ofs = f->get_position();
  1182. uint64_t importmd_ofs = f->get_64();
  1183. fw->store_64(0); //metadata offset
  1184. uint32_t flags = f->get_32();
  1185. bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
  1186. uint64_t uid_data = f->get_64();
  1187. fw->store_32(flags);
  1188. fw->store_64(uid_data);
  1189. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  1190. save_ustring(fw, get_ustring(f));
  1191. }
  1192. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1193. fw->store_32(0); // reserved
  1194. f->get_32();
  1195. }
  1196. //string table
  1197. uint32_t string_table_size = f->get_32();
  1198. fw->store_32(string_table_size);
  1199. for (uint32_t i = 0; i < string_table_size; i++) {
  1200. String s = get_ustring(f);
  1201. save_ustring(fw, s);
  1202. }
  1203. //external resources
  1204. uint32_t ext_resources_size = f->get_32();
  1205. fw->store_32(ext_resources_size);
  1206. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1207. String type = get_ustring(f);
  1208. String path = get_ustring(f);
  1209. if (using_uids) {
  1210. ResourceUID::ID uid = f->get_64();
  1211. if (uid != ResourceUID::INVALID_ID) {
  1212. if (ResourceUID::get_singleton()->has_id(uid)) {
  1213. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  1214. path = ResourceUID::get_singleton()->get_id_path(uid);
  1215. }
  1216. }
  1217. }
  1218. bool relative = false;
  1219. if (!path.begins_with("res://")) {
  1220. path = local_path.path_join(path).simplify_path();
  1221. relative = true;
  1222. }
  1223. if (p_map.has(path)) {
  1224. String np = p_map[path];
  1225. path = np;
  1226. }
  1227. String full_path = path;
  1228. if (relative) {
  1229. //restore relative
  1230. path = local_path.path_to_file(path);
  1231. }
  1232. save_ustring(fw, type);
  1233. save_ustring(fw, path);
  1234. if (using_uids) {
  1235. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
  1236. fw->store_64(uid);
  1237. }
  1238. }
  1239. int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position();
  1240. //internal resources
  1241. uint32_t int_resources_size = f->get_32();
  1242. fw->store_32(int_resources_size);
  1243. for (uint32_t i = 0; i < int_resources_size; i++) {
  1244. String path = get_ustring(f);
  1245. uint64_t offset = f->get_64();
  1246. save_ustring(fw, path);
  1247. fw->store_64(offset + size_diff);
  1248. }
  1249. //rest of file
  1250. uint8_t b = f->get_8();
  1251. while (!f->eof_reached()) {
  1252. fw->store_8(b);
  1253. b = f->get_8();
  1254. }
  1255. f.unref();
  1256. bool all_ok = fw->get_error() == OK;
  1257. fw->seek(md_ofs);
  1258. fw->store_64(importmd_ofs + size_diff);
  1259. if (!all_ok) {
  1260. return ERR_CANT_CREATE;
  1261. }
  1262. fw.unref();
  1263. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1264. if (da->exists(p_path + ".depren")) {
  1265. da->remove(p_path);
  1266. da->rename(p_path + ".depren", p_path);
  1267. }
  1268. return OK;
  1269. }
  1270. void ResourceFormatLoaderBinary::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  1271. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1272. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'.");
  1273. ResourceLoaderBinary loader;
  1274. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1275. loader.res_path = loader.local_path;
  1276. loader.get_classes_used(f, r_classes);
  1277. }
  1278. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1279. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1280. if (f.is_null()) {
  1281. return ""; //could not read
  1282. }
  1283. ResourceLoaderBinary loader;
  1284. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1285. loader.res_path = loader.local_path;
  1286. String r = loader.recognize(f);
  1287. return ClassDB::get_compatibility_remapped_class(r);
  1288. }
  1289. String ResourceFormatLoaderBinary::get_resource_script_class(const String &p_path) const {
  1290. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1291. if (f.is_null()) {
  1292. return ""; //could not read
  1293. }
  1294. ResourceLoaderBinary loader;
  1295. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1296. loader.res_path = loader.local_path;
  1297. return loader.recognize_script_class(f);
  1298. }
  1299. ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const {
  1300. String ext = p_path.get_extension().to_lower();
  1301. if (!ClassDB::is_resource_extension(ext)) {
  1302. return ResourceUID::INVALID_ID;
  1303. }
  1304. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1305. if (f.is_null()) {
  1306. return ResourceUID::INVALID_ID; //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. loader.open(f, true);
  1312. if (loader.error != OK) {
  1313. return ResourceUID::INVALID_ID; //could not read
  1314. }
  1315. return loader.uid;
  1316. }
  1317. ///////////////////////////////////////////////////////////
  1318. ///////////////////////////////////////////////////////////
  1319. ///////////////////////////////////////////////////////////
  1320. void ResourceFormatSaverBinaryInstance::_pad_buffer(Ref<FileAccess> f, int p_bytes) {
  1321. int extra = 4 - (p_bytes % 4);
  1322. if (extra < 4) {
  1323. for (int i = 0; i < extra; i++) {
  1324. f->store_8(0); //pad to 32
  1325. }
  1326. }
  1327. }
  1328. 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) {
  1329. switch (p_property.get_type()) {
  1330. case Variant::NIL: {
  1331. f->store_32(VARIANT_NIL);
  1332. // don't store anything
  1333. } break;
  1334. case Variant::BOOL: {
  1335. f->store_32(VARIANT_BOOL);
  1336. bool val = p_property;
  1337. f->store_32(val);
  1338. } break;
  1339. case Variant::INT: {
  1340. int64_t val = p_property;
  1341. if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) {
  1342. f->store_32(VARIANT_INT64);
  1343. f->store_64(val);
  1344. } else {
  1345. f->store_32(VARIANT_INT);
  1346. f->store_32(int32_t(p_property));
  1347. }
  1348. } break;
  1349. case Variant::FLOAT: {
  1350. double d = p_property;
  1351. float fl = d;
  1352. if (double(fl) != d) {
  1353. f->store_32(VARIANT_DOUBLE);
  1354. f->store_double(d);
  1355. } else {
  1356. f->store_32(VARIANT_FLOAT);
  1357. f->store_real(fl);
  1358. }
  1359. } break;
  1360. case Variant::STRING: {
  1361. f->store_32(VARIANT_STRING);
  1362. String val = p_property;
  1363. save_unicode_string(f, val);
  1364. } break;
  1365. case Variant::VECTOR2: {
  1366. f->store_32(VARIANT_VECTOR2);
  1367. Vector2 val = p_property;
  1368. f->store_real(val.x);
  1369. f->store_real(val.y);
  1370. } break;
  1371. case Variant::VECTOR2I: {
  1372. f->store_32(VARIANT_VECTOR2I);
  1373. Vector2i val = p_property;
  1374. f->store_32(val.x);
  1375. f->store_32(val.y);
  1376. } break;
  1377. case Variant::RECT2: {
  1378. f->store_32(VARIANT_RECT2);
  1379. Rect2 val = p_property;
  1380. f->store_real(val.position.x);
  1381. f->store_real(val.position.y);
  1382. f->store_real(val.size.x);
  1383. f->store_real(val.size.y);
  1384. } break;
  1385. case Variant::RECT2I: {
  1386. f->store_32(VARIANT_RECT2I);
  1387. Rect2i val = p_property;
  1388. f->store_32(val.position.x);
  1389. f->store_32(val.position.y);
  1390. f->store_32(val.size.x);
  1391. f->store_32(val.size.y);
  1392. } break;
  1393. case Variant::VECTOR3: {
  1394. f->store_32(VARIANT_VECTOR3);
  1395. Vector3 val = p_property;
  1396. f->store_real(val.x);
  1397. f->store_real(val.y);
  1398. f->store_real(val.z);
  1399. } break;
  1400. case Variant::VECTOR3I: {
  1401. f->store_32(VARIANT_VECTOR3I);
  1402. Vector3i val = p_property;
  1403. f->store_32(val.x);
  1404. f->store_32(val.y);
  1405. f->store_32(val.z);
  1406. } break;
  1407. case Variant::VECTOR4: {
  1408. f->store_32(VARIANT_VECTOR4);
  1409. Vector4 val = p_property;
  1410. f->store_real(val.x);
  1411. f->store_real(val.y);
  1412. f->store_real(val.z);
  1413. f->store_real(val.w);
  1414. } break;
  1415. case Variant::VECTOR4I: {
  1416. f->store_32(VARIANT_VECTOR4I);
  1417. Vector4i val = p_property;
  1418. f->store_32(val.x);
  1419. f->store_32(val.y);
  1420. f->store_32(val.z);
  1421. f->store_32(val.w);
  1422. } break;
  1423. case Variant::PLANE: {
  1424. f->store_32(VARIANT_PLANE);
  1425. Plane val = p_property;
  1426. f->store_real(val.normal.x);
  1427. f->store_real(val.normal.y);
  1428. f->store_real(val.normal.z);
  1429. f->store_real(val.d);
  1430. } break;
  1431. case Variant::QUATERNION: {
  1432. f->store_32(VARIANT_QUATERNION);
  1433. Quaternion val = p_property;
  1434. f->store_real(val.x);
  1435. f->store_real(val.y);
  1436. f->store_real(val.z);
  1437. f->store_real(val.w);
  1438. } break;
  1439. case Variant::AABB: {
  1440. f->store_32(VARIANT_AABB);
  1441. AABB val = p_property;
  1442. f->store_real(val.position.x);
  1443. f->store_real(val.position.y);
  1444. f->store_real(val.position.z);
  1445. f->store_real(val.size.x);
  1446. f->store_real(val.size.y);
  1447. f->store_real(val.size.z);
  1448. } break;
  1449. case Variant::TRANSFORM2D: {
  1450. f->store_32(VARIANT_TRANSFORM2D);
  1451. Transform2D val = p_property;
  1452. f->store_real(val.columns[0].x);
  1453. f->store_real(val.columns[0].y);
  1454. f->store_real(val.columns[1].x);
  1455. f->store_real(val.columns[1].y);
  1456. f->store_real(val.columns[2].x);
  1457. f->store_real(val.columns[2].y);
  1458. } break;
  1459. case Variant::BASIS: {
  1460. f->store_32(VARIANT_BASIS);
  1461. Basis val = p_property;
  1462. f->store_real(val.rows[0].x);
  1463. f->store_real(val.rows[0].y);
  1464. f->store_real(val.rows[0].z);
  1465. f->store_real(val.rows[1].x);
  1466. f->store_real(val.rows[1].y);
  1467. f->store_real(val.rows[1].z);
  1468. f->store_real(val.rows[2].x);
  1469. f->store_real(val.rows[2].y);
  1470. f->store_real(val.rows[2].z);
  1471. } break;
  1472. case Variant::TRANSFORM3D: {
  1473. f->store_32(VARIANT_TRANSFORM3D);
  1474. Transform3D val = p_property;
  1475. f->store_real(val.basis.rows[0].x);
  1476. f->store_real(val.basis.rows[0].y);
  1477. f->store_real(val.basis.rows[0].z);
  1478. f->store_real(val.basis.rows[1].x);
  1479. f->store_real(val.basis.rows[1].y);
  1480. f->store_real(val.basis.rows[1].z);
  1481. f->store_real(val.basis.rows[2].x);
  1482. f->store_real(val.basis.rows[2].y);
  1483. f->store_real(val.basis.rows[2].z);
  1484. f->store_real(val.origin.x);
  1485. f->store_real(val.origin.y);
  1486. f->store_real(val.origin.z);
  1487. } break;
  1488. case Variant::PROJECTION: {
  1489. f->store_32(VARIANT_PROJECTION);
  1490. Projection val = p_property;
  1491. f->store_real(val.columns[0].x);
  1492. f->store_real(val.columns[0].y);
  1493. f->store_real(val.columns[0].z);
  1494. f->store_real(val.columns[0].w);
  1495. f->store_real(val.columns[1].x);
  1496. f->store_real(val.columns[1].y);
  1497. f->store_real(val.columns[1].z);
  1498. f->store_real(val.columns[1].w);
  1499. f->store_real(val.columns[2].x);
  1500. f->store_real(val.columns[2].y);
  1501. f->store_real(val.columns[2].z);
  1502. f->store_real(val.columns[2].w);
  1503. f->store_real(val.columns[3].x);
  1504. f->store_real(val.columns[3].y);
  1505. f->store_real(val.columns[3].z);
  1506. f->store_real(val.columns[3].w);
  1507. } break;
  1508. case Variant::COLOR: {
  1509. f->store_32(VARIANT_COLOR);
  1510. Color val = p_property;
  1511. // Color are always floats
  1512. f->store_float(val.r);
  1513. f->store_float(val.g);
  1514. f->store_float(val.b);
  1515. f->store_float(val.a);
  1516. } break;
  1517. case Variant::STRING_NAME: {
  1518. f->store_32(VARIANT_STRING_NAME);
  1519. String val = p_property;
  1520. save_unicode_string(f, val);
  1521. } break;
  1522. case Variant::NODE_PATH: {
  1523. f->store_32(VARIANT_NODE_PATH);
  1524. NodePath np = p_property;
  1525. f->store_16(np.get_name_count());
  1526. uint16_t snc = np.get_subname_count();
  1527. if (np.is_absolute()) {
  1528. snc |= 0x8000;
  1529. }
  1530. f->store_16(snc);
  1531. for (int i = 0; i < np.get_name_count(); i++) {
  1532. if (string_map.has(np.get_name(i))) {
  1533. f->store_32(string_map[np.get_name(i)]);
  1534. } else {
  1535. save_unicode_string(f, np.get_name(i), true);
  1536. }
  1537. }
  1538. for (int i = 0; i < np.get_subname_count(); i++) {
  1539. if (string_map.has(np.get_subname(i))) {
  1540. f->store_32(string_map[np.get_subname(i)]);
  1541. } else {
  1542. save_unicode_string(f, np.get_subname(i), true);
  1543. }
  1544. }
  1545. } break;
  1546. case Variant::RID: {
  1547. f->store_32(VARIANT_RID);
  1548. WARN_PRINT("Can't save RIDs.");
  1549. RID val = p_property;
  1550. f->store_32(val.get_id());
  1551. } break;
  1552. case Variant::OBJECT: {
  1553. f->store_32(VARIANT_OBJECT);
  1554. Ref<Resource> res = p_property;
  1555. if (res.is_null() || res->get_meta(SNAME("_skip_save_"), false)) {
  1556. f->store_32(OBJECT_EMPTY);
  1557. return; // Don't save it.
  1558. }
  1559. if (!res->is_built_in()) {
  1560. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1561. f->store_32(external_resources[res]);
  1562. } else {
  1563. if (!resource_map.has(res)) {
  1564. f->store_32(OBJECT_EMPTY);
  1565. ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference.");
  1566. }
  1567. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1568. f->store_32(resource_map[res]);
  1569. //internal resource
  1570. }
  1571. } break;
  1572. case Variant::CALLABLE: {
  1573. f->store_32(VARIANT_CALLABLE);
  1574. WARN_PRINT("Can't save Callables.");
  1575. } break;
  1576. case Variant::SIGNAL: {
  1577. f->store_32(VARIANT_SIGNAL);
  1578. WARN_PRINT("Can't save Signals.");
  1579. } break;
  1580. case Variant::DICTIONARY: {
  1581. f->store_32(VARIANT_DICTIONARY);
  1582. Dictionary d = p_property;
  1583. f->store_32(uint32_t(d.size()));
  1584. List<Variant> keys;
  1585. d.get_key_list(&keys);
  1586. for (const Variant &E : keys) {
  1587. write_variant(f, E, resource_map, external_resources, string_map);
  1588. write_variant(f, d[E], resource_map, external_resources, string_map);
  1589. }
  1590. } break;
  1591. case Variant::ARRAY: {
  1592. f->store_32(VARIANT_ARRAY);
  1593. Array a = p_property;
  1594. f->store_32(uint32_t(a.size()));
  1595. for (const Variant &var : a) {
  1596. write_variant(f, var, resource_map, external_resources, string_map);
  1597. }
  1598. } break;
  1599. case Variant::PACKED_BYTE_ARRAY: {
  1600. f->store_32(VARIANT_PACKED_BYTE_ARRAY);
  1601. Vector<uint8_t> arr = p_property;
  1602. int len = arr.size();
  1603. f->store_32(len);
  1604. const uint8_t *r = arr.ptr();
  1605. f->store_buffer(r, len);
  1606. _pad_buffer(f, len);
  1607. } break;
  1608. case Variant::PACKED_INT32_ARRAY: {
  1609. f->store_32(VARIANT_PACKED_INT32_ARRAY);
  1610. Vector<int32_t> arr = p_property;
  1611. int len = arr.size();
  1612. f->store_32(len);
  1613. const int32_t *r = arr.ptr();
  1614. for (int i = 0; i < len; i++) {
  1615. f->store_32(r[i]);
  1616. }
  1617. } break;
  1618. case Variant::PACKED_INT64_ARRAY: {
  1619. f->store_32(VARIANT_PACKED_INT64_ARRAY);
  1620. Vector<int64_t> arr = p_property;
  1621. int len = arr.size();
  1622. f->store_32(len);
  1623. const int64_t *r = arr.ptr();
  1624. for (int i = 0; i < len; i++) {
  1625. f->store_64(r[i]);
  1626. }
  1627. } break;
  1628. case Variant::PACKED_FLOAT32_ARRAY: {
  1629. f->store_32(VARIANT_PACKED_FLOAT32_ARRAY);
  1630. Vector<float> arr = p_property;
  1631. int len = arr.size();
  1632. f->store_32(len);
  1633. const float *r = arr.ptr();
  1634. for (int i = 0; i < len; i++) {
  1635. f->store_float(r[i]);
  1636. }
  1637. } break;
  1638. case Variant::PACKED_FLOAT64_ARRAY: {
  1639. f->store_32(VARIANT_PACKED_FLOAT64_ARRAY);
  1640. Vector<double> arr = p_property;
  1641. int len = arr.size();
  1642. f->store_32(len);
  1643. const double *r = arr.ptr();
  1644. for (int i = 0; i < len; i++) {
  1645. f->store_double(r[i]);
  1646. }
  1647. } break;
  1648. case Variant::PACKED_STRING_ARRAY: {
  1649. f->store_32(VARIANT_PACKED_STRING_ARRAY);
  1650. Vector<String> arr = p_property;
  1651. int len = arr.size();
  1652. f->store_32(len);
  1653. const String *r = arr.ptr();
  1654. for (int i = 0; i < len; i++) {
  1655. save_unicode_string(f, r[i]);
  1656. }
  1657. } break;
  1658. case Variant::PACKED_VECTOR2_ARRAY: {
  1659. f->store_32(VARIANT_PACKED_VECTOR2_ARRAY);
  1660. Vector<Vector2> arr = p_property;
  1661. int len = arr.size();
  1662. f->store_32(len);
  1663. const Vector2 *r = arr.ptr();
  1664. for (int i = 0; i < len; i++) {
  1665. f->store_real(r[i].x);
  1666. f->store_real(r[i].y);
  1667. }
  1668. } break;
  1669. case Variant::PACKED_VECTOR3_ARRAY: {
  1670. f->store_32(VARIANT_PACKED_VECTOR3_ARRAY);
  1671. Vector<Vector3> arr = p_property;
  1672. int len = arr.size();
  1673. f->store_32(len);
  1674. const Vector3 *r = arr.ptr();
  1675. for (int i = 0; i < len; i++) {
  1676. f->store_real(r[i].x);
  1677. f->store_real(r[i].y);
  1678. f->store_real(r[i].z);
  1679. }
  1680. } break;
  1681. case Variant::PACKED_COLOR_ARRAY: {
  1682. f->store_32(VARIANT_PACKED_COLOR_ARRAY);
  1683. Vector<Color> arr = p_property;
  1684. int len = arr.size();
  1685. f->store_32(len);
  1686. const Color *r = arr.ptr();
  1687. for (int i = 0; i < len; i++) {
  1688. f->store_float(r[i].r);
  1689. f->store_float(r[i].g);
  1690. f->store_float(r[i].b);
  1691. f->store_float(r[i].a);
  1692. }
  1693. } break;
  1694. case Variant::PACKED_VECTOR4_ARRAY: {
  1695. f->store_32(VARIANT_PACKED_VECTOR4_ARRAY);
  1696. Vector<Vector4> arr = p_property;
  1697. int len = arr.size();
  1698. f->store_32(len);
  1699. const Vector4 *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. f->store_real(r[i].w);
  1705. }
  1706. } break;
  1707. default: {
  1708. ERR_FAIL_MSG("Invalid variant.");
  1709. }
  1710. }
  1711. }
  1712. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1713. switch (p_variant.get_type()) {
  1714. case Variant::OBJECT: {
  1715. Ref<Resource> res = p_variant;
  1716. if (res.is_null() || external_resources.has(res) || res->get_meta(SNAME("_skip_save_"), false)) {
  1717. return;
  1718. }
  1719. if (!p_main && (!bundle_resources) && !res->is_built_in()) {
  1720. if (res->get_path() == path) {
  1721. ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
  1722. return;
  1723. }
  1724. int idx = external_resources.size();
  1725. external_resources[res] = idx;
  1726. return;
  1727. }
  1728. if (resource_set.has(res)) {
  1729. return;
  1730. }
  1731. resource_set.insert(res);
  1732. List<PropertyInfo> property_list;
  1733. res->get_property_list(&property_list);
  1734. for (const PropertyInfo &E : property_list) {
  1735. if (E.usage & PROPERTY_USAGE_STORAGE) {
  1736. Variant value = res->get(E.name);
  1737. if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1738. NonPersistentKey npk;
  1739. npk.base = res;
  1740. npk.property = E.name;
  1741. non_persistent_map[npk] = value;
  1742. Ref<Resource> sres = value;
  1743. if (sres.is_valid()) {
  1744. resource_set.insert(sres);
  1745. saved_resources.push_back(sres);
  1746. } else {
  1747. _find_resources(value);
  1748. }
  1749. } else {
  1750. _find_resources(value);
  1751. }
  1752. }
  1753. }
  1754. saved_resources.push_back(res);
  1755. } break;
  1756. case Variant::ARRAY: {
  1757. Array varray = p_variant;
  1758. _find_resources(varray.get_typed_script());
  1759. for (const Variant &v : varray) {
  1760. _find_resources(v);
  1761. }
  1762. } break;
  1763. case Variant::DICTIONARY: {
  1764. Dictionary d = p_variant;
  1765. List<Variant> keys;
  1766. d.get_key_list(&keys);
  1767. for (const Variant &E : keys) {
  1768. _find_resources(E);
  1769. Variant v = d[E];
  1770. _find_resources(v);
  1771. }
  1772. } break;
  1773. case Variant::NODE_PATH: {
  1774. //take the chance and save node path strings
  1775. NodePath np = p_variant;
  1776. for (int i = 0; i < np.get_name_count(); i++) {
  1777. get_string_index(np.get_name(i));
  1778. }
  1779. for (int i = 0; i < np.get_subname_count(); i++) {
  1780. get_string_index(np.get_subname(i));
  1781. }
  1782. } break;
  1783. default: {
  1784. }
  1785. }
  1786. }
  1787. void ResourceFormatSaverBinaryInstance::save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len) {
  1788. CharString utf8 = p_string.utf8();
  1789. if (p_bit_on_len) {
  1790. p_f->store_32((utf8.length() + 1) | 0x80000000);
  1791. } else {
  1792. p_f->store_32(utf8.length() + 1);
  1793. }
  1794. p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1795. }
  1796. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1797. StringName s = p_string;
  1798. if (string_map.has(s)) {
  1799. return string_map[s];
  1800. }
  1801. string_map[s] = strings.size();
  1802. strings.push_back(s);
  1803. return strings.size() - 1;
  1804. }
  1805. static String _resource_get_class(Ref<Resource> p_resource) {
  1806. Ref<MissingResource> missing_resource = p_resource;
  1807. if (missing_resource.is_valid()) {
  1808. return missing_resource->get_original_class();
  1809. } else {
  1810. return p_resource->get_class();
  1811. }
  1812. }
  1813. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) {
  1814. Error err;
  1815. Ref<FileAccess> f;
  1816. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1817. Ref<FileAccessCompressed> fac;
  1818. fac.instantiate();
  1819. fac->configure("RSCC");
  1820. f = fac;
  1821. err = fac->open_internal(p_path, FileAccess::WRITE);
  1822. } else {
  1823. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1824. }
  1825. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'.");
  1826. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1827. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1828. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1829. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1830. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1831. if (!p_path.begins_with("res://")) {
  1832. takeover_paths = false;
  1833. }
  1834. local_path = p_path.get_base_dir();
  1835. path = ProjectSettings::get_singleton()->localize_path(p_path);
  1836. _find_resources(p_resource, true);
  1837. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1838. //save header compressed
  1839. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1840. f->store_buffer(header, 4);
  1841. }
  1842. if (big_endian) {
  1843. f->store_32(1);
  1844. f->set_big_endian(true);
  1845. } else {
  1846. f->store_32(0);
  1847. }
  1848. f->store_32(0); //64 bits file, false for now
  1849. f->store_32(VERSION_MAJOR);
  1850. f->store_32(VERSION_MINOR);
  1851. f->store_32(FORMAT_VERSION);
  1852. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1853. return ERR_CANT_CREATE;
  1854. }
  1855. save_unicode_string(f, _resource_get_class(p_resource));
  1856. f->store_64(0); //offset to import metadata
  1857. String script_class;
  1858. {
  1859. uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS;
  1860. #ifdef REAL_T_IS_DOUBLE
  1861. format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE;
  1862. #endif
  1863. if (!p_resource->is_class("PackedScene")) {
  1864. Ref<Script> s = p_resource->get_script();
  1865. if (s.is_valid()) {
  1866. script_class = s->get_global_name();
  1867. if (!script_class.is_empty()) {
  1868. format_flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS;
  1869. }
  1870. }
  1871. }
  1872. f->store_32(format_flags);
  1873. }
  1874. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true);
  1875. f->store_64(uid);
  1876. if (!script_class.is_empty()) {
  1877. save_unicode_string(f, script_class);
  1878. }
  1879. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  1880. f->store_32(0); // reserved
  1881. }
  1882. List<ResourceData> resources;
  1883. Dictionary missing_resource_properties = p_resource->get_meta(META_MISSING_RESOURCES, Dictionary());
  1884. {
  1885. for (const Ref<Resource> &E : saved_resources) {
  1886. ResourceData &rd = resources.push_back(ResourceData())->get();
  1887. rd.type = _resource_get_class(E);
  1888. List<PropertyInfo> property_list;
  1889. E->get_property_list(&property_list);
  1890. for (const PropertyInfo &F : property_list) {
  1891. if (skip_editor && F.name.begins_with("__editor")) {
  1892. continue;
  1893. }
  1894. if (F.name == META_PROPERTY_MISSING_RESOURCES) {
  1895. continue;
  1896. }
  1897. if ((F.usage & PROPERTY_USAGE_STORAGE)) {
  1898. Property p;
  1899. p.name_idx = get_string_index(F.name);
  1900. if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1901. NonPersistentKey npk;
  1902. npk.base = E;
  1903. npk.property = F.name;
  1904. if (non_persistent_map.has(npk)) {
  1905. p.value = non_persistent_map[npk];
  1906. }
  1907. } else {
  1908. p.value = E->get(F.name);
  1909. }
  1910. if (p.pi.type == Variant::OBJECT && missing_resource_properties.has(F.name)) {
  1911. // Was this missing resource overridden? If so do not save the old value.
  1912. Ref<Resource> res = p.value;
  1913. if (res.is_null()) {
  1914. p.value = missing_resource_properties[F.name];
  1915. }
  1916. }
  1917. Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name);
  1918. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) {
  1919. continue;
  1920. }
  1921. p.pi = F;
  1922. rd.properties.push_back(p);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. f->store_32(strings.size()); //string table size
  1928. for (int i = 0; i < strings.size(); i++) {
  1929. save_unicode_string(f, strings[i]);
  1930. }
  1931. // save external resource table
  1932. f->store_32(external_resources.size()); //amount of external resources
  1933. Vector<Ref<Resource>> save_order;
  1934. save_order.resize(external_resources.size());
  1935. for (const KeyValue<Ref<Resource>, int> &E : external_resources) {
  1936. save_order.write[E.value] = E.key;
  1937. }
  1938. for (int i = 0; i < save_order.size(); i++) {
  1939. save_unicode_string(f, save_order[i]->get_save_class());
  1940. String res_path = save_order[i]->get_path();
  1941. res_path = relative_paths ? local_path.path_to_file(res_path) : res_path;
  1942. save_unicode_string(f, res_path);
  1943. ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false);
  1944. f->store_64(ruid);
  1945. }
  1946. // save internal resource table
  1947. f->store_32(saved_resources.size()); //amount of internal resources
  1948. Vector<uint64_t> ofs_pos;
  1949. HashSet<String> used_unique_ids;
  1950. for (Ref<Resource> &r : saved_resources) {
  1951. if (r->is_built_in()) {
  1952. if (!r->get_scene_unique_id().is_empty()) {
  1953. if (used_unique_ids.has(r->get_scene_unique_id())) {
  1954. r->set_scene_unique_id("");
  1955. } else {
  1956. used_unique_ids.insert(r->get_scene_unique_id());
  1957. }
  1958. }
  1959. }
  1960. }
  1961. HashMap<Ref<Resource>, int> resource_map;
  1962. int res_index = 0;
  1963. for (Ref<Resource> &r : saved_resources) {
  1964. if (r->is_built_in()) {
  1965. if (r->get_scene_unique_id().is_empty()) {
  1966. String new_id;
  1967. while (true) {
  1968. new_id = _resource_get_class(r) + "_" + Resource::generate_scene_unique_id();
  1969. if (!used_unique_ids.has(new_id)) {
  1970. break;
  1971. }
  1972. }
  1973. r->set_scene_unique_id(new_id);
  1974. used_unique_ids.insert(new_id);
  1975. }
  1976. save_unicode_string(f, "local://" + r->get_scene_unique_id());
  1977. if (takeover_paths) {
  1978. r->set_path(p_path + "::" + r->get_scene_unique_id(), true);
  1979. }
  1980. #ifdef TOOLS_ENABLED
  1981. r->set_edited(false);
  1982. #endif
  1983. } else {
  1984. save_unicode_string(f, r->get_path()); //actual external
  1985. }
  1986. ofs_pos.push_back(f->get_position());
  1987. f->store_64(0); //offset in 64 bits
  1988. resource_map[r] = res_index++;
  1989. }
  1990. Vector<uint64_t> ofs_table;
  1991. //now actually save the resources
  1992. for (const ResourceData &rd : resources) {
  1993. ofs_table.push_back(f->get_position());
  1994. save_unicode_string(f, rd.type);
  1995. f->store_32(rd.properties.size());
  1996. for (const Property &p : rd.properties) {
  1997. f->store_32(p.name_idx);
  1998. write_variant(f, p.value, resource_map, external_resources, string_map, p.pi);
  1999. }
  2000. }
  2001. for (int i = 0; i < ofs_table.size(); i++) {
  2002. f->seek(ofs_pos[i]);
  2003. f->store_64(ofs_table[i]);
  2004. }
  2005. f->seek_end();
  2006. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  2007. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  2008. return ERR_CANT_CREATE;
  2009. }
  2010. return OK;
  2011. }
  2012. Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  2013. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  2014. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'.");
  2015. Ref<FileAccess> fw;
  2016. local_path = p_path.get_base_dir();
  2017. uint8_t header[4];
  2018. f->get_buffer(header, 4);
  2019. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  2020. // Compressed.
  2021. Ref<FileAccessCompressed> fac;
  2022. fac.instantiate();
  2023. Error err = fac->open_after_magic(f);
  2024. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  2025. f = fac;
  2026. Ref<FileAccessCompressed> facw;
  2027. facw.instantiate();
  2028. facw->configure("RSCC");
  2029. err = facw->open_internal(p_path + ".uidren", FileAccess::WRITE);
  2030. ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".uidren'.");
  2031. fw = facw;
  2032. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  2033. // Not a binary resource.
  2034. return ERR_FILE_UNRECOGNIZED;
  2035. } else {
  2036. fw = FileAccess::open(p_path + ".uidren", FileAccess::WRITE);
  2037. ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".uidren'.");
  2038. uint8_t magich[4] = { 'R', 'S', 'R', 'C' };
  2039. fw->store_buffer(magich, 4);
  2040. }
  2041. big_endian = f->get_32();
  2042. bool use_real64 = f->get_32();
  2043. f->set_big_endian(big_endian != 0); //read big endian if saved as big endian
  2044. #ifdef BIG_ENDIAN_ENABLED
  2045. fw->store_32(!big_endian);
  2046. #else
  2047. fw->store_32(big_endian);
  2048. #endif
  2049. fw->set_big_endian(big_endian != 0);
  2050. fw->store_32(use_real64); //use real64
  2051. uint32_t ver_major = f->get_32();
  2052. uint32_t ver_minor = f->get_32();
  2053. uint32_t ver_format = f->get_32();
  2054. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  2055. fw.unref();
  2056. {
  2057. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2058. da->remove(p_path + ".uidren");
  2059. }
  2060. // Use the old approach.
  2061. WARN_PRINT("This file is old, so it does not support UIDs, opening and resaving '" + p_path + "'.");
  2062. return ERR_UNAVAILABLE;
  2063. }
  2064. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  2065. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED,
  2066. 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).",
  2067. local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH));
  2068. }
  2069. // Since we're not actually converting the file contents, leave the version
  2070. // numbers in the file untouched.
  2071. fw->store_32(ver_major);
  2072. fw->store_32(ver_minor);
  2073. fw->store_32(ver_format);
  2074. save_ustring(fw, get_ustring(f)); //type
  2075. fw->store_64(f->get_64()); //metadata offset
  2076. uint32_t flags = f->get_32();
  2077. flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS;
  2078. f->get_64(); // Skip previous UID
  2079. fw->store_32(flags);
  2080. fw->store_64(p_uid);
  2081. if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
  2082. save_ustring(fw, get_ustring(f));
  2083. }
  2084. //rest of file
  2085. uint8_t b = f->get_8();
  2086. while (!f->eof_reached()) {
  2087. fw->store_8(b);
  2088. b = f->get_8();
  2089. }
  2090. f.unref();
  2091. bool all_ok = fw->get_error() == OK;
  2092. if (!all_ok) {
  2093. return ERR_CANT_CREATE;
  2094. }
  2095. fw.unref();
  2096. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2097. da->remove(p_path);
  2098. da->rename(p_path + ".uidren", p_path);
  2099. return OK;
  2100. }
  2101. Error ResourceFormatSaverBinary::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  2102. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  2103. ResourceFormatSaverBinaryInstance saver;
  2104. return saver.save(local_path, p_resource, p_flags);
  2105. }
  2106. Error ResourceFormatSaverBinary::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  2107. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  2108. ResourceFormatSaverBinaryInstance saver;
  2109. return saver.set_uid(local_path, p_uid);
  2110. }
  2111. bool ResourceFormatSaverBinary::recognize(const Ref<Resource> &p_resource) const {
  2112. return true; //all recognized
  2113. }
  2114. void ResourceFormatSaverBinary::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  2115. String base = p_resource->get_base_extension().to_lower();
  2116. p_extensions->push_back(base);
  2117. if (base != "res") {
  2118. p_extensions->push_back("res");
  2119. }
  2120. }
  2121. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr;
  2122. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  2123. singleton = this;
  2124. }