public static void Main(string[] args) { bool showHelp = false; string currentProject = null; var options = new OptionSet() { { "h|help", "show this message and exit", v => showHelp = v != null }, { "p|project=", "override current project", v => currentProject = v }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (extras.Count != 0 || showHelp == true) { Console.WriteLine("Usage: {0} [OPTIONS]+", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } Console.WriteLine("Loading project..."); var manager = Manager.Load(currentProject); if (manager.ActiveProject == null) { Console.WriteLine("Nothing to do: no active project loaded."); return; } var project = manager.ActiveProject; var installPath = project.InstallPath; var listsPath = project.ListsPath; if (installPath == null) { Console.WriteLine("Could not detect install path."); return; } if (listsPath == null) { Console.WriteLine("Could not detect lists path."); return; } var knownHashes = manager.LoadListsAnimationNames(); var archivePaths = new List <string>(); archivePaths.Add(Path.Combine(installPath, "afterbirth.a")); archivePaths.Add(Path.Combine(installPath, "animations.a")); var outputPaths = new List <string>(); var nameHash = ArchiveFile.ComputeNameHash("resources/animations.b"); for (int i = 0; i < archivePaths.Count; i++) { var archivePath = archivePaths[i]; if (File.Exists(archivePath) == false) { continue; } var outputPath = GetListPath(installPath, archivePath); if (outputPath == null) { throw new InvalidOperationException(); } Console.WriteLine(outputPath); outputPath = Path.Combine(listsPath, outputPath); if (outputPaths.Contains(outputPath) == true) { throw new InvalidOperationException(); } outputPaths.Add(outputPath); if (File.Exists(archivePath + ".bak") == true) { archivePath += ".bak"; } var hashes = new List <uint>(); using (var input = File.OpenRead(archivePath)) { IArchiveFile archive; if (ArchiveFile.IsValid(input) == true) { archive = new ArchiveFile(); } else if (Gibbed.Antibirth.FileFormats.ArchiveFile.IsValid(input) == true) { archive = new Gibbed.Antibirth.FileFormats.ArchiveFile(); } else { throw new NotSupportedException(); } archive.Deserialize(input); var entry = archive.Entries.FirstOrDefault(e => e.NameHash == nameHash); if (entry == null) { continue; } input.Seek(entry.Offset, SeekOrigin.Begin); using (var data = entry.Read(input, archive)) { var cache = new AnimationCacheBinaryFile(); cache.Deserialize(data); hashes.AddRange(cache.AnimatedActors.Keys); } } HandleEntries(hashes, knownHashes, outputPath); } }
public static void Main(string[] args) { var mode = Mode.Unknown; bool showHelp = false; bool verbose = false; var options = new OptionSet() { { "binary", "convert XML to binary", v => mode = v != null ? Mode.ToBinary : mode }, { "xml", "convert binary to XML", v => mode = v != null ? Mode.ToXml : mode }, { "v|verbose", "be verbose", v => verbose = v != null }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (mode == Mode.Unknown && extras.Count >= 1) { var extension = Path.GetExtension(extras[0]); if (extension == ".b") { mode = Mode.ToXml; } else if (extension == ".anm2" || Directory.Exists(extras[0]) == true) { mode = Mode.ToBinary; } } if (showHelp == true || mode == Mode.Unknown || extras.Count < 1 || extras.Count > 2) { Console.WriteLine("Usage: {0} [OPTIONS]+ input [output]", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } if (mode == Mode.ToBinary) { throw new NotImplementedException(); } else if (mode == Mode.ToXml) { string inputPath = extras[0]; string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, null) + "_converted"; var cache = new AnimationCacheBinaryFile(); using (var input = File.OpenRead(inputPath)) { cache.Deserialize(input); } var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, OmitXmlDeclaration = true, Indent = true, IndentChars = " ", }; var culture = CultureInfo.InvariantCulture; if (verbose == true) { Console.WriteLine("Loading project..."); } var manager = ProjectData.Manager.Load(); if (manager.ActiveProject == null) { Console.WriteLine("Warning: no active project loaded."); } var hashes = manager.LoadListsAnimationNames(); foreach (var kv in cache.AnimatedActors) { string entryName = hashes[kv.Key]; string entryPath; if (entryName == null) { entryPath = Path.Combine(outputPath, "__UNKNOWN", kv.Key.ToString("X8") + ".anm2"); } else { entryPath = Path.Combine(outputPath, FilterEntryName(entryName)); } var entryParent = Path.GetDirectoryName(entryPath); if (string.IsNullOrEmpty(entryParent) == false) { Directory.CreateDirectory(entryParent); } using (var writer = XmlWriter.Create(entryPath, settings)) { var instance = kv.Value; writer.WriteStartDocument(); writer.WriteComment("Converted to ANM2 by Gibbed.Rebirth.ConvertAnimations"); writer.WriteStartElement("AnimatedActor"); writer.WriteStartElement("Content"); writer.WriteStartElement("Spritesheets"); //BaseSpritesheetPath isn't needed by the game to load actual .anm2 files. //writer.WriteAttributeString("BasePath", instance.Content.BaseSpritesheetPath); foreach (var spritesheet in instance.Content.Spritesheets) { writer.WriteStartElement("Spritesheet"); writer.WriteAttributeString("Path", spritesheet.Path); writer.WriteAttributeString("Id", spritesheet.Id.ToString(culture)); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Layers"); foreach (var layer in instance.Content.Layers) { writer.WriteStartElement("Layer"); writer.WriteAttributeString("Name", layer.Name); writer.WriteAttributeString("Id", layer.Id.ToString(culture)); writer.WriteAttributeString("SpritesheetId", layer.SpritesheetId.ToString(culture)); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Nulls"); foreach (var null_ in instance.Content.Nulls) { writer.WriteStartElement("Null"); writer.WriteAttributeString("Id", null_.Id.ToString(culture)); writer.WriteAttributeString("Name", null_.Name); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Events"); foreach (var event_ in instance.Content.Events) { writer.WriteStartElement("Event"); writer.WriteAttributeString("Id", event_.Id.ToString(culture)); writer.WriteAttributeString("Name", event_.Name); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement("Animations"); writer.WriteAttributeString("DefaultAnimation", instance.DefaultAnimation); foreach (var animation in instance.Animations) { writer.WriteStartElement("Animation"); writer.WriteAttributeString("Name", animation.Name); writer.WriteAttributeString("FrameNum", animation.FrameNum.ToString(culture)); writer.WriteAttributeString("Loop", animation.Loop.ToString(culture)); writer.WriteStartElement("RootAnimation"); foreach (var frame in animation.RootAnimation.Frames) { writer.WriteStartElement("Frame"); WriteFrame(writer, frame, culture); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("LayerAnimations"); foreach (var layerAnimation in animation.LayerAnimations) { writer.WriteStartElement("LayerAnimation"); writer.WriteAttributeString("LayerId", layerAnimation.LayerId.ToString(culture)); writer.WriteAttributeString("Visible", layerAnimation.Visible.ToString(culture)); foreach (var frame in layerAnimation.Frames) { writer.WriteStartElement("Frame"); WriteFrame(writer, frame, culture); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("NullAnimations"); foreach (var nullAnimation in animation.NullAnimations) { writer.WriteStartElement("NullAnimation"); writer.WriteAttributeString("NullId", nullAnimation.NullId.ToString(culture)); writer.WriteAttributeString("Visible", nullAnimation.Visible.ToString(culture)); foreach (var frame in nullAnimation.Frames) { writer.WriteStartElement("Frame"); WriteFrame(writer, frame, culture); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Triggers"); foreach (var trigger in animation.Triggers) { writer.WriteStartElement("Trigger"); writer.WriteAttributeString("EventId", trigger.EventId.ToString(culture)); writer.WriteAttributeString("AtFrame", trigger.AtFrame.ToString(culture)); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } } } else { throw new InvalidOperationException(); } }