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); }
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); }
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; }
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; }
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); } }
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; }
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; } }
public Nullable<Message> Receive(Bitstream stream) { bool hasData = stream.ReadBool(); if (!hasData) { return null; } if (stream.ReadBool()) { return ReadChunk(stream); } else { return ReadSingle(stream); } }
public Message?Receive(Bitstream stream) { var hasData = stream.ReadBool(); if (!hasData) { return(null); } if (stream.ReadBool()) { return(ReadChunk(stream)); } return(ReadSingle(stream)); }
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, }); } }
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); } } }
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)); }
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)); }
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); }
public Vector UnpackVector(PropertyInfo info, Bitstream stream) { float x = UnpackFloat(info, stream); float y = UnpackFloat(info, stream); float z; if (info.Flags.HasFlag(PropertyInfo.MultiFlag.Normal)) { bool sign = stream.ReadBool(); float f = x * x + y * y; if (1 >= f) { z = 0; } else { z = (float)Math.Sqrt(1 - f); } if (sign) { z *= -1; } } else { z = UnpackFloat(info, stream); } return(new Vector(x, y, z)); }
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 }); } }
public Nullable <Message> Receive(Bitstream stream) { bool hasData = stream.ReadBool(); if (!hasData) { return(null); } if (stream.ReadBool()) { return(ReadChunk(stream)); } else { return(ReadSingle(stream)); } }
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; }
private uint ReadFieldNumber(uint lastField, Bitstream stream) { if (stream.ReadBool()) { return(unchecked (lastField + 1)); } var value = stream.ReadVarUInt(); if (value == 0x3FFF) { return(uint.MaxValue); } return(unchecked (lastField + value + 1)); }
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); }
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); }
public override void Deserialize(Bitstream msg) { SnapId = msg.ReadUInt32(); BaselineId = msg.ReadUInt32(); ushort count = msg.ReadByte(); Dictionary<int, NetworkObject> state = new Dictionary<int, NetworkObject>(); int addDeltaCount = 0; for (int i=0; i < count; ++i) { ushort objTypeId = msg.ReadByte(); var entId = msg.ReadVariableUInt32(); NetworkObject obj = ObjectMapper.Lookup(entId, objTypeId, true); if (obj == null) { obj = ObjectMapper.Create(entId, objTypeId); } NetworkObject realObj = ObjectMapper.Lookup(entId, objTypeId, false); var realHash = 0; if (realObj != null) realHash = realObj.GetHashCode(); NetworkObject baseline = null; var usebaseLine = msg.ReadBool(); uint bOffset = 0; if (usebaseLine) { byte offset = msg.ReadByte(); var objBaselineId = BaselineId - (uint)offset; bOffset = objBaselineId; baseline = ObjectMapper.GetBaseline(objBaselineId, realHash); } var ownerType = obj.GetType().GetTypeInfo(); var dItem = DataLookupTable.Get(ownerType); ReadNetObject(dItem, msg, obj, baseline); ObjectMapper.AddDeltaState(realHash, SnapId, obj); if (BaselineId != 0 && realHash != 0) addDeltaCount++; } if (addDeltaCount == count) ObjectMapper.LastSimId = BaselineId; }
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); } }
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); } } }
public Vector UnpackVector(PropertyInfo info, Bitstream stream) { float x = UnpackFloat(info, stream); float y = UnpackFloat(info, stream); float z; if (info.Flags.HasFlag(PropertyInfo.MultiFlag.Normal)) { bool sign = stream.ReadBool(); float f = x * x + y * y; if (1 >= f) { z = 0; } else { z = (float) Math.Sqrt(1 - f); } if (sign) { z *= -1; } } else { z = UnpackFloat(info, stream); } return new Vector(x, y, z); }
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; } }
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); }
private uint ReadFieldNumber(uint lastField, Bitstream stream) { if (stream.ReadBool()) { return unchecked(lastField + 1); } else { uint value = stream.ReadVarUInt(); if (value == 0x3FFF) { return UInt32.MaxValue; } else { return unchecked(lastField + value + 1); } } }
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, }; } }
public override void Deserialize(Bitstream msg) { OwnerEntityId = msg.ReadUInt32(); Success = msg.ReadBool(); }
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; }
internal static void ReadNetObject(DataSerializationItem item, Bitstream msg, NetworkObject owner, NetworkObject baseline=null) { if (owner is DefinitionNetworkObject) { var d = msg.ReadBool(); if (d) { (owner as DefinitionNetworkObject).Destroy = true; return; } } foreach (var prop in item.Properties) { var useBaseline = msg.ReadBool(); if (useBaseline) { if (baseline != null) { ApplyBaseline(prop, owner, baseline); } else { throw new InvalidOperationException("Can't find baseline!"); } continue; } switch (prop.Type) { case DataTypes.BOOL: prop.Set(owner, msg.ReadBool()); break; case DataTypes.UBYTE: prop.Set(owner, msg.ReadByte()); break; case DataTypes.UINT: prop.Set(owner, msg.ReadVariableUInt32()); break; case DataTypes.BYTE: prop.Set(owner, msg.ReadSByte()); break; case DataTypes.INT: prop.Set(owner, msg.ReadVariableInt32()); break; case DataTypes.ULONG: prop.Set(owner, msg.ReadUInt64()); break; case DataTypes.LONG: prop.Set(owner, msg.ReadInt64()); break; case DataTypes.FLOAT: prop.Set(owner, msg.ReadFloat()); break; case DataTypes.DOUBLE: prop.Set(owner, msg.ReadDouble()); break; case DataTypes.STRING: prop.Set(owner, msg.ReadString()); break; case DataTypes.VECTOR2: prop.Set(owner, msg.ReadVector2()); break; case DataTypes.NETPROP: var childProp = prop.ChildProperty; if (childProp != null) { var o = (NetworkObject)prop.CreateChildProperty(); if (o != null) { var child = (NetworkObject)prop.Get<NetworkObject>(baseline); ReadNetObject(childProp, msg, o, child); prop.Set(owner, o); } } break; } } }
private object ReadParam(Bitstream msg) { var fullTypeName = msg.ReadString(); var type = LookupType(fullTypeName); var dsitem = new DataSerializationProperty(null, type); object o; switch (dsitem.Type) { case DataTypes.BOOL: o = msg.ReadBool(); break; case DataTypes.UBYTE: o = msg.ReadByte(); break; case DataTypes.UINT: o = msg.ReadVariableUInt32(); break; case DataTypes.BYTE: o = msg.ReadSByte(); break; case DataTypes.INT: o = msg.ReadVariableInt32(); break; case DataTypes.ULONG: o = msg.ReadUInt64(); break; case DataTypes.LONG: o = msg.ReadInt64(); break; case DataTypes.FLOAT: o = msg.ReadFloat(); break; case DataTypes.DOUBLE: o = msg.ReadDouble(); break; case DataTypes.STRING: o = msg.ReadString(); break; case DataTypes.VECTOR2: o = msg.ReadVector2(); break; default: throw new ArgumentException("Invalid RPC paramter type."); } return o; }