MainWindow.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using LauncherUpdater.Function;
  2. using LauncherUpdater.HTTP;
  3. using LauncherUpdater.Response;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.IO.Hashing;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Markup;
  14. namespace LauncherUpdater
  15. {
  16. public partial class MainWindow : Window
  17. {
  18. private readonly HttpRequest _httpRequest;
  19. private readonly FileConfig _fileConfig;
  20. public MainWindow()
  21. {
  22. _httpRequest = new HttpRequest();
  23. _fileConfig = new FileConfig();
  24. InitializeComponent();
  25. var task = CheckLauncherFiles();
  26. task.Wait();
  27. }
  28. private async Task CheckLauncherFiles()
  29. {
  30. try
  31. {
  32. foreach (var process in Process.GetProcessesByName("LauncherIcarus"))
  33. {
  34. process.Kill();
  35. }
  36. if (!Directory.Exists(Configuration.Default.LauncherDir))
  37. {
  38. Directory.CreateDirectory(Configuration.Default.LauncherDir);
  39. }
  40. ProgressBar.Width = 0.0;
  41. var launcherFiles = JsonConvert.DeserializeObject<ResponseListLauncherFiles>(
  42. await _httpRequest.GetLauncherFileAsync()
  43. );
  44. if (launcherFiles?.TotalBytes > 0.0)
  45. {
  46. foreach (var file in launcherFiles.Files)
  47. {
  48. var hash = string.Empty;
  49. if (File.Exists(Configuration.Default.LauncherDir + file.Filename))
  50. {
  51. using (var fileStream =
  52. File.Open(Configuration.Default.LauncherDir + file.Filename, FileMode.Open)
  53. )
  54. {
  55. using (var memoryStream = new MemoryStream())
  56. {
  57. await fileStream.CopyToAsync(memoryStream);
  58. hash = Crc32.Hash(memoryStream.ToArray())
  59. .Aggregate(hash, (current, next) => current + next.ToString("x2"))
  60. .ToLower();
  61. }
  62. }
  63. }
  64. if (file.Crc32 != hash)
  65. {
  66. await DownloadAsync(file.Filename, Configuration.Default.LauncherDir, file.Url);
  67. }
  68. var num3 = file.Size / launcherFiles.TotalBytes * 370.0;
  69. ProgressBar.Width += Math.Round(num3, 0);
  70. }
  71. }
  72. _fileConfig.SetLauncherIsChecked(EncryptionHelper.Encrypt("TRUE"));
  73. Process.Start(new ProcessStartInfo
  74. {
  75. WorkingDirectory = Configuration.Default.LauncherDir,
  76. FileName = "LauncherIcarus.exe",
  77. CreateNoWindow = true,
  78. });
  79. Environment.Exit(0);
  80. }
  81. catch (Exception ex)
  82. {
  83. Logger.WriteLog(Application.Current + " : " + ex.Message);
  84. Environment.Exit(0);
  85. }
  86. }
  87. private static async Task DownloadAsync(string filename, string downloadPath, string url)
  88. {
  89. var changedEventHandler = (DownloadProgressChangedEventHandler)((s, e) => { });
  90. var completedEventHandler = (DownloadDataCompletedEventHandler)((s, e) => { });
  91. using (var webClient = new WebClient())
  92. {
  93. webClient.DownloadProgressChanged += changedEventHandler;
  94. webClient.DownloadDataCompleted += completedEventHandler;
  95. await webClient.DownloadFileTaskAsync(
  96. new Uri(Configuration.Default.ServerIp + url), downloadPath + filename
  97. );
  98. }
  99. }
  100. }
  101. }