Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Important improvements to the updater #2982

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/UniGetUI.Core.Data/CoreData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public static string IgnoredUpdatesDatabaseFile
/// The ID of the notification that is used to inform the user that updates are available
/// </summary>
public const int UpdatesAvailableNotificationTag = 1234;
public const int UniGetUICanBeUpdated = 1235;


/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/UniGetUI.Interface.Enums/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ public class NotificationArguments
public const string Show = "openUniGetUI";
public const string ShowOnUpdatesTab = "openUniGetUIOnUpdatesTab";
public const string UpdateAllPackages = "updateAll";
public const string ReleaseSelfUpdateLock = "releaseSelfUpdateLock";
}
}
141 changes: 0 additions & 141 deletions src/UniGetUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,147 +404,6 @@ public async void DisposeAndQuit(int outputCode = 0)
Environment.Exit(outputCode);
}

private async void UpdateUniGetUIIfPossible(int round = 0)
{
InfoBar? banner = null;
try
{
Logger.Debug("Starting update check");

string fileContents;

using (HttpClient client = new(CoreData.GenericHttpClientParameters))
{
client.Timeout = TimeSpan.FromSeconds(600);
client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString);
fileContents = await client.GetStringAsync("https://www.marticliment.com/versions/unigetui.ver");
}

if (!fileContents.Contains("///"))
{
throw new FormatException("The updates file does not follow the FloatVersion///Sha256Hash format");
}

float LatestVersion = float.Parse(fileContents.Split("///")[0].Replace("\n", "").Trim(), CultureInfo.InvariantCulture);
string InstallerHash = fileContents.Split("///")[1].Replace("\n", "").Trim().ToLower();

if (LatestVersion > CoreData.VersionNumber)
{
Logger.Info("Updates found, downloading installer...");
Logger.Info("Current version: " + CoreData.VersionNumber.ToString(CultureInfo.InvariantCulture));
Logger.Info("Latest version : " + LatestVersion.ToString(CultureInfo.InvariantCulture));

banner = MainWindow.UpdatesBanner;
banner.Title = CoreTools.Translate("WingetUI version {0} is being downloaded.", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Message = CoreTools.Translate("This may take a minute or two");
banner.Severity = InfoBarSeverity.Informational;
banner.IsOpen = true;
banner.IsClosable = false;

Uri DownloadUrl = new("https://github.com/marticliment/WingetUI/releases/latest/download/UniGetUI.Installer.exe");
string InstallerPath = Path.Join(Directory.CreateTempSubdirectory().FullName, "unigetui-updater.exe");

using (HttpClient client = new(CoreData.GenericHttpClientParameters))
{
client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString);
HttpResponseMessage result = await client.GetAsync(DownloadUrl);
using FileStream fs = new(InstallerPath, FileMode.CreateNew);
await result.Content.CopyToAsync(fs);
}

string Hash = "";
SHA256 Sha256 = SHA256.Create();
using (FileStream stream = File.OpenRead(InstallerPath))
{
Hash = Convert.ToHexString(Sha256.ComputeHash(stream)).ToLower();
}

if (Hash == InstallerHash)
{

banner.Title = CoreTools.Translate("WingetUI {0} is ready to be installed.", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Message = CoreTools.Translate("The update will be installed upon closing WingetUI");
banner.ActionButton = new Button
{
Content = CoreTools.Translate("Update now")
};
banner.ActionButton.Click += (_, _) => { MainWindow.HideWindow(); };
banner.Severity = InfoBarSeverity.Success;
banner.IsOpen = true;
banner.IsClosable = true;

if (MainWindow.Visible)
{
Logger.Debug("Waiting for mainWindow to be hidden");
}

while (MainWindow.Visible)
{
await Task.Delay(100);
}

if (Settings.Get("DisableAutoUpdateWingetUI"))
{
Logger.Warn("User disabled updates!");
return;
}

Logger.ImportantInfo("The hash matches the expected value, starting update process...");
Process p = new();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = $"/c start /B \"\" \"{InstallerPath}\" /silent";
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
DisposeAndQuit();
}
else
{
Logger.Error("Hash mismatch, not updating!");
Logger.Error("Current hash : " + Hash);
Logger.Error("Expected hash: " + InstallerHash);
File.Delete(InstallerPath);

banner.Title = CoreTools.Translate("The installer hash does not match the expected value.");
banner.Message = CoreTools.Translate("The update will not continue.");
banner.Severity = InfoBarSeverity.Error;
banner.IsOpen = true;
banner.IsClosable = true;

await Task.Delay(3600000); // Check again in 1 hour
UpdateUniGetUIIfPossible();
}
}
else
{
Logger.Info("UniGetUI is up to date");
await Task.Delay(3600000); // Check again in 1 hour
UpdateUniGetUIIfPossible();
}
}
catch (Exception e)
{
if (banner is not null)
{
banner.Title = CoreTools.Translate("An error occurred when checking for updates: ");
banner.Message = e.Message;
banner.Severity = InfoBarSeverity.Error;
banner.IsOpen = true;
banner.IsClosable = true;
}

Logger.Error(e);

if (round >= 3)
{
return;
}

await Task.Delay(600000); // Try again in 10 minutes
UpdateUniGetUIIfPossible(round + 1);
}
}

public void KillAndRestart()
{
Process.Start(CoreData.UniGetUIExecutableFile);
Expand Down
Loading
Loading