Esempio n. 1
0
        /// <summary>
        /// Exports the file
        /// </summary>
        /// <param name="includeMipmap">Indicates if available mipmaps should be included</param>
        /// <returns>The task</returns>
        public async Task ExportFileAsync(bool includeMipmap)
        {
            RL.Logger?.LogTraceSource($"The archive file {FileName} is being exported...");

            // Run as a load operation
            using (Archive.LoadOperation.Run())
            {
                // Lock the access to the archive
                using (await Archive.ArchiveLock.LockAsync())
                {
                    // Run as a task
                    await Task.Run(async() =>
                    {
                        // Get the file extensions
                        var ext = (includeMipmap ? FileData.CastTo <IArchiveImageFileData>().SupportedMipmapExportFileExtensions : FileData.SupportedExportFileExtensions);

                        // Get the output path
                        var result = await Services.BrowseUI.SaveFileAsync(new SaveFileViewModel()
                        {
                            Title       = Resources.Archive_ExportHeader,
                            DefaultName = new FileSystemPath(FileName).ChangeFileExtension(ext.First().GetPrimaryFileExtension(), true),
                            Extensions  = GetFileFilterCollection(ext).ToString()
                        });

                        if (result.CanceledByUser)
                        {
                            return;
                        }

                        Archive.SetDisplayStatus(String.Format(Resources.Archive_ExportingFileStatus, FileName));

                        try
                        {
                            // Get the file bytes
                            var bytes = FileData.GetDecodedFileBytes(ArchiveFileStream, Archive.ArchiveFileGenerator, false);

                            // Get the file format
                            var format = result.SelectedFileLocation.FileExtension;

                            if (!includeMipmap)
                            {
                                // Export the file
                                ExportFile(result.SelectedFileLocation, bytes, format);
                            }
                            else
                            {
                                // Helper method for getting the mipmap file stream
                                Stream GetMipmapStream(int index)
                                {
                                    var path = $"{result.SelectedFileLocation.RemoveFileExtension().FullPath} ({index}){format.FileExtensions}";

                                    return(File.Open(path, FileMode.Create, FileAccess.Write));
                                }

                                // Export the mipmaps
                                FileData.CastTo <IArchiveImageFileData>().ExportMipmaps(bytes, GetMipmapStream, format);
                            }
                        }
                        catch (Exception ex)
                        {
                            ex.HandleError("Exporting archive file", FileName);

                            await Services.MessageUI.DisplayExceptionMessageAsync(ex, String.Format(Resources.Archive_ExportError, FileName));

                            return;
                        }
                        finally
                        {
                            Archive.SetDisplayStatus(String.Empty);
                        }

                        RL.Logger?.LogTraceSource($"The archive file has been exported");

                        await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Archive_ExportFileSuccess);
                    });
                }
            }
        }