1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-16 19:55:36 +01:00
GiantsTools/Giants.Launcher/Updater/Updater.cs

116 lines
4.3 KiB
C#
Raw Permalink Normal View History

using Giants.WebApi.Clients;
using System;
2020-08-10 06:58:51 +02:00
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading.Tasks;
2020-08-10 06:58:51 +02:00
using System.Windows.Forms;
namespace Giants.Launcher
{
public class Updater
{
private readonly AsyncCompletedEventHandler updateCompletedCallback;
2020-08-13 06:58:53 +02:00
private readonly DownloadProgressChangedEventHandler updateProgressCallback;
public Updater(
AsyncCompletedEventHandler updateCompletedCallback,
DownloadProgressChangedEventHandler updateProgressCallback)
{
this.updateCompletedCallback = updateCompletedCallback;
this.updateProgressCallback = updateProgressCallback;
}
2020-08-10 06:58:51 +02:00
public bool IsUpdateRequired(ApplicationType applicationType, VersionInfo remoteVersionInfo, Version localVersion)
2020-08-13 06:58:53 +02:00
{
if (remoteVersionInfo?.InstallerUri != null
&& this.ToVersion(remoteVersionInfo.Version) > localVersion)
2020-08-13 06:58:53 +02:00
{
2020-10-18 08:44:41 +02:00
// Display update prompt
string updateMsg = applicationType == ApplicationType.Game ?
string.Format(Resources.UpdateAvailableText, this.ToVersion(remoteVersionInfo.Version).ToString()) :
string.Format(Resources.LauncherUpdateAvailableText, this.ToVersion(remoteVersionInfo.Version).ToString());
2020-10-18 08:44:41 +02:00
if (MessageBox.Show(updateMsg, Resources.UpdateAvailableTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
2020-08-13 06:58:53 +02:00
{
2020-10-18 08:44:41 +02:00
return true;
2020-08-13 06:58:53 +02:00
}
}
2020-10-18 08:44:41 +02:00
return false;
}
public Task UpdateApplication(ApplicationType applicationType, VersionInfo versionInfo)
{
try
{
this.StartApplicationUpdate(applicationType, versionInfo);
}
catch (Exception e)
2020-08-13 06:58:53 +02:00
{
string errorMsg = string.Format(Resources.UpdateDownloadFailedText, e.Message);
MessageBox.Show(errorMsg, Resources.UpdateDownloadFailedTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
2020-08-13 06:58:53 +02:00
}
return Task.CompletedTask;
}
private int GetHttpFileSize(Uri uri)
{
2020-08-13 06:58:53 +02:00
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Proxy = null;
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK && int.TryParse(resp.Headers.Get("Content-Length"), out int contentLength))
{
2020-08-13 06:58:53 +02:00
resp.Close();
return contentLength;
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
resp.Close();
return -1;
2020-08-10 06:58:51 +02:00
}
2020-08-13 06:58:53 +02:00
private void StartApplicationUpdate(ApplicationType applicationType, VersionInfo versionInfo)
{
string patchFileName = Path.GetFileName(versionInfo.InstallerUri.AbsoluteUri);
string localPath = Path.Combine(Path.GetTempPath(), patchFileName);
// Delete the file locally if it already exists, just to be safe
if (File.Exists(localPath))
{
File.Delete(localPath);
}
int fileSize = this.GetHttpFileSize(versionInfo.InstallerUri);
if (fileSize == -1)
{
string errorMsg = string.Format(Resources.UpdateDownloadFailedText, "File not found on server.");
MessageBox.Show(errorMsg, Resources.UpdateDownloadFailedTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var updateInfo = new UpdateInfo()
{
FilePath = localPath,
FileSize = fileSize,
ApplicationType = applicationType
2020-08-10 06:58:51 +02:00
};
2020-08-13 06:58:53 +02:00
// Download the update
// TODO: Super old code, replace this with async HttpClient
WebClient client = new WebClient();
client.DownloadFileAsync(versionInfo.InstallerUri, localPath, updateInfo);
2020-08-13 06:58:53 +02:00
client.DownloadFileCompleted += this.updateCompletedCallback;
client.DownloadProgressChanged += this.updateProgressCallback;
}
2020-08-10 06:58:51 +02:00
private Version ToVersion(AppVersion version)
2020-08-13 06:58:53 +02:00
{
return new Version(version.Major, version.Minor, version.Build, version.Revision);
}
}
2020-08-10 06:58:51 +02:00
}