Skip to content

Commit 11c9ffc

Browse files
authored
Merge pull request #167 from Legi428/AsyncReleasesDL
Fix issues with fetching updates of Unity versions by using the new Api
2 parents f9cf564 + 64cc970 commit 11c9ffc

15 files changed

+531
-431
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace UnityLauncherPro
2+
{
3+
public readonly struct DownloadProgress
4+
{
5+
public long TotalRead { get; }
6+
public long TotalBytes { get; }
7+
8+
public DownloadProgress(long totalRead, long totalBytes)
9+
{
10+
TotalRead = totalRead;
11+
TotalBytes = totalBytes;
12+
}
13+
}
14+
}

UnityLauncherPro/Data/UnityVersion.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace UnityLauncherPro
6+
{
7+
public class UnityVersion
8+
{
9+
[JsonPropertyName("version")]
10+
public string Version { get; set; }
11+
[JsonPropertyName("stream")]
12+
[JsonConverter(typeof(UnityVersionStreamConverter))]
13+
public UnityVersionStream Stream { get; set; }
14+
[JsonPropertyName("releaseDate")]
15+
public DateTime ReleaseDate { get; set; }
16+
}
17+
18+
public class UnityVersionStreamConverter : JsonConverter<UnityVersionStream>
19+
{
20+
public override UnityVersionStream Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
string streamString = reader.GetString();
23+
if (Enum.TryParse<UnityVersionStream>(streamString, true, out var result))
24+
{
25+
return result;
26+
}
27+
throw new JsonException($"Unable to convert \"{streamString}\" to UnityVersionStream");
28+
}
29+
30+
public override void Write(Utf8JsonWriter writer, UnityVersionStream value, JsonSerializerOptions options)
31+
{
32+
writer.WriteStringValue(value.ToString().ToUpper());
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace UnityLauncherPro
5+
{
6+
public class UnityVersionResponse
7+
{
8+
[JsonPropertyName("offset")]
9+
public int Offset { get; set; }
10+
[JsonPropertyName("limit")]
11+
public int Limit { get; set; }
12+
[JsonPropertyName("total")]
13+
public int Total { get; set; }
14+
[JsonPropertyName("results")]
15+
public List<UnityVersion> Results { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace UnityLauncherPro
2+
{
3+
public enum UnityVersionStream
4+
{
5+
Alpha,
6+
Beta,
7+
LTS,
8+
Tech
9+
}
10+
}

UnityLauncherPro/Data/Updates.cs

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Window x:Class="UnityLauncherPro.DownloadProgressWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Title="Download Progress" Height="70" Width="500"
5+
ResizeMode="NoResize"
6+
WindowStyle="None"
7+
PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus"
8+
Background="{DynamicResource ThemeDarkestBackground}">
9+
<Grid>
10+
<Grid>
11+
<Label Content="{Binding Title, RelativeSource={RelativeSource AncestorType=Window}, FallbackValue=Title}" IsHitTestVisible="False" Margin="5,0,0,-5" Foreground="{DynamicResource ThemeMainTitle}" FontSize="12" HorizontalAlignment="Left" />
12+
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="23" Width="23" Background="Transparent" Click="CancelDownloadClick" Padding="0,2" IsTabStop="False">
13+
<TextBlock Text="" FontSize="10" Foreground="{DynamicResource ThemeWindowMinClose}" Padding="5,3,4,4" HorizontalAlignment="Center" />
14+
</Button>
15+
</Grid>
16+
<Grid VerticalAlignment="Bottom" Background="{DynamicResource ThemeMainBackgroundColor}">
17+
<ProgressBar x:Name="ProgressBar" Height="23" VerticalAlignment="Center" Margin="10, 10, 70, 10"/>
18+
<TextBlock x:Name="ProgressText" Text="0%" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10, 10, 70, 10"/>
19+
<Button Style="{StaticResource CustomButton}" Content="Cancel" Width="50" Height="23" HorizontalAlignment="Right" Margin="10" Click="CancelDownloadClick"/>
20+
</Grid>
21+
</Grid>
22+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Input;
4+
5+
namespace UnityLauncherPro
6+
{
7+
public partial class DownloadProgressWindow
8+
{
9+
private readonly Action _cancelAction;
10+
private readonly string _subjectName;
11+
private static MainWindow MainWindow => Tools.mainWindow;
12+
13+
public DownloadProgressWindow(string subjectName, Action cancelAction = null)
14+
{
15+
InitializeComponent();
16+
_subjectName = subjectName;
17+
Title = subjectName;
18+
_cancelAction = cancelAction;
19+
Topmost = true;
20+
Owner = MainWindow;
21+
WindowStartupLocation = WindowStartupLocation.CenterOwner;
22+
MainWindow.IsEnabled = false;
23+
}
24+
25+
public void UpdateProgress(DownloadProgress downloadProgress)
26+
{
27+
Title = $"Downloading {_subjectName} ({downloadProgress.TotalRead / 1024d / 1024d:F1} MB / {downloadProgress.TotalBytes / 1024d / 1024d:F1} MB)";
28+
var progress = downloadProgress.TotalBytes == 0 ? 0 : downloadProgress.TotalRead * 100d / downloadProgress.TotalBytes;
29+
ProgressBar.Value = progress;
30+
ProgressText.Text = $"{progress / 100:P1}";
31+
}
32+
33+
private void CancelDownloadClick(object sender, RoutedEventArgs e)
34+
{
35+
CancelDownload();
36+
}
37+
38+
private void CancelDownload()
39+
{
40+
_cancelAction?.Invoke();
41+
}
42+
43+
private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
44+
{
45+
var window = (Window)sender;
46+
window.Topmost = true;
47+
}
48+
49+
protected override void OnClosed(EventArgs e)
50+
{
51+
base.OnClosed(e);
52+
_cancelAction?.Invoke();
53+
MainWindow.IsEnabled = true;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)