Program.cs 3.3 KB

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