示例#1
0
        static void CompileFile(string inputPath, string outputPath)
        {
            outputPath = String.IsNullOrEmpty(outputPath) ? inputPath : outputPath;

            outputPath = Path.ChangeExtension(outputPath, ".MBIN");

            var data = EXmlFile.ReadTemplate(inputPath);

            if (data == null)
            {
                Console.WriteLine("Failed to deserialize EXML file, is it formatted correctly?");
                return;
            }

            if (data.GetType() == typeof(TkGeometryData))
            {
                outputPath = Path.ChangeExtension(outputPath, ".MBIN.PC");
            }

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

            using (var file = new MBINFile(outputPath)) {
                file.Header = new MBINHeader();
                file.Header.SetDefaults();
                if (data.GetType() == typeof(TkGeometryData))
                {
                    file.Header.Magic = 0xDDDDDDDD; // only used by TkGeometryData / .MBIN.PC files, maybe used to signal the file is PC only?
                }
                if (data.GetType() == typeof(TkAnimMetadata))
                {
                    file.Header.Tag         = 0xFFFFFFFFFFFFFFFF;
                    file.Header.MbinVersion = 0x9B251350AE1ABCA7;
                    file.Header.EndPadding  = 0xFEFEFEFEFEFEFEFE;
                }

                file.SetData(data);         // this will also get the length of the data

                if (data.GetType() != typeof(TkAnimMetadata))
                {
                    file.Header.EndPadding = file.FileLength;
                }

                file.Save();
            }

            Console.WriteLine($"MBIN data written to \"{outputPath}\" successfully?");
        }
示例#2
0
        static void CompileFile(string input, string output)
        {
            if (String.IsNullOrEmpty(output))
            {
                output = input;
            }

            output = Path.ChangeExtension(output, ".MBIN");

            var data = EXmlFile.ReadTemplate(input);

            if (data == null)
            {
                Console.WriteLine("Failed to deserialize EXML file, is it formatted correctly?");
                return;
            }

            if (data.GetType() == typeof(Models.Structs.TkGeometryData))
            {
                output = Path.ChangeExtension(output, ".MBIN.PC");
            }

            if (File.Exists(output))
            {
                File.Delete(output); // todo: ask for confirmation?
            }
            using (var file = new MBINFile(output))
            {
                file.Header = new Models.MBINHeader();
                file.Header.SetDefaults();
                if (data.GetType() == typeof(Models.Structs.TkGeometryData))
                {
                    file.Header.Magic = 0xDDDDDDDD; // only used by TkGeometryData / .MBIN.PC files, maybe used to signal the file is PC only?
                }
                file.SetData(data);
                file.Save();
            }

            Console.WriteLine($"MBIN data written to \"{output}\" 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);
            }
        }