Esempio n. 1
0
        /// <summary>
        /// Imports collection by reading its data from a <see cref="BinaryReader"/> provided.
        /// </summary>
        /// <param name="type"><see cref="SerializeType"/> type of importing collection.</param>
        /// <param name="br"><see cref="BinaryReader"/> to read data with.</param>
        public override void Import(SerializeType type, BinaryReader br)
        {
            var position = br.BaseStream.Position;
            var header   = new SerializationHeader();

            header.Read(br);

            if (header.ID != BinBlockID.Nikki)
            {
                throw new Exception($"Missing serialized header in the imported collection");
            }

            if (header.Game != this.GameINT)
            {
                throw new Exception($"Stated game inside collection is {header.Game}, while should be {this.GameINT}");
            }

            var manager = this.GetManager(header.Name);

            if (manager == null)
            {
                throw new Exception($"Cannot find manager named {header.Name}");
            }

            br.BaseStream.Position = position;
            manager.Import(type, br);
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes instance into a byte array and stores it in the file provided.
        /// </summary>
        /// <param name="bw"><see cref="BinaryWriter"/> to write data with.</param>
        public override void Serialize(BinaryWriter bw)
        {
            byte[] array;
            var    size = this._data.Length + (this.FEngColorCount << 3) + this.CollectionName.Length + 0x20;

            using (var ms = new MemoryStream(size))
                using (var writer = new BinaryWriter(ms))
                {
                    writer.WriteNullTermUTF8(this.CollectionName);
                    writer.Write(this.FEngColorCount);

                    foreach (var color in this._colorinfo)
                    {
                        writer.Write(color.Offset);
                        writer.Write(color.Alpha);
                        writer.Write(color.Red);
                        writer.Write(color.Green);
                        writer.Write(color.Blue);
                    }

                    writer.Write(this._data.Length);
                    writer.Write(this._data);

                    array = ms.ToArray();
                }

            array = Interop.Compress(array, LZCompressionType.RAWW);

            var header = new SerializationHeader(array.Length, this.GameINT, this.Manager.Name);

            header.Write(bw);
            bw.Write(array.Length);
            bw.Write(array);
        }
Esempio n. 3
0
        /// <summary>
        /// Imports collection from file provided and attempts to add it to the end of
        /// this <see cref="Manager{T}"/> in case it does not exist.
        /// </summary>
        /// <param name="type">Type of serialization of a collection.</param>
        /// <param name="br"><see cref="BinaryReader"/> to read data with.</param>
        public override void Import(SerializeType type, BinaryReader br)
        {
            var position = br.BaseStream.Position;
            var header   = new SerializationHeader();

            header.Read(br);

            var collection = new FNGroup();

            if (header.ID != BinBlockID.Nikki)
            {
                br.BaseStream.Position = position;
                collection.Disassemble(br);
            }
            else
            {
                if (header.Game != this.GameINT)
                {
                    throw new Exception($"Stated game inside collection is {header.Game}, while should be {this.GameINT}");
                }

                if (header.Name != this.Name)
                {
                    throw new Exception($"Imported collection is not a collection of type {this.Name}");
                }

                collection.Deserialize(br);
            }

            var index = this.IndexOf(collection);

            if (index == -1)
            {
                // Allow import of FNGroups because it is safe
                this._is_read_only = false;
                ++this.Capacity;
                collection.Manager = this;
                this.Add(collection);
                this._is_read_only = true;
            }
            else
            {
                switch (type)
                {
                case SerializeType.Negate:
                    break;

                case SerializeType.Synchronize:
                case SerializeType.Override:
                    collection.Manager = this;
                    this.Replace(collection, index);
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Imports collection from file provided and attempts to add it to the end of
        /// this <see cref="Manager{T}"/> in case it does not exist.
        /// </summary>
        /// <param name="type">Type of serialization of a collection.</param>
        /// <param name="br"><see cref="BinaryReader"/> to read data with.</param>
        public override void Import(SerializeType type, BinaryReader br)
        {
            var position = br.BaseStream.Position;
            var header   = new SerializationHeader();

            header.Read(br);

            var collection = new DBModelPart();

            if (header.ID != BinBlockID.Nikki)
            {
                throw new Exception($"Missing serialized header in the imported collection");
            }
            else
            {
                if (header.Game != this.GameINT)
                {
                    throw new Exception($"Stated game inside collection is {header.Game}, while should be {this.GameINT}");
                }

                if (header.Name != this.Name)
                {
                    throw new Exception($"Imported collection is not a collection of type {this.Name}");
                }

                collection.Deserialize(br);
            }

            var index = this.IndexOf(collection);

            if (index == -1)
            {
                collection.Manager = this;
                this.Add(collection);
            }
            else
            {
                switch (type)
                {
                case SerializeType.Negate:
                    break;

                case SerializeType.Override:
                    collection.Manager = this;
                    this.Replace(collection, index);
                    break;

                case SerializeType.Synchronize:
                    this[index].Synchronize(collection);
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 5
0
        public void FromToBytes()
        {
            // Arrange
            var version = new ProtocolVersion(1, 0);
            var name    = "Something";

            // Act
            var header  = new SerializationHeader(version, name);
            var bytes   = header.ToBytes();
            var header2 = SerializationHeader.FromBytes(bytes);

            // Assert
            Assert.That(header2.Protocol, Is.EqualTo(version));
            Assert.That(header2.InvariantName, Is.EqualTo(name));
        }
Esempio n. 6
0
        /// <summary>
        /// Extracts a response from the <see cref="NetMQMessage"/>
        /// </summary>
        /// <param name="message"><see cref="NetMQMessage"/> wrapping a response object</param>
        /// <returns>Request identifier, response object contained within the <see cref="NetMQMessage"/></returns>
        public (int requestId, object response) ExtractResponse(NetMQMessage message)
        {
            var requestId = message[1].ConvertToInt32();
            var data      = message[3].ToByteArray();
            var header    = SerializationHeader.FromBytes(data);

            if (!serializerCache.SerializerFor(header.InvariantName, out var serializer))
            {
                throw new MissingSerializerException(header.InvariantName);
            }

            var package  = serializer.Deserialize <Package>(data, header.EncodedLength);
            var response = packageFactory.Unpack(package);

            return(requestId, response);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a <see cref="NetMQMessage"/> wrapping a request object
        /// </summary>
        /// <param name="request">Request object to be wrapped in a <see cref="NetMQMessage"/></param>
        /// <param name="requestId">An <see cref="int"/> identifier for matching asynchronous requests and responses</param>
        /// <returns><see cref="NetMQMessage"/> wrapping the request object</returns>
        public NetMQMessage CreateRequestMessage(object request, int requestId)
        {
            var serializer = serializerCache.DefaultSerializer;
            var package    = packageFactory.Pack(request);
            var header     = new SerializationHeader(new ProtocolVersion(1, 0), serializer.Descriptor.InvariantName);

            var data = serializer.Serialize(package, header.EncodedLength);

            header.ToBytes(data);

            var message = new NetMQMessage(4);

            message.AppendEmptyFrame();
            message.Append(requestId);
            message.AppendEmptyFrame();
            message.Append(data);
            return(message);
        }
Esempio n. 8
0
        /// <summary>
        /// Extracts a request from the <see cref="NetMQMessage"/>
        /// </summary>
        /// <param name="message"><see cref="NetMQMessage"/> wrapping a request object</param>
        /// <returns>Request object contained within the <see cref="NetMQMessage"/>, address of remote sender, and request identifier</returns>
        public (object request, byte[] address, int requestId, string serializationName) ExtractRequest(NetMQMessage message)
        {
            var address   = message[0].ToByteArray();
            var requestId = message[2].ConvertToInt32();

            var data   = message[4].ToByteArray();
            var header = SerializationHeader.FromBytes(data);

            if (!serializerCache.SerializerFor(header.InvariantName, out var serializer))
            {
                throw new MissingSerializerException(header.InvariantName);
            }

            var package = serializer.Deserialize <Package>(data, header.EncodedLength);
            var request = packageFactory.Unpack(package);

            return(request, address, requestId, header.InvariantName);
        }
Esempio n. 9
0
        /// <summary>
        /// Creates a <see cref="NetMQMessage"/> wrapping a response object
        /// </summary>
        /// <param name="response">Response object to be wrapped in a <see cref="NetMQMessage"/></param>
        /// <param name="address">Address of the remote</param>
        /// <param name="requestId">An <see cref="int"/> identifier for matching asynchronous requests and responses</param>
        /// <param name="serializerName"><see cref="SerializationDescriptor"/> of the <see cref="ISerializer"/> to use</param>
        /// <returns><see cref="NetMQMessage"/> wrapping the response object</returns>
        public NetMQMessage CreateResponseMessage(object response, byte[] address, int requestId, string serializerName)
        {
            if (!serializerCache.SerializerFor(serializerName, out var serializer))
            {
                throw new MissingSerializerException(serializerName);
            }

            var package = packageFactory.Pack(response);
            var header  = new SerializationHeader(new ProtocolVersion(1, 0), serializerName);

            var data = serializer.Serialize(package, header.EncodedLength);

            header.ToBytes(data);

            var message = new NetMQMessage(5);

            message.Append(address);
            message.AppendEmptyFrame();
            message.Append(requestId);
            message.AppendEmptyFrame();
            message.Append(data);
            return(message);
        }
Esempio n. 10
0
        /// <summary>
        /// Imports collection from file provided and attempts to add it to the end of
        /// this <see cref="Manager{T}"/> in case it does not exist.
        /// </summary>
        /// <param name="type">Type of serialization of a collection.</param>
        /// <param name="br"><see cref="BinaryReader"/> to read data with.</param>
        public override void Import(SerializeType type, BinaryReader br)
        {
            var position = br.BaseStream.Position;
            var header   = new SerializationHeader();

            header.Read(br);

            var collection = new TPKBlock();

            if (header.ID != BinBlockID.Nikki)
            {
                br.BaseStream.Position = position;

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    var offset = br.BaseStream.Position;
                    var id     = br.ReadEnum <BinBlockID>();
                    var size   = br.ReadInt32();

                    br.BaseStream.Position = offset;

                    if (id == BinBlockID.EmitterTexturePage)
                    {
                        collection.ReadTexturePages(br);
                    }

                    if (id == BinBlockID.TPKBlocks)
                    {
                        collection.Disassemble(br);
                        break;
                    }

                    br.BaseStream.Position = offset + size + 8;
                }
            }
            else
            {
                if (header.Game != this.GameINT)
                {
                    throw new Exception($"Stated game inside collection is {header.Game}, while should be {this.GameINT}");
                }

                if (header.Name != this.Name)
                {
                    throw new Exception($"Imported collection is not a collection of type {this.Name}");
                }

                collection.Deserialize(br);
            }

            var index = this.IndexOf(collection);

            if (index == -1)
            {
                collection.Manager = this;
                this.Add(collection);
            }
            else
            {
                switch (type)
                {
                case SerializeType.Negate:
                    break;

                case SerializeType.Override:
                    collection.Manager = this;
                    this.Replace(collection, index);
                    break;

                case SerializeType.Synchronize:
                    this[index].Synchronize(collection);
                    break;

                default:
                    break;
                }
            }
        }