Program.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. Console.WriteLine("Hello, World!");
  10. var logger = new LoggerConfiguration()
  11. .MinimumLevel.Debug()
  12. .WriteTo.File("update.log", LogEventLevel.Debug)// restricted... is Optional
  13. .CreateLogger();
  14. try
  15. {
  16. FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo("VeloeMinecraftLauncher.exe");
  17. LatestLauncherVersion latestLauncherInfo;
  18. using (var webClient = new HttpClient())
  19. {
  20. var jsonData = string.Empty;
  21. try
  22. {
  23. jsonData = webClient.GetStringAsync("https://files.veloe.link/launcher/update/versions.json").Result;
  24. }
  25. catch (Exception)
  26. {
  27. logger.Error("Error occured on getting versions.json from the server.");
  28. throw;
  29. }
  30. latestLauncherInfo = string.IsNullOrEmpty(jsonData)
  31. ? new LatestLauncherVersion()
  32. : JsonSerializer.Deserialize<LatestLauncherVersion>(jsonData);
  33. }
  34. if (latestLauncherInfo is not null)
  35. {
  36. string[] version = latestLauncherInfo.latest.Split('.');
  37. logger.Information("Latest version on server: {0}", latestLauncherInfo.latest);
  38. string[] versionOnServer = fileVersionInfo.FileVersion.Split('.');
  39. logger.Information("Launcher version: {0}", fileVersionInfo.FileVersion);
  40. if (Int16.Parse(version[0]) > Int16.Parse(versionOnServer[0]) ||
  41. Int16.Parse(version[1]) > Int16.Parse(versionOnServer[1]) ||
  42. Int16.Parse(version[2]) > Int16.Parse(versionOnServer[2]) ||
  43. Int16.Parse(version[3]) > Int16.Parse(versionOnServer[3]))
  44. {
  45. WebClient webClient = new WebClient();
  46. try
  47. {
  48. logger.Information("Downloading latest.zip");
  49. webClient.DownloadFile(new System.Uri("https://files.veloe.link/launcher/update/latest.zip"), "latest.zip");
  50. }
  51. catch (Exception ex)
  52. {
  53. logger.Error("Error occured on getting latest.zip from the server.");
  54. throw;
  55. }
  56. logger.Information("Unpacking latest.zip");
  57. ZipFile.ExtractToDirectory($"latest.zip", Directory.GetCurrentDirectory(), true);
  58. logger.Information("Deleting latest.zip");
  59. File.Delete($"latest.zip");
  60. }
  61. else
  62. {
  63. logger.Information("No update required.");
  64. }
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. logger.Error(ex.Message);
  70. logger.Error("Path exe: {0}", Directory.GetCurrentDirectory());
  71. logger.Error(ex.StackTrace);
  72. }