Commit 8a5d8daa authored by RedArcaneArcher's avatar RedArcaneArcher

cancel button

parent 031497ad
......@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using System.Threading;
using ReactiveUI;
namespace SunriseLauncher.Models
......@@ -113,6 +114,9 @@ namespace SunriseLauncher.Models
}
}
[JsonIgnore]
public CancellationTokenSource CancellationTokenSource { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
......
......@@ -23,6 +23,23 @@ namespace SunriseLauncher.Models
}
}
public class StateConverterUpdating : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is null)
return false;
var state = (State)value;
return state == State.Updating;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class StateConverterNotUpdating : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
......@@ -31,7 +48,7 @@ namespace SunriseLauncher.Models
return false;
var state = (State)value;
return state != State.Updating && state != State.Unchecked;
return state != State.Updating;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
......
......@@ -27,6 +27,7 @@ namespace SunriseLauncher.Services
server.ProgressDesc = "retrieving manfiest";
server.ProgressValue = 0;
server.ProgressMax = 0;
server.CancellationTokenSource = new CancellationTokenSource();
var manifest = MainfestFactory.Get(server.ManifestURL);
if (manifest == null)
......@@ -93,6 +94,8 @@ namespace SunriseLauncher.Services
await semaphore.WaitAsync();
try
{
var token = server.CancellationTokenSource.Token;
var files = await manifest.GetFilesAsync();
if (files == null)
{
......@@ -103,6 +106,12 @@ namespace SunriseLauncher.Services
server.ProgressMax = files.Count;
foreach (var file in files)
{
if (token.IsCancellationRequested)
{
server.State = State.Error;
return new UpdateResult(false, null);
}
server.ProgressValue++;
if (!file.Verify())
......@@ -111,10 +120,11 @@ namespace SunriseLauncher.Services
return new UpdateResult(false, "Manifest file failed inspection " + file.Path);
}
if (!await Updatefile(file, server))
var result = await Updatefile(file, server);
if (!result.Success)
{
server.State = State.Error;
return new UpdateResult(false, "Could not update file " + file.Path);
return result;
}
}
}
......@@ -134,15 +144,16 @@ namespace SunriseLauncher.Services
return new UpdateResult(true, null);
}
private async Task<bool> Updatefile(ManifestFile file, Server server)
private async Task<UpdateResult> Updatefile(ManifestFile file, Server server)
{
if (await Checkfile(file, server))
return true;
return new UpdateResult(true, null);
var path = Path.Combine(server.InstallPath, file.Path);
var tempfile = path + "~";
Console.WriteLine("downloading {0}", path);
var token = server.CancellationTokenSource.Token;
Console.WriteLine("downloading {0}", path);
Shuffler.Shuffle(file.Sources);
foreach (var source in file.Sources)
{
......@@ -154,7 +165,8 @@ namespace SunriseLauncher.Services
using (var hash = Hashing.GetHashAlgorithm(file))
{
if (hash == null) return false;
if (hash == null)
return new UpdateResult(false, "hash algorithm missing for " + file.Path);
var dirname = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(dirname)) Directory.CreateDirectory(dirname);
......@@ -168,7 +180,7 @@ namespace SunriseLauncher.Services
using (var hashstream = new CryptoStream(filestream, hash, CryptoStreamMode.Write))
using (var reader = await response.Content.ReadAsStreamAsync())
{
size = await CopyToProgressFileAsync(reader, hashstream, 81920, server, CancellationToken.None);
size = await CopyToProgressFileAsync(reader, hashstream, 81920, server, token);
hashstream.FlushFinalBlock();
checksum = hash.Hash;
}
......@@ -176,7 +188,7 @@ namespace SunriseLauncher.Services
if (size == file.Size && Hashing.VerifyChecksum(checksum, file))
{
File.Move(tempfile, path, true);
return true;
return new UpdateResult(true, null);
}
else
{
......@@ -190,6 +202,16 @@ namespace SunriseLauncher.Services
}
}
}
catch (OperationCanceledException ex)
{
if (ex.CancellationToken == token)
{
Console.WriteLine("update stopped due to cancellation request");
return new UpdateResult(false, "");
}
Console.WriteLine("OperationCanceledException while downloading source {0}: {1}", source.URL, ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("exception while downloading source {0}: {1}", source.URL, ex.Message);
......@@ -205,7 +227,7 @@ namespace SunriseLauncher.Services
server.ProgressMaxFile = 0;
}
}
return false;
return new UpdateResult(false, "Could not update file " + file.Path);
}
private async Task<bool> Checkfile(ManifestFile file, Server server)
......
......@@ -4,6 +4,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon>sunrise.ico</ApplicationIcon>
<AssemblyName>Sunrise</AssemblyName>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>true</RunAnalyzersDuringLiveAnalysis>
</PropertyGroup>
<ItemGroup>
<Compile Update="**\*.xaml.cs">
......@@ -12,7 +14,10 @@
<AvaloniaResource Include="**\*.xaml">
<SubType>Designer</SubType>
</AvaloniaResource>
<AvaloniaResource Include="Assets\**" />
<AvaloniaResource Include="Assets\*" />
</ItemGroup>
<ItemGroup>
<Resource Include="Fonts\Mont.ttf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.9.0" />
......
......@@ -85,7 +85,7 @@ namespace SunriseLauncher.ViewModels
SelectedItem = server;
var updateResult = await fileUpdater.UpdateAsync(server, true);
if (!updateResult.Success)
if (!updateResult.Success && !string.IsNullOrEmpty(updateResult.Message))
{
var msgbox = new MessageBoxView(updateResult.Message, "See log.txt for details.", false);
await msgbox.ShowDialog(Window);
......@@ -122,7 +122,7 @@ namespace SunriseLauncher.ViewModels
server.Metadata = vm.Metadata;
var updateResult = await fileUpdater.UpdateAsync(server, false);
if (!updateResult.Success)
if (!updateResult.Success && !string.IsNullOrEmpty(updateResult.Message))
{
var msgbox = new MessageBoxView(updateResult.Message, "See log.txt for details.", false);
await msgbox.ShowDialog(Window);
......@@ -141,6 +141,14 @@ namespace SunriseLauncher.ViewModels
}
}
public async void CancelUpdate()
{
if (SelectedItem == null)
return;
SelectedItem.CancellationTokenSource.Cancel();
}
public async void RefreshServers()
{
foreach (var server in Items.Where(x => x.State != State.Updating))
......@@ -152,7 +160,7 @@ namespace SunriseLauncher.ViewModels
foreach (var server in Items.Where(x => x.State == State.Unchecked))
{
var updateResult = await fileUpdater.UpdateAsync(server, false);
if (!updateResult.Success)
if (!updateResult.Success && !string.IsNullOrEmpty(updateResult.Message))
{
var msgbox = new MessageBoxView(updateResult.Message, "See log.txt for details.", false);
await msgbox.ShowDialog(Window);
......@@ -166,7 +174,7 @@ namespace SunriseLauncher.ViewModels
return;
var updateResult = await fileUpdater.UpdateAsync(SelectedItem, true);
if (!updateResult.Success)
if (!updateResult.Success && !string.IsNullOrEmpty(updateResult.Message))
{
var msgbox = new MessageBoxView(updateResult.Message, "See log.txt for details.", false);
await msgbox.ShowDialog(Window);
......
......@@ -8,7 +8,7 @@
x:Class="SunriseLauncher.Views.MainWindow"
Icon="/Assets/sunrise.ico"
Title="Sunrise Launcher v0.0.2"
Width="640" Height="360" MaxHeight="800" Background="Black">
Width="640" Height="360" MaxHeight="800">
<Window.Background>
<ImageBrush Source="/Assets/background_top.png" AlignmentY="Top" DestinationRect="0,0,611,800" TileMode="Tile" Stretch="None" SourceRect="0,0,611,175"/>
</Window.Background>
......
......@@ -9,6 +9,7 @@
<UserControl.Resources>
<models:StateConverterReady x:Key="StateConverterReady" />
<models:StateConverterNotUpdating x:Key="StateConverterNotUpdating" />
<models:StateConverterUpdating x:Key="StateConverterUpdating" />
</UserControl.Resources>
<Grid RowDefinitions="*,48" ColumnDefinitions="auto,*,auto,auto" Margin="4">
......@@ -44,7 +45,8 @@
CommandParameter="{Binding}">
<Image Stretch="None" Source="/Assets/remove_server.png"/>
</Button>
<TextBlock Foreground="White" Text="{Binding Launch}" FontFamily="Corbel,Arial" FontWeight="SemiBold" FontSize="20" VerticalAlignment="Center" Grid.Column="2" Margin="6"/>
<TextBlock Foreground="White" Text="{Binding Launch}" FontFamily="Corbel,Arial" FontSize="20" VerticalAlignment="Center" Grid.Column="2" Margin="6"/>
<!--Corbel,Arial-->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
......@@ -66,11 +68,17 @@
<Image Source="/Assets/refresh.png" Stretch="None" />
</Button>
<StackPanel DataContext="{Binding SelectedItem}" IsVisible="{Binding $self.DataContext, Converter={x:Static ObjectConverters.IsNotNull}}" Grid.Row="1" Grid.Column="1" Margin="4,2,4,2" VerticalAlignment="Center">
<TextBlock Foreground="White" HorizontalAlignment="Left" FontFamily="Corbel,Trebuchet MS,Arial" Text="{Binding ProgressDesc}" />
<ProgressBar Classes="Files" IsVisible="{Binding ProgressMaxFile}" Minimum="0" Maximum="{Binding ProgressMaxFile, Mode=OneWay}" Value="{Binding ProgressValueFile, Mode=OneWay}" Margin="1" />
<ProgressBar Classes="Files" IsVisible="{Binding ProgressDesc, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" IsIndeterminate="{Binding !ProgressMax, Mode=OneWay}" Minimum="0" Maximum="{Binding ProgressMax, Mode=OneWay}" Value="{Binding ProgressValue, Mode=OneWay}" Margin="1" />
</StackPanel>
<Grid ColumnDefinitions="*,34" Grid.Row="1" Grid.Column="1" IsVisible="{Binding SelectedItem, Converter={x:Static ObjectConverters.IsNotNull}}" Margin="2" >
<StackPanel DataContext="{Binding SelectedItem}" VerticalAlignment="Center">
<TextBlock Foreground="White" HorizontalAlignment="Left" FontFamily="Corbel,Trebuchet MS,Arial" Text="{Binding ProgressDesc}" />
<ProgressBar Classes="Files" IsVisible="{Binding ProgressMaxFile}" Minimum="0" Maximum="{Binding ProgressMaxFile, Mode=OneWay}" Value="{Binding ProgressValueFile, Mode=OneWay}" Margin="1" />
<ProgressBar Classes="Files" IsVisible="{Binding ProgressDesc, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" IsIndeterminate="{Binding !ProgressMax, Mode=OneWay}" Minimum="0" Maximum="{Binding ProgressMax, Mode=OneWay}" Value="{Binding ProgressValue, Mode=OneWay}" Margin="1" />
</StackPanel>
<Button Classes="Image" Grid.Column="1" Command="{Binding CancelUpdate}" IsVisible="{Binding SelectedItem.State, Converter={StaticResource StateConverterUpdating}}" >
<Image Stretch="None" Source="/Assets/cancel.png"/>
</Button>
</Grid>
<Button Classes="Image" IsEnabled="{Binding SelectedItem.State, Converter={StaticResource StateConverterNotUpdating}}" Grid.Row="1" Grid.Column="2" Margin="4" Command="{Binding VerifyFiles}" >
<Image Source="/Assets/verify_files.png" Stretch="None" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment