예제 #1
0
        public static void Export(AdfFile adf, RuntimeTypeLibrary runtime, Stream input, XmlWriter writer)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("adf");

            if (adf.InstanceInfos.Count > 0)
            {
                writer.WriteStartElement("instances");

                foreach (var instanceInfo in adf.InstanceInfos)
                {
                    writer.WriteStartElement("instance");
                    writer.WriteAttributeString("root", instanceInfo.Name);

                    var typeDefinition = runtime.GetTypeDefinition(instanceInfo.TypeHash);
                    input.Position = instanceInfo.Offset;
                    using (var data = input.ReadToMemoryStream((int)instanceInfo.Size))
                    {
                        var exporter = new InstanceExporter(adf, runtime);
                        exporter.Write(typeDefinition, instanceInfo.Name, data, writer);
                    }

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
예제 #2
0
            public InstanceExporter(AdfFile adf, RuntimeTypeLibrary runtime)
            {
                if (adf == null)
                {
                    throw new ArgumentNullException("adf");
                }

                if (runtime == null)
                {
                    throw new ArgumentNullException("runtime");
                }

                this._Adf       = adf;
                this._Runtime   = runtime;
                this._WorkQueue = new Queue <WorkItem>();
            }
예제 #3
0
        private static void Main(string[] args)
        {
            var  mode             = Mode.Unknown;
            bool showHelp         = false;
            var  typeLibraryPaths = new List <string>();

            var options = new OptionSet
            {
                // ReSharper disable AccessToModifiedClosure
                { "e|export", "convert from binary to XML", v => SetOption(v, ref mode, Mode.Export) },
                { "i|import", "convert from XML to binary", v => SetOption(v, ref mode, Mode.Import) },
                // ReSharper restore AccessToModifiedClosure
                { "t|type-library=", "load type library from file", v => typeLibraryPaths.Add(v) },
                { "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 != null && extension.ToLowerInvariant() == ".xml")
                {
                    mode = Mode.Import;
                }
                else
                {
                    mode = Mode.Export;
                }
            }

            if (extras.Count < 1 || extras.Count > 2 ||
                showHelp == true ||
                mode == Mode.Unknown)
            {
                Console.WriteLine("Usage: {0} [OPTIONS]+ [-e] input_adf [output_xml]", GetExecutableName());
                Console.WriteLine("       {0} [OPTIONS]+ [-i] input_xml [output_adf]", GetExecutableName());
                Console.WriteLine("Convert an ADF file between binary and XML format.");
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            var runtime = new RuntimeTypeLibrary();

            foreach (var typeLibraryPath in typeLibraryPaths)
            {
                var adf = new FileFormats.AdfFile("");

                using (var input = File.OpenRead(typeLibraryPath))
                {
                    adf.Deserialize(input);
                }

                runtime.AddTypeDefinitions(adf);
            }

            if (mode == Mode.Export)
            {
                string inputPath  = extras[0];
                string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, ".xml");

                using (var input = File.OpenRead(inputPath))
                {
                    var adf = new AdfFile(Path.GetExtension(inputPath));
                    adf.Deserialize(input);
                    runtime.AddTypeDefinitions(adf);


                    var settings = new XmlWriterSettings
                    {
                        Indent          = true,
                        IndentChars     = "    ",
                        CheckCharacters = false,
                    };

                    using (var output = File.Create(outputPath))
                    {
                        var writer = XmlWriter.Create(output, settings);
                        Exporter.Export(adf, writer /*, runtime, input, writer*/);
                        writer.Flush();
                    }
                }
            }
            else if (mode == Mode.Import)
            {
                string inputPath = extras[0];

                using (var input = File.OpenRead(inputPath))
                {
                    var doc = new XPathDocument(input);
                    var nav = doc.CreateNavigator();

                    var root = nav.SelectSingleNode("/adf");
                    if (root == null)
                    {
                        throw new FormatException();
                    }

                    // retrieve the original extension as stored in the XML file
                    var    outputExtension = root.GetAttribute("extension", "");
                    string outputPath      = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, outputExtension);


                    AdfFile adf = Importer.Import(root);

                    using (var output = File.Create(outputPath))
                    {
                        output.Position = adf.EstimateHeaderSize();
                        adf.Serialize(output, 0);
                    }
                }
            }
        }