Beispiel #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);
        }
        public static async Task ProcessFile(string file, string outPath, string tempPath, int compressionLevel, CancellationToken cancellationToken, string savedesc = "PS1 Game - Saved Data", string name = "PS1 Game", string pic0 = "PIC0.PNG", string pic1 = "PIC1.PNG", string icon0 = "ICON0.PNG", string basePbp = "BASE.PBP")
        {
            Console.WriteLine($"Converting {file}...");

            List <string> tempFiles = null;
            string        srcToc    = null;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);
            try
            {
                if (!string.IsNullOrEmpty(file))
                {
                    if (FileExtensionHelper.IsPbp(file))
                    {
                        await ExtractPbp(file, outPath, cancellationToken);
                    }
                    else
                    {
                        if (FileExtensionHelper.IsCue(file))
                        {
                            var filePath = Path.GetDirectoryName(file);

                            var cueFiles = CueReader.Read(file);
                            if (cueFiles.Count > 1)
                            {
                                var mergedBin = MergeBins(file, cueFiles, tempPath);
                                var cueFile   = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(mergedBin.Path) + ".cue");
                                CueWriter.Write(cueFile, mergedBin.CueFiles);
                                srcToc = cueFile;
                                file   = mergedBin.Path;

                                tempFiles.Add(mergedBin.Path);
                                tempFiles.Add(cueFile);
                            }
                            else
                            {
                                srcToc = file;
                                file   = Path.Combine(filePath, cueFiles.First().FileName);
                            }
                        }

                        await ConvertIso(file, srcToc, outPath, compressionLevel, cancellationToken, savedesc, name, pic0, pic1, icon0, basePbp);
                    }
                }
            }
            finally
            {
                Console.CursorVisible = true;
                if (cancellationToken.IsCancellationRequested)
                {
                    Console.WriteLine("Conversion cancelled");
                }
                else
                {
                    Console.WriteLine("Conversion completed!");
                }

                if (tempFiles != null)
                {
                    foreach (var tempFile in tempFiles)
                    {
                        System.IO.File.Delete(tempFile);
                    }
                }
            }
        }