resource_format_binary.cpp 69 KB

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