export.cpp 48 KB

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