Program.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. logger.Information("Latest launcher version on server: {0}", latestLauncherVersion.latest);
  46. logger.Information("Launcher version: {0}", fileVersionInfo.FileVersion);
  47. if (!(new Version(latestLauncherVersion.latest) > new Version(fileVersionInfo.FileVersion)))
  48. {
  49. logger.Information("No update required.");
  50. return;
  51. }
  52. }
  53. }
  54. WebClient webClient = new WebClient();
  55. try
  56. {
  57. logger.Information("Downloading latest.zip");
  58. string url = String.Empty;
  59. if (OperatingSystem.IsWindows())
  60. url = "https://files.veloe.link/launcher/update/latest.zip";
  61. if (OperatingSystem.IsLinux())
  62. url = "https://files.veloe.link/launcher/update/latest_linux-x64.zip";
  63. if (url == String.Empty)
  64. return;
  65. webClient.DownloadFile(new System.Uri(url), "latest.zip"); ;
  66. }
  67. catch (Exception ex)
  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. logger.Error(ex.StackTrace);
  82. }