Ejemplo n.º 1
0
        private string ReadString(BitStream bitStream, DataTableProperty dataTableProperty)
        {
            int length = (int)bitStream.ReadBits(9);

            string result = bitStream.ReadString(length);

            return result;
        }
Ejemplo n.º 2
0
        private float ReadFloat(BitStream bitStream, DataTableProperty dataTableProperty)
        {
            float result = 0.0f;

            if (ReadSpecialFloat(bitStream, dataTableProperty, out result))
            {
                return result;
            }

            ulong interp = bitStream.ReadBits(dataTableProperty.NumberOfBits);

            result = (float)interp / ((1 << dataTableProperty.NumberOfBits) - 1);
            result = dataTableProperty.LowValue + (dataTableProperty.HighValue - dataTableProperty.LowValue) * result;

            return result;
        }
Ejemplo n.º 3
0
 private int ReadInt32(BitStream bitStream, DataTableProperty dataTableProperty)
 {
     if (dataTableProperty.Flags.HasFlag(DataTablePropertyFlags.Varint))
     {
         if (dataTableProperty.Flags.HasFlag(DataTablePropertyFlags.Unsigned))
         {
             return (int)bitStream.ReadVarint32();
         }
         else
         {
             return (int)bitStream.ReadVarint32();
         }
     }
     else
     {
         if (dataTableProperty.Flags.HasFlag(DataTablePropertyFlags.Unsigned))
         {
             return (int)bitStream.ReadBits(dataTableProperty.NumberOfBits);
         }
         else
         {
             return (int)bitStream.ReadBits(dataTableProperty.NumberOfBits);
         }
     }
 }
Ejemplo n.º 4
0
        private float ReadBitCoordMP(BitStream bitStream, bool isIntegral, bool isLowPrecision)
        {
            float value = 0;

            bool isNegative = false;

            int integerValue = 0;
            int fractionValue = 0;

            bool inBounds = bitStream.ReadBit();

            if (isIntegral)
            {
                integerValue = bitStream.ReadBit() ? 1 : 0;

                if (integerValue == 1)
                {
                    isNegative = bitStream.ReadBit();

                    if (inBounds)
                    {
                        value = (float)(bitStream.ReadBits(11) + 1);
                    }
                    else
                    {
                        value = (float)(bitStream.ReadBits(14) + 1);
                    }
                }
            }
            else
            {
                integerValue = bitStream.ReadBit() ? 1 : 0;
                isNegative = bitStream.ReadBit();

                if (integerValue == 1)
                {
                    if (inBounds)
                    {
                        value = (float)(bitStream.ReadBits(11) + 1);
                    }
                    else
                    {
                        value = (float)(bitStream.ReadBits(14) + 1);
                    }
                }

                fractionValue = (int)bitStream.ReadBits(isLowPrecision ? 3 : 5);

                value = integerValue + ((float)fractionValue * (isLowPrecision ? HL2SDK.COORD_RESOLUTION_LOWPRECISION : HL2SDK.COORD_RESOLUTION));
            }

            if (isNegative)
            {
                value = -value;
            }

            return value;
        }
Ejemplo n.º 5
0
        private float ReadBitNormal(BitStream bitStream)
        {
            bool isNegative = bitStream.ReadBit();

            uint fractionValue = bitStream.ReadBits(HL2SDK.NORMAL_FRACTIONAL_BITS);

            float value = (float)fractionValue * HL2SDK.NORMAL_RESOLUTION;

            if (isNegative)
            {
                value = -value;
            }

            return value;
        }
Ejemplo n.º 6
0
        private float ReadBitCoord(BitStream bitStream)
        {
            float value = 0;

            bool isNegative = false;

            int integerValue = (int)bitStream.ReadBits(1);
            int fractionValue = (int)bitStream.ReadBits(1);

            if ((integerValue | fractionValue) != 0)
            {
                isNegative = bitStream.ReadBit();

                if (integerValue == 1)
                {
                    integerValue = (int)bitStream.ReadBits(14) + 1;
                }

                if (fractionValue == 1)
                {
                    fractionValue = (int)bitStream.ReadBits(HL2SDK.COORD_FRACTIONAL_BITS);
                }

                value = integerValue + ((float)fractionValue * HL2SDK.COORD_RESOLUTION);
            }

            if (isNegative)
            {
                value = -value;
            }

            return value;
        }
Ejemplo n.º 7
0
        private float ReadBitCellCoord(BitStream bitStream, int numberOfBits, bool isIntegral, bool isLowPrecision)
        {
            float value = 0.0f;

            int integerValue = 0;
            int fractionValue = 0;

            if (isIntegral)
            {
                value = (float)bitStream.ReadBits(numberOfBits);
            }
            else
            {
                integerValue = (int)bitStream.ReadBits(numberOfBits);
                fractionValue = (int)bitStream.ReadBits(isLowPrecision ? HL2SDK.COORD_FRACTIONAL_BITS_MP_LOWPRECISION : HL2SDK.COORD_FRACTIONAL_BITS);

                value = integerValue + ((float)fractionValue * (isLowPrecision ? HL2SDK.COORD_RESOLUTION_LOWPRECISION : HL2SDK.COORD_RESOLUTION));
            }

            return value;
        }
Ejemplo n.º 8
0
        private object[] ReadArray(BitStream bitStream, ServerClassProperty serverClassProperty)
        {
            int numberOfElements = serverClassProperty.DataTableProperty.NumberOfElements;
            int maxElements = numberOfElements;

            int numberOfBits = 1;

            while ((maxElements >>= 1) != 0)
            {
                numberOfBits++;
            }

            int count = (int)bitStream.ReadBits(numberOfBits);

            object[] result = new object[count];

            ServerClassProperty intermediaryServerClassProperty = new ServerClassProperty("", serverClassProperty.ArrayElementProperty, null);

            for (int i = 0; i < count; i++)
            {
                result[i] = ReadServerClassProperty(bitStream, intermediaryServerClassProperty);
            }

            return result;
        }
Ejemplo n.º 9
0
        private void PacketEntities(byte[] packetBuffer)
        {
            PacketEntities packetEntitys = new PacketEntities(packetBuffer);

            BitStream bitStream = new BitStream(packetEntitys.EntityData);

            int entityIndex = -1;
            EntityHeaderFlags entityHeaderFlags = EntityHeaderFlags.FHDR_ZERO;
            EntityUpdateType entityUpdateType = EntityUpdateType.PreserveEnt;
            bool isDelta = packetEntitys.IsDelta;
            int lastEntityIndex = -1;
            int updatedEntries = packetEntitys.UpdatedEntries;

            while (entityUpdateType < EntityUpdateType.Finished)
            {
                updatedEntries--;

                bool isEntity = (updatedEntries >= 0) ? true : false;

                if (isEntity)
                {
                    entityHeaderFlags = EntityHeaderFlags.FHDR_ZERO;

                    entityIndex = (int)(bitStream.ReadUBitInt() + lastEntityIndex + 1);
                    lastEntityIndex = entityIndex;

                    if (!bitStream.ReadBit())
                    {
                        if (bitStream.ReadBit())
                        {
                            entityHeaderFlags |= EntityHeaderFlags.FHDR_ENTERPVS;
                        }
                    }
                    else
                    {
                        entityHeaderFlags |= EntityHeaderFlags.FHDR_LEAVEPVS;

                        if (bitStream.ReadBit())
                        {
                            entityHeaderFlags |= EntityHeaderFlags.FHDR_DELETE;
                        }
                    }
                }

                for (entityUpdateType = EntityUpdateType.PreserveEnt; entityUpdateType == EntityUpdateType.PreserveEnt;)
                {
                    if (!isEntity || entityIndex > HL2SDK.ENTITY_SENTINEL)
                    {
                        entityUpdateType = EntityUpdateType.Finished;
                    }
                    else
                    {
                        if (entityHeaderFlags.HasFlag(EntityHeaderFlags.FHDR_ENTERPVS))
                        {
                            entityUpdateType = EntityUpdateType.EnterPVS;
                        }
                        else if (entityHeaderFlags.HasFlag(EntityHeaderFlags.FHDR_LEAVEPVS))
                        {
                            entityUpdateType = EntityUpdateType.LeavePVS;
                        }
                        else
                        {
                            entityUpdateType = EntityUpdateType.DeltaEnt;
                        }
                    }

                    switch (entityUpdateType)
                    {
                        case EntityUpdateType.DeltaEnt:
                            {
                                Entity entity = m_Demo.Entities.GetEntity(entityIndex);

                                if (entity != null)
                                {
                                    entity.Update(bitStream, true);
                                }
                                else
                                {
                                    Console.WriteLine("EntityUpdateType.DeltaEnt: entity == null");

                                    throw new Exception("EntityUpdateType.DeltaEnt: entity == null");
                                }

                                break;
                            }
                        case EntityUpdateType.EnterPVS:
                            {
                                uint serverClassID = bitStream.ReadBits((int)Math.Ceiling(Math.Log(m_Demo.ServerClasses.Count, 2)));
                                uint seiral = bitStream.ReadBits(10);

                                ServerClass serverClass = m_Demo.ServerClasses.GetServerClass((int)serverClassID);

                                Entity entity = new Entity(m_Demo, entityIndex, seiral, serverClass);

                                entity.Update(bitStream, false);

                                m_Demo.Entities.Add(entity);

                                break;
                            }
                        case EntityUpdateType.LeavePVS:
                            {
                                if (!isDelta)
                                {
                                    Console.WriteLine("WARNING: EntityUpdateType.LeavePVS on Full Update");

                                    entityUpdateType = EntityUpdateType.Failed;

                                    throw new Exception("WARNING: EntityUpdateType.LeavePVS on Full Update");
                                }
                                else
                                {
                                    m_Demo.Entities.Remove(entityIndex);
                                }

                                break;
                            }
                        case EntityUpdateType.PreserveEnt:
                            {
                                if (!isDelta)
                                {
                                    Console.WriteLine("WARNING: EntityUpdateType.PreserveEnt on Full Update");

                                    entityUpdateType = EntityUpdateType.Failed;

                                    throw new Exception("WARNING: EntityUpdateType.PreserveEnt on Full Update");
                                }
                                else
                                {
                                    if (entityIndex >= HL2SDK.MAX_EDICTS)
                                    {
                                        Console.WriteLine("EntityUpdateType.PreserveEnt: entityIndex == MAX_EDICTS");

                                        throw new Exception("EntityUpdateType.PreserveEnt: entityIndex == MAX_EDICTS");
                                    }
                                    else
                                    {
                                    }
                                }

                                break;
                            }
                    }
                }
            }
        }
Ejemplo n.º 10
0
        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);
            }
        }