1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/**************************************************************************/
/*  export.cpp                                                            */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "export.h"

#include "core/bind/core_bind.h"
#include "core/crypto/crypto_core.h"
#include "core/io/marshalls.h"
#include "core/io/zip_io.h"
#include "core/object.h"
#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/project_settings.h"
#include "core/version.h"
#include "editor/editor_export.h"
#include "editor/editor_node.h"
#include "platform/uwp/logo.gen.h"

// Mono build doesn't support UWP, so we show a specific error.
// We don't bypass the whole logic so that it doesn't lose potential UWP presets
// added to export_presets.cfg from a non-Mono build (and in case third-parties
// actually have Mono-enabled UWP templates they can use).
#include "modules/modules_enabled.gen.h" // For mono.

#include "thirdparty/minizip/unzip.h"
#include "thirdparty/minizip/zip.h"

#include <zlib.h>

// Capabilities
static const char *uwp_capabilities[] = {
	"allJoyn",
	"codeGeneration",
	"internetClient",
	"internetClientServer",
	"privateNetworkClientServer",
	nullptr
};
static const char *uwp_uap_capabilities[] = {
	"appointments",
	"blockedChatMessages",
	"chat",
	"contacts",
	"enterpriseAuthentication",
	"musicLibrary",
	"objects3D",
	"picturesLibrary",
	"phoneCall",
	"removableStorage",
	"sharedUserCertificates",
	"userAccountInformation",
	"videosLibrary",
	"voipCall",
	nullptr
};
static const char *uwp_device_capabilities[] = {
	"bluetooth",
	"location",
	"microphone",
	"proximity",
	"webcam",
	nullptr
};

class AppxPackager {
	enum {
		FILE_HEADER_MAGIC = 0x04034b50,
		DATA_DESCRIPTOR_MAGIC = 0x08074b50,
		CENTRAL_DIR_MAGIC = 0x02014b50,
		END_OF_CENTRAL_DIR_MAGIC = 0x06054b50,
		ZIP64_END_OF_CENTRAL_DIR_MAGIC = 0x06064b50,
		ZIP64_END_DIR_LOCATOR_MAGIC = 0x07064b50,
		P7X_SIGNATURE = 0x58434b50,
		ZIP64_HEADER_ID = 0x0001,
		ZIP_VERSION = 20,
		ZIP_ARCHIVE_VERSION = 45,
		GENERAL_PURPOSE = 0x00,
		BASE_FILE_HEADER_SIZE = 30,
		DATA_DESCRIPTOR_SIZE = 24,
		BASE_CENTRAL_DIR_SIZE = 46,
		EXTRA_FIELD_LENGTH = 28,
		ZIP64_HEADER_SIZE = 24,
		ZIP64_END_OF_CENTRAL_DIR_SIZE = (56 - 12),
		END_OF_CENTRAL_DIR_SIZE = 42,
		BLOCK_SIZE = 65536,
	};

	struct BlockHash {
		String base64_hash;
		size_t compressed_size;
	};

	struct FileMeta {
		String name;
		int lfh_size;
		bool compressed;
		size_t compressed_size;
		size_t uncompressed_size;
		Vector<BlockHash> hashes;
		uLong file_crc32;
		ZPOS64_T zip_offset;

		FileMeta() :
				lfh_size(0),
				compressed(false),
				compressed_size(0),
				uncompressed_size(0),
				file_crc32(0),
				zip_offset(0) {}
	};

	String progress_task;
	FileAccess *package;

	Set<String> mime_types;

	Vector<FileMeta> file_metadata;

	ZPOS64_T central_dir_offset;
	ZPOS64_T end_of_central_dir_offset;
	Vector<uint8_t> central_dir_data;

	String hash_block(const uint8_t *p_block_data, size_t p_block_len);

	void make_block_map(const String &p_path);
	void make_content_types(const String &p_path);

	_FORCE_INLINE_ unsigned int buf_put_int16(uint16_t p_val, uint8_t *p_buf) {
		for (int i = 0; i < 2; i++) {
			*p_buf++ = (p_val >> (i * 8)) & 0xFF;
		}
		return 2;
	}

	_FORCE_INLINE_ unsigned int buf_put_int32(uint32_t p_val, uint8_t *p_buf) {
		for (int i = 0; i < 4; i++) {
			*p_buf++ = (p_val >> (i * 8)) & 0xFF;
		}
		return 4;
	}

	_FORCE_INLINE_ unsigned int buf_put_int64(uint64_t p_val, uint8_t *p_buf) {
		for (int i = 0; i < 8; i++) {
			*p_buf++ = (p_val >> (i * 8)) & 0xFF;
		}
		return 8;
	}

	_FORCE_INLINE_ unsigned int buf_put_string(String p_val, uint8_t *p_buf) {
		for (int i = 0; i < p_val.length(); i++) {
			*p_buf++ = p_val.utf8().get(i);
		}
		return p_val.length();
	}

	Vector<uint8_t> make_file_header(FileMeta p_file_meta);
	void store_central_dir_header(const FileMeta &p_file, bool p_do_hash = true);
	Vector<uint8_t> make_end_of_central_record();

	String content_type(String p_extension);

public:
	void set_progress_task(String p_task) { progress_task = p_task; }
	void init(FileAccess *p_fa);
	Error add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress = false);
	void finish();

	AppxPackager();
	~AppxPackager();
};

///////////////////////////////////////////////////////////////////////////

String AppxPackager::hash_block(const uint8_t *p_block_data, size_t p_block_len) {
	unsigned char hash[32];
	char base64[45];

	CryptoCore::sha256(p_block_data, p_block_len, hash);
	size_t len = 0;
	CryptoCore::b64_encode((unsigned char *)base64, 45, &len, (unsigned char *)hash, 32);
	base64[44] = '\0';

	return String(base64);
}

void AppxPackager::make_block_map(const String &p_path) {
	FileAccess *tmp_file = FileAccess::open(p_path, FileAccess::WRITE);

	tmp_file->store_string("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
	tmp_file->store_string("<BlockMap xmlns=\"http://schemas.microsoft.com/appx/2010/blockmap\" HashMethod=\"http://www.w3.org/2001/04/xmlenc#sha256\">");

	for (int i = 0; i < file_metadata.size(); i++) {
		FileMeta file = file_metadata[i];

		tmp_file->store_string(
				"<File Name=\"" + file.name.replace("/", "\\") + "\" Size=\"" + itos(file.uncompressed_size) + "\" LfhSize=\"" + itos(file.lfh_size) + "\">");

		for (int j = 0; j < file.hashes.size(); j++) {
			tmp_file->store_string("<Block Hash=\"" + file.hashes[j].base64_hash + "\" ");
			if (file.compressed) {
				tmp_file->store_string("Size=\"" + itos(file.hashes[j].compressed_size) + "\" ");
			}
			tmp_file->store_string("/>");
		}

		tmp_file->store_string("</File>");
	}

	tmp_file->store_string("</BlockMap>");

	tmp_file->close();
	memdelete(tmp_file);
}

String AppxPackager::content_type(String p_extension) {
	if (p_extension == "png") {
		return "image/png";
	} else if (p_extension == "jpg") {
		return "image/jpg";
	} else if (p_extension == "xml") {
		return "application/xml";
	} else if (p_extension == "exe" || p_extension == "dll") {
		return "application/x-msdownload";
	} else {
		return "application/octet-stream";
	}
}

void AppxPackager::make_content_types(const String &p_path) {
	FileAccess *tmp_file = FileAccess::open(p_path, FileAccess::WRITE);

	tmp_file->store_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	tmp_file->store_string("<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">");

	Map<String, String> types;

	for (int i = 0; i < file_metadata.size(); i++) {
		String ext = file_metadata[i].name.get_extension().to_lower();

		if (types.has(ext)) {
			continue;
		}

		types[ext] = content_type(ext);

		tmp_file->store_string("<Default Extension=\"" + ext + "\" ContentType=\"" + types[ext] + "\" />");
	}

	// Appx signature file
	tmp_file->store_string("<Default Extension=\"p7x\" ContentType=\"application/octet-stream\" />");

	// Override for package files
	tmp_file->store_string("<Override PartName=\"/AppxManifest.xml\" ContentType=\"application/vnd.ms-appx.manifest+xml\" />");
	tmp_file->store_string("<Override PartName=\"/AppxBlockMap.xml\" ContentType=\"application/vnd.ms-appx.blockmap+xml\" />");
	tmp_file->store_string("<Override PartName=\"/AppxSignature.p7x\" ContentType=\"application/vnd.ms-appx.signature\" />");
	tmp_file->store_string("<Override PartName=\"/AppxMetadata/CodeIntegrity.cat\" ContentType=\"application/vnd.ms-pkiseccat\" />");

	tmp_file->store_string("</Types>");

	tmp_file->close();
	memdelete(tmp_file);
}

Vector<uint8_t> AppxPackager::make_file_header(FileMeta p_file_meta) {
	Vector<uint8_t> buf;
	buf.resize(BASE_FILE_HEADER_SIZE + p_file_meta.name.length());

	int offs = 0;
	// Write magic
	offs += buf_put_int32(FILE_HEADER_MAGIC, &buf.write[offs]);

	// Version
	offs += buf_put_int16(ZIP_VERSION, &buf.write[offs]);

	// Special flag
	offs += buf_put_int16(GENERAL_PURPOSE, &buf.write[offs]);

	// Compression
	offs += buf_put_int16(p_file_meta.compressed ? Z_DEFLATED : 0, &buf.write[offs]);

	// File date and time
	offs += buf_put_int32(0, &buf.write[offs]);

	// CRC-32
	offs += buf_put_int32(p_file_meta.file_crc32, &buf.write[offs]);

	// Compressed size
	offs += buf_put_int32(p_file_meta.compressed_size, &buf.write[offs]);

	// Uncompressed size
	offs += buf_put_int32(p_file_meta.uncompressed_size, &buf.write[offs]);

	// File name length
	offs += buf_put_int16(p_file_meta.name.length(), &buf.write[offs]);

	// Extra data length
	offs += buf_put_int16(0, &buf.write[offs]);

	// File name
	offs += buf_put_string(p_file_meta.name, &buf.write[offs]);<--- Variable 'offs' is assigned a value that is never used.

	// Done!
	return buf;
}

void AppxPackager::store_central_dir_header(const FileMeta &p_file, bool p_do_hash) {
	Vector<uint8_t> &buf = central_dir_data;
	int offs = buf.size();
	buf.resize(buf.size() + BASE_CENTRAL_DIR_SIZE + p_file.name.length());

	// Write magic
	offs += buf_put_int32(CENTRAL_DIR_MAGIC, &buf.write[offs]);

	// ZIP versions
	offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf.write[offs]);
	offs += buf_put_int16(ZIP_VERSION, &buf.write[offs]);

	// General purpose flag
	offs += buf_put_int16(GENERAL_PURPOSE, &buf.write[offs]);

	// Compression
	offs += buf_put_int16(p_file.compressed ? Z_DEFLATED : 0, &buf.write[offs]);

	// Modification date/time
	offs += buf_put_int32(0, &buf.write[offs]);

	// Crc-32
	offs += buf_put_int32(p_file.file_crc32, &buf.write[offs]);

	// File sizes
	offs += buf_put_int32(p_file.compressed_size, &buf.write[offs]);
	offs += buf_put_int32(p_file.uncompressed_size, &buf.write[offs]);

	// File name length
	offs += buf_put_int16(p_file.name.length(), &buf.write[offs]);

	// Extra field length
	offs += buf_put_int16(0, &buf.write[offs]);

	// Comment length
	offs += buf_put_int16(0, &buf.write[offs]);

	// Disk number start, internal/external file attributes
	for (int i = 0; i < 8; i++) {
		buf.write[offs++] = 0;
	}

	// Relative offset
	offs += buf_put_int32(p_file.zip_offset, &buf.write[offs]);

	// File name
	offs += buf_put_string(p_file.name, &buf.write[offs]);<--- Variable 'offs' is assigned a value that is never used.

	// Done!
}

Vector<uint8_t> AppxPackager::make_end_of_central_record() {
	Vector<uint8_t> buf;
	buf.resize(ZIP64_END_OF_CENTRAL_DIR_SIZE + 12 + END_OF_CENTRAL_DIR_SIZE); // Size plus magic

	int offs = 0;

	// Write magic
	offs += buf_put_int32(ZIP64_END_OF_CENTRAL_DIR_MAGIC, &buf.write[offs]);

	// Size of this record
	offs += buf_put_int64(ZIP64_END_OF_CENTRAL_DIR_SIZE, &buf.write[offs]);

	// Version (yes, twice)
	offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf.write[offs]);
	offs += buf_put_int16(ZIP_ARCHIVE_VERSION, &buf.write[offs]);

	// Disk number
	for (int i = 0; i < 8; i++) {
		buf.write[offs++] = 0;
	}

	// Number of entries (total and per disk)
	offs += buf_put_int64(file_metadata.size(), &buf.write[offs]);
	offs += buf_put_int64(file_metadata.size(), &buf.write[offs]);

	// Size of central dir
	offs += buf_put_int64(central_dir_data.size(), &buf.write[offs]);

	// Central dir offset
	offs += buf_put_int64(central_dir_offset, &buf.write[offs]);

	////// ZIP64 locator

	// Write magic for zip64 central dir locator
	offs += buf_put_int32(ZIP64_END_DIR_LOCATOR_MAGIC, &buf.write[offs]);

	// Disk number
	for (int i = 0; i < 4; i++) {
		buf.write[offs++] = 0;
	}

	// Relative offset
	offs += buf_put_int64(end_of_central_dir_offset, &buf.write[offs]);

	// Number of disks
	offs += buf_put_int32(1, &buf.write[offs]);

	/////// End of zip directory

	// Write magic for end central dir
	offs += buf_put_int32(END_OF_CENTRAL_DIR_MAGIC, &buf.write[offs]);

	// Dummy stuff for Zip64
	for (int i = 0; i < 4; i++) {
		buf.write[offs++] = 0x0;
	}
	for (int i = 0; i < 12; i++) {
		buf.write[offs++] = 0xFF;
	}

	// Size of comments
	for (int i = 0; i < 2; i++) {
		buf.write[offs++] = 0;
	}

	// Done!
	return buf;
}

void AppxPackager::init(FileAccess *p_fa) {
	package = p_fa;
	central_dir_offset = 0;
	end_of_central_dir_offset = 0;
}

Error AppxPackager::add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress) {
	if (p_file_no >= 1 && p_total_files >= 1) {
		if (EditorNode::progress_task_step(progress_task, "File: " + p_file_name, (p_file_no * 100) / p_total_files)) {
			return ERR_SKIP;
		}
	}

	FileMeta meta;
	meta.name = p_file_name;
	meta.uncompressed_size = p_len;
	meta.compressed_size = p_len;<--- meta.compressed_size is assigned
	meta.compressed = p_compress;
	meta.zip_offset = package->get_position();

	Vector<uint8_t> file_buffer;

	// Data for compression
	z_stream strm;
	FileAccess *strm_f = nullptr;
	Vector<uint8_t> strm_in;
	strm_in.resize(BLOCK_SIZE);
	Vector<uint8_t> strm_out;

	if (p_compress) {
		strm.zalloc = zipio_alloc;
		strm.zfree = zipio_free;
		strm.opaque = &strm_f;

		strm_out.resize(BLOCK_SIZE + 8);

		deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
	}

	int step = 0;

	while (p_len - step > 0) {
		size_t block_size = (p_len - step) > BLOCK_SIZE ? (size_t)BLOCK_SIZE : (p_len - step);

		for (uint64_t i = 0; i < block_size; i++) {
			strm_in.write[i] = p_buffer[step + i];
		}

		BlockHash bh;
		bh.base64_hash = hash_block(strm_in.ptr(), block_size);

		if (p_compress) {
			strm.avail_in = block_size;
			strm.avail_out = strm_out.size();
			strm.next_in = (uint8_t *)strm_in.ptr();
			strm.next_out = strm_out.ptrw();

			int total_out_before = strm.total_out;

			int err = deflate(&strm, Z_FULL_FLUSH);
			ERR_FAIL_COND_V(err < 0, ERR_BUG); // Negative means bug

			bh.compressed_size = strm.total_out - total_out_before;

			//package->store_buffer(strm_out.ptr(), strm.total_out - total_out_before);
			int start = file_buffer.size();
			file_buffer.resize(file_buffer.size() + bh.compressed_size);
			for (uint64_t i = 0; i < bh.compressed_size; i++) {
				file_buffer.write[start + i] = strm_out[i];
			}
		} else {
			bh.compressed_size = block_size;
			//package->store_buffer(strm_in.ptr(), block_size);
			int start = file_buffer.size();
			file_buffer.resize(file_buffer.size() + block_size);
			for (uint64_t i = 0; i < bh.compressed_size; i++) {
				file_buffer.write[start + i] = strm_in[i];
			}
		}

		meta.hashes.push_back(bh);

		step += block_size;
	}

	if (p_compress) {
		strm.avail_in = 0;
		strm.avail_out = strm_out.size();
		strm.next_in = (uint8_t *)strm_in.ptr();
		strm.next_out = strm_out.ptrw();

		int total_out_before = strm.total_out;

		deflate(&strm, Z_FINISH);

		//package->store_buffer(strm_out.ptr(), strm.total_out - total_out_before);
		int start = file_buffer.size();
		file_buffer.resize(file_buffer.size() + (strm.total_out - total_out_before));
		for (uint64_t i = 0; i < (strm.total_out - total_out_before); i++) {
			file_buffer.write[start + i] = strm_out[i];
		}

		deflateEnd(&strm);
		meta.compressed_size = strm.total_out;<--- meta.compressed_size is overwritten

	} else {
		meta.compressed_size = p_len;
	}

	// Calculate file CRC-32
	uLong crc = crc32(0L, Z_NULL, 0);
	crc = crc32(crc, p_buffer, p_len);
	meta.file_crc32 = crc;

	// Create file header
	Vector<uint8_t> file_header = make_file_header(meta);
	meta.lfh_size = file_header.size();

	// Store the header and file;
	package->store_buffer(file_header.ptr(), file_header.size());
	package->store_buffer(file_buffer.ptr(), file_buffer.size());

	file_metadata.push_back(meta);

	return OK;
}

void AppxPackager::finish() {
	// Create and add block map file
	EditorNode::progress_task_step("export", "Creating block map...", 4);

	const String &tmp_blockmap_file_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmpblockmap.xml");
	make_block_map(tmp_blockmap_file_path);

	FileAccess *blockmap_file = FileAccess::open(tmp_blockmap_file_path, FileAccess::READ);
	Vector<uint8_t> blockmap_buffer;
	blockmap_buffer.resize(blockmap_file->get_len());

	blockmap_file->get_buffer(blockmap_buffer.ptrw(), blockmap_buffer.size());

	add_file("AppxBlockMap.xml", blockmap_buffer.ptr(), blockmap_buffer.size(), -1, -1, true);

	blockmap_file->close();
	memdelete(blockmap_file);

	// Add content types

	EditorNode::progress_task_step("export", "Setting content types...", 5);

	const String &tmp_content_types_file_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmpcontenttypes.xml");
	make_content_types(tmp_content_types_file_path);

	FileAccess *types_file = FileAccess::open(tmp_content_types_file_path, FileAccess::READ);
	Vector<uint8_t> types_buffer;
	types_buffer.resize(types_file->get_len());

	types_file->get_buffer(types_buffer.ptrw(), types_buffer.size());

	add_file("[Content_Types].xml", types_buffer.ptr(), types_buffer.size(), -1, -1, true);

	types_file->close();
	memdelete(types_file);

	// Cleanup generated files.
	DirAccess::remove_file_or_error(tmp_blockmap_file_path);
	DirAccess::remove_file_or_error(tmp_content_types_file_path);

	// Pre-process central directory before signing
	for (int i = 0; i < file_metadata.size(); i++) {
		store_central_dir_header(file_metadata[i]);
	}

	// Write central directory
	EditorNode::progress_task_step("export", "Finishing package...", 6);
	central_dir_offset = package->get_position();
	package->store_buffer(central_dir_data.ptr(), central_dir_data.size());

	// End record
	end_of_central_dir_offset = package->get_position();
	Vector<uint8_t> end_record = make_end_of_central_record();
	package->store_buffer(end_record.ptr(), end_record.size());

	package->close();
	memdelete(package);
	package = nullptr;
}

AppxPackager::AppxPackager() {}<--- Member variable 'AppxPackager::package' is not initialized in the constructor.

AppxPackager::~AppxPackager() {}

////////////////////////////////////////////////////////////////////

class EditorExportPlatformUWP : public EditorExportPlatform {
	GDCLASS(EditorExportPlatformUWP, EditorExportPlatform);

	Ref<ImageTexture> logo;

	enum Platform {
		ARM,
		X86,
		X64
	};

	bool _valid_resource_name(const String &p_name) const {
		if (p_name.empty()) {
			return false;
		}
		if (p_name.ends_with(".")) {
			return false;
		}

		static const char *invalid_names[] = {
			"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
			"COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
			nullptr
		};

		const char **t = invalid_names;
		while (*t) {
			if (p_name == *t) {
				return false;
			}
			t++;
		}

		return true;
	}

	bool _valid_guid(const String &p_guid) const {
		Vector<String> parts = p_guid.split("-");

		if (parts.size() != 5) {
			return false;
		}
		if (parts[0].length() != 8) {
			return false;
		}
		for (int i = 1; i < 4; i++) {
			if (parts[i].length() != 4) {
				return false;
			}
		}
		if (parts[4].length() != 12) {
			return false;
		}

		return true;
	}

	bool _valid_bgcolor(const String &p_color) const {
		if (p_color.empty()) {
			return true;
		}
		if (p_color.begins_with("#") && p_color.is_valid_html_color()) {
			return true;
		}

		// Colors from https://msdn.microsoft.com/en-us/library/windows/apps/dn934817.aspx
		static const char *valid_colors[] = {
			"aliceBlue", "antiqueWhite", "aqua", "aquamarine", "azure", "beige",
			"bisque", "black", "blanchedAlmond", "blue", "blueViolet", "brown",
			"burlyWood", "cadetBlue", "chartreuse", "chocolate", "coral", "cornflowerBlue",
			"cornsilk", "crimson", "cyan", "darkBlue", "darkCyan", "darkGoldenrod",
			"darkGray", "darkGreen", "darkKhaki", "darkMagenta", "darkOliveGreen", "darkOrange",
			"darkOrchid", "darkRed", "darkSalmon", "darkSeaGreen", "darkSlateBlue", "darkSlateGray",
			"darkTurquoise", "darkViolet", "deepPink", "deepSkyBlue", "dimGray", "dodgerBlue",
			"firebrick", "floralWhite", "forestGreen", "fuchsia", "gainsboro", "ghostWhite",
			"gold", "goldenrod", "gray", "green", "greenYellow", "honeydew",
			"hotPink", "indianRed", "indigo", "ivory", "khaki", "lavender",
			"lavenderBlush", "lawnGreen", "lemonChiffon", "lightBlue", "lightCoral", "lightCyan",
			"lightGoldenrodYellow", "lightGreen", "lightGray", "lightPink", "lightSalmon", "lightSeaGreen",
			"lightSkyBlue", "lightSlateGray", "lightSteelBlue", "lightYellow", "lime", "limeGreen",
			"linen", "magenta", "maroon", "mediumAquamarine", "mediumBlue", "mediumOrchid",
			"mediumPurple", "mediumSeaGreen", "mediumSlateBlue", "mediumSpringGreen", "mediumTurquoise", "mediumVioletRed",
			"midnightBlue", "mintCream", "mistyRose", "moccasin", "navajoWhite", "navy",
			"oldLace", "olive", "oliveDrab", "orange", "orangeRed", "orchid",
			"paleGoldenrod", "paleGreen", "paleTurquoise", "paleVioletRed", "papayaWhip", "peachPuff",
			"peru", "pink", "plum", "powderBlue", "purple", "red",
			"rosyBrown", "royalBlue", "saddleBrown", "salmon", "sandyBrown", "seaGreen",
			"seaShell", "sienna", "silver", "skyBlue", "slateBlue", "slateGray",
			"snow", "springGreen", "steelBlue", "tan", "teal", "thistle",
			"tomato", "transparent", "turquoise", "violet", "wheat", "white",
			"whiteSmoke", "yellow", "yellowGreen",
			nullptr
		};

		const char **color = valid_colors;

		while (*color) {
			if (p_color == *color) {
				return true;
			}
			color++;
		}

		return false;
	}

	bool _valid_image(const StreamTexture *p_image, int p_width, int p_height) const {
		if (!p_image) {
			return false;
		}

		// TODO: Add resource creation or image rescaling to enable other scales:
		// 1.25, 1.5, 2.0
		return p_width == p_image->get_width() && p_height == p_image->get_height();
	}

	Vector<uint8_t> _fix_manifest(const Ref<EditorExportPreset> &p_preset, const Vector<uint8_t> &p_template, bool p_give_internet) const {
		String result = String::utf8((const char *)p_template.ptr(), p_template.size());

		result = result.replace("$godot_version$", VERSION_FULL_NAME);

		result = result.replace("$identity_name$", p_preset->get("package/unique_name"));
		result = result.replace("$publisher$", p_preset->get("package/publisher"));

		result = result.replace("$product_guid$", p_preset->get("identity/product_guid"));
		result = result.replace("$publisher_guid$", p_preset->get("identity/publisher_guid"));

		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"));
		result = result.replace("$version_string$", version);

		Platform arch = (Platform)(int)p_preset->get("architecture/target");
		String architecture = arch == ARM ? "arm" : (arch == X86 ? "x86" : "x64");
		result = result.replace("$architecture$", architecture);

		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")));

		result = result.replace("$publisher_display_name$", p_preset->get("package/publisher_display_name"));
		result = result.replace("$app_description$", p_preset->get("package/description"));
		result = result.replace("$bg_color$", p_preset->get("images/background_color"));
		result = result.replace("$short_name$", p_preset->get("package/short_name"));

		String name_on_tiles = "";
		if ((bool)p_preset->get("tiles/show_name_on_square150x150")) {
			name_on_tiles += "          <uap:ShowOn Tile=\"square150x150Logo\" />\n";
		}
		if ((bool)p_preset->get("tiles/show_name_on_wide310x150")) {
			name_on_tiles += "          <uap:ShowOn Tile=\"wide310x150Logo\" />\n";
		}
		if ((bool)p_preset->get("tiles/show_name_on_square310x310")) {
			name_on_tiles += "          <uap:ShowOn Tile=\"square310x310Logo\" />\n";
		}

		String show_name_on_tiles = "";
		if (!name_on_tiles.empty()) {
			show_name_on_tiles = "<uap:ShowNameOnTiles>\n" + name_on_tiles + "        </uap:ShowNameOnTiles>";
		}

		result = result.replace("$name_on_tiles$", name_on_tiles);

		String rotations = "";
		if ((bool)p_preset->get("orientation/landscape")) {
			rotations += "          <uap:Rotation Preference=\"landscape\" />\n";
		}
		if ((bool)p_preset->get("orientation/portrait")) {
			rotations += "          <uap:Rotation Preference=\"portrait\" />\n";
		}
		if ((bool)p_preset->get("orientation/landscape_flipped")) {
			rotations += "          <uap:Rotation Preference=\"landscapeFlipped\" />\n";
		}
		if ((bool)p_preset->get("orientation/portrait_flipped")) {
			rotations += "          <uap:Rotation Preference=\"portraitFlipped\" />\n";
		}

		String rotation_preference = "";
		if (!rotations.empty()) {
			rotation_preference = "<uap:InitialRotationPreference>\n" + rotations + "        </uap:InitialRotationPreference>";
		}

		result = result.replace("$rotation_preference$", rotation_preference);

		String capabilities_elements = "";
		const char **basic = uwp_capabilities;
		while (*basic) {
			if ((bool)p_preset->get("capabilities/" + String(*basic))) {
				capabilities_elements += "    <Capability Name=\"" + String(*basic) + "\" />\n";
			}
			basic++;
		}
		const char **uap = uwp_uap_capabilities;
		while (*uap) {
			if ((bool)p_preset->get("capabilities/" + String(*uap))) {
				capabilities_elements += "    <uap:Capability Name=\"" + String(*uap) + "\" />\n";
			}
			uap++;
		}
		const char **device = uwp_device_capabilities;
		while (*device) {
			if ((bool)p_preset->get("capabilities/" + String(*device))) {
				capabilities_elements += "    <DeviceCapability Name=\"" + String(*device) + "\" />\n";
			}
			device++;
		}

		if (!((bool)p_preset->get("capabilities/internetClient")) && p_give_internet) {
			capabilities_elements += "    <Capability Name=\"internetClient\" />\n";
		}

		String capabilities_string = "<Capabilities />";
		if (!capabilities_elements.empty()) {
			capabilities_string = "<Capabilities>\n" + capabilities_elements + "  </Capabilities>";
		}

		result = result.replace("$capabilities_place$", capabilities_string);

		Vector<uint8_t> r_ret;
		r_ret.resize(result.length());

		for (int i = 0; i < result.length(); i++) {
			r_ret.write[i] = result.utf8().get(i);
		}

		return r_ret;
	}

	Vector<uint8_t> _get_image_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
		Vector<uint8_t> data;
		StreamTexture *image = nullptr;

		if (p_path.find("StoreLogo") != -1) {
			image = p_preset->get("images/store_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/store_logo")));
		} else if (p_path.find("Square44x44Logo") != -1) {
			image = p_preset->get("images/square44x44_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/square44x44_logo")));
		} else if (p_path.find("Square71x71Logo") != -1) {
			image = p_preset->get("images/square71x71_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/square71x71_logo")));
		} else if (p_path.find("Square150x150Logo") != -1) {
			image = p_preset->get("images/square150x150_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/square150x150_logo")));
		} else if (p_path.find("Square310x310Logo") != -1) {
			image = p_preset->get("images/square310x310_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/square310x310_logo")));
		} else if (p_path.find("Wide310x150Logo") != -1) {
			image = p_preset->get("images/wide310x150_logo").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/wide310x150_logo")));
		} else if (p_path.find("SplashScreen") != -1) {
			image = p_preset->get("images/splash_screen").is_zero() ? nullptr : Object::cast_to<StreamTexture>(((Object *)p_preset->get("images/splash_screen")));
		} else {
			ERR_PRINT("Unable to load logo");
		}

		if (!image) {
			return data;
		}

		String tmp_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("uwp_tmp_logo.png");

		Error err = image->get_data()->save_png(tmp_path);

		if (err != OK) {
			String err_string = "Couldn't save temp logo file.";

			EditorNode::add_io_error(err_string);
			ERR_FAIL_V_MSG(data, err_string);
		}

		FileAccess *f = FileAccess::open(tmp_path, FileAccess::READ, &err);

		if (err != OK) {
			String err_string = "Couldn't open temp logo file.";
			// Cleanup generated file.
			DirAccess::remove_file_or_error(tmp_path);
			EditorNode::add_io_error(err_string);
			ERR_FAIL_V_MSG(data, err_string);
		}

		data.resize(f->get_len());
		f->get_buffer(data.ptrw(), data.size());

		f->close();
		memdelete(f);
		DirAccess::remove_file_or_error(tmp_path);

		return data;
	}

	static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
		/* TODO: This was copied verbatim from Android export. It should be
		 * refactored to the parent class and also be used for .zip export.
		 */

		/*
		 *  By not compressing files with little or not benefit in doing so,
		 *  a performance gain is expected at runtime. Moreover, if the APK is
		 *  zip-aligned, assets stored as they are can be efficiently read by
		 *  Android by memory-mapping them.
		 */

		// -- Unconditional uncompress to mimic AAPT plus some other

		static const char *unconditional_compress_ext[] = {
			// From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
			// These formats are already compressed, or don't compress well:
			".jpg", ".jpeg", ".png", ".gif",
			".wav", ".mp2", ".mp3", ".ogg", ".aac",
			".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
			".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
			".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
			".amr", ".awb", ".wma", ".wmv",
			// Godot-specific:
			".webp", // Same reasoning as .png
			".cfb", // Don't let small config files slow-down startup
			".scn", // Binary scenes are usually already compressed
			".stex", // Streamable textures are usually already compressed
			// Trailer for easier processing
			nullptr
		};

		for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
			if (p_path.to_lower().ends_with(String(*ext))) {
				return false;
			}
		}

		// -- Compressed resource?

		if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
			// Already compressed
			return false;
		}

		// --- TODO: Decide on texture resources according to their image compression setting

		return true;
	}

	static Error save_appx_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
		AppxPackager *packager = (AppxPackager *)p_userdata;
		String dst_path = p_path.replace_first("res://", "game/");

		return packager->add_file(dst_path, p_data.ptr(), p_data.size(), p_file, p_total, _should_compress_asset(p_path, p_data));
	}

public:
	virtual String get_name() const {
		return "UWP";
	}
	virtual String get_os_name() const {
		return "UWP";
	}

	virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
		List<String> list;
		list.push_back("appx");
		return list;
	}

	virtual Ref<Texture> get_logo() const {
		return logo;
	}

	virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
		r_features->push_back("s3tc");
		r_features->push_back("etc");
		switch ((int)p_preset->get("architecture/target")) {
			case EditorExportPlatformUWP::ARM: {
				r_features->push_back("arm");
			} break;
			case EditorExportPlatformUWP::X86: {
				r_features->push_back("32");
			} break;
			case EditorExportPlatformUWP::X64: {
				r_features->push_back("64");
			} break;
		}
	}

	virtual void get_export_options(List<ExportOption> *r_options) {
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));

		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "architecture/target", PROPERTY_HINT_ENUM, "arm,x86,x64"), 1));

		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));

		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/display_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/short_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game.Name"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/description"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher", PROPERTY_HINT_PLACEHOLDER_TEXT, "CN=CompanyName"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher_display_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));

		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/product_guid", PROPERTY_HINT_PLACEHOLDER_TEXT, "00000000-0000-0000-0000-000000000000"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/publisher_guid", PROPERTY_HINT_PLACEHOLDER_TEXT, "00000000-0000-0000-0000-000000000000"), ""));

		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "signing/certificate", PROPERTY_HINT_GLOBAL_FILE, "*.pfx"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "signing/password"), ""));
		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "signing/algorithm", PROPERTY_HINT_ENUM, "MD5,SHA1,SHA256"), 2));

		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/major"), 1));
		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/minor"), 0));
		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/build"), 0));
		r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/revision"), 0));

		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape"), true));
		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_flipped"), true));
		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_flipped"), true));

		r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "images/background_color"), "transparent"));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/store_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square44x44_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square71x71_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square150x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square310x310_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/wide310x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));
		r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/splash_screen", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture"), Variant()));

		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square150x150"), false));
		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_wide310x150"), false));
		r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square310x310"), false));

		// Capabilities
		const char **basic = uwp_capabilities;
		while (*basic) {
			r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*basic)), false));
			basic++;
		}

		const char **uap = uwp_uap_capabilities;
		while (*uap) {
			r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*uap)), false));
			uap++;
		}

		const char **device = uwp_device_capabilities;
		while (*device) {
			r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*device)), false));
			device++;
		}
	}

	virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
		String err;
		bool valid = false;

		// Look for export templates (first official, and if defined custom templates).

		Platform arch = (Platform)(int)(p_preset->get("architecture/target"));
		String platform_infix;
		switch (arch) {
			case EditorExportPlatformUWP::ARM: {
				platform_infix = "arm";
			} break;
			case EditorExportPlatformUWP::X86: {
				platform_infix = "x86";
			} break;
			case EditorExportPlatformUWP::X64: {
				platform_infix = "x64";
			} break;
		}

		bool dvalid = exists_export_template("uwp_" + platform_infix + "_debug.zip", &err);
		bool rvalid = exists_export_template("uwp_" + platform_infix + "_release.zip", &err);

		if (p_preset->get("custom_template/debug") != "") {
			dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
			if (!dvalid) {
				err += TTR("Custom debug template not found.") + "\n";
			}
		}
		if (p_preset->get("custom_template/release") != "") {
			rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
			if (!rvalid) {
				err += TTR("Custom release template not found.") + "\n";
			}
		}

		valid = dvalid || rvalid;

#ifdef MODULE_MONO_ENABLED
		// If this is a Mono build, provide a custom error so that users are not confused.
		// We don't bypass the whole logic to check templates because third-parties might have
		// Mono-enabled UWP builds using this path.
		r_missing_templates = false; // Don't warn about those.
		r_error = TTR("Godot's Mono version does not support the UWP platform. Use the standard build (no C# support) if you wish to target UWP.");
#else
		r_missing_templates = !valid;

		if (!err.empty()) {
			r_error = err;
		}
#endif // MODULE_MONO_ENABLED

		return valid;
	}

	virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
		String err;
		bool valid = true;

#ifdef MODULE_MONO_ENABLED
		// Don't warn about project configuration issue if this is a Mono build
		// without custom-provided Mono-enabled UWP templates.
		// We check if we have valid templates to decide if we should actually
		// validate the config.
		bool tmp;
		if (!has_valid_export_configuration(p_preset, err, tmp)) {
			return false;
		}
#endif // MODULE_MONO_ENABLED

		// Validate the rest of the configuration.

		if (!_valid_resource_name(p_preset->get("package/short_name"))) {
			valid = false;
			err += TTR("Invalid package short name.") + "\n";
		}

		if (!_valid_resource_name(p_preset->get("package/unique_name"))) {
			valid = false;
			err += TTR("Invalid package unique name.") + "\n";
		}

		if (!_valid_resource_name(p_preset->get("package/publisher_display_name"))) {
			valid = false;
			err += TTR("Invalid package publisher display name.") + "\n";
		}

		if (!_valid_guid(p_preset->get("identity/product_guid"))) {
			valid = false;
			err += TTR("Invalid product GUID.") + "\n";
		}

		if (!_valid_guid(p_preset->get("identity/publisher_guid"))) {
			valid = false;
			err += TTR("Invalid publisher GUID.") + "\n";
		}

		if (!_valid_bgcolor(p_preset->get("images/background_color"))) {
			valid = false;
			err += TTR("Invalid background color.") + "\n";
		}

		if (!p_preset->get("images/store_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/store_logo"))), 50, 50)) {
			valid = false;
			err += TTR("Invalid Store Logo image dimensions (should be 50x50).") + "\n";
		}

		if (!p_preset->get("images/square44x44_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/square44x44_logo"))), 44, 44)) {
			valid = false;
			err += TTR("Invalid square 44x44 logo image dimensions (should be 44x44).") + "\n";
		}

		if (!p_preset->get("images/square71x71_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/square71x71_logo"))), 71, 71)) {
			valid = false;
			err += TTR("Invalid square 71x71 logo image dimensions (should be 71x71).") + "\n";
		}

		if (!p_preset->get("images/square150x150_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/square150x150_logo"))), 150, 150)) {
			valid = false;
			err += TTR("Invalid square 150x150 logo image dimensions (should be 150x150).") + "\n";
		}

		if (!p_preset->get("images/square310x310_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/square310x310_logo"))), 310, 310)) {
			valid = false;
			err += TTR("Invalid square 310x310 logo image dimensions (should be 310x310).") + "\n";
		}

		if (!p_preset->get("images/wide310x150_logo").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/wide310x150_logo"))), 310, 150)) {
			valid = false;
			err += TTR("Invalid wide 310x150 logo image dimensions (should be 310x150).") + "\n";
		}

		if (!p_preset->get("images/splash_screen").is_zero() && !_valid_image((Object::cast_to<StreamTexture>((Object *)p_preset->get("images/splash_screen"))), 620, 300)) {
			valid = false;
			err += TTR("Invalid splash screen image dimensions (should be 620x300).") + "\n";
		}

		r_error = err;
		return valid;
	}

#ifdef WINDOWS_ENABLED
	void extract_all_files_from_zip(String zip_file_path, String extract_path) {
		FileAccess *src_f = nullptr;
		zlib_filefunc_def io = zipio_create_io_from_file(&src_f);

		unzFile pkg = unzOpen2(zip_file_path.utf8().get_data(), &io);

		int ret = unzGoToFirstFile(pkg);

		while (ret == UNZ_OK) {
			// get file name
			unz_file_info info;
			char fname[16384];
			ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);<--- ret is assigned

			String path = String::utf8(fname);

			//write the files
			Vector<uint8_t> data;

			//read
			data.resize(info.uncompressed_size);
			unzOpenCurrentFile(pkg);
			unzReadCurrentFile(pkg, data.ptrw(), data.size());
			unzCloseCurrentFile(pkg);

			//check if the subfolder doesn't exist and create it
			String file_path = extract_path + "/" + path.get_base_dir();
			DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
			if (!da->dir_exists(file_path)) {
				da->make_dir_recursive(file_path);
			}
			memdelete(da);

			FileAccess *unzipped_file = FileAccess::open(extract_path + "/" + path, FileAccess::WRITE);
			unzipped_file->store_buffer(data.ptrw(), data.size());
			unzipped_file->close();

			ret = unzGoToNextFile(pkg);<--- ret is overwritten
		}

		unzClose(pkg);
	}
#endif

	virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) {
		ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);

		String src_appx;

		EditorProgress ep("export", "Exporting for UWP", 7, true);

		if (p_debug) {
			src_appx = p_preset->get("custom_template/debug");
		} else {
			src_appx = p_preset->get("custom_template/release");
		}

		src_appx = src_appx.strip_edges();

		Platform arch = (Platform)(int)p_preset->get("architecture/target");

		if (src_appx == "") {
			String err, infix;
			switch (arch) {
				case ARM: {
					infix = "_arm_";
				} break;
				case X86: {
					infix = "_x86_";
				} break;
				case X64: {
					infix = "_x64_";
				} break;
			}
			if (p_debug) {
				src_appx = find_export_template("uwp" + infix + "debug.zip", &err);
			} else {
				src_appx = find_export_template("uwp" + infix + "release.zip", &err);
			}
			if (src_appx == "") {
				EditorNode::add_io_error(err);
				return ERR_FILE_NOT_FOUND;
			}
		}

		if (!DirAccess::exists(p_path.get_base_dir())) {
			return ERR_FILE_BAD_PATH;
		}

		Error err = OK;<--- Shadowed declaration

		FileAccess *fa_pack = FileAccess::open(p_path, FileAccess::WRITE, &err);
		ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");

		AppxPackager packager;
		packager.init(fa_pack);

		FileAccess *src_f = nullptr;
		zlib_filefunc_def io = zipio_create_io_from_file(&src_f);

		if (ep.step("Creating package...", 0)) {
			return ERR_SKIP;
		}

		unzFile pkg = unzOpen2(src_appx.utf8().get_data(), &io);

		if (!pkg) {
			EditorNode::add_io_error("Could not find template appx to export:\n" + src_appx);
			return ERR_FILE_NOT_FOUND;
		}

		int ret = unzGoToFirstFile(pkg);

		if (ep.step("Copying template files...", 1)) {
			return ERR_SKIP;
		}

		EditorNode::progress_add_task("template_files", "Template files", 100);
		packager.set_progress_task("template_files");

		int template_files_amount = 9;
		int template_file_no = 1;

		while (ret == UNZ_OK) {
			// get file name
			unz_file_info info;
			char fname[16384];
			ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);<--- ret is assigned

			String path = String::utf8(fname);

			if (path.ends_with("/")) {
				// Ignore directories
				ret = unzGoToNextFile(pkg);
				continue;
			}

			Vector<uint8_t> data;
			bool do_read = true;

			if (path.begins_with("Assets/")) {
				path = path.replace(".scale-100", "");

				data = _get_image_data(p_preset, path);
				if (data.size() > 0) {
					do_read = false;
				}
			}

			//read
			if (do_read) {
				data.resize(info.uncompressed_size);
				unzOpenCurrentFile(pkg);
				unzReadCurrentFile(pkg, data.ptrw(), data.size());
				unzCloseCurrentFile(pkg);
			}

			if (path == "AppxManifest.xml") {
				data = _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
			}

			print_line("ADDING: " + path);

			err = packager.add_file(path, data.ptr(), data.size(), template_file_no++, template_files_amount, _should_compress_asset(path, data));
			if (err != OK) {
				return err;
			}

			ret = unzGoToNextFile(pkg);<--- ret is overwritten
		}

		EditorNode::progress_end_task("template_files");

		if (ep.step("Creating command line...", 2)) {
			return ERR_SKIP;
		}

		Vector<String> cl = ((String)p_preset->get("command_line/extra_args")).strip_edges().split(" ");
		for (int i = 0; i < cl.size(); i++) {
			if (cl[i].strip_edges().length() == 0) {
				cl.remove(i);
				i--;
			}
		}

		if (!(p_flags & DEBUG_FLAG_DUMB_CLIENT)) {
			cl.push_back("--path");
			cl.push_back("game");
		}

		gen_export_flags(cl, p_flags);

		// Command line file
		Vector<uint8_t> clf;

		// Argc
		clf.resize(4);
		encode_uint32(cl.size(), clf.ptrw());

		for (int i = 0; i < cl.size(); i++) {
			CharString txt = cl[i].utf8();
			int base = clf.size();
			clf.resize(base + 4 + txt.length());
			encode_uint32(txt.length(), &clf.write[base]);
			memcpy(&clf.write[base + 4], txt.ptr(), txt.length());
			print_line(itos(i) + " param: " + cl[i]);
		}

		err = packager.add_file("__cl__.cl", clf.ptr(), clf.size(), -1, -1, false);
		if (err != OK) {
			return err;
		}

		if (ep.step("Adding project files...", 3)) {
			return ERR_SKIP;
		}

		EditorNode::progress_add_task("project_files", "Project Files", 100);
		packager.set_progress_task("project_files");

		err = export_project_files(p_preset, save_appx_file, &packager, copy_shared_objects);

		EditorNode::progress_end_task("project_files");

		if (ep.step("Closing package...", 7)) {
			return ERR_SKIP;
		}

		unzClose(pkg);

		packager.finish();

#ifdef WINDOWS_ENABLED
		// Repackage it with makeappx if available
		String makeappx_path = EditorSettings::get_singleton()->get("export/uwp/makeappx");
		if (makeappx_path != String()) {
			if (FileAccess::exists(makeappx_path)) {
				// Get uwp_temp_path
				String uwp_temp_path = EditorSettings::get_singleton()->get_cache_dir() + "/uwptemp";

				// Extract current appx file
				extract_all_files_from_zip(p_path, uwp_temp_path);

				// Call makeappx
				List<String> args_makeappx;
				args_makeappx.push_back("pack");
				args_makeappx.push_back("/d");
				args_makeappx.push_back(uwp_temp_path);
				args_makeappx.push_back("/p");
				args_makeappx.push_back(p_path);
				args_makeappx.push_back("/o");

				OS::get_singleton()->execute(makeappx_path, args_makeappx, true);

				// Delete uwp_temp_path folder recursively
				DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
				Error err = da->change_dir(uwp_temp_path);<--- Shadow variable
				if (err == OK) {
					err = da->erase_contents_recursive();
					if (err != OK) {
						ERR_PRINT("Could not delete UWP temporary folder: '" + uwp_temp_path + "'.");
						return err;
					} else {
						da->remove(uwp_temp_path);
					}
				} else {
					ERR_PRINT("Could not change dir to UWP temporary folder: '" + uwp_temp_path + "'.");
					ERR_PRINT("Could not delete UWP temporary folder: '" + uwp_temp_path + "'.");
					return err;
				}
				memdelete(da);
			} else {
				ERR_PRINT("Could not find makeappx executable at " + makeappx_path + ", aborting.");
				return ERR_FILE_NOT_FOUND;
			}
		}

		// Sign with signtool
		String signtool_path = EditorSettings::get_singleton()->get("export/uwp/signtool");
		if (signtool_path == String()) {
			return OK;
		}

		if (!FileAccess::exists(signtool_path)) {
			ERR_PRINT("Could not find signtool executable at " + signtool_path + ", aborting.");
			return ERR_FILE_NOT_FOUND;
		}

		static String algs[] = { "MD5", "SHA1", "SHA256" };

		String cert_path = EditorSettings::get_singleton()->get("export/uwp/debug_certificate");
		String cert_pass = EditorSettings::get_singleton()->get("export/uwp/debug_password");
		int cert_alg = EditorSettings::get_singleton()->get("export/uwp/debug_algorithm");

		if (!p_debug) {
			cert_path = p_preset->get("signing/certificate");
			cert_pass = p_preset->get("signing/password");
			cert_alg = p_preset->get("signing/algorithm");
		}

		if (cert_path == String()) {
			return OK; // Certificate missing, don't try to sign
		}

		if (!FileAccess::exists(cert_path)) {
			ERR_PRINT("Could not find certificate file at " + cert_path + ", aborting.");
			return ERR_FILE_NOT_FOUND;
		}

		if (cert_alg < 0 || cert_alg > 2) {
			ERR_PRINT("Invalid certificate algorithm " + itos(cert_alg) + ", aborting.");
			return ERR_INVALID_DATA;
		}

		List<String> args;
		args.push_back("sign");
		args.push_back("/fd");
		args.push_back(algs[cert_alg]);
		args.push_back("/a");
		args.push_back("/f");
		args.push_back(cert_path);
		args.push_back("/p");
		args.push_back(cert_pass);
		args.push_back(p_path);

		OS::get_singleton()->execute(signtool_path, args, true);
#endif // WINDOWS_ENABLED

		return OK;
	}

	virtual void get_platform_features(List<String> *r_features) {
		r_features->push_back("pc");
		r_features->push_back("UWP");
	}

	virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
	}

	static Error copy_shared_objects(void *p_userdata, const SharedObject &p_so) {
		return save_appx_file(p_userdata, p_so.path, FileAccess::get_file_as_array(p_so.path), 0, 1);
	}

	EditorExportPlatformUWP() {
		Ref<Image> img = memnew(Image(_uwp_logo));
		logo.instance();
		logo->create_from_image(img);
	}
};

void register_uwp_exporter() {
#ifdef WINDOWS_ENABLED
	EDITOR_DEF("export/uwp/signtool", "");
	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/uwp/signtool", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
	EDITOR_DEF("export/uwp/makeappx", "");
	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/uwp/makeappx", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
	EDITOR_DEF("export/uwp/debug_certificate", "");
	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/uwp/debug_certificate", PROPERTY_HINT_GLOBAL_FILE, "*.pfx"));
	EDITOR_DEF("export/uwp/debug_password", "");
	EDITOR_DEF("export/uwp/debug_algorithm", 2); // SHA256 is the default
	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "export/uwp/debug_algorithm", PROPERTY_HINT_ENUM, "MD5,SHA1,SHA256"));
#endif // WINDOWS_ENABLED

	Ref<EditorExportPlatformUWP> exporter;
	exporter.instance();
	EditorExport::get_singleton()->add_export_platform(exporter);
}