public override void ReadPacket() { SequenceNumber = ReadUShort(); // Read the byte flag representing update types and reconstruct it var updateTypeFlag = ReadByte(); // Keep track of value of current bit var currentTypeValue = 1; for (var i = 0; i < (int)UpdateType.Count; i++) { // If this bit was set in our flag, we add the type to the list if ((updateTypeFlag & currentTypeValue) != 0) { UpdateTypes.Add((UpdateType)currentTypeValue); } // Increase the value of current bit currentTypeValue *= 2; } // Based on the update types, we read the corresponding values if (UpdateTypes.Contains(UpdateType.PlayerUpdate)) { // First we read the length of the player update list var numPlayerUpdates = ReadByte(); // Then we read all the values into PlayerUpdate instances for (var i = 0; i < numPlayerUpdates; i++) { // We create a new instance var playerUpdate = new PlayerUpdate(); // Read the information into the new instance ReadPlayerUpdate(playerUpdate); // Add the instance to the list PlayerUpdates.Add(playerUpdate); } } if (UpdateTypes.Contains(UpdateType.EntityUpdate)) { // First we read the length of the entity update list var numEntityUpdates = ReadByte(); // Then we read all the values into the EntityUpdates dictionary for (var i = 0; i < numEntityUpdates; i++) { // Create a new EntityUpdate instance var entityUpdate = new EntityUpdate(); // Read the values into the instance ReadEntityUpdate(entityUpdate); // Add it to the list EntityUpdates.Add(entityUpdate); } } }
/// <inheritdoc /> public void WriteData(IPacket packet) { packet.Write(EntityType); packet.Write(Id); // Construct the byte flag representing update types byte updateTypeFlag = 0; // Keep track of value of current bit byte currentTypeValue = 1; for (var i = 0; i < Enum.GetNames(typeof(EntityUpdateType)).Length; i++) { // Cast the current index of the loop to a PlayerUpdateType and check if it is // contained in the update type list, if so, we add the current bit to the flag if (UpdateTypes.Contains((EntityUpdateType)i)) { updateTypeFlag |= currentTypeValue; } currentTypeValue *= 2; } // Write the update type flag packet.Write(updateTypeFlag); // Conditionally write the state and data fields if (UpdateTypes.Contains(EntityUpdateType.Position)) { packet.Write(Position); } if (UpdateTypes.Contains(EntityUpdateType.State)) { packet.Write(State); } if (UpdateTypes.Contains(EntityUpdateType.Variables)) { // First write the number of bytes we are writing packet.Write((byte)Variables.Count); foreach (var b in Variables) { packet.Write(b); } } }
public override Packet CreatePacket() { Reset(); // Write packet header information Write(PacketId.PlayerUpdate); Write(SequenceNumber); // Construct the byte flag representing update types byte updateTypeFlag = 0; foreach (var updateType in UpdateTypes) { updateTypeFlag |= (byte)updateType; } // Write the update type flag Write(updateTypeFlag); if (UpdateTypes.Contains(UpdateType.PlayerUpdate)) { // First we write the length of the player update list Write((byte)PlayerUpdates.Count); // Then for each PlayerUpdate instance, we write their values foreach (var playerUpdate in PlayerUpdates) { WritePlayerUpdate(playerUpdate); } } if (UpdateTypes.Contains(UpdateType.EntityUpdate)) { // First we write the length of the entity update list Write((byte)EntityUpdates.Count); // Then for each EntityUpdate instance, we write their values foreach (var entityUpdate in EntityUpdates) { WriteEntityUpdate(entityUpdate); } } WriteLength(); return(this); }
/// <inheritdoc /> public void ReadData(IPacket packet) { EntityType = packet.ReadByte(); Id = packet.ReadByte(); // Read the byte flag representing update types and reconstruct it var updateTypeFlag = packet.ReadByte(); // Keep track of value of current bit var currentTypeValue = 1; for (var i = 0; i < Enum.GetNames(typeof(EntityUpdateType)).Length; i++) { // If this bit was set in our flag, we add the type to the list if ((updateTypeFlag & currentTypeValue) != 0) { UpdateTypes.Add((EntityUpdateType)i); } // Increase the value of current bit currentTypeValue *= 2; } // Based on the update types, we read the corresponding values if (UpdateTypes.Contains(EntityUpdateType.Position)) { Position = packet.ReadVector2(); } if (UpdateTypes.Contains(EntityUpdateType.State)) { State = packet.ReadByte(); } if (UpdateTypes.Contains(EntityUpdateType.Variables)) { // We first read how many bytes are in the array var numBytes = packet.ReadByte(); for (var i = 0; i < numBytes; i++) { var readByte = packet.ReadByte(); Variables.Add(readByte); } } }
/// <inheritdoc /> public override void WriteData(IPacket packet) { // Write the player update information packet.Write(Id); // Construct the byte flag representing update types byte updateTypeFlag = 0; // Keep track of value of current bit byte currentTypeValue = 1; for (var i = 0; i < Enum.GetNames(typeof(PlayerUpdateType)).Length; i++) { // Cast the current index of the loop to a PlayerUpdateType and check if it is // contained in the update type list, if so, we add the current bit to the flag if (UpdateTypes.Contains((PlayerUpdateType)i)) { updateTypeFlag |= currentTypeValue; } currentTypeValue *= 2; } // Write the update type flag packet.Write(updateTypeFlag); // Conditionally write the position, scale, map position and animation info if (UpdateTypes.Contains(PlayerUpdateType.Position)) { packet.Write(Position); } if (UpdateTypes.Contains(PlayerUpdateType.Scale)) { packet.Write(Scale); } if (UpdateTypes.Contains(PlayerUpdateType.MapPosition)) { packet.Write(MapPosition); } if (UpdateTypes.Contains(PlayerUpdateType.Animation)) { // First write the number of infos we are writing // We also limit this to a byte, if the list is larger than 255 animations, // we just don't send them the rest ¯\_(ツ)_/¯ var numAnimations = (byte)System.Math.Min(AnimationInfos.Count, 255); packet.Write(numAnimations); for (var i = 0; i < numAnimations; i++) { var animationInfo = AnimationInfos[i]; packet.Write(animationInfo.ClipId); packet.Write(animationInfo.Frame); // Check whether there is effect info to write if (animationInfo.EffectInfo == null) { packet.Write((byte)0); } else { // Again, we first write the length of the effect info array var numEffects = animationInfo.EffectInfo.Length; packet.Write((byte)numEffects); byte currentByte = 0; byte currentBitValue = 1; // And then the values of the array itself for (var j = 0; j < numEffects; j++) { if (animationInfo.EffectInfo[j]) { currentByte |= currentBitValue; } if (currentBitValue == 128) { // We have reached the last bit in our byte, so we reset packet.Write(currentByte); currentByte = 0; currentBitValue = 1; } else { // Otherwise we move on to the next bit by doubling the value currentBitValue *= 2; } } // If we haven't written this byte yet, we write it now if (currentBitValue != 128) { packet.Write(currentByte); } } } } }
/// <inheritdoc /> public override void ReadData(IPacket packet) { Id = packet.ReadUShort(); // Read the byte flag representing update types and reconstruct it var updateTypeFlag = packet.ReadByte(); // Keep track of value of current bit var currentTypeValue = 1; for (var i = 0; i < Enum.GetNames(typeof(PlayerUpdateType)).Length; i++) { // If this bit was set in our flag, we add the type to the list if ((updateTypeFlag & currentTypeValue) != 0) { UpdateTypes.Add((PlayerUpdateType)i); } // Increase the value of current bit currentTypeValue *= 2; } // Based on the update types, we read the corresponding values if (UpdateTypes.Contains(PlayerUpdateType.Position)) { Position = packet.ReadVector2(); } if (UpdateTypes.Contains(PlayerUpdateType.Scale)) { Scale = packet.ReadBool(); } if (UpdateTypes.Contains(PlayerUpdateType.MapPosition)) { MapPosition = packet.ReadVector2(); } if (UpdateTypes.Contains(PlayerUpdateType.Animation)) { // We first read how many animations are in the packet var numAnimations = packet.ReadByte(); for (var i = 0; i < numAnimations; i++) { // Create a new animation info instance var animationInfo = new AnimationInfo { ClipId = packet.ReadUShort(), Frame = packet.ReadByte() }; // Now we read how many effect are in the packet and // create an array with that length var numEffects = packet.ReadByte(); // Check whether there is effect info to be read if (numEffects != 0) { var effectInfo = new bool[numEffects]; var currentByte = packet.ReadByte(); byte currentBitValue = 1; for (var j = 0; j < numEffects; j++) { effectInfo[j] = (currentByte & currentBitValue) != 0; if (currentBitValue == 128 && j != numEffects - 1) { // We have reached the last bit in our byte, so we read another currentByte = packet.ReadByte(); currentBitValue = 1; } else { // Otherwise we move on to the next bit by doubling the value currentBitValue *= 2; } } // Save the effect info in the animation info instance animationInfo.EffectInfo = effectInfo; } AnimationInfos.Add(animationInfo); } } }