private void ExtractPbp(string srcPbp, ProcessOptions processOptions, CancellationToken cancellationToken) { var info = new ExtractOptions() { SourcePbp = srcPbp, OutputPath = processOptions.OutputPath, DiscName = "- Disc {0}", Discs = processOptions.Discs, CreateCuesheet = true, CheckIfFileExists = processOptions.CheckIfFileExists, FileNameFormat = processOptions.FileNameFormat, GetGameInfo = (gameId) => { var game = GetGameEntry(gameId, srcPbp, false); if (game == null) { return(null); } return(new GameInfo() { GameID = game.ScannerID, GameName = game.GameName, Title = game.SaveDescription, MainGameID = game.SaveFolderName, Region = game.Format }); } }; var popstation = new Popstation.Popstation { ActionIfFileExists = _eventHandler.ActionIfFileExists, Notify = _notifier.Notify, TempFiles = tempFiles }; popstation.Extract(info, cancellationToken); }
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); }
private static void ProcessFiles(ProcessOptions options) { var eventHandler = new EventHandler(); var notifier = new AggregateNotifier(); var consoleNotifer = new ConsoleNotifier(options.Verbosity); notifier.Add(consoleNotifer); if (options.Log) { var logNotifier = new LogNotifier(DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".log"); notifier.Add(logNotifier); } var gameDb = new GameDB(Path.Combine(ApplicationInfo.AppPath, "Resources", "gameInfo.db")); var processing = new Processing(notifier, eventHandler, gameDb); notifier.Notify(PopstationEventEnum.ProcessingStart, null); if (options.Files.Count == 0) { notifier.Notify(PopstationEventEnum.Error, "No files matched!"); } else if (options.Files.Count > 1) { notifier.Notify(PopstationEventEnum.Info, $"Matched {options.Files.Count} files"); var i = 1; var processed = 0; try { foreach (var file in options.Files) { if (!File.Exists(file)) { notifier.Notify(PopstationEventEnum.Error, $"Could not find file '{file}'"); continue; } notifier.Notify(PopstationEventEnum.Info, $"Processing {i} of {options.Files.Count}"); notifier.Notify(PopstationEventEnum.FileName, $"Processing {file}"); var result = processing.ProcessFile(file, options, _cancellationTokenSource.Token); if (_cancellationTokenSource.Token.IsCancellationRequested || eventHandler.Cancelled) { break; } processed += result ? 1 : 0; i++; } } finally { notifier.Notify(PopstationEventEnum.Info, $"{i - 1} files processed"); } } else { var file = options.Files[0]; if (!File.Exists(file)) { notifier.Notify(PopstationEventEnum.Error, $"Could not find file '{file}'"); return; } notifier.Notify(PopstationEventEnum.FileName, $"Processing {file}"); processing.ProcessFile(file, options, _cancellationTokenSource.Token); } notifier.Notify(PopstationEventEnum.ProcessingComplete, null); }
static void Main(string[] args) { SevenZip.SevenZipBase.SetLibraryPath(Path.Combine(ApplicationInfo.AppPath, $"{(System.Environment.Is64BitOperatingSystem ? "x64" : "x86")}/7z.dll")); _cancellationTokenSource = new CancellationTokenSource(); Console.CancelKeyPress += CancelEventHandler; var tempPath = Path.Combine(Path.GetTempPath(), "PSXPackager"); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { Console.WriteLine($"PSXPackager v1.4.1 by RupertAvery\r\n"); if (o.CompressionLevel < 0 || o.CompressionLevel > 9) { Console.WriteLine($"Invalid compression level, please enter a value from 0 to 9"); return; } if (!string.IsNullOrEmpty(o.Discs)) { if (!Regex.IsMatch(o.Discs, "\\d(,\\d)*")) { Console.WriteLine($"Invalid discs specification, please enter a comma separated list of values from 1-5"); return; } } if (!string.IsNullOrEmpty(o.InputPath)) { Console.WriteLine($"Input : {o.InputPath}"); } if (string.IsNullOrEmpty(o.OutputPath)) { if (!string.IsNullOrEmpty(o.InputPath)) { o.OutputPath = Path.GetDirectoryName(o.InputPath); } } Console.WriteLine($"Output: {o.OutputPath}"); Console.WriteLine($"Compression Level: {o.CompressionLevel}"); if (o.OverwriteIfExists) { Console.WriteLine("WARNING: You have chosen to overwrite all files in the output directory!"); } Console.WriteLine(); var files = new List <string>(); if (!string.IsNullOrEmpty(o.InputPath)) { if (PathIsDirectory(o.InputPath)) { files.AddRange(GetFilesFromDirectory(o.InputPath, null, o.Recursive)); } else { var filename = Path.GetFileName(o.InputPath); var path = Path.GetDirectoryName(o.InputPath); if (!string.IsNullOrEmpty(path) && PathIsDirectory(path) && ContainsWildCards(filename)) { files.AddRange(GetFilesFromDirectory(path, filename, o.Recursive)); } else { if (ContainsWildCards(filename)) { files.AddRange(GetFilesFromDirectory(".", filename, o.Recursive)); } else { files.Add(o.InputPath); } } } } var discs = string.IsNullOrEmpty(o.Discs) ? Enumerable.Range(1, 5).ToList() : o.Discs.Split(new char[] { ',' }).Select(int.Parse).ToList(); var options = new ProcessOptions() { Files = files, OutputPath = o.OutputPath, TempPath = tempPath, Discs = discs, CheckIfFileExists = !o.OverwriteIfExists, SkipIfFileExists = o.SkipIfExists, FileNameFormat = o.FileNameFormat, CompressionLevel = o.CompressionLevel, Verbosity = o.Verbosity, Log = o.Log, ExtractResources = o.ExtractResources != null, ImportResources = o.ImportResources != null, GenerateResourceFolders = o.GenerateResourceFolders != null, CustomResourceFormat = o.ImportResources ?? o.ExtractResources ?? o.GenerateResourceFolders, ResourceFoldersPath = o.ResourceFoldersPath, }; ProcessFiles(options); }); }
static void Main(string[] args) { SevenZip.SevenZipBase.SetLibraryPath("x64/7z.dll"); _cancellationTokenSource = new CancellationTokenSource(); Console.CancelKeyPress += CancelEventHandler; var tempPath = Path.Combine(Path.GetTempPath(), "PSXPackager"); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { Console.WriteLine($"PSXPackager v1.4.1 by RupertAvery\r\n"); if (o.CompressionLevel < 0 || o.CompressionLevel > 9) { Console.WriteLine($"Invalid compression level, please enter a value from 0 to 9"); return; } if (!string.IsNullOrEmpty(o.Discs)) { if (!Regex.IsMatch(o.Discs, "\\d(,\\d)*")) { Console.WriteLine($"Invalid discs specification, please enter a comma separated list of values from 1-5"); return; } } if (!string.IsNullOrEmpty(o.InputPath)) { Console.WriteLine($"Input : {o.InputPath}"); } else if (!string.IsNullOrEmpty(o.Batch)) { Console.WriteLine($"Batch : {o.Batch}"); Console.WriteLine($"Extension: {o.Filters}"); } if (string.IsNullOrEmpty(o.OutputPath)) { if (!string.IsNullOrEmpty(o.InputPath)) { o.OutputPath = Path.GetDirectoryName(o.InputPath); } else if (!string.IsNullOrEmpty(o.Batch)) { o.OutputPath = o.Batch; } } Console.WriteLine($"Output: {o.OutputPath}"); Console.WriteLine($"Compression Level: {o.CompressionLevel}"); if (o.OverwriteIfExists) { Console.WriteLine("WARNING: You have chosen to overwrite all files in the output directory!"); } Console.WriteLine(); var files = new List <string>(); if (!string.IsNullOrEmpty(o.InputPath)) { if (PathIsDirectory(o.InputPath)) { files.AddRange(GetFilesFromDirectory(o.InputPath, o.Filters)); } else { var filename = Path.GetFileName(o.InputPath); var path = Path.GetDirectoryName(o.InputPath); if (!string.IsNullOrEmpty(path) && PathIsDirectory(path) && ContainsWildCards(filename)) { files.AddRange(GetFilesFromDirectory(path, filename)); } else { if (ContainsWildCards(filename)) { files.AddRange(GetFilesFromDirectory(".", filename)); } else { files.Add(o.InputPath); } } } } else if (!string.IsNullOrEmpty(o.Batch)) { Console.WriteLine($"WARNING: Use of -b is deprecated. Use -i with wildcards instead."); files.AddRange(GetFilesFromDirectory(o.InputPath, o.Filters)); } var discs = string.IsNullOrEmpty(o.Discs) ? Enumerable.Range(1, 5).ToList() : o.Discs.Split(new char[] { ',' }).Select(int.Parse).ToList(); var options = new ProcessOptions() { Files = files, OutputPath = o.OutputPath, TempPath = tempPath, Discs = discs, CheckIfFileExists = !o.OverwriteIfExists, FileNameFormat = o.FileNameFormat, CompressionLevel = o.CompressionLevel, Verbosity = o.Verbosity, Log = o.Log }; ProcessFiles(options); }); }