示例#1
0
        static void ExtractMBinFile(FileInfo inputFile, string outputFileName)
        {
            using var mbinFile   = new MBINFile(inputFile.FullName);
            using var outputFile = File.Create(outputFileName);
            using var sw         = new StreamWriter(outputFile);

            if (!(mbinFile.Load() && mbinFile.Header.IsValid))
            {
                throw new InvalidDataException("Invalid File");
            }

            var type = NMSTemplate.GetTemplateType(mbinFile.Header.GetXMLTemplateName());

            if (type == null)
            {
                throw new NullReferenceException();
            }

            var nms = (NMSAttribute)(type.GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null);

            var broken = nms.Broken;

            // GUID's for the old files
            ulong[] UnsupportedGUIDs = new ulong[] { };
            var     mismatch         = (mbinFile.Header.TemplateGUID != nms.GUID);
            bool    unsupported      = (UnsupportedGUIDs.Contains(mbinFile.Header.TemplateGUID));

            if (broken)
            {
                var previousColour = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("File is broken");
                Console.ForegroundColor = previousColour;
            }
            else if (unsupported)
            {
                var previousColour = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("File is Unsupported");
                Console.ForegroundColor = previousColour;
            }
            else if (mismatch)
            {
                var previousColour = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("File is Mismatched");
                Console.ForegroundColor = previousColour;
            }

            var data = mbinFile.GetData();

            sw.Write(EXmlFile.WriteTemplate(data));
            sw.Flush();
        }
示例#2
0
        /// <summary>Convert EXML to MBIN</summary>
        /// <param name="fIn">Source file</param>
        /// <param name="msOut">Output stream</param>
        /// <param name="fileOut">Output file path. Passed through as the return value. For geometry files, ".PC" will be appended.</param>
        /// <returns>fileOut</returns>
        private static string ConvertEXML(string inputPath, FileStream fIn, MemoryStream msOut, string fileOut)
        {
            string      templateName;
            NMSTemplate data = null;

            try {
                data = EXmlFile.ReadTemplateFromStream(fIn, out templateName);

                Type type = NMSTemplate.GetTemplateType(templateName);
                var  nms  = (NMSAttribute)(data.GetType().GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null);
                if (nms.Broken)
                {
                    FileIsBroken(inputPath, data);
                }

                if (data is null)
                {
                    throw new InvalidDataException($"Failed to deserialize EXML.");
                }
                if (data is libMBIN.NMS.Toolkit.TkGeometryData | data is libMBIN.NMS.Toolkit.TkGeometryStreamData)
                {
                    fileOut += ".PC";
                }

                var mbin = new MBINFile(msOut)
                {
                    Header = new MBINHeader()
                };
                mbin.Header.SetDefaults(data.GetType(), FormatVersion);
                mbin.SetData(data);
                mbin.Save();
            } catch (Exception e) {
                throw new ExmlException(e, fIn.Name, data);
            }

            return(fileOut);
        }
示例#3
0
        /// <summary>Convert MBIN to EXML</summary>
        /// <param name="fIn">Source file</param>
        /// <param name="msOut">Output stream</param>
        /// <param name="fileOut">Output file path. Passed through as the return value. Not actually used.</param>
        /// <returns>fileOut</returns>
        private static string ConvertMBIN(string inputPath, FileStream fIn, MemoryStream msOut, string fileOut)
        {
            var mbin = new MBINFile(fIn);

            if (!(mbin.Load() && mbin.Header.IsValid))
            {
                throw new InvalidDataException("Not a valid MBIN file!");
            }

            var type   = NMSTemplate.GetTemplateType(mbin.Header.GetXMLTemplateName());
            var nms    = (NMSAttribute)(type.GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null);
            var broken = nms.Broken;

            // GUID's for the old files
            ulong[] UnsupportedGUIDs = new ulong[] { };
            var     mismatch         = (mbin.Header.TemplateGUID != nms.GUID);
            bool    unsupported      = (UnsupportedGUIDs.Contains(mbin.Header.TemplateGUID));

            //if ( broken && mismatch ) {
            //    FileIsUnsupported( fIn.Name, mbin );
            //} else
            if (broken)
            {
                FileIsBroken(inputPath, mbin);
            }
            else if (unsupported)
            {
                FileIsUnused(inputPath, mbin);
            }
            else if (mismatch)
            {
                FileIsUnrecognized(inputPath, mbin, nms.GUID);
            }

            var sw = new StreamWriter(msOut);

            NMSTemplate data = null;
            string      msg  = "";

            try {
                msg  = $"Failed to read {mbin.Header.GetXMLTemplateName()} from MBIN.";
                data = mbin.GetData();
                if (data is null)
                {
                    throw new InvalidDataException("Invalid MBIN data.");
                }

                msg = $"Failed serializing {mbin.Header.GetXMLTemplateName()} to EXML.";
                string exml = EXmlFile.WriteTemplate(data, HideVersionInfo);

                if (StreamToConsole)
                {
                    EmitInfo($"");
                    EmitInfo($"[INPUT]: {inputPath}");
                    EmitInfo($"{exml}");
                }
                else
                {
                    sw.Write(exml);
                    sw.Flush();
                    if (msOut.Length == 0)
                    {
                        throw new InvalidDataException("Invalid EXML data.");
                    }
                }
            } catch (Exception e) {
                throw new MbinException(msg, e, fIn.Name, mbin);
            }

            return(fileOut);
        }