MainWindow.xaml.cs 4.1 KB

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