Example #1
0
        private Message ReadSingle(Bitstream stream)
        {
            var isCompressed = stream.ReadBool();

            if (isCompressed)
            {
                var uncompressed_length = stream.ReadBits(26);
                var length = stream.ReadBits(18);

                var data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message
                {
                    IsCompressed = false,
                    Data = new SnappyDecompressor().Decompress(data, 0, data.Length)
                });
            }
            else
            {
                var length = stream.ReadBits(18);

                var data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message
                {
                    IsCompressed = false,
                    Data = data
                });
            }
        }
Example #2
0
        private UpdateFlag ReadHeader(ref uint id, Bitstream stream)
        {
            var value = stream.ReadBits(6);

            if ((value & 0x30) > 0)
            {
                var a = (value >> 4) & 3;
                var b = (uint)((a == 3) ? 16 : 0);

                value = (stream.ReadBits((byte)(4 * a + b)) << 4) | (value & 0xF);
            }

            id = unchecked (id + value + 1);

            var flags = UpdateFlag.None;

            if (!stream.ReadBool())
            {
                if (stream.ReadBool())
                {
                    flags |= UpdateFlag.EnterPvs;
                }
            }
            else
            {
                flags |= UpdateFlag.LeavePvs;

                if (stream.ReadBool())
                {
                    flags |= UpdateFlag.Delete;
                }
            }

            return(flags);
        }
Example #3
0
        private void ReadChunkHeader(Bitstream stream)
        {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile)
            {
                var filenameLength = stream.ReadUInt32();
                var filename       = new byte[filenameLength + 1]; // semantically wrong. should be
                // 0x104
                stream.Read(filename, 0, (int)filenameLength);     // and then read to end of string
                filename[filenameLength] = 0;                      // whatever
                header.Filename          = Encoding.UTF8.GetString(filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed)
            {
                header.DecompressedLength = stream.ReadBits(26);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount =
                (header.ByteLength + DotaGameConnection.BYTES_PER_CHUNK - 1) /
                DotaGameConnection.BYTES_PER_CHUNK;

            Receiving     = true;
            dataIn        = new byte[header.ByteLength];
            dataReceived  = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #4
0
        private float UnpackFloatCoord(Bitstream stream)
        {
            bool hasInteger  = stream.ReadBool();
            bool hasFraction = stream.ReadBool();

            if (hasInteger || hasFraction)
            {
                bool sign = stream.ReadBool();

                uint integer = 0;
                if (hasInteger)
                {
                    integer = stream.ReadBits(COORD_INTEGER_BITS) + 1;
                }

                uint fraction = 0;
                if (hasFraction)
                {
                    fraction = stream.ReadBits(COORD_FRACTIONAL_BITS);
                }

                float f = (float)(integer + fraction * COORD_RESOLUTION);

                if (sign)
                {
                    f *= -1;
                }

                return(f);
            }
            else
            {
                return(0);
            }
        }
Example #5
0
        private Nullable <Message> ReadChunk(Bitstream stream)
        {
            uint offset = stream.ReadBits(18);
            uint count  = stream.ReadBits(3);

            log.DebugFormat("chunk start = {0} end = {1}", offset, count);

            if (offset == 0)
            {
                Preconditions.CheckArgument(!Receiving);
                ReadChunkHeader(stream);
            }
            else
            {
                Preconditions.CheckArgument(Receiving);
            }

            uint byteOffset = offset * Connection.BYTES_PER_CHUNK;

            uint byteCount;

            if (offset + count < header.ChunkCount)
            {
                byteCount = count * Connection.BYTES_PER_CHUNK;
            }
            else
            {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (uint i = offset;
                 i < offset + count;
                 ++i)
            {
                if (!dataReceived[i])
                {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount)
            {
                log.Debug("chunk complete");
                Receiving = false;
                return(new Message {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,

                    Data = dataIn,
                });
            }
            else
            {
                log.DebugFormat("chunk has {0}/{1}", countReceived, header.ChunkCount);
                return(null);
            }
        }
Example #6
0
        private UpdateFlag ReadHeader(ref uint id, Bitstream stream) {
            uint value = stream.ReadBits(6);

            if ((value & 0x30) > 0) {
                uint a = (value >> 4) & 3;
                uint b = (uint) ((a == 3) ? 16 : 0);

                value = (stream.ReadBits((byte) (4 * a + b)) << 4) | (value & 0xF);
            }

            id = unchecked(id + value + 1);

            var flags = UpdateFlag.None;

            if (!stream.ReadBool()) {
                if (stream.ReadBool()) {
                    flags |= UpdateFlag.EnterPvs;
                }
            } else {
                flags |= UpdateFlag.LeavePvs;

                if (stream.ReadBool()) {
                    flags |= UpdateFlag.Delete;
                }
            }

            return flags;
        }
Example #7
0
        private Message ReadSingle(Bitstream stream)
        {
            bool isCompressed = stream.ReadBool();

            if (isCompressed)
            {
                uint uncompressed_length = stream.ReadBits(26);
                uint length = stream.ReadBits(18);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message {
                    IsCompressed = false,
                    Data = Snappy.Sharp.Snappy.Uncompress(data)
                });
            }
            else
            {
                uint length = stream.ReadBits(18);
                Preconditions.CheckArgument(length < Int32.MaxValue);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message {
                    IsCompressed = false,
                    Data = data,
                });
            }
        }
Example #8
0
        private float UnpackFloatCellCoord(PropertyInfo info, Bitstream stream, FloatType type)
        {
            uint  value = stream.ReadBits(info.NumBits);
            float f     = value;

            if ((value >> 31) > 0)
            {
                f *= -1;
            }

            if (type == FloatType.None)
            {
                uint fraction = stream.ReadBits(5);

                return(f + 0.03125f * fraction);
            }
            else if (type == FloatType.LowPrecision)
            {
                uint fraction = stream.ReadBits(3);

                return(f + 0.125f * fraction);
            }
            else if (type == FloatType.Integral)
            {
                return(f);
            }
            else
            {
                throw new InvalidOperationException("Unknown float type");
            }
        }
Example #9
0
        private string ReadKeyIfIncluded(Bitstream stream, List <string> keyHistory)
        {
            var has_key = stream.ReadBool();

            if (!has_key)
            {
                return(null);
            }

            var is_substring = stream.ReadBool();

            string key;

            if (!is_substring)
            {
                key = stream.ReadString();
            }
            else
            {
                var fromIndex  = (int)stream.ReadBits(5);
                var fromLength = (int)stream.ReadBits(5);
                key = keyHistory[fromIndex].Substring(0, fromLength);

                key += stream.ReadString();
            }

            if (keyHistory.Count == KEY_HISTORY_SIZE)
            {
                keyHistory.RemoveAt(0);
            }

            keyHistory.Add(key);

            return(key);
        }
Example #10
0
        private string ReadKeyIfIncluded(Bitstream stream, List<string> keyHistory) {
            bool has_key = stream.ReadBool();

            if (!has_key) {
                return null;
            } 

            bool is_substring = stream.ReadBool();

            string key;

            if (!is_substring) {
                key = stream.ReadString();
            } else {
                int fromIndex = (int) stream.ReadBits(5);
                int fromLength = (int) stream.ReadBits(5);
                key = keyHistory[fromIndex].Substring(0, fromLength);

                key += stream.ReadString();
            }

            Preconditions.CheckArgument(key.Length <= MAX_KEY_SIZE);

            if (keyHistory.Count == KEY_HISTORY_SIZE) {
                keyHistory.RemoveAt(0);
            }

            keyHistory.Add(key);

            return key;
        }
Example #11
0
        public ulong UnpackInt64(PropertyInfo info, Bitstream stream)
        {
            if (info.Flags.HasFlag(PropertyInfo.MultiFlag.EncodedAgainstTickcount))
            {
                return(stream.ReadVarUInt());
            }
            var negate     = false;
            var secondBits = (byte)(info.NumBits - 32);

            if (!info.Flags.HasFlag(PropertyInfo.MultiFlag.Unsigned))
            {
                --secondBits;

                if (stream.ReadBool())
                {
                    negate = true;
                }
            }

            ulong a     = stream.ReadBits(32);
            ulong b     = stream.ReadBits(secondBits);
            var   value = (b << 32) | a;

            if (negate)
            {
                value = unchecked ((ulong)((long)value * -1));
            }

            return(value);
        }
Example #12
0
        private void ReadEnterPvs(uint id, uint baseline, bool update_baseline, Bitstream stream)
        {
            var clazz  = state.Classes[(int)stream.ReadBits(ClassBitLength)];
            var serial = stream.ReadBits(10);

            Create(id, clazz, baseline);
            ReadAndUnpackFields(state.Slots[id].Entity, stream);

            if (update_baseline)
            {
                state.Slots[id].Baselines[1 - baseline] = state.Slots[id].Entity.Copy();
            }
        }
Example #13
0
        private void ReadEnterPvs(uint id, uint baseline, bool update_baseline, Bitstream stream)
        {
            var clazz  = client.Classes[(int)stream.ReadBits(this.ClassBitLength)];
            var serial = stream.ReadBits(10);

            log.Debug(String.Format("{0} entering pvs as a {1} (serial {2})", id, clazz.ClassName, serial));

            Create(id, clazz, baseline);
            ReadAndUnpackFields(client.Slots[id].Entity, stream);

            if (update_baseline)
            {
                client.Slots[id].Baselines[1 - baseline] = client.Slots[id].Entity.Copy();
            }
        }
Example #14
0
        private Message?ReadChunk(Bitstream stream)
        {
            var offset = stream.ReadBits(18);
            var count  = stream.ReadBits(3);

            if (offset == 0)
            {
                ReadChunkHeader(stream);
            }

            var byteOffset = offset * DotaGameConnection.BYTES_PER_CHUNK;

            uint byteCount;

            if (offset + count < header.ChunkCount)
            {
                byteCount = count * DotaGameConnection.BYTES_PER_CHUNK;
            }
            else
            {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (var i = offset;
                 i < offset + count;
                 ++i)
            {
                if (!dataReceived[i])
                {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount)
            {
                Receiving = false;
                return(new Message
                {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,
                    Data = dataIn
                });
            }
            return(null);
        }
Example #15
0
        public void Update(Bitstream stream, uint baseline, bool updateBaseline, uint updated, bool isDelta) {
            uint id = UInt32.MaxValue;
            uint found = 0;

            while (found < updated) {
                var flags = ReadHeader(ref id, stream);

                if (flags.HasFlag(UpdateFlag.EnterPvs)) {
                    ReadEnterPvs(id, baseline, updateBaseline, stream);
                } else if (flags.HasFlag(UpdateFlag.LeavePvs)) {
                    if (flags.HasFlag(UpdateFlag.Delete)) {
                        Delete(id);
                    }
                } else {
                    ReadUpdate(id, stream);
                }

                ++found;
            }

            if (isDelta) {
                while (stream.ReadBool()) {
                    id = stream.ReadBits(11);
                    Delete(id);
                }
            }
        }
Example #16
0
        public float UnpackFloat(PropertyInfo info, Bitstream stream) {
            var flags = info.Flags;

            if (flags.HasFlag(PropertyInfo.MultiFlag.Coord)) {
                return UnpackFloatCoord(stream);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMp)) {
                return UnpackFloatCoordMp(stream, FloatType.None);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMpLowPrecision)) {
                return UnpackFloatCoordMp(stream, FloatType.LowPrecision);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMpIntegral)) {
                return UnpackFloatCoordMp(stream, FloatType.Integral);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.NoScale)) {
                return UnpackFloatNoScale(stream);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.Normal)) {
                return UnpackFloatNormal(stream);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoord)) {
                return UnpackFloatCellCoord(info, stream, FloatType.None);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoordLowPrecision)) {
                return UnpackFloatCellCoord(info, stream, FloatType.LowPrecision);
            } else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoordIntegral)) {
                return UnpackFloatCellCoord(info, stream, FloatType.Integral);
            } else {
                uint dividend = stream.ReadBits(info.NumBits);
                uint divisor = (uint) (1 << info.NumBits) - 1;

                float f = ((float) dividend) / divisor;
                float range = info.HighValue - info.LowValue;

                return f * range + info.LowValue;
            }
        }
Example #17
0
        private byte[] ReadValueIfIncluded(Bitstream stream, bool userDataFixedSize,
                                           uint userDataSizeBits)
        {
            var has_value = stream.ReadBool();

            if (!has_value)
            {
                return(null);
            }

            uint length;
            uint bitLength;

            if (userDataFixedSize)
            {
                length    = (userDataSizeBits + 7) / 8;
                bitLength = userDataSizeBits;
            }
            else
            {
                length    = stream.ReadBits(14);
                bitLength = 8 * length;
            }

            return(stream.ReadManyBits(bitLength));
        }
Example #18
0
        private byte[] ReadValueIfIncluded(Bitstream stream, bool userDataFixedSize,
                                           uint userDataSizeBits)
        {
            bool has_value = stream.ReadBool();

            if (!has_value)
            {
                return(null);
            }

            uint length;
            uint bitLength;

            if (userDataFixedSize)
            {
                length    = (userDataSizeBits + 7) / 8;
                bitLength = userDataSizeBits;
            }
            else
            {
                length    = stream.ReadBits(14);
                bitLength = 8 * length;
            }

            Preconditions.CheckArgument(length <= MAX_VALUE_SIZE);

            return(stream.ReadManyBits(bitLength));
        }
Example #19
0
        public uint UnpackInt(PropertyInfo info, Bitstream stream)
        {
            var flags = info.Flags;

            if (flags.HasFlag(PropertyInfo.MultiFlag.EncodedAgainstTickcount))
            {
                if (flags.HasFlag(PropertyInfo.MultiFlag.Unsigned))
                {
                    return(stream.ReadVarUInt());
                }
                else
                {
                    uint value = stream.ReadVarUInt();
                    return(unchecked ((uint)((-(value & 1)) ^ (value >> 1))));
                }
            }
            else
            {
                byte numBits = info.NumBits;

                uint isUnsigned = Convert.ToUInt32(flags.HasFlag(PropertyInfo.MultiFlag.Unsigned));
                uint signer     = (0x80000000 >> (32 - numBits)) & unchecked ((uint)(isUnsigned - 1));

                uint value = stream.ReadBits(numBits) ^ signer;
                return(value - signer);
            }
        }
Example #20
0
        private float UnpackFloatCoord(Bitstream stream) {
            bool hasInteger = stream.ReadBool();
            bool hasFraction = stream.ReadBool();

            if (hasInteger || hasFraction) {
                bool sign = stream.ReadBool();

                uint integer = 0;
                if (hasInteger) {
                    integer = stream.ReadBits(COORD_INTEGER_BITS) + 1;
                }

                uint fraction = 0;
                if (hasFraction) {
                    fraction = stream.ReadBits(COORD_FRACTIONAL_BITS);
                }

                float f = (float) (integer + fraction * COORD_RESOLUTION);

                if (sign) {
                    f *= -1;
                }

                return f;
            } else {
                return 0;
            }
        }
Example #21
0
        private Nullable<Message> ReadChunk(Bitstream stream) {
            uint offset = stream.ReadBits(18);
            uint count = stream.ReadBits(3);

            log.DebugFormat("chunk start = {0} end = {1}", offset, count);

            if (offset == 0) {
                Preconditions.CheckArgument(!Receiving);
                ReadChunkHeader(stream);
            } else {
                Preconditions.CheckArgument(Receiving);
            }

            uint byteOffset = offset * Connection.BYTES_PER_CHUNK;

            uint byteCount;
            if (offset + count < header.ChunkCount) {
                byteCount = count * Connection.BYTES_PER_CHUNK;
            } else {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (uint i = offset;
                    i < offset + count;
                    ++i) {
                if (!dataReceived[i]) {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount) {
                log.Debug("chunk complete");
                Receiving = false;
                return new Message {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,

                    Data = dataIn,
                };
            } else {
                log.DebugFormat("chunk has {0}/{1}", countReceived, header.ChunkCount);
                return null;
            }
        }
Example #22
0
        public string UnpackString(PropertyInfo info, Bitstream stream)
        {
            var length = stream.ReadBits(9);

            var buffer = new byte[length];

            stream.Read(buffer, 0, (int)length);

            return(new string((from byte b in buffer select(char) b).ToArray <char>()));
        }
Example #23
0
        public string UnpackString(PropertyInfo info, Bitstream stream)
        {
            uint length = stream.ReadBits(9);

            Preconditions.CheckArgument(length <= MAX_STRING_LENGTH);

            byte[] buffer = new byte[length];
            stream.Read(buffer, 0, (int)length);

            return(new String((from byte b in buffer select(char) b).ToArray <char>()));
        }
Example #24
0
        private void ReadChunkHeader(Bitstream stream)
        {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile)
            {
                uint   filenameLength = stream.ReadUInt32();
                byte[] filename       = new byte[filenameLength + 1]; // semantically wrong. should be
                                                                      // 0x104
                stream.Read(filename, 0, (int)filenameLength);        // and then read to end of string
                filename[filenameLength] = 0;                         // whatever
                header.Filename          = Encoding.UTF8.GetString(filename);
                log.ErrorFormat("read filename: \"{0}\" ({1})", filenameLength, header.Filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed)
            {
                header.DecompressedLength = stream.ReadBits(26);
                log.DebugFormat(
                    "chunkheader compressed: decompressed len {0}",
                    header.DecompressedLength);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount =
                (header.ByteLength + Connection.BYTES_PER_CHUNK - 1) /
                Connection.BYTES_PER_CHUNK;
            log.DebugFormat(
                "chunkheader len {0} expecting {1} chunks",
                header.ByteLength, header.ChunkCount);

            Receiving     = true;
            dataIn        = new byte[header.ByteLength];
            dataReceived  = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #25
0
        public float UnpackFloat(PropertyInfo info, Bitstream stream)
        {
            var flags = info.Flags;

            if (flags.HasFlag(PropertyInfo.MultiFlag.Coord))
            {
                return(UnpackFloatCoord(stream));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMp))
            {
                return(UnpackFloatCoordMp(stream, FloatType.None));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMpLowPrecision))
            {
                return(UnpackFloatCoordMp(stream, FloatType.LowPrecision));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CoordMpIntegral))
            {
                return(UnpackFloatCoordMp(stream, FloatType.Integral));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.NoScale))
            {
                return(UnpackFloatNoScale(stream));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.Normal))
            {
                return(UnpackFloatNormal(stream));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoord))
            {
                return(UnpackFloatCellCoord(info, stream, FloatType.None));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoordLowPrecision))
            {
                return(UnpackFloatCellCoord(info, stream, FloatType.LowPrecision));
            }
            else if (flags.HasFlag(PropertyInfo.MultiFlag.CellCoordIntegral))
            {
                return(UnpackFloatCellCoord(info, stream, FloatType.Integral));
            }
            else
            {
                uint dividend = stream.ReadBits(info.NumBits);
                uint divisor  = (uint)(1 << info.NumBits) - 1;

                float f     = ((float)dividend) / divisor;
                float range = info.HighValue - info.LowValue;

                return(f * range + info.LowValue);
            }
        }
Example #26
0
        public ulong UnpackInt64(PropertyInfo info, Bitstream stream)
        {
            if (info.Flags.HasFlag(PropertyInfo.MultiFlag.EncodedAgainstTickcount))
            {
                log.DebugFormat(
                    "{0}.{1} is encoded against tick count",
                    info.DtName, info.VarName);
                return(stream.ReadVarUInt());
            }
            else
            {
                bool negate     = false;
                byte secondBits = (byte)(info.NumBits - 32);

                if (!info.Flags.HasFlag(PropertyInfo.MultiFlag.Unsigned))
                {
                    --secondBits;

                    if (stream.ReadBool())
                    {
                        negate = true;
                    }
                }

                Preconditions.CheckArgument(info.NumBits >= secondBits);

                ulong a     = stream.ReadBits(32);
                ulong b     = stream.ReadBits(secondBits);
                ulong value = (b << 32) | a;

                if (negate)
                {
                    value = unchecked ((ulong)((long)value * -1));
                }

                return(value);
            }
        }
Example #27
0
        private float UnpackFloatNormal(Bitstream stream)
        {
            bool sign  = stream.ReadBool();
            uint value = stream.ReadBits(NORMAL_FRACTIONAL_BITS);

            float f = (float)(value * NORMAL_RESOLUTION);

            if (sign)
            {
                f *= -1;
            }

            return(f);
        }
Example #28
0
        private string ReadKeyIfIncluded(Bitstream stream, List <string> keyHistory)
        {
            bool has_key = stream.ReadBool();

            if (!has_key)
            {
                return(null);
            }

            bool is_substring = stream.ReadBool();

            string key;

            if (!is_substring)
            {
                key = stream.ReadString();
            }
            else
            {
                int fromIndex  = (int)stream.ReadBits(5);
                int fromLength = (int)stream.ReadBits(5);
                key = keyHistory[fromIndex].Substring(0, fromLength);

                key += stream.ReadString();
            }

            Preconditions.CheckArgument(key.Length <= MAX_KEY_SIZE);

            if (keyHistory.Count == KEY_HISTORY_SIZE)
            {
                keyHistory.RemoveAt(0);
            }

            keyHistory.Add(key);

            return(key);
        }
Example #29
0
        private void ReadChunkHeader(Bitstream stream) {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile) {
                uint filenameLength = stream.ReadUInt32();
                byte[] filename = new byte[filenameLength + 1]; // semantically wrong. should be
                                                                // 0x104
                stream.Read(filename, 0, (int)filenameLength); // and then read to end of string
                filename[filenameLength] = 0; // whatever 
                header.Filename = Encoding.UTF8.GetString(filename);
                log.ErrorFormat("read filename: \"{0}\" ({1})", filenameLength, header.Filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed) {
                header.DecompressedLength = stream.ReadBits(26);
                log.DebugFormat(
                    "chunkheader compressed: decompressed len {0}", 
                    header.DecompressedLength);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount = 
                (header.ByteLength + Connection.BYTES_PER_CHUNK - 1) /
                Connection.BYTES_PER_CHUNK;
            log.DebugFormat(
                "chunkheader len {0} expecting {1} chunks", 
                header.ByteLength, header.ChunkCount);

            Receiving = true;
            dataIn = new byte[header.ByteLength];
            dataReceived = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #30
0
        public uint UnpackInt(PropertyInfo info, Bitstream stream) {
            var flags = info.Flags;

            if (flags.HasFlag(PropertyInfo.MultiFlag.EncodedAgainstTickcount)) {
                if (flags.HasFlag(PropertyInfo.MultiFlag.Unsigned)) {
                    return stream.ReadVarUInt();
                } else {
                    uint value = stream.ReadVarUInt();
                    return unchecked((uint) ((-(value & 1)) ^ (value >> 1)));
                }
            } else {
                byte numBits = info.NumBits;

                uint isUnsigned = Convert.ToUInt32(flags.HasFlag(PropertyInfo.MultiFlag.Unsigned));
                uint signer = (0x80000000 >> (32 - numBits)) & unchecked((uint) (isUnsigned - 1));

                uint value = stream.ReadBits(numBits) ^ signer;
                return value - signer;
            }
        }
Example #31
0
        public void UnpackArray(uint tick, List <Property> elements, PropertyInfo info, Bitstream stream)
        {
            byte countBits = (byte)(Util.Log2(info.NumElements + 1));
            uint count     = stream.ReadBits(countBits);

            if (elements.Count > count)
            {
                elements.RemoveRange(0, elements.Count - (int)count);
            }
            else
            {
                while (elements.Count < count)
                {
                    elements.Add(Property.For(info.ArrayProp));
                }
            }

            foreach (var element in elements)
            {
                element.Update(tick, this, stream);
            }
        }
Example #32
0
        public void Update(Bitstream stream, uint baseline, bool updateBaseline, uint updated, bool isDelta)
        {
            var  id    = uint.MaxValue;
            uint found = 0;

            while (found < updated)
            {
                var flags = ReadHeader(ref id, stream);

                if (flags.HasFlag(UpdateFlag.EnterPvs))
                {
                    ReadEnterPvs(id, baseline, updateBaseline, stream);
                }
                else if (flags.HasFlag(UpdateFlag.LeavePvs))
                {
                    if (flags.HasFlag(UpdateFlag.Delete))
                    {
                        Delete(id);
                    }
                }
                else
                {
                    ReadUpdate(id, stream);
                }

                ++found;
            }

            if (isDelta)
            {
                while (stream.ReadBool())
                {
                    id = stream.ReadBits(11);
                    Delete(id);
                }
            }
        }
Example #33
0
        public void UnpackArray(uint tick, List<Property> elements, PropertyInfo info, Bitstream stream) {
            byte countBits = (byte) (Util.Log2(info.NumElements + 1));
            uint count = stream.ReadBits(countBits);

            if (elements.Count > count) {
                elements.RemoveRange(0, elements.Count - (int) count);
            } else {
                while (elements.Count < count) {
                    elements.Add(Property.For(info.ArrayProp));
                }
            }

            foreach (var element in elements) {
                element.Update(tick, this, stream);
            }
        }
Example #34
0
        public string UnpackString(PropertyInfo info, Bitstream stream) {
            uint length = stream.ReadBits(9);

            Preconditions.CheckArgument(length <= MAX_STRING_LENGTH);

            byte[] buffer = new byte[length];
            stream.Read(buffer, 0, (int) length);

            return new String((from byte b in buffer select (char) b).ToArray<char>());
        }
Example #35
0
        private byte[] ReadValueIfIncluded(Bitstream stream, bool userDataFixedSize,
                uint userDataSizeBits) {
            bool has_value = stream.ReadBool();

            if (!has_value) {
                return null;
            }

            uint length;
            uint bitLength;

            if (userDataFixedSize) {
                length = (userDataSizeBits + 7) / 8;
                bitLength = userDataSizeBits;
            } else {
                length = stream.ReadBits(14);
                bitLength = 8 * length;
            }

            Preconditions.CheckArgument(length <= MAX_VALUE_SIZE);

            return stream.ReadManyBits(bitLength);
        }
Example #36
0
        private float UnpackFloatCellCoord(PropertyInfo info, Bitstream stream, FloatType type) {
            uint value = stream.ReadBits(info.NumBits);
            float f = value;

            if ((value >> 31) > 0) {
                f *= -1;
            }

            if (type == FloatType.None) {
                uint fraction = stream.ReadBits(5);

                return f + 0.03125f * fraction;
            } else if (type == FloatType.LowPrecision) {
                uint fraction = stream.ReadBits(3);

                return f + 0.125f * fraction;
            } else if (type == FloatType.Integral) {
                return f;
            } else {
                throw new InvalidOperationException("Unknown float type");
            }
        }
Example #37
0
        private float UnpackFloatNormal(Bitstream stream) {
            bool sign = stream.ReadBool();
            uint value = stream.ReadBits(NORMAL_FRACTIONAL_BITS);

            float f = (float) (value * NORMAL_RESOLUTION);

            if (sign) {
                f *= -1;
            }

            return f;
        }
Example #38
0
        public ulong UnpackInt64(PropertyInfo info, Bitstream stream) {
            if (info.Flags.HasFlag(PropertyInfo.MultiFlag.EncodedAgainstTickcount)) {
                log.DebugFormat(
                    "{0}.{1} is encoded against tick count", 
                    info.DtName, info.VarName);
                return stream.ReadVarUInt();
            } else {
                bool negate = false;
                byte secondBits = (byte) (info.NumBits - 32);

                if (!info.Flags.HasFlag(PropertyInfo.MultiFlag.Unsigned)) {
                    --secondBits;

                    if (stream.ReadBool()) {
                        negate = true;
                    }
                }

                Preconditions.CheckArgument(info.NumBits >= secondBits);

                ulong a = stream.ReadBits(32);
                ulong b = stream.ReadBits(secondBits);
                ulong value = (b << 32) | a;

                if (negate) {
                    value = unchecked((ulong) ((long) value * -1));
                }

                return value;
            }
        }
Example #39
0
        private Message ReadSingle(Bitstream stream) {
            bool isCompressed = stream.ReadBool();
            
            if (isCompressed) {
                uint uncompressed_length = stream.ReadBits(26);
                uint length = stream.ReadBits(18);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int) length);

                return new Message {
                    IsCompressed = false,
                    Data = Snappy.Sharp.Snappy.Uncompress(data)
                };
            } else {
                uint length = stream.ReadBits(18);
                Preconditions.CheckArgument(length < Int32.MaxValue);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return new Message {
                    IsCompressed = false,
                    Data = data,
                };
            }
        }
Example #40
0
        private void ReadEnterPvs(uint id, uint baseline, bool update_baseline, Bitstream stream) {
            var clazz = client.Classes[(int) stream.ReadBits(this.ClassBitLength)];
            var serial = stream.ReadBits(10);

            log.Debug(String.Format("{0} entering pvs as a {1} (serial {2})", id, clazz.ClassName, serial));

            Create(id, clazz, baseline);
            ReadAndUnpackFields(client.Slots[id].Entity, stream);

            if (update_baseline) {
                client.Slots[id].Baselines[1 - baseline] = client.Slots[id].Entity.Copy();
            }
        }