|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Rapr |
| 8 | +{ |
| 9 | + public class WinGetUpdateManager : IUpdateManager, IDisposable |
| 10 | + { |
| 11 | + private const string WinGetPackageId = "lostindark.DriverStoreExplorer"; |
| 12 | + private readonly UpdateManager updateManager = new UpdateManager(); |
| 13 | + |
| 14 | + public bool HandlesRestart => true; |
| 15 | + |
| 16 | + public async Task<VersionInfo> GetLatestVersionInfo() |
| 17 | + { |
| 18 | + // Use winget search to get the latest available version — version numbers |
| 19 | + // in the output are always locale-independent (digits and dots). |
| 20 | + string output = await RunCommandAsync( |
| 21 | + "winget", |
| 22 | + $"search --id {WinGetPackageId} --exact --source winget --disable-interactivity --accept-source-agreements") |
| 23 | + .ConfigureAwait(false); |
| 24 | + |
| 25 | + if (!string.IsNullOrEmpty(output)) |
| 26 | + { |
| 27 | + var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); |
| 28 | + bool pastSeparator = false; |
| 29 | + |
| 30 | + foreach (var line in lines) |
| 31 | + { |
| 32 | + var trimmed = line.Trim(); |
| 33 | + |
| 34 | + if (trimmed.StartsWith("-", StringComparison.Ordinal)) |
| 35 | + { |
| 36 | + pastSeparator = true; |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if (pastSeparator) |
| 41 | + { |
| 42 | + // Extract the last whitespace-delimited token — that's the version |
| 43 | + var parts = trimmed.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 44 | + if (parts.Length > 0 && Version.TryParse(parts[parts.Length - 1], out var version)) |
| 45 | + { |
| 46 | + return new VersionInfo |
| 47 | + { |
| 48 | + Version = version, |
| 49 | + PageUrl = new Uri($"https://github.com/lostindark/DriverStoreExplorer/releases/tag/v{parts[parts.Length - 1]}"), |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Fall back to GitHub API if winget command fails |
| 57 | + return await this.updateManager.GetLatestVersionInfo().ConfigureAwait(false); |
| 58 | + } |
| 59 | + |
| 60 | + public Task ApplyUpdateAsync(VersionInfo versionInfo, IProgress<float> progress) |
| 61 | + { |
| 62 | + int pid = Process.GetCurrentProcess().Id; |
| 63 | + string exePath = Assembly.GetExecutingAssembly().Location; |
| 64 | + |
| 65 | + string scriptDir = Path.Combine(Path.GetTempPath(), "DriverStoreExplorer"); |
| 66 | + if (!Directory.Exists(scriptDir)) |
| 67 | + { |
| 68 | + Directory.CreateDirectory(scriptDir); |
| 69 | + } |
| 70 | + |
| 71 | + string scriptPath = Path.Combine(scriptDir, "winget_update.cmd"); |
| 72 | + string script = $@"@echo off |
| 73 | +:wait |
| 74 | +tasklist /FI ""PID eq {pid}"" 2>NUL | find /I ""{pid}"" >NUL |
| 75 | +if %errorlevel%==0 (timeout /t 1 /nobreak >nul & goto wait) |
| 76 | +winget upgrade --id {WinGetPackageId} --silent --disable-interactivity --accept-source-agreements --accept-package-agreements |
| 77 | +start """" ""{exePath}"" |
| 78 | +del ""%~f0"" |
| 79 | +"; |
| 80 | + |
| 81 | + File.WriteAllText(scriptPath, script); |
| 82 | + |
| 83 | + Process.Start(new ProcessStartInfo |
| 84 | + { |
| 85 | + FileName = "cmd.exe", |
| 86 | + Arguments = $"/c \"{scriptPath}\"", |
| 87 | + CreateNoWindow = true, |
| 88 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 89 | + UseShellExecute = false |
| 90 | + }); |
| 91 | + |
| 92 | + return Task.CompletedTask; |
| 93 | + } |
| 94 | + |
| 95 | + public void Dispose() |
| 96 | + { |
| 97 | + this.updateManager.Dispose(); |
| 98 | + } |
| 99 | + |
| 100 | + private static Task<string> RunCommandAsync(string fileName, string arguments) |
| 101 | + { |
| 102 | + return Task.Run(() => |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + var psi = new ProcessStartInfo |
| 107 | + { |
| 108 | + FileName = fileName, |
| 109 | + Arguments = arguments, |
| 110 | + UseShellExecute = false, |
| 111 | + RedirectStandardOutput = true, |
| 112 | + RedirectStandardError = true, |
| 113 | + CreateNoWindow = true |
| 114 | + }; |
| 115 | + |
| 116 | + using (var process = Process.Start(psi)) |
| 117 | + { |
| 118 | + string output = process.StandardOutput.ReadToEnd(); |
| 119 | + process.WaitForExit(); |
| 120 | + return output; |
| 121 | + } |
| 122 | + } |
| 123 | + catch |
| 124 | + { |
| 125 | + return null; |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments