public bool Export() { XElement files = edit.Root.Element("Files"); ConsoleCount count = new ConsoleCount( "Exporting file {0:0000} of {1:0000}", files.Elements("File").Count() ); foreach (XElement fileEdit in files.Elements("File")) { count.Show(); string path = fileEdit.Element("Path").Value; IList<string> exportPaths = fileEdit.Elements("Import") .Select(f => this.config.ResolvePath(f.Value)) .ToList(); foreach (string p in exportPaths.Select(f => System.IO.Path.GetDirectoryName(f))) if (!System.IO.Directory.Exists(p)) System.IO.Directory.CreateDirectory(p); if (exportPaths.Any()) { GameFile file = fileManager.RescueFile(path); file.Format.Read(); try { file.Format.Export(exportPaths.ToArray()); } catch (Exception ex) { //Console.WriteLine("Can not export {0}", path); //Console.WriteLine(ex.Message); if (!(ex is NotImplementedException) && !(ex is NotSupportedException)) return false; } } count.UpdateCoordinates(); } return true; }
public void Write(params string[] outputPath) { // Check if we can write to the output paths GameFile rootFile = fileManager.Root as GameFile; if (rootFile != null && outputPath.Length != 1) throw new ArgumentException("Only ONE file need to be written"); else if (rootFile == null && fileManager.Root.Files.Count != outputPath.Length) throw new ArgumentException("There are not enough output paths."); // Write files data to memory buffers ConsoleCount count = new ConsoleCount( "Writing internal file {0:0000} of {1:0000}", this.updateQueue.Count); bool outWritten = false; foreach (string filePath in this.updateQueue) { count.Show(); // Get file to write GameFile file = this.fileManager.Root.SearchFile(filePath) as GameFile; if (file == null) throw new Exception("File " + filePath + " not found."); // Check if this is the output file to write // In this case, we will write directly to the output file if (file == rootFile) { file.Format.Write(outputPath[0]); outWritten = true; count.UpdateCoordinates(); continue; } // TODO: Do the same in the case of more than one output files. file.Format.Write(); count.UpdateCoordinates(); } this.updateQueue.Clear(); // Write to output files count = new ConsoleCount("Writing external file {0:0000} of {1:0000}", outputPath.Length); if (rootFile != null) { count.Show(); if (!outWritten) rootFile.Stream.WriteTo(outputPath[0]); } else { for (int i = 0; i < fileManager.Root.Files.Count; i++) { GameFile f = fileManager.Root.Files[i] as GameFile; if (f != null) { count.Show(); f.Stream.WriteTo(outputPath[i]); count.UpdateCoordinates(); } } } }
public bool Import(Func<string, bool> importFilter) { XElement files = edit.Root.Element("Files"); StreamWriter skipFiles = null; ConsoleCount count = new ConsoleCount( "Importing file {0:0000} of {1:0000}", files.Elements("File").Count() ); foreach (XElement fileEdit in files.Elements("File")) { count.Show(); string path = fileEdit.Element("Path").Value; IEnumerable<string> import = fileEdit.Elements("Import"). Select(f => this.config.ResolvePath(f.Value)); if (import.All(f => System.IO.File.Exists(f)) && import.Any(importFilter)) { try { GameFile file = fileManager.RescueFile(path); file.Format.Read(); file.Format.Import(import.ToArray()); this.UpdateQueue(file); } catch (Exception ex) { Console.WriteLine("ERROR with {0}", path); Console.WriteLine(ex.ToString()); return false; } } else { if (skipFiles == null) skipFiles = new StreamWriter("skipped.txt", false); foreach (string f in import) skipFiles.WriteLine(f); skipFiles.WriteLine(); } count.UpdateCoordinates(); } if (skipFiles != null) skipFiles.Close(); return true; }
public bool Import(Func<string, bool> importFilter) { XElement files = edit.Root.Element("Files"); System.IO.StreamWriter skipFiles = null; ConsoleCount count = new ConsoleCount( "Importing file {0:0000} of {1:0000}", files.Elements().Count() ); foreach (XElement fileEdit in files.Elements()) { count.Show(); bool isVirtual = (fileEdit.Name == "VirtualFile"); string path = fileEdit.Element("Path")?.Value ?? "<VirtualFile>"; IList<string> import = fileEdit.Elements("Import") .Select(f => this.config.ResolvePath(f.Value)) .ToList(); bool internalFilter = Convert.ToBoolean(fileEdit.Element("InternalFilter")?.Value ?? "false"); bool validFile = (!internalFilter || import.Any(importFilter)); if (import.All(System.IO.File.Exists) && validFile) { try { if (!isVirtual) { GameFile file = fileManager.RescueFile(path); file.Format.Read(); file.Format.Import(import.ToArray()); this.UpdateQueue(file); } else { // It's a virtual file, just a layer // Just create the format and import. string type = fileEdit.Element("Type").Value; Format format = FileManager.GetFormat(type); format.Initialize(null, fileEdit.Element("Parameters")); format.Import(import.ToArray()); } } catch (Exception ex) { Console.WriteLine("ERROR with {0}, {1}", path, import.FirstOrDefault()); Console.WriteLine(ex); return false; } } else { if (skipFiles == null) skipFiles = new System.IO.StreamWriter("skipped.txt", false); foreach (string f in import) skipFiles.WriteLine(f); skipFiles.WriteLine(); } count.UpdateCoordinates(); } if (skipFiles != null) skipFiles.Close(); return true; }