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

114 lines
4.3 KiB
C#
Raw Normal View History

2020-08-10 06:58:51 +02:00
using System;
2020-08-11 20:24:04 +02:00
using System.Collections.Generic;
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;
using Giants.WebApi.Clients;
2020-08-10 06:58:51 +02:00
namespace Giants.Launcher
{
public class Updater
{
2020-08-11 20:24:04 +02:00
private readonly IDictionary<ApplicationType, Version> appVersions;
private readonly AsyncCompletedEventHandler updateCompletedCallback;
2020-08-13 06:58:53 +02:00
private readonly DownloadProgressChangedEventHandler updateProgressCallback;
public Updater(
IDictionary<ApplicationType, Version> appVersions,
AsyncCompletedEventHandler updateCompletedCallback,
DownloadProgressChangedEventHandler updateProgressCallback)
{
2020-08-11 20:24:04 +02:00
this.appVersions = appVersions;
this.updateCompletedCallback = updateCompletedCallback;
this.updateProgressCallback = updateProgressCallback;
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public Task UpdateApplication(ApplicationType applicationType, VersionInfo versionInfo)
{
try
{
if (this.ToVersion(versionInfo.Version) > this.appVersions[applicationType])
{
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)
{
// 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());
if (MessageBox.Show(updateMsg, Resources.UpdateAvailableTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
2020-08-10 06:58:51 +02:00
{
2020-08-13 06:58:53 +02:00
return; // User declined update
}
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
}