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;
|
2020-08-11 10:29:45 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-08-10 06:58:51 +02:00
|
|
|
|
using System.Windows.Forms;
|
2020-08-11 10:29:45 +02:00
|
|
|
|
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;
|
2020-08-11 10:29:45 +02:00
|
|
|
|
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;
|
2020-08-11 10:29:45 +02:00
|
|
|
|
this.updateCompletedCallback = updateCompletedCallback;
|
|
|
|
|
this.updateProgressCallback = updateProgressCallback;
|
|
|
|
|
}
|
2020-08-10 06:58:51 +02:00
|
|
|
|
|
2020-10-18 08:44:41 +02:00
|
|
|
|
public bool IsUpdateRequired(ApplicationType applicationType, VersionInfo versionInfo)
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
2020-10-18 08:44:41 +02:00
|
|
|
|
if (this.ToVersion(versionInfo.Version) > this.appVersions[applicationType])
|
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(versionInfo.Version).ToString()) :
|
|
|
|
|
string.Format(Resources.LauncherUpdateAvailableText, this.ToVersion(versionInfo.Version).ToString());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-09-11 07:50:39 +02:00
|
|
|
|
catch (Exception e)
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
2020-09-11 07:50:39 +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-11 10:29:45 +02:00
|
|
|
|
{
|
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-11 10:29:45 +02:00
|
|
|
|
{
|
2020-08-13 06:58:53 +02:00
|
|
|
|
resp.Close();
|
|
|
|
|
return contentLength;
|
2020-08-11 10:29:45 +02:00
|
|
|
|
}
|
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-11 10:29:45 +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();
|
2020-08-11 10:29:45 +02:00
|
|
|
|
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
|
|
|
|
|
2020-08-11 10:29:45 +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
|
|
|
|
}
|