public void AddTypeDefinitions(FileFormats.AdfFile adf)
        {
            foreach (var typeDefinition in adf.TypeDefinitions)
            {
                if (this._TypeDefinitions.ContainsKey(typeDefinition.NameHash) == true)
                {
                    continue;
                    throw new InvalidOperationException();
                }

                this._TypeDefinitions.Add(typeDefinition.NameHash, typeDefinition);
            }
        }
Example #2
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);
                    }
                }
            }
        }
        private static void Main(string[] args)
        {
            var  mode     = Mode.Unknown;
            bool showHelp = false;

            var options = new OptionSet
            {
                // ReSharper disable AccessToModifiedClosure
                { "e|export", "convert from binary to JSON", v => SetOption(v, ref mode, Mode.Export) },
                { "i|import", "convert from JSON to binary", v => SetOption(v, ref mode, Mode.Import) },
                // ReSharper restore AccessToModifiedClosure
                { "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() == ".json")
                {
                    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_upgrade_onlinec [output_json]", GetExecutableName());
                Console.WriteLine("       {0} [OPTIONS]+ [-i] input_json [output_upgrade_onlinec]", GetExecutableName());
                Console.WriteLine("Convert an upgrade file between binary and JSON format.");
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

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

                var adf      = new FileFormats.AdfFile();
                var itemRoot = new UpgradeRoot();

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

                    var itemRootInfo = adf.InstanceInfos.FirstOrDefault(i => i.Name == "Profile");
                    if (itemRootInfo.TypeHash != UpgradeRoot.TypeHash)
                    {
                        throw new FormatException();
                    }

                    input.Position = itemRootInfo.Offset;
                    itemRoot.Deserialize(input, adf.Endian);
                }

                using (var output = File.Create(outputPath))
                    using (var textWriter = new StreamWriter(output))
                        using (var writer = new JsonTextWriter(textWriter))
                        {
                            writer.Indentation = 2;
                            writer.IndentChar  = ' ';
                            writer.Formatting  = Formatting.Indented;

                            var jsonItems = itemRoot.Upgrades.Select(
                                u =>
                            {
                                var jsonItem = new UpgradeRootJson.Upgrade()
                                {
                                    Name           = ResolveHash(adf, u.Name),
                                    TaskGroup      = ResolveHash(adf, u.TaskGroup),
                                    StarThreshold  = u.StarThreshold,
                                    Cost           = u.Cost,
                                    Ability        = ResolveHash(adf, u.Ability),
                                    UIName         = ResolveHash(adf, u.UIName),
                                    UIDescription  = ResolveHash(adf, u.UIDescription),
                                    UIDisplayOrder = u.UIDisplayOrder,
                                    UIImage        = u.UIImage,
                                    UIVideo        = u.UIVideo,
                                    UITreeName     = ResolveHash(adf, u.UITreeName),
                                    UITreeOrder    = u.UITreeOrder,
                                };
                                jsonItem.Prerequisites.AddRange(u.Prerequisites.Select(f => ResolveHash(adf, f)));
                                return(jsonItem);
                            }).ToArray();

                            var serializer = JsonSerializer.Create();
                            serializer.Serialize(writer, jsonItems);
                        }
            }
            else if (mode == Mode.Import)
            {
                string inputPath  = extras[0];
                string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, ".onlinec");

                var adf = new AdfFile()
                {
                    Endian = Endian.Little,
                };
                var itemRoot = new UpgradeRoot();

                using (var input = File.OpenRead(inputPath))
                    using (var textReader = new StreamReader(input))
                        using (var reader = new JsonTextReader(textReader))
                        {
                            var serializer = JsonSerializer.Create();
                            var jsonItems  = serializer.Deserialize <UpgradeRootJson.Upgrade[]>(reader);

                            itemRoot.Upgrades.AddRange(jsonItems.Select(
                                                           ju => new UpgradeRoot.Upgrade()
                            {
                                Name           = ComputeHash(adf, ju.Name),
                                TaskGroup      = ComputeHash(adf, ju.TaskGroup),
                                StarThreshold  = ju.StarThreshold,
                                Prerequisites  = ju.Prerequisites.Select(f => ComputeHash(adf, f)).ToArray(),
                                Cost           = ju.Cost,
                                Ability        = ComputeHash(adf, ju.Ability),
                                UIName         = ComputeHash(adf, ju.UIName),
                                UIDescription  = ComputeHash(adf, ju.UIDescription),
                                UIDisplayOrder = ju.UIDisplayOrder,
                                UIImage        = ju.UIImage,
                                UIVideo        = ju.UIVideo,
                                UITreeName     = ComputeHash(adf, ju.UITreeName),
                                UITreeOrder    = ju.UITreeOrder,
                            }));
                        }

                using (var output = File.Create(outputPath))
                {
                    using (var data = new MemoryStream())
                    {
                        itemRoot.Serialize(data, adf.Endian);
                        data.Flush();
                        data.Position = 0;

                        output.Position = adf.EstimateHeaderSize();
                        var itemRootPosition = output.Position;
                        output.WriteFromStream(data, data.Length);

                        adf.InstanceInfos.Add(new AdfFile.InstanceInfo()
                        {
                            Name     = "Profile",
                            NameHash = "Profile".HashJenkins(),
                            Offset   = (uint)itemRootPosition,
                            Size     = (uint)data.Length,
                            TypeHash = UpgradeRoot.TypeHash,
                        });

                        adf.Serialize(output, 0);
                    }
                }
            }
            else
            {
                throw new NotSupportedException();
            }
        }