Program.cs 3.3 KB

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