Beispiel #1
0
        private bool ProcessTick()
        {
            DemoCommand demoCommand = (DemoCommand)m_BitStream.ReadByte();

            int tickNumber = m_BitStream.ReadInt32();
            int playerSlot = m_BitStream.ReadByte();

            switch (demoCommand)
            {
                case DemoCommand.DataTables:
                    {
                        int length = m_BitStream.ReadInt32();
                        byte[] data = m_BitStream.ReadBytes(length);

                        BitStream bitStream = new BitStream(data);

                        while (true)
                        {
                            int dataTableType = bitStream.ReadVarint32();
                            int dataTablelength = bitStream.ReadVarint32();
                            byte[] dataTableData = bitStream.ReadBytes(dataTablelength);

                            DataTable dataTable = new DataTable(this.DataTables.Count, dataTableData);

                            if (dataTable.IsEnd)
                            {
                                break;
                            }
                            else
                            {
                                this.DataTables.Add(dataTable);
                            }
                        }

                        int count = bitStream.ReadShort();

                        for (int i = 0; i < count; i++)
                        {
                            int serverClassID = bitStream.ReadShort();
                            string serverClassName = bitStream.ReadString();
                            string dataTableName = bitStream.ReadString();

                            DataTable dataTable = this.DataTables.GetDataTable(dataTableName);

                            ServerClass serverClass = new ServerClass(serverClassID, serverClassName, dataTable.ID, dataTable.Name);

                            this.ServerClasses.Add(serverClass);
                        }

                        this.ServerClasses.ProcessAllServerClasses();

                        break;
                    }
                case DemoCommand.Packet:
                case DemoCommand.SignOn:
                    {
                        m_BitStream.SeekBytes(160, SeekOrigin.Current);

                        int length = m_BitStream.ReadInt32();
                        byte[] data = m_BitStream.ReadBytes(length);

                        BitStream packetReader = new BitStream(data);

                        while (packetReader.PositionInBytes < packetReader.LengthInBytes)
                        {
                            int packetID = packetReader.ReadVarint32();
                            int packetLength = packetReader.ReadVarint32();
                            byte[] packetData = packetReader.ReadBytes(packetLength);

                            PacketType packetType;

                            if (!Enum.TryParse<PacketType>(packetID.ToString(), out packetType))
                            {
                                Console.WriteLine("Unknown Packet Type: {0}", packetID);

                                break;
                            }

                            this.PacketHandlers.OnPacketReceived(packetType, packetData);
                        }

                        break;
                    }
                case DemoCommand.Stop:
                    {
                        return true;
                    }
                case DemoCommand.StringTables:
                    {
                        int length = m_BitStream.ReadInt32();
                        byte[] data = m_BitStream.ReadBytes(length);

                        BitStream bitStream = new BitStream(data);

                        int stringTableCount = bitStream.ReadByte();

                        for (int i = 0; i < stringTableCount; i++)
                        {
                            string stringTableName = bitStream.ReadString();

                            int entryCount = bitStream.ReadUShort();

                            for (int j = 0; j < entryCount; j++)
                            {
                                string entryName = bitStream.ReadString();
                                bool entryHasData = bitStream.ReadBit();

                                byte[] entryData = null;

                                if (entryHasData)
                                {
                                    int entryDataLength = bitStream.ReadShort();

                                    entryData = bitStream.ReadBytes(entryDataLength);
                                }

                                StringTableEntry stringTableEntry = new StringTableEntry(entryName, entryData);

                                this.StringTables.Add(stringTableName, stringTableEntry);
                            }

                            bool hasExtraTableData = bitStream.ReadBit();

                            if (hasExtraTableData)
                            {
                                int extraTableDataLength = bitStream.ReadShort();
                                byte[] extraTableData = bitStream.ReadBytes(extraTableDataLength);
                            }
                        }

                        break;
                    }
                case DemoCommand.SyncTick:
                    {
                        break;
                    }

                default:
                    {
                        Console.WriteLine("Unknown Demo Command: {0}", demoCommand);

                        break;
                    }
            }

            return false;
        }
        private void Update(BitStream bitStream, string tableName, int tableFlags, int numberOfEntries, int maxEntries, int userDataSize, int userDataSizeInBits, bool userDataIsFixedSize)
        {
            bool encodeUsingDictionaries = bitStream.ReadBit();

            if (encodeUsingDictionaries)
            {
                Console.WriteLine("Cannot Decode StringTable Update");

                return;
            }

            List<String> entryHistory = new List<String>();
            int lastEntriesIndex = -1;

            for (int i = 0; i < numberOfEntries; i++)
            {
                int entryIndex = lastEntriesIndex + 1;
                int entryLength = (int)Math.Log(maxEntries, 2);

                if (!bitStream.ReadBit())
                {
                    entryIndex = (int)bitStream.ReadBits(entryLength);
                }

                lastEntriesIndex = entryIndex;

                if (entryIndex < 0 || entryIndex >= maxEntries)
                {
                    Console.WriteLine("Invalid StringTableEntry Index");

                    return;
                }

                string entryName = "";

                if (bitStream.ReadBit())
                {
                    bool substringCheck = bitStream.ReadBit();

                    if (substringCheck)
                    {
                        int index = (int)bitStream.ReadBits(5);
                        int bytesToCopy = (int)bitStream.ReadBits(5);

                        entryName = entryHistory[index].Substring(0, bytesToCopy);
                        entryName += bitStream.ReadString();
                    }
                    else
                    {
                        entryName = bitStream.ReadString();
                    }
                }

                if (entryHistory.Count > 31)
                {
                    entryHistory.RemoveAt(0);
                }

                entryHistory.Add(entryName);

                byte[] entryData = null;

                if (bitStream.ReadBit())
                {
                    if (userDataIsFixedSize)
                    {
                        entryData = bitStream.ReadBitsToArray(userDataSizeInBits);
                    }
                    else
                    {
                        int bytesToRead = (int)bitStream.ReadBits(14);

                        entryData = bitStream.ReadBytes(bytesToRead);
                    }
                }

                StringTableEntry stringTableEntry = new StringTableEntry(entryName, entryIndex, entryData);

                Add(tableName, stringTableEntry);
            }
        }
Beispiel #3
0
        public ProtoMessage(byte[] protoMessageData)
        {
            this.Values = new Dictionary<String, List<Object>>();

            BitStream bitStream = new BitStream(protoMessageData);

            while ((bitStream.PositionInBytes) < bitStream.LengthInBytes)
            {
                int key = bitStream.ReadVarint32();

                int fieldNumber = key >> 3;
                int wireType = key & 7;

                object value = null;

                switch (wireType)
                {
                    case 0:
                        {
                            value = bitStream.ReadVarint32();

                            break;
                        }
                    case 1:
                        {
                            Console.WriteLine("ProtoMessage WireType 1");

                            bitStream.SeekBytes(0, SeekOrigin.End);

                            break;
                        }
                    case 2:
                        {
                            int length = bitStream.ReadVarint32();

                            byte[] buffer = bitStream.ReadBytes(length);

                            value = buffer;

                            break;
                        }
                    case 5:
                        {
                            value = bitStream.ReadFloat();

                            break;
                        }

                    default:
                        {
                            Console.WriteLine("Unknown ProtoMessage WireType: {0}", wireType);

                            bitStream.SeekBytes(0, SeekOrigin.End);

                            return;
                        }
                }

                if (value != null)
                {
                    List<Object> fieldValues;

                    if (this.Values.TryGetValue(fieldNumber.ToString(), out fieldValues))
                    {
                        fieldValues.Add(value);
                    }
                    else
                    {
                        fieldValues = new List<Object>();

                        fieldValues.Add(value);

                        this.Values.Add(fieldNumber.ToString(), fieldValues);
                    }
                }
            }
        }