Пример #1
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            CRC32 = reader.ReadUInt32();

            var size = reader.ReadUInt16();

            for (var i = 0; i < size; i++)
            {
                var entry = new NameEntry
                {
                    Name = reader.ReadNullTermString(Encoding.UTF8),
                    Unknown1 = reader.ReadUInt32(), // TODO: This might be uint64 and be m_nId, same as RERL
                    Unknown2 = reader.ReadUInt32(),
                };

                Names.Add(entry);
            }

            var headerSize = reader.BaseStream.Position - Offset;

            Data = reader.ReadBytes((int)Size - (int)headerSize);

            if (Crc32.Compute(Data) != CRC32)
            {
                throw new InvalidDataException("CRC32 mismatch for read data.");
            }
        }
Пример #2
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = this.Offset;

            CRC32 = reader.ReadUInt32();

            var size = reader.ReadUInt16();
            int headerSize = 4 + 2;

            for (var i = 0; i < size; i++)
            {
                var entry = new NameEntry
                {
                    Name = reader.ReadNullTermString(Encoding.UTF8),
                    CRC32 = reader.ReadUInt32(),
                };

                Names.Add(entry);

                headerSize += entry.Name.Length + 1 + 4; // string length + null byte + 4 bytes
            }

            Data = reader.ReadBytes((int)this.Size - headerSize);

            if (Crc32.Compute(Data) != CRC32)
            {
                throw new InvalidDataException("CRC32 mismatch for read data.");
            }
        }
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var offset = reader.ReadUInt32();
            var size = reader.ReadUInt32();

            if (size == 0)
            {
                return;
            }

            reader.BaseStream.Position += offset - 8; // 8 is 2 uint32s we just read

            for (var i = 0; i < size; i++)
            {
                var resInfo = new ResourceReferenceInfo();
                resInfo.Id = reader.ReadUInt64();

                var previousPosition = reader.BaseStream.Position;

                // jump to string
                // offset is counted from current position,
                // so we will need to add 8 to position later
                reader.BaseStream.Position += reader.ReadInt64();

                resInfo.Name = reader.ReadNullTermString(Encoding.UTF8);

                ResourceRefInfoList.Add(resInfo);

                reader.BaseStream.Position = previousPosition + 8; // 8 is to account for string offset
            }
        }
        public override void Deserialize( Stream stream )
        {
            base.Deserialize( stream );

            using ( var br = new BinaryReader( stream, Encoding.UTF8, true ) )
            {
                CilentChallenge = br.ReadInt32();
                Reason = br.ReadNullTermString();
            }
        }
Пример #5
0
        /// <summary>
        /// Reads the given <see cref="Stream"/>.
        /// </summary>
        /// <param name="input">The input <see cref="Stream"/> to read from.</param>
        /// <returns>Returns true if this archive contains inline entries, false otherwise.</returns>
        public bool Read(Stream input)
        {
            if (FileName == null)
            {
                throw new InvalidOperationException("If you call Read() directly with a stream, you must call SetFileName() first.");
            }

            Reader = new BinaryReader(input);

            if (Reader.ReadUInt32() != Package.MAGIC)
            {
                throw new InvalidDataException("Given file is not a VPK.");
            }

            Version = Reader.ReadUInt32();
            TreeSize = Reader.ReadUInt32();

            if (Version == 1)
            {
                // Nothing else
            }
            else if (Version == 2)
            {
                FileDataSectionSize = Reader.ReadUInt32();
                ArchiveMD5SectionSize = Reader.ReadUInt32();
                OtherMD5SectionSize = Reader.ReadUInt32();
                SignatureSectionSize = Reader.ReadUInt32();
            }
            else
            {
                throw new InvalidDataException(string.Format("Bad VPK version. ({0})", Version));
            }

            var typeEntries = new Dictionary<string, List<PackageEntry>>();
            bool hasInlineEntries = false;

            // Types
            while (true)
            {
                string typeName = Reader.ReadNullTermString(Encoding.UTF8);

                if (typeName == "")
                {
                    break;
                }

                var entries = new List<PackageEntry>();

                // Directories
                while (true)
                {
                    string directoryName = Reader.ReadNullTermString(Encoding.UTF8);

                    if (directoryName == "")
                    {
                        break;
                    }

                    // Files
                    while (true)
                    {
                        string fileName = Reader.ReadNullTermString(Encoding.UTF8);

                        if (fileName == "")
                        {
                            break;
                        }

                        var entry = new PackageEntry();
                        entry.FileName = fileName;
                        entry.DirectoryName = directoryName.Replace("/", "\\");
                        entry.TypeName = typeName;
                        entry.CRC32 = Reader.ReadUInt32();
                        entry.SmallData = new byte[Reader.ReadUInt16()];
                        entry.ArchiveIndex = Reader.ReadUInt16();
                        entry.Offset = Reader.ReadUInt32();
                        entry.Length = Reader.ReadUInt32();

                        if (Reader.ReadUInt16() != 0xFFFF)
                        {
                            throw new FormatException("Invalid terminator.");
                        }

                        if (entry.SmallData.Length > 0)
                        {
                            Reader.Read(entry.SmallData, 0, entry.SmallData.Length);
                        }

                        if (entry.ArchiveIndex == 0x7FFF)
                        {
                            hasInlineEntries = true;
                        }

                        entries.Add(entry);
                    }
                }

                typeEntries.Add(typeName, entries);
            }

            Entries = typeEntries;

            OffsetAfterHeader = (uint)Reader.BaseStream.Position;

            return hasInlineEntries;
        }
Пример #6
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var vertexOffset = reader.ReadUInt32();
            var vertexCount = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + vertexOffset;
            for (var i = 0; i < vertexCount; i++)
            {
                var vertexBuffer = default(VertexBuffer);

                vertexBuffer.Count = reader.ReadUInt32();            //0
                vertexBuffer.Size = reader.ReadUInt32();             //4

                var refA = reader.BaseStream.Position;
                var attributeOffset = reader.ReadUInt32();  //8
                var attributeCount = reader.ReadUInt32();   //12

                //TODO: Read attributes in the future
                var refB = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();       //16
                var totalSize = reader.ReadUInt32();        //20

                vertexBuffer.Attributes = new List<VertexAttribute>();

                reader.BaseStream.Position = refA + attributeOffset;
                for (var j = 0; j < attributeCount; j++)
                {
                    var previousPosition = reader.BaseStream.Position;

                    var attribute = default(VertexAttribute);

                    attribute.Name = reader.ReadNullTermString(Encoding.UTF8);

                    // Offset is always 40 bytes from the start
                    reader.BaseStream.Position = previousPosition + 36;

                    attribute.Type = (DXGI_FORMAT)reader.ReadUInt32();
                    attribute.Offset = reader.ReadUInt32();

                    // There's unusual amount of padding in attributes
                    reader.BaseStream.Position = previousPosition + 56;

                    vertexBuffer.Attributes.Add(attribute);
                }

                reader.BaseStream.Position = refB + dataOffset;

                vertexBuffer.Buffer = reader.ReadBytes((int)vertexBuffer.Count * (int)vertexBuffer.Size);
                VertexBuffers.Add(vertexBuffer);

                reader.BaseStream.Position = refB + 4 + 4; //Go back to the vertex array to read the next iteration

                //if(i > 0)break; // TODO: Read only first buffer
            }

            reader.BaseStream.Position = Offset + 4 + 4; //We are back at the header.

            var indexOffset = reader.ReadUInt32();
            var indexCount = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + 8 + indexOffset; //8 to take into account vertexOffset / count
            for (var i = 0; i < indexCount; i++)
            {
                var indexBuffer = default(IndexBuffer);

                indexBuffer.Count = reader.ReadUInt32();        //0
                indexBuffer.Size = reader.ReadUInt32();         //4

                var unknown1 = reader.ReadUInt32();     //8
                var unknown2 = reader.ReadUInt32();     //12

                var refC = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();   //16
                var dataSize = reader.ReadUInt32();     //20

                reader.BaseStream.Position = refC + dataOffset;

                indexBuffer.Buffer = reader.ReadBytes((int)indexBuffer.Count * (int)indexBuffer.Size);
                IndexBuffers.Add(indexBuffer);

                reader.BaseStream.Position = refC + 4 + 4; //Go back to the index array to read the next iteration.

                //if(i > 0)break; // TODO: Read only first buffer
            }
        }
Пример #7
0
        public void Update()
        {
            byte[] message;
            while ((message = GetIncomingMessage()) != null)
            {
                BinaryReader reader = new BinaryReader(new MemoryStream(message));
                byte id = reader.ReadByte();
                switch (id)
                {
                    // World Update
                    case 16:

                        // Eat events
                        short eats = reader.ReadInt16();
                        for (int i = 0; i < eats; i++)
                        {
                            int eater = reader.ReadInt32();
                            int victim = reader.ReadInt32();
                        }

                        // Blob update
                        int player;
                        while ((player = reader.ReadInt32()) != 0)
                        {
                            int x = reader.ReadInt32();
                            int y = reader.ReadInt32();
                            short size = reader.ReadInt16();
                            byte r = reader.ReadByte(), g = reader.ReadByte(), b = reader.ReadByte();
                            byte flags = reader.ReadByte();
                            if ((flags & 0x02) > 0)
                            {
                                int skip = reader.ReadInt32();
                            }
                            if ((flags & 0x04) > 0)
                            {
                                string str = reader.ReadNullTermString();
                            }

                            string name = reader.ReadNullTermStringUnicode();

                            var blob = World.Instance.GetBlobById(player);
                            blob.UpdatePosition(new Vector2(x, y));
                            blob.UpdateSize(size);
                            blob.r = r;
                            blob.g = g;
                            blob.b = b;
                            blob.flags = flags;
                        }

                        int removals = reader.ReadInt32();
                        for (int i = 0; i < removals; i++)
                        {
                            int blob = reader.ReadInt32();
                            World.Instance.RemoveBlob(blob);
                        }

                        break;

                    // Owns cell
                    case 32:
                        int cell = reader.ReadInt32();
                        World.Instance.playerBlobs.Add(cell);
                        Log.Print($"Player owns {cell}");
                        break;

                    // World size
                    case 64:
                        float min_x = (float)reader.ReadDouble();
                        float min_y = (float)reader.ReadDouble();
                        float max_x = (float)reader.ReadDouble();
                        float max_y = (float)reader.ReadDouble();
                        World.Instance.boundsMin = new Vector2(min_x, min_y);
                        World.Instance.boundsMax = new Vector2(max_x, max_y);
                        Log.Print($"World size: \n\tX: {min_x} - {max_x}\n\tY: {min_y} - {max_y}");
                        socket.Send(new Spawn("TEST CLIENT").GetBytes()); // Request player spawn
                        break;
                }
            }
        }
Пример #8
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;
            var outStream = new MemoryStream();
            BinaryWriter outWrite = new BinaryWriter(outStream);
            BinaryReader outRead = new BinaryReader(outStream); // Why why why why why why why

            var sig = reader.ReadBytes(4);
            if (Encoding.ASCII.GetString(sig) != Encoding.ASCII.GetString(SIG))
            {
                throw new InvalidDataException("Invalid KV Signature");
            }

            // outWrite.Write(sig);
            var encoding = reader.ReadBytes(16);
            if (Encoding.ASCII.GetString(encoding) != Encoding.ASCII.GetString(ENCODING))
            {
                throw new InvalidDataException("Unrecognized KV3 Encoding");
            }

            // outWrite.Write(encoding);
            var format = reader.ReadBytes(16);
            if (Encoding.ASCII.GetString(format) != Encoding.ASCII.GetString(FORMAT))
            {
                throw new InvalidDataException("Unrecognised KV3 Format");
            }

            // outWrite.Write(format);

            // Ok we are 100% sure its now KV, good

            // It is flags, right?
            var flags = reader.ReadUInt32(); // TODO: Figure out what this is
            // outWrite.Write(flags);
            if ((flags & 0x80000000) > 0)
            {
                outWrite.Write(reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)));
            }
            else
            {
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    try
                    {
                        var blockMask = reader.ReadUInt16();
                        for (int i = 0; i < 16; i++)
                        {
                            // is the ith bit 1
                            if ((blockMask & (1 << i)) > 0)
                            {
                                var offsetSize = reader.ReadUInt16();
                                var offset = ((offsetSize & 0xFFF0) >> 4) + 1;
                                var size = (offsetSize & 0x000F) + 3;

                                var lookupSize = (offset < size) ? offset : size; // If the offset is larger or equal to the size, use the size instead.

                                // Kill me now
                                var p = outRead.BaseStream.Position;
                                outRead.BaseStream.Position = p - offset;
                                var data = outRead.ReadBytes(lookupSize);
                                outWrite.BaseStream.Position = p;

                                while (size > 0)
                                {
                                    outWrite.Write(data, 0, (lookupSize < size) ? lookupSize : size);
                                    size -= lookupSize;
                                }
                            }
                            else
                            {
                                var data = reader.ReadByte();
                                outWrite.Write(data);
                            }
                        }
                    }
                    catch (EndOfStreamException e)
                    {
                        break;
                    }
                }
            }

            outRead.BaseStream.Position = 0;
            var stringCount = outRead.ReadUInt32(); //Assuming UInt
            stringArray = new string[stringCount];
            for (var i = 0; i < stringCount; i++)
            {
                stringArray[i] = outRead.ReadNullTermString(Encoding.UTF8);
            }

            Data = ParseBinaryKV3(outRead, null, true);
        }
        public override void Read(BinaryReader reader, Resource resource)
        {
            base.Read(reader, resource);

            // Output is PermEntityLumpData_t we need to iterate m_entityKeyValues inside it.
            var entityKeyValues = (NTROArray)Output["m_entityKeyValues"];

            Datas = new List<List<Tuple<uint, uint, object>>>();

            foreach (var entityKV in entityKeyValues)
            {
                // entity is EntityKeyValueData_t
                var entity = ((NTROValue<NTROStruct>)entityKV).Value;
                var dataArray = (NTROArray)entity["m_keyValuesData"];
                var data = new List<byte>();
                foreach (NTROValue<byte> entry in dataArray)
                {
                    data.Add(entry.Value);
                }

                using (var dataStream = new MemoryStream(data.ToArray()))
                using (var dataReader = new BinaryReader(dataStream))
                {
                    var a = dataReader.ReadUInt32(); // always 1?
                    var valuesCount = dataReader.ReadUInt32();
                    var c = dataReader.ReadUInt32(); // always 0?

                    var values = new List<Tuple<uint, uint, object>>();
                    while (dataStream.Position != dataStream.Length)
                    {
                        var miscType = dataReader.ReadUInt32(); //Stuff before type, some pointer?
                        var type = dataReader.ReadUInt32();
                        switch (type)
                        {
                            case 0x06:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadByte())); //1
                                break;
                            case 0x01:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadSingle())); //4
                                break;
                            case 0x05:
                            case 0x09:
                            case 0x25: //TODO: figure out the difference
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadBytes(4))); //4
                                break;
                            case 0x1a:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadUInt64())); //8
                                break;
                            case 0x03:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, $"{{{dataReader.ReadSingle()}, {dataReader.ReadSingle()}, {dataReader.ReadSingle()}}}")); //12
                                break;
                            case 0x27:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadBytes(12))); //12
                                break;
                            case 0x1e:
                                values.Add(new Tuple<uint, uint, object>(type, miscType, dataReader.ReadNullTermString(Encoding.UTF8)));
                                break;
                            default:
                                throw new NotImplementedException($"Unknown type {type}");
                        }
                    }

                    Datas.Add(values);
                }
            }
        }