Skip to content

Commit

Permalink
Download/Launch refactoring part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
TrinityDevelopers committed Apr 16, 2020
1 parent 5a872ec commit b358043
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 95 deletions.
18 changes: 9 additions & 9 deletions ZenovaLauncher/Pages/PlayPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@
FontSize="14"
Style="{DynamicResource LauncherAccentButtonStyle}"
Click="PlayButtonClick"
Content="{Binding Path=IsNotDownloading, Converter={StaticResource PlayButtonContent}}"
IsEnabled="{Binding Path=IsNotDownloading}">
Content="{Binding Path=IsNotLaunching, Converter={StaticResource PlayButtonContent}}"
IsEnabled="{Binding Path=IsNotLaunching}">
</Button>
<Grid
x:Name="ProgressGrid"
Grid.Row="1"
Grid.ColumnSpan="3"
HorizontalAlignment="Stretch"
Background="{DynamicResource LauncherControlPageBackgroundChromeMediumBrush}"
Visibility="{Binding Path=IsDownloading, Converter={StaticResource BooleanToVisibilityConverter}}">
Visibility="{Binding Path=IsLaunching, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid.RenderTransform>
<TranslateTransform />
</Grid.RenderTransform>
Expand All @@ -103,7 +103,7 @@
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression>
<ei:ComparisonCondition LeftOperand="{Binding Path=IsDownloading}" RightOperand="True"/>
<ei:ComparisonCondition LeftOperand="{Binding Path=IsLaunching}" RightOperand="True"/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
Expand All @@ -116,7 +116,7 @@
</ei:ControlStoryboardAction.Storyboard>
</ei:ControlStoryboardAction>
</i:EventTrigger>
<ei:DataTrigger Binding="{Binding Path=IsDownloading}" Value="True">
<ei:DataTrigger Binding="{Binding Path=IsLaunching}" Value="True">
<ei:ControlStoryboardAction ControlStoryboardOption="Play">
<ei:ControlStoryboardAction.Storyboard>
<Storyboard>
Expand All @@ -134,13 +134,13 @@
ui:ControlHelper.CornerRadius="0"
Background="{DynamicResource LauncherControlTransientLowBackgroundBrush}"
Minimum="0"
Maximum="{Binding DownloadInfo.TotalSize}"
Value="{Binding DownloadInfo.DownloadedBytes}"
IsIndeterminate="{Binding DownloadInfo.IsProgressIndeterminate}" />
Maximum="{Binding LaunchInfo.TotalSize}"
Value="{Binding LaunchInfo.DownloadedBytes}"
IsIndeterminate="{Binding LaunchInfo.IsProgressIndeterminate}" />
<TextBlock
Margin="25,0,0,20"
VerticalAlignment="Center"
Text="{Binding DownloadInfo.DisplayStatus}"
Text="{Binding LaunchInfo.DisplayStatus}"
FontSize="12" />
</Grid>
<Grid Grid.Row="2" Grid.ColumnSpan="3" Background="{DynamicResource LauncherControlPageBackgroundChromeMediumBrush}" />
Expand Down
16 changes: 8 additions & 8 deletions ZenovaLauncher/Pages/ProfilesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
Click="PlayButtonClick"
Width="66"
Content="{Binding Path=Launching, Converter={StaticResource PlayButtonContent}}"
IsEnabled="{Binding Path=DataContext.IsNotDownloading, RelativeSource={RelativeSource AncestorType={x:Type Page}}}"/>
IsEnabled="{Binding Path=DataContext.IsNotLaunching, RelativeSource={RelativeSource AncestorType={x:Type Page}}}"/>
<Button Padding="5,5,6,6" Style="{DynamicResource LauncherDefaultButtonStyle}">
<Button.Content>
<ui:SymbolIcon Symbol="More" />
Expand Down Expand Up @@ -170,7 +170,7 @@
Grid.Row="1"
HorizontalAlignment="Stretch"
Background="{DynamicResource LauncherControlPageBackgroundChromeMediumBrush}"
Visibility="{Binding Path=IsDownloading, Converter={StaticResource BooleanToVisibilityConverter}}">
Visibility="{Binding Path=IsLaunching, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid.RenderTransform>
<TranslateTransform />
</Grid.RenderTransform>
Expand All @@ -179,7 +179,7 @@
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression>
<ei:ComparisonCondition LeftOperand="{Binding Path=IsDownloading}" RightOperand="True"/>
<ei:ComparisonCondition LeftOperand="{Binding Path=IsLaunching}" RightOperand="True"/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
Expand All @@ -192,7 +192,7 @@
</ei:ControlStoryboardAction.Storyboard>
</ei:ControlStoryboardAction>
</i:EventTrigger>
<ei:DataTrigger Binding="{Binding Path=IsDownloading}" Value="True">
<ei:DataTrigger Binding="{Binding Path=IsLaunching}" Value="True">
<ei:ControlStoryboardAction ControlStoryboardOption="Play">
<ei:ControlStoryboardAction.Storyboard>
<Storyboard>
Expand All @@ -210,13 +210,13 @@
ui:ControlHelper.CornerRadius="0"
Background="{DynamicResource LauncherControlTransientLowBackgroundBrush}"
Minimum="0"
Maximum="{Binding DownloadInfo.TotalSize}"
Value="{Binding DownloadInfo.DownloadedBytes}"
IsIndeterminate="{Binding DownloadInfo.IsProgressIndeterminate}" />
Maximum="{Binding LaunchInfo.TotalSize}"
Value="{Binding LaunchInfo.DownloadedBytes}"
IsIndeterminate="{Binding LaunchInfo.IsProgressIndeterminate}" />
<TextBlock
Margin="25,0,0,0"
VerticalAlignment="Center"
Text="{Binding DownloadInfo.DisplayStatus}"
Text="{Binding LaunchInfo.DisplayStatus}"
FontSize="12" />
</Grid>
</Grid>
Expand Down
95 changes: 74 additions & 21 deletions ZenovaLauncher/Profiles/ProfileLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Management.Core;
using Windows.Management.Deployment;
Expand All @@ -20,27 +21,29 @@ public class ProfileLauncher : NotifyPropertyChangedBase

private volatile bool _hasLaunchTask = false;

public bool IsDownloading => DownloadInfo != null;
public bool IsNotDownloading => !IsDownloading;
public bool IsLaunching => LaunchInfo != null;
public bool IsNotLaunching => !IsLaunching;

private VersionDownloadInfo _downloadInfo;
public VersionDownloadInfo DownloadInfo
private ProfileLaunchInfo _launchInfo;
public ProfileLaunchInfo LaunchInfo
{
get { return _downloadInfo; }
set { _downloadInfo = value; OnPropertyChanged("DownloadInfo"); OnPropertyChanged("IsDownloading"); OnPropertyChanged("IsNotDownloading"); }
get { return _launchInfo; }
set { _launchInfo = value; OnPropertyChanged("LaunchInfo"); OnPropertyChanged("IsLaunching"); OnPropertyChanged("IsNotLaunching"); }
}

public Profile LaunchedProfile { get; set; }

public void LaunchProfile(Profile p)
{
if (!IsDownloading)
if (!IsLaunching)
{
LaunchedProfile = p;
p.UpdateLaunchStatus();
if (!p.Version.IsInstalled)
{
Task.Run(async () =>
{
Download(p);
await Download(p);
Launch(p);
});
}
Expand Down Expand Up @@ -193,16 +196,14 @@ private async Task ReRegisterPackage(string gameDir)
RestoreMinecraftDataFromReinstall();
}

private async void Download(Profile p)
private async Task Download(Profile p)
{
LaunchedProfile = p;
p.UpdateLaunchStatus();
MinecraftVersion v = p.Version;
if (v.Beta)
return;

CancellationTokenSource cancelSource = new CancellationTokenSource();
DownloadInfo = new VersionDownloadInfo
LaunchInfo = new ProfileLaunchInfo()
{
IsInitializing = true,
CancelCommand = new RelayCommand((o) => cancelSource.Cancel())
Expand All @@ -224,14 +225,14 @@ private async void Download(Profile p)
{
await downloader.Download(v.UUID, "1", dlPath, (current, total) =>
{
if (DownloadInfo.IsInitializing)
if (LaunchInfo.IsInitializing)
{
Debug.WriteLine("Actual download started");
DownloadInfo.IsInitializing = false;
LaunchInfo.IsInitializing = false;
if (total.HasValue)
DownloadInfo.TotalSize = total.Value;
LaunchInfo.TotalSize = total.Value;
}
DownloadInfo.DownloadedBytes = current;
LaunchInfo.DownloadedBytes = current;
}, cancelSource.Token);
Debug.WriteLine("Download complete");
}
Expand All @@ -240,30 +241,82 @@ await downloader.Download(v.UUID, "1", dlPath, (current, total) =>
Debug.WriteLine("Download failed:\n" + e.ToString());
if (!(e is TaskCanceledException))
MessageBox.Show("Download failed:\n" + e.ToString());
DownloadInfo = null;
LaunchInfo = null;
return;
}
try
{
DownloadInfo.IsExtracting = true;
LaunchInfo.IsExtracting = true;
string dirPath = v.GameDirectory;
if (Directory.Exists(dirPath))
Directory.Delete(dirPath, true);
ZipFile.ExtractToDirectory(dlPath, dirPath);
DownloadInfo = null;
LaunchInfo = null;
File.Delete(Path.Combine(dirPath, "AppxSignature.p7x"));
}
catch (Exception e)
{
Debug.WriteLine("Extraction failed:\n" + e.ToString());
MessageBox.Show("Extraction failed:\n" + e.ToString());
DownloadInfo = null;
LaunchInfo = null;
return;
}
DownloadInfo = null;
LaunchInfo = null;
LaunchedProfile = null;
v.UpdateInstallStatus();
p.UpdateLaunchStatus();
}

public class ProfileLaunchInfo : NotifyPropertyChangedBase
{

private bool _isInitializing;
private bool _isExtracting;
private long _downloadedBytes;
private long _totalSize;

public bool IsInitializing
{
get { return _isInitializing; }
set { _isInitializing = value; OnPropertyChanged("IsProgressIndeterminate"); OnPropertyChanged("DisplayStatus"); }
}

public bool IsExtracting
{
get { return _isExtracting; }
set { _isExtracting = value; OnPropertyChanged("IsProgressIndeterminate"); OnPropertyChanged("DisplayStatus"); }
}

public bool IsProgressIndeterminate
{
get { return IsInitializing || IsExtracting; }
}

public long DownloadedBytes
{
get { return _downloadedBytes; }
set { _downloadedBytes = value; OnPropertyChanged("DownloadedBytes"); OnPropertyChanged("DisplayStatus"); }
}

public long TotalSize
{
get { return _totalSize; }
set { _totalSize = value; OnPropertyChanged("TotalSize"); OnPropertyChanged("DisplayStatus"); }
}

public string DisplayStatus
{
get
{
if (IsInitializing)
return "Preparing...";
if (IsExtracting)
return "Extracting...";
return "Downloading " + ((double)DownloadedBytes / 1024 / 1024).ToString("N2") + " MB / " + ((double)TotalSize / 1024 / 1024).ToString("N2") + " MB";
}
}

public ICommand CancelCommand { get; set; }
}
}
}
56 changes: 0 additions & 56 deletions ZenovaLauncher/Profiles/VersionDownloadInfo.cs

This file was deleted.

1 change: 0 additions & 1 deletion ZenovaLauncher/ZenovaLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
<DependentUpon>AddProfileDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Profiles\ProfileLauncher.cs" />
<Compile Include="Profiles\VersionDownloadInfo.cs" />
<Compile Include="Utils\NotifyPropertyChangedBase.cs" />
<Compile Include="Pages\ModsPage.xaml.cs">
<DependentUpon>ModsPage.xaml</DependentUpon>
Expand Down

0 comments on commit b358043

Please sign in to comment.