resource_format_binary.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "globals.h"
  32. #include "io/file_access_compressed.h"
  33. #include "io/marshalls.h"
  34. #include "os/dir_access.h"
  35. #include "version.h"
  36. //#define print_bl(m_what) print_line(m_what)
  37. #define print_bl(m_what)
  38. enum {
  39. //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization)
  40. VARIANT_NIL = 1,
  41. VARIANT_BOOL = 2,
  42. VARIANT_INT = 3,
  43. VARIANT_REAL = 4,
  44. VARIANT_STRING = 5,
  45. VARIANT_VECTOR2 = 10,
  46. VARIANT_RECT2 = 11,
  47. VARIANT_VECTOR3 = 12,
  48. VARIANT_PLANE = 13,
  49. VARIANT_QUAT = 14,
  50. VARIANT_AABB = 15,
  51. VARIANT_MATRIX3 = 16,
  52. VARIANT_TRANSFORM = 17,
  53. VARIANT_MATRIX32 = 18,
  54. VARIANT_COLOR = 20,
  55. VARIANT_IMAGE = 21,
  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_RAW_ARRAY = 31,
  63. VARIANT_INT_ARRAY = 32,
  64. VARIANT_REAL_ARRAY = 33,
  65. VARIANT_STRING_ARRAY = 34,
  66. VARIANT_VECTOR3_ARRAY = 35,
  67. VARIANT_COLOR_ARRAY = 36,
  68. VARIANT_VECTOR2_ARRAY = 37,
  69. IMAGE_ENCODING_EMPTY = 0,
  70. IMAGE_ENCODING_RAW = 1,
  71. IMAGE_ENCODING_LOSSLESS = 2,
  72. IMAGE_ENCODING_LOSSY = 3,
  73. IMAGE_FORMAT_GRAYSCALE = 0,
  74. IMAGE_FORMAT_INTENSITY = 1,
  75. IMAGE_FORMAT_GRAYSCALE_ALPHA = 2,
  76. IMAGE_FORMAT_RGB = 3,
  77. IMAGE_FORMAT_RGBA = 4,
  78. IMAGE_FORMAT_INDEXED = 5,
  79. IMAGE_FORMAT_INDEXED_ALPHA = 6,
  80. IMAGE_FORMAT_BC1 = 7,
  81. IMAGE_FORMAT_BC2 = 8,
  82. IMAGE_FORMAT_BC3 = 9,
  83. IMAGE_FORMAT_BC4 = 10,
  84. IMAGE_FORMAT_BC5 = 11,
  85. IMAGE_FORMAT_PVRTC2 = 12,
  86. IMAGE_FORMAT_PVRTC2_ALPHA = 13,
  87. IMAGE_FORMAT_PVRTC4 = 14,
  88. IMAGE_FORMAT_PVRTC4_ALPHA = 15,
  89. IMAGE_FORMAT_ETC = 16,
  90. IMAGE_FORMAT_ATC = 17,
  91. IMAGE_FORMAT_ATC_ALPHA_EXPLICIT = 18,
  92. IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED = 19,
  93. IMAGE_FORMAT_CUSTOM = 30,
  94. OBJECT_EMPTY = 0,
  95. OBJECT_EXTERNAL_RESOURCE = 1,
  96. OBJECT_INTERNAL_RESOURCE = 2,
  97. OBJECT_EXTERNAL_RESOURCE_INDEX = 3,
  98. FORMAT_VERSION = 1,
  99. FORMAT_VERSION_CAN_RENAME_DEPS = 1
  100. };
  101. void ResourceInteractiveLoaderBinary::_advance_padding(uint32_t p_len) {
  102. uint32_t extra = 4 - (p_len % 4);
  103. if (extra < 4) {
  104. for (uint32_t i = 0; i < extra; i++)
  105. f->get_8(); //pad to 32
  106. }
  107. }
  108. Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v, bool p_for_export_data) {
  109. uint32_t type = f->get_32();
  110. print_bl("find property of type: " + itos(type));
  111. switch (type) {
  112. case VARIANT_NIL: {
  113. r_v = Variant();
  114. } break;
  115. case VARIANT_BOOL: {
  116. r_v = bool(f->get_32());
  117. } break;
  118. case VARIANT_INT: {
  119. r_v = int(f->get_32());
  120. } break;
  121. case VARIANT_REAL: {
  122. r_v = f->get_real();
  123. } break;
  124. case VARIANT_STRING: {
  125. r_v = get_unicode_string();
  126. } break;
  127. case VARIANT_VECTOR2: {
  128. Vector2 v;
  129. v.x = f->get_real();
  130. v.y = f->get_real();
  131. r_v = v;
  132. } break;
  133. case VARIANT_RECT2: {
  134. Rect2 v;
  135. v.pos.x = f->get_real();
  136. v.pos.y = f->get_real();
  137. v.size.x = f->get_real();
  138. v.size.y = f->get_real();
  139. r_v = v;
  140. } break;
  141. case VARIANT_VECTOR3: {
  142. Vector3 v;
  143. v.x = f->get_real();
  144. v.y = f->get_real();
  145. v.z = f->get_real();
  146. r_v = v;
  147. } break;
  148. case VARIANT_PLANE: {
  149. Plane v;
  150. v.normal.x = f->get_real();
  151. v.normal.y = f->get_real();
  152. v.normal.z = f->get_real();
  153. v.d = f->get_real();
  154. r_v = v;
  155. } break;
  156. case VARIANT_QUAT: {
  157. Quat v;
  158. v.x = f->get_real();
  159. v.y = f->get_real();
  160. v.z = f->get_real();
  161. v.w = f->get_real();
  162. r_v = v;
  163. } break;
  164. case VARIANT_AABB: {
  165. AABB v;
  166. v.pos.x = f->get_real();
  167. v.pos.y = f->get_real();
  168. v.pos.z = f->get_real();
  169. v.size.x = f->get_real();
  170. v.size.y = f->get_real();
  171. v.size.z = f->get_real();
  172. r_v = v;
  173. } break;
  174. case VARIANT_MATRIX32: {
  175. Matrix32 v;
  176. v.elements[0].x = f->get_real();
  177. v.elements[0].y = f->get_real();
  178. v.elements[1].x = f->get_real();
  179. v.elements[1].y = f->get_real();
  180. v.elements[2].x = f->get_real();
  181. v.elements[2].y = f->get_real();
  182. r_v = v;
  183. } break;
  184. case VARIANT_MATRIX3: {
  185. Matrix3 v;
  186. v.elements[0].x = f->get_real();
  187. v.elements[0].y = f->get_real();
  188. v.elements[0].z = f->get_real();
  189. v.elements[1].x = f->get_real();
  190. v.elements[1].y = f->get_real();
  191. v.elements[1].z = f->get_real();
  192. v.elements[2].x = f->get_real();
  193. v.elements[2].y = f->get_real();
  194. v.elements[2].z = f->get_real();
  195. r_v = v;
  196. } break;
  197. case VARIANT_TRANSFORM: {
  198. Transform v;
  199. v.basis.elements[0].x = f->get_real();
  200. v.basis.elements[0].y = f->get_real();
  201. v.basis.elements[0].z = f->get_real();
  202. v.basis.elements[1].x = f->get_real();
  203. v.basis.elements[1].y = f->get_real();
  204. v.basis.elements[1].z = f->get_real();
  205. v.basis.elements[2].x = f->get_real();
  206. v.basis.elements[2].y = f->get_real();
  207. v.basis.elements[2].z = f->get_real();
  208. v.origin.x = f->get_real();
  209. v.origin.y = f->get_real();
  210. v.origin.z = f->get_real();
  211. r_v = v;
  212. } break;
  213. case VARIANT_COLOR: {
  214. Color v;
  215. v.r = f->get_real();
  216. v.g = f->get_real();
  217. v.b = f->get_real();
  218. v.a = f->get_real();
  219. r_v = v;
  220. } break;
  221. case VARIANT_IMAGE: {
  222. uint32_t encoding = f->get_32();
  223. if (encoding == IMAGE_ENCODING_EMPTY) {
  224. r_v = Variant();
  225. break;
  226. } else if (encoding == IMAGE_ENCODING_RAW) {
  227. uint32_t width = f->get_32();
  228. uint32_t height = f->get_32();
  229. uint32_t mipmaps = f->get_32();
  230. uint32_t format = f->get_32();
  231. Image::Format fmt;
  232. switch (format) {
  233. case IMAGE_FORMAT_GRAYSCALE: {
  234. fmt = Image::FORMAT_GRAYSCALE;
  235. } break;
  236. case IMAGE_FORMAT_INTENSITY: {
  237. fmt = Image::FORMAT_INTENSITY;
  238. } break;
  239. case IMAGE_FORMAT_GRAYSCALE_ALPHA: {
  240. fmt = Image::FORMAT_GRAYSCALE_ALPHA;
  241. } break;
  242. case IMAGE_FORMAT_RGB: {
  243. fmt = Image::FORMAT_RGB;
  244. } break;
  245. case IMAGE_FORMAT_RGBA: {
  246. fmt = Image::FORMAT_RGBA;
  247. } break;
  248. case IMAGE_FORMAT_INDEXED: {
  249. fmt = Image::FORMAT_INDEXED;
  250. } break;
  251. case IMAGE_FORMAT_INDEXED_ALPHA: {
  252. fmt = Image::FORMAT_INDEXED_ALPHA;
  253. } break;
  254. case IMAGE_FORMAT_BC1: {
  255. fmt = Image::FORMAT_BC1;
  256. } break;
  257. case IMAGE_FORMAT_BC2: {
  258. fmt = Image::FORMAT_BC2;
  259. } break;
  260. case IMAGE_FORMAT_BC3: {
  261. fmt = Image::FORMAT_BC3;
  262. } break;
  263. case IMAGE_FORMAT_BC4: {
  264. fmt = Image::FORMAT_BC4;
  265. } break;
  266. case IMAGE_FORMAT_BC5: {
  267. fmt = Image::FORMAT_BC5;
  268. } break;
  269. case IMAGE_FORMAT_PVRTC2: {
  270. fmt = Image::FORMAT_PVRTC2;
  271. } break;
  272. case IMAGE_FORMAT_PVRTC2_ALPHA: {
  273. fmt = Image::FORMAT_PVRTC2_ALPHA;
  274. } break;
  275. case IMAGE_FORMAT_PVRTC4: {
  276. fmt = Image::FORMAT_PVRTC4;
  277. } break;
  278. case IMAGE_FORMAT_PVRTC4_ALPHA: {
  279. fmt = Image::FORMAT_PVRTC4_ALPHA;
  280. } break;
  281. case IMAGE_FORMAT_ETC: {
  282. fmt = Image::FORMAT_ETC;
  283. } break;
  284. case IMAGE_FORMAT_ATC: {
  285. fmt = Image::FORMAT_ATC;
  286. } break;
  287. case IMAGE_FORMAT_ATC_ALPHA_EXPLICIT: {
  288. fmt = Image::FORMAT_ATC_ALPHA_EXPLICIT;
  289. } break;
  290. case IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED: {
  291. fmt = Image::FORMAT_ATC_ALPHA_INTERPOLATED;
  292. } break;
  293. case IMAGE_FORMAT_CUSTOM: {
  294. fmt = Image::FORMAT_CUSTOM;
  295. } break;
  296. default: {
  297. ERR_FAIL_V(ERR_FILE_CORRUPT);
  298. }
  299. }
  300. uint32_t datalen = f->get_32();
  301. DVector<uint8_t> imgdata;
  302. imgdata.resize(datalen);
  303. DVector<uint8_t>::Write w = imgdata.write();
  304. f->get_buffer(w.ptr(), datalen);
  305. _advance_padding(datalen);
  306. w = DVector<uint8_t>::Write();
  307. r_v = Image(width, height, mipmaps, fmt, imgdata);
  308. } else {
  309. //compressed
  310. DVector<uint8_t> data;
  311. data.resize(f->get_32());
  312. DVector<uint8_t>::Write w = data.write();
  313. f->get_buffer(w.ptr(), data.size());
  314. w = DVector<uint8_t>::Write();
  315. Image img;
  316. if (encoding == IMAGE_ENCODING_LOSSY && Image::lossy_unpacker) {
  317. img = Image::lossy_unpacker(data);
  318. } else if (encoding == IMAGE_ENCODING_LOSSLESS && Image::lossless_unpacker) {
  319. img = Image::lossless_unpacker(data);
  320. }
  321. _advance_padding(data.size());
  322. r_v = img;
  323. }
  324. } break;
  325. case VARIANT_NODE_PATH: {
  326. Vector<StringName> names;
  327. Vector<StringName> subnames;
  328. StringName property;
  329. bool absolute;
  330. int name_count = f->get_16();
  331. uint32_t subname_count = f->get_16();
  332. absolute = subname_count & 0x8000;
  333. subname_count &= 0x7FFF;
  334. for (int i = 0; i < name_count; i++)
  335. names.push_back(string_map[f->get_32()]);
  336. for (uint32_t i = 0; i < subname_count; i++)
  337. subnames.push_back(string_map[f->get_32()]);
  338. property = string_map[f->get_32()];
  339. NodePath np = NodePath(names, subnames, absolute, property);
  340. //print_line("got path: "+String(np));
  341. r_v = np;
  342. } break;
  343. case VARIANT_RID: {
  344. r_v = f->get_32();
  345. } break;
  346. case VARIANT_OBJECT: {
  347. uint32_t type = f->get_32();
  348. switch (type) {
  349. case OBJECT_EMPTY: {
  350. //do none
  351. } break;
  352. case OBJECT_INTERNAL_RESOURCE: {
  353. uint32_t index = f->get_32();
  354. if (p_for_export_data) {
  355. r_v = "@RESLOCAL:" + itos(index);
  356. } else {
  357. String path = res_path + "::" + itos(index);
  358. RES res = ResourceLoader::load(path);
  359. if (res.is_null()) {
  360. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  361. }
  362. r_v = res;
  363. }
  364. } break;
  365. case OBJECT_EXTERNAL_RESOURCE: {
  366. //old file format, still around for compatibility
  367. String type = get_unicode_string();
  368. String path = get_unicode_string();
  369. if (p_for_export_data) {
  370. r_v = "@RESPATH:" + type + ":" + path;
  371. } else {
  372. if (path.find("://") == -1 && path.is_rel_path()) {
  373. // path is relative to file being loaded, so convert to a resource path
  374. path = Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  375. }
  376. if (remaps.find(path)) {
  377. path = remaps[path];
  378. }
  379. RES res = ResourceLoader::load(path, type);
  380. if (res.is_null()) {
  381. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  382. }
  383. r_v = res;
  384. }
  385. } break;
  386. case OBJECT_EXTERNAL_RESOURCE_INDEX: {
  387. //new file format, just refers to an index in the external list
  388. uint32_t erindex = f->get_32();
  389. if (p_for_export_data) {
  390. r_v = "@RESEXTERNAL:" + itos(erindex);
  391. } else {
  392. if (erindex >= external_resources.size()) {
  393. WARN_PRINT("Broken external resource! (index out of size");
  394. r_v = Variant();
  395. } else {
  396. String type = external_resources[erindex].type;
  397. String path = external_resources[erindex].path;
  398. if (path.find("://") == -1 && path.is_rel_path()) {
  399. // path is relative to file being loaded, so convert to a resource path
  400. path = Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
  401. }
  402. RES res = ResourceLoader::load(path, type);
  403. if (res.is_null()) {
  404. WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data());
  405. }
  406. r_v = res;
  407. }
  408. }
  409. } break;
  410. default: {
  411. ERR_FAIL_V(ERR_FILE_CORRUPT);
  412. } break;
  413. }
  414. } break;
  415. case VARIANT_INPUT_EVENT: {
  416. } break;
  417. case VARIANT_DICTIONARY: {
  418. uint32_t len = f->get_32();
  419. Dictionary d(len & 0x80000000); //last bit means shared
  420. len &= 0x7FFFFFFF;
  421. for (uint32_t i = 0; i < len; i++) {
  422. Variant key;
  423. Error err = parse_variant(key, p_for_export_data);
  424. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  425. Variant value;
  426. err = parse_variant(value, p_for_export_data);
  427. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  428. d[key] = value;
  429. }
  430. r_v = d;
  431. } break;
  432. case VARIANT_ARRAY: {
  433. uint32_t len = f->get_32();
  434. Array a(len & 0x80000000); //last bit means shared
  435. len &= 0x7FFFFFFF;
  436. a.resize(len);
  437. for (uint32_t i = 0; i < len; i++) {
  438. Variant val;
  439. Error err = parse_variant(val, p_for_export_data);
  440. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  441. a[i] = val;
  442. }
  443. r_v = a;
  444. } break;
  445. case VARIANT_RAW_ARRAY: {
  446. uint32_t len = f->get_32();
  447. DVector<uint8_t> array;
  448. array.resize(len);
  449. DVector<uint8_t>::Write w = array.write();
  450. f->get_buffer(w.ptr(), len);
  451. _advance_padding(len);
  452. w = DVector<uint8_t>::Write();
  453. r_v = array;
  454. } break;
  455. case VARIANT_INT_ARRAY: {
  456. uint32_t len = f->get_32();
  457. DVector<int> array;
  458. array.resize(len);
  459. DVector<int>::Write w = array.write();
  460. f->get_buffer((uint8_t *)w.ptr(), len * 4);
  461. #ifdef BIG_ENDIAN_ENABLED
  462. {
  463. uint32_t *ptr = (uint32_t *)w.ptr();
  464. for (int i = 0; i < len; i++) {
  465. ptr[i] = BSWAP32(ptr[i]);
  466. }
  467. }
  468. #endif
  469. w = DVector<int>::Write();
  470. r_v = array;
  471. } break;
  472. case VARIANT_REAL_ARRAY: {
  473. uint32_t len = f->get_32();
  474. DVector<real_t> array;
  475. array.resize(len);
  476. DVector<real_t>::Write w = array.write();
  477. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t));
  478. #ifdef BIG_ENDIAN_ENABLED
  479. {
  480. uint32_t *ptr = (uint32_t *)w.ptr();
  481. for (int i = 0; i < len; i++) {
  482. ptr[i] = BSWAP32(ptr[i]);
  483. }
  484. }
  485. #endif
  486. w = DVector<real_t>::Write();
  487. r_v = array;
  488. } break;
  489. case VARIANT_STRING_ARRAY: {
  490. uint32_t len = f->get_32();
  491. DVector<String> array;
  492. array.resize(len);
  493. DVector<String>::Write w = array.write();
  494. for (uint32_t i = 0; i < len; i++)
  495. w[i] = get_unicode_string();
  496. w = DVector<String>::Write();
  497. r_v = array;
  498. } break;
  499. case VARIANT_VECTOR2_ARRAY: {
  500. uint32_t len = f->get_32();
  501. DVector<Vector2> array;
  502. array.resize(len);
  503. DVector<Vector2>::Write w = array.write();
  504. if (sizeof(Vector2) == 8) {
  505. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 2);
  506. #ifdef BIG_ENDIAN_ENABLED
  507. {
  508. uint32_t *ptr = (uint32_t *)w.ptr();
  509. for (int i = 0; i < len * 2; i++) {
  510. ptr[i] = BSWAP32(ptr[i]);
  511. }
  512. }
  513. #endif
  514. } else {
  515. ERR_EXPLAIN("Vector2 size is NOT 8!");
  516. ERR_FAIL_V(ERR_UNAVAILABLE);
  517. }
  518. w = DVector<Vector2>::Write();
  519. r_v = array;
  520. } break;
  521. case VARIANT_VECTOR3_ARRAY: {
  522. uint32_t len = f->get_32();
  523. DVector<Vector3> array;
  524. array.resize(len);
  525. DVector<Vector3>::Write w = array.write();
  526. if (sizeof(Vector3) == 12) {
  527. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 3);
  528. #ifdef BIG_ENDIAN_ENABLED
  529. {
  530. uint32_t *ptr = (uint32_t *)w.ptr();
  531. for (int i = 0; i < len * 3; i++) {
  532. ptr[i] = BSWAP32(ptr[i]);
  533. }
  534. }
  535. #endif
  536. } else {
  537. ERR_EXPLAIN("Vector3 size is NOT 12!");
  538. ERR_FAIL_V(ERR_UNAVAILABLE);
  539. }
  540. w = DVector<Vector3>::Write();
  541. r_v = array;
  542. } break;
  543. case VARIANT_COLOR_ARRAY: {
  544. uint32_t len = f->get_32();
  545. DVector<Color> array;
  546. array.resize(len);
  547. DVector<Color>::Write w = array.write();
  548. if (sizeof(Color) == 16) {
  549. f->get_buffer((uint8_t *)w.ptr(), len * sizeof(real_t) * 4);
  550. #ifdef BIG_ENDIAN_ENABLED
  551. {
  552. uint32_t *ptr = (uint32_t *)w.ptr();
  553. for (int i = 0; i < len * 4; i++) {
  554. ptr[i] = BSWAP32(ptr[i]);
  555. }
  556. }
  557. #endif
  558. } else {
  559. ERR_EXPLAIN("Color size is NOT 16!");
  560. ERR_FAIL_V(ERR_UNAVAILABLE);
  561. }
  562. w = DVector<Color>::Write();
  563. r_v = array;
  564. } break;
  565. default: {
  566. ERR_FAIL_V(ERR_FILE_CORRUPT);
  567. } break;
  568. }
  569. return OK; //never reach anyway
  570. }
  571. void ResourceInteractiveLoaderBinary::set_local_path(const String &p_local_path) {
  572. res_path = p_local_path;
  573. }
  574. Ref<Resource> ResourceInteractiveLoaderBinary::get_resource() {
  575. return resource;
  576. }
  577. Error ResourceInteractiveLoaderBinary::poll() {
  578. if (error != OK)
  579. return error;
  580. int s = stage;
  581. if (s < external_resources.size()) {
  582. String path = external_resources[s].path;
  583. if (remaps.has(path)) {
  584. path = remaps[path];
  585. }
  586. RES res = ResourceLoader::load(path, external_resources[s].type);
  587. if (res.is_null()) {
  588. if (!ResourceLoader::get_abort_on_missing_resources()) {
  589. ResourceLoader::notify_dependency_error(local_path, path, external_resources[s].type);
  590. } else {
  591. error = ERR_FILE_MISSING_DEPENDENCIES;
  592. ERR_EXPLAIN("Can't load dependency: " + path);
  593. ERR_FAIL_V(error);
  594. }
  595. } else {
  596. resource_cache.push_back(res);
  597. }
  598. stage++;
  599. return error;
  600. }
  601. s -= external_resources.size();
  602. if (s >= internal_resources.size()) {
  603. error = ERR_BUG;
  604. ERR_FAIL_COND_V(s >= internal_resources.size(), error);
  605. }
  606. bool main = s == (internal_resources.size() - 1);
  607. //maybe it is loaded already
  608. String path;
  609. int subindex = 0;
  610. if (!main) {
  611. path = internal_resources[s].path;
  612. if (path.begins_with("local://")) {
  613. path = path.replace_first("local://", "");
  614. subindex = path.to_int();
  615. path = res_path + "::" + path;
  616. }
  617. if (ResourceCache::has(path)) {
  618. //already loaded, don't do anything
  619. stage++;
  620. error = OK;
  621. return error;
  622. }
  623. } else {
  624. if (!ResourceCache::has(res_path))
  625. path = res_path;
  626. }
  627. uint64_t offset = internal_resources[s].offset;
  628. f->seek(offset);
  629. String t = get_unicode_string();
  630. Object *obj = ObjectTypeDB::instance(t);
  631. if (!obj) {
  632. error = ERR_FILE_CORRUPT;
  633. ERR_EXPLAIN(local_path + ":Resource of unrecognized type in file: " + t);
  634. }
  635. ERR_FAIL_COND_V(!obj, ERR_FILE_CORRUPT);
  636. Resource *r = obj->cast_to<Resource>();
  637. if (!r) {
  638. error = ERR_FILE_CORRUPT;
  639. memdelete(obj); //bye
  640. ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_type());
  641. ERR_FAIL_COND_V(!r, ERR_FILE_CORRUPT);
  642. }
  643. RES res = RES(r);
  644. r->set_path(path);
  645. r->set_subindex(subindex);
  646. int pc = f->get_32();
  647. //set properties
  648. for (int i = 0; i < pc; i++) {
  649. uint32_t name_idx = f->get_32();
  650. if (name_idx >= (uint32_t)string_map.size()) {
  651. error = ERR_FILE_CORRUPT;
  652. ERR_FAIL_V(ERR_FILE_CORRUPT);
  653. }
  654. Variant value;
  655. error = parse_variant(value);
  656. if (error)
  657. return error;
  658. res->set(string_map[name_idx], value);
  659. }
  660. #ifdef TOOLS_ENABLED
  661. res->set_edited(false);
  662. #endif
  663. stage++;
  664. resource_cache.push_back(res);
  665. if (main) {
  666. if (importmd_ofs) {
  667. f->seek(importmd_ofs);
  668. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  669. imd->set_editor(get_unicode_string());
  670. int sc = f->get_32();
  671. for (int i = 0; i < sc; i++) {
  672. String src = get_unicode_string();
  673. String md5 = get_unicode_string();
  674. imd->add_source(src, md5);
  675. }
  676. int pc = f->get_32();
  677. for (int i = 0; i < pc; i++) {
  678. String name = get_unicode_string();
  679. Variant val;
  680. parse_variant(val);
  681. imd->set_option(name, val);
  682. }
  683. res->set_import_metadata(imd);
  684. }
  685. f->close();
  686. resource = res;
  687. error = ERR_FILE_EOF;
  688. } else {
  689. error = OK;
  690. }
  691. return OK;
  692. }
  693. int ResourceInteractiveLoaderBinary::get_stage() const {
  694. return stage;
  695. }
  696. int ResourceInteractiveLoaderBinary::get_stage_count() const {
  697. return external_resources.size() + internal_resources.size();
  698. }
  699. static void save_ustring(FileAccess *f, const String &p_string) {
  700. CharString utf8 = p_string.utf8();
  701. f->store_32(utf8.length() + 1);
  702. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  703. }
  704. static String get_ustring(FileAccess *f) {
  705. int len = f->get_32();
  706. Vector<char> str_buf;
  707. str_buf.resize(len);
  708. f->get_buffer((uint8_t *)&str_buf[0], len);
  709. String s;
  710. s.parse_utf8(&str_buf[0]);
  711. return s;
  712. }
  713. String ResourceInteractiveLoaderBinary::get_unicode_string() {
  714. int len = f->get_32();
  715. if (len > str_buf.size()) {
  716. str_buf.resize(len);
  717. }
  718. if (len == 0)
  719. return String();
  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. Error ResourceInteractiveLoaderBinary::get_export_data(ExportData &r_export_data) {
  726. for (int i = 0; i < external_resources.size(); i++) {
  727. ExportData::Dependency dep;
  728. dep.path = external_resources[i].path;
  729. dep.type = external_resources[i].type;
  730. r_export_data.dependencies[i] = dep;
  731. }
  732. for (int i = 0; i < internal_resources.size(); i++) {
  733. bool main = i == (internal_resources.size() - 1);
  734. //maybe it is loaded already
  735. r_export_data.resources.resize(r_export_data.resources.size() + 1);
  736. ExportData::ResourceData &res_data = r_export_data.resources[r_export_data.resources.size() - 1];
  737. res_data.index = -1;
  738. if (!main) {
  739. String path = internal_resources[i].path;
  740. if (path.begins_with("local://")) {
  741. path = path.replace_first("local://", "");
  742. res_data.index = path.to_int();
  743. }
  744. } else {
  745. }
  746. uint64_t offset = internal_resources[i].offset;
  747. f->seek(offset);
  748. String t = get_unicode_string();
  749. res_data.type = t;
  750. int pc = f->get_32();
  751. //set properties
  752. for (int i = 0; i < pc; i++) {
  753. uint32_t name_idx = f->get_32();
  754. if (name_idx >= (uint32_t)string_map.size()) {
  755. error = ERR_FILE_CORRUPT;
  756. ERR_FAIL_V(ERR_FILE_CORRUPT);
  757. }
  758. Variant value;
  759. error = parse_variant(value, true);
  760. if (error)
  761. return error;
  762. ExportData::PropertyData pdata;
  763. pdata.name = string_map[name_idx];
  764. pdata.value = value;
  765. res_data.properties.push_back(pdata);
  766. }
  767. }
  768. return OK;
  769. }
  770. void ResourceInteractiveLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) {
  771. open(p_f);
  772. if (error)
  773. return;
  774. for (int i = 0; i < external_resources.size(); i++) {
  775. String dep = external_resources[i].path;
  776. if (dep.ends_with("*")) {
  777. dep = ResourceLoader::guess_full_filename(dep, external_resources[i].type);
  778. }
  779. if (p_add_types && external_resources[i].type != String()) {
  780. dep += "::" + external_resources[i].type;
  781. }
  782. p_dependencies->push_back(dep);
  783. }
  784. }
  785. void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) {
  786. error = OK;
  787. f = p_f;
  788. uint8_t header[4];
  789. f->get_buffer(header, 4);
  790. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  791. //compressed
  792. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  793. fac->open_after_magic(f);
  794. f = fac;
  795. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  796. //not normal
  797. error = ERR_FILE_UNRECOGNIZED;
  798. ERR_EXPLAIN("Unrecognized binary resource file: " + local_path);
  799. ERR_FAIL();
  800. }
  801. bool big_endian = f->get_32();
  802. #ifdef BIG_ENDIAN_ENABLED
  803. endian_swap = !big_endian;
  804. #else
  805. bool endian_swap = big_endian;
  806. #endif
  807. bool use_real64 = f->get_32();
  808. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  809. uint32_t ver_major = f->get_32();
  810. uint32_t ver_minor = f->get_32();
  811. uint32_t ver_format = f->get_32();
  812. print_bl("big endian: " + itos(big_endian));
  813. print_bl("endian swap: " + itos(endian_swap));
  814. print_bl("real64: " + itos(use_real64));
  815. print_bl("major: " + itos(ver_major));
  816. print_bl("minor: " + itos(ver_minor));
  817. print_bl("format: " + itos(ver_format));
  818. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  819. f->close();
  820. ERR_EXPLAIN("File Format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a a new engine version: " + local_path);
  821. ERR_FAIL();
  822. }
  823. type = get_unicode_string();
  824. print_bl("type: " + type);
  825. importmd_ofs = f->get_64();
  826. for (int i = 0; i < 14; i++)
  827. f->get_32(); //skip a few reserved fields
  828. uint32_t string_table_size = f->get_32();
  829. string_map.resize(string_table_size);
  830. for (uint32_t i = 0; i < string_table_size; i++) {
  831. StringName s = get_unicode_string();
  832. string_map[i] = s;
  833. }
  834. print_bl("strings: " + itos(string_table_size));
  835. uint32_t ext_resources_size = f->get_32();
  836. for (uint32_t i = 0; i < ext_resources_size; i++) {
  837. ExtResource er;
  838. er.type = get_unicode_string();
  839. er.path = get_unicode_string();
  840. external_resources.push_back(er);
  841. }
  842. //see if the exporter has different set of external resources for more efficient loading
  843. /*
  844. String preload_depts = "deps/"+res_path.md5_text();
  845. if (Globals::get_singleton()->has(preload_depts)) {
  846. external_resources.clear();
  847. //ignore external resources and use these
  848. NodePath depts=Globals::get_singleton()->get(preload_depts);
  849. external_resources.resize(depts.get_name_count());
  850. for(int i=0;i<depts.get_name_count();i++) {
  851. external_resources[i].path=depts.get_name(i);
  852. }
  853. print_line(res_path+" - EXTERNAL RESOURCES: "+itos(external_resources.size()));
  854. }*/
  855. print_bl("ext resources: " + itos(ext_resources_size));
  856. uint32_t int_resources_size = f->get_32();
  857. for (uint32_t i = 0; i < int_resources_size; i++) {
  858. IntResource ir;
  859. ir.path = get_unicode_string();
  860. ir.offset = f->get_64();
  861. internal_resources.push_back(ir);
  862. }
  863. print_bl("int resources: " + itos(int_resources_size));
  864. if (f->eof_reached()) {
  865. error = ERR_FILE_CORRUPT;
  866. ERR_EXPLAIN("Premature End Of File: " + local_path);
  867. ERR_FAIL();
  868. }
  869. }
  870. String ResourceInteractiveLoaderBinary::recognize(FileAccess *p_f) {
  871. error = OK;
  872. f = p_f;
  873. uint8_t header[4];
  874. f->get_buffer(header, 4);
  875. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  876. //compressed
  877. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  878. fac->open_after_magic(f);
  879. f = fac;
  880. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  881. //not normal
  882. error = ERR_FILE_UNRECOGNIZED;
  883. return "";
  884. }
  885. bool big_endian = f->get_32();
  886. #ifdef BIG_ENDIAN_ENABLED
  887. endian_swap = !big_endian;
  888. #else
  889. bool endian_swap = big_endian;
  890. #endif
  891. bool use_real64 = f->get_32();
  892. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  893. uint32_t ver_major = f->get_32();
  894. uint32_t ver_minor = f->get_32();
  895. uint32_t ver_format = f->get_32();
  896. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  897. f->close();
  898. return "";
  899. }
  900. String type = get_unicode_string();
  901. return type;
  902. }
  903. ResourceInteractiveLoaderBinary::ResourceInteractiveLoaderBinary() {
  904. f = NULL;
  905. stage = 0;
  906. endian_swap = false;
  907. use_real64 = false;
  908. error = OK;
  909. }
  910. ResourceInteractiveLoaderBinary::~ResourceInteractiveLoaderBinary() {
  911. if (f)
  912. memdelete(f);
  913. }
  914. Ref<ResourceInteractiveLoader> ResourceFormatLoaderBinary::load_interactive(const String &p_path, Error *r_error) {
  915. if (r_error)
  916. *r_error = ERR_FILE_CANT_OPEN;
  917. Error err;
  918. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  919. if (err != OK) {
  920. ERR_FAIL_COND_V(err != OK, Ref<ResourceInteractiveLoader>());
  921. }
  922. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  923. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  924. ria->res_path = ria->local_path;
  925. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  926. ria->open(f);
  927. return ria;
  928. }
  929. void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  930. if (p_type == "") {
  931. get_recognized_extensions(p_extensions);
  932. return;
  933. }
  934. List<String> extensions;
  935. ObjectTypeDB::get_extensions_for_type(p_type, &extensions);
  936. extensions.sort();
  937. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  938. String ext = E->get().to_lower();
  939. p_extensions->push_back(ext);
  940. }
  941. }
  942. void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const {
  943. List<String> extensions;
  944. ObjectTypeDB::get_resource_base_extensions(&extensions);
  945. extensions.sort();
  946. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  947. String ext = E->get().to_lower();
  948. p_extensions->push_back(ext);
  949. }
  950. }
  951. bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const {
  952. return true; //handles all
  953. }
  954. Error ResourceFormatLoaderBinary::load_import_metadata(const String &p_path, Ref<ResourceImportMetadata> &r_var) const {
  955. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  956. if (!f) {
  957. return ERR_FILE_CANT_OPEN;
  958. }
  959. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  960. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  961. ria->res_path = ria->local_path;
  962. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  963. ria->recognize(f);
  964. if (ria->error != OK)
  965. return ERR_FILE_UNRECOGNIZED;
  966. f = ria->f;
  967. uint64_t imp_ofs = f->get_64();
  968. if (imp_ofs == 0)
  969. return ERR_UNAVAILABLE;
  970. f->seek(imp_ofs);
  971. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  972. imd->set_editor(ria->get_unicode_string());
  973. int sc = f->get_32();
  974. for (int i = 0; i < sc; i++) {
  975. String src = ria->get_unicode_string();
  976. String md5 = ria->get_unicode_string();
  977. imd->add_source(src, md5);
  978. }
  979. int pc = f->get_32();
  980. for (int i = 0; i < pc; i++) {
  981. String name = ria->get_unicode_string();
  982. Variant val;
  983. ria->parse_variant(val);
  984. imd->set_option(name, val);
  985. }
  986. r_var = imd;
  987. return OK;
  988. }
  989. ResourceFormatLoaderBinary *ResourceFormatLoaderBinary::singleton = NULL;
  990. void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  991. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  992. ERR_FAIL_COND(!f);
  993. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  994. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  995. ria->res_path = ria->local_path;
  996. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  997. ria->get_dependencies(f, p_dependencies, p_add_types);
  998. }
  999. Error ResourceFormatLoaderBinary::get_export_data(const String &p_path, ExportData &r_export_data) {
  1000. Error err;
  1001. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  1002. if (err != OK) {
  1003. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  1004. }
  1005. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1006. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1007. ria->res_path = ria->local_path;
  1008. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1009. ria->open(f);
  1010. return ria->get_export_data(r_export_data);
  1011. }
  1012. Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  1013. // Error error=OK;
  1014. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1015. ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
  1016. FileAccess *fw = NULL; //=FileAccess::open(p_path+".depren");
  1017. String local_path = p_path.get_base_dir();
  1018. uint8_t header[4];
  1019. f->get_buffer(header, 4);
  1020. if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') {
  1021. //compressed
  1022. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1023. fac->open_after_magic(f);
  1024. f = fac;
  1025. FileAccessCompressed *facw = memnew(FileAccessCompressed);
  1026. facw->configure("RSCC");
  1027. Error err = facw->_open(p_path + ".depren", FileAccess::WRITE);
  1028. if (err) {
  1029. memdelete(fac);
  1030. memdelete(facw);
  1031. ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT);
  1032. }
  1033. fw = facw;
  1034. } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') {
  1035. //not normal
  1036. //error=ERR_FILE_UNRECOGNIZED;
  1037. memdelete(f);
  1038. ERR_EXPLAIN("Unrecognized binary resource file: " + local_path);
  1039. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  1040. } else {
  1041. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  1042. if (!fw) {
  1043. memdelete(f);
  1044. }
  1045. ERR_FAIL_COND_V(!fw, ERR_CANT_CREATE);
  1046. }
  1047. bool big_endian = f->get_32();
  1048. #ifdef BIG_ENDIAN_ENABLED
  1049. endian_swap = !big_endian;
  1050. #else
  1051. bool endian_swap = big_endian;
  1052. #endif
  1053. bool use_real64 = f->get_32();
  1054. f->set_endian_swap(big_endian != 0); //read big endian if saved as big endian
  1055. fw->store_32(endian_swap);
  1056. fw->set_endian_swap(big_endian != 0);
  1057. fw->store_32(use_real64); //use real64
  1058. uint32_t ver_major = f->get_32();
  1059. uint32_t ver_minor = f->get_32();
  1060. uint32_t ver_format = f->get_32();
  1061. if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) {
  1062. memdelete(f);
  1063. memdelete(fw);
  1064. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1065. da->remove(p_path + ".depren");
  1066. memdelete(da);
  1067. //fuck it, use the old approach;
  1068. WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: " + p_path).utf8().get_data());
  1069. Error err;
  1070. f = FileAccess::open(p_path, FileAccess::READ, &err);
  1071. if (err != OK) {
  1072. ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN);
  1073. }
  1074. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1075. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1076. ria->res_path = ria->local_path;
  1077. ria->remaps = p_map;
  1078. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1079. ria->open(f);
  1080. err = ria->poll();
  1081. while (err == OK) {
  1082. err = ria->poll();
  1083. }
  1084. ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
  1085. RES res = ria->get_resource();
  1086. ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT);
  1087. return ResourceFormatSaverBinary::singleton->save(p_path, res);
  1088. }
  1089. if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) {
  1090. memdelete(f);
  1091. memdelete(fw);
  1092. ERR_EXPLAIN("File Format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a a new engine version: " + local_path);
  1093. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  1094. }
  1095. fw->store_32(VERSION_MAJOR); //current version
  1096. fw->store_32(VERSION_MINOR);
  1097. fw->store_32(FORMAT_VERSION);
  1098. save_ustring(fw, get_ustring(f)); //type
  1099. size_t md_ofs = f->get_pos();
  1100. size_t importmd_ofs = f->get_64();
  1101. fw->store_64(0); //metadata offset
  1102. for (int i = 0; i < 14; i++) {
  1103. fw->store_32(0);
  1104. f->get_32();
  1105. }
  1106. //string table
  1107. uint32_t string_table_size = f->get_32();
  1108. fw->store_32(string_table_size);
  1109. for (uint32_t i = 0; i < string_table_size; i++) {
  1110. String s = get_ustring(f);
  1111. save_ustring(fw, s);
  1112. }
  1113. //external resources
  1114. uint32_t ext_resources_size = f->get_32();
  1115. fw->store_32(ext_resources_size);
  1116. for (uint32_t i = 0; i < ext_resources_size; i++) {
  1117. String type = get_ustring(f);
  1118. String path = get_ustring(f);
  1119. bool relative = false;
  1120. if (!path.begins_with("res://")) {
  1121. path = local_path.plus_file(path).simplify_path();
  1122. relative = true;
  1123. }
  1124. if (p_map.has(path)) {
  1125. String np = p_map[path];
  1126. path = np;
  1127. }
  1128. if (relative) {
  1129. //restore relative
  1130. path = local_path.path_to_file(path);
  1131. }
  1132. save_ustring(fw, type);
  1133. save_ustring(fw, path);
  1134. }
  1135. int64_t size_diff = (int64_t)fw->get_pos() - (int64_t)f->get_pos();
  1136. //internal resources
  1137. uint32_t int_resources_size = f->get_32();
  1138. fw->store_32(int_resources_size);
  1139. for (uint32_t i = 0; i < int_resources_size; i++) {
  1140. String path = get_ustring(f);
  1141. uint64_t offset = f->get_64();
  1142. save_ustring(fw, path);
  1143. fw->store_64(offset + size_diff);
  1144. }
  1145. //rest of file
  1146. uint8_t b = f->get_8();
  1147. while (!f->eof_reached()) {
  1148. fw->store_8(b);
  1149. b = f->get_8();
  1150. }
  1151. bool all_ok = fw->get_error() == OK;
  1152. fw->seek(md_ofs);
  1153. fw->store_64(importmd_ofs + size_diff);
  1154. memdelete(f);
  1155. memdelete(fw);
  1156. if (!all_ok) {
  1157. return ERR_CANT_CREATE;
  1158. }
  1159. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1160. da->remove(p_path);
  1161. da->rename(p_path + ".depren", p_path);
  1162. memdelete(da);
  1163. return OK;
  1164. }
  1165. String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const {
  1166. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1167. if (!f) {
  1168. return ""; //could not rwead
  1169. }
  1170. Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary);
  1171. ria->local_path = Globals::get_singleton()->localize_path(p_path);
  1172. ria->res_path = ria->local_path;
  1173. // ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
  1174. String r = ria->recognize(f);
  1175. return r;
  1176. }
  1177. ///////////////////////////////////////////////////////////
  1178. ///////////////////////////////////////////////////////////
  1179. ///////////////////////////////////////////////////////////
  1180. void ResourceFormatSaverBinaryInstance::_pad_buffer(int p_bytes) {
  1181. int extra = 4 - (p_bytes % 4);
  1182. if (extra < 4) {
  1183. for (int i = 0; i < extra; i++)
  1184. f->store_8(0); //pad to 32
  1185. }
  1186. }
  1187. void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, const PropertyInfo &p_hint) {
  1188. switch (p_property.get_type()) {
  1189. case Variant::NIL: {
  1190. f->store_32(VARIANT_NIL);
  1191. // don't store anything
  1192. } break;
  1193. case Variant::BOOL: {
  1194. f->store_32(VARIANT_BOOL);
  1195. bool val = p_property;
  1196. f->store_32(val);
  1197. } break;
  1198. case Variant::INT: {
  1199. f->store_32(VARIANT_INT);
  1200. int val = p_property;
  1201. f->store_32(val);
  1202. } break;
  1203. case Variant::REAL: {
  1204. f->store_32(VARIANT_REAL);
  1205. real_t val = p_property;
  1206. f->store_real(val);
  1207. } break;
  1208. case Variant::STRING: {
  1209. f->store_32(VARIANT_STRING);
  1210. String val = p_property;
  1211. save_unicode_string(val);
  1212. } break;
  1213. case Variant::VECTOR2: {
  1214. f->store_32(VARIANT_VECTOR2);
  1215. Vector2 val = p_property;
  1216. f->store_real(val.x);
  1217. f->store_real(val.y);
  1218. } break;
  1219. case Variant::RECT2: {
  1220. f->store_32(VARIANT_RECT2);
  1221. Rect2 val = p_property;
  1222. f->store_real(val.pos.x);
  1223. f->store_real(val.pos.y);
  1224. f->store_real(val.size.x);
  1225. f->store_real(val.size.y);
  1226. } break;
  1227. case Variant::VECTOR3: {
  1228. f->store_32(VARIANT_VECTOR3);
  1229. Vector3 val = p_property;
  1230. f->store_real(val.x);
  1231. f->store_real(val.y);
  1232. f->store_real(val.z);
  1233. } break;
  1234. case Variant::PLANE: {
  1235. f->store_32(VARIANT_PLANE);
  1236. Plane val = p_property;
  1237. f->store_real(val.normal.x);
  1238. f->store_real(val.normal.y);
  1239. f->store_real(val.normal.z);
  1240. f->store_real(val.d);
  1241. } break;
  1242. case Variant::QUAT: {
  1243. f->store_32(VARIANT_QUAT);
  1244. Quat val = p_property;
  1245. f->store_real(val.x);
  1246. f->store_real(val.y);
  1247. f->store_real(val.z);
  1248. f->store_real(val.w);
  1249. } break;
  1250. case Variant::_AABB: {
  1251. f->store_32(VARIANT_AABB);
  1252. AABB val = p_property;
  1253. f->store_real(val.pos.x);
  1254. f->store_real(val.pos.y);
  1255. f->store_real(val.pos.z);
  1256. f->store_real(val.size.x);
  1257. f->store_real(val.size.y);
  1258. f->store_real(val.size.z);
  1259. } break;
  1260. case Variant::MATRIX32: {
  1261. f->store_32(VARIANT_MATRIX32);
  1262. Matrix32 val = p_property;
  1263. f->store_real(val.elements[0].x);
  1264. f->store_real(val.elements[0].y);
  1265. f->store_real(val.elements[1].x);
  1266. f->store_real(val.elements[1].y);
  1267. f->store_real(val.elements[2].x);
  1268. f->store_real(val.elements[2].y);
  1269. } break;
  1270. case Variant::MATRIX3: {
  1271. f->store_32(VARIANT_MATRIX3);
  1272. Matrix3 val = p_property;
  1273. f->store_real(val.elements[0].x);
  1274. f->store_real(val.elements[0].y);
  1275. f->store_real(val.elements[0].z);
  1276. f->store_real(val.elements[1].x);
  1277. f->store_real(val.elements[1].y);
  1278. f->store_real(val.elements[1].z);
  1279. f->store_real(val.elements[2].x);
  1280. f->store_real(val.elements[2].y);
  1281. f->store_real(val.elements[2].z);
  1282. } break;
  1283. case Variant::TRANSFORM: {
  1284. f->store_32(VARIANT_TRANSFORM);
  1285. Transform val = p_property;
  1286. f->store_real(val.basis.elements[0].x);
  1287. f->store_real(val.basis.elements[0].y);
  1288. f->store_real(val.basis.elements[0].z);
  1289. f->store_real(val.basis.elements[1].x);
  1290. f->store_real(val.basis.elements[1].y);
  1291. f->store_real(val.basis.elements[1].z);
  1292. f->store_real(val.basis.elements[2].x);
  1293. f->store_real(val.basis.elements[2].y);
  1294. f->store_real(val.basis.elements[2].z);
  1295. f->store_real(val.origin.x);
  1296. f->store_real(val.origin.y);
  1297. f->store_real(val.origin.z);
  1298. } break;
  1299. case Variant::COLOR: {
  1300. f->store_32(VARIANT_COLOR);
  1301. Color val = p_property;
  1302. f->store_real(val.r);
  1303. f->store_real(val.g);
  1304. f->store_real(val.b);
  1305. f->store_real(val.a);
  1306. } break;
  1307. case Variant::IMAGE: {
  1308. f->store_32(VARIANT_IMAGE);
  1309. Image val = p_property;
  1310. if (val.empty()) {
  1311. f->store_32(IMAGE_ENCODING_EMPTY);
  1312. break;
  1313. }
  1314. int encoding = IMAGE_ENCODING_RAW;
  1315. float quality = 0.7;
  1316. if (val.get_format() <= Image::FORMAT_INDEXED_ALPHA) {
  1317. //can only compress uncompressed stuff
  1318. if (p_hint.hint == PROPERTY_HINT_IMAGE_COMPRESS_LOSSY && Image::lossy_packer) {
  1319. encoding = IMAGE_ENCODING_LOSSY;
  1320. float qs = p_hint.hint_string.to_double();
  1321. if (qs != 0.0)
  1322. quality = qs;
  1323. } else if (p_hint.hint == PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS && Image::lossless_packer) {
  1324. encoding = IMAGE_ENCODING_LOSSLESS;
  1325. }
  1326. }
  1327. f->store_32(encoding); //raw encoding
  1328. if (encoding == IMAGE_ENCODING_RAW) {
  1329. f->store_32(val.get_width());
  1330. f->store_32(val.get_height());
  1331. f->store_32(val.get_mipmaps());
  1332. switch (val.get_format()) {
  1333. case Image::FORMAT_GRAYSCALE:
  1334. f->store_32(IMAGE_FORMAT_GRAYSCALE);
  1335. break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  1336. case Image::FORMAT_INTENSITY:
  1337. f->store_32(IMAGE_FORMAT_INTENSITY);
  1338. break; ///< one byte per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255
  1339. case Image::FORMAT_GRAYSCALE_ALPHA:
  1340. f->store_32(IMAGE_FORMAT_GRAYSCALE_ALPHA);
  1341. break; ///< two bytes per pixel: f->store_32(IMAGE_FORMAT_ ); break; 0-255. alpha 0-255
  1342. case Image::FORMAT_RGB:
  1343. f->store_32(IMAGE_FORMAT_RGB);
  1344. break; ///< one byte R: f->store_32(IMAGE_FORMAT_ ); break; one byte G: f->store_32(IMAGE_FORMAT_ ); break; one byte B
  1345. case Image::FORMAT_RGBA:
  1346. f->store_32(IMAGE_FORMAT_RGBA);
  1347. break; ///< one byte R: f->store_32(IMAGE_FORMAT_ ); break; one byte G: f->store_32(IMAGE_FORMAT_ ); break; one byte B: f->store_32(IMAGE_FORMAT_ ); break; one byte A
  1348. case Image::FORMAT_INDEXED:
  1349. f->store_32(IMAGE_FORMAT_INDEXED);
  1350. break; ///< index byte 0-256: f->store_32(IMAGE_FORMAT_ ); break; and after image end: f->store_32(IMAGE_FORMAT_ ); break; 256*3 bytes of palette
  1351. case Image::FORMAT_INDEXED_ALPHA:
  1352. f->store_32(IMAGE_FORMAT_INDEXED_ALPHA);
  1353. break; ///< index byte 0-256: f->store_32(IMAGE_FORMAT_ ); break; and after image end: f->store_32(IMAGE_FORMAT_ ); break; 256*4 bytes of palette (alpha)
  1354. case Image::FORMAT_BC1:
  1355. f->store_32(IMAGE_FORMAT_BC1);
  1356. break; // DXT1
  1357. case Image::FORMAT_BC2:
  1358. f->store_32(IMAGE_FORMAT_BC2);
  1359. break; // DXT3
  1360. case Image::FORMAT_BC3:
  1361. f->store_32(IMAGE_FORMAT_BC3);
  1362. break; // DXT5
  1363. case Image::FORMAT_BC4:
  1364. f->store_32(IMAGE_FORMAT_BC4);
  1365. break; // ATI1
  1366. case Image::FORMAT_BC5:
  1367. f->store_32(IMAGE_FORMAT_BC5);
  1368. break; // ATI2
  1369. case Image::FORMAT_PVRTC2: f->store_32(IMAGE_FORMAT_PVRTC2); break;
  1370. case Image::FORMAT_PVRTC2_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC2_ALPHA); break;
  1371. case Image::FORMAT_PVRTC4: f->store_32(IMAGE_FORMAT_PVRTC4); break;
  1372. case Image::FORMAT_PVRTC4_ALPHA: f->store_32(IMAGE_FORMAT_PVRTC4_ALPHA); break;
  1373. case Image::FORMAT_ETC: f->store_32(IMAGE_FORMAT_ETC); break;
  1374. case Image::FORMAT_ATC: f->store_32(IMAGE_FORMAT_ATC); break;
  1375. case Image::FORMAT_ATC_ALPHA_EXPLICIT: f->store_32(IMAGE_FORMAT_ATC_ALPHA_EXPLICIT); break;
  1376. case Image::FORMAT_ATC_ALPHA_INTERPOLATED: f->store_32(IMAGE_FORMAT_ATC_ALPHA_INTERPOLATED); break;
  1377. case Image::FORMAT_CUSTOM: f->store_32(IMAGE_FORMAT_CUSTOM); break;
  1378. default: {
  1379. }
  1380. }
  1381. int dlen = val.get_data().size();
  1382. f->store_32(dlen);
  1383. DVector<uint8_t>::Read r = val.get_data().read();
  1384. f->store_buffer(r.ptr(), dlen);
  1385. _pad_buffer(dlen);
  1386. } else {
  1387. DVector<uint8_t> data;
  1388. if (encoding == IMAGE_ENCODING_LOSSY) {
  1389. data = Image::lossy_packer(val, quality);
  1390. } else if (encoding == IMAGE_ENCODING_LOSSLESS) {
  1391. data = Image::lossless_packer(val);
  1392. }
  1393. int ds = data.size();
  1394. f->store_32(ds);
  1395. if (ds > 0) {
  1396. DVector<uint8_t>::Read r = data.read();
  1397. f->store_buffer(r.ptr(), ds);
  1398. _pad_buffer(ds);
  1399. }
  1400. }
  1401. } break;
  1402. case Variant::NODE_PATH: {
  1403. f->store_32(VARIANT_NODE_PATH);
  1404. NodePath np = p_property;
  1405. f->store_16(np.get_name_count());
  1406. uint16_t snc = np.get_subname_count();
  1407. if (np.is_absolute())
  1408. snc |= 0x8000;
  1409. f->store_16(snc);
  1410. for (int i = 0; i < np.get_name_count(); i++)
  1411. f->store_32(get_string_index(np.get_name(i)));
  1412. for (int i = 0; i < np.get_subname_count(); i++)
  1413. f->store_32(get_string_index(np.get_subname(i)));
  1414. f->store_32(get_string_index(np.get_property()));
  1415. } break;
  1416. case Variant::_RID: {
  1417. f->store_32(VARIANT_RID);
  1418. WARN_PRINT("Can't save RIDs");
  1419. RID val = p_property;
  1420. f->store_32(val.get_id());
  1421. } break;
  1422. case Variant::OBJECT: {
  1423. f->store_32(VARIANT_OBJECT);
  1424. RES res = p_property;
  1425. if (res.is_null()) {
  1426. f->store_32(OBJECT_EMPTY);
  1427. return; // don't save it
  1428. }
  1429. if (res->get_path().length() && res->get_path().find("::") == -1) {
  1430. f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
  1431. f->store_32(external_resources[res]);
  1432. } else {
  1433. if (!resource_set.has(res)) {
  1434. f->store_32(OBJECT_EMPTY);
  1435. ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?");
  1436. ERR_FAIL();
  1437. }
  1438. f->store_32(OBJECT_INTERNAL_RESOURCE);
  1439. f->store_32(res->get_subindex());
  1440. //internal resource
  1441. }
  1442. } break;
  1443. case Variant::INPUT_EVENT: {
  1444. f->store_32(VARIANT_INPUT_EVENT);
  1445. WARN_PRINT("Can't save InputEvent (maybe it could..)");
  1446. } break;
  1447. case Variant::DICTIONARY: {
  1448. f->store_32(VARIANT_DICTIONARY);
  1449. Dictionary d = p_property;
  1450. f->store_32(uint32_t(d.size()) | (d.is_shared() ? 0x80000000 : 0));
  1451. List<Variant> keys;
  1452. d.get_key_list(&keys);
  1453. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1454. //if (!_check_type(dict[E->get()]))
  1455. // continue;
  1456. write_variant(E->get());
  1457. write_variant(d[E->get()]);
  1458. }
  1459. } break;
  1460. case Variant::ARRAY: {
  1461. f->store_32(VARIANT_ARRAY);
  1462. Array a = p_property;
  1463. f->store_32(uint32_t(a.size()) | (a.is_shared() ? 0x80000000 : 0));
  1464. for (int i = 0; i < a.size(); i++) {
  1465. write_variant(a[i]);
  1466. }
  1467. } break;
  1468. case Variant::RAW_ARRAY: {
  1469. f->store_32(VARIANT_RAW_ARRAY);
  1470. DVector<uint8_t> arr = p_property;
  1471. int len = arr.size();
  1472. f->store_32(len);
  1473. DVector<uint8_t>::Read r = arr.read();
  1474. f->store_buffer(r.ptr(), len);
  1475. _pad_buffer(len);
  1476. } break;
  1477. case Variant::INT_ARRAY: {
  1478. f->store_32(VARIANT_INT_ARRAY);
  1479. DVector<int> arr = p_property;
  1480. int len = arr.size();
  1481. f->store_32(len);
  1482. DVector<int>::Read r = arr.read();
  1483. for (int i = 0; i < len; i++)
  1484. f->store_32(r[i]);
  1485. } break;
  1486. case Variant::REAL_ARRAY: {
  1487. f->store_32(VARIANT_REAL_ARRAY);
  1488. DVector<real_t> arr = p_property;
  1489. int len = arr.size();
  1490. f->store_32(len);
  1491. DVector<real_t>::Read r = arr.read();
  1492. for (int i = 0; i < len; i++) {
  1493. f->store_real(r[i]);
  1494. }
  1495. } break;
  1496. case Variant::STRING_ARRAY: {
  1497. f->store_32(VARIANT_STRING_ARRAY);
  1498. DVector<String> arr = p_property;
  1499. int len = arr.size();
  1500. f->store_32(len);
  1501. DVector<String>::Read r = arr.read();
  1502. for (int i = 0; i < len; i++) {
  1503. save_unicode_string(r[i]);
  1504. }
  1505. } break;
  1506. case Variant::VECTOR3_ARRAY: {
  1507. f->store_32(VARIANT_VECTOR3_ARRAY);
  1508. DVector<Vector3> arr = p_property;
  1509. int len = arr.size();
  1510. f->store_32(len);
  1511. DVector<Vector3>::Read r = arr.read();
  1512. for (int i = 0; i < len; i++) {
  1513. f->store_real(r[i].x);
  1514. f->store_real(r[i].y);
  1515. f->store_real(r[i].z);
  1516. }
  1517. } break;
  1518. case Variant::VECTOR2_ARRAY: {
  1519. f->store_32(VARIANT_VECTOR2_ARRAY);
  1520. DVector<Vector2> arr = p_property;
  1521. int len = arr.size();
  1522. f->store_32(len);
  1523. DVector<Vector2>::Read r = arr.read();
  1524. for (int i = 0; i < len; i++) {
  1525. f->store_real(r[i].x);
  1526. f->store_real(r[i].y);
  1527. }
  1528. } break;
  1529. case Variant::COLOR_ARRAY: {
  1530. f->store_32(VARIANT_COLOR_ARRAY);
  1531. DVector<Color> arr = p_property;
  1532. int len = arr.size();
  1533. f->store_32(len);
  1534. DVector<Color>::Read r = arr.read();
  1535. for (int i = 0; i < len; i++) {
  1536. f->store_real(r[i].r);
  1537. f->store_real(r[i].g);
  1538. f->store_real(r[i].b);
  1539. f->store_real(r[i].a);
  1540. }
  1541. } break;
  1542. default: {
  1543. ERR_EXPLAIN("Invalid variant");
  1544. ERR_FAIL();
  1545. }
  1546. }
  1547. }
  1548. void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1549. switch (p_variant.get_type()) {
  1550. case Variant::OBJECT: {
  1551. RES res = p_variant.operator RefPtr();
  1552. if (res.is_null() || external_resources.has(res))
  1553. return;
  1554. if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
  1555. int idx = external_resources.size();
  1556. external_resources[res] = idx;
  1557. return;
  1558. }
  1559. if (resource_set.has(res))
  1560. return;
  1561. List<PropertyInfo> property_list;
  1562. res->get_property_list(&property_list);
  1563. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  1564. if (E->get().usage & PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage & PROPERTY_USAGE_BUNDLE)) {
  1565. _find_resources(res->get(E->get().name));
  1566. }
  1567. }
  1568. resource_set.insert(res);
  1569. saved_resources.push_back(res);
  1570. } break;
  1571. case Variant::ARRAY: {
  1572. Array varray = p_variant;
  1573. int len = varray.size();
  1574. for (int i = 0; i < len; i++) {
  1575. Variant v = varray.get(i);
  1576. _find_resources(v);
  1577. }
  1578. } break;
  1579. case Variant::DICTIONARY: {
  1580. Dictionary d = p_variant;
  1581. List<Variant> keys;
  1582. d.get_key_list(&keys);
  1583. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1584. _find_resources(E->get());
  1585. Variant v = d[E->get()];
  1586. _find_resources(v);
  1587. }
  1588. } break;
  1589. case Variant::NODE_PATH: {
  1590. //take the chance and save node path strings
  1591. NodePath np = p_variant;
  1592. for (int i = 0; i < np.get_name_count(); i++)
  1593. get_string_index(np.get_name(i));
  1594. for (int i = 0; i < np.get_subname_count(); i++)
  1595. get_string_index(np.get_subname(i));
  1596. get_string_index(np.get_property());
  1597. } break;
  1598. default: {
  1599. }
  1600. }
  1601. }
  1602. #if 0
  1603. Error ResourceFormatSaverBinary::_save_obj(const Object *p_object,SavedObject *so) {
  1604. //use classic way
  1605. List<PropertyInfo> property_list;
  1606. p_object->get_property_list( &property_list );
  1607. for(List<PropertyInfo>::Element *E=property_list.front();E;E=E->next()) {
  1608. if (skip_editor && E->get().name.begins_with("__editor"))
  1609. continue;
  1610. if (E->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage&PROPERTY_USAGE_BUNDLE)) {
  1611. SavedObject::SavedProperty sp;
  1612. sp.name_idx=get_string_index(E->get().name);
  1613. sp.value = p_object->get(E->get().name);
  1614. _find_resources(sp.value);
  1615. so->properties.push_back(sp);
  1616. }
  1617. }
  1618. return OK;
  1619. }
  1620. Error ResourceFormatSaverBinary::save(const Object *p_object,const Variant &p_meta) {
  1621. ERR_FAIL_COND_V(!f,ERR_UNCONFIGURED);
  1622. ERR_EXPLAIN("write_object should supply either an object, a meta, or both");
  1623. ERR_FAIL_COND_V(!p_object && p_meta.get_type()==Variant::NIL, ERR_INVALID_PARAMETER);
  1624. SavedObject *so = memnew( SavedObject );
  1625. if (p_object)
  1626. so->type=p_object->get_type();
  1627. _find_resources(p_meta);
  1628. so->meta=p_meta;
  1629. Error err = _save_obj(p_object,so);
  1630. ERR_FAIL_COND_V( err, ERR_INVALID_DATA );
  1631. saved_objects.push_back(so);
  1632. return OK;
  1633. }
  1634. #endif
  1635. void ResourceFormatSaverBinaryInstance::save_unicode_string(const String &p_string) {
  1636. CharString utf8 = p_string.utf8();
  1637. f->store_32(utf8.length() + 1);
  1638. f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  1639. }
  1640. int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) {
  1641. StringName s = p_string;
  1642. if (string_map.has(s))
  1643. return string_map[s];
  1644. string_map[s] = strings.size();
  1645. strings.push_back(s);
  1646. return strings.size() - 1;
  1647. }
  1648. Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1649. Error err;
  1650. if (p_flags & ResourceSaver::FLAG_COMPRESS) {
  1651. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1652. fac->configure("RSCC");
  1653. f = fac;
  1654. err = fac->_open(p_path, FileAccess::WRITE);
  1655. if (err)
  1656. memdelete(f);
  1657. } else {
  1658. f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1659. }
  1660. ERR_FAIL_COND_V(err, err);
  1661. FileAccessRef _fref(f);
  1662. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1663. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1664. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1665. big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN;
  1666. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1667. if (!p_path.begins_with("res://"))
  1668. takeover_paths = false;
  1669. local_path = p_path.get_base_dir();
  1670. //bin_meta_idx = get_string_index("__bin_meta__"); //is often used, so create
  1671. _find_resources(p_resource, true);
  1672. if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) {
  1673. //save header compressed
  1674. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  1675. f->store_buffer(header, 4);
  1676. }
  1677. if (big_endian) {
  1678. f->store_32(1);
  1679. f->set_endian_swap(true);
  1680. } else
  1681. f->store_32(0);
  1682. f->store_32(0); //64 bits file, false for now
  1683. f->store_32(VERSION_MAJOR);
  1684. f->store_32(VERSION_MINOR);
  1685. f->store_32(FORMAT_VERSION);
  1686. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1687. f->close();
  1688. return ERR_CANT_CREATE;
  1689. }
  1690. //f->store_32(saved_resources.size()+external_resources.size()); // load steps -not needed
  1691. save_unicode_string(p_resource->get_type());
  1692. uint64_t md_at = f->get_pos();
  1693. f->store_64(0); //offset to impoty metadata
  1694. for (int i = 0; i < 14; i++)
  1695. f->store_32(0); // reserved
  1696. List<ResourceData> resources;
  1697. {
  1698. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1699. ResourceData &rd = resources.push_back(ResourceData())->get();
  1700. rd.type = E->get()->get_type();
  1701. List<PropertyInfo> property_list;
  1702. E->get()->get_property_list(&property_list);
  1703. for (List<PropertyInfo>::Element *F = property_list.front(); F; F = F->next()) {
  1704. if (skip_editor && F->get().name.begins_with("__editor"))
  1705. continue;
  1706. if (F->get().usage & PROPERTY_USAGE_STORAGE || (bundle_resources && F->get().usage & PROPERTY_USAGE_BUNDLE)) {
  1707. Property p;
  1708. p.name_idx = get_string_index(F->get().name);
  1709. p.value = E->get()->get(F->get().name);
  1710. if ((F->get().usage & PROPERTY_USAGE_STORE_IF_NONZERO && p.value.is_zero()) || (F->get().usage & PROPERTY_USAGE_STORE_IF_NONONE && p.value.is_one()))
  1711. continue;
  1712. p.pi = F->get();
  1713. rd.properties.push_back(p);
  1714. }
  1715. }
  1716. }
  1717. }
  1718. f->store_32(strings.size()); //string table size
  1719. for (int i = 0; i < strings.size(); i++) {
  1720. //print_bl("saving string: "+strings[i]);
  1721. save_unicode_string(strings[i]);
  1722. }
  1723. // save external resource table
  1724. f->store_32(external_resources.size()); //amount of external resources
  1725. Vector<RES> save_order;
  1726. save_order.resize(external_resources.size());
  1727. for (Map<RES, int>::Element *E = external_resources.front(); E; E = E->next()) {
  1728. save_order[E->get()] = E->key();
  1729. }
  1730. for (int i = 0; i < save_order.size(); i++) {
  1731. save_unicode_string(save_order[i]->get_save_type());
  1732. String path = save_order[i]->get_path();
  1733. path = relative_paths ? local_path.path_to_file(path) : path;
  1734. save_unicode_string(path);
  1735. }
  1736. // save internal resource table
  1737. f->store_32(saved_resources.size()); //amount of internal resources
  1738. Vector<uint64_t> ofs_pos;
  1739. Set<int> used_indices;
  1740. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1741. RES r = E->get();
  1742. if (r->get_path() == "" || r->get_path().find("::") != -1) {
  1743. if (r->get_subindex() != 0) {
  1744. if (used_indices.has(r->get_subindex())) {
  1745. r->set_subindex(0); //repeated
  1746. } else {
  1747. used_indices.insert(r->get_subindex());
  1748. }
  1749. }
  1750. }
  1751. }
  1752. for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
  1753. RES r = E->get();
  1754. if (r->get_path() == "" || r->get_path().find("::") != -1) {
  1755. if (r->get_subindex() == 0) {
  1756. int new_subindex = 1;
  1757. if (used_indices.size()) {
  1758. new_subindex = used_indices.back()->get() + 1;
  1759. }
  1760. r->set_subindex(new_subindex);
  1761. used_indices.insert(new_subindex);
  1762. }
  1763. save_unicode_string("local://" + itos(r->get_subindex()));
  1764. if (takeover_paths) {
  1765. r->set_path(p_path + "::" + itos(r->get_subindex()), true);
  1766. }
  1767. } else {
  1768. save_unicode_string(r->get_path()); //actual external
  1769. }
  1770. ofs_pos.push_back(f->get_pos());
  1771. f->store_64(0); //offset in 64 bits
  1772. }
  1773. Vector<uint64_t> ofs_table;
  1774. // int saved_idx=0;
  1775. //now actually save the resources
  1776. for (List<ResourceData>::Element *E = resources.front(); E; E = E->next()) {
  1777. ResourceData &rd = E->get();
  1778. ofs_table.push_back(f->get_pos());
  1779. save_unicode_string(rd.type);
  1780. f->store_32(rd.properties.size());
  1781. for (List<Property>::Element *F = rd.properties.front(); F; F = F->next()) {
  1782. Property &p = F->get();
  1783. f->store_32(p.name_idx);
  1784. write_variant(p.value, F->get().pi);
  1785. }
  1786. }
  1787. for (int i = 0; i < ofs_table.size(); i++) {
  1788. f->seek(ofs_pos[i]);
  1789. f->store_64(ofs_table[i]);
  1790. }
  1791. f->seek_end();
  1792. print_line("Saving: " + p_path);
  1793. if (p_resource->get_import_metadata().is_valid()) {
  1794. uint64_t md_pos = f->get_pos();
  1795. Ref<ResourceImportMetadata> imd = p_resource->get_import_metadata();
  1796. save_unicode_string(imd->get_editor());
  1797. f->store_32(imd->get_source_count());
  1798. for (int i = 0; i < imd->get_source_count(); i++) {
  1799. save_unicode_string(imd->get_source_path(i));
  1800. save_unicode_string(imd->get_source_md5(i));
  1801. }
  1802. List<String> options;
  1803. imd->get_options(&options);
  1804. f->store_32(options.size());
  1805. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1806. save_unicode_string(E->get());
  1807. write_variant(imd->get_option(E->get()));
  1808. }
  1809. f->seek(md_at);
  1810. f->store_64(md_pos);
  1811. f->seek_end();
  1812. }
  1813. f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1814. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1815. f->close();
  1816. return ERR_CANT_CREATE;
  1817. }
  1818. f->close();
  1819. return OK;
  1820. }
  1821. Error ResourceFormatSaverBinary::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1822. String local_path = Globals::get_singleton()->localize_path(p_path);
  1823. ResourceFormatSaverBinaryInstance saver;
  1824. return saver.save(local_path, p_resource, p_flags);
  1825. }
  1826. bool ResourceFormatSaverBinary::recognize(const RES &p_resource) const {
  1827. return true; //all recognized
  1828. }
  1829. void ResourceFormatSaverBinary::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1830. String base = p_resource->get_base_extension().to_lower();
  1831. p_extensions->push_back(base);
  1832. }
  1833. ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = NULL;
  1834. ResourceFormatSaverBinary::ResourceFormatSaverBinary() {
  1835. singleton = this;
  1836. }