|
| 1 | +using System.IO.Compression; |
| 2 | +using Avalonia.Media; |
| 3 | +using Avalonia.Media.Immutable; |
| 4 | +using NetSparkleUpdater; |
| 5 | +using NetSparkleUpdater.Enums; |
| 6 | +using NetSparkleUpdater.SignatureVerifiers; |
| 7 | +using NetSparkleUpdater.UI.Avalonia; |
| 8 | +using Serilog; |
| 9 | +using Utils; |
| 10 | + |
| 11 | +namespace ShinRyuModManager.UserInterface.Updater; |
| 12 | + |
| 13 | +public static class AutoUpdating { |
| 14 | + private const string GH_PAGES_ROOT = "https://thetruecolonel.github.io/SRMM-AppCast"; |
| 15 | + |
| 16 | + private static string _tempDir; |
| 17 | + private static SparkleUpdater _updater; |
| 18 | + |
| 19 | + public static void Init() { |
| 20 | + _tempDir = Path.Combine(Environment.CurrentDirectory, "srmm_temp"); |
| 21 | + |
| 22 | + try { |
| 23 | + HandleRyuUpdater(); |
| 24 | + } catch (Exception ex) { |
| 25 | + Log.Error(ex, "Problem trying to download RyuUpdater! Aborting auto updating..."); |
| 26 | + |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + var suffix = AssemblyVersion.GetBuildSuffix(); |
| 31 | + |
| 32 | + if (string.Equals(suffix, "debug", StringComparison.OrdinalIgnoreCase)) { |
| 33 | + return; // Don't need to be annoyed with "Update Now" when debugging |
| 34 | + } |
| 35 | + |
| 36 | + var appcastUrl = $"{GH_PAGES_ROOT}/releases/appcast_{suffix}.xml"; |
| 37 | + |
| 38 | + // Update check for SRMM |
| 39 | + _updater = new PortableUpdater(appcastUrl, new Ed25519Checker(SecurityMode.Unsafe)) { |
| 40 | + UIFactory = new UIFactory { |
| 41 | + HideReleaseNotes = true, |
| 42 | + UseStaticUpdateWindowBackgroundColor = true, |
| 43 | + UpdateWindowGridBackgroundBrush = new ImmutableSolidColorBrush(Color.Parse("#373535")) |
| 44 | + }, |
| 45 | + TmpDownloadFilePath = _tempDir, |
| 46 | + TmpDownloadFileNameWithExtension = $"{Guid.NewGuid()}.zip", |
| 47 | + RelaunchAfterUpdate = false, |
| 48 | + LogWriter = new SerilogWriter() |
| 49 | + }; |
| 50 | + |
| 51 | + _ = _updater.StartLoop(true, TimeSpan.FromMinutes(5)); |
| 52 | + } |
| 53 | + |
| 54 | + private static void HandleRyuUpdater() { |
| 55 | + string ryuUpdaterPath; |
| 56 | + string updaterAppcastUrl; |
| 57 | + string updaterLatestUrl; |
| 58 | + |
| 59 | + if (OperatingSystem.IsWindows()) { |
| 60 | + ryuUpdaterPath = Path.Combine(Environment.CurrentDirectory, "RyuUpdater.exe"); |
| 61 | + updaterAppcastUrl = $"{GH_PAGES_ROOT}/releases/appcast_ryuupdater-windows.xml"; |
| 62 | + updaterLatestUrl = $"{GH_PAGES_ROOT}/updater/RyuUpdater-Windows-Latest.zip"; |
| 63 | + } else { |
| 64 | + ryuUpdaterPath = Path.Combine(Environment.CurrentDirectory, "RyuUpdater"); |
| 65 | + updaterAppcastUrl = $"{GH_PAGES_ROOT}/releases/appcast_ryuupdater-linux.xml"; |
| 66 | + updaterLatestUrl = $"{GH_PAGES_ROOT}/updater/RyuUpdater-Linux-Latest.zip"; |
| 67 | + } |
| 68 | + |
| 69 | + // Pull grab latest version of updater if missing |
| 70 | + if (!File.Exists(ryuUpdaterPath)) { |
| 71 | + using var downloadStream = Utils.Client.GetStreamAsync(updaterLatestUrl).GetAwaiter().GetResult(); |
| 72 | + |
| 73 | + ZipFile.ExtractToDirectory(downloadStream, Environment.CurrentDirectory, overwriteFiles: true); |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: Linux doesn't store the required information for this to work on compiled binaries. To come back to. |
| 77 | + /*// Update RyuUpdater quietly |
| 78 | + var ryuUpdater = new RyuUpdaterUpdater(updaterAppcastUrl, new Ed25519Checker(SecurityMode.Unsafe), ryuUpdaterPath) { |
| 79 | + UserInteractionMode = UserInteractionMode.DownloadAndInstall, |
| 80 | + UIFactory = null, |
| 81 | + TmpDownloadFilePath = _tempDir, |
| 82 | + TmpDownloadFileNameWithExtension = $"{Guid.NewGuid()}.zip" |
| 83 | + }; |
| 84 | + _ = ryuUpdater.StartLoop(true);*/ |
| 85 | + } |
| 86 | +} |
0 commit comments