Skip to content

Commit

Permalink
UI is no more blocking / slowing our thread, movement shall race with…
Browse files Browse the repository at this point in the history
… your hardware limits now.
  • Loading branch information
RevoLand committed Nov 21, 2016
1 parent 53c9328 commit 2274107
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 69 deletions.
Binary file modified Binaries/Steam Library Manager.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion Binaries/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.2.0|Fixes and improvements
1.3.3.0|Performance improvements & Movement form visual updates
39 changes: 17 additions & 22 deletions Source/Steam Library Manager/Definitions/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public List<FileSystemInfo> getFileList(bool includeDownloads = true, bool inclu

public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary, CancellationTokenSource cancellationToken, bool compressGame = false)
{
currentForm.formLogs.Add($"Populating file list, please wait");

ConcurrentBag<string> copiedFiles = new ConcurrentBag<string>();
ConcurrentBag<string> createdDirectories = new ConcurrentBag<string>();
List<FileSystemInfo> gameFiles = getFileList();
Expand All @@ -137,8 +139,7 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,

try
{
int totalMovenFileCount = 0;
long totalFileSize = 0, movenFileSize = 0;
long totalFileSize = 0;
ParallelOptions parallelOptions = new ParallelOptions()
{
CancellationToken = cancellationToken.Token
Expand All @@ -149,6 +150,8 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
Interlocked.Add(ref totalFileSize, (file as FileInfo).Length);
});

currentForm.formLogs.Add($"File list populated, total files to move: {gameFiles.Count}");

if (!IsCompressed && compressGame)
{
FileInfo compressedArchive = new FileInfo(compressedName.FullName.Replace(installedLibrary.steamAppsPath.FullName, targetLibrary.steamAppsPath.FullName));
Expand All @@ -162,14 +165,11 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,

foreach (FileSystemInfo currentFile in gameFiles)
{
totalMovenFileCount++;
movenFileSize += (currentFile as FileInfo).Length;

string newFileName = currentFile.FullName.Substring(installedLibrary.steamAppsPath.FullName.Length + 1);

compressed.CreateEntryFromFile(currentFile.FullName, newFileName, CompressionLevel.Optimal);

currentForm.reportFileMovement(newFileName, totalMovenFileCount, gameFiles.Count, movenFileSize, totalFileSize);
currentForm.reportFileMovement(newFileName, gameFiles.Count, (currentFile as FileInfo).Length, totalFileSize);

if (cancellationToken.IsCancellationRequested)
throw new OperationCanceledException(cancellationToken.Token);
Expand All @@ -182,9 +182,6 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
{
foreach (ZipArchiveEntry currentFile in ZipFile.OpenRead(compressedName.FullName).Entries)
{
totalMovenFileCount++;
movenFileSize += currentFile.Length;

FileInfo newFile = new FileInfo(Path.Combine(targetLibrary.steamAppsPath.FullName, currentFile.FullName));

if (!newFile.Directory.Exists)
Expand All @@ -196,7 +193,7 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
currentFile.ExtractToFile(newFile.FullName, true);
copiedFiles.Add(newFile.FullName);

currentForm.reportFileMovement(newFile.FullName, totalMovenFileCount, gameFiles.Count, movenFileSize, totalFileSize);
currentForm.reportFileMovement(newFile.FullName, gameFiles.Count, currentFile.Length, totalFileSize);

if (cancellationToken.IsCancellationRequested)
throw new OperationCanceledException(cancellationToken.Token);
Expand All @@ -219,11 +216,9 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
(currentFile as FileInfo).CopyTo(newFile.FullName, true);
}

Interlocked.Increment(ref totalMovenFileCount);
Interlocked.Add(ref movenFileSize, (currentFile as FileInfo).Length);
copiedFiles.Add(newFile.FullName);

currentForm.reportFileMovement(newFile.FullName, totalMovenFileCount, gameFiles.Count, movenFileSize, totalFileSize);
currentForm.reportFileMovement(newFile.FullName, gameFiles.Count, (currentFile as FileInfo).Length, totalFileSize);
});

parallelOptions.MaxDegreeOfParallelism = 1;
Expand All @@ -242,19 +237,19 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,

(currentFile as FileInfo).CopyTo(newFile.FullName, true);
}

Interlocked.Increment(ref totalMovenFileCount);
Interlocked.Add(ref movenFileSize, (currentFile as FileInfo).Length);
copiedFiles.Add(newFile.FullName);

currentForm.reportFileMovement(newFile.FullName, totalMovenFileCount, gameFiles.Count, movenFileSize, totalFileSize);
currentForm.reportFileMovement(newFile.FullName, gameFiles.Count, (currentFile as FileInfo).Length, totalFileSize);
});

if (!IsCompressed)
{
// Copy .ACF file
if (fullAcfPath.Exists)
{
fullAcfPath.CopyTo(Path.Combine(targetLibrary.steamAppsPath.FullName, acfName), true);
currentForm.reportFileMovement(Path.Combine(targetLibrary.steamAppsPath.FullName, acfName), gameFiles.Count, fullAcfPath.Length, totalFileSize);
}

if (workShopAcfPath.Exists)
{
Expand All @@ -267,6 +262,7 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
}

workShopAcfPath.CopyTo(newACFPath.FullName, true);
currentForm.reportFileMovement(newACFPath.FullName, gameFiles.Count, newACFPath.Length, totalFileSize);
}
}
}
Expand All @@ -280,19 +276,19 @@ public void copyGameFiles(Forms.MoveGameForm currentForm, Library targetLibrary,
}
catch (OperationCanceledException)
{
MessageBoxResult moveGamesBeforeDeletion = MessageBox.Show("Game movement cancelled. Would you like to remove files that already moven?", "Remove moven files?", MessageBoxButton.YesNo);
MessageBoxResult removeMovenFiles = MessageBox.Show("Game movement cancelled. Would you like to remove files that already moven?", "Remove moven files?", MessageBoxButton.YesNo);

if (moveGamesBeforeDeletion == MessageBoxResult.Yes)
if (removeMovenFiles == MessageBoxResult.Yes)
Functions.fileSystem.removeGivenFiles(copiedFiles, createdDirectories);

currentForm.formLogs.Add($"Operation cancelled by user. Time Elapsed: {timeElapsed.Elapsed}");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
MessageBoxResult moveGamesBeforeDeletion = MessageBox.Show("An error happened while moving game files. Would you like to remove files that already moven?", "Remove moven files?", MessageBoxButton.YesNo);
MessageBoxResult removeMovenFiles = MessageBox.Show("An error happened while moving game files. Would you like to remove files that already moven?", "Remove moven files?", MessageBoxButton.YesNo);

if (moveGamesBeforeDeletion == MessageBoxResult.Yes)
if (removeMovenFiles == MessageBoxResult.Yes)
Functions.fileSystem.removeGivenFiles(copiedFiles, createdDirectories);

currentForm.formLogs.Add($"An error happened while moving game files. Time Elapsed: {timeElapsed.Elapsed}");
Expand Down Expand Up @@ -344,7 +340,6 @@ public bool deleteFiles()
workShopAcfPath.Delete();
}


return true;
}
catch (Exception ex)
Expand Down
11 changes: 6 additions & 5 deletions Source/Steam Library Manager/Forms/MoveGameForm.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsCompressed}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="Content" Value="De-compress" />
<Setter Property="Content" Value="Keep Compressed" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
Expand All @@ -40,15 +40,16 @@

<Button x:Name="button" Content="Copy" Margin="10,319,10,0" VerticalAlignment="Top" Height="45" Click="button_Click"/>

<ListView x:Name="textBox" Margin="10,369,10,15" AllowDrop="False" IsTabStop="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<ListView x:Name="textBox" Margin="10,369,10,60" AllowDrop="False" IsTabStop="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock MaxHeight="16" Text="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

<!--<ProgressBar x:Name="progressReport" Margin="10,0,10,25" Height="30" VerticalAlignment="Bottom" />-->
<Label x:Name="progressReportLabel" Margin="0,0,10,0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
<Grid Name="progressReportGrid" Margin="10,539,10,10">
<ProgressBar x:Name="progressReport" Margin="0,0,0,24" Value="{Binding ProgressBar}" />
<Label x:Name="progressReportLabel" Content="{Binding ProgressLabel}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
</Grid>
</Grid>
</Window>
104 changes: 64 additions & 40 deletions Source/Steam Library Manager/Forms/MoveGameForm.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;

Expand All @@ -23,6 +24,61 @@ public partial class MoveGameForm : Window
// Define task
System.Threading.Tasks.Task currentTask;

ProgressReport pr = new ProgressReport();

class ProgressReport : INotifyPropertyChanged
{
int progressBar;
public int ProgressBar
{
get { return progressBar; }
set
{
progressBar = value;
NotifyPropertyChanged("ProgressBar");
}
}

int movenFileCount;
public int MovenFileCount
{
get { return movenFileCount; }
set
{
movenFileCount = value;
NotifyPropertyChanged("MovenFileCount");
}
}

long movenFileSize;
public long MovenFileSize
{
get { return movenFileSize; }
set
{
movenFileSize = value;
NotifyPropertyChanged("MovenFileSize");
}
}

string progressLabel;
public string ProgressLabel
{
get { return progressLabel; }
set
{
progressLabel = value;
NotifyPropertyChanged("ProgressLabel");
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public Framework.AsyncObservableCollection<string> formLogs = new Framework.AsyncObservableCollection<string>();

public MoveGameForm(Definitions.Game gameToMove, Definitions.Library libraryToMove)
Expand All @@ -38,6 +94,7 @@ public MoveGameForm(Definitions.Game gameToMove, Definitions.Library libraryToMo
private void moveGameForm_Loaded(object sender, RoutedEventArgs e)
{
DataContext = Game;
progressReportGrid.DataContext = pr;

targetLibraryText.Content = targetLibrary.fullPath;
}
Expand All @@ -55,48 +112,15 @@ private void moveGameForm_SourceInitialized(object sender, EventArgs e)
Framework.WindowPlacement.SetPlacement(this, Properties.Settings.Default.moveGameFormPlacement);
}

public void reportFileMovement(string movenFileName, int movenFileCount, int totalFileCount, long movenFileSize, long totalFileSize)
public void reportFileMovement(string movenFileName, int totalFileCount, long movenFileSize, long totalFileSize)
{
formLogs.Add(string.Format("[{0}/{1}] {2}\n", movenFileCount, totalFileCount, movenFileName));
pr.MovenFileCount += 1;
pr.MovenFileSize += movenFileSize;

/*
if (progressReportLabel.Dispatcher.CheckAccess())
{
progressReportLabel.Content = $"{Functions.fileSystem.FormatBytes(totalFileSize - movenFileSize)} left - {Functions.fileSystem.FormatBytes(movenFileSize)} / {Functions.fileSystem.FormatBytes(totalFileSize)}";
}
else
{
progressReportLabel.Dispatcher.Invoke(delegate
{
progressReportLabel.Content = $"{Functions.fileSystem.FormatBytes(totalFileSize - movenFileSize)} left - {Functions.fileSystem.FormatBytes(movenFileSize)} / {Functions.fileSystem.FormatBytes(totalFileSize)}";
}, System.Windows.Threading.DispatcherPriority.Normal);
}
if (progressReport.Dispatcher.CheckAccess())
{
progressReport.Value = ((int)Math.Round((double)(100 * movenFileSize) / totalFileSize));
}
else
{
progressReport.Dispatcher.Invoke(delegate
{
progressReport.Value = ((int)Math.Round((double)(100 * movenFileSize) / totalFileSize));
}, System.Windows.Threading.DispatcherPriority.Normal);
}
if (textBox.Dispatcher.CheckAccess())
{
textBox.ScrollIntoView(textBox.Items[textBox.Items.Count - 1]);
}
else
{
textBox.Dispatcher.Invoke(delegate
{
textBox.ScrollIntoView(textBox.Items[textBox.Items.Count - 1]);
}, System.Windows.Threading.DispatcherPriority.Normal);
}
*/
pr.ProgressBar = ((int)Math.Round((double)(100 * pr.MovenFileSize) / totalFileSize));
pr.ProgressLabel = $"{Functions.fileSystem.FormatBytes(totalFileSize - pr.MovenFileSize)} left - {Functions.fileSystem.FormatBytes(pr.MovenFileSize)} / {Functions.fileSystem.FormatBytes(totalFileSize)}";

formLogs.Add(string.Format("[{0}/{1}] {2}\n", pr.MovenFileCount, totalFileCount, movenFileName));
}

private void button_Click(object sender, RoutedEventArgs e)
Expand All @@ -120,7 +144,7 @@ private void button_Click(object sender, RoutedEventArgs e)
if (!cancellationToken.IsCancellationRequested)
{
// If game is not exists in the target library
if (targetLibrary.Games.Count(x => x.acfName == Game.acfName && compressGame != x.IsCompressed) == 0)
if (targetLibrary.Games.Count(x => x.acfName == Game.acfName && compressGame == x.IsCompressed) == 0)
{
// Add game to new library
Functions.Games.AddNewGame(Game.fullAcfPath.FullName.Replace(Game.installedLibrary.steamAppsPath.FullName, targetLibrary.steamAppsPath.FullName), Game.appID, Game.appName, Game.installationPath.Name, targetLibrary, Game.sizeOnDisk, compressGame);
Expand Down
3 changes: 3 additions & 0 deletions Source/Steam Library Manager/Functions/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public static string FormatBytes(long bytes)
dblSByte = bytes / 1024.0;
}

if (dblSByte < 0)
dblSByte = 0;

// Format the string
return string.Format("{0:0.##} {1}", dblSByte, Suffix[current]);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Steam Library Manager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.3.2.0")]
[assembly: AssemblyFileVersion("1.3.3.0")]

0 comments on commit 2274107

Please sign in to comment.