resource_format_binary.cpp 67 KB

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