MainWindow.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using LauncherUpdater.Function;
  2. using LauncherUpdater.HTTP;
  3. using LauncherUpdater.Response;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.CodeDom.Compiler;
  7. using System.ComponentModel;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Net;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Markup;
  15. namespace LauncherUpdater
  16. {
  17. public partial class MainWindow : Window, IComponentConnector
  18. {
  19. private readonly HttpRequest httpRequest;
  20. public DirectoryInfo lDir;
  21. private FileConfig fileConfig;
  22. public MainWindow()
  23. {
  24. this.httpRequest = new HttpRequest();
  25. this.fileConfig = new FileConfig();
  26. this.InitializeComponent();
  27. this.lDir = new DirectoryInfo(Environment.CurrentDirectory);
  28. this.CheckLauncherFiles();
  29. }
  30. public async Task CheckLauncherFiles()
  31. {
  32. try
  33. {
  34. foreach (Process process in Process.GetProcessesByName("LauncherIcarus"))
  35. process.Kill();
  36. if (!Directory.Exists(Configuration.Default.LauncherDir))
  37. Directory.CreateDirectory(Configuration.Default.LauncherDir);
  38. this.progressBar.Width = 0.0;
  39. double totalDownloaded = 0.0;
  40. ResponseListLauncherFiles get =
  41. JsonConvert.DeserializeObject<ResponseListLauncherFiles>(
  42. await this.httpRequest.GetLauncherFileAsync());
  43. if (get.TotalBytes > 0.0)
  44. {
  45. foreach (Files k in get.File)
  46. {
  47. Crc32 crc32 = new Crc32();
  48. string empty = string.Empty;
  49. if (System.IO.File.Exists(Configuration.Default.LauncherDir + k.filename))
  50. {
  51. using (FileStream inputStream =
  52. System.IO.File.Open(Configuration.Default.LauncherDir + k.filename, FileMode.Open))
  53. {
  54. foreach (byte num in crc32.ComputeHash((Stream) inputStream))
  55. empty += num.ToString("x2").ToLower();
  56. }
  57. if (k.crc32 != empty)
  58. {
  59. int num1 = await this.DownloadArchiveAsync(k.filename,
  60. Configuration.Default.LauncherDir, k.url)
  61. ? 1
  62. : 0;
  63. }
  64. }
  65. else
  66. {
  67. int num2 = await this.DownloadArchiveAsync(k.filename, Configuration.Default.LauncherDir,
  68. k.url)
  69. ? 1
  70. : 0;
  71. }
  72. double num3 = k.size / get.TotalBytes * 370.0;
  73. totalDownloaded += k.size;
  74. this.progressBar.Width += Math.Round(num3, 0);
  75. }
  76. }
  77. this.fileConfig.SetLauncherIsChecked(EncryptionHelper.Encrypt("TRUE"));
  78. DirectoryInfo directoryInfo = new DirectoryInfo(Environment.CurrentDirectory);
  79. Process.Start(new ProcessStartInfo()
  80. {
  81. WorkingDirectory = directoryInfo.FullName + "\\Launcher\\",
  82. FileName = "LauncherIcarus.exe",
  83. CreateNoWindow = true
  84. });
  85. Environment.Exit(0);
  86. get = (ResponseListLauncherFiles) null;
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.WriteLog(Application.Current?.ToString() + " : " + ex.Message);
  91. Environment.Exit(0);
  92. }
  93. }
  94. private async Task<bool> DownloadArchiveAsync(
  95. string filename,
  96. string downloadPath,
  97. string url)
  98. {
  99. DownloadProgressChangedEventHandler changedEventHandler =
  100. (DownloadProgressChangedEventHandler) ((s, e) => { });
  101. DownloadDataCompletedEventHandler completedEventHandler =
  102. (DownloadDataCompletedEventHandler) ((s, e) => { });
  103. using (WebClient webClient = new WebClient())
  104. {
  105. webClient.DownloadProgressChanged += changedEventHandler;
  106. webClient.DownloadDataCompleted += completedEventHandler;
  107. await webClient.DownloadFileTaskAsync(new Uri(Configuration.Default.ServerIP + url),
  108. downloadPath + filename);
  109. }
  110. return true;
  111. }
  112. }
  113. }