resource_format_binary.cpp 69 KB

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