export.cpp 47 KB

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