Skip to content

Commit

Permalink
tweak updater so it won't display no updates on canceling.
Browse files Browse the repository at this point in the history
  • Loading branch information
cadaei committed Sep 29, 2019
1 parent 1d3d48f commit d545bec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
8 changes: 6 additions & 2 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,15 +1004,19 @@ private async void CheckForUpdates(bool silentCheck = false)
{
if (Updater.IsProgramInstalled)
{
if (await Updater.CheckForInstallerUpdate(silentCheck, collectionDirty))
bool? installerUpdateRunning = await Updater.CheckForInstallerUpdate(silentCheck, collectionDirty);
if (!installerUpdateRunning.HasValue) return; // error
if (installerUpdateRunning.Value)
{
Close();
return;
}
}
else
{
if (await Updater.CheckForPortableUpdate(silentCheck, collectionDirty))
bool? portableUpdaterRunning = await Updater.CheckForPortableUpdate(silentCheck, collectionDirty);
if (!portableUpdaterRunning.HasValue) return; // error
if (portableUpdaterRunning.Value)
{
Close();
return;
Expand Down
76 changes: 36 additions & 40 deletions ARKBreedingStats/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,74 +70,70 @@ private static bool isInstalled()
assemblyLocation.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
}

public static async Task<bool> CheckForInstallerUpdate(bool silentCheck, bool collectionDirty)
public static async Task<bool?> CheckForInstallerUpdate(bool silentCheck, bool collectionDirty)
{
try
{
(bool wantsProgramUpdate, IList<string> urls) = await checkAndAskForUpdate(collectionDirty);
if (wantsProgramUpdate)
(bool? wantsProgramUpdate, IList<string> urls) = await checkAndAskForUpdate(collectionDirty);
if (wantsProgramUpdate == null) return null;
if (!wantsProgramUpdate.Value) return false;

// Launch the installer and exit the app
string installerUrl = urls.FirstOrDefault(url => url.EndsWith(".exe"));
if (installerUrl != null)
{
// Launch the installer and exit the app
string installerUrl = urls.FirstOrDefault(url => url.EndsWith(".exe"));
if (installerUrl != null)
{
// download installer to temp dir
string installerFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(installerUrl));
await DownloadAsync(installerUrl, installerFile);
// download installer to temp dir
string installerFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(installerUrl));
await DownloadAsync(installerUrl, installerFile);

ProcessStartInfo startInfo = new ProcessStartInfo(installerFile)
{
Verb = "runas"
};
Process.Start(startInfo);
return true;
}
ProcessStartInfo startInfo = new ProcessStartInfo(installerFile)
{
Verb = "runas"
};
Process.Start(startInfo);
return true;
}
}
catch (Exception ex)
{
if (silentCheck)
return false;
if (MessageBox.Show("Error while checking for new version.\n\n" +
if (!silentCheck
&& MessageBox.Show("Error while checking for new version.\n\n" +
$"{ex.Message}\n\n" +
"Try checking for an updated version of ARK Smart Breeding. " +
"Do you want to visit the releases page?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
Process.Start(ReleasesUrl);
return false;
}
return false;
return null;
}

public static async Task<bool> CheckForPortableUpdate(bool silentCheck, bool collectionDirty)
public static async Task<bool?> CheckForPortableUpdate(bool silentCheck, bool collectionDirty)
{
try
{
(bool wantsProgramUpdate, IList<string> urls) = await checkAndAskForUpdate(collectionDirty);
if (wantsProgramUpdate)
(bool? wantsProgramUpdate, IList<string> urls) = await checkAndAskForUpdate(collectionDirty);
if (wantsProgramUpdate == null) return null;
if (!wantsProgramUpdate.Value) return false;

// Launch the installer and exit the app
string zipPackageUrl = urls.FirstOrDefault(url => url.EndsWith(".zip"));
if (zipPackageUrl != null)
{
// Launch the installer and exit the app
string zipPackageUrl = urls.FirstOrDefault(url => url.EndsWith(".zip"));
if (zipPackageUrl != null)
{
await LaunchUpdater();
return true;
}
await LaunchUpdater();
return true;
}
}
catch (Exception ex)
{
if (silentCheck)
return false;
if (MessageBox.Show("Error while checking for new version.\n\n" +
if (!silentCheck
&& MessageBox.Show("Error while checking for new version.\n\n" +
$"{ex.Message}\n\n" +
"Try checking for an updated version of ARK Smart Breeding. " +
"Do you want to visit the releases page?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
Process.Start(ReleasesUrl);
return false;
}
return false;
return null;
}

/// <summary>
Expand All @@ -162,8 +158,8 @@ private static async Task LaunchUpdater()
/// If new release exists ask to install it
/// </summary>
/// <param name="collectionDirty"></param>
/// <returns>true if new release should be installed; download urls or null</returns>
private static async Task<(bool, IList<string> urls)> checkAndAskForUpdate(bool collectionDirty)
/// <returns>true if new release should be installed, null if it was canceled; download urls or null</returns>
private static async Task<(bool?, IList<string> urls)> checkAndAskForUpdate(bool collectionDirty)
{
(string releaseTag, IList<string> urls) = await FetchReleaseFeed();
(bool? updateAvailable, string localVersion, string remoteVersion) = Updater.updateAvailable(releaseTag);
Expand All @@ -186,7 +182,7 @@ private static async Task LaunchUpdater()
MessageBox.Show("Your Creature Collection has been modified since it was last saved. " +
"Please either save or discard your changes to proceed with the update.",
"Unsaved Changes", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return (false, null);
return (null, null);
}

return (true, urls);
Expand Down

0 comments on commit d545bec

Please sign in to comment.