コード例 #1
0
        public XnbFile <T> Deserialize <T>(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var xnbResult = new XnbFile <T>();

            var xnbStreamReader = new XnbStreamReader(stream);

            // check if file starts with XNB
            ValidatePrefix(xnbStreamReader);

            // read the XNB target OS
            xnbResult.XnbTargetOs = ReadTargetOs(xnbStreamReader);

            // read the XNB format version
            xnbResult.FormatVersion = ReadFormatVersion(xnbStreamReader);

            // read the XNB flag
            var flags = xnbResult.Flags = ReadFlags(xnbStreamReader);

            // read the file size
            var size = xnbResult.Size = (int)xnbStreamReader.ReadUInt32();

            // check if encoded. If so, decode stream
            xnbStreamReader = DecodeXnbStreamReader(xnbStreamReader, flags, size);

            var xnbReaderManager = new XnbReaderManager();

            xnbReaderManager.ReadReaders(xnbStreamReader);

            // ensure there are no shared resources
            var sharedResourcesCount = xnbStreamReader.Read7BitEncodedInt();

            if (sharedResourcesCount != 0)
            {
                throw new NotSupportedException("Shared resources are not supported");
            }

            int readerIndex = xnbStreamReader.Read7BitEncodedInt();
            var reader      = xnbReaderManager.XnbReaderFromIndex(readerIndex);

            if (reader == null)
            {
                xnbResult.Content = default(T);
            }
            else if (reader.Read(xnbReaderManager, xnbStreamReader) is T content)
            {
                xnbResult.Content = content;
            }
            else
            {
                throw new XnbException("Invalid type");
            }

            return(xnbResult);
        }
コード例 #2
0
 public abstract void Write(XnbReaderManager xnbReaderManager, XnbStreamWriter xnbStreamWriter, object value);
コード例 #3
0
 public abstract object Read(XnbReaderManager xnbReaderManager, XnbStreamReader xnbStreamReader);
コード例 #4
0
        public byte[] Serialize <T>(T value,
                                    XnbTargetOs xnbTargetOs = XnbTargetOs.MicrosoftWindow,
                                    byte formatVersion      = 5,
                                    XnbFlags flags          = 0
                                    )
        {
            var xnbStreamWriter = new XnbStreamWriter(new MemoryStream());

            // write XNB default header
            xnbStreamWriter.Write('X');
            xnbStreamWriter.Write('N');
            xnbStreamWriter.Write('B');

            // write XNB target OS
            xnbStreamWriter.Write((byte)xnbTargetOs);

            // write XNB format version
            xnbStreamWriter.Write(formatVersion);

            // write XNB flags
            // remove compression flag
            flags = flags & ~XnbFlags.ContentCompressedLzx;
            xnbStreamWriter.Write((byte)flags);

            //tmp file size
            long sizeIndex = xnbStreamWriter.BaseStream.Position;

            xnbStreamWriter.Write(0);

            //TODO: encoding here

            Type          readerType       = XnbTypeReaderTypeResolver.ResolveFromTargetType <T>();
            XnbTypeReader reader           = XnbReaderFactory.CreateReaderFromType(readerType);
            var           xnbReaderManager = new XnbReaderManager(reader);
            var           tmpStream        = new XnbStreamWriter(new MemoryStream());

            reader.Write(xnbReaderManager, tmpStream, value);
            List <XnbTypeReader> xnbTypeReaders = xnbReaderManager.XnbTypeReaders;

            // write reader count
            xnbStreamWriter.Write7BitEncodedInt(xnbTypeReaders.Count);

            // write readers and version
            foreach (XnbTypeReader xnbTypeReader in xnbTypeReaders)
            {
                xnbStreamWriter.Write(xnbTypeReader.Name);
                xnbStreamWriter.Write(xnbTypeReader.Version);
            }

            // no shared resources
            xnbStreamWriter.Write7BitEncodedInt(0);

            // write initial reader index
            xnbStreamWriter.Write7BitEncodedInt(1);

            tmpStream.BaseStream.Position = 0;
            tmpStream.BaseStream.CopyTo(xnbStreamWriter.BaseStream);

            xnbStreamWriter.BaseStream.Position = sizeIndex;
            xnbStreamWriter.Write((int)xnbStreamWriter.BaseStream.Length);

            using (var ms = new MemoryStream())
            {
                xnbStreamWriter.BaseStream.Position = 0;
                xnbStreamWriter.BaseStream.CopyTo(ms);
                return(ms.ToArray());
            }
        }