static void Convert(RootCommandOptions options)
        {
            foreach (var file in options.Files)
            {
                var filePath = file.ToString(); // Returns the original path supplied to FileInfo.

                // Convert from XML
                if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    // XML to MTX
                    if (string.Equals(options.Format, "mtx", StringComparison.OrdinalIgnoreCase) ||
                        (options.Format is null && filePath.EndsWith(".mtx.xml", StringComparison.OrdinalIgnoreCase)))
                    {
                        var destinationPath = options.Output is not null
                            ? options.Output.FullName
                            : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension.

                        MtxEncoding encoding;
                        if (options.Fpd is not null)
                        {
                            var fpdFile = new FpdFile(options.Fpd.ToString());
                            encoding = new CharacterMapMtxEncoding(fpdFile.Characters);
                        }
                        else if (options.Fnt is not null)
                        {
                            var fntFile = new FntFile(options.Fnt.ToString());
                            encoding = new CharacterMapMtxEncoding(fntFile.Characters);
                        }
                        else
                        {
                            encoding = new Utf16MtxEncoding();
                        }

                        var serializedSource   = File.ReadAllText(filePath);
                        var deserializedSource = Utf8XmlSerializer.Deserialize <MtxSerializable>(serializedSource)
                                                 ?? throw new NullValueException();
                        var destination = new MtxFile(deserializedSource, encoding);

                        destination.Save(destinationPath);
                    }

                    // XML to CNVRS-TEXT
                    else if (string.Equals(options.Format, "cnvrs-text", StringComparison.OrdinalIgnoreCase) ||
                             (options.Format is null && filePath.EndsWith(".cnvrs-text.xml", StringComparison.OrdinalIgnoreCase)))
                    {
                        var destinationPath = options.Output is not null
                            ? options.Output.FullName
                            : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension.

                        var serializedSource   = File.ReadAllText(filePath);
                        var deserializedSource = Utf8XmlSerializer.Deserialize <CnvrsTextSerializable>(serializedSource)
                                                 ?? throw new NullValueException();
                        var destination = new CnvrsTextFile(deserializedSource);

                        destination.Save(destinationPath);
                    }
                }
Beispiel #2
0
        public void OnExecute()
        {
            // Don't execute if we are showing help.
            if (ShowHelp)
            {
                return;
            }

            MtxEncoding encoding = null;
            var         version  = Version ?? "auto";

            if (version == "auto")
            {
                if (FntPath != null || FpdPath != null)
                {
                    version = "1";
                }
                else
                {
                    version = "2";
                }
            }
            if (version == "1")
            {
                if (FntPath != null)
                {
                    if (!File.Exists(FntPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FntPath));
                        return;
                    }

                    var fntFile = new FntFile(FntPath);
                    encoding = new CharacterMapMtxEncoding(fntFile.Characters);
                }
                else if (FpdPath != null)
                {
                    if (!File.Exists(FpdPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FpdPath));
                        return;
                    }

                    var fpdFile = new FpdFile(FpdPath);
                    encoding = new CharacterMapMtxEncoding(fpdFile.Characters);
                }
                else
                {
                    Error(ErrorMessages.FntOrFpdOptionMustBeSet);
                    return;
                }
            }
            else if (version == "2")
            {
                encoding = new Utf16MtxEncoding();
            }

            foreach (var file in Files)
            {
                if (!File.Exists(file))
                {
                    Error(string.Format(ErrorMessages.FileDoesNotExist, file));
                    continue;
                }

                var format = Format ?? "auto";
                if (format == "auto")
                {
                    var extension = Path.GetExtension(file);

                    if (extension.Equals(".mtx", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "json";
                    }
                    else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "mtx";
                    }
                    else
                    {
                        Error(string.Format(ErrorMessages.CouldNotDetermineOutputFormat, file));
                        continue;
                    }
                }

                if (format == "json")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".json");

                    try
                    {
                        var mtxFile = new MtxFile(file, encoding);
                        var mtxJson = new MtxJson
                        {
                            Has64BitOffsets = mtxFile.Has64BitOffsets,
                            Entries         = mtxFile.Entries,
                        };
                        JsonFileSerializer.Serialize(outputFilename, mtxJson);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
                else if (format == "mtx")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".mtx");

                    try
                    {
                        var mtxJson = JsonFileSerializer.Deserialize <MtxJson>(file);
                        var mtxFile = new MtxFile(mtxJson.Entries, encoding, mtxJson.Has64BitOffsets ? true : Has64BitOffsets);
                        mtxFile.Save(outputFilename);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
            }
        }