public void CreateModPackFile() { PackFileService packFileService = new PackFileService(new PackFileDataBase(), null); var packContainer = packFileService.CreateNewPackFileContainer("MyTestPackFile", PackFileCAType.MOD); var packFile = new PackFile("ExampleFile.txt", new FileSystemSource(@"Data\FolderData\SubFolder0\Subfolder_0_file0.txt")); packFileService.AddFileToPack(packContainer, @"data\content\files", packFile); //var packFileContent = Encoding.UTF8.GetString(packFile.DataSource.ReadData()); using (MemoryStream ms0 = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms0)) packFileService.Save(packContainer, writer); // Load it again var orgData = ms0.ToArray(); using (MemoryStream ms1 = new MemoryStream(orgData)) { Assert.AreEqual(ms1.Length, orgData.Length); using (BinaryReader reader = new BinaryReader(ms1)) { var loadedPackFile = new PackFileContainer("File", reader); Assert.AreEqual(packContainer.Header.Version, loadedPackFile.Header.Version); Assert.AreEqual(packContainer.Header.FileType, loadedPackFile.Header.FileType); Assert.AreEqual(1, loadedPackFile.Header.FileCount); Assert.AreEqual(0, loadedPackFile.Header.ReferenceFileCount); } } } }
public void AddFileToFolder() { PackFileService packFileService = new PackFileService(new PackFileDataBase()); var loadedPackFile = packFileService.Load(@"Data\CaPackFile_01.pack"); packFileService.AddFileToPack(loadedPackFile, @"warmachines\materials", TestPackFileHelper.CreatePackFile("TestFile.txt")); var newFileCount = loadedPackFile.FileList.Count; Assert.AreEqual(5, newFileCount); var file = packFileService.FindFile(@"warmachines\materials\TestFile.txt"); Assert.NotNull(file); }
public static PackFile Save(PackFileService packFileService, string filename, PackFile packFile, byte[] updatedData = null, bool promptSaveOverride = true) { filename = filename.ToLower(); var selectedEditabelPackFile = packFileService.GetEditablePack(); if (selectedEditabelPackFile == null) { MessageBox.Show("No editable pack selected!"); return(null); } var existingFile = packFileService.FindFile(filename, selectedEditabelPackFile); if (existingFile != null && promptSaveOverride) { var fullPath = packFileService.GetFullPath(existingFile as PackFile, selectedEditabelPackFile); if (MessageBox.Show($"Replace existing file?\n{fullPath} \nin packfile:{selectedEditabelPackFile.Name}", "", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) { var extention = Path.GetExtension(fullPath); using (var browser = new SavePackFileWindow(packFileService)) { browser.ViewModel.Filter.SetExtentions(new List <string>() { extention }); if (browser.ShowDialog() == true) { var path = browser.FilePath; if (path.Contains(extention) == false) { path += extention; } filename = path; existingFile = null; } else { return(null); } } } } if (existingFile == null) { var directoryPath = Path.GetDirectoryName(filename); var justFileName = Path.GetFileName(filename); var data = updatedData; if (data == null) { data = packFile.DataSource.ReadData(); } var newPackFile = new PackFile(justFileName, new MemorySource(data)); packFileService.AddFileToPack(selectedEditabelPackFile, directoryPath, newPackFile); return(newPackFile); } else { if (updatedData == null) { throw new Exception("Trying to update an existing file, but no data is provided"); } packFileService.SaveFile(existingFile as PackFile, updatedData); return(existingFile as PackFile); } }