This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstaller.cs
More file actions
1024 lines (981 loc) · 38.3 KB
/
Copy pathInstaller.cs
File metadata and controls
1024 lines (981 loc) · 38.3 KB
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
using IWshRuntimeLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using File = System.IO.File;
namespace GlestInstaller {
/// <summary>
/// The installer window
/// </summary>
public class Installer : Form {
private const string UninstallKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
private static int bitness = Marshal.SizeOf(typeof(IntPtr)) * 8;
private RichTextBox licenseTextBox;
private PictureBox pictureBox1;
private Button installButton;
private TextBox pathTextBox;
private Label label2;
private CheckBox desktopShortcutCheckBox;
private CheckBox startMenuShortcutCheckBox;
private CheckBox networkFixCheckBox;
private ProgressBar progressBar;
private Button pathButton;
private int start, target; //progress is from 0 to 1000
private static bool closing, busy;
private static Process sevenZip;
private static StringBuilder sevenZipOutput;
private static ConfigParams Config;
private static WebClient binariesClient, dataClient;
private static ManualResetEventSlim dataResetEvent = new ManualResetEventSlim(false);
private static int lineNumber;
/// <summary>
/// Constructs the installer form
/// </summary>
public Installer() {
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072;
}
/// <summary>
/// The main entry point for the application
/// </summary>
[STAThread]
public static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Installer());
}
/// <summary>
/// Called when the form is loaded
/// </summary>
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
licenseTextBox.SelectAll();
licenseTextBox.SelectionAlignment = HorizontalAlignment.Center;
licenseTextBox.DeselectAll();
UIScaler.AddToScaler(this);
UIScaler.ExcludeFontScaling(licenseTextBox);
pathTextBox.Text = GetDefaultInstallPath();
}
private static string GetDefaultInstallPath() {
return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + Path.DirectorySeparatorChar + "Glest";
}
/// <summary>
/// Called when the form is shown
/// </summary>
protected override void OnShown(EventArgs e) {
base.OnShown(e);
try {
InitConfig();
} catch (Exception ex) {
MessageBox.Show("Could not load configuration: " + ExceptionToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
return;
}
string version;
string path = CheckIsInstalled(out version);
if (path != null) {
try {
pathTextBox.Text = path;
int update = new Version(Config.Version).CompareTo(new Version(version));
if (update > 0)
installButton.Text = "Agree && Update";
else if (update < 0)
installButton.Text = "Agree && Downgrade";
else if (update == 0) {
installButton.Text = "Agree && Reinstall";
if (MessageBox.Show("The same version of Glest seems to already be installed. Do you want to uninstall?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
Thread thread = new Thread(() => StartUninstall(false));
thread.Name = "UninstallThread";
thread.IsBackground = true;
thread.Start();
}
}
} catch {
}
}
}
/// <summary>
/// Initializes the installation configuration
/// </summary>
private static void InitConfig() {
if (Config == null) {
KeyValueConfigurationCollection collection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings;
Config = new ConfigParams(collection["version"].Value.Trim(),
new Uri(collection["binaries"].Value.Trim()),
collection["binaries-dir"].Value.Trim(),
collection["binaries-md5"].Value.Trim().Replace("-", string.Empty).ToLower(),
new Uri(collection["data"].Value.Trim()),
collection["data-dir"].Value.Trim(),
collection["data-md5"].Value.Trim().Replace("-", string.Empty).ToLower(),
long.Parse(collection["data-bytes"].Value.Trim()),
int.Parse(collection["data-7zlinecount"].Value.Trim()));
}
}
/// <summary>
/// Called when the path selection button is clicked
/// </summary>
private void pathButton_Click(object sender, EventArgs e) {
using (FolderBrowserDialog dialog = new FolderBrowserDialog()) {
dialog.SelectedPath = pathTextBox.Text;
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() == DialogResult.OK)
pathTextBox.Text = dialog.SelectedPath + Path.DirectorySeparatorChar + "Glest";
}
}
/// <summary>
/// Called when the "Install" button is clicked
/// </summary>
private void installButton_Click(object sender, EventArgs e) {
Thread thread = new Thread(StartInstall);
thread.Name = "InstallThread";
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// Sets the progress bar value
/// </summary>
/// <param name="value">The progress value where 0 is no progress and 1000 is finished progress</param>
private void SetProgress(int value) {
try {
//Invoke(new Action(() => {
ThreadPool.QueueUserWorkItem(val => progressBar.Value = (int) val, value);
//}));
} catch {
}
}
/// <summary>
/// Sets the status button text
/// </summary>
/// <param name="status">The text to show</param>
private void SetButtonText(string status) {
try {
//Invoke(new Action(() => {
installButton.Text = status;
//}));
} catch {
}
}
private static string ExceptionToString(Exception ex) {
if (ex == null)
return string.Empty;
try {
using (StreamWriter log = File.CreateText("error.log"))
log.WriteLine(ex.StackTrace);
} catch {
}
StringBuilder builder = new StringBuilder();
while (ex != null) {
builder.Append(ex.Message + " ");
ex = ex.InnerException;
}
return builder.ToString();
}
/// <summary>
/// Shows a message box on the form thread
/// </summary>
private static DialogResult ShowMessageBox(string message, string title, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information) {
try {
return /*(DialogResult) Invoke(new Func<object>(() => */MessageBox.Show(message, title, buttons, icon)/*))*/;
} catch {
Environment.Exit(1);
return DialogResult.None;
}
}
/// <summary>
/// Presents a dialog that shows that the installation or uninstallation was a success
/// </summary>
/// <param name="uninstall">Whether what was successful was an uninstallation</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
private void ShowSuccess(bool uninstall, List<string> warnings) {
installButton.Text = "Success!";
StringBuilder message = new StringBuilder();
MessageBoxIcon icon;
if (warnings.Count == 0) {
message.Append("Glest" + (uninstall ? " un" : " ") + "installed successfully!");
icon = MessageBoxIcon.Information;
} else {
message.Append("Glest is now" + (uninstall ? " un" : " ") + "installed, although ");
if (warnings.Count == 1)
message.Append("some errors were");
else
message.Append("an error was");
message.Append(" encountered during" + (uninstall ? " un" : " ") + "installation:\n");
foreach (string error in warnings) {
message.Append(" - ");
message.AppendLine(error);
}
icon = MessageBoxIcon.Warning;
}
ShowMessageBox(message.ToString(), "Installation Status", MessageBoxButtons.OK, icon);
}
/// <summary>
/// Configures the binaries to their respective path
/// </summary>
/// <param name="path">The path that contains the extracted binary archive</param>
public static void ConfigureBinaries(string path) {
string target;
foreach (string file in Directory.GetFiles(path + Path.DirectorySeparatorChar + Config.BinariesDir + Path.DirectorySeparatorChar + "vs2019", "*", SearchOption.AllDirectories)) {
target = path + Path.DirectorySeparatorChar + Path.GetFileName(file);
DeleteFileIfExists(target);
File.Move(file, target);
}
string location = Assembly.GetEntryAssembly().Location;
target = path + Path.DirectorySeparatorChar + nameof(GlestInstaller) + ".exe";
File.Copy(location, target, true);
File.Copy(Path.ChangeExtension(location, ".exe.config"), target + ".config", true);
}
/// <summary>
/// Configures the data to its respective path
/// </summary>
/// <param name="path">The path that contains the extracted data archive</param>
public static void ConfigureData(string path) {
path += Path.DirectorySeparatorChar;
if (!string.Equals(Config.DataDir, "data", StringComparison.InvariantCultureIgnoreCase)) {
string target = path + "data";
DeleteFolderIfExists(target);
Directory.Move(path + Config.DataDir, target);
}
}
/// <summary>
/// Returns the MD5 checksum of the specified file
/// </summary>
/// <param name="file">The file whose checksum to calculate</param>
public static string CalculateMD5(string file) {
using (BufferedStream stream = new BufferedStream(File.OpenRead(file), 1200000)) {
using (MD5Cng md5 = new MD5Cng())
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLower();
}
}
/// <summary>
/// Downloads the archives and extracts and configures the files
/// </summary>
/// <param name="path">The path to install into</param>
public void DownloadAndExtract(string path) {
string binariesPath = path + Path.DirectorySeparatorChar + "binaries.zip";
string dataPath = path + Path.DirectorySeparatorChar + "data.zip";
dataClient = new WebClient();
try {
dataClient.Headers.Add("user-agent", "Glest");
//dataClient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
dataClient.Proxy = null;
target = 900;
Exception downloadError = null;
if (File.Exists(dataPath) && CalculateMD5(dataPath) == Config.DataMD5) {
dataResetEvent.Set();
SetProgress(start = target = 100);
} else {
dataResetEvent.Reset();
DownloadProgressChangedEventHandler callback = null;
callback = (sender, e) => {
long total = e == null || e.TotalBytesToReceive <= 0L ? Config.DataBytes : e.TotalBytesToReceive;
int progress = start + (int) ((target - start) * e.BytesReceived / (double) total);
if (progress < start)
progress = start;
else if (progress > target)
progress = target;
SetProgress(progress);
Application.DoEvents();
};
dataClient.DownloadProgressChanged += callback;
dataClient.DownloadFileCompleted += (sender, e) => {
dataClient.DownloadProgressChanged -= callback;
if (e == null || e.Error == null)
SetButtonText("Extracting data...\n(takes some time)");
else
downloadError = e.Error;
dataResetEvent.Set();
};
dataClient.DownloadFileAsync(Config.DataUrl, dataPath);
}
if (File.Exists(binariesPath) && CalculateMD5(binariesPath) == Config.BinariesMD5)
SetButtonText("Downloading data...\n(takes some time)");
else {
binariesClient = new WebClient();
try {
binariesClient.Headers.Add("user-agent", "Glest");
//binaryClient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
binariesClient.Proxy = null;
SetButtonText("Downloading binaries...\n(takes some time)");
binariesClient.DownloadFile(Config.BinariesUrl, binariesPath);
} finally {
if (binariesClient != null) {
binariesClient.Dispose();
binariesClient = null;
}
}
}
SetButtonText("Downloading data...\n(takes some time)");
if (closing)
Thread.Sleep(-1); //pause
else if (downloadError != null)
throw downloadError;
else if (CalculateMD5(binariesPath) != Config.BinariesMD5)
throw new WarningException("MD5 hash of binaries.zip does not match the one specified in config file");
Extract(binariesPath, path, false);
ConfigureBinaries(path);
dataResetEvent.Wait();
if (downloadError != null)
throw downloadError;
else if (closing)
return;
SetButtonText("Extracting...");
start = target;
target = 980;
try {
//Invoke(new Action(() => {
progressBar.Style = ProgressBarStyle.Continuous;
//}));
} catch {
if (closing)
Thread.Sleep(-1); //pause
}
if (CalculateMD5(dataPath) != Config.DataMD5)
throw new WarningException("MD5 hash of data.zip does not match the one specified in config file");
Extract(dataPath, path, true);
ConfigureData(path);
target = 1000;
SetProgress(target);
} finally {
if (dataClient != null) {
dataClient.Dispose();
dataClient = null;
}
}
}
/// <summary>
/// Extracts the specified archive into the specified directory
/// </summary>
/// <param name="path">The path to the archive to extract</param>
/// <param name="dir">The directory to extract to</param>
/// <param name="monitor">Whether to monitor the extraction progress</param>
public void Extract(string path, string dir, bool monitor) {
lineNumber = 0;
sevenZipOutput = new StringBuilder();
sevenZip = new Process();
try {
sevenZip.StartInfo.FileName = "7z.exe";
sevenZip.StartInfo.Arguments = "x -y -o\"" + dir + "\" \"" + path + "\"";
sevenZip.StartInfo.UseShellExecute = false;
sevenZip.StartInfo.CreateNoWindow = true;
sevenZip.StartInfo.RedirectStandardOutput = true;
sevenZip.OutputDataReceived += (sender, e) => {
sevenZipOutput.AppendLine(e.Data);
if (monitor) {
lineNumber++;
int progress = start + (int) ((target - start) * lineNumber / (double) Config.Data7zLineCount);
if (progress < start)
progress = start;
else if (progress > target)
progress = target;
SetProgress(progress);
Application.DoEvents();
}
};
if (!sevenZip.Start())
throw new FileLoadException("7z could not start");
sevenZip.BeginOutputReadLine();
do {
Application.DoEvents();
} while (!sevenZip.WaitForExit(400));
} finally {
if (sevenZip != null) {
sevenZip.Dispose();
sevenZip = null;
}
}
string output = sevenZipOutput.ToString();
int index = output.IndexOf("Error:");
if (index != -1)
throw new ArgumentException(output.Substring(index + 6).Trim().Replace("\n", "").Replace("\r", ""));
}
/// <summary>
/// Recursively calculates the directory size
/// </summary>
/// <param name="dir">The directory whose size to calculate.</param>
[CLSCompliant(false)]
public static ulong CalcutateDirectorySize(DirectoryInfo dir) {
ulong size = 0ul;
foreach (FileInfo file in dir.GetFiles()) //add file sizes
size += (ulong) file.Length;
foreach (DirectoryInfo d in dir.GetDirectories()) //add subdirectory sizes
size += CalcutateDirectorySize(d);
return size;
}
/// <summary>
/// Registers the application and configures registry
/// </summary>
/// <param name="path">The path to the installed files</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public void Register(string path, List<string> warnings) {
try {
using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(UninstallKeyPath, true)) {
if (parent == null)
throw new Exception("Uninstall registry key not found.");
RegistryKey key = null;
try {
string guidString = GetGuidString();
key = parent.OpenSubKey(guidString, true) ??
parent.CreateSubKey(guidString);
if (key == null)
throw new KeyNotFoundException(string.Format("Unable to create uninstaller '{0}\\{1}'", UninstallKeyPath, guidString));
string uninstallerPath = "\"" + path + "\\" + nameof(GlestInstaller) + ".exe\"";
key.SetValue("DisplayName", "Glest");
key.SetValue("ApplicationVersion", Config.Version);
key.SetValue("Publisher", "Glest Team");
key.SetValue("DisplayIcon", uninstallerPath);
key.SetValue("DisplayVersion", Config.Version);
key.SetValue("URLInfoAbout", "https://glest.github.io/");
key.SetValue("Contact", "mathusum.mut@gmail.com");
key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
key.SetValue("UninstallString", uninstallerPath);
key.SetValue("EstimatedSize", (int) (CalcutateDirectorySize(new DirectoryInfo(path)) / 1024ul), RegistryValueKind.DWord);
} finally {
if (key != null)
key.Close();
}
}
if (networkFixCheckBox.Checked) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile", true)) {
if (key == null)
throw new Exception("Network throttling key not found.");
try {
key.SetValue("NetworkThrottlingIndex", -1, RegistryValueKind.DWord);
} finally {
if (key != null)
key.Close();
}
}
}
} catch (Exception e) {
warnings.Add("Could not write to registry: " + ExceptionToString(e));
}
}
/// <summary>
/// Creates the selected shortcuts
/// </summary>
/// <param name="path">The path to the installed files</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public void CreateShortcuts(string path, List<string> warnings) {
string exePath = path + Path.DirectorySeparatorChar + "glest-" + bitness + ".exe";
if (startMenuShortcutCheckBox.Checked) {
try {
string shortcutDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "Glest");
Directory.CreateDirectory(shortcutDirectory);
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut) shell.CreateShortcut(Path.Combine(shortcutDirectory, "Glest.lnk"));
shortcut.Description = "Glest 3D RTS Game";
shortcut.WorkingDirectory = path;
shortcut.TargetPath = exePath;
shortcut.Save();
shortcut = (IWshShortcut) shell.CreateShortcut(Path.Combine(shortcutDirectory, "Map Editor.lnk"));
shortcut.Description = "Map Editor for Glest 3D RTS Game";
shortcut.WorkingDirectory = path;
shortcut.TargetPath = path + Path.DirectorySeparatorChar + "map_editor-" + bitness + ".exe";
shortcut.Save();
shortcut = (IWshShortcut) shell.CreateShortcut(Path.Combine(shortcutDirectory, "G3D Viewer.lnk"));
shortcut.Description = "Model Viewer for Glest 3D RTS Game";
shortcut.WorkingDirectory = path;
shortcut.TargetPath = path + Path.DirectorySeparatorChar + "g3d_viewer-" + bitness + ".exe";
shortcut.Save();
/*shortcut = (IWshShortcut) shell.CreateShortcut(Path.Combine(shortcutDirectory, "Uninstall Glest.lnk"));
shortcut.Description = "Uninstalls Glest 3D RTS Game";
shortcut.WorkingDirectory = path;
shortcut.TargetPath = path + Path.DirectorySeparatorChar + nameof(GlestInstaller) + ".exe";
shortcut.Save();*/
} catch (Exception e) {
warnings.Add("Could not add start menu shortcut: " + ExceptionToString(e));
}
}
if (desktopShortcutCheckBox.Checked) {
try {
IWshShortcut shortcut = (IWshShortcut) new WshShell().CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar + "Glest.lnk");
shortcut.Description = "Glest 3D RTS Game";
shortcut.WorkingDirectory = path;
shortcut.TargetPath = exePath;
shortcut.Save();
} catch (Exception e) {
warnings.Add("Could not add desktop shortcut: " + ExceptionToString(e));
}
}
}
/// <summary>
/// Installs into to the specified path
/// </summary>
/// <param name="path">The path to install in</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public void Install(string path, List<string> warnings) {
Directory.CreateDirectory(path);
DownloadAndExtract(path);
DeleteTemp(path, warnings);
Register(path, warnings);
CreateShortcuts(path, warnings);
}
/// <summary>
/// Deletes the specified file if it exists
/// </summary>
/// <param name="path">The path to the file to delete</param>
/// <param name="timeout">The approximate timeout in milliseconds</param>
public static void DeleteFileIfExists(string path, int timeout = int.MaxValue) {
if (File.Exists(path)) {
File.Delete(path);
int counter = 0;
while (File.Exists(path)) {
Thread.Sleep(50);
counter += 50;
if (counter >= timeout)
break;
}
}
}
private static void DeleteFolderInner(string path) {
foreach (string directory in Directory.GetDirectories(path))
DeleteFolderInner(directory);
try {
Directory.Delete(path, true);
} catch (DirectoryNotFoundException) {
} catch (IOException) { //quantum weirdness
Thread.Sleep(50);
Directory.Delete(path, true);
Thread.Sleep(50);
} catch (UnauthorizedAccessException) { //quantum weirdness
Thread.Sleep(50);
Directory.Delete(path, true);
Thread.Sleep(50);
}
}
/// <summary>
/// Deletes the specified folder if it exists
/// </summary>
/// <param name="path">The path to the folder to delete</param>
/// <param name="timeout">The approximate timeout in milliseconds</param>
public static void DeleteFolderIfExists(string path, int timeout = int.MaxValue) {
if (Directory.Exists(path)) {
DeleteFolderInner(path);
int counter = 0;
while (Directory.Exists(path)) {
Thread.Sleep(50);
counter += 50;
if (counter >= timeout)
break;
}
}
}
/// <summary>
/// Cleans up all temporary files and directories
/// </summary>
/// <param name="path">The path where the installation is located</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public static void DeleteTemp(string path, List<string> warnings) {
try {
DeleteFileIfExists(path + Path.DirectorySeparatorChar + "binaries.zip");
} catch (Exception e) {
warnings.Add("Could not delete binaries.zip: " + ExceptionToString(e));
}
try {
DeleteFileIfExists(path + Path.DirectorySeparatorChar + "data.zip");
} catch (Exception e) {
warnings.Add("Could not delete data.zip: " + ExceptionToString(e));
}
try {
DeleteFolderIfExists(path + Path.DirectorySeparatorChar + Config.BinariesDir);
} catch (Exception e) {
warnings.Add("Could not delete " + Config.BinariesDir + ": " + ExceptionToString(e));
}
try {
DeleteFolderIfExists(path + Path.DirectorySeparatorChar + Config.DataDir);
} catch (Exception e) {
warnings.Add("Could not delete " + Config.DataDir + ": " + ExceptionToString(e));
}
}
/// <summary>
/// Deletes the shortcuts
/// </summary>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public static void DeleteShortcuts(List<string> warnings) {
try {
DeleteFolderIfExists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "Glest"));
} catch (Exception e) {
warnings.Add("Could not delete start menu shortcut: " + ExceptionToString(e));
}
try {
DeleteFileIfExists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar + "Glest.lnk");
} catch (Exception e) {
warnings.Add("Could not delete desktop shortcut: " + ExceptionToString(e));
}
}
private static string GetGuidString() {
return new Guid(((GuidAttribute) typeof(Installer).Assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value).ToString("B");
}
/// <summary>
/// Deletes the egistry keys
/// </summary>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public static void DeleteRegistryKeys(List<string> warnings) {
try {
using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(UninstallKeyPath, true)) {
if (parent != null)
parent.DeleteSubKeyTree(GetGuidString(), false);
}
} catch (Exception e) {
warnings.Add("Could not delete registry key: " + ExceptionToString(e));
}
}
/// <summary>
/// Removes all installation files and traces
/// </summary>
/// <param name="path">The path where the installation is located</param>
/// <param name="warnings">The warnings list to add to if an error happens</param>
public static void Uninstall(string path, List<string> warnings) {
DeleteFolderIfExists(path);
DeleteShortcuts(warnings);
DeleteRegistryKeys(warnings);
}
private void ResetUI() {
try {
if (IsHandleCreated && Visible) {
//Invoke(new Action(() => {
installButton.Text = "Agree && Install";
installButton.Enabled = true;
pathButton.Enabled = true;
progressBar.Value = 0;
start = 0;
//}));
}
} catch {
Environment.Exit(0);
}
}
private string StartProgressUI(string message) {
/*return (string) Invoke(new Func<object>(() => {*/
progressBar.Value = 0;
start = 0;
installButton.Enabled = false;
pathButton.Enabled = false;
installButton.Text = message;
return pathTextBox.Text;
/*}));*/
}
/// <summary>
/// Removes all installation files and traces
/// </summary>
/// <param name="silent">Whether to present any messages to the user</param>
public void StartUninstall(bool silent) {
lock (this) {
busy = true;
string path = null;
try {
if (silent)
path = /*(string) Invoke(new Func<object>(() => */pathTextBox.Text/*))*/;
else
path = StartProgressUI("Uninstalling...");
List <string> warnings = new List<string>();
Uninstall(path, warnings);
busy = false;
if (!silent)
ShowSuccess(true, warnings);
} catch (Exception ex) {
if (!silent && IsHandleCreated && Visible && ShowMessageBox("An error occurred: " + ExceptionToString(ex) + ". Try running as administrator if the issue persists. Do you want to retry?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
StartUninstall(false);
} finally {
busy = false;
if (!silent)
ResetUI();
}
}
}
/// <summary>
/// Returns the path where it is installed if it is found
/// </summary>
/// <param name="version">The currently installed version</param>
public static string CheckIsInstalled(out string version) {
version = null;
try {
using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(UninstallKeyPath, true)) {
if (parent == null)
throw new Exception("Uninstall registry key not found.");
RegistryKey key = null;
try {
string guidString = GetGuidString();
key = parent.OpenSubKey(guidString, true);
if (key == null)
return null;
version = key.GetValue("ApplicationVersion").ToString();
return Path.GetDirectoryName(key.GetValue("UninstallString").ToString().Trim('"'));
} finally {
if (key != null)
key.Close();
}
}
} catch {
return null;
}
}
/// <summary>
/// Starts the installation
/// </summary>
public void StartInstall() {
lock (this) {
busy = true;
string path = null;
try {
path = StartProgressUI("Installing...\n(takes some time)");
StartUninstall(true);
busy = true;
List<string> warnings = new List<string>();
Install(path, warnings);
busy = false;
ShowSuccess(false, warnings);
if (warnings.Count == 0) {
//try {
/*Invoke(new Action(Close));*/
Close();
//} catch {
//}
}
} catch (Exception ex) {
if (IsHandleCreated && Visible) {
switch (ShowMessageBox("An error occurred: " + ExceptionToString(ex) + ". Try running as administrator or changing the install path if the issue persists. Do you want to retry, or clean up?", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)) {
case DialogResult.Retry:
StartInstall();
break;
case DialogResult.Abort:
StartUninstall(false);
break;
default:
return;
}
}
} finally {
busy = false;
ResetUI();
}
}
}
/// <summary>
/// Called when the window is being closed
/// </summary>
/// <param name="e">Whether to cancel the window close event</param>
protected override void OnClosing(CancelEventArgs e) {
if (busy) {
if (MessageBox.Show("The current process is not finished yet. Are you sure you want to exit?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
closing = true;
if (sevenZip != null) {
try {
Process sevenZ = sevenZip;
sevenZip = null;
if (sevenZ != null)
sevenZ.Kill();
sevenZ.WaitForExit(2500);
Thread.Sleep(100);
} catch {
}
}
WebClient client = dataClient;
if (client != null) {
try {
client.CancelAsync();
dataResetEvent.Wait();
Thread.Sleep(1000);
} catch {
}
}
client = binariesClient;
if (client != null) {
try {
client.CancelAsync();
Thread.Sleep(1000);
} catch {
}
}
try {
Uninstall(pathTextBox.Text, new List<string>());
} catch {
}
} else
e.Cancel = true;
}
base.OnClosing(e);
}
/// <summary>
/// Disposes of the resources used by the form
/// </summary>
/// <param name="disposing">Whether to dispose managed resources</param>
protected override void Dispose(bool disposing) {
Environment.Exit(0); //to prevent hanging bug
//base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Installer));
this.licenseTextBox = new System.Windows.Forms.RichTextBox();
this.installButton = new System.Windows.Forms.Button();
this.pathTextBox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.pathButton = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.desktopShortcutCheckBox = new System.Windows.Forms.CheckBox();
this.startMenuShortcutCheckBox = new System.Windows.Forms.CheckBox();
this.networkFixCheckBox = new System.Windows.Forms.CheckBox();
this.progressBar = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// licenseTextBox
//
this.licenseTextBox.BackColor = System.Drawing.Color.White;
this.licenseTextBox.ForeColor = System.Drawing.Color.Black;
this.licenseTextBox.Location = new System.Drawing.Point(12, 253);
this.licenseTextBox.Margin = new System.Windows.Forms.Padding(8);
this.licenseTextBox.Name = "licenseTextBox";
this.licenseTextBox.ReadOnly = true;
this.licenseTextBox.Size = new System.Drawing.Size(431, 200);
this.licenseTextBox.TabIndex = 0;
this.licenseTextBox.Text = resources.GetString("licenseTextBox.Text");
//
// installButton
//
this.installButton.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.installButton.ForeColor = System.Drawing.Color.Black;
this.installButton.Location = new System.Drawing.Point(283, 12);
this.installButton.Name = "installButton";
this.installButton.Size = new System.Drawing.Size(160, 121);
this.installButton.TabIndex = 3;
this.installButton.Text = "Agree && Install";
this.installButton.UseVisualStyleBackColor = true;
this.installButton.Click += new System.EventHandler(this.installButton_Click);
//
// pathTextBox
//
this.pathTextBox.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.pathTextBox.Location = new System.Drawing.Point(66, 150);
this.pathTextBox.Multiline = true;
this.pathTextBox.Name = "pathTextBox";
this.pathTextBox.ReadOnly = true;
this.pathTextBox.Size = new System.Drawing.Size(319, 23);
this.pathTextBox.TabIndex = 4;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(10, 150);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(50, 23);
this.label2.TabIndex = 5;
this.label2.Text = "Path:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pathButton
//
this.pathButton.Font = new System.Drawing.Font("Calibri", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.pathButton.ForeColor = System.Drawing.Color.Black;
this.pathButton.Location = new System.Drawing.Point(396, 150);
this.pathButton.Name = "pathButton";
this.pathButton.Size = new System.Drawing.Size(45, 23);
this.pathButton.TabIndex = 6;
this.pathButton.Text = "···";
this.pathButton.UseVisualStyleBackColor = true;
this.pathButton.Click += new System.EventHandler(this.pathButton_Click);
//
// pictureBox1
//
this.pictureBox1.BackgroundImage = global::GlestInstaller.Properties.Resources.banner;
this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.pictureBox1.Location = new System.Drawing.Point(3, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(264, 121);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// desktopShortcutCheckBox
//
this.desktopShortcutCheckBox.Checked = true;
this.desktopShortcutCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.desktopShortcutCheckBox.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.desktopShortcutCheckBox.Location = new System.Drawing.Point(15, 188);
this.desktopShortcutCheckBox.Name = "desktopShortcutCheckBox";
this.desktopShortcutCheckBox.Size = new System.Drawing.Size(128, 24);
this.desktopShortcutCheckBox.TabIndex = 7;
this.desktopShortcutCheckBox.Text = "Desktop Shortcut";
this.desktopShortcutCheckBox.UseVisualStyleBackColor = true;
//
// startMenuShortcutCheckBox
//
this.startMenuShortcutCheckBox.Checked = true;
this.startMenuShortcutCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.startMenuShortcutCheckBox.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.startMenuShortcutCheckBox.Location = new System.Drawing.Point(149, 191);
this.startMenuShortcutCheckBox.Name = "startMenuShortcutCheckBox";
this.startMenuShortcutCheckBox.Size = new System.Drawing.Size(139, 19);
this.startMenuShortcutCheckBox.TabIndex = 8;
this.startMenuShortcutCheckBox.Text = "Start Menu Shortcut";
this.startMenuShortcutCheckBox.UseVisualStyleBackColor = true;
//
// networkFixCheckBox
//
this.networkFixCheckBox.Checked = true;
this.networkFixCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.networkFixCheckBox.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.networkFixCheckBox.Location = new System.Drawing.Point(294, 191);
this.networkFixCheckBox.Name = "networkFixCheckBox";
this.networkFixCheckBox.Size = new System.Drawing.Size(140, 19);
this.networkFixCheckBox.TabIndex = 9;
this.networkFixCheckBox.Text = "Network Throttle Fix";
this.networkFixCheckBox.UseVisualStyleBackColor = true;
//
// progressBar
//
this.progressBar.Location = new System.Drawing.Point(12, 219);
this.progressBar.Maximum = 1000;
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(431, 23);
this.progressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
this.progressBar.TabIndex = 10;
//
// Installer
//
this.AcceptButton = this.installButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.MidnightBlue;
this.ClientSize = new System.Drawing.Size(453, 463);