Esempio n. 1
0
File: Mpq.cs Progetto: Frassle/Ibasa
        public Mpq(Stream stream)
        {
            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            long offset = 0;
            bool founda, foundb;
            {
                Ibasa.Media.FourCC mpqa = new Media.FourCC("MPQ\x1A");
                Ibasa.Media.FourCC mpqb = new Media.FourCC("MPQ\x1B");

                //find offset
                var fourcc = Reader.Read<Ibasa.Media.FourCC>();
                founda = fourcc == mpqa;
                foundb = fourcc == mpqb;

                while (!founda && !foundb)
                {
                    offset += 512;
                    fourcc = Reader.Read<Ibasa.Media.FourCC>();
                    founda = fourcc == mpqa;
                    foundb = fourcc == mpqb;
                }
            }

            if (foundb)
            {
                var userdatasize = Reader.ReadUInt32();
                offset += Reader.ReadUInt32();
                var userdataheader = Reader.ReadUInt32();
            }

            Reader.Seek(offset, SeekOrigin.Begin);

            if (Reader.Read<Media.FourCC>() != new Media.FourCC("MPQ\x1A"))
                throw new Exception("WTF");

            HeaderSize = Reader.ReadUInt32();
            ArchiveSize = Reader.ReadUInt32();
            FormatVersion = Reader.ReadUInt16();
            BlockSize = Reader.ReadUInt16();
            HashTablePos = Reader.ReadUInt32();
            BlockTablePos = Reader.ReadUInt32();
            HashTableSize = Reader.ReadUInt32();
            BlockTableSize = Reader.ReadUInt32();

            if (FormatVersion >= 2)
            {
                HiBlockTablePos64 = Reader.ReadUInt64();
                HashTablePosHi = Reader.ReadUInt16();
                BlockTablePosHi = Reader.ReadUInt16();
            }
            if (FormatVersion >= 3)
            {
                ArchiveSize64 = Reader.ReadUInt64();
                BetTablePos64 = Reader.ReadUInt64();
                HetTablePos64 = Reader.ReadUInt64();
            }
            if (FormatVersion >= 4)
            {
                hashtablesize64 = Reader.ReadUInt64();
                blocktablesize64 = Reader.ReadUInt64();
                hiblocktablesize64 = Reader.ReadUInt64();
                hettablesize64 = Reader.ReadUInt64();
                bettablesize64 = Reader.ReadUInt64();
                dawchunksize = Reader.ReadUInt32();
                md5_blocktable = Reader.ReadBytes(16);
                md5_hashtable = Reader.ReadBytes(16);
                md5_hiblocktable = Reader.ReadBytes(16);
                md5_bettable = Reader.ReadBytes(16);
                md5_hettable = Reader.ReadBytes(16);
                md5_mpqheader = Reader.ReadBytes(16);
            }

            if (HetTablePos64 != 0)
            {
                ParseHET();
            }
            if (BetTablePos64 != 0)
            {
                ParseBET();
            }
        }
Esempio n. 2
0
        public Mpq(Stream stream)
        {
            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            long offset = 0;
            bool founda, foundb;

            {
                Ibasa.Media.FourCC mpqa = new Media.FourCC("MPQ\x1A");
                Ibasa.Media.FourCC mpqb = new Media.FourCC("MPQ\x1B");

                //find offset
                var fourcc = Reader.Read <Ibasa.Media.FourCC>();
                founda = fourcc == mpqa;
                foundb = fourcc == mpqb;

                while (!founda && !foundb)
                {
                    offset += 512;
                    fourcc  = Reader.Read <Ibasa.Media.FourCC>();
                    founda  = fourcc == mpqa;
                    foundb  = fourcc == mpqb;
                }
            }

            if (foundb)
            {
                var userdatasize = Reader.ReadUInt32();
                offset += Reader.ReadUInt32();
                var userdataheader = Reader.ReadUInt32();
            }

            Reader.Seek(offset, SeekOrigin.Begin);

            if (Reader.Read <Media.FourCC>() != new Media.FourCC("MPQ\x1A"))
            {
                throw new Exception("WTF");
            }

            HeaderSize     = Reader.ReadUInt32();
            ArchiveSize    = Reader.ReadUInt32();
            FormatVersion  = Reader.ReadUInt16();
            BlockSize      = Reader.ReadUInt16();
            HashTablePos   = Reader.ReadUInt32();
            BlockTablePos  = Reader.ReadUInt32();
            HashTableSize  = Reader.ReadUInt32();
            BlockTableSize = Reader.ReadUInt32();

            if (FormatVersion >= 2)
            {
                HiBlockTablePos64 = Reader.ReadUInt64();
                HashTablePosHi    = Reader.ReadUInt16();
                BlockTablePosHi   = Reader.ReadUInt16();
            }
            if (FormatVersion >= 3)
            {
                ArchiveSize64 = Reader.ReadUInt64();
                BetTablePos64 = Reader.ReadUInt64();
                HetTablePos64 = Reader.ReadUInt64();
            }
            if (FormatVersion >= 4)
            {
                hashtablesize64    = Reader.ReadUInt64();
                blocktablesize64   = Reader.ReadUInt64();
                hiblocktablesize64 = Reader.ReadUInt64();
                hettablesize64     = Reader.ReadUInt64();
                bettablesize64     = Reader.ReadUInt64();
                dawchunksize       = Reader.ReadUInt32();
                md5_blocktable     = Reader.ReadBytes(16);
                md5_hashtable      = Reader.ReadBytes(16);
                md5_hiblocktable   = Reader.ReadBytes(16);
                md5_bettable       = Reader.ReadBytes(16);
                md5_hettable       = Reader.ReadBytes(16);
                md5_mpqheader      = Reader.ReadBytes(16);
            }

            if (HetTablePos64 != 0)
            {
                ParseHET();
            }
            if (BetTablePos64 != 0)
            {
                ParseBET();
            }
        }
Esempio n. 3
0
File: Vtf.cs Progetto: Frassle/Ibasa
        public void Load(Stream stream)
        {
            var reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            string signature = Encoding.ASCII.GetString(reader.ReadBytes(4));

            if (signature != "VTF\0")
                throw new InvalidDataException("File signature does not match 'VTF'.");

            VersionMajor = reader.ReadInt32();
            VersionMinor = reader.ReadInt32();

            if (VersionMajor != 7 && (VersionMinor < 0 || VersionMinor > 5))
                throw new InvalidDataException(string.Format("File version {0}.{1} does not match 7.0 to 7.5.", VersionMajor, VersionMinor));

            HeaderSize = reader.ReadInt32();

            ReadHeader70(reader);
            ReadHeader72(reader);
            ReadHeader73(reader);

            ReadData(reader);
        }
Esempio n. 4
0
        private void ReadExtension(Ibasa.IO.BinaryReader reader)
        {
            int size = reader.ReadUInt16();

            if (size < 495) //size can grow with new versions
            {
                return;
            }

            Extension.AuthorName          = reader.ReadBytes(41);
            Extension.AuthorComments      = reader.ReadBytes(324);
            Extension.DateTimeStampMonth  = reader.ReadUInt16();
            Extension.DateTimeStampDay    = reader.ReadUInt16();
            Extension.DateTimeStampYear   = reader.ReadUInt16();
            Extension.DateTimeStampHour   = reader.ReadUInt16();
            Extension.DateTimeStampMinute = reader.ReadUInt16();
            Extension.DateTimeStampSecond = reader.ReadUInt16();
            Extension.JobId                 = reader.ReadBytes(41);
            Extension.JobTimeHours          = reader.ReadUInt16();
            Extension.JobTimeMinutes        = reader.ReadUInt16();
            Extension.JobTimeSeconds        = reader.ReadUInt16();
            Extension.SoftwareId            = reader.ReadBytes(41);
            Extension.SoftwareVersionNumber = reader.ReadUInt16();
            Extension.SoftwareVersionLetter = reader.ReadByte();
            Extension.KeyColor              = reader.ReadInt32();
            Extension.PixelAspectRatioN     = reader.ReadUInt16();
            Extension.PixelAspectRatioD     = reader.ReadUInt16();
            Extension.GammaValueN           = reader.ReadUInt16();
            Extension.GammaValueD           = reader.ReadUInt16();
            long colorCorrectionOffset = reader.ReadUInt32();
            long postageStampOffset    = reader.ReadUInt32();
            long scanLineOffset        = reader.ReadUInt32();

            Extension.Attributes = reader.ReadByte();

            //read color correction
            if (colorCorrectionOffset != 0)
            {
                reader.BaseStream.Seek(colorCorrectionOffset, SeekOrigin.Begin);
                Extension.ColorCorrection = new Vector4i[256];
                for (int i = 0; i < 256; ++i)
                {
                    int a = reader.ReadUInt16();
                    int r = reader.ReadUInt16();
                    int g = reader.ReadUInt16();
                    int b = reader.ReadUInt16();

                    Extension.ColorCorrection[i] = new Vector4i(r, g, b, a);
                }
            }
            //read scan lines
            if (scanLineOffset != 0)
            {
                reader.BaseStream.Seek(scanLineOffset, SeekOrigin.Begin);
                Extension.ScanLines = new long[Header.Height];
                for (int i = 0; i < Header.Height; ++i)
                {
                    Extension.ScanLines[i] = reader.ReadUInt32();
                }
            }
            //read postage stamp
            if (postageStampOffset != 0)
            {
                reader.BaseStream.Seek(postageStampOffset, SeekOrigin.Begin);

                int width  = reader.ReadByte();
                int height = reader.ReadByte();
                Extension.PostageStamp = ReadData(reader, width, height);
            }
        }
Esempio n. 5
0
File: Bsp.cs Progetto: Frassle/Ibasa
        public Bsp(Stream stream, IEnumerable<Package.Wad> wads)
        {
            if (!stream.CanSeek || !stream.CanRead)
                throw new ArgumentException("stream must be seekable and readable.", "stream");
            if (wads == null)
                wads = System.Linq.Enumerable.Empty<Package.Wad>();

            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            if (Reader.ReadUInt32() != 30)
                throw new InvalidDataException("Version is not 30.");

            long entitiesOffset = Reader.ReadUInt32();
            long entitiesCount = Reader.ReadUInt32();
            long planesOffset = Reader.ReadUInt32();
            long planesCount = Reader.ReadUInt32();
            long texturesOffset = Reader.ReadUInt32();
            long texturesCount = Reader.ReadUInt32();
            long vertexesOffset = Reader.ReadUInt32();
            long vertexesCount = Reader.ReadUInt32();
            long visibilityOffset = Reader.ReadUInt32();
            long visibilityCount = Reader.ReadUInt32();
            long nodesOffset = Reader.ReadUInt32();
            long nodesCount = Reader.ReadUInt32();
            long texinfoOffset = Reader.ReadUInt32();
            long texinfoCount = Reader.ReadUInt32();
            long facesOffset = Reader.ReadUInt32();
            long facesCount = Reader.ReadUInt32();
            long lightingOffset = Reader.ReadUInt32();
            long lightingCount = Reader.ReadUInt32();
            long clipnodesOffset = Reader.ReadUInt32();
            long clipnodesCount = Reader.ReadUInt32();
            long leafsOffset = Reader.ReadUInt32();
            long leafsCount = Reader.ReadUInt32();
            long marksurfacesOffset = Reader.ReadUInt32();
            long marksurfacesCount = Reader.ReadUInt32();
            long edgesOffset = Reader.ReadUInt32();
            long edgesCount = Reader.ReadUInt32();
            long surfedgesOffset = Reader.ReadUInt32();
            long surfedgesCount = Reader.ReadUInt32();
            long modelsOffset = Reader.ReadUInt32();
            long modelsCount = Reader.ReadUInt32();

            //Entities
            Reader.Seek(entitiesOffset, SeekOrigin.Begin);
            Entities = Encoding.ASCII.GetString(Reader.ReadBytes((int)entitiesCount));

            //Planes
            Reader.Seek(planesOffset, SeekOrigin.Begin);
            Planes = new Planef[planesCount / 20];
            for (int i = 0; i < Planes.Length; ++i)
            {
                Planes[i] = new Planef(
                    Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle());

                uint type = Reader.ReadUInt32();
            }

            //Textures
            {
                Reader.Seek(texturesOffset, SeekOrigin.Begin);
                long[] offsets = new long[Reader.ReadUInt32()];
                for (int i = 0; i < offsets.Length; ++i)
                {
                    offsets[i] = Reader.ReadUInt32();
                }

                Textures = new Resource[offsets.Length];
                for (int i = 0; i < offsets.Length; ++i)
                {
                    Reader.Seek(texturesOffset + offsets[i], SeekOrigin.Begin);

                    string name = Encoding.ASCII.GetString(Reader.ReadBytes(16));
                    int nullbyte = name.IndexOf('\0');
                    name = nullbyte == -1 ? name : name.Substring(0, nullbyte);

                    int width = Reader.ReadInt32();
                    int height = Reader.ReadInt32();

                    long[] dataoffsets = new long[4];
                    dataoffsets[0] = Reader.ReadUInt32();
                    dataoffsets[1] = Reader.ReadUInt32();
                    dataoffsets[2] = Reader.ReadUInt32();
                    dataoffsets[3] = Reader.ReadUInt32();

                    Resource resource = null;

                    if (dataoffsets.All(o => o == 0))
                    {
                        foreach (var wad in wads)
                        {
                            resource = wad[name];
                            if (resource != null)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        resource = new Resource(new Size3i(width, height, 1), 4, 1, Format.R8G8B8A8UNorm);

                        byte[][] images = new byte[4][];

                        for (int mipSlice = 0; mipSlice < 4; ++mipSlice)
                        {
                            Reader.Seek(texturesOffset + offsets[i] + dataoffsets[mipSlice], SeekOrigin.Begin);

                            images[mipSlice] = Reader.ReadBytes(width * height);
                            width >>= 1; height >>= 1;
                        }

                        Reader.Seek(2, SeekOrigin.Current);
                        byte[] pallet = Reader.ReadBytes(256 * 3);

                        for (int mipSlice = 0; mipSlice < 4; ++mipSlice)
                        {
                            byte[] data = resource[mipSlice, 0];
                            byte[] image = images[mipSlice];

                            for (int j = 0; j < image.Length; ++j)
                            {
                                int palletIndex = image[j] * 3;
                                int dataIndex = j * 3;

                                data[dataIndex + 0] = pallet[palletIndex + 0];
                                data[dataIndex + 1] = pallet[palletIndex + 1];
                                data[dataIndex + 2] = pallet[palletIndex + 2];
                            }
                        }
                    }

                    Textures[i] = resource;
                }
            }

            //Vertices
            Reader.Seek(vertexesOffset, SeekOrigin.Begin);
            Vertices = new Vector3f[vertexesCount / 12];
            for (int i = 0; i < Vertices.Length; ++i)
            {
                Vertices[i] = new Vector3f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle());
            }

            //Visibility
            Reader.Seek(visibilityOffset, SeekOrigin.Begin);
            Visibility = Reader.ReadBytes((int)visibilityCount);

            //Nodes

            //Surfaces
            Reader.Seek(texinfoOffset, SeekOrigin.Begin);
            Surfaces = new Surface[texinfoCount / 24];
            for (int i = 0; i < Surfaces.Length; ++i)
            {
                Surfaces[i] = new Surface(
                    new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()),
                    new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()),
                    Reader.ReadInt32(), Reader.ReadInt32());
            }

            //Faces
            Reader.Seek(facesOffset, SeekOrigin.Begin);
            Faces = new Face[facesCount / 20];
            for (int i = 0; i < Faces.Length; ++i)
            {
                Faces[i] = new Face(
                    Reader.ReadUInt16(), Reader.ReadInt16(),
                    Reader.ReadInt32(), Reader.ReadInt16(), Reader.ReadInt16(),
                    Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(),
                    Reader.ReadInt32());
            }

            //Lighting
            Reader.Seek(facesOffset, SeekOrigin.Begin);
            Lighting = Reader.ReadBytes((int)lightingCount);

            //#define clipnodes     9
            //#define leafs        10
            //#define marksurfaces 11
            //#define edges        12
            //#define surfedges    13
            //#define models       14
            
            //Edges
            Reader.Seek(edgesOffset, SeekOrigin.Begin);
            Edges = new Edge[edgesCount / 4];
            for (int i = 0; i < Edges.Length; ++i)
            {
                Edges[i] = new Edge(Reader.ReadUInt16(), Reader.ReadUInt16());
            }

            //SurfaceEdges
            Reader.Seek(surfedgesOffset, SeekOrigin.Begin);
            SurfaceEdges = new int[surfedgesCount / 4];
            for (int i = 0; i < Planes.Length; ++i)
            {
                SurfaceEdges[i] = Reader.ReadInt32();
            }
        }
Esempio n. 6
0
File: Gcf.cs Progetto: Frassle/Ibasa
        public Gcf(Stream stream)
        {
            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            {
                long seek = Reader.Position;
                uint checksum = Reader.ReadBytes(10 * 4).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                if (Reader.ReadUInt32() != 0x00000001) // HeaderVersion Always 0x00000001
                {
                    throw new InvalidDataException("Header version mismatch.");
                }
                if (Reader.ReadUInt32() != 0x00000001) // MajorVersion Always 0x00000001
                {
                    throw new InvalidDataException("Major version mismatch.");
                }
                var minor = Reader.ReadInt32();
                if (minor != 3 && minor != 5 && minor != 6)
                {
                    throw new InvalidDataException(string.Format("Minor version mismatch, read {0} expected 3, 5 or 6.", minor));
                }

                GCFVersion = new Version(1, minor);
                CacheID = Reader.ReadInt32();
                LastVersionPlayed = Reader.ReadInt32();

                var dummy0 = Reader.ReadUInt32();
                var dummy1 = Reader.ReadUInt32();

                FileSize = Reader.ReadUInt32(); // Total size of GCF file in bytes.
                SectorSize = Reader.ReadUInt32(); // Size of each data block in bytes.
                SectorCount = Reader.ReadUInt32(); // Number of data blocks.
                
                if (checksum != Reader.ReadUInt32())// Header checksum.
                    throw new InvalidDataException("Checksum mismatch.");
            }

            {
                long seek = Reader.Position;
                uint checksum = Reader.Read<uint>(7).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                BlockCount = Reader.ReadUInt32(); // Number of data blocks.
                if (BlockCount != SectorCount)
                {
                    throw new InvalidDataException("BlockCount does not match SectorCount.");
                }

                BlocksUsed = Reader.ReadUInt32(); // Number of data blocks that point to data.

                var dummy0 = Reader.ReadUInt32();
                var dummy1 = Reader.ReadUInt32();
                var dummy2 = Reader.ReadUInt32();
                var dummy3 = Reader.ReadUInt32();
                var dummy4 = Reader.ReadUInt32();

                if (checksum != Reader.ReadUInt32()) // Header checksum.
                    throw new InvalidDataException("Checksum mismatch.");
            }

            BlockEntryOffset = Reader.Position;
            //Seek past block entries
            Reader.Seek(BlockCount * BlockEntry.Size, SeekOrigin.Current);

            { //FragmentationMapHeader
                long seek = Reader.Position;
                uint checksum = Reader.Read<uint>(3).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                uint sectorCount = Reader.ReadUInt32();
                if (SectorCount != sectorCount)
                    throw new InvalidDataException();

                FirstUnusedEntry = Reader.ReadUInt32();
                Terminator = Reader.ReadUInt32() == 0 ? 0x0000ffff : 0xffffffff; 

                if (checksum != Reader.ReadUInt32()) // Header checksum.
                    throw new InvalidDataException("Checksum mismatch.");
            }

            FragmentationMapEntryOffset = Reader.BaseStream.Position;
            //Seek past block entries
            Reader.Seek(BlockCount * 4, SeekOrigin.Current);

            { //BlockMapHeader
                if (GCFVersion.Minor <= 5)
                {
                    uint blockCount = Reader.ReadUInt32();
                    uint firstBlockEntryIndex = Reader.ReadUInt32();
                    uint lastBlockEntryIndex = Reader.ReadUInt32();
                    uint dummy0 = Reader.ReadUInt32();
                    uint checksum = Reader.ReadUInt32();

                    //struct BlockMapEntry
                    //{
                    //    public uint PreviousBlockEntryIndex;	// The previous block entry.  (N/A if == BlockCount.)
                    //    public uint NextBlockEntryIndex;	// The next block entry.  (N/A if == BlockCount.)
                    //}

                    Reader.Seek(blockCount * 8, SeekOrigin.Current);
                }
            }

            { //DirectoryHeader

                if (Reader.ReadUInt32() != 0x00000004) // HeaderVersion Always 0x00000004
                    throw new InvalidDataException("Header version mismatch.");
                if (Reader.ReadUInt32() != CacheID) // CacheID
                    throw new InvalidDataException("CacheID mismatch.");
                if (Reader.ReadUInt32() != LastVersionPlayed) // LastVersionPlayed
                    throw new InvalidDataException("LastVersionPlayed mismatch.");

                ItemCount = Reader.ReadUInt32(); //public uint ItemCount;	// Number of items in the directory.	
                FileCount = Reader.ReadUInt32(); //public uint FileCount;	// Number of files in the directory.
                var dummy0 = Reader.ReadUInt32(); 
                DirectorySize = Reader.ReadUInt32(); //public uint DirectorySize;	// Size of lpGCFDirectoryEntries & lpGCFDirectoryNames & lpGCFDirectoryInfo1Entries & lpGCFDirectoryInfo2Entries & lpGCFDirectoryCopyEntries & lpGCFDirectoryLocalEntries in bytes.
                NameSize = Reader.ReadUInt32(); //public uint NameSize;		// Size of the directory names in bytes.
                HashTableKeyCount = Reader.ReadUInt32(); //public uint HashTableKeyCount;	// Number of Info1 entires.
                CopyCount = Reader.ReadUInt32(); //public uint CopyCount;	// Number of files to copy.
                LocalCount = Reader.ReadUInt32(); //public uint LocalCount;	// Number of files to keep local.
                var dummy1 = Reader.ReadUInt32(); 
                var dummy2 = Reader.ReadUInt32();

                var checksum = Reader.ReadUInt32(); // Header checksum never computes
            }

            DirectoryEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * DirectoryEntry.Size, SeekOrigin.Current);

            DirectoryNameOffset = Reader.BaseStream.Position;
            Reader.Seek(NameSize, SeekOrigin.Current);

            DirectoryInfo1EntryOffset = Reader.BaseStream.Position;
            Reader.Seek(HashTableKeyCount * DirectoryInfo1Entry.Size, SeekOrigin.Current);

            DirectoryInfo2EntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * DirectoryInfo2Entry.Size, SeekOrigin.Current);

            DirectoryCopyEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(CopyCount * DirectoryCopyEntry.Size, SeekOrigin.Current);

            DirectoryLocalEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(LocalCount * DirectoryLocalEntry.Size, SeekOrigin.Current);

            { //DirectoryMapHeader
                //public uint Dummy0;
                //public uint Dummy1;
                Reader.Seek(8, SeekOrigin.Current);
            }

            DirectoryMapEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * 4, SeekOrigin.Current);

            { //ChecksumHeader ChecksumMapHeader
                if (Reader.ReadUInt32() != 0x00000001) // HeaderVersion Always 0x00000001
                    throw new InvalidDataException();

                ChecksumSize = Reader.ReadUInt32();	// Size of LPGCFCHECKSUMHEADER & LPGCFCHECKSUMMAPHEADER & in bytes.

                if (Reader.ReadUInt32() != 0x14893721) // FormatCode  Always 0x14893721
                    throw new InvalidDataException();
                if (Reader.ReadUInt32() != 0x00000001) // Dummy0  Always 0x00000001
                    throw new InvalidDataException();

                ChecksumItemCount = Reader.ReadUInt32(); // Number of file ID entries.
                ChecksumCount = Reader.ReadUInt32(); // Number of checksums.
            }

            ChecksumMapEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ChecksumItemCount * ChecksumMapEntry.Size, SeekOrigin.Current);

            ChecksumEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ChecksumCount * 4, SeekOrigin.Current);

            //Skip past signature
            Reader.Seek(0x80, SeekOrigin.Current);

            if (Reader.ReadUInt32() != LastVersionPlayed) // LastVersionPlayed
                throw new InvalidDataException("LastVersionPlayed mismatch.");

            { //DataHeader
                uint checksum = 0;
                uint sectorCount, sectorSize;

                checksum += (sectorCount = Reader.ReadUInt32());	// Number of data blocks.
                if (SectorCount != sectorCount)
                    throw new InvalidDataException();

                checksum += (sectorSize = Reader.ReadUInt32()); // Size of each data block in bytes.
                if (SectorSize != sectorSize)
                    throw new InvalidDataException();

                checksum += (FirstSectorOffset = Reader.ReadUInt32()); // Offset to first data block.
                checksum += (SectorsUsed = Reader.ReadUInt32()); // Number of data blocks that contain data.

                if (checksum != Reader.ReadUInt32()) //public uint Checksum;		// Header checksum.
                    throw new InvalidDataException();
            }

            for (uint index = 0; index < ItemCount; ++index)
            {
                DirectoryEntry entry = GetDirectoryEntry(index);

                if (entry.ParentIndex == 0xFFFFFFFF)
                {
                    RootDirectory = new GcfDirectoryInfo(this, index);
                    break;
                }
            }
        }
Esempio n. 7
0
        public Bsp(Stream stream, IEnumerable <Package.Wad> wads)
        {
            if (!stream.CanSeek || !stream.CanRead)
            {
                throw new ArgumentException("stream must be seekable and readable.", "stream");
            }
            if (wads == null)
            {
                wads = System.Linq.Enumerable.Empty <Package.Wad>();
            }

            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            if (Reader.ReadUInt32() != 30)
            {
                throw new InvalidDataException("Version is not 30.");
            }

            long entitiesOffset     = Reader.ReadUInt32();
            long entitiesCount      = Reader.ReadUInt32();
            long planesOffset       = Reader.ReadUInt32();
            long planesCount        = Reader.ReadUInt32();
            long texturesOffset     = Reader.ReadUInt32();
            long texturesCount      = Reader.ReadUInt32();
            long vertexesOffset     = Reader.ReadUInt32();
            long vertexesCount      = Reader.ReadUInt32();
            long visibilityOffset   = Reader.ReadUInt32();
            long visibilityCount    = Reader.ReadUInt32();
            long nodesOffset        = Reader.ReadUInt32();
            long nodesCount         = Reader.ReadUInt32();
            long texinfoOffset      = Reader.ReadUInt32();
            long texinfoCount       = Reader.ReadUInt32();
            long facesOffset        = Reader.ReadUInt32();
            long facesCount         = Reader.ReadUInt32();
            long lightingOffset     = Reader.ReadUInt32();
            long lightingCount      = Reader.ReadUInt32();
            long clipnodesOffset    = Reader.ReadUInt32();
            long clipnodesCount     = Reader.ReadUInt32();
            long leafsOffset        = Reader.ReadUInt32();
            long leafsCount         = Reader.ReadUInt32();
            long marksurfacesOffset = Reader.ReadUInt32();
            long marksurfacesCount  = Reader.ReadUInt32();
            long edgesOffset        = Reader.ReadUInt32();
            long edgesCount         = Reader.ReadUInt32();
            long surfedgesOffset    = Reader.ReadUInt32();
            long surfedgesCount     = Reader.ReadUInt32();
            long modelsOffset       = Reader.ReadUInt32();
            long modelsCount        = Reader.ReadUInt32();

            //Entities
            Reader.Seek(entitiesOffset, SeekOrigin.Begin);
            Entities = Encoding.ASCII.GetString(Reader.ReadBytes((int)entitiesCount));

            //Planes
            Reader.Seek(planesOffset, SeekOrigin.Begin);
            Planes = new Planef[planesCount / 20];
            for (int i = 0; i < Planes.Length; ++i)
            {
                Planes[i] = new Planef(
                    Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle());

                uint type = Reader.ReadUInt32();
            }

            //Textures
            {
                Reader.Seek(texturesOffset, SeekOrigin.Begin);
                long[] offsets = new long[Reader.ReadUInt32()];
                for (int i = 0; i < offsets.Length; ++i)
                {
                    offsets[i] = Reader.ReadUInt32();
                }

                Textures = new Resource[offsets.Length];
                for (int i = 0; i < offsets.Length; ++i)
                {
                    Reader.Seek(texturesOffset + offsets[i], SeekOrigin.Begin);

                    string name     = Encoding.ASCII.GetString(Reader.ReadBytes(16));
                    int    nullbyte = name.IndexOf('\0');
                    name = nullbyte == -1 ? name : name.Substring(0, nullbyte);

                    int width  = Reader.ReadInt32();
                    int height = Reader.ReadInt32();

                    long[] dataoffsets = new long[4];
                    dataoffsets[0] = Reader.ReadUInt32();
                    dataoffsets[1] = Reader.ReadUInt32();
                    dataoffsets[2] = Reader.ReadUInt32();
                    dataoffsets[3] = Reader.ReadUInt32();

                    Resource resource = null;

                    if (dataoffsets.All(o => o == 0))
                    {
                        foreach (var wad in wads)
                        {
                            resource = wad[name];
                            if (resource != null)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        resource = new Resource(new Size3i(width, height, 1), 4, 1, Format.R8G8B8A8UNorm);

                        byte[][] images = new byte[4][];

                        for (int mipSlice = 0; mipSlice < 4; ++mipSlice)
                        {
                            Reader.Seek(texturesOffset + offsets[i] + dataoffsets[mipSlice], SeekOrigin.Begin);

                            images[mipSlice] = Reader.ReadBytes(width * height);
                            width          >>= 1; height >>= 1;
                        }

                        Reader.Seek(2, SeekOrigin.Current);
                        byte[] pallet = Reader.ReadBytes(256 * 3);

                        for (int mipSlice = 0; mipSlice < 4; ++mipSlice)
                        {
                            byte[] data  = resource[mipSlice, 0];
                            byte[] image = images[mipSlice];

                            for (int j = 0; j < image.Length; ++j)
                            {
                                int palletIndex = image[j] * 3;
                                int dataIndex   = j * 3;

                                data[dataIndex + 0] = pallet[palletIndex + 0];
                                data[dataIndex + 1] = pallet[palletIndex + 1];
                                data[dataIndex + 2] = pallet[palletIndex + 2];
                            }
                        }
                    }

                    Textures[i] = resource;
                }
            }

            //Vertices
            Reader.Seek(vertexesOffset, SeekOrigin.Begin);
            Vertices = new Vector3f[vertexesCount / 12];
            for (int i = 0; i < Vertices.Length; ++i)
            {
                Vertices[i] = new Vector3f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle());
            }

            //Visibility
            Reader.Seek(visibilityOffset, SeekOrigin.Begin);
            Visibility = Reader.ReadBytes((int)visibilityCount);

            //Nodes

            //Surfaces
            Reader.Seek(texinfoOffset, SeekOrigin.Begin);
            Surfaces = new Surface[texinfoCount / 24];
            for (int i = 0; i < Surfaces.Length; ++i)
            {
                Surfaces[i] = new Surface(
                    new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()),
                    new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()),
                    Reader.ReadInt32(), Reader.ReadInt32());
            }

            //Faces
            Reader.Seek(facesOffset, SeekOrigin.Begin);
            Faces = new Face[facesCount / 20];
            for (int i = 0; i < Faces.Length; ++i)
            {
                Faces[i] = new Face(
                    Reader.ReadUInt16(), Reader.ReadInt16(),
                    Reader.ReadInt32(), Reader.ReadInt16(), Reader.ReadInt16(),
                    Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(),
                    Reader.ReadInt32());
            }

            //Lighting
            Reader.Seek(facesOffset, SeekOrigin.Begin);
            Lighting = Reader.ReadBytes((int)lightingCount);

            //#define clipnodes     9
            //#define leafs        10
            //#define marksurfaces 11
            //#define edges        12
            //#define surfedges    13
            //#define models       14

            //Edges
            Reader.Seek(edgesOffset, SeekOrigin.Begin);
            Edges = new Edge[edgesCount / 4];
            for (int i = 0; i < Edges.Length; ++i)
            {
                Edges[i] = new Edge(Reader.ReadUInt16(), Reader.ReadUInt16());
            }

            //SurfaceEdges
            Reader.Seek(surfedgesOffset, SeekOrigin.Begin);
            SurfaceEdges = new int[surfedgesCount / 4];
            for (int i = 0; i < Planes.Length; ++i)
            {
                SurfaceEdges[i] = Reader.ReadInt32();
            }
        }
Esempio n. 8
0
        public Gcf(Stream stream)
        {
            Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);

            {
                long seek     = Reader.Position;
                uint checksum = Reader.ReadBytes(10 * 4).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                if (Reader.ReadUInt32() != 0x00000001) // HeaderVersion Always 0x00000001
                {
                    throw new InvalidDataException("Header version mismatch.");
                }
                if (Reader.ReadUInt32() != 0x00000001) // MajorVersion Always 0x00000001
                {
                    throw new InvalidDataException("Major version mismatch.");
                }
                var minor = Reader.ReadInt32();
                if (minor != 3 && minor != 5 && minor != 6)
                {
                    throw new InvalidDataException(string.Format("Minor version mismatch, read {0} expected 3, 5 or 6.", minor));
                }

                GCFVersion        = new Version(1, minor);
                CacheID           = Reader.ReadInt32();
                LastVersionPlayed = Reader.ReadInt32();

                var dummy0 = Reader.ReadUInt32();
                var dummy1 = Reader.ReadUInt32();

                FileSize    = Reader.ReadUInt32();   // Total size of GCF file in bytes.
                SectorSize  = Reader.ReadUInt32();   // Size of each data block in bytes.
                SectorCount = Reader.ReadUInt32();   // Number of data blocks.

                if (checksum != Reader.ReadUInt32()) // Header checksum.
                {
                    throw new InvalidDataException("Checksum mismatch.");
                }
            }

            {
                long seek     = Reader.Position;
                uint checksum = Reader.Read <uint>(7).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                BlockCount = Reader.ReadUInt32(); // Number of data blocks.
                if (BlockCount != SectorCount)
                {
                    throw new InvalidDataException("BlockCount does not match SectorCount.");
                }

                BlocksUsed = Reader.ReadUInt32(); // Number of data blocks that point to data.

                var dummy0 = Reader.ReadUInt32();
                var dummy1 = Reader.ReadUInt32();
                var dummy2 = Reader.ReadUInt32();
                var dummy3 = Reader.ReadUInt32();
                var dummy4 = Reader.ReadUInt32();

                if (checksum != Reader.ReadUInt32()) // Header checksum.
                {
                    throw new InvalidDataException("Checksum mismatch.");
                }
            }

            BlockEntryOffset = Reader.Position;
            //Seek past block entries
            Reader.Seek(BlockCount * BlockEntry.Size, SeekOrigin.Current);

            { //FragmentationMapHeader
                long seek     = Reader.Position;
                uint checksum = Reader.Read <uint>(3).Aggregate(0U, (sum, value) => sum + value);
                Reader.Position = seek;

                uint sectorCount = Reader.ReadUInt32();
                if (SectorCount != sectorCount)
                {
                    throw new InvalidDataException();
                }

                FirstUnusedEntry = Reader.ReadUInt32();
                Terminator       = Reader.ReadUInt32() == 0 ? 0x0000ffff : 0xffffffff;

                if (checksum != Reader.ReadUInt32()) // Header checksum.
                {
                    throw new InvalidDataException("Checksum mismatch.");
                }
            }

            FragmentationMapEntryOffset = Reader.BaseStream.Position;
            //Seek past block entries
            Reader.Seek(BlockCount * 4, SeekOrigin.Current);

            { //BlockMapHeader
                if (GCFVersion.Minor <= 5)
                {
                    uint blockCount           = Reader.ReadUInt32();
                    uint firstBlockEntryIndex = Reader.ReadUInt32();
                    uint lastBlockEntryIndex  = Reader.ReadUInt32();
                    uint dummy0   = Reader.ReadUInt32();
                    uint checksum = Reader.ReadUInt32();

                    //struct BlockMapEntry
                    //{
                    //    public uint PreviousBlockEntryIndex;	// The previous block entry.  (N/A if == BlockCount.)
                    //    public uint NextBlockEntryIndex;	// The next block entry.  (N/A if == BlockCount.)
                    //}

                    Reader.Seek(blockCount * 8, SeekOrigin.Current);
                }
            }

            {                                          //DirectoryHeader
                if (Reader.ReadUInt32() != 0x00000004) // HeaderVersion Always 0x00000004
                {
                    throw new InvalidDataException("Header version mismatch.");
                }
                if (Reader.ReadUInt32() != CacheID) // CacheID
                {
                    throw new InvalidDataException("CacheID mismatch.");
                }
                if (Reader.ReadUInt32() != LastVersionPlayed) // LastVersionPlayed
                {
                    throw new InvalidDataException("LastVersionPlayed mismatch.");
                }

                ItemCount = Reader.ReadUInt32();         //public uint ItemCount;	// Number of items in the directory.
                FileCount = Reader.ReadUInt32();         //public uint FileCount;	// Number of files in the directory.
                var dummy0 = Reader.ReadUInt32();
                DirectorySize     = Reader.ReadUInt32(); //public uint DirectorySize;	// Size of lpGCFDirectoryEntries & lpGCFDirectoryNames & lpGCFDirectoryInfo1Entries & lpGCFDirectoryInfo2Entries & lpGCFDirectoryCopyEntries & lpGCFDirectoryLocalEntries in bytes.
                NameSize          = Reader.ReadUInt32(); //public uint NameSize;		// Size of the directory names in bytes.
                HashTableKeyCount = Reader.ReadUInt32(); //public uint HashTableKeyCount;	// Number of Info1 entires.
                CopyCount         = Reader.ReadUInt32(); //public uint CopyCount;	// Number of files to copy.
                LocalCount        = Reader.ReadUInt32(); //public uint LocalCount;	// Number of files to keep local.
                var dummy1 = Reader.ReadUInt32();
                var dummy2 = Reader.ReadUInt32();

                var checksum = Reader.ReadUInt32(); // Header checksum never computes
            }

            DirectoryEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * DirectoryEntry.Size, SeekOrigin.Current);

            DirectoryNameOffset = Reader.BaseStream.Position;
            Reader.Seek(NameSize, SeekOrigin.Current);

            DirectoryInfo1EntryOffset = Reader.BaseStream.Position;
            Reader.Seek(HashTableKeyCount * DirectoryInfo1Entry.Size, SeekOrigin.Current);

            DirectoryInfo2EntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * DirectoryInfo2Entry.Size, SeekOrigin.Current);

            DirectoryCopyEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(CopyCount * DirectoryCopyEntry.Size, SeekOrigin.Current);

            DirectoryLocalEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(LocalCount * DirectoryLocalEntry.Size, SeekOrigin.Current);

            { //DirectoryMapHeader
                //public uint Dummy0;
                //public uint Dummy1;
                Reader.Seek(8, SeekOrigin.Current);
            }

            DirectoryMapEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ItemCount * 4, SeekOrigin.Current);

            {                                          //ChecksumHeader ChecksumMapHeader
                if (Reader.ReadUInt32() != 0x00000001) // HeaderVersion Always 0x00000001
                {
                    throw new InvalidDataException();
                }

                ChecksumSize = Reader.ReadUInt32();    // Size of LPGCFCHECKSUMHEADER & LPGCFCHECKSUMMAPHEADER & in bytes.

                if (Reader.ReadUInt32() != 0x14893721) // FormatCode  Always 0x14893721
                {
                    throw new InvalidDataException();
                }
                if (Reader.ReadUInt32() != 0x00000001) // Dummy0  Always 0x00000001
                {
                    throw new InvalidDataException();
                }

                ChecksumItemCount = Reader.ReadUInt32(); // Number of file ID entries.
                ChecksumCount     = Reader.ReadUInt32(); // Number of checksums.
            }

            ChecksumMapEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ChecksumItemCount * ChecksumMapEntry.Size, SeekOrigin.Current);

            ChecksumEntryOffset = Reader.BaseStream.Position;
            Reader.Seek(ChecksumCount * 4, SeekOrigin.Current);

            //Skip past signature
            Reader.Seek(0x80, SeekOrigin.Current);

            if (Reader.ReadUInt32() != LastVersionPlayed) // LastVersionPlayed
            {
                throw new InvalidDataException("LastVersionPlayed mismatch.");
            }

            { //DataHeader
                uint checksum = 0;
                uint sectorCount, sectorSize;

                checksum += (sectorCount = Reader.ReadUInt32());        // Number of data blocks.
                if (SectorCount != sectorCount)
                {
                    throw new InvalidDataException();
                }

                checksum += (sectorSize = Reader.ReadUInt32()); // Size of each data block in bytes.
                if (SectorSize != sectorSize)
                {
                    throw new InvalidDataException();
                }

                checksum += (FirstSectorOffset = Reader.ReadUInt32()); // Offset to first data block.
                checksum += (SectorsUsed = Reader.ReadUInt32());       // Number of data blocks that contain data.

                if (checksum != Reader.ReadUInt32())                   //public uint Checksum;		// Header checksum.
                {
                    throw new InvalidDataException();
                }
            }

            for (uint index = 0; index < ItemCount; ++index)
            {
                DirectoryEntry entry = GetDirectoryEntry(index);

                if (entry.ParentIndex == 0xFFFFFFFF)
                {
                    RootDirectory = new GcfDirectoryInfo(this, index);
                    break;
                }
            }
        }
Esempio n. 9
0
        private void Load(Stream stream)
        {
            var  reader     = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);
            long baseOffset = reader.BaseStream.Position;

            string signature = Encoding.ASCII.GetString(reader.ReadBytes(4));

            if (signature != "IDST")
            {
                throw new InvalidDataException("File signature does not match 'IDST'.");
            }

            Version = reader.ReadInt32();

            if ((Version < 44 || Version > 49))
            {
                throw new InvalidDataException(string.Format("File version {0} does not match 44 to 49.", Version));
            }

            Checksum = reader.ReadInt32();
            Name     = Encoding.ASCII.GetString(reader.ReadBytes(64)).Trim('\0');
            int length = reader.ReadInt32(); //file length

            EyePosition          = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
            IlluminationPosition = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

            Hull = new Boxf(
                new Point3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                new Size3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()));

            ViewHull = new Boxf(
                new Point3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                new Size3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()));

            Flags = (MdlFlags)reader.ReadInt32();

            int  bonesCount  = reader.ReadInt32();
            long bonesOffset = baseOffset + reader.ReadInt32();

            int  boneControllersCount  = reader.ReadInt32();
            long boneControllersOffset = baseOffset + reader.ReadInt32();

            int  hitBoxSetsCount  = reader.ReadInt32();
            long hitBoxSetsOffset = baseOffset + reader.ReadInt32();

            int  localAnimCount  = reader.ReadInt32();
            long localAnimOffset = baseOffset + reader.ReadInt32();

            int  localSeqCount  = reader.ReadInt32();
            long localSeqOffset = baseOffset + reader.ReadInt32();

            int activityListVersion = reader.ReadInt32();
            int eventsIndexed       = reader.ReadInt32();

            int  texturesCount  = reader.ReadInt32();
            long texturesOffset = baseOffset + reader.ReadInt32();

            int  cdTexturesCount  = reader.ReadInt32();
            long cdTexturesOffset = baseOffset + reader.ReadInt32();

            int  skinRefsCount     = reader.ReadInt32();
            int  skinFamiliesCount = reader.ReadInt32();
            long skinOffset        = baseOffset + reader.ReadInt32();

            int  bodyPartsCount  = reader.ReadInt32();
            long bodyPartsOffset = baseOffset + reader.ReadInt32();

            int  localAttachmentsCount  = reader.ReadInt32();
            long localAttachmentsOffset = baseOffset + reader.ReadInt32();

            int  localNodesCount      = reader.ReadInt32();
            long localNodesOffset     = baseOffset + reader.ReadInt32();
            long localNodesNameOffset = baseOffset + reader.ReadInt32();

            int  flexDescsCount  = reader.ReadInt32();
            long flexDescsOffset = baseOffset + reader.ReadInt32();

            int  flexControllersCount  = reader.ReadInt32();
            long flexControllersOffset = baseOffset + reader.ReadInt32();

            int  flexRulesCount  = reader.ReadInt32();
            long flexRulesOffset = baseOffset + reader.ReadInt32();

            int  ikChainsCount  = reader.ReadInt32();
            long ikChainsOffset = baseOffset + reader.ReadInt32();

            int  mouthsCount  = reader.ReadInt32();
            long mouthsOffset = baseOffset + reader.ReadInt32();

            //===
            //Start reading from offsets
            //===

            reader.BaseStream.Position = bonesOffset;
            Bones = LoadBones(reader, bonesCount);

            reader.BaseStream.Position = boneControllersOffset;
            BoneControllers            = LoadBoneControllers(reader, boneControllersCount);

            //reader.BaseStream.Position = hitBoxSetsOffset;
            //HitBoxSets = LoadHitBoxSets(reader, hitBoxSetsCount);

            reader.BaseStream.Position = localAnimOffset;
            LocalAnimations            = LoadLocalAnimations(reader, localAnimCount);

            reader.BaseStream.Position = localSeqOffset;
            LocalSequences             = LoadLocalSequences(reader, localSeqCount);

            reader.BaseStream.Position = texturesOffset;
            Textures = LoadTextures(reader, texturesCount);

            reader.BaseStream.Position = cdTexturesOffset;
            CdTextures = LoadStringTable(reader, cdTexturesCount, baseOffset);

            //skin refs
            //TODO

            reader.BaseStream.Position = bodyPartsOffset;
            BodyParts = LoadBodyParts(reader, bodyPartsCount);
        }
Esempio n. 10
0
File: Mdl.cs Progetto: Frassle/Ibasa
        private void Load(Stream stream)
        {
            var reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII);
            long baseOffset = reader.BaseStream.Position;

            string signature = Encoding.ASCII.GetString(reader.ReadBytes(4));

            if (signature != "IDST")
                throw new InvalidDataException("File signature does not match 'IDST'.");

            Version = reader.ReadInt32();

            if ((Version < 44 || Version > 49))
                throw new InvalidDataException(string.Format("File version {0} does not match 44 to 49.", Version));

            Checksum = reader.ReadInt32();
            Name = Encoding.ASCII.GetString(reader.ReadBytes(64)).Trim('\0');
            int length = reader.ReadInt32(); //file length

            EyePosition = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
            IlluminationPosition = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

            Hull = new Boxf(
                new Point3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                new Size3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()));

            ViewHull = new Boxf(
                new Point3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                new Size3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()));

            Flags = (MdlFlags)reader.ReadInt32();

            int bonesCount = reader.ReadInt32();
            long bonesOffset = baseOffset + reader.ReadInt32();

            int boneControllersCount = reader.ReadInt32();
            long boneControllersOffset = baseOffset + reader.ReadInt32();

            int hitBoxSetsCount = reader.ReadInt32();
            long hitBoxSetsOffset = baseOffset + reader.ReadInt32();

            int localAnimCount = reader.ReadInt32();
            long localAnimOffset = baseOffset + reader.ReadInt32();

            int localSeqCount = reader.ReadInt32();
            long localSeqOffset = baseOffset + reader.ReadInt32();

            int activityListVersion = reader.ReadInt32(); 
            int eventsIndexed = reader.ReadInt32();

            int texturesCount = reader.ReadInt32();
            long texturesOffset = baseOffset + reader.ReadInt32();

            int cdTexturesCount = reader.ReadInt32();
            long cdTexturesOffset = baseOffset + reader.ReadInt32();

            int skinRefsCount = reader.ReadInt32();
            int skinFamiliesCount = reader.ReadInt32();
            long skinOffset = baseOffset + reader.ReadInt32();

            int bodyPartsCount = reader.ReadInt32();
            long bodyPartsOffset = baseOffset + reader.ReadInt32();

            int localAttachmentsCount = reader.ReadInt32();
            long localAttachmentsOffset = baseOffset + reader.ReadInt32();

            int localNodesCount = reader.ReadInt32();
            long localNodesOffset = baseOffset + reader.ReadInt32();
            long localNodesNameOffset = baseOffset + reader.ReadInt32();

            int flexDescsCount = reader.ReadInt32();
            long flexDescsOffset = baseOffset + reader.ReadInt32();

            int flexControllersCount = reader.ReadInt32();
            long flexControllersOffset = baseOffset + reader.ReadInt32();

            int flexRulesCount = reader.ReadInt32();
            long flexRulesOffset = baseOffset + reader.ReadInt32();

            int ikChainsCount = reader.ReadInt32();
            long ikChainsOffset = baseOffset + reader.ReadInt32();

            int mouthsCount = reader.ReadInt32();
            long mouthsOffset = baseOffset + reader.ReadInt32();

            //===
            //Start reading from offsets
            //===

            reader.BaseStream.Position = bonesOffset;
            Bones = LoadBones(reader, bonesCount);

            reader.BaseStream.Position = boneControllersOffset;
            BoneControllers = LoadBoneControllers(reader, boneControllersCount);

            //reader.BaseStream.Position = hitBoxSetsOffset;
            //HitBoxSets = LoadHitBoxSets(reader, hitBoxSetsCount);

            reader.BaseStream.Position = localAnimOffset;
            LocalAnimations = LoadLocalAnimations(reader, localAnimCount);

            reader.BaseStream.Position = localSeqOffset;
            LocalSequences = LoadLocalSequences(reader, localSeqCount);

            reader.BaseStream.Position = texturesOffset;
            Textures = LoadTextures(reader, texturesCount);

            reader.BaseStream.Position = cdTexturesOffset;
            CdTextures = LoadStringTable(reader, cdTexturesCount, baseOffset);

            //skin refs
            //TODO

            reader.BaseStream.Position = bodyPartsOffset;
            BodyParts = LoadBodyParts(reader, bodyPartsCount);
        }