public void DeleteGameDirectory(ManagedGameVersion game, IProgress <int> progress)
        {
            progress.Report(10);
            if (Directory.Exists(game.Path))
            {
                Directory.Delete(game.Path, true);
            }

            RootDirectoryEntity        root          = new RootDirectoryEntityIO().Deserialize(game.GameXML);
            List <RootDirectoryEntity> otherVersions = new List <RootDirectoryEntity>();

            foreach (var mgv in _mvs.GetManagedVersions())
            {
                RootDirectoryEntity data = new RootDirectoryEntityIO().Deserialize(mgv.GameXML);
                otherVersions.Add(data);
            }

            Progress <int> partialProgress = new Progress <int>(prog =>
            {
                int percent = (int)(prog * 0.9);
                progress.Report(10 + percent);
            });

            string container = Settings.Default.ContainerDirectory;

            GameFilesRemover.RemoveFiles(root, otherVersions, Helpers.EntityToPath(container), _dirCache, partialProgress);
            SaveDirCache();

            System.Windows.Application.Current.Dispatcher.BeginInvoke((Action) delegate()
            {
                _mvs.Remove(game);
            });
            progress.Report(100);
        }
        public void UpdateGameDirectory(RootDirectoryEntity updated, string outputDir, IProgress <int> progress)
        {
            ManagedGameVersion   oldMGV        = _mvs.GetManagedVersions().First(mvg => mvg.Version == updated.Version);
            RootDirectoryEntity  oldBuild      = new RootDirectoryEntityIO().Deserialize(oldMGV.GameXML);
            HashSet <FileEntity> oldFiles      = oldBuild.GetAllFileEntities(true).OfType <FileEntity>().ToHashSet();
            HashSet <FileEntity> newFiles      = updated.GetAllFileEntities(true).OfType <FileEntity>().ToHashSet();
            HashSet <FileEntity> changedFiles  = newFiles.Except(oldFiles).ToHashSet();
            HashSet <FileEntity> filesToRemove = oldFiles.Except(newFiles).ToHashSet();

            // Do not remove files that only changed location
            HashSet <string>     newFilesHashes  = newFiles.Select(x => x.Hash).ToHashSet();
            HashSet <FileEntity> movedFileHashes = filesToRemove.Where(f => newFilesHashes.Contains(f.Hash)).ToHashSet();

            filesToRemove.ExceptWith(movedFileHashes);

            string container = Settings.Default.ContainerDirectory;
            List <RootDirectoryEntity> otherVersions = new List <RootDirectoryEntity>();

            foreach (var mgv in _mvs.GetManagedVersions().Except(new List <ManagedGameVersion>()
            {
                oldMGV
            }))
            {
                RootDirectoryEntity data = new RootDirectoryEntityIO().Deserialize(mgv.GameXML);
                otherVersions.Add(data);
            }

            int            basePercent     = 0;
            Progress <int> partialProgress = new Progress <int>(percent =>
            {
                progress.Report(basePercent + percent / 2);
            });

            // Remove old unused files from cache
            GameFilesRemover.RemoveFiles(filesToRemove,
                                         otherVersions,
                                         Helpers.EntityToPath(container),
                                         _dirCache,
                                         partialProgress
                                         );
            SaveDirCache();
            basePercent = 50;
            // Remove old unused files from game directory
            foreach (FileEntity unusedFile in filesToRemove)
            {
                string path = Path.Combine(oldMGV.Path, unusedFile.RelativePath);
                try
                {
                    File.Delete(path);
                }
                catch (Exception ex) { }
            }

            // Update files in directory
            CreateGameDirectory(updated, oldMGV.Path, partialProgress);
            progress.Report(100);
        }
        private async void RemoveGameFiles()
        {
            string             btnContent = btnRemove.Content as string;
            ManagedGameVersion selected   = lbVersions.SelectedItem as ManagedGameVersion;

            if (selected == null)
            {
                return;
            }

            _containerPath = txtContainer.Text;
            if (_containerPath == null || !Directory.Exists(_containerPath))
            {
                MessageBox.Show("Please select container directory.", "Error");
                return;
            }

            if (!LoadDirCache())
            {
                return;
            }

            MessageBoxResult result = MessageBox.Show("This process will remove ALL files from the selected version and delete the game directory. Proceed?", "Warning", MessageBoxButton.YesNo);

            if (result != MessageBoxResult.Yes)
            {
                return;
            }

            btnRemove.IsEnabled = false;
            btnRemove.Content   = "Removing...";

            if (Directory.Exists(selected.Path))
            {
                await Task.Run(() => Directory.Delete(selected.Path, true));
            }

            RootDirectoryEntity        root          = new RootDirectoryEntityIO().Deserialize(selected.GameXML);
            List <RootDirectoryEntity> otherVersions = new List <RootDirectoryEntity>();

            foreach (var mgv in ManagedVersions)
            {
                RootDirectoryEntity data = new RootDirectoryEntityIO().Deserialize(mgv.GameXML);
                otherVersions.Add(data);
            }

            await Task.Run(() => GameFilesRemover.RemoveFiles(root, otherVersions, _containerPath, Helpers.EntityToPath(_containerPath), _dirCache, null));

            ManagedVersions.Remove(selected);
            SaveManagedVersions();
            SaveDirCache();

            btnRemove.IsEnabled = true;
            btnRemove.Content   = btnContent;
        }