resource_format_binary.cpp 63 KB

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