export.cpp 49 KB

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