Пример #1
0
        static void DecompileFile(string input, string output)
        {
            if (String.IsNullOrEmpty(output))
            {
                output = Path.ChangeExtension(input, ".exml"); // emoose XML, because there's no way this XML format is compatible with MXML
            }
            if (File.Exists(output))
            {
                File.Delete(output); // todo: ask for confirmation?
            }
            // no error checking ^^ (todo: error checking)
            var file = new MBINFile(input);

            file.Load();

            var data = file.GetData();

            if (data == null)
            {
                Console.WriteLine($"Failed to deserialize template \"{file.Header.GetXMLTemplateName()}\", has the structure been mapped yet?");
                return;
            }

            var xmlString = EXmlFile.WriteTemplate(data);

            if (string.IsNullOrEmpty(xmlString))
            {
                Console.WriteLine($"Error serializing template \"{file.Header.GetXMLTemplateName()}\" to XML!");
                return;
            }

            File.WriteAllText(output, xmlString);
            Console.WriteLine($"XML data written to \"{output}\" successfully?");
        }
Пример #2
0
        static void DecompileFile(string inputPath, string outputPath, bool getVersion = false, bool verbose = false)
        {
            outputPath = String.IsNullOrEmpty(outputPath) ? inputPath : outputPath;

            if (Path.GetExtension(outputPath) == ".PC")
            {
                outputPath = Path.GetFileNameWithoutExtension(outputPath); // remove the ".PC"
            }
            outputPath = Path.ChangeExtension(outputPath, ".exml");        // emoose XML, because there's no way this XML format is compatible with MXML

            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);   // todo: ask for confirmation?
            }

            // no error checking ^^ (todo: error checking)
            var file = new MBINFile(inputPath);

            file.Load( );

            if (getVersion)
            {
                ShowMBINVersion(file, verbose);
            }
            else
            {
                var data = file.GetData();
                if (data == null)
                {
                    Console.WriteLine($"Failed to deserialize template \"{file.Header.GetXMLTemplateName()}\", has the structure been mapped yet?");
                    return;
                }

                var xmlString = EXmlFile.WriteTemplate(data);
                if (string.IsNullOrEmpty(xmlString))
                {
                    Console.WriteLine($"Error serializing template \"{file.Header.GetXMLTemplateName()}\" to XML!");
                    return;
                }
                File.WriteAllText(outputPath, xmlString);
                Console.WriteLine($"XML data written to \"{outputPath}\" successfully?");
            }
        }
Пример #3
0
        public static void ConvertFile(string fileIn, string fileOut, FormatType inputFormat, FormatType outputFormat)
        {
            fileOut = ChangeFileExtension(fileOut, outputFormat);

            FileMode fileMode = GetFileMode(fileOut);

            Directory.CreateDirectory(Path.GetDirectoryName(fileOut));

            try {
                using (var indentScope = new Logger.IndentScope())
                    using (var fIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read))
                        using (var ms = new MemoryStream()) {
                            if (inputFormat == FormatType.MBIN)
                            {
                                var mbin = new MBINFile(fIn);
                                if (!mbin.Load() || !mbin.Header.IsValid)
                                {
                                    throw new InvalidDataException("Not a valid MBIN file!");
                                }

                                var sw = new StreamWriter(ms);

                                NMSTemplate data = null;
                                try {
                                    data = mbin.GetData();
                                    if (data is null)
                                    {
                                        throw new InvalidDataException("Invalid MBIN data.");
                                    }
                                } catch (Exception e) {
                                    throw new MbinException($"Failed to read {mbin.Header.GetXMLTemplateName()} from MBIN.", e, fileIn, mbin);
                                }

                                try {
                                    sw.Write(EXmlFile.WriteTemplate(data));
                                    sw.Flush();
                                    if (ms.Length == 0)
                                    {
                                        throw new InvalidDataException("Invalid EXML data.");
                                    }
                                } catch (Exception e) {
                                    throw new MbinException($"Failed serializing {mbin.Header.GetXMLTemplateName()} to EXML.", e, fileIn, mbin);
                                }
                            }
                            else if (inputFormat == FormatType.EXML)
                            {
                                NMSTemplate data = null;
                                try {
                                    data = EXmlFile.ReadTemplateFromStream(fIn);
                                    if (data is null)
                                    {
                                        throw new InvalidDataException($"Failed to deserialize EXML.");
                                    }
                                    if (data is TkGeometryData)
                                    {
                                        fileOut += ".PC";
                                    }
                                    var mbin = new MBINFile(ms)
                                    {
                                        Header = new MBINHeader()
                                    };
                                    mbin.Header.SetDefaults(data.GetType());
                                    mbin.SetData(data);
                                    mbin.Save();
                                } catch (Exception e) {
                                    throw new ExmlException(e, fileIn, data);
                                }
                            }

                            ms.Flush();
                            using (var fOut = new FileStream(fileOut, fileMode, FileAccess.Write)) ms.WriteTo(fOut);
                        }
            } catch (Exception e) {
                File.Delete(fileOut);
                if (e is CompilerException)
                {
                    throw;
                }
                throw new CompilerException(e, fileIn);
            }
        }