public static byte[] GetPacketDataWithoutType(SourceDemoInfo info, byte[] data, IEnumerable <int> forbiddenFruits) { PacketReader reader = new PacketReader(info, data); BitWriterDeluxe writer = new BitWriterDeluxe(); reader.MessageRead += (s, e) => { if (!forbiddenFruits.Contains(e.Type)) { writer.MoveBitsIn(e.DataBitWriter); } }; reader.Parse(); var newData = writer.Data; if (PacketFormatValid(info, newData)) { return(newData); } else { return(data); } }
public static void WriteDeleterPacket(BinaryWriter writer, int tick) { // Packet stuff writer.Write((byte)ParsedDemo.MessageType.Packet); writer.Write(tick); writer.Write(new byte[0x54]); // svc_packetentities var bw2 = new BitWriterDeluxe(); bw2.WriteUnsignedBits(26, 6); bw2.WriteUnsignedBits(4095, MAX_EDICT_BITS); bw2.WriteBoolean(false); //bw.WriteBits(deltaFrom, 32); bw2.WriteBoolean(true); bw2.WriteUnsignedBits(4095, MAX_EDICT_BITS); var bw3 = GetDeleterPacketEntitiesData(); bw2.WriteUnsignedBits((uint)bw3.BitsWritten, DELTASIZE_BITS); bw2.MoveBitsIn(bw3); bw2.WriteBoolean(false); var bytes = bw2.Data; writer.Write(bytes.Count()); writer.Write(bytes); }
public void MoveBitsIn(BitWriterDeluxe del) { int bitCount = del.BitsWritten; var bytes = del.Data; WriteBitsFromArray(bytes, 0, bitCount); }
public DataReadEventArgs(byte[] data, BitWriterDeluxe dataBitWriter, byte[] withoutType, BitWriterDeluxe withoutTypeBitWriter, int type) { Data = data; DataBitWriter = dataBitWriter; WithoutType = withoutType; WithoutTypeBitWriter = withoutTypeBitWriter; Type = type; }
private static BitWriterDeluxe GetDeleterPacketEntitiesData() { BitWriterDeluxe bw = new BitWriterDeluxe(); for (int i = 0; i < 4095; ++i) { bw.WriteUnsignedBits(0, 6); bw.WriteBoolean(false); bw.WriteBoolean(true); } return(bw); }
public static void TestBitWriting() { try { Random rng = new Random(); const int size = 1000; byte[] byteArray = new byte[size]; for (int i = 0; i < size; ++i) { byteArray[i] = ((byte)rng.Next(0, 255)); } int currentBit = 0; int totalBits = byteArray.Count() * 8; BitWriterDeluxe writer = new BitWriterDeluxe(); while (currentBit < totalBits) { int addition = Math.Min(totalBits - currentBit, rng.Next(1, 100)); int newIndex = currentBit + addition; writer.WriteRangeFromArray(byteArray, currentBit, newIndex); currentBit = newIndex; } var results = writer.Data; for (int i = 0; i < byteArray.Count(); ++i) { if (results[i] != byteArray[i]) { throw new Exception(string.Format("{0} = {1} on index {2}", results[i], byteArray[i], i)); } } Console.WriteLine("Copying succeeded."); Console.WriteLine("Size comparison {0} = {1}", byteArray.Count(), results.Count()); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); }
public void Parse() { try { int type = 0; var bb = new BitBuffer(Data); while (bb.BitsLeft > NetMessageBits && !ShouldStop) { var dataBitWriter = new BitWriterDeluxe(); var withoutTypeBitWriter = new BitWriterDeluxe(); int startIndex = bb.CurrentBit; type = (int)bb.ReadUnsignedBits(NetMessageBits); MessageTypeRead?.Invoke(this, new MessageReceivedEventArgs(type, bb)); int withoutTypeIndex = bb.CurrentBit; MsgHandler handler; if (Handlers.TryGetValue((uint)type, out handler)) { handler(bb); dataBitWriter.WriteRangeFromArray(Data, startIndex, bb.CurrentBit); withoutTypeBitWriter.WriteRangeFromArray(Data, withoutTypeIndex, bb.CurrentBit); var bytes = dataBitWriter.Data; var bytes2 = withoutTypeBitWriter.Data; MessageRead?.Invoke(this, new DataReadEventArgs(bytes, dataBitWriter, bytes2, withoutTypeBitWriter, type)); } else { throw new Exception("Unknown packet type found."); } } PacketSuccessfullyRead?.Invoke(this, new EventArgs()); } catch (Exception e) { ExceptionThrown?.Invoke(this, new ExceptionEventArgs(e)); } }