export.cpp 48 KB

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