private async Task BatchInject(UPath inputDirectory, UPath outputDirectory, string pluginIdArgument) { if (!TryParseGuidArgument(pluginIdArgument, out var pluginId)) { return; } var sourceFileSystem = FileSystemFactory.CreateSubFileSystem(inputDirectory.FullName, new StreamManager()); var destinationFileSystem = FileSystemFactory.CreateSubFileSystem(outputDirectory.FullName, new StreamManager()); _batchInjector.ScanSubDirectories = true; _batchInjector.PluginId = pluginId; await _batchInjector.Process(sourceFileSystem, destinationFileSystem); }
private void ExtractAllImage(UPath directoryPath) { var destinationFileSystem = FileSystemFactory.CreateSubFileSystem(directoryPath.FullName, new StreamManager()); for (var i = 0; i < _imageState.Images.Count; i++) { var imageFileName = _imageState.Images[i].Name; if (string.IsNullOrEmpty(imageFileName)) { imageFileName = _stateInfo.FilePath.GetNameWithoutExtension() + $".{i:00}"; } ExtractImageInternal(_imageState.Images[i], destinationFileSystem, imageFileName + ".png"); } }
private void ExtractImage(string imageIndexArgument, UPath filePath) { if (!int.TryParse(imageIndexArgument, out var imageIndex)) { Console.WriteLine($"'{imageIndexArgument}' is not a valid number."); return; } if (imageIndex >= _imageState.Images.Count) { Console.WriteLine($"Index '{imageIndex}' was out of bounds."); return; } var destinationFileSystem = FileSystemFactory.CreateSubFileSystem(filePath.GetDirectory().FullName, new StreamManager()); ExtractImageInternal(_imageState.Images[imageIndex], destinationFileSystem, filePath.GetName()); }
private async Task Execute() { log.Text = string.Empty; if (!VerifyInput()) { return; } ToggleUi(false); var selectedPlugin = (PluginElement)plugins.SelectedValue; _batchProcessor.PluginId = selectedPlugin.IsEmpty ? Guid.Empty : selectedPlugin.Plugin.PluginId; _batchProcessor.ScanSubDirectories = subDirectoryBox.Checked ?? false; var sourceFileSystem = FileSystemFactory.CreateSubFileSystem(selectedInputPath.Text, new StreamManager()); var destinationFileSystem = FileSystemFactory.CreateSubFileSystem(selectedOutputPath.Text, new StreamManager()); await _batchProcessor.Process(sourceFileSystem, destinationFileSystem); ToggleUi(true); }
private async Task ReplaceDirectory(TreeGridItem item) { var itemPath = GetAbsolutePath(item); var filePaths = _archiveFileSystem.EnumerateAllFiles(itemPath).Select(x => x.GetSubDirectory(itemPath).ToRelative()).ToArray(); if (filePaths.Length <= 0) { _formInfo.FormCommunicator.ReportStatus(true, "No files to replace."); return; } // Select folder var replacePath = SelectFolder(); if (replacePath.IsNull || replacePath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No folder selected."); return; } // Extract elements _formInfo.FormCommunicator.ReportStatus(true, string.Empty); var sourceFileSystem = FileSystemFactory.CreateSubFileSystem(replacePath.FullName, _formInfo.FileState.StreamManager); _formInfo.Progress.StartProgress(); await _asyncOperation.StartAsync(cts => { var count = 0; foreach (var filePath in filePaths) { if (cts.IsCancellationRequested) { break; } _formInfo.Progress.ReportProgress("Replace files", count++, filePaths.Length); var afi = ((AfiFileEntry)_archiveFileSystem.GetFileEntry(itemPath / filePath)).ArchiveFileInfo; if (IsFileLocked(afi, true)) { continue; } if (!sourceFileSystem.FileExists(filePath)) { continue; } var currentFileStream = sourceFileSystem.OpenFile(filePath); //TODO this cast smells, should IFileState/IPluginState be generified? ((IArchiveState)_formInfo.FileState.PluginState).AttemptReplaceFile(afi, currentFileStream); AddChangedDirectory(afi.FilePath.GetDirectory()); } }); _formInfo.Progress.ReportProgress("Replace files", 1, 1); _formInfo.Progress.FinishProgress(); if (_asyncOperation.WasCancelled) { _formInfo.FormCommunicator.ReportStatus(false, "File replacement cancelled."); } else { _formInfo.FormCommunicator.ReportStatus(true, "File(s) replaced successfully."); } UpdateFiles(GetAbsolutePath((TreeGridItem)folderView.SelectedItem)); UpdateDirectories(); _formInfo.FormCommunicator.Update(true, false); }
private async Task ReplaceFiles(IList <IArchiveFileInfo> files) { if (files.Count <= 0) { _formInfo.FormCommunicator.ReportStatus(true, "No files to replace."); return; } // Select destination UPath replaceDirectory; UPath replaceFileName; if (files.Count == 1) { var selectedPath = OpenFile(files[0].FilePath.GetName()); if (selectedPath.IsNull || selectedPath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No file selected."); return; } replaceDirectory = selectedPath.GetDirectory(); replaceFileName = selectedPath.GetName(); } else { var selectedPath = SelectFolder(); if (selectedPath.IsNull || selectedPath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No folder selected."); return; } replaceDirectory = selectedPath; replaceFileName = UPath.Empty; } // Extract elements _formInfo.FormCommunicator.ReportStatus(true, string.Empty); var sourceFileSystem = FileSystemFactory.CreateSubFileSystem(replaceDirectory.FullName, _formInfo.FileState.StreamManager); _formInfo.Progress.StartProgress(); await _asyncOperation.StartAsync(cts => { var count = 0; foreach (var file in files) { if (cts.IsCancellationRequested) { break; } _formInfo.Progress.ReportProgress("Replace files", count++, files.Count); if (IsFileLocked(file, true)) { continue; } var filePath = replaceFileName.IsEmpty ? file.FilePath.GetName() : replaceFileName; if (!sourceFileSystem.FileExists(filePath)) { continue; } var currentFileStream = sourceFileSystem.OpenFile(filePath); //TODO this cast smells, should IFileState/IPluginState be generified? ((IArchiveState)_formInfo.FileState.PluginState).AttemptReplaceFile(file, currentFileStream); AddChangedDirectory(file.FilePath.GetDirectory()); } }); _formInfo.Progress.ReportProgress("Replace files", 1, 1); _formInfo.Progress.FinishProgress(); if (_asyncOperation.WasCancelled) { _formInfo.FormCommunicator.ReportStatus(false, "File replacement cancelled."); } else { _formInfo.FormCommunicator.ReportStatus(true, "File(s) replaced successfully."); } UpdateFiles(GetAbsolutePath((TreeGridItem)folderView.SelectedItem)); UpdateDirectories(); _formInfo.FormCommunicator.Update(true, false); }
private async Task ExtractDirectory(TreeGridItem item) { var itemPath = GetAbsolutePath(item); var filePaths = _archiveFileSystem.EnumerateAllFiles(itemPath, _searchTerm.Get()).Select(x => x.GetSubDirectory(itemPath).ToRelative()).ToArray(); if (filePaths.Length <= 0) { _formInfo.FormCommunicator.ReportStatus(true, "No files to extract."); return; } // Select folder var extractPath = SelectFolder(); if (extractPath.IsNull || extractPath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No folder selected."); return; } // Extract elements _formInfo.FormCommunicator.ReportStatus(true, string.Empty); var subFolder = item == folders[0] ? GetRootName() : GetAbsolutePath(item).ToRelative(); var destinationFileSystem = FileSystemFactory.CreateSubFileSystem((extractPath / subFolder).FullName, _formInfo.FileState.StreamManager); _formInfo.Progress.StartProgress(); await _asyncOperation.StartAsync(cts => { var count = 0; foreach (var filePath in filePaths) { if (cts.IsCancellationRequested) { break; } _formInfo.Progress.ReportProgress("Extract files", count++, filePaths.Length); var afi = ((AfiFileEntry)_archiveFileSystem.GetFileEntry(itemPath / filePath)).ArchiveFileInfo; if (IsFileLocked(afi, false)) { continue; } Stream newFileStream; try { newFileStream = destinationFileSystem.OpenFile(filePath, FileMode.Create, FileAccess.Write); } catch (IOException) { continue; } var currentFileStream = afi.GetFileData().Result; currentFileStream.CopyTo(newFileStream); newFileStream.Close(); } }); _formInfo.Progress.ReportProgress("Extract files", 1, 1); _formInfo.Progress.FinishProgress(); if (_asyncOperation.WasCancelled) { _formInfo.FormCommunicator.ReportStatus(false, "File extraction cancelled."); } else { _formInfo.FormCommunicator.ReportStatus(true, "File(s) extracted successfully."); } }
private async Task ExtractFiles(IList <IArchiveFileInfo> files) { if (files.Count <= 0) { _formInfo.FormCommunicator.ReportStatus(true, "No files to extract."); return; } // Select folder or file var selectedPath = files.Count > 1 ? SelectFolder() : SaveFile(files[0].FilePath.GetName()); if (selectedPath.IsNull || selectedPath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No target selected."); return; } // Use containing directory as root if a file was selected var extractRoot = files.Count > 1 ? selectedPath : selectedPath.GetDirectory(); // Extract elements _formInfo.FormCommunicator.ReportStatus(true, string.Empty); var destinationFileSystem = FileSystemFactory.CreateSubFileSystem(extractRoot.FullName, _formInfo.FileState.StreamManager); _formInfo.Progress.StartProgress(); await _asyncOperation.StartAsync(async cts => { var count = 0; foreach (var file in files) { if (cts.IsCancellationRequested) { break; } _formInfo.Progress.ReportProgress("Extract files", count++, files.Count); if (IsFileLocked(file, false)) { continue; } Stream newFileStream; try { // Use in-archive filename if a folder was selected, use selected filename if a file was selected var extractName = files.Count > 1 ? file.FilePath.GetName() : selectedPath.GetName(); newFileStream = destinationFileSystem.OpenFile(extractName, FileMode.Create, FileAccess.Write); } catch (IOException) { continue; } var currentFileStream = await file.GetFileData(); currentFileStream.CopyTo(newFileStream); newFileStream.Close(); } }); _formInfo.Progress.ReportProgress("Extract files", 1, 1); _formInfo.Progress.FinishProgress(); if (_asyncOperation.WasCancelled) { _formInfo.FormCommunicator.ReportStatus(false, "File extraction cancelled."); } else { _formInfo.FormCommunicator.ReportStatus(true, "File(s) extracted successfully."); } }
private async Task AddFiles(TreeGridItem item) { // Select folder var selectedPath = SelectFolder(); if (selectedPath.IsNull || selectedPath.IsEmpty) { _formInfo.FormCommunicator.ReportStatus(false, "No folder selected."); return; } // Add elements var subFolder = GetAbsolutePath(item); var sourceFileSystem = FileSystemFactory.CreateSubFileSystem(selectedPath.FullName, _formInfo.FileState.StreamManager); var elements = sourceFileSystem.EnumerateAllFiles(UPath.Root).ToArray(); if (elements.Length <= 0) { _formInfo.FormCommunicator.ReportStatus(false, "No files to add."); return; } _formInfo.FormCommunicator.ReportStatus(true, string.Empty); _formInfo.Progress.StartProgress(); var filesNotAdded = false; await _asyncOperation.StartAsync(cts => { var count = 0; foreach (var filePath in elements) { if (cts.IsCancellationRequested) { break; } _formInfo.Progress.ReportProgress("Add files", count++, elements.Length); // Do not add file if it already exists // This would be replacement and is not part of this operation if (_archiveFileSystem.FileExists(subFolder / filePath.ToRelative())) { continue; } // TODO: This will currently copy files to memory, instead of just using a reference to any more memory efficient stream (like FileStream) Stream createdFile; try { // The plugin can throw if a file is not addable createdFile = _archiveFileSystem.OpenFile(subFolder / filePath.ToRelative(), FileMode.Create, FileAccess.Write); } catch (Exception e) { _formInfo.Logger.Fatal(e, "Could not add the file {0}", filePath); filesNotAdded = true; continue; } var sourceFile = sourceFileSystem.OpenFile(filePath); sourceFile.CopyTo(createdFile); sourceFile.Close(); } }); _formInfo.Progress.ReportProgress("Add files", 1, 1); _formInfo.Progress.FinishProgress(); if (_asyncOperation.WasCancelled) { _formInfo.FormCommunicator.ReportStatus(false, "File adding cancelled."); } else if (filesNotAdded) { _formInfo.FormCommunicator.ReportStatus(true, "Some file(s) could not be added successfully. Refer to the log for more information."); } else { _formInfo.FormCommunicator.ReportStatus(true, "File(s) added successfully."); } UpdateFiles(GetAbsolutePath((TreeGridItem)folderView.SelectedItem)); _formInfo.FormCommunicator.Update(true, false); }