Exemple #1
0
        private static void Main(string[] args)
        {
            var    mode              = Mode.Unknown;
            string baseName          = "";
            bool   showHelp          = false;
            bool   verbose           = false;
            bool   useMultiExporting = true;

            var options = new OptionSet()
            {
                { "i|import|fcb", "convert XML to FCB", v => mode = v != null ? Mode.Import : mode },
                { "e|export|xml", "convert FCB to XML", v => mode = v != null ? Mode.Export : mode },
                { "b|base-name=", "when converting, use specified base name instead of file name", v => baseName = v },
                {
                    "nme|no-multi-export", "when exporting, disable multi-exporting of entitylibrary and lib files",
                    v => useMultiExporting = v == null
                },
                { "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 (!string.IsNullOrEmpty(extension))
                {
                    extension = extension.ToLowerInvariant();
                }

                if (extension == ".xml")
                {
                    mode = Mode.Import;
                }
                else
                {
                    mode = Mode.Export;
                }
            }

            if (showHelp || 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 (verbose)
            {
                Console.WriteLine("Loading project...");
            }

            var manager = ProjectData.Manager.Load();

            if (manager.ActiveProject == null)
            {
                Console.WriteLine("Warning: no active project loaded.");
                return;
            }

            var project = manager.ActiveProject;

            if (verbose)
            {
                Console.WriteLine("Loading binary class and object definitions...");
            }

            BinaryObjectInfo.InfoManager infoManager;

            if (!System.Diagnostics.Debugger.IsAttached)
            {
                try
                {
                    infoManager = BinaryObjectInfo.InfoManager.Load(project.ListsPath);
                }
                catch (BinaryObjectInfo.LoadException e)
                {
                    Console.WriteLine("Failed to load binary definitions!");
                    Console.WriteLine("  {0}", e.Message);
                    return;
                }
                catch (BinaryObjectInfo.XmlLoadException e)
                {
                    Console.WriteLine("Failed to load binary definitions!");
                    Console.WriteLine("  in \"{0}\"", e.FilePath);
                    Console.WriteLine("  {0}", e.Message);
                    return;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception while loading binary definitions!");
                    Console.WriteLine();
                    Console.WriteLine("{0}", e);
                    return;
                }
            }
            else
            {
                infoManager = BinaryObjectInfo.InfoManager.Load(project.ListsPath);
            }

            if (mode == Mode.Import)
            {
                string inputPath = extras[0];
                string outputPath;

                if (extras.Count > 1)
                {
                    outputPath = extras[1];
                }
                else
                {
                    outputPath = RemoveConverted(Path.ChangeExtension(inputPath, null));
                    if (string.IsNullOrEmpty(Path.GetExtension(outputPath)))
                    {
                        var filename = Path.GetFileName(outputPath);
                        if (new Regex(@".+_[0-9a-fA-F]{8}").IsMatch(filename))
                        {
                            outputPath += ".obj";
                        }
                        else
                        {
                            outputPath += ".lib";
                        }
                    }
                }

                // Twice to remove *.lib.xml
                var basePath = Path.ChangeExtension(Path.ChangeExtension(inputPath, null), null);

                inputPath  = Path.GetFullPath(inputPath);
                outputPath = Path.GetFullPath(outputPath);
                basePath   = Path.GetFullPath(basePath);

                var bof = new BinaryObjectFile();

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

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

                    if (string.IsNullOrEmpty(baseName))
                    {
                        var baseNameFromObject = root.GetAttribute("def", "");
                        if (!string.IsNullOrEmpty(baseNameFromObject))
                        {
                            baseName = baseNameFromObject;
                        }
                        else
                        {
                            baseName = GetBaseNameFromPath(inputPath);
                        }
                    }

                    if (verbose)
                    {
                        Console.WriteLine("Reading XML...");
                    }

                    var objectFileDef = infoManager.GetObjectFileDefinition(baseName);
                    if (objectFileDef == null)
                    {
                        Console.WriteLine("Warning: could not find binary object file definition '{0}'", baseName);
                    }

                    var importing = new Importing(infoManager);

                    var classDef = objectFileDef != null ? objectFileDef.Object : null;
                    bof.Root = importing.Import(classDef, basePath, root);
                }

                if (verbose)
                {
                    Console.WriteLine("Writing FCB...");
                }

                using (var output = File.Create(outputPath))
                {
                    bof.Serialize(output);
                }
            }
            else if (mode == Mode.Export)
            {
                string inputPath = extras[0];
                string outputPath;
                string basePath;

                if (extras.Count > 1)
                {
                    outputPath = extras[1];
                    basePath   = Path.ChangeExtension(outputPath, null);
                }
                else
                {
                    basePath   = Path.ChangeExtension(inputPath, null);
                    outputPath = inputPath + ".xml";
                }

                if (string.IsNullOrEmpty(baseName))
                {
                    baseName = GetBaseNameFromPath(inputPath);
                }

                if (string.IsNullOrEmpty(baseName))
                {
                    throw new InvalidOperationException();
                }

                inputPath  = Path.GetFullPath(inputPath);
                outputPath = Path.GetFullPath(outputPath);
                basePath   = Path.GetFullPath(basePath);

                var objectFileDef = infoManager.GetObjectFileDefinition(baseName);
                if (objectFileDef == null)
                {
                    Console.WriteLine("Warning: could not find binary object file definition '{0}'", baseName);
                }

                if (verbose)
                {
                    Console.WriteLine("Reading FCB...");
                }

                var bof = new BinaryObjectFile();
                using (var input = File.OpenRead(inputPath))
                {
                    bof.Deserialize(input);
                }

                if (verbose)
                {
                    Console.WriteLine("Writing XML...");
                }

                if (useMultiExporting && Exporting.IsSuitableForEntityLibraryMultiExport(bof))
                {
                    Exporting.MultiExportEntityLibrary(objectFileDef, basePath, outputPath, infoManager, bof);
                }
                else if (useMultiExporting && Exporting.IsSuitableForLibraryMultiExport(bof))
                {
                    Exporting.MultiExportLibrary(objectFileDef, basePath, outputPath, infoManager, bof);
                }
                else if (useMultiExporting && Exporting.IsSuitableForNomadObjectTemplatesMultiExport(bof))
                {
                    Exporting.MultiExportNomadObjectTemplates(objectFileDef, basePath, outputPath, infoManager, bof);
                }
                else
                {
                    Exporting.Export(objectFileDef, outputPath, infoManager, bof);
                }
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            var    mode              = Mode.Unknown;
            string baseName          = "";
            bool   showHelp          = false;
            bool   verbose           = false;
            bool   useMultiExporting = true;

            var options = new OptionSet()
            {
                { "i|import|fcb", "convert XML to FCB", v => mode = v != null ? Mode.Import : mode },
                { "e|export|xml", "convert FCB to XML", v => mode = v != null ? Mode.Export : mode },
                { "b|base-name=", "when converting, use specified base name instead of file name", v => baseName = v },
                {
                    "nme|no-multi-export", "when exporting, disable multi-exporting of entitylibrary and lib files",
                    v => useMultiExporting = v == null
                },
                { "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 (!string.IsNullOrEmpty(extension))
                {
                    extension = extension.ToLowerInvariant();
                }

                if (extension == ".xml")
                {
                    mode = Mode.Import;
                }
                else
                {
                    mode = Mode.Export;
                }
            }

            if (showHelp || 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;
            }

            // custom
            Prepare();

            if (mode == Mode.Import)
            {
                string inputPath = extras[0];
                string outputPath;

                if (extras.Count > 1)
                {
                    outputPath = extras[1];
                }
                else
                {
                    outputPath = RemoveConverted(Path.ChangeExtension(inputPath, null));

                    if (string.IsNullOrEmpty(Path.GetExtension(outputPath)))
                    {
                        var filename = Path.GetFileName(outputPath);
                        if (new Regex(@".+_[0-9a-fA-F]{8}").IsMatch(filename))
                        {
                            outputPath += ".obj";
                        }
                        else
                        {
                            outputPath += ".lib";
                        }
                    }
                }

                // Twice to remove *.lib.xml
                var basePath = Path.ChangeExtension(Path.ChangeExtension(inputPath, null), null);

                inputPath  = Path.GetFullPath(inputPath);
                outputPath = Path.GetFullPath(outputPath);
                basePath   = Path.GetFullPath(basePath);

                var bof = new BinaryObjectFile();

                using (var input = File.OpenRead(inputPath))
                {
                    var header  = "";
                    var version = "";

                    var doc = new XPathDocument(input);
                    var nav = doc.CreateNavigator();

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

                    version     = root.GetAttribute("version", "");
                    bof.Version = (ushort)Convert.ToInt32(version);
                    Utility.Log($"Imported version = {version}");

                    header     = root.GetAttribute("header", "");
                    bof.Header = header;
                    Utility.Log($"Imported header = {header}");

                    baseName = GetBaseNameFromPath(inputPath);

                    if (verbose)
                    {
                        Console.WriteLine("Reading XML...");
                    }

                    var importing = new Importing();

                    bof.Root = importing.Import(basePath, root);
                }

                if (verbose)
                {
                    Console.WriteLine("Writing FCB...");
                }

                using (var output = File.Create(outputPath))
                {
                    bof.Serialize(output);
                }
            }
            else if (mode == Mode.Export)
            {
                string inputPath = extras[0];
                string outputPath;
                string basePath;

                if (extras.Count > 1)
                {
                    outputPath = extras[1];
                    basePath   = Path.ChangeExtension(outputPath, null);
                }
                else
                {
                    basePath   = Path.ChangeExtension(inputPath, null);
                    outputPath = inputPath + ".xml";
                }

                if (string.IsNullOrEmpty(baseName))
                {
                    baseName = GetBaseNameFromPath(inputPath);
                }

                if (string.IsNullOrEmpty(baseName))
                {
                    throw new InvalidOperationException();
                }

                inputPath  = Path.GetFullPath(inputPath);
                outputPath = Path.GetFullPath(outputPath);
                basePath   = Path.GetFullPath(basePath);

                if (verbose)
                {
                    Console.WriteLine("Reading FCB...");
                }

                var bof = new BinaryObjectFile();
                using (var input = File.OpenRead(inputPath))
                {
                    bof.Deserialize(input);
                }

                if (verbose)
                {
                    Console.WriteLine("Writing XML...");
                }

                if (useMultiExporting && Exporting.IsSuitableForEntityLibraryMultiExport(bof))
                {
                    Exporting.MultiExportEntityLibrary(basePath, outputPath, bof);
                }
                else if (useMultiExporting && Exporting.IsSuitableForLibraryMultiExport(bof))
                {
                    Exporting.MultiExportLibrary(basePath, outputPath, bof);
                }
                else if (useMultiExporting && Exporting.IsSuitableForNomadObjectTemplatesMultiExport(bof))
                {
                    Exporting.MultiExportNomadObjectTemplates(basePath, outputPath, bof);
                }
                else
                {
                    Exporting.Export(outputPath, bof);
                }
            }
        }