1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-05 06:45:37 +01:00
GiantsTools/Giants.Launcher/Updater/Updater.cs

111 lines
3.3 KiB
C#
Raw Normal View History

2020-08-10 06:58:51 +02:00
using System;
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;
using Giants.WebApi.Clients;
2020-08-10 06:58:51 +02:00
namespace Giants.Launcher
{
public class Updater
{
2020-08-10 08:24:50 +02:00
private readonly Version appVersion;
private readonly AsyncCompletedEventHandler updateCompletedCallback;
private readonly DownloadProgressChangedEventHandler updateProgressCallback;
2020-08-10 06:58:51 +02:00
public Updater(
Version appVersion,
AsyncCompletedEventHandler updateCompletedCallback,
DownloadProgressChangedEventHandler updateProgressCallback)
2020-08-10 06:58:51 +02:00
{
2020-08-10 08:24:50 +02:00
this.appVersion = appVersion;
this.updateCompletedCallback = updateCompletedCallback;
this.updateProgressCallback = updateProgressCallback;
}
2020-08-10 06:58:51 +02:00
public Task UpdateApplication(ApplicationType applicationType, VersionInfo versionInfo)
{
try
{
if (this.ToVersion(versionInfo.Version) > this.appVersion)
{
this.StartApplicationUpdate(applicationType, versionInfo);
}
}
catch (Exception)
{
}
2020-08-10 06:58:51 +02:00
return Task.CompletedTask;
2020-08-10 06:58:51 +02:00
}
private int GetHttpFileSize(Uri uri)
{
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))
{
resp.Close();
return contentLength;
}
resp.Close();
return -1;
}
private void StartApplicationUpdate(ApplicationType applicationType, VersionInfo versionInfo)
2020-08-10 06:58:51 +02:00
{
// Display update prompt
string updateMsg = applicationType == ApplicationType.Game ?
string.Format(Resources.UpdateAvailableText, this.ToVersion(versionInfo.Version).ToString()) :
string.Format(Resources.LauncherUpdateAvailableText, this.ToVersion(versionInfo.Version).ToString());
2020-08-10 06:58:51 +02:00
if (MessageBox.Show(updateMsg, Resources.UpdateAvailableTitle, MessageBoxButtons.YesNo) == DialogResult.No)
{
2020-08-10 06:58:51 +02:00
return; // User declined update
}
2020-08-10 06:58:51 +02:00
string patchFileName = Path.GetFileName(versionInfo.InstallerUri.AbsoluteUri);
string localPath = Path.Combine(Path.GetTempPath(), patchFileName);
2020-08-10 06:58:51 +02:00
// Delete the file locally if it already exists, just to be safe
if (File.Exists(localPath))
2020-08-10 06:58:51 +02:00
{
File.Delete(localPath);
2020-08-10 06:58:51 +02:00
}
int fileSize = this.GetHttpFileSize(versionInfo.InstallerUri);
if (fileSize == -1)
2020-08-10 06:58:51 +02:00
{
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()
2020-08-10 06:58:51 +02:00
{
FilePath = localPath,
FileSize = fileSize,
ApplicationType = applicationType
2020-08-10 06:58:51 +02:00
};
// Download the update
// TODO: Super old code, replace this with async HttpClient
WebClient client = new WebClient();
client.DownloadFileAsync(versionInfo.InstallerUri, localPath, updateInfo);
client.DownloadFileCompleted += this.updateCompletedCallback;
client.DownloadProgressChanged += this.updateProgressCallback;
2020-08-10 06:58:51 +02:00
}
private Version ToVersion(AppVersion version)
2020-08-10 06:58:51 +02:00
{
return new Version(version.Major, version.Minor, version.Build, version.Revision);
2020-08-10 06:58:51 +02:00
}
}
2020-08-10 06:58:51 +02:00
}