2021-03-11 15:00:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib.Http;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib
|
|
|
|
|
{
|
|
|
|
|
public class LauncherUpdater
|
|
|
|
|
{
|
2021-03-19 05:04:27 +00:00
|
|
|
|
public static Lazy<AbsolutePath> CommonFolder = new (() =>
|
2021-03-11 15:00:26 +00:00
|
|
|
|
{
|
|
|
|
|
var entryPoint = AbsolutePath.EntryPoint;
|
2021-03-19 05:04:27 +00:00
|
|
|
|
|
2021-03-11 15:00:26 +00:00
|
|
|
|
// If we're not in a folder that looks like a version, abort
|
|
|
|
|
if (!Version.TryParse(entryPoint.FileName.ToString(), out var version))
|
|
|
|
|
{
|
2021-03-19 05:04:27 +00:00
|
|
|
|
return entryPoint;
|
2021-03-11 15:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're not in a folder that has Wabbajack.exe in the parent folder, abort
|
2021-03-19 05:04:27 +00:00
|
|
|
|
if (!entryPoint.Parent.Combine(Consts.AppName).WithExtension(new Extension(".exe")).IsFile)
|
2021-03-11 15:00:26 +00:00
|
|
|
|
{
|
2021-03-19 05:04:27 +00:00
|
|
|
|
return entryPoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entryPoint.Parent;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public static async Task Run()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (CommonFolder.Value == AbsolutePath.EntryPoint)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log("Outside of standard install folder, not updating");
|
2021-03-11 15:00:26 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 05:04:27 +00:00
|
|
|
|
var version = Version.Parse(AbsolutePath.EntryPoint.FileName.ToString());
|
|
|
|
|
|
|
|
|
|
var oldVersions = CommonFolder.Value
|
2021-03-11 15:00:26 +00:00
|
|
|
|
.EnumerateDirectories()
|
|
|
|
|
.Select(f => Version.TryParse(f.FileName.ToString(), out var ver) ? (ver, f) : default)
|
|
|
|
|
.Where(f => f != default)
|
|
|
|
|
.Where(f => f.ver < version)
|
|
|
|
|
.Select(f => f!)
|
|
|
|
|
.OrderByDescending(f => f)
|
|
|
|
|
.Skip(2)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var (_, path) in oldVersions)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Deleting old Wabbajack version at: {path}");
|
|
|
|
|
await path.DeleteDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var release = (await GetReleases())
|
|
|
|
|
.Select(release => Version.TryParse(release.Tag, out version) ? (version, release) : default)
|
|
|
|
|
.Where(r => r != default)
|
|
|
|
|
.OrderByDescending(r => r.version)
|
|
|
|
|
.Select(r =>
|
|
|
|
|
{
|
|
|
|
|
var (version, release) = r;
|
|
|
|
|
var asset = release.Assets.FirstOrDefault(a => a.Name == "Wabbajack.exe");
|
|
|
|
|
return asset != default ? (version, release, asset) : default;
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var launcherFolder = AbsolutePath.EntryPoint.Parent;
|
|
|
|
|
var exePath = launcherFolder.Combine("Wabbajack.exe");
|
|
|
|
|
|
|
|
|
|
var launcherVersion = FileVersionInfo.GetVersionInfo(exePath.ToString());
|
|
|
|
|
|
2021-04-01 20:52:23 +00:00
|
|
|
|
if (release != default && release.version > Version.Parse(launcherVersion.FileVersion!))
|
2021-03-11 15:00:26 +00:00
|
|
|
|
{
|
2021-03-11 22:51:35 +00:00
|
|
|
|
Utils.Log($"Updating Launcher from {launcherVersion.FileVersion} to {release.version}");
|
2021-03-11 15:00:26 +00:00
|
|
|
|
var tempPath = launcherFolder.Combine("Wabbajack.exe.temp");
|
|
|
|
|
var client = new Client();
|
|
|
|
|
client.UseChromeUserAgent();
|
|
|
|
|
await client.DownloadAsync(release.asset.BrowserDownloadUrl!, tempPath);
|
|
|
|
|
|
|
|
|
|
if (tempPath.Size != release.asset.Size)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log(
|
|
|
|
|
$"Downloaded launcher did not match expected size: {tempPath.Size} expected {release.asset.Size}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (exePath.Exists)
|
|
|
|
|
await exePath.DeleteAsync();
|
|
|
|
|
await tempPath.MoveToAsync(exePath);
|
|
|
|
|
|
|
|
|
|
Utils.Log("Finished updating wabbajack");
|
2021-03-11 22:51:35 +00:00
|
|
|
|
await Metrics.Send("updated_launcher", $"{launcherVersion.FileVersion} -> {release.version}");
|
2021-03-11 15:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<Release[]> GetReleases()
|
|
|
|
|
{
|
|
|
|
|
Utils.Log("Getting new Wabbajack version list");
|
|
|
|
|
var client = new Client();
|
|
|
|
|
client.UseChromeUserAgent();
|
|
|
|
|
return await client.GetJsonAsync<Release[]>(Consts.GITHUB_REPO_RELEASES.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Release
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("tag_name")] public string Tag { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
[JsonProperty("assets")] public Asset[] Assets { get; set; } = Array.Empty<Asset>();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Asset
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("browser_download_url")]
|
|
|
|
|
public Uri? BrowserDownloadUrl { get; set; }
|
|
|
|
|
|
|
|
|
|
[JsonProperty("name")] public string Name { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
[JsonProperty("size")] public long Size { get; set; } = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|