resource_format_binary.cpp 60 KB

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