export.cpp 48 KB

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