resource_format_binary.cpp 55 KB

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