Wip for 3.0 launcher

This commit is contained in:
Timothy Baldridge 2021-10-02 11:01:09 -06:00
parent e4582ebde3
commit 03b3ec498d
10 changed files with 1504 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
<Application x:Class="Wabbajack.Launcher.App"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wabbajack.Launcher"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,11 @@
using Avalonia;
namespace Wabbajack.Launcher
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

View File

@ -0,0 +1,23 @@
<Window x:Class="Wabbajack.Launcher.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wabbajack.Launcher"
mc:Ignorable="d"
Title="Wabbajack Launcher" Height="320" Width="600"
WindowStyle="None"
Background="#121212"
BorderThickness="0"
AllowsTransparency="False"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="275"></RowDefinition>
<RowDefinition Height="45"></RowDefinition>
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="Wabba_Mouth_Small.png"></Image>
<Label Grid.Row="1" Foreground="White" FontSize="20" Content="{Binding Status}"></Label>
</Grid>
</Window>

View File

@ -0,0 +1,25 @@
using System;
using Avalonia.Controls;
namespace Wabbajack.Launcher
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
try
{
DataContext = new MainWindowVM();
}
catch(Exception ex)
{
System.Console.Error.WriteLine("Error creating datacontext.");
System.Console.Error.WriteLine(ex);
throw;
}
}
}
}

View File

@ -0,0 +1,228 @@
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;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.ComponentModel;
namespace Wabbajack.Launcher
{
public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private WebClient _client = new();
public Uri GITHUB_REPO = new("https://api.github.com/repos/wabbajack-tools/wabbajack/releases");
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _status = "Checking for Updates";
private Release _version;
private List<string> _errors = new List<string>();
public string Status
{
set
{
_status = value;
OnPropertyChanged("Status");
}
get
{
return _status;
}
}
public MainWindowVM()
{
Task.Run(CheckForUpdates);
}
private async Task CheckForUpdates()
{
_client.Headers.Add ("user-agent", "Wabbajack Launcher");
Status = "Selecting Release";
try
{
var releases = await GetReleases();
_version = releases.OrderByDescending(r =>
{
if (r.Tag.Split(".").Length == 4 && Version.TryParse(r.Tag, out var v))
return v;
return new Version(0, 0, 0, 0);
}).FirstOrDefault();
}
catch (Exception ex)
{
_errors.Add(ex.Message);
await FinishAndExit();
}
if (_version == null)
{
_errors.Add("Unable to parse Github releases");
await FinishAndExit();
}
Status = "Looking for Updates";
var base_folder = Path.Combine(Directory.GetCurrentDirectory(), _version.Tag);
if (File.Exists(Path.Combine(base_folder, "Wabbajack.exe")))
{
await FinishAndExit();
}
var asset = _version.Assets.FirstOrDefault(a => a.Name == _version.Tag + ".zip");
if (asset == null)
{
_errors.Add("No zip file for release " + _version.Tag);
await FinishAndExit();
}
var wc = new WebClient();
wc.DownloadProgressChanged += UpdateProgress;
Status = $"Downloading {_version.Tag} ...";
byte[] data;
try
{
data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl);
}
catch (Exception ex)
{
_errors.Add(ex.Message);
// Something went wrong so fallback to original URL
try
{
data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl);
}
catch (Exception ex2)
{
_errors.Add(ex2.Message);
await FinishAndExit();
throw; // avoid unsigned variable 'data'
}
}
try
{
using (var zip = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read))
{
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);
}
}
}
catch (Exception ex)
{
_errors.Add(ex.Message);
}
finally
{
await FinishAndExit();
}
}
private async Task FinishAndExit()
{
try
{
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();
var filename = Path.Combine(wjFolder, "Wabbajack.exe");
await CreateBatchFile(filename);
var info = new ProcessStartInfo
{
FileName = filename,
Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(s => s.Contains(' ') ? '\"' + s + '\"' : s)),
WorkingDirectory = wjFolder,
};
Process.Start(info);
}
catch (Exception)
{
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);
}
}
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);
}
private void UpdateProgress(object sender, DownloadProgressChangedEventArgs e)
{
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 JsonSerializer.Deserialize<Release[]>(data)!;
}
class Release
{
[JsonPropertyName("tag_name")]
public string Tag { get; set; }
[JsonPropertyName("assets")]
public Asset[] Assets { get; set; }
}
class Asset
{
[JsonPropertyName("browser_download_url")]
public Uri BrowserDownloadUrl { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework Condition=" '$(OS)' == 'Windows_NT'">net6.0-windows</TargetFramework>
<TargetFramework Condition=" '$(OS)' != 'Windows_NT'">net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<AssemblyVersion>2.5.3.1</AssemblyVersion>
<FileVersion>2.5.3.1</FileVersion>
<Copyright>Copyright © 2019-2020</Copyright>
<Description>Wabbajack Application Launcher</Description>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<AssemblyName>Wabbajack</AssemblyName>
<RootNamespace>Wabbajack</RootNamespace>
<IncludeSymbolsInSingleFile>false</IncludeSymbolsInSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<TrimMode>Link</TrimMode>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\Icons\wabbajack.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Wabba_Mouth_Small.png" />
<Resource Include="Wabba_Mouth_Small.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.7" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.7" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
</ItemGroup>
</Project>

View File

@ -112,6 +112,8 @@ ProjectSection(SolutionItems) = preProject
nuget.config = nuget.config
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wabbajack.Launcher", "Wabbajack.Launcher\Wabbajack.Launcher.csproj", "{8A4B3B2F-01EC-4C47-9470-FC06DF162D4D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -310,6 +312,10 @@ Global
{DEB4B073-4EAA-49FD-9D43-F0F8CB930E7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEB4B073-4EAA-49FD-9D43-F0F8CB930E7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEB4B073-4EAA-49FD-9D43-F0F8CB930E7A}.Release|Any CPU.Build.0 = Release|Any CPU
{8A4B3B2F-01EC-4C47-9470-FC06DF162D4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A4B3B2F-01EC-4C47-9470-FC06DF162D4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A4B3B2F-01EC-4C47-9470-FC06DF162D4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A4B3B2F-01EC-4C47-9470-FC06DF162D4D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4057B668-8595-44FE-9805-007B284A838F} = {98B731EE-4FC0-4482-A069-BCBA25497871}