Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // See https://aka.ms/new-console-template for more information
  2. using Serilog;
  3. using Serilog.Events;
  4. using System.Diagnostics;
  5. using System.IO.Compression;
  6. using System.Reflection;
  7. using System.Text.Json;
  8. using VeloeLauncherUpdater;
  9. if (args.Length > 0 && args[0] == "--version")
  10. {
  11. Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Version);
  12. return;
  13. }
  14. if (args.Length > 0) return;
  15. var logger = new LoggerConfiguration()
  16. .MinimumLevel.Debug()
  17. .WriteTo.File("update.log", LogEventLevel.Debug)// restricted... is Optional
  18. .CreateLogger();
  19. try
  20. {
  21. string fileName = "VeloeMinecraftLauncher";
  22. using var httpClient = new HttpClient();
  23. if (OperatingSystem.IsWindows())
  24. fileName += ".exe";
  25. if (OperatingSystem.IsWindows() && File.Exists(fileName))
  26. {
  27. FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(fileName);
  28. LatestLauncherVersion? latestLauncherVersion;
  29. var jsonData = string.Empty;
  30. try
  31. {
  32. jsonData = httpClient.GetStringAsync("https://files.veloe.link/launcher/update/versions.json").Result;
  33. }
  34. catch (Exception)
  35. {
  36. logger.Error("Error occured on getting versions.json from the server.");
  37. throw;
  38. }
  39. latestLauncherVersion = string.IsNullOrEmpty(jsonData)
  40. ? new LatestLauncherVersion()
  41. : JsonSerializer.Deserialize<LatestLauncherVersion>(jsonData, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
  42. if (latestLauncherVersion?.Latest is not null && fileVersionInfo?.FileVersion is not null)
  43. {
  44. logger.Information("Latest launcher version on server: {0}", latestLauncherVersion.Latest);
  45. logger.Information("Launcher version: {0}", fileVersionInfo.FileVersion);
  46. if (fileVersionInfo?.FileVersion is not null && !(new Version(latestLauncherVersion.Latest) > new Version(fileVersionInfo.FileVersion)))
  47. {
  48. logger.Information("No update required.");
  49. return;
  50. }
  51. }
  52. }
  53. if(OperatingSystem.IsLinux())
  54. {
  55. logger.Warning("Force update mode only.");
  56. }
  57. try
  58. {
  59. logger.Information("Downloading latest.zip");
  60. string url = String.Empty;
  61. url = OperatingSystem.IsWindows() ?
  62. OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14393) ?
  63. "https://files.veloe.link/launcher/update/latest.zip" :
  64. "https://files.veloe.link/launcher/update/latest_net7.0.zip" :
  65. OperatingSystem.IsLinux() ?
  66. "https://files.veloe.link/launcher/update/latest_linux-x64.zip" :
  67. string.Empty;
  68. if (url == String.Empty) return;
  69. using HttpResponseMessage response = await httpClient.GetAsync(new System.Uri(url), HttpCompletionOption.ResponseHeadersRead);
  70. response.EnsureSuccessStatusCode();
  71. using Stream contentStream = await response.Content.ReadAsStreamAsync();
  72. using FileStream fileStream = File.OpenWrite("latest.zip");
  73. await contentStream.CopyToAsync(fileStream);
  74. }
  75. catch (Exception)
  76. {
  77. logger.Error("Error occured on getting latest.zip from the server.");
  78. throw;
  79. }
  80. logger.Information("Unpacking latest.zip");
  81. ZipFile.ExtractToDirectory($"latest.zip", Directory.GetCurrentDirectory(), true);
  82. logger.Information("Deleting latest.zip");
  83. File.Delete($"latest.zip");
  84. }
  85. catch (Exception ex)
  86. {
  87. logger.Error(ex.Message);
  88. logger.Error("Path exe: {0}", Directory.GetCurrentDirectory());
  89. if (ex.StackTrace is not null)
  90. logger.Error(ex.StackTrace);
  91. }