Exemple #1
0
        /// <inheritdoc />
        public async Task <LoadResult> LoadFile(IFileSystem fileSystem, UPath path, LoadFileContext loadFileContext)
        {
            ContractAssertions.IsNotNull(_stateInfo, "stateInfo");

            // If the same file is passed to another plugin, take the parent of the current state
            var parent    = _stateInfo;
            var statePath = _stateInfo.AbsoluteDirectory / _stateInfo.FilePath.ToRelative();

            if (fileSystem.ConvertPathToInternal(path) == statePath)
            {
                parent = _stateInfo.ParentStateInfo;
            }

            // 1. Load file
            var loadResult = await _parentPluginManager.LoadFile(fileSystem, path, parent, loadFileContext);

            if (!loadResult.IsSuccessful)
            {
                return(loadResult);
            }

            // 2. Add file to loaded files
            _loadedFiles.Add(loadResult.LoadedState);

            return(loadResult);
        }
        private void ExtractWith(IFileSystem sourceFileSystem, UPath filePath, Guid pluginId)
        {
            // Load file
            LoadResult loadResult;

            try
            {
                loadResult = pluginId == Guid.Empty ?
                             _pluginManager.LoadFile(sourceFileSystem, filePath).Result :
                             _pluginManager.LoadFile(sourceFileSystem, filePath, pluginId).Result;
            }
            catch (Exception e)
            {
                Console.WriteLine($"Batch Error: {filePath}: {e.Message}");
                return;
            }

            if (!loadResult.IsSuccessful)
            {
                Console.WriteLine($"Batch Error: {filePath}: {loadResult.Message}");
                return;
            }

            var absolutePath          = (UPath)sourceFileSystem.ConvertPathToInternal(filePath);
            var destinationDirectory  = absolutePath.GetDirectory() / absolutePath.GetName().Replace('.', '_');
            var destinationFileSystem = FileSystemFactory.CreatePhysicalFileSystem(destinationDirectory, new StreamManager());

            switch (loadResult.LoadedState.PluginState)
            {
            case IArchiveState archiveState:
                foreach (var afi in archiveState.Files)
                {
                    var newFileStream = destinationFileSystem.OpenFile(afi.FilePath, FileMode.Create, FileAccess.Write);
                    afi.GetFileData().Result.CopyTo(newFileStream);

                    newFileStream.Close();
                }
                break;

            case IImageState imageState:
                var index = 0;
                foreach (var img in imageState.Images)
                {
                    var kanvasImage = new KanvasImage(imageState, img);
                    kanvasImage.GetImage().Save(destinationDirectory + "/" + (img.Name ?? $"{index:00}") + ".png");

                    index++;
                }
                break;

            default:
                Console.WriteLine($"Batch Error: {filePath}: '{loadResult.LoadedState.PluginState.GetType().Name}' is not supported.");
                break;
            }

            _pluginManager.Close(loadResult.LoadedState);
        }
        private async Task <IStateInfo> LoadArchive(string file)
        {
            var loadResult = await _pluginManager.LoadFile(file);

            if (!loadResult.IsSuccessful)
            {
                throw loadResult.Exception;
            }

            return(loadResult.LoadedState);
        }