Esempio n. 1
0
        public bool ProcessFile(
            string file,
            ProcessOptions options,
            CancellationToken cancellationToken)
        {
            bool result = true;


            if (_eventHandler.Cancelled)
            {
                return(false);
            }

            options.CheckIfFileExists = !_eventHandler.OverwriteIfExists && options.CheckIfFileExists;

            tempFiles.Clear();

            try
            {
                if (FileExtensionHelper.IsArchive(file))
                {
                    Unpack(file, options.TempPath, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(false);
                    }

                    file = "";

                    if (tempFiles.Count(FileExtensionHelper.IsImageFile) == 0)
                    {
                        _notifier?.Notify(PopstationEventEnum.Error, "No image files found!");
                        return(false);
                    }
                    else if (tempFiles.Count(FileExtensionHelper.IsImageFile) == 1)
                    {
                        var cue = tempFiles.FirstOrDefault(FileExtensionHelper.IsCue);
                        if (cue != null)
                        {
                            file = cue;
                        }
                        else
                        {
                            file = tempFiles.FirstOrDefault(FileExtensionHelper.IsImageFile);
                        }
                    }
                    else if (tempFiles.Count(FileExtensionHelper.IsBin) > 1)
                    {
                        _notifier?.Notify(PopstationEventEnum.Info, $"Multi-bin image was found!");

                        var cue = tempFiles.FirstOrDefault(FileExtensionHelper.IsCue);
                        if (cue != null)
                        {
                            file = cue;
                        }
                        else
                        {
                            _notifier?.Notify(PopstationEventEnum.Warning, $"No cue sheet found!");
                            return(false);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(file))
                {
                    if (FileExtensionHelper.IsPbp(file))
                    {
                        ExtractPbp(file, options, cancellationToken);
                    }
                    else
                    {
                        if (FileExtensionHelper.IsCue(file))
                        {
                            var(outfile, srcToc) = ProcessCue(file, options.TempPath);
                            result = ConvertIso(outfile, srcToc, options, cancellationToken);
                        }
                        else if (FileExtensionHelper.IsM3u(file))
                        {
                            var filePath = Path.GetDirectoryName(file);
                            var files    = new List <string>();
                            var tocs     = new List <string>();
                            var m3UFile  = M3uFileReader.Read(file);

                            if (m3UFile.FileEntries.Count == 0)
                            {
                                _notifier?.Notify(PopstationEventEnum.Error, $"Invalid number of entries, found {m3UFile.FileEntries.Count}");
                                return(false);
                            }
                            else if (m3UFile.FileEntries.Count > 5)
                            {
                                _notifier?.Notify(PopstationEventEnum.Error, $"Invalid number of entries, found {m3UFile.FileEntries.Count}, max is 5");
                                return(false);
                            }

                            _notifier?.Notify(PopstationEventEnum.Info, $"Found {m3UFile.FileEntries.Count} entries");

                            foreach (var fileEntry in m3UFile.FileEntries)
                            {
                                if (FileExtensionHelper.IsCue(fileEntry))
                                {
                                    var(outfile, srcToc) = ProcessCue(Path.Combine(filePath, fileEntry), options.TempPath);
                                    files.Add(outfile);
                                    tocs.Add(srcToc);
                                }
                                else if (FileExtensionHelper.IsImageFile(fileEntry))
                                {
                                    files.Add(Path.Combine(filePath, fileEntry));
                                }
                                else
                                {
                                    _notifier?.Notify(PopstationEventEnum.Warning, $"Unsupported playlist entry '{fileEntry}'");
                                    _notifier?.Notify(PopstationEventEnum.Warning, "Only the following are supported: .cue .img .bin .iso");
                                    return(false);
                                }
                            }
                            result = ConvertIsos(files.ToArray(), tocs.ToArray(), options, cancellationToken);
                        }
                        else
                        {
                            result = ConvertIso(file, "", options, cancellationToken);
                        }
                    }
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    _notifier?.Notify(PopstationEventEnum.Warning, "Conversion cancelled");
                    return(false);
                }
            }
            catch (CancellationException ex)
            {
                _notifier?.Notify(PopstationEventEnum.Error, ex.Message);
                return(false);
            }
            catch (FileNotFoundException ex)
            {
                _notifier?.Notify(PopstationEventEnum.Error, ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                _notifier?.Notify(PopstationEventEnum.Error, ex.Message);
                return(false);
                //throw;
            }
            finally
            {
                if (tempFiles != null)
                {
                    foreach (var tempFile in tempFiles)
                    {
                        if (File.Exists(tempFile))
                        {
                            File.Delete(tempFile);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        private void Scan(object obj)
        {
            if (_model.IsScanning)
            {
                var result = MessageBox.Show(Window, "Abort scanning?", "Batch", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                if (result == MessageBoxResult.Yes)
                {
                    Cancel();
                }
                return;
            }

            if (string.IsNullOrEmpty(_model.Settings.InputPath))
            {
                MessageBox.Show(Window, "No input path specified", "Batch", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!Directory.Exists(_model.Settings.InputPath))
            {
                MessageBox.Show(Window, "Invalid directory or directory not found", "Batch", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var patterns = new List <string>();

            if (_model.ConvertImageToPbp || _model.GenerateResourceFolders)
            {
                if (_model.Settings.IsM3uChecked)
                {
                    patterns.Add("*.m3u");
                }
                if (_model.Settings.IsBinChecked)
                {
                    patterns.Add("*.cue");
                    patterns.Add("*.bin");
                }
                if (_model.Settings.IsImgChecked)
                {
                    patterns.Add("*.img");
                }
                if (_model.Settings.IsIsoChecked)
                {
                    patterns.Add("*.iso");
                }
                if (_model.Settings.Is7zChecked)
                {
                    patterns.Add("*.7z");
                }
                if (_model.Settings.IsZipChecked)
                {
                    patterns.Add("*.zip");
                }
                if (_model.Settings.IsRarChecked)
                {
                    patterns.Add("*.rar");
                }
            }


            if (_model.ConvertPbpToImage || _model.ExtractResources || _model.GenerateResourceFolders)
            {
                patterns.Add("*.pbp");
            }



            Task.Run(() =>
            {
                _dispatcher.Invoke(() =>
                {
                    _model.IsScanning = true;
                    _model.BatchEntries.Clear();
                });

                var ignoreFileSet = new HashSet <string>();

                string GetAbsolutePath(string currentDirectory, string file)
                {
                    if (Path.IsPathFullyQualified(file))
                    {
                        return(file);
                    }
                    return(Path.Combine(currentDirectory, file));
                }


                string GetFullPath(string file)
                {
                    return(Path.Combine(_model.Settings.InputPath, file));
                }

                foreach (var pattern in patterns)
                {
                    if (_token.IsCancellationRequested)
                    {
                        break;
                    }

                    var searchOption = SearchOption.TopDirectoryOnly;

                    if (_model.Settings.RecurseFolders)
                    {
                        searchOption = SearchOption.AllDirectories;
                    }

                    var files = Directory.EnumerateFiles(_model.Settings.InputPath, pattern, searchOption);

                    foreach (var file in files.Select(GetFullPath))
                    {
                        var currentPath = Path.GetDirectoryName(file);

                        if (_token.IsCancellationRequested)
                        {
                            break;
                        }

                        switch (pattern)
                        {
                        case "*.m3u":
                            {
                                var playlist = M3uFileReader.Read(file);
                                foreach (var fileEntry in playlist.FileEntries)
                                {
                                    ignoreFileSet.Add(GetAbsolutePath(currentPath, fileEntry));
                                }

                                break;
                            }

                        case "*.cue":
                            {
                                var cueFiles = CueFileReader.Read(file);
                                foreach (var fileEntry in cueFiles.FileEntries)
                                {
                                    ignoreFileSet.Add(GetAbsolutePath(currentPath, fileEntry.FileName));
                                }

                                break;
                            }
                        }

                        var relativePath = Path.GetRelativePath(_model.Settings.InputPath, file);

                        if (!ignoreFileSet.Contains(file))
                        {
                            _dispatcher.Invoke(() =>
                            {
                                _model.BatchEntries.Add(new BatchEntryModel()
                                {
                                    RelativePath = relativePath,
                                    MaxProgress  = 100,
                                    Progress     = 0,
                                    Status       = "Ready"
                                });
                            });
                        }
                    }
                }

                _dispatcher.Invoke(() =>
                {
                    _model.IsScanning = false;
                });

                if (_token.IsCancellationRequested)
                {
                    _dispatcher.Invoke(() =>
                    {
                        MessageBox.Show(Window, "Scan aborted!", "Batch", MessageBoxButton.OK, MessageBoxImage.Warning);
                    });
                }

                _dispatcher.Invoke(() =>
                {
                    MessageBox.Show(Window, $"Scan found {_model.BatchEntries.Count} entries", "Batch", MessageBoxButton.OK, MessageBoxImage.Information);
                });
            });
        }