Skip to content

Commit

Permalink
Merge pull request #167 from Legi428/AsyncReleasesDL
Browse files Browse the repository at this point in the history
Fix issues with fetching updates of Unity versions by using the new Api
  • Loading branch information
unitycoder authored Oct 16, 2024
2 parents f9cf564 + 64cc970 commit 11c9ffc
Show file tree
Hide file tree
Showing 15 changed files with 531 additions and 431 deletions.
14 changes: 14 additions & 0 deletions UnityLauncherPro/Data/DownloadProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace UnityLauncherPro
{
public readonly struct DownloadProgress
{
public long TotalRead { get; }
public long TotalBytes { get; }

public DownloadProgress(long totalRead, long totalBytes)
{
TotalRead = totalRead;
TotalBytes = totalBytes;
}
}
}
35 changes: 35 additions & 0 deletions UnityLauncherPro/Data/UnityVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace UnityLauncherPro
{
public class UnityVersion
{
[JsonPropertyName("version")]
public string Version { get; set; }
[JsonPropertyName("stream")]
[JsonConverter(typeof(UnityVersionStreamConverter))]
public UnityVersionStream Stream { get; set; }
[JsonPropertyName("releaseDate")]
public DateTime ReleaseDate { get; set; }
}

public class UnityVersionStreamConverter : JsonConverter<UnityVersionStream>
{
public override UnityVersionStream Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string streamString = reader.GetString();
if (Enum.TryParse<UnityVersionStream>(streamString, true, out var result))
{
return result;
}
throw new JsonException($"Unable to convert \"{streamString}\" to UnityVersionStream");
}

public override void Write(Utf8JsonWriter writer, UnityVersionStream value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString().ToUpper());
}
}
}
17 changes: 17 additions & 0 deletions UnityLauncherPro/Data/UnityVersionResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace UnityLauncherPro
{
public class UnityVersionResponse
{
[JsonPropertyName("offset")]
public int Offset { get; set; }
[JsonPropertyName("limit")]
public int Limit { get; set; }
[JsonPropertyName("total")]
public int Total { get; set; }
[JsonPropertyName("results")]
public List<UnityVersion> Results { get; set; }
}
}
10 changes: 10 additions & 0 deletions UnityLauncherPro/Data/UnityVersionStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace UnityLauncherPro
{
public enum UnityVersionStream
{
Alpha,
Beta,
LTS,
Tech
}
}
10 changes: 0 additions & 10 deletions UnityLauncherPro/Data/Updates.cs

This file was deleted.

22 changes: 22 additions & 0 deletions UnityLauncherPro/DownloadProgressWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Window x:Class="UnityLauncherPro.DownloadProgressWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Download Progress" Height="70" Width="500"
ResizeMode="NoResize"
WindowStyle="None"
PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus"
Background="{DynamicResource ThemeDarkestBackground}">
<Grid>
<Grid>
<Label Content="{Binding Title, RelativeSource={RelativeSource AncestorType=Window}, FallbackValue=Title}" IsHitTestVisible="False" Margin="5,0,0,-5" Foreground="{DynamicResource ThemeMainTitle}" FontSize="12" HorizontalAlignment="Left" />
<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">
<TextBlock Text="" FontSize="10" Foreground="{DynamicResource ThemeWindowMinClose}" Padding="5,3,4,4" HorizontalAlignment="Center" />
</Button>
</Grid>
<Grid VerticalAlignment="Bottom" Background="{DynamicResource ThemeMainBackgroundColor}">
<ProgressBar x:Name="ProgressBar" Height="23" VerticalAlignment="Center" Margin="10, 10, 70, 10"/>
<TextBlock x:Name="ProgressText" Text="0%" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10, 10, 70, 10"/>
<Button Style="{StaticResource CustomButton}" Content="Cancel" Width="50" Height="23" HorizontalAlignment="Right" Margin="10" Click="CancelDownloadClick"/>
</Grid>
</Grid>
</Window>
56 changes: 56 additions & 0 deletions UnityLauncherPro/DownloadProgressWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Windows;
using System.Windows.Input;

namespace UnityLauncherPro
{
public partial class DownloadProgressWindow
{
private readonly Action _cancelAction;
private readonly string _subjectName;
private static MainWindow MainWindow => Tools.mainWindow;

public DownloadProgressWindow(string subjectName, Action cancelAction = null)
{
InitializeComponent();
_subjectName = subjectName;
Title = subjectName;
_cancelAction = cancelAction;
Topmost = true;
Owner = MainWindow;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
MainWindow.IsEnabled = false;
}

public void UpdateProgress(DownloadProgress downloadProgress)
{
Title = $"Downloading {_subjectName} ({downloadProgress.TotalRead / 1024d / 1024d:F1} MB / {downloadProgress.TotalBytes / 1024d / 1024d:F1} MB)";
var progress = downloadProgress.TotalBytes == 0 ? 0 : downloadProgress.TotalRead * 100d / downloadProgress.TotalBytes;
ProgressBar.Value = progress;
ProgressText.Text = $"{progress / 100:P1}";
}

private void CancelDownloadClick(object sender, RoutedEventArgs e)
{
CancelDownload();
}

private void CancelDownload()
{
_cancelAction?.Invoke();
}

private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var window = (Window)sender;
window.Topmost = true;
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_cancelAction?.Invoke();
MainWindow.IsEnabled = true;
}
}
}
Loading

0 comments on commit 11c9ffc

Please sign in to comment.