/// <summary> /// Synchronizes the texture info for the selected game data directory /// </summary> /// <returns>The task</returns> public async Task SyncTextureInfoAsync() { OpenSpaceGameModeInfoAttribute attr = GameModeSelection.SelectedValue.GetAttribute <OpenSpaceGameModeInfoAttribute>(); DirectoryBrowserResult result = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Utilities_SyncTextureInfo_SelectDirHeader, DefaultDirectory = attr.Game?.GetInstallDir(false) ?? FileSystemPath.EmptyPath }); if (result.CanceledByUser) { return; } try { IsLoading = true; TextureInfoEditResult syncResult = await Task.Run(() => { // Get the settings OpenSpaceSettings gameSettings = attr.GetSettings(); // Get the file extension for the level data files var fileExt = GetLevelFileExtension(gameSettings); // Get the level data files var dataFiles = Directory.GetFiles(result.SelectedDirectory, $"*{fileExt}", SearchOption.AllDirectories).Select(x => new FileSystemPath(x)); // Get the .cnt file names var fileNames = GetCntFileNames(gameSettings); // Get the full paths and only keep the ones which exist var cntFiles = fileNames.Select(x => result.SelectedDirectory + x).Where(x => x.FileExists); // Sync the texture info return(EditTextureInfo(gameSettings, dataFiles, cntFiles)); }); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(String.Format(Resources.Utilities_SyncTextureInfo_Success, syncResult.EditedTextures, syncResult.TotalTextures)); } catch (Exception ex) { Logger.Error(ex, "Syncing texture info"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_SyncTextureInfo_Error); } finally { IsLoading = false; } }
public async Task EncodeAsync() { // Allow the user to select the files FileBrowserResult fileResult = await Services.BrowseUI.BrowseFileAsync(new FileBrowserViewModel() { Title = Resources.Utilities_Decoder_EncodeFileSelectionHeader, DefaultDirectory = SelectedType.Game?.GetInstallDir(false).FullPath, ExtensionFilter = SelectedType.GetFileFilter(), MultiSelection = true }); if (fileResult.CanceledByUser) { return; } // Allow the user to select the destination directory DirectoryBrowserResult destinationResult = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Browse_DestinationHeader, }); if (destinationResult.CanceledByUser) { return; } try { // Process the files await Task.Run(() => ProcessFile(fileResult.SelectedFiles, destinationResult.SelectedDirectory, false)); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Utilities_Decoder_EncodeSuccess); } catch (NotImplementedException ex) { Logger.Debug(ex, "Encoding files"); await Services.MessageUI.DisplayMessageAsync(Resources.NotImplemented, MessageType.Error); } catch (Exception ex) { Logger.Error(ex, "Encoding files"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_Decoder_EncodeError); } }
public async Task ConvertAsync() { if (IsLoading) { return; } try { IsLoading = true; // Attempt to get a default directory FileSystemPath?defaultDir = SelectedType.SelectedMode.GetDefaultDir(); // Make sure the directory exists if (defaultDir?.DirectoryExists != true) { defaultDir = null; } // Allow the user to select the files FileBrowserResult fileResult = await Services.BrowseUI.BrowseFileAsync(new FileBrowserViewModel() { Title = Resources.Utilities_Converter_FileSelectionHeader, DefaultDirectory = defaultDir?.FullPath, ExtensionFilter = SelectedType.SourceFileExtension.GetFileFilterItem.ToString(), MultiSelection = true }); if (fileResult.CanceledByUser) { return; } // Allow the user to select the destination directory DirectoryBrowserResult destinationResult = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Browse_DestinationHeader, }); if (destinationResult.CanceledByUser) { return; } // Allow the user to select the file extension to export as FileExtensionSelectionDialogResult extResult = await Services.UI.SelectFileExtensionAsync(new FileExtensionSelectionDialogViewModel(SelectedType.ConvertFormats, Resources.Utilities_Converter_ExportExtensionHeader)); if (extResult.CanceledByUser) { return; } try { await Task.Run(() => { using RCPContext context = new(fileResult.SelectedFiles.First().Parent); SelectedType.SelectedMode.InitContext(context); // Convert every file foreach (FileSystemPath file in fileResult.SelectedFiles) { // Get the destination file FileSystemPath destinationFile = destinationResult.SelectedDirectory + file.Name; // Set the file extension destinationFile = destinationFile.ChangeFileExtension(new FileExtension(extResult.SelectedFileFormat, multiple: true)).GetNonExistingFileName(); // Convert the file SelectedType.Convert(context, file.Name, destinationFile); } }); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Utilities_Converter_Success); } catch (Exception ex) { Logger.Error(ex, "Converting files"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_Converter_Error); } } finally { IsLoading = false; } }
public async Task ConvertBackAsync() { if (IsLoading) { return; } try { IsLoading = true; // Allow the user to select the files FileBrowserResult fileResult = await Services.BrowseUI.BrowseFileAsync(new FileBrowserViewModel() { Title = Resources.Utilities_Converter_FileSelectionHeader, ExtensionFilter = new FileFilterItemCollection(SelectedType.ConvertFormats.Select(x => new FileFilterItem($"*{x}", x.ToUpper()))).ToString(), MultiSelection = true }); if (fileResult.CanceledByUser) { return; } // Allow the user to select the destination directory DirectoryBrowserResult destinationResult = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Browse_DestinationHeader, }); if (destinationResult.CanceledByUser) { return; } // Get the state object?state = await SelectedType.GetConvertBackStateAsync(); if (state is null) { return; } try { await Task.Run(() => { using RCPContext context = new(fileResult.SelectedFiles.First().Parent); SelectedType.SelectedMode.InitContext(context); // Convert every file foreach (FileSystemPath file in fileResult.SelectedFiles) { // Get the destination file FileSystemPath destinationFile = destinationResult.SelectedDirectory + file.Name; // Set the file extension destinationFile = destinationFile.ChangeFileExtension(SelectedType.SourceFileExtension).GetNonExistingFileName(); // Convert the file SelectedType.ConvertBack(context, file, destinationFile.Name, state); } }); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Utilities_Converter_Success); } catch (Exception ex) { Logger.Error(ex, "Converting files"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_Converter_Error); } } finally { IsLoading = false; } }
public async Task SerializeAsync() { if (IsLoading) { return; } try { IsLoading = true; // Allow the user to select the files FileBrowserResult fileResult = await Services.BrowseUI.BrowseFileAsync(new FileBrowserViewModel() { Title = Resources.Utilities_Serializers_FileSelectionHeader, ExtensionFilter = new FileFilterItem("*.json", "JSON").ToString(), MultiSelection = true, }); if (fileResult.CanceledByUser || !fileResult.SelectedFiles.Any()) { return; } // Allow the user to select the destination directory DirectoryBrowserResult destinationResult = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Browse_DestinationHeader, }); if (destinationResult.CanceledByUser) { return; } try { await Task.Run(() => { using RCPContext context = new(destinationResult.SelectedDirectory); SelectedType.SelectedMode.InitContext(context); // Serialize every file foreach (FileSystemPath file in fileResult.SelectedFiles) { // Get the destination file FileSystemPath destinationFile = destinationResult.SelectedDirectory + file.Name; // Set the file extension destinationFile = destinationFile.ChangeFileExtension(SelectedType.FileExtension).GetNonExistingFileName(); // Deserialize the file JSON object data = JsonHelpers.DeserializeFromFile(file, SelectedType.Type); // Serialize the data to the destination file SelectedType.Serialize(context, destinationFile.Name, data); } }); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Utilities_Serializers_SerializeSuccess); } catch (Exception ex) { Logger.Error(ex, "Serializing files"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_Serializers_SerializeError); } } finally { IsLoading = false; } }
public async Task DeserializeAsync() { if (IsLoading) { return; } try { IsLoading = true; // Attempt to get a default directory FileSystemPath?defaultDir = SelectedType.SelectedMode.GetDefaultDir(); // Make sure the directory exists if (defaultDir?.DirectoryExists != true) { defaultDir = null; } // Allow the user to select the files FileBrowserResult fileResult = await Services.BrowseUI.BrowseFileAsync(new FileBrowserViewModel() { Title = Resources.Utilities_Serializers_FileSelectionHeader, DefaultDirectory = defaultDir?.FullPath, ExtensionFilter = SelectedType.FileExtension.GetFileFilterItem.ToString(), MultiSelection = true }); if (fileResult.CanceledByUser || !fileResult.SelectedFiles.Any()) { return; } // Allow the user to select the destination directory DirectoryBrowserResult destinationResult = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Browse_DestinationHeader, }); if (destinationResult.CanceledByUser) { return; } try { await Task.Run(() => { using RCPContext context = new(fileResult.SelectedFiles.First().Parent); SelectedType.SelectedMode.InitContext(context); // Deserialize every file foreach (FileSystemPath file in fileResult.SelectedFiles) { object?fileObj = SelectedType.Deserialize(context, file.Name); if (fileObj is null) { Logger.Warn("Deserialized file was null"); continue; } // Get the destination file FileSystemPath destinationFile = destinationResult.SelectedDirectory + file.Name; // Set the file extension destinationFile = destinationFile.ChangeFileExtension(new FileExtension(".json")).GetNonExistingFileName(); // Serialize as JSON JsonHelpers.SerializeToFile(fileObj, destinationFile); } }); await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Utilities_Serializers_DeserializeSuccess); } catch (Exception ex) { Logger.Error(ex, "Deserializing files"); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Utilities_Serializers_DeserializeError); } } finally { IsLoading = false; } }
/// <summary> /// Imports files to the directory /// </summary> /// <returns>The task</returns> public async Task ImportAsync() { // Run as a load operation using (await Archive.LoadOperation.RunAsync()) { // Lock the access to the archive using (await Archive.ArchiveLock.LockAsync()) { // Get the directory DirectoryBrowserResult result = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Archive_ImportDirectoryHeader, }); if (result.CanceledByUser) { return; } // Run as a task await Task.Run(async() => { // Keep track of the number of files getting imported int imported = 0; try { // Enumerate each directory view model foreach (ArchiveDirectoryViewModel dir in this.GetAllChildren(true)) { // Enumerate each file foreach (ArchiveFileViewModel file in dir.Files) { // Get the file directory, relative to the selected directory FileSystemPath fileDir = result.SelectedDirectory + dir.FullPath.Remove(0, FullPath.Length).Trim(Path.DirectorySeparatorChar); if (!fileDir.DirectoryExists) { continue; } // Get the base file path FileSystemPath baseFilePath = fileDir + new FileSystemPath(file.FileName); // Get the file path, without an extension FileSystemPath filePath = baseFilePath.RemoveFileExtension(true); // Make sure there are potential file matches if (!Directory.GetFiles(fileDir, $"{filePath.Name}*", SearchOption.TopDirectoryOnly).Any()) { continue; } // Get the file stream using ArchiveFileStream fileStream = file.GetDecodedFileStream(); // Initialize the file without loading the thumbnail file.InitializeFile(fileStream, ArchiveFileViewModel.ThumbnailLoadMode.None); // Check if the base file exists without changing the extensions if (baseFilePath.FileExists) { // Import the file file.ImportFile(baseFilePath, false); imported++; continue; } // Attempt to find a file for each supported extension foreach (FileExtension ext in file.FileType.ImportFormats) { // Get the path FileSystemPath fullFilePath = filePath.ChangeFileExtension(ext); // Make sure the file exists if (!fullFilePath.FileExists) { continue; } // Import the file file.ImportFile(fullFilePath, true); imported++; // Break the loop break; } } } } catch (Exception ex) { Logger.Error(ex, "Importing archive directory {0}", DisplayName); await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Archive_ImportDir_Error); return; } // Make sure at least one file has been imported if (imported == 0) { await Services.MessageUI.DisplayMessageAsync(Resources.Archive_ImportNoFilesError, MessageType.Warning); } }); } } }
/// <summary> /// Exports the directory /// </summary> /// <param name="forceNativeFormat">Indicates if the native format should be forced</param> /// <param name="selectedFilesOnly">Indicates if only selected files in the current directory should be exported</param> /// <returns>The task</returns> public async Task ExportAsync(bool forceNativeFormat, bool selectedFilesOnly = false) { // Run as a load operation using (await Archive.LoadOperation.RunAsync()) { // Lock the access to the archive using (await Archive.ArchiveLock.LockAsync()) { // Get the output path DirectoryBrowserResult result = await Services.BrowseUI.BrowseDirectoryAsync(new DirectoryBrowserViewModel() { Title = Resources.Archive_ExportHeader }); if (result.CanceledByUser) { return; } // Make sure there isn't an existing file at the output path if ((result.SelectedDirectory + ExportDirName).FileExists) { await Services.MessageUI.DisplayMessageAsync(String.Format(Resources.Archive_ExportDirFileConflict, ExportDirName), MessageType.Error); return; } // Run as a task await Task.Run(async() => { // Get the manager IArchiveDataManager manager = Archive.Manager; // Save the selected format for each collection Dictionary <IArchiveFileType, FileExtension?> selectedFormats = new(); try { ArchiveDirectoryViewModel[] allDirs; if (selectedFilesOnly) { allDirs = new ArchiveDirectoryViewModel[] { this } } ; else { allDirs = this.GetAllChildren(true).ToArray(); } int fileIndex = 0; int filesCount = allDirs.SelectMany(x => x.Files).Count(x => !selectedFilesOnly || x.IsSelected); // Handle each directory foreach (ArchiveDirectoryViewModel item in allDirs) { // Get the directory path FileSystemPath path = result.SelectedDirectory + ExportDirName + item.FullPath.Remove(0, FullPath.Length).Trim(manager.PathSeparatorCharacter); // Create the directory Directory.CreateDirectory(path); // Save each file foreach (ArchiveFileViewModel file in item.Files.Where(x => !selectedFilesOnly || x.IsSelected)) { // Get the file stream using ArchiveFileStream fileStream = file.GetDecodedFileStream(); // Initialize the file without loading the thumbnail file.InitializeFile(fileStream, ArchiveFileViewModel.ThumbnailLoadMode.None); fileStream.SeekToBeginning(); // Check if the format has not been selected if (!forceNativeFormat && !selectedFormats.ContainsKey(file.FileType) && file.FileType is not ArchiveFileType_Default) { // Get the available extensions string[] ext = new string[] { Resources.Archive_Export_Format_Original }.Concat(file.FileType.ExportFormats.Select(x => x.FileExtensions)).ToArray(); // Have user select the format FileExtensionSelectionDialogResult extResult = await Services.UI.SelectFileExtensionAsync(new FileExtensionSelectionDialogViewModel(ext, String.Format(Resources.Archive_FileExtensionSelectionInfoHeader, file.FileType.TypeDisplayName))); // Since this operation can't be canceled we get the first format if (extResult.CanceledByUser) { extResult.SelectedFileFormat = ext.First(); } // Add the selected format FileExtension?e = extResult.SelectedFileFormat == ext.First() ? null : new FileExtension(extResult.SelectedFileFormat, multiple: true); selectedFormats.Add(file.FileType, e); } // Get the selected format FileExtension?format = forceNativeFormat || file.FileType is ArchiveFileType_Default ? null : selectedFormats[file.FileType]; // Get the final file name to use when exporting FileSystemPath exportFileName = format == null ? new FileSystemPath(file.FileName) : new FileSystemPath(file.FileName).ChangeFileExtension(format, true); Archive.SetDisplayStatus($"{String.Format(Resources.Archive_ExportingFileStatus, file.FileName)}" + $"{Environment.NewLine}{++fileIndex}/{filesCount}"); try { // Export the file file.ExportFile(path + exportFileName, fileStream, format); } catch (Exception ex) { // If the export failed for a native format we throw if (format == null) { throw; } Logger.Error(ex, "Exporting archive file {0}", file.FileName); // If the export failed and we tried converting it we instead export it as the native format // Start by setting the file in the error state, thus changing the type file.InitializeAsError(); // Seek to the beginning of the stream in case some bytes were read fileStream.SeekToBeginning(); // Export the file as the native format file.ExportFile(path + file.FileName, fileStream, null); } } } } catch (Exception ex) { Logger.Error(ex, "Exporting archive directory {0}", DisplayName); await Services.MessageUI.DisplayExceptionMessageAsync(ex, String.Format(Resources.Archive_ExportError, DisplayName)); return; } finally { Archive.SetDisplayStatus(String.Empty); } await Services.MessageUI.DisplaySuccessfulActionMessageAsync(Resources.Archive_ExportFilesSuccess); }); } } }