export.cpp 44 KB

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