Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="KbinReader"/> class.
        /// </summary>
        /// <param name="buffer">An array of bytes containing the contents of a binary XML.</param>
        public KbinReader(byte[] buffer)
        {
            //Read header section.
            var binaryBuffer    = new BigEndianBinaryBuffer(buffer);
            var signature       = binaryBuffer.ReadU8();
            var compressionFlag = binaryBuffer.ReadU8();
            var encodingFlag    = binaryBuffer.ReadU8();
            var encodingFlagNot = binaryBuffer.ReadU8();

            //Verify magic.
            if (signature != 0xA0)
            {
                throw new KbinException($"Signature was invalid. 0x{signature.ToString("X2")} != 0xA0");
            }

            //Encoding flag should be an inverse of the fourth byte.
            if ((byte)~encodingFlag != encodingFlagNot)
            {
                throw new KbinException($"Third byte was not an inverse of the fourth. {~encodingFlag} != {encodingFlagNot}");
            }

            var compressed = compressionFlag == 0x42;

            Encoding = EncodingDictionary.EncodingMap[encodingFlag];

            //Get buffer lengths and load.
            var span       = new Span <byte>(buffer);
            var nodeLength = binaryBuffer.ReadS32();

            _nodeBuffer = new NodeBuffer(span.Slice(8, nodeLength).ToArray(), compressed, Encoding);

            var dataLength = BitConverterHelper.GetBigEndianInt32(span.Slice(nodeLength + 8, 4));

            _dataBuffer = new DataBuffer(span.Slice(nodeLength + 12, dataLength).ToArray(), Encoding);

            var settings = new XmlWriterSettings
            {
                Async    = false,
                Encoding = Encoding,
                Indent   = false
            };

            _writerStream = new MemoryStream();
            _xmlWriter    = XmlWriter.Create(_writerStream, settings);
            _xmlWriter.WriteStartDocument();
        }
Exemple #2
0
 public static string DoubleToString(Span <byte> bytes) => BitConverterHelper.GetBigEndianDouble(bytes).ToString("0.000000");
Exemple #3
0
 public static string SingleToStringWithoutCopy(Span <byte> bytes) => BitConverterHelper.GetBigEndianSingleWithoutCopy(bytes).ToString("0.000000");
Exemple #4
0
 public static string S64ToString(Span <byte> bytes) => BitConverterHelper.GetBigEndianInt64(bytes).ToString();
Exemple #5
0
 public static string U32ToString(Span <byte> bytes) => BitConverterHelper.GetBigEndianUInt32(bytes).ToString();
Exemple #6
0
        public static Span <byte> DoubleToBytes(ReadOnlySpan <char> input) => BitConverterHelper.GetBigEndianBytes(double.Parse(input
#if NETSTANDARD2_0
                                                                                                                                .ToString()
#endif
                                                                                                                                ));
Exemple #7
0
        public static Span <byte> S64ToBytes(ReadOnlySpan <char> str) => BitConverterHelper.GetBigEndianBytes(long.Parse(str
#if NETSTANDARD2_0
                                                                                                                         .ToString()
#endif
                                                                                                                         ));