export.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "export.h"
  31. #include "bind/core_bind.h"
  32. #include "editor/editor_export.h"
  33. #include "editor/editor_node.h"
  34. #include "io/marshalls.h"
  35. #include "io/zip_io.h"
  36. #include "object.h"
  37. #include "os/file_access.h"
  38. #include "platform/uwp/logo.gen.h"
  39. #include "project_settings.h"
  40. #include "version.h"
  41. #include "thirdparty/minizip/unzip.h"
  42. #include "thirdparty/minizip/zip.h"
  43. #include "thirdparty/misc/base64.h"
  44. #include "thirdparty/misc/sha256.h"
  45. #include <zlib.h>
  46. // Capabilities
  47. static const char *uwp_capabilities[] = {
  48. "allJoyn",
  49. "codeGeneration",
  50. "internetClient",
  51. "internetClientServer",
  52. "privateNetworkClientServer",
  53. NULL
  54. };
  55. static const char *uwp_uap_capabilities[] = {
  56. "appointments",
  57. "blockedChatMessages",
  58. "chat",
  59. "contacts",
  60. "enterpriseAuthentication",
  61. "musicLibrary",
  62. "objects3D",
  63. "picturesLibrary",
  64. "phoneCall",
  65. "removableStorage",
  66. "sharedUserCertificates",
  67. "userAccountInformation",
  68. "videosLibrary",
  69. "voipCall",
  70. NULL
  71. };
  72. static const char *uwp_device_capabilites[] = {
  73. "bluetooth",
  74. "location",
  75. "microphone",
  76. "proximity",
  77. "webcam",
  78. NULL
  79. };
  80. class AppxPackager {
  81. enum {
  82. FILE_HEADER_MAGIC = 0x04034b50,
  83. DATA_DESCRIPTOR_MAGIC = 0x08074b50,
  84. CENTRAL_DIR_MAGIC = 0x02014b50,
  85. END_OF_CENTRAL_DIR_MAGIC = 0x06054b50,
  86. ZIP64_END_OF_CENTRAL_DIR_MAGIC = 0x06064b50,
  87. ZIP64_END_DIR_LOCATOR_MAGIC = 0x07064b50,
  88. P7X_SIGNATURE = 0x58434b50,
  89. ZIP64_HEADER_ID = 0x0001,
  90. ZIP_VERSION = 20,
  91. ZIP_ARCHIVE_VERSION = 45,
  92. GENERAL_PURPOSE = 0x00,
  93. BASE_FILE_HEADER_SIZE = 30,
  94. DATA_DESCRIPTOR_SIZE = 24,
  95. BASE_CENTRAL_DIR_SIZE = 46,
  96. EXTRA_FIELD_LENGTH = 28,
  97. ZIP64_HEADER_SIZE = 24,
  98. ZIP64_END_OF_CENTRAL_DIR_SIZE = (56 - 12),
  99. END_OF_CENTRAL_DIR_SIZE = 42,
  100. BLOCK_SIZE = 65536,
  101. };
  102. struct BlockHash {
  103. String base64_hash;
  104. size_t compressed_size;
  105. };
  106. struct FileMeta {
  107. String name;
  108. int lfh_size;
  109. bool compressed;
  110. size_t compressed_size;
  111. size_t uncompressed_size;
  112. Vector<BlockHash> hashes;
  113. uLong file_crc32;
  114. ZPOS64_T zip_offset;
  115. };
  116. String progress_task;
  117. FileAccess *package;
  118. String tmp_blockmap_file_path;
  119. String tmp_content_types_file_path;
  120. Set<String> mime_types;
  121. Vector<FileMeta> file_metadata;
  122. ZPOS64_T central_dir_offset;
  123. ZPOS64_T end_of_central_dir_offset;
  124. Vector<uint8_t> central_dir_data;
  125. String hash_block(uint8_t *p_block_data, size_t p_block_len);
  126. void make_block_map();
  127. void make_content_types();
  128. _FORCE_INLINE_ unsigned int buf_put_int16(uint16_t p_val, uint8_t *p_buf) {
  129. for (int i = 0; i < 2; i++) {
  130. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  131. }
  132. return 2;
  133. }
  134. _FORCE_INLINE_ unsigned int buf_put_int32(uint32_t p_val, uint8_t *p_buf) {
  135. for (int i = 0; i < 4; i++) {
  136. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  137. }
  138. return 4;
  139. }
  140. _FORCE_INLINE_ unsigned int buf_put_int64(uint64_t p_val, uint8_t *p_buf) {
  141. for (int i = 0; i < 8; i++) {
  142. *p_buf++ = (p_val >> (i * 8)) & 0xFF;
  143. }
  144. return 8;
  145. }
  146. _FORCE_INLINE_ unsigned int buf_put_string(String p_val, uint8_t *p_buf) {
  147. for (int i = 0; i < p_val.length(); i++) {
  148. *p_buf++ = p_val.utf8().get(i);
  149. }
  150. return p_val.length();
  151. }
  152. Vector<uint8_t> make_file_header(FileMeta p_file_meta);
  153. void store_central_dir_header(const FileMeta &p_file, bool p_do_hash = true);
  154. Vector<uint8_t> make_end_of_central_record();
  155. String content_type(String p_extension);
  156. public:
  157. void set_progress_task(String p_task) { progress_task = p_task; }
  158. void init(FileAccess *p_fa);
  159. void add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress = false);
  160. void finish();
  161. AppxPackager();
  162. ~AppxPackager();
  163. };
  164. ///////////////////////////////////////////////////////////////////////////
  165. String AppxPackager::hash_block(uint8_t *p_block_data, size_t p_block_len) {
  166. char hash[32];
  167. char base64[45];
  168. sha256_context ctx;
  169. sha256_init(&ctx);
  170. sha256_hash(&ctx, p_block_data, p_block_len);
  171. sha256_done(&ctx, (uint8_t *)hash);
  172. base64_encode(base64, hash, 32);
  173. base64[44] = '\0';
  174. return String(base64);
  175. }
  176. void AppxPackager::make_block_map() {
  177. FileAccess *tmp_file = FileAccess::open(tmp_blockmap_file_path, FileAccess::WRITE);
  178. tmp_file->store_string("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
  179. tmp_file->store_string("<BlockMap xmlns=\"http://schemas.microsoft.com/appx/2010/blockmap\" HashMethod=\"http://www.w3.org/2001/04/xmlenc#sha256\">");
  180. for (int i = 0; i < file_metadata.size(); i++) {
  181. FileMeta file = file_metadata[i];
  182. tmp_file->store_string(
  183. "<File Name=\"" + file.name.replace("/", "\\") + "\" Size=\"" + itos(file.uncompressed_size) + "\" LfhSize=\"" + itos(file.lfh_size) + "\">");
  184. for (int j = 0; j < file.hashes.size(); j++) {
  185. tmp_file->store_string("<Block Hash=\"" + file.hashes[j].base64_hash + "\" ");
  186. if (file.compressed)
  187. tmp_file->store_string("Size=\"" + itos(file.hashes[j].compressed_size) + "\" ");
  188. tmp_file->store_string("/>");
  189. }
  190. tmp_file->store_string("</File>");
  191. }
  192. tmp_file->store_string("</BlockMap>");
  193. tmp_file->close();
  194. memdelete(tmp_file);
  195. tmp_file = NULL;
  196. }
  197. String AppxPackager::content_type(String p_extension) {
  198. if (p_extension == "png")
  199. return "image/png";
  200. else if (p_extension == "jpg")
  201. return "image/jpg";
  202. else if (p_extension == "xml")
  203. return "application/xml";
  204. else if (p_extension == "exe" || p_extension == "dll")
  205. return "application/x-msdownload";
  206. else
  207. return "application/octet-stream";
  208. }
  209. void AppxPackager::make_content_types() {
  210. FileAccess *tmp_file = FileAccess::open(tmp_content_types_file_path, FileAccess::WRITE);
  211. tmp_file->store_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  212. tmp_file->store_string("<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">");
  213. Map<String, String> types;
  214. for (int i = 0; i < file_metadata.size(); i++) {
  215. String ext = file_metadata[i].name.get_extension();
  216. if (types.has(ext)) continue;
  217. types[ext] = content_type(ext);
  218. tmp_file->store_string("<Default Extension=\"" + ext + "\" ContentType=\"" + types[ext] + "\" />");
  219. }
  220. // Appx signature file
  221. tmp_file->store_string("<Default Extension=\"p7x\" ContentType=\"application/octet-stream\" />");
  222. // Override for package files
  223. tmp_file->store_string("<Override PartName=\"/AppxManifest.xml\" ContentType=\"application/vnd.ms-appx.manifest+xml\" />");
  224. tmp_file->store_string("<Override PartName=\"/AppxBlockMap.xml\" ContentType=\"application/vnd.ms-appx.blockmap+xml\" />");
  225. tmp_file->store_string("<Override PartName=\"/AppxSignature.p7x\" ContentType=\"application/vnd.ms-appx.signature\" />");
  226. tmp_file->store_string("<Override PartName=\"/AppxMetadata/CodeIntegrity.cat\" ContentType=\"application/vnd.ms-pkiseccat\" />");
  227. tmp_file->store_string("</Types>");
  228. tmp_file->close();
  229. memdelete(tmp_file);
  230. tmp_file = NULL;
  231. }
  232. Vector<uint8_t> AppxPackager::make_file_header(FileMeta p_file_meta) {
  233. Vector<uint8_t> buf;
  234. buf.resize(BASE_FILE_HEADER_SIZE + p_file_meta.name.length());
  235. int offs = 0;
  236. // Write magic
  237. offs += buf_put_int32(FILE_HEADER_MAGIC, &buf[offs]);
  238. // Version
  239. offs += buf_put_int16(ZIP_VERSION, &buf[offs]);
  240. // Special flag
  241. offs += buf_put_int16(GENERAL_PURPOSE, &buf[offs]);
  242. // Compression
  243. offs += buf_put_int16(p_file_meta.compressed ? Z_DEFLATED : 0, &buf[offs]);
  244. // File date and time
  245. offs += buf_put_int32(0, &buf[offs]);
  246. // CRC-32
  247. offs += buf_put_int32(p_file_meta.file_crc32, &buf[offs]);
  248. // Compressed size
  249. offs += buf_put_int32(p_file_meta.compressed_size, &buf[offs]);
  250. // Uncompressed size
  251. offs += buf_put_int32(p_file_meta.uncompressed_size, &buf[offs]);
  252. // File name length
  253. offs += buf_put_int16(p_file_meta.name.length(), &buf[offs]);
  254. // Extra data length
  255. offs += buf_put_int16(0, &buf[offs]);
  256. // File name
  257. offs += buf_put_string(p_file_meta.name, &buf[offs]);
  258. // Done!
  259. return buf;
  260. }
  261. void AppxPackager::store_central_dir_header(const FileMeta &p_file, bool p_do_hash) {
  262. Vector<uint8_t> &buf = central_dir_data;
  263. int offs = buf.size();
  264. buf.resize(buf.size() + BASE_CENTRAL_DIR_SIZE + p_file.name.length());
  265. // Write magic
  266. offs += buf_put_int32(CENTRAL_DIR_MAGIC, &buf[offs]);
  267. // ZIP versions
  268. offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf[offs]);
  269. offs += buf_put_int16(ZIP_VERSION, &buf[offs]);
  270. // General purpose flag
  271. offs += buf_put_int16(GENERAL_PURPOSE, &buf[offs]);
  272. // Compression
  273. offs += buf_put_int16(p_file.compressed ? Z_DEFLATED : 0, &buf[offs]);
  274. // Modification date/time
  275. offs += buf_put_int32(0, &buf[offs]);
  276. // Crc-32
  277. offs += buf_put_int32(p_file.file_crc32, &buf[offs]);
  278. // File sizes
  279. offs += buf_put_int32(p_file.compressed_size, &buf[offs]);
  280. offs += buf_put_int32(p_file.uncompressed_size, &buf[offs]);
  281. // File name length
  282. offs += buf_put_int16(p_file.name.length(), &buf[offs]);
  283. // Extra field length
  284. offs += buf_put_int16(0, &buf[offs]);
  285. // Comment length
  286. offs += buf_put_int16(0, &buf[offs]);
  287. // Disk number start, internal/external file attributes
  288. for (int i = 0; i < 8; i++) {
  289. buf[offs++] = 0;
  290. }
  291. // Relative offset
  292. offs += buf_put_int32(p_file.zip_offset, &buf[offs]);
  293. // File name
  294. offs += buf_put_string(p_file.name, &buf[offs]);
  295. // Done!
  296. }
  297. Vector<uint8_t> AppxPackager::make_end_of_central_record() {
  298. Vector<uint8_t> buf;
  299. buf.resize(ZIP64_END_OF_CENTRAL_DIR_SIZE + 12 + END_OF_CENTRAL_DIR_SIZE); // Size plus magic
  300. int offs = 0;
  301. // Write magic
  302. offs += buf_put_int32(ZIP64_END_OF_CENTRAL_DIR_MAGIC, &buf[offs]);
  303. // Size of this record
  304. offs += buf_put_int64(ZIP64_END_OF_CENTRAL_DIR_SIZE, &buf[offs]);
  305. // Version (yes, twice)
  306. offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf[offs]);
  307. offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf[offs]);
  308. // Disk number
  309. for (int i = 0; i < 8; i++) {
  310. buf[offs++] = 0;
  311. }
  312. // Number of entries (total and per disk)
  313. offs += buf_put_int64(file_metadata.size(), &buf[offs]);
  314. offs += buf_put_int64(file_metadata.size(), &buf[offs]);
  315. // Size of central dir
  316. offs += buf_put_int64(central_dir_data.size(), &buf[offs]);
  317. // Central dir offset
  318. offs += buf_put_int64(central_dir_offset, &buf[offs]);
  319. ////// ZIP64 locator
  320. // Write magic for zip64 central dir locator
  321. offs += buf_put_int32(ZIP64_END_DIR_LOCATOR_MAGIC, &buf[offs]);
  322. // Disk number
  323. for (int i = 0; i < 4; i++) {
  324. buf[offs++] = 0;
  325. }
  326. // Relative offset
  327. offs += buf_put_int64(end_of_central_dir_offset, &buf[offs]);
  328. // Number of disks
  329. offs += buf_put_int32(1, &buf[offs]);
  330. /////// End of zip directory
  331. // Write magic for end central dir
  332. offs += buf_put_int32(END_OF_CENTRAL_DIR_MAGIC, &buf[offs]);
  333. // Dummy stuff for Zip64
  334. for (int i = 0; i < 4; i++) {
  335. buf[offs++] = 0x0;
  336. }
  337. for (int i = 0; i < 12; i++) {
  338. buf[offs++] = 0xFF;
  339. }
  340. // Size of comments
  341. for (int i = 0; i < 2; i++) {
  342. buf[offs++] = 0;
  343. }
  344. // Done!
  345. return buf;
  346. }
  347. void AppxPackager::init(FileAccess *p_fa) {
  348. package = p_fa;
  349. central_dir_offset = 0;
  350. end_of_central_dir_offset = 0;
  351. tmp_blockmap_file_path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpblockmap.xml";
  352. tmp_content_types_file_path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpcontenttypes.xml";
  353. }
  354. void AppxPackager::add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress) {
  355. if (p_file_no >= 1 && p_total_files >= 1) {
  356. EditorNode::progress_task_step(progress_task, "File: " + p_file_name, (p_file_no * 100) / p_total_files);
  357. }
  358. bool do_hash = p_file_name != "AppxSignature.p7x";
  359. FileMeta meta;
  360. meta.name = p_file_name;
  361. meta.uncompressed_size = p_len;
  362. meta.compressed_size = p_len;
  363. meta.compressed = p_compress;
  364. meta.zip_offset = package->get_pos();
  365. Vector<uint8_t> file_buffer;
  366. // Data for compression
  367. z_stream strm;
  368. FileAccess *strm_f = NULL;
  369. Vector<uint8_t> strm_in;
  370. strm_in.resize(BLOCK_SIZE);
  371. Vector<uint8_t> strm_out;
  372. if (p_compress) {
  373. strm.zalloc = zipio_alloc;
  374. strm.zfree = zipio_free;
  375. strm.opaque = &strm_f;
  376. strm_out.resize(BLOCK_SIZE + 8);
  377. deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
  378. }
  379. int step = 0;
  380. while (p_len - step > 0) {
  381. size_t block_size = (p_len - step) > BLOCK_SIZE ? BLOCK_SIZE : (p_len - step);
  382. for (int i = 0; i < block_size; i++) {
  383. strm_in[i] = p_buffer[step + i];
  384. }
  385. BlockHash bh;
  386. bh.base64_hash = hash_block(strm_in.ptr(), block_size);
  387. if (p_compress) {
  388. strm.avail_in = block_size;
  389. strm.avail_out = strm_out.size();
  390. strm.next_in = strm_in.ptr();
  391. strm.next_out = strm_out.ptr();
  392. int total_out_before = strm.total_out;
  393. deflate(&strm, Z_FULL_FLUSH);
  394. bh.compressed_size = strm.total_out - total_out_before;
  395. //package->store_buffer(strm_out.ptr(), strm.total_out - total_out_before);
  396. int start = file_buffer.size();
  397. file_buffer.resize(file_buffer.size() + bh.compressed_size);
  398. for (int i = 0; i < bh.compressed_size; i++)
  399. file_buffer[start + i] = strm_out[i];
  400. } else {
  401. bh.compressed_size = block_size;
  402. //package->store_buffer(strm_in.ptr(), block_size);
  403. int start = file_buffer.size();
  404. file_buffer.resize(file_buffer.size() + block_size);
  405. for (int i = 0; i < bh.compressed_size; i++)
  406. file_buffer[start + i] = strm_in[i];
  407. }
  408. meta.hashes.push_back(bh);
  409. step += block_size;
  410. }
  411. if (p_compress) {
  412. strm.avail_in = 0;
  413. strm.avail_out = strm_out.size();
  414. strm.next_in = strm_in.ptr();
  415. strm.next_out = strm_out.ptr();
  416. int total_out_before = strm.total_out;
  417. deflate(&strm, Z_FINISH);
  418. //package->store_buffer(strm_out.ptr(), strm.total_out - total_out_before);
  419. int start = file_buffer.size();
  420. file_buffer.resize(file_buffer.size() + (strm.total_out - total_out_before));
  421. for (int i = 0; i < (strm.total_out - total_out_before); i++)
  422. file_buffer[start + i] = strm_out[i];
  423. deflateEnd(&strm);
  424. meta.compressed_size = strm.total_out;
  425. } else {
  426. meta.compressed_size = p_len;
  427. }
  428. // Calculate file CRC-32
  429. uLong crc = crc32(0L, Z_NULL, 0);
  430. crc = crc32(crc, p_buffer, p_len);
  431. meta.file_crc32 = crc;
  432. // Create file header
  433. Vector<uint8_t> file_header = make_file_header(meta);
  434. meta.lfh_size = file_header.size();
  435. // Store the header and file;
  436. package->store_buffer(file_header.ptr(), file_header.size());
  437. package->store_buffer(file_buffer.ptr(), file_buffer.size());
  438. file_metadata.push_back(meta);
  439. }
  440. void AppxPackager::finish() {
  441. // Create and add block map file
  442. EditorNode::progress_task_step("export", "Creating block map...", 4);
  443. make_block_map();
  444. FileAccess *blockmap_file = FileAccess::open(tmp_blockmap_file_path, FileAccess::READ);
  445. Vector<uint8_t> blockmap_buffer;
  446. blockmap_buffer.resize(blockmap_file->get_len());
  447. blockmap_file->get_buffer(blockmap_buffer.ptr(), blockmap_buffer.size());
  448. add_file("AppxBlockMap.xml", blockmap_buffer.ptr(), blockmap_buffer.size(), -1, -1, true);
  449. blockmap_file->close();
  450. memdelete(blockmap_file);
  451. blockmap_file = NULL;
  452. // Add content types
  453. EditorNode::progress_task_step("export", "Setting content types...", 5);
  454. make_content_types();
  455. FileAccess *types_file = FileAccess::open(tmp_content_types_file_path, FileAccess::READ);
  456. Vector<uint8_t> types_buffer;
  457. types_buffer.resize(types_file->get_len());
  458. types_file->get_buffer(types_buffer.ptr(), types_buffer.size());
  459. add_file("[Content_Types].xml", types_buffer.ptr(), types_buffer.size(), -1, -1, true);
  460. types_file->close();
  461. memdelete(types_file);
  462. types_file = NULL;
  463. // Pre-process central directory before signing
  464. for (int i = 0; i < file_metadata.size(); i++) {
  465. store_central_dir_header(file_metadata[i]);
  466. }
  467. // Write central directory
  468. EditorNode::progress_task_step("export", "Finishing package...", 6);
  469. central_dir_offset = package->get_pos();
  470. package->store_buffer(central_dir_data.ptr(), central_dir_data.size());
  471. // End record
  472. end_of_central_dir_offset = package->get_pos();
  473. Vector<uint8_t> end_record = make_end_of_central_record();
  474. package->store_buffer(end_record.ptr(), end_record.size());
  475. package->close();
  476. memdelete(package);
  477. package = NULL;
  478. }
  479. AppxPackager::AppxPackager() {}
  480. AppxPackager::~AppxPackager() {}
  481. ////////////////////////////////////////////////////////////////////
  482. class EditorExportUWP : public EditorExportPlatform {
  483. GDCLASS(EditorExportUWP, EditorExportPlatform);
  484. Ref<ImageTexture> logo;
  485. enum Platform {
  486. ARM,
  487. X86,
  488. X64
  489. };
  490. bool _valid_resource_name(const String &p_name) const {
  491. if (p_name.empty()) return false;
  492. if (p_name.ends_with(".")) return false;
  493. static const char *invalid_names[] = {
  494. "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
  495. "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
  496. NULL
  497. };
  498. const char **t = invalid_names;
  499. while (*t) {
  500. if (p_name == *t) return false;
  501. t++;
  502. }
  503. return true;
  504. }
  505. bool _valid_guid(const String &p_guid) const {
  506. Vector<String> parts = p_guid.split("-");
  507. if (parts.size() != 5) return false;
  508. if (parts[0].length() != 8) return false;
  509. for (int i = 1; i < 4; i++)
  510. if (parts[i].length() != 4) return false;
  511. if (parts[4].length() != 12) return false;
  512. return true;
  513. }
  514. bool _valid_bgcolor(const String &p_color) const {
  515. if (p_color.empty()) return true;
  516. if (p_color.begins_with("#") && p_color.is_valid_html_color()) return true;
  517. // Colors from https://msdn.microsoft.com/en-us/library/windows/apps/dn934817.aspx
  518. static const char *valid_colors[] = {
  519. "aliceBlue", "antiqueWhite", "aqua", "aquamarine", "azure", "beige",
  520. "bisque", "black", "blanchedAlmond", "blue", "blueViolet", "brown",
  521. "burlyWood", "cadetBlue", "chartreuse", "chocolate", "coral", "cornflowerBlue",
  522. "cornsilk", "crimson", "cyan", "darkBlue", "darkCyan", "darkGoldenrod",
  523. "darkGray", "darkGreen", "darkKhaki", "darkMagenta", "darkOliveGreen", "darkOrange",
  524. "darkOrchid", "darkRed", "darkSalmon", "darkSeaGreen", "darkSlateBlue", "darkSlateGray",
  525. "darkTurquoise", "darkViolet", "deepPink", "deepSkyBlue", "dimGray", "dodgerBlue",
  526. "firebrick", "floralWhite", "forestGreen", "fuchsia", "gainsboro", "ghostWhite",
  527. "gold", "goldenrod", "gray", "green", "greenYellow", "honeydew",
  528. "hotPink", "indianRed", "indigo", "ivory", "khaki", "lavender",
  529. "lavenderBlush", "lawnGreen", "lemonChiffon", "lightBlue", "lightCoral", "lightCyan",
  530. "lightGoldenrodYellow", "lightGreen", "lightGray", "lightPink", "lightSalmon", "lightSeaGreen",
  531. "lightSkyBlue", "lightSlateGray", "lightSteelBlue", "lightYellow", "lime", "limeGreen",
  532. "linen", "magenta", "maroon", "mediumAquamarine", "mediumBlue", "mediumOrchid",
  533. "mediumPurple", "mediumSeaGreen", "mediumSlateBlue", "mediumSpringGreen", "mediumTurquoise", "mediumVioletRed",
  534. "midnightBlue", "mintCream", "mistyRose", "moccasin", "navajoWhite", "navy",
  535. "oldLace", "olive", "oliveDrab", "orange", "orangeRed", "orchid",
  536. "paleGoldenrod", "paleGreen", "paleTurquoise", "paleVioletRed", "papayaWhip", "peachPuff",
  537. "peru", "pink", "plum", "powderBlue", "purple", "red",
  538. "rosyBrown", "royalBlue", "saddleBrown", "salmon", "sandyBrown", "seaGreen",
  539. "seaShell", "sienna", "silver", "skyBlue", "slateBlue", "slateGray",
  540. "snow", "springGreen", "steelBlue", "tan", "teal", "thistle",
  541. "tomato", "transparent", "turquoise", "violet", "wheat", "white",
  542. "whiteSmoke", "yellow", "yellowGreen",
  543. NULL
  544. };
  545. const char **color = valid_colors;
  546. while (*color) {
  547. if (p_color == *color) return true;
  548. color++;
  549. }
  550. return false;
  551. }
  552. bool _valid_image(const StreamTexture *p_image, int p_width, int p_height) const {
  553. if (!p_image) {
  554. return false;
  555. }
  556. // TODO: Add resource creation or image rescaling to enable other scales:
  557. // 1.25, 1.5, 2.0
  558. real_t scales[] = { 1.0 };
  559. bool valid_w = false;
  560. bool valid_h = false;
  561. for (int i = 0; i < 1; i++) {
  562. int w = ceil(p_width * scales[i]);
  563. int h = ceil(p_height * scales[i]);
  564. if (w == p_image->get_width())
  565. valid_w = true;
  566. if (h == p_image->get_height())
  567. valid_h = true;
  568. }
  569. return valid_w && valid_h;
  570. }
  571. Vector<uint8_t> _fix_manifest(const Ref<EditorExportPreset> &p_preset, const Vector<uint8_t> &p_template, bool p_give_internet) const {
  572. String result = String::utf8((const char *)p_template.ptr(), p_template.size());
  573. result = result.replace("$godot_version$", VERSION_FULL_NAME);
  574. result = result.replace("$identity_name$", p_preset->get("package/unique_name"));
  575. result = result.replace("$publisher$", p_preset->get("package/publisher"));
  576. result = result.replace("$product_guid$", p_preset->get("identity/product_guid"));
  577. result = result.replace("$publisher_guid$", p_preset->get("identity/publisher_guid"));
  578. String version = itos(p_preset->get("version/major")) + "." + itos(p_preset->get("version/minor")) + "." + itos(p_preset->get("version/build")) + "." + itos(p_preset->get("version/revision"));
  579. result = result.replace("$version_string$", version);
  580. Platform arch = (Platform)(int)p_preset->get("architecture/target");
  581. String architecture = arch == ARM ? "ARM" : arch == X86 ? "x86" : "x64";
  582. result = result.replace("$architecture$", architecture);
  583. result = result.replace("$display_name$", String(p_preset->get("package/display_name")).empty() ? (String)ProjectSettings::get_singleton()->get("application/config/name") : String(p_preset->get("package/display_name")));
  584. result = result.replace("$publisher_display_name$", p_preset->get("package/publisher_display_name"));
  585. result = result.replace("$app_description$", p_preset->get("package/description"));
  586. result = result.replace("$bg_color$", p_preset->get("images/background_color"));
  587. result = result.replace("$short_name$", p_preset->get("package/short_name"));
  588. String name_on_tiles = "";
  589. if ((bool)p_preset->get("tiles/show_name_on_square150x150")) {
  590. name_on_tiles += " <uap:ShowOn Tile=\"square150x150Logo\" />\n";
  591. }
  592. if ((bool)p_preset->get("tiles/show_name_on_wide310x150")) {
  593. name_on_tiles += " <uap:ShowOn Tile=\"wide310x150Logo\" />\n";
  594. }
  595. if ((bool)p_preset->get("tiles/show_name_on_square310x310")) {
  596. name_on_tiles += " <uap:ShowOn Tile=\"square310x310Logo\" />\n";
  597. }
  598. String show_name_on_tiles = "";
  599. if (!name_on_tiles.empty()) {
  600. show_name_on_tiles = "<uap:ShowNameOnTiles>\n" + name_on_tiles + " </uap:ShowNameOnTiles>";
  601. }
  602. result = result.replace("$name_on_tiles$", name_on_tiles);
  603. String rotations = "";
  604. if ((bool)p_preset->get("orientation/landscape")) {
  605. rotations += " <uap:Rotation Preference=\"landscape\" />\n";
  606. }
  607. if ((bool)p_preset->get("orientation/portrait")) {
  608. rotations += " <uap:Rotation Preference=\"portrait\" />\n";
  609. }
  610. if ((bool)p_preset->get("orientation/landscape_flipped")) {
  611. rotations += " <uap:Rotation Preference=\"landscapeFlipped\" />\n";
  612. }
  613. if ((bool)p_preset->get("orientation/portrait_flipped")) {
  614. rotations += " <uap:Rotation Preference=\"portraitFlipped\" />\n";
  615. }
  616. String rotation_preference = "";
  617. if (!rotations.empty()) {
  618. rotation_preference = "<uap:InitialRotationPreference>\n" + rotations + " </uap:InitialRotationPreference>";
  619. }
  620. result = result.replace("$rotation_preference$", rotation_preference);
  621. String capabilities_elements = "";
  622. const char **basic = uwp_capabilities;
  623. while (*basic) {
  624. if ((bool)p_preset->get("capabilities/" + String(*basic))) {
  625. capabilities_elements += " <Capability Name=\"" + String(*basic) + "\" />\n";
  626. }
  627. basic++;
  628. }
  629. const char **uap = uwp_uap_capabilities;
  630. while (*uap) {
  631. if ((bool)p_preset->get("capabilities/" + String(*uap))) {
  632. capabilities_elements += " <uap:Capability Name=\"" + String(*uap) + "\" />\n";
  633. }
  634. uap++;
  635. }
  636. const char **device = uwp_device_capabilites;
  637. while (*device) {
  638. if ((bool)p_preset->get("capabilities/" + String(*device))) {
  639. capabilities_elements += " <DeviceCapability Name=\"" + String(*device) + "\" />\n";
  640. }
  641. device++;
  642. }
  643. if (!((bool)p_preset->get("capabilities/internetClient")) && p_give_internet) {
  644. capabilities_elements += " <Capability Name=\"internetClient\" />\n";
  645. }
  646. String capabilities_string = "<Capabilities />";
  647. if (!capabilities_elements.empty()) {
  648. capabilities_string = "<Capabilities>\n" + capabilities_elements + " </Capabilities>";
  649. }
  650. result = result.replace("$capabilities_place$", capabilities_string);
  651. Vector<uint8_t> r_ret;
  652. r_ret.resize(result.length());
  653. for (int i = 0; i < result.length(); i++)
  654. r_ret[i] = result.utf8().get(i);
  655. return r_ret;
  656. }
  657. Vector<uint8_t> _get_image_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  658. Vector<uint8_t> data;
  659. StreamTexture *image;
  660. if (p_path.find("StoreLogo") != -1) {
  661. image = p_preset->get("images/store_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/store_logo"))->cast_to<StreamTexture>();
  662. } else if (p_path.find("Square44x44Logo") != -1) {
  663. image = p_preset->get("images/square44x44_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/square44x44_logo"))->cast_to<StreamTexture>();
  664. } else if (p_path.find("Square71x71Logo") != -1) {
  665. image = p_preset->get("images/square71x71_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/square71x71_logo"))->cast_to<StreamTexture>();
  666. } else if (p_path.find("Square150x150Logo") != -1) {
  667. image = p_preset->get("images/square150x150_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/square150x150_logo"))->cast_to<StreamTexture>();
  668. } else if (p_path.find("Square310x310Logo") != -1) {
  669. image = p_preset->get("images/square310x310_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/square310x310_logo"))->cast_to<StreamTexture>();
  670. } else if (p_path.find("Wide310x150Logo") != -1) {
  671. image = p_preset->get("images/wide310x150_logo").is_zero() ? NULL : ((Object *)p_preset->get("images/wide310x150_logo"))->cast_to<StreamTexture>();
  672. } else if (p_path.find("SplashScreen") != -1) {
  673. image = p_preset->get("images/splash_screen").is_zero() ? NULL : ((Object *)p_preset->get("images/splash_screen"))->cast_to<StreamTexture>();
  674. }
  675. if (!image) return data;
  676. String tmp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/uwp_tmp_logo.png");
  677. Error err = image->get_data()->save_png(tmp_path);
  678. if (err != OK) {
  679. String err_string = "Couldn't save temp logo file.";
  680. EditorNode::add_io_error(err_string);
  681. ERR_EXPLAIN(err_string);
  682. ERR_FAIL_V(data);
  683. }
  684. FileAccess *f = FileAccess::open(tmp_path, FileAccess::READ, &err);
  685. if (err != OK) {
  686. String err_string = "Couldn't open temp logo file.";
  687. EditorNode::add_io_error(err_string);
  688. ERR_EXPLAIN(err_string);
  689. ERR_FAIL_V(data);
  690. }
  691. data.resize(f->get_len());
  692. f->get_buffer(data.ptr(), data.size());
  693. f->close();
  694. memdelete(f);
  695. // Delete temp file
  696. DirAccess *dir = DirAccess::open(tmp_path.get_base_dir(), &err);
  697. if (err != OK) {
  698. String err_string = "Couldn't open temp path to remove temp logo file.";
  699. EditorNode::add_io_error(err_string);
  700. ERR_EXPLAIN(err_string);
  701. ERR_FAIL_V(data);
  702. }
  703. err = dir->remove(tmp_path);
  704. memdelete(dir);
  705. if (err != OK) {
  706. String err_string = "Couldn't remove temp logo file.";
  707. EditorNode::add_io_error(err_string);
  708. ERR_EXPLAIN(err_string);
  709. ERR_FAIL_V(data);
  710. }
  711. return data;
  712. }
  713. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  714. /* TODO: This was copied verbatim from Android export. It should be
  715. * refactored to the parent class and also be used for .zip export.
  716. */
  717. /*
  718. * By not compressing files with little or not benefit in doing so,
  719. * a performance gain is expected at runtime. Moreover, if the APK is
  720. * zip-aligned, assets stored as they are can be efficiently read by
  721. * Android by memory-mapping them.
  722. */
  723. // -- Unconditional uncompress to mimic AAPT plus some other
  724. static const char *unconditional_compress_ext[] = {
  725. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  726. // These formats are already compressed, or don't compress well:
  727. ".jpg", ".jpeg", ".png", ".gif",
  728. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  729. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  730. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  731. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  732. ".amr", ".awb", ".wma", ".wmv",
  733. // Godot-specific:
  734. ".webp", // Same reasoning as .png
  735. ".cfb", // Don't let small config files slow-down startup
  736. ".scn", // Binary scenes are usually already compressed
  737. ".stex", // Streamable textures are usually already compressed
  738. // Trailer for easier processing
  739. NULL
  740. };
  741. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  742. if (p_path.to_lower().ends_with(String(*ext))) {
  743. return false;
  744. }
  745. }
  746. // -- Compressed resource?
  747. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  748. // Already compressed
  749. return false;
  750. }
  751. // --- TODO: Decide on texture resources according to their image compression setting
  752. return true;
  753. }
  754. static Error save_appx_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  755. AppxPackager *packager = (AppxPackager *)p_userdata;
  756. String dst_path = p_path.replace_first("res://", "game/");
  757. packager->add_file(dst_path, p_data.ptr(), p_data.size(), p_file, p_total, _should_compress_asset(p_path, p_data));
  758. return OK;
  759. }
  760. public:
  761. virtual String get_name() const {
  762. return "Windows Universal";
  763. }
  764. virtual String get_os_name() const {
  765. return "UWP";
  766. }
  767. virtual String get_binary_extension() const {
  768. return "appx";
  769. }
  770. virtual Ref<Texture> get_logo() const {
  771. return logo;
  772. }
  773. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  774. r_features->push_back("s3tc");
  775. r_features->push_back("etc");
  776. }
  777. virtual void get_export_options(List<ExportOption> *r_options) {
  778. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "architecture/target", PROPERTY_HINT_ENUM, "ARM,x86,x64"), 1));
  779. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  780. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/display_name"), ""));
  781. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/short_name"), "Godot"));
  782. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name"), "Godot.Engine"));
  783. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/description"), "Godot Engine"));
  784. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher"), "CN=GodotEngine"));
  785. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher_display_name"), "Godot Engine"));
  786. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/product_guid"), "00000000-0000-0000-0000-000000000000"));
  787. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/publisher_guid"), "00000000-0000-0000-0000-000000000000"));
  788. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/major"), 1));
  789. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/minor"), 0));
  790. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/build"), 0));
  791. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/revision"), 0));
  792. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape"), true));
  793. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
  794. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_flipped"), true));
  795. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_flipped"), true));
  796. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "images/background_color"), "transparent"));
  797. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/store_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  798. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square44x44_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  799. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square71x71_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  800. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square150x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  801. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square310x310_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  802. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/wide310x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  803. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/splash_screen", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
  804. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square150x150"), false));
  805. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_wide310x150"), false));
  806. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square310x310"), false));
  807. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  808. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
  809. // Capabilites
  810. const char **basic = uwp_capabilities;
  811. while (*basic) {
  812. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*basic).camelcase_to_underscore(false)), false));
  813. basic++;
  814. }
  815. const char **uap = uwp_uap_capabilities;
  816. while (*uap) {
  817. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*uap).camelcase_to_underscore(false)), false));
  818. uap++;
  819. }
  820. const char **device = uwp_device_capabilites;
  821. while (*device) {
  822. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*device).camelcase_to_underscore(false)), false));
  823. device++;
  824. }
  825. }
  826. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  827. String err;
  828. bool valid = true;
  829. Platform arch = (Platform)(int)(p_preset->get("architecture/target"));
  830. String custom_debug_binary = p_preset->get("custom_template/debug");
  831. String custom_release_binary = p_preset->get("custom_template/release");
  832. String platform_infix;
  833. switch (arch) {
  834. case EditorExportUWP::ARM: {
  835. platform_infix = "arm";
  836. } break;
  837. case EditorExportUWP::X86: {
  838. platform_infix = "x86";
  839. } break;
  840. case EditorExportUWP::X64: {
  841. platform_infix = "x64";
  842. } break;
  843. }
  844. if (!exists_export_template("uwp_" + platform_infix + "_debug.zip", &err) || !exists_export_template("uwp_" + platform_infix + "_debug.zip", &err)) {
  845. valid = false;
  846. r_missing_templates = true;
  847. }
  848. if (!valid && custom_debug_binary == "" && custom_release_binary == "") {
  849. if (!err.empty()) {
  850. r_error = err;
  851. }
  852. return valid;
  853. }
  854. bool dvalid = true;
  855. bool rvalid = true;
  856. if (!FileAccess::exists(custom_debug_binary)) {
  857. dvalid = false;
  858. err = "\nCustom debug binary not found.";
  859. }
  860. if (!FileAccess::exists(custom_release_binary)) {
  861. rvalid = false;
  862. err += "\nCustom release binary not found.";
  863. }
  864. if (dvalid || rvalid)
  865. valid = true;
  866. if (!valid) {
  867. r_error = err;
  868. return valid;
  869. }
  870. if (!_valid_resource_name(p_preset->get("package/unique_name"))) {
  871. valid = false;
  872. err += "\nInvalid unique name.";
  873. }
  874. if (!_valid_guid(p_preset->get("identity/product_guid"))) {
  875. valid = false;
  876. err += "\nInvalid product GUID.";
  877. }
  878. if (!_valid_guid(p_preset->get("identity/publisher_guid"))) {
  879. valid = false;
  880. err += "\nInvalid publisher GUID.";
  881. }
  882. if (!_valid_bgcolor(p_preset->get("images/background_color"))) {
  883. valid = false;
  884. err += "\nInvalid background color.";
  885. }
  886. if (!p_preset->get("images/store_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/store_logo"))->cast_to<StreamTexture>(), 50, 50)) {
  887. valid = false;
  888. err += "\nInvalid Store Logo image dimensions (should be 50x50).";
  889. }
  890. if (!p_preset->get("images/square44x44_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/square44x44_logo"))->cast_to<StreamTexture>(), 44, 44)) {
  891. valid = false;
  892. err += "\nInvalid square 44x44 logo image dimensions (should be 44x44).";
  893. }
  894. if (!p_preset->get("images/square71x71_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/square71x71_logo"))->cast_to<StreamTexture>(), 71, 71)) {
  895. valid = false;
  896. err += "\nInvalid square 71x71 logo image dimensions (should be 71x71).";
  897. }
  898. if (!p_preset->get("images/square150x150_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/square150x150_logo"))->cast_to<StreamTexture>(), 150, 0)) {
  899. valid = false;
  900. err += "\nInvalid square 150x150 logo image dimensions (should be 150x150).";
  901. }
  902. if (!p_preset->get("images/square310x310_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/square310x310_logo"))->cast_to<StreamTexture>(), 310, 310)) {
  903. valid = false;
  904. err += "\nInvalid square 310x310 logo image dimensions (should be 310x310).";
  905. }
  906. if (!p_preset->get("images/wide310x150_logo").is_zero() && !_valid_image(((Object *)p_preset->get("images/wide310x150_logo"))->cast_to<StreamTexture>(), 310, 150)) {
  907. valid = false;
  908. err += "\nInvalid wide 310x150 logo image dimensions (should be 310x150).";
  909. }
  910. if (!p_preset->get("images/splash_screen").is_zero() && !_valid_image(((Object *)p_preset->get("images/splash_screen"))->cast_to<StreamTexture>(), 620, 300)) {
  911. valid = false;
  912. err += "\nInvalid splash screen image dimensions (should be 620x300).";
  913. }
  914. r_error = err;
  915. return valid;
  916. }
  917. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) {
  918. String src_appx;
  919. EditorProgress ep("export", "Exporting for Windows Universal", 7);
  920. if (p_debug)
  921. src_appx = p_preset->get("custom_template/debug");
  922. else
  923. src_appx = p_preset->get("custom_template/release");
  924. src_appx = src_appx.strip_edges();
  925. Platform arch = (Platform)(int)p_preset->get("architecture/target");
  926. if (src_appx == "") {
  927. String err, infix;
  928. switch (arch) {
  929. case ARM: {
  930. infix = "_arm_";
  931. } break;
  932. case X86: {
  933. infix = "_x86_";
  934. } break;
  935. case X64: {
  936. infix = "_x64_";
  937. } break;
  938. }
  939. if (p_debug) {
  940. src_appx = find_export_template("uwp" + infix + "debug.zip", &err);
  941. } else {
  942. src_appx = find_export_template("uwp" + infix + "release.zip", &err);
  943. }
  944. if (src_appx == "") {
  945. EditorNode::add_io_error(err);
  946. return ERR_FILE_NOT_FOUND;
  947. }
  948. }
  949. Error err = OK;
  950. FileAccess *fa_pack = FileAccess::open(p_path, FileAccess::WRITE, &err);
  951. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  952. AppxPackager packager;
  953. packager.init(fa_pack);
  954. FileAccess *src_f = NULL;
  955. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  956. ep.step("Creating package...", 0);
  957. unzFile pkg = unzOpen2(src_appx.utf8().get_data(), &io);
  958. if (!pkg) {
  959. EditorNode::add_io_error("Could not find template appx to export:\n" + src_appx);
  960. return ERR_FILE_NOT_FOUND;
  961. }
  962. int ret = unzGoToFirstFile(pkg);
  963. ep.step("Copying template files...", 1);
  964. EditorNode::progress_add_task("template_files", "Template files", 100);
  965. packager.set_progress_task("template_files");
  966. int template_files_amount = 9;
  967. int template_file_no = 1;
  968. while (ret == UNZ_OK) {
  969. // get file name
  970. unz_file_info info;
  971. char fname[16834];
  972. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16834, NULL, 0, NULL, 0);
  973. String path = fname;
  974. if (path.ends_with("/")) {
  975. // Ignore directories
  976. ret = unzGoToNextFile(pkg);
  977. continue;
  978. }
  979. Vector<uint8_t> data;
  980. bool do_read = true;
  981. if (path.begins_with("Assets/")) {
  982. path = path.replace(".scale-100", "");
  983. data = _get_image_data(p_preset, path);
  984. if (data.size() > 0) do_read = false;
  985. }
  986. //read
  987. if (do_read) {
  988. data.resize(info.uncompressed_size);
  989. unzOpenCurrentFile(pkg);
  990. unzReadCurrentFile(pkg, data.ptr(), data.size());
  991. unzCloseCurrentFile(pkg);
  992. }
  993. if (path == "AppxManifest.xml") {
  994. data = _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
  995. }
  996. print_line("ADDING: " + path);
  997. packager.add_file(path, data.ptr(), data.size(), template_file_no++, template_files_amount, _should_compress_asset(path, data));
  998. ret = unzGoToNextFile(pkg);
  999. }
  1000. EditorNode::progress_end_task("template_files");
  1001. ep.step("Creating command line...", 2);
  1002. Vector<String> cl = ((String)p_preset->get("command_line/extra_args")).strip_edges().split(" ");
  1003. for (int i = 0; i < cl.size(); i++) {
  1004. if (cl[i].strip_edges().length() == 0) {
  1005. cl.remove(i);
  1006. i--;
  1007. }
  1008. }
  1009. if (!(p_flags & DEBUG_FLAG_DUMB_CLIENT)) {
  1010. cl.push_back("--path");
  1011. cl.push_back("game");
  1012. }
  1013. gen_export_flags(cl, p_flags);
  1014. // Command line file
  1015. Vector<uint8_t> clf;
  1016. // Argc
  1017. clf.resize(4);
  1018. encode_uint32(cl.size(), clf.ptr());
  1019. for (int i = 0; i < cl.size(); i++) {
  1020. CharString txt = cl[i].utf8();
  1021. int base = clf.size();
  1022. clf.resize(base + 4 + txt.length());
  1023. encode_uint32(txt.length(), &clf[base]);
  1024. copymem(&clf[base + 4], txt.ptr(), txt.length());
  1025. print_line(itos(i) + " param: " + cl[i]);
  1026. }
  1027. packager.add_file("__cl__.cl", clf.ptr(), clf.size(), -1, -1, false);
  1028. ep.step("Adding project files...", 3);
  1029. EditorNode::progress_add_task("project_files", "Project Files", 100);
  1030. packager.set_progress_task("project_files");
  1031. err = export_project_files(p_preset, save_appx_file, &packager);
  1032. EditorNode::progress_end_task("project_files");
  1033. ep.step("Closing package...", 7);
  1034. unzClose(pkg);
  1035. packager.finish();
  1036. return OK;
  1037. }
  1038. virtual void get_platform_features(List<String> *r_features) {
  1039. r_features->push_back("pc");
  1040. r_features->push_back("UWP");
  1041. }
  1042. EditorExportUWP() {
  1043. Ref<Image> img = memnew(Image(_uwp_logo));
  1044. logo.instance();
  1045. logo->create_from_image(img);
  1046. }
  1047. };
  1048. void register_uwp_exporter() {
  1049. Ref<EditorExportUWP> exporter = Ref<EditorExportUWP>(memnew(EditorExportUWP));
  1050. EditorExport::get_singleton()->add_export_platform(exporter);
  1051. }