resource_format_binary.cpp 55 KB

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