SPT-AKI-Installer/SPTInstaller/Models/InstallerUpdateInfo.cs

190 lines
5.1 KiB
C#
Raw Normal View History

2024-04-27 14:51:54 -04:00
using ReactiveUI;
using Serilog;
2023-07-30 16:15:52 -04:00
using SPTInstaller.Helpers;
using System.Diagnostics;
using System.Threading.Tasks;
2024-05-01 10:32:22 -04:00
using Newtonsoft.Json;
namespace SPTInstaller.Models;
2024-05-01 10:32:22 -04:00
public class InstallerUpdateInfo : ReactiveObject
{
2024-06-29 11:34:04 -04:00
private Version? _newVersion;
2024-05-01 10:32:22 -04:00
2024-06-29 11:34:04 -04:00
public Version? NewVersion
{
get => _newVersion;
set => this.RaiseAndSetIfChanged(ref _newVersion, value);
}
private string _changeLog;
public string ChangeLog
{
get => _changeLog;
set => this.RaiseAndSetIfChanged(ref _changeLog, value);
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
private string _updateInfoText = "";
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
public string UpdateInfoText
{
get => _updateInfoText;
set => this.RaiseAndSetIfChanged(ref _updateInfoText, value);
}
2024-05-01 10:32:22 -04:00
2023-08-03 18:02:38 -04:00
private bool _updating = false;
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
public bool Updating
{
2023-07-30 16:15:52 -04:00
get => _updating;
set => this.RaiseAndSetIfChanged(ref _updating, value);
}
2024-05-01 10:32:22 -04:00
2023-08-03 18:02:38 -04:00
private bool _updateAvailable = false;
2024-05-01 10:32:22 -04:00
2023-08-03 18:02:38 -04:00
public bool UpdateAvailable
{
get => _updateAvailable;
set => this.RaiseAndSetIfChanged(ref _updateAvailable, value);
}
2024-05-01 10:32:22 -04:00
2023-08-03 18:02:38 -04:00
private bool _checkingForUpdates = false;
2024-05-01 10:32:22 -04:00
2023-08-03 18:02:38 -04:00
public bool CheckingForUpdates
{
get => _checkingForUpdates;
set => this.RaiseAndSetIfChanged(ref _checkingForUpdates, value);
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
private int _downloadProgress;
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
public int DownloadProgress
{
2023-07-30 16:15:52 -04:00
get => _downloadProgress;
set => this.RaiseAndSetIfChanged(ref _downloadProgress, value);
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
public async Task UpdateInstaller()
{
Updating = true;
2023-08-03 18:02:38 -04:00
UpdateAvailable = false;
2023-07-30 16:15:52 -04:00
var updater = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "update.ps1"));
2024-05-01 10:32:22 -04:00
if (!FileHelper.StreamAssemblyResourceOut("update.ps1", updater.FullName))
{
Log.Fatal("Failed to prepare update file");
return;
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
if (!updater.Exists)
{
UpdateInfoText = "Failed to get updater from resources :(";
return;
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
var newInstallerPath = await DownloadNewInstaller();
2024-05-01 10:32:22 -04:00
if (string.IsNullOrWhiteSpace(newInstallerPath))
2023-07-30 16:15:52 -04:00
return;
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
Process.Start(new ProcessStartInfo
{
FileName = "powershell.exe",
2024-05-01 10:32:22 -04:00
ArgumentList =
{
"-ExecutionPolicy", "Bypass", "-File", $"{updater.FullName}", $"{newInstallerPath}",
$"{Path.Join(Environment.CurrentDirectory, "SPTInstaller.exe")}"
}
2023-07-30 16:15:52 -04:00
});
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
private async Task<string> DownloadNewInstaller()
{
2024-05-01 12:00:30 -04:00
UpdateInfoText = $"Downloading installer v{NewVersion}";
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
var progress = new Progress<double>(x => DownloadProgress = (int)x);
2024-05-01 10:32:22 -04:00
var file = await DownloadCacheHelper.DownloadFileAsync("SPTInstaller.exe", DownloadCacheHelper.InstallerUrl,
progress);
2023-07-30 16:15:52 -04:00
if (file == null || !file.Exists)
{
UpdateInfoText = "Failed to download new installer :(";
return "";
}
2024-05-01 10:32:22 -04:00
2023-07-30 16:15:52 -04:00
return file.FullName;
}
2024-05-01 10:32:22 -04:00
2023-11-12 09:52:02 -05:00
private void EndCheck(string infoText, bool updateAvailable, bool log = true)
2023-08-03 18:02:38 -04:00
{
2023-11-12 09:52:02 -05:00
if (log)
{
Log.Information(infoText);
}
2023-08-03 18:02:38 -04:00
UpdateInfoText = infoText;
CheckingForUpdates = false;
UpdateAvailable = updateAvailable;
}
2024-05-01 10:32:22 -04:00
public async Task CheckForUpdates(Version? currentVersion)
{
if (currentVersion == null)
return;
UpdateInfoText = "Checking for installer updates";
CheckingForUpdates = true;
try
{
var installerInfoFile =
2024-05-04 16:18:49 -04:00
await DownloadCacheHelper.GetOrDownloadFileAsync("installer.json", DownloadCacheHelper.InstallerInfoUrl, null
, DownloadCacheHelper.SuggestedTtl);
2024-05-01 10:32:22 -04:00
if (installerInfoFile == null)
{
EndCheck("Failed to download installer info", false);
return;
}
var installerInfo =
JsonConvert.DeserializeObject<InstallerInfo>(File.ReadAllText(installerInfoFile.FullName));
if (installerInfo == null)
{
EndCheck("Failed to parse installer info json", false);
return;
}
var latestVersion = new Version(installerInfo.LatestVersion);
if (latestVersion <= currentVersion)
{
EndCheck("No updates available", false);
return;
}
2024-05-01 12:00:30 -04:00
NewVersion = latestVersion;
2024-05-01 10:32:22 -04:00
foreach (var change in installerInfo.Changes)
{
ChangeLog += $"◉ {change}\n";
}
EndCheck($"Update Installer: v{latestVersion}", true);
2024-05-01 10:32:22 -04:00
return;
}
catch (Exception ex)
{
EndCheck(ex.Message, false, false);
Log.Error(ex, "Failed to check for updates");
}
return;
}
}