2021-10-02 17:01:09 +00:00
|
|
|
|
using System;
|
2021-10-08 13:16:51 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-10-02 17:01:09 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2022-06-01 21:45:55 +00:00
|
|
|
|
using System.Threading;
|
2021-10-08 13:16:51 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
2022-06-01 21:45:55 +00:00
|
|
|
|
using Wabbajack.Compression.Zip;
|
|
|
|
|
using Wabbajack.Downloaders.Http;
|
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.DTOs.DownloadStates;
|
|
|
|
|
using Wabbajack.Networking.NexusApi;
|
2021-10-08 13:16:51 +00:00
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
|
using Wabbajack.Paths.IO;
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.Launcher.ViewModels;
|
|
|
|
|
|
|
|
|
|
public class MainWindowViewModel : ViewModelBase
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private readonly WebClient _client = new();
|
|
|
|
|
private readonly List<string> _errors = new();
|
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
private (Version Version, long Size, Func<Task<Uri>> Uri) _version;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public Uri GITHUB_REPO = new("https://api.github.com/repos/wabbajack-tools/wabbajack/releases");
|
2022-06-01 21:45:55 +00:00
|
|
|
|
private readonly NexusApi _nexusApi;
|
|
|
|
|
private readonly HttpDownloader _downloader;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
public MainWindowViewModel(NexusApi nexusApi, HttpDownloader downloader)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
_nexusApi = nexusApi;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Status = "Checking for new versions";
|
2022-06-01 21:45:55 +00:00
|
|
|
|
_downloader = downloader;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var tsk = CheckForUpdates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Reactive] public string Status { get; set; }
|
2021-10-08 13:16:51 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private async Task CheckForUpdates()
|
|
|
|
|
{
|
|
|
|
|
_client.Headers.Add("user-agent", "Wabbajack Launcher");
|
|
|
|
|
Status = "Selecting Release";
|
|
|
|
|
|
|
|
|
|
try
|
2021-10-08 13:16:51 +00:00
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
var nexusRelease = await GetNexusReleases(CancellationToken.None);
|
|
|
|
|
if (nexusRelease != default)
|
|
|
|
|
_version = nexusRelease;
|
|
|
|
|
else
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
_version = await GetGithubRelease(CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-08 13:16:51 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
catch (Exception ex)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
_errors.Add(ex.Message);
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
if (_version == default)
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
_errors.Add("Unable to find releases");
|
2021-10-23 16:51:17 +00:00
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Status = "Looking for Updates";
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
var baseFolder = KnownFolders.CurrentDirectory.Combine(_version.Version.ToString());
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
if (baseFolder.Combine("Wabbajack.exe").FileExists()) await FinishAndExit();
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
Status = $"Getting download Uri for {_version.Version}";
|
|
|
|
|
var uri = await _version.Uri();
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var wc = new WebClient();
|
|
|
|
|
wc.DownloadProgressChanged += UpdateProgress;
|
2022-06-01 21:45:55 +00:00
|
|
|
|
Status = $"Downloading {_version.Version} ...";
|
2021-10-23 16:51:17 +00:00
|
|
|
|
byte[] data;
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
data = await wc.DownloadDataTaskAsync(uri);
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_errors.Add(ex.Message);
|
|
|
|
|
// Something went wrong so fallback to original URL
|
2021-10-02 17:01:09 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
data = await wc.DownloadDataTaskAsync(uri);
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
catch (Exception ex2)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
_errors.Add(ex2.Message);
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
throw; // avoid unsigned variable 'data'
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
using var zip = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read);
|
|
|
|
|
foreach (var entry in zip.Entries)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
Status = $"Extracting: {entry.Name}";
|
|
|
|
|
var outPath = baseFolder.Combine(entry.FullName.ToRelativePath());
|
|
|
|
|
if (!outPath.Parent.DirectoryExists())
|
|
|
|
|
outPath.Parent.CreateDirectory();
|
|
|
|
|
|
|
|
|
|
if (entry.FullName.EndsWith("/") || entry.FullName.EndsWith("\\"))
|
|
|
|
|
continue;
|
|
|
|
|
await using var o = entry.Open();
|
|
|
|
|
await using var of = outPath.Open(FileMode.Create, FileAccess.Write, FileShare.None);
|
|
|
|
|
await o.CopyToAsync(of);
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_errors.Add(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private async Task FinishAndExit()
|
|
|
|
|
{
|
|
|
|
|
try
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Status = "Launching...";
|
|
|
|
|
var wjFolder = KnownFolders.CurrentDirectory.EnumerateDirectories()
|
|
|
|
|
.OrderByDescending(v =>
|
|
|
|
|
Version.TryParse(v.FileName.ToString(), out var ver) ? ver : new Version(0, 0, 0, 0))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var filename = wjFolder.Combine("Wabbajack.exe");
|
|
|
|
|
await CreateBatchFile(filename);
|
|
|
|
|
var info = new ProcessStartInfo
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
FileName = filename.ToString(),
|
|
|
|
|
Arguments = string.Join(" ",
|
|
|
|
|
Environment.GetCommandLineArgs().Skip(1).Select(s => s.Contains(' ') ? '\"' + s + '\"' : s)),
|
|
|
|
|
WorkingDirectory = wjFolder.ToString()
|
|
|
|
|
};
|
|
|
|
|
Process.Start(info);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
if (_errors.Count == 0)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Status = "Failed: Unknown error";
|
|
|
|
|
await Task.Delay(10000);
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
foreach (var error in _errors)
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Status = "Failed: " + error;
|
|
|
|
|
await Task.Delay(10000);
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
finally
|
2021-10-02 17:01:09 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Environment.Exit(0);
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private async Task CreateBatchFile(AbsolutePath filename)
|
|
|
|
|
{
|
|
|
|
|
filename = filename.Parent.Combine("wabbajack-cli.exe");
|
|
|
|
|
var data = $"\"{filename}\" %*";
|
|
|
|
|
var file = Path.Combine(Directory.GetCurrentDirectory(), "wabbajack-cli.bat");
|
|
|
|
|
if (File.Exists(file) && await File.ReadAllTextAsync(file) == data) return;
|
|
|
|
|
await File.WriteAllTextAsync(file, data);
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private void UpdateProgress(object sender, DownloadProgressChangedEventArgs e)
|
|
|
|
|
{
|
2022-06-01 21:45:55 +00:00
|
|
|
|
Status = $"Downloading {_version.Version} ({e.ProgressPercentage}%)...";
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2022-06-01 21:45:55 +00:00
|
|
|
|
private async Task<(Version Version, long Size, Func<Task<Uri>> Uri)> GetGithubRelease(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var releases = await GetGithubReleases();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var version = releases.Select(r =>
|
|
|
|
|
{
|
|
|
|
|
if (r.Tag.Split(".").Length == 4 && Version.TryParse(r.Tag, out var v))
|
|
|
|
|
return (v, r);
|
|
|
|
|
return (new Version(0, 0, 0, 0), r);
|
|
|
|
|
})
|
|
|
|
|
.OrderByDescending(r => r.Item1)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var asset = version.r.Assets.FirstOrDefault(a => a.Name == version.Item1 + ".zip");
|
|
|
|
|
if (asset == null)
|
|
|
|
|
{
|
|
|
|
|
Status = $"Error, no asset found for Github Release {version.r}";
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (version.Item1, asset.Size, async () => asset!.BrowserDownloadUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Release[]> GetGithubReleases()
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
|
|
|
|
Status = "Checking GitHub Repository";
|
|
|
|
|
var data = await _client.DownloadStringTaskAsync(GITHUB_REPO);
|
|
|
|
|
Status = "Parsing Response";
|
|
|
|
|
return JsonSerializer.Deserialize<Release[]>(data)!;
|
|
|
|
|
}
|
2022-06-01 21:45:55 +00:00
|
|
|
|
|
|
|
|
|
private async Task<(Version Version, long Size, Func<Task<Uri>> uri)> GetNexusReleases(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
Status = "Checking Nexus for updates";
|
|
|
|
|
if (!await _nexusApi.IsPremium(token))
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
var data = await _nexusApi.ModFiles("site", 403, token);
|
|
|
|
|
Status = "Parsing Response";
|
|
|
|
|
//return JsonSerializer.Deserialize<Release[]>(data)!;
|
|
|
|
|
|
|
|
|
|
var found = data.info.Files.Where(f => f.CategoryId == 5)
|
|
|
|
|
.Where(f => f.Name.EndsWith(".zip"))
|
|
|
|
|
.Select(f => Version.TryParse(f.Name[..^4], out var version) ? (version, f.SizeInBytes ?? f.Size, f.FileId) : default)
|
|
|
|
|
.FirstOrDefault(f => f != default);
|
|
|
|
|
if (found == default) return default;
|
|
|
|
|
|
|
|
|
|
return (found.version, found.Item2, async () =>
|
|
|
|
|
{
|
|
|
|
|
var link = await _nexusApi.DownloadLink("site", 403, found.FileId, token);
|
|
|
|
|
|
|
|
|
|
return link.info.First().URI;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private class Release
|
|
|
|
|
{
|
|
|
|
|
[JsonPropertyName("tag_name")] public string Tag { get; set; }
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[JsonPropertyName("assets")] public Asset[] Assets { get; set; }
|
|
|
|
|
}
|
2021-10-02 17:01:09 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private class Asset
|
|
|
|
|
{
|
|
|
|
|
[JsonPropertyName("browser_download_url")]
|
|
|
|
|
public Uri BrowserDownloadUrl { get; set; }
|
2021-10-08 13:16:51 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[JsonPropertyName("name")] public string Name { get; set; }
|
2022-06-01 21:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("size")] public long Size { get; set; }
|
2021-10-02 17:01:09 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|