2020-02-07 04:20:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-02-08 00:22:20 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2020-02-07 04:20:49 +00:00
|
|
|
|
using Wabbajack.Launcher.Annotations;
|
2021-03-09 12:31:45 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-02-07 04:20:49 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Launcher
|
|
|
|
|
{
|
|
|
|
|
public class MainWindowVM : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2020-02-08 00:22:20 +00:00
|
|
|
|
private WebClient _client = new WebClient();
|
2020-02-08 05:31:50 +00:00
|
|
|
|
public Uri GITHUB_REPO = new Uri("https://api.github.com/repos/wabbajack-tools/wabbajack/releases");
|
2020-02-08 00:22:20 +00:00
|
|
|
|
|
2020-02-07 04:20:49 +00:00
|
|
|
|
|
|
|
|
|
[NotifyPropertyChangedInvocator]
|
|
|
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _status = "Checking for Updates";
|
2020-02-08 00:22:20 +00:00
|
|
|
|
private Release _version;
|
2021-03-09 12:31:45 +00:00
|
|
|
|
private List<string> _errors = new List<string>();
|
2020-02-07 04:20:49 +00:00
|
|
|
|
|
|
|
|
|
public string Status
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_status = value;
|
|
|
|
|
OnPropertyChanged("Status");
|
|
|
|
|
}
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MainWindowVM()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(CheckForUpdates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CheckForUpdates()
|
|
|
|
|
{
|
2020-02-08 00:22:20 +00:00
|
|
|
|
_client.Headers.Add ("user-agent", "Wabbajack Launcher");
|
|
|
|
|
Status = "Selecting Release";
|
2020-02-07 04:20:49 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var releases = await GetReleases();
|
|
|
|
|
_version = releases.OrderByDescending(r =>
|
|
|
|
|
{
|
2020-02-08 06:05:33 +00:00
|
|
|
|
if (r.Tag.Split(".").Length == 4 && Version.TryParse(r.Tag, out var v))
|
2020-02-08 00:22:20 +00:00
|
|
|
|
return v;
|
|
|
|
|
return new Version(0, 0, 0, 0);
|
|
|
|
|
}).FirstOrDefault();
|
|
|
|
|
}
|
2021-03-09 12:31:45 +00:00
|
|
|
|
catch (Exception ex)
|
2020-02-08 00:22:20 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
_errors.Add(ex.Message);
|
|
|
|
|
await FinishAndExit();
|
2020-02-08 00:22:20 +00:00
|
|
|
|
}
|
2021-03-09 12:04:50 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
if (_version == null)
|
2021-03-09 12:31:45 +00:00
|
|
|
|
{
|
|
|
|
|
_errors.Add("Unable to parse Github releases");
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
2020-02-08 00:22:20 +00:00
|
|
|
|
|
|
|
|
|
Status = "Looking for Updates";
|
2021-03-09 12:04:50 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
var base_folder = Path.Combine(Directory.GetCurrentDirectory(), _version.Tag);
|
2021-03-09 12:04:50 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
if (File.Exists(Path.Combine(base_folder, "Wabbajack.exe")))
|
2021-03-09 12:31:45 +00:00
|
|
|
|
{
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
2020-02-08 00:22:20 +00:00
|
|
|
|
|
|
|
|
|
var asset = _version.Assets.FirstOrDefault(a => a.Name == _version.Tag + ".zip");
|
|
|
|
|
if (asset == null)
|
2021-03-09 12:31:45 +00:00
|
|
|
|
{
|
|
|
|
|
_errors.Add("No zip file for release " + _version.Tag);
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
2020-02-07 04:20:49 +00:00
|
|
|
|
|
|
|
|
|
var wc = new WebClient();
|
|
|
|
|
wc.DownloadProgressChanged += UpdateProgress;
|
2020-02-08 00:22:20 +00:00
|
|
|
|
Status = $"Downloading {_version.Tag} ...";
|
2021-03-09 12:04:50 +00:00
|
|
|
|
byte[] data;
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-11 22:51:35 +00:00
|
|
|
|
data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl);
|
2021-03-09 12:04:50 +00:00
|
|
|
|
}
|
2021-03-09 12:31:45 +00:00
|
|
|
|
catch (Exception ex)
|
2021-03-09 12:04:50 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
_errors.Add(ex.Message);
|
2021-03-09 12:04:50 +00:00
|
|
|
|
// Something went wrong so fallback to original URL
|
2021-03-09 12:31:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex2)
|
|
|
|
|
{
|
|
|
|
|
_errors.Add(ex2.Message);
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
throw; // avoid unsigned variable 'data'
|
|
|
|
|
}
|
2021-03-09 12:04:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 12:31:45 +00:00
|
|
|
|
try
|
2020-02-07 04:20:49 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
using (var zip = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read))
|
2020-02-07 04:20:49 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
foreach (var entry in zip.Entries)
|
|
|
|
|
{
|
|
|
|
|
Status = $"Extracting: {entry.Name}";
|
|
|
|
|
var outPath = Path.Combine(base_folder, entry.FullName);
|
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(outPath)))
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
|
|
|
|
|
|
|
|
|
|
if (entry.FullName.EndsWith("/") || entry.FullName.EndsWith("\\"))
|
|
|
|
|
continue;
|
|
|
|
|
await using var o = entry.Open();
|
|
|
|
|
await using var of = File.Create(outPath);
|
|
|
|
|
await o.CopyToAsync(of);
|
|
|
|
|
}
|
2020-02-07 04:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 12:31:45 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_errors.Add(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
await FinishAndExit();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 04:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 12:31:45 +00:00
|
|
|
|
private async Task FinishAndExit()
|
2020-02-07 04:20:49 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
try
|
2020-02-07 04:20:49 +00:00
|
|
|
|
{
|
2021-03-09 12:31:45 +00:00
|
|
|
|
Status = "Launching...";
|
|
|
|
|
var wjFolder = Directory.EnumerateDirectories(Directory.GetCurrentDirectory())
|
|
|
|
|
.OrderByDescending(v =>
|
|
|
|
|
Version.TryParse(Path.GetFileName(v), out var ver) ? ver : new Version(0, 0, 0, 0))
|
|
|
|
|
.FirstOrDefault();
|
2021-07-20 22:16:27 +00:00
|
|
|
|
|
|
|
|
|
var filename = Path.Combine(wjFolder, "Wabbajack.exe");
|
|
|
|
|
await CreateBatchFile(filename);
|
2021-03-09 12:31:45 +00:00
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
{
|
2021-07-20 22:16:27 +00:00
|
|
|
|
FileName = filename,
|
2021-03-09 12:31:45 +00:00
|
|
|
|
Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(s => s.Contains(' ') ? '\"' + s + '\"' : s)),
|
|
|
|
|
WorkingDirectory = wjFolder,
|
|
|
|
|
};
|
|
|
|
|
Process.Start(info);
|
|
|
|
|
}
|
2021-03-18 03:36:37 +00:00
|
|
|
|
catch (Exception)
|
2021-03-09 12:31:45 +00:00
|
|
|
|
{
|
|
|
|
|
if (_errors.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Status = "Failed: Unknown error";
|
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
}
|
|
|
|
|
foreach (var error in _errors)
|
|
|
|
|
{
|
|
|
|
|
Status = "Failed: " + error;
|
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
}
|
2020-02-07 04:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 22:16:27 +00:00
|
|
|
|
private async Task CreateBatchFile(string filename)
|
|
|
|
|
{
|
|
|
|
|
filename = Path.Combine(Path.GetDirectoryName(filename), "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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 04:20:49 +00:00
|
|
|
|
private void UpdateProgress(object sender, DownloadProgressChangedEventArgs e)
|
|
|
|
|
{
|
2020-02-08 00:22:20 +00:00
|
|
|
|
Status = $"Downloading {_version.Tag} ({e.ProgressPercentage}%)...";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Release[]> GetReleases()
|
|
|
|
|
{
|
|
|
|
|
Status = "Checking GitHub Repository";
|
|
|
|
|
var data = await _client.DownloadStringTaskAsync(GITHUB_REPO);
|
|
|
|
|
Status = "Parsing Response";
|
|
|
|
|
return JsonConvert.DeserializeObject<Release[]>(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Release
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("tag_name")]
|
|
|
|
|
public string Tag { get; set; }
|
2021-03-09 12:04:50 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
[JsonProperty("assets")]
|
|
|
|
|
public Asset[] Assets { get; set; }
|
2021-03-09 12:04:50 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Asset
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("browser_download_url")]
|
|
|
|
|
public Uri BrowserDownloadUrl { get; set; }
|
2021-03-11 22:51:35 +00:00
|
|
|
|
|
2020-02-08 00:22:20 +00:00
|
|
|
|
[JsonProperty("name")]
|
|
|
|
|
public string Name { get; set; }
|
2020-02-07 04:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|