Exemple #1
0
        private void ReadFooter(Ibasa.IO.BinaryReader reader)
        {
            if (reader.PeekChar() == -1)
            {
                return; //end of stream or cant seek no footer possible
            }
            reader.BaseStream.Seek(-26, SeekOrigin.End);

            long   extensionOffset = reader.ReadUInt32();
            long   developerOffset = reader.ReadUInt32();
            string signature       = new string(reader.ReadChars(18));

            if (signature != "TRUEVISION-XFILE.\0")
            {
                return;
            }

            if (extensionOffset != 0)
            {
                reader.BaseStream.Seek(extensionOffset, SeekOrigin.Begin);
                ReadExtension(reader);
            }
            if (developerOffset != 0)
            {
                reader.BaseStream.Seek(developerOffset, SeekOrigin.Begin);
                ReadDeveloper(reader);
            }
        }
Exemple #2
0
        private void ReadColorMap(Ibasa.IO.BinaryReader reader)
        {
            if (Header.ColorMapType == 0)
            {
                return;
            }

            ColorMap = new uint[256];

            switch (Header.ColorMapDepth)
            {
            case 16:
            {
                for (int i = Header.ColourMapOrigin; i < Header.ColorMapLength; ++i)
                {
                    uint color = reader.ReadUInt16();
                    uint r     = (color >> 10) & 0x1F;
                    uint g     = (color >> 5) & 0x1F;
                    uint b     = (color) & 0x1F;
                    //expand 8 bits
                    r           = (r << 3) | (r >> 2);
                    g           = (g << 3) | (g >> 2);
                    b           = (b << 3) | (b >> 2);
                    ColorMap[i] = (r << 16) | (g << 8) | b;
                }
            } break;

            case 24:
            {
                for (int i = Header.ColourMapOrigin; i < Header.ColorMapLength; ++i)
                {
                    uint b = reader.ReadByte();
                    uint g = reader.ReadByte();
                    uint r = reader.ReadByte();
                    ColorMap[i] = (r << 16) | (g << 8) | b;
                }
            } break;

            case 32:
            {
                for (int i = Header.ColourMapOrigin; i < Header.ColorMapLength; ++i)
                {
                    ColorMap[i] = reader.ReadUInt32();
                }
            } break;

            default:
                throw new InvalidDataException("Unrecognized color map depth.");
            }
        }
Exemple #3
0
        private void ReadDeveloper(Ibasa.IO.BinaryReader reader)
        {
            int tags = reader.ReadUInt16();

            uint[] tol = new uint[tags * 3];

            for (int i = 0; i < tags; ++i)
            {
                tol[i * 3 + 0] = reader.ReadUInt16();
                tol[i * 3 + 1] = reader.ReadUInt32();
                tol[i * 3 + 2] = reader.ReadUInt32();
            }

            for (int i = 0; i < tags; ++i)
            {
                int  tag    = (int)tol[i * 3 + 0];
                long offset = tol[i * 3 + 1];
                int  length = (int)tol[i * 3 + 2];

                reader.BaseStream.Seek(offset, SeekOrigin.Begin);
                DeveloperArea.Add(new KeyValuePair <int, byte[]>(tag, reader.ReadBytes(length)));
            }
        }
Exemple #4
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();
            }
        }
Exemple #5
0
 /// <summary>
 /// Reads a <see cref="Vector8ui"/> from an <see cref="Ibasa.IO.BinaryReader">.
 /// </summary>
 public static Vector8ui ReadVector8ui(this Ibasa.IO.BinaryReader reader)
 {
     return(new Vector8ui(reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32()));
 }
Exemple #6
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();
            }
        }
Exemple #7
0
        private void ParseHET()
        {
            Reader.Seek((long)HetTablePos64, SeekOrigin.Begin);

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

            if (Reader.ReadUInt32() != 1)
            {
                throw new Exception("WTF");
            }

            HetDataSize        = Reader.ReadUInt32();
            HetTableSize       = Reader.ReadUInt32();
            HetMaxFileCount    = Reader.ReadUInt16();
            HetHashTableSize   = Reader.ReadUInt16();
            HetHashEntrySize   = Reader.ReadUInt32();
            HetTotalIndexSize  = Reader.ReadUInt32();
            HetIndexSizeExtra  = Reader.ReadUInt32();
            HetIndexSize       = Reader.ReadUInt32();
            HetBlockTableSize  = Reader.ReadUInt32();
            HetHashTableOffset = Reader.Position;
        }
Exemple #8
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();
            }
        }
Exemple #9
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);
            }
        }
Exemple #10
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;
                }
            }
        }
Exemple #11
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();
            }
        }
Exemple #12
0
        BlockEntry GetBlockEntry(uint index)
        {
            Reader.Seek(BlockEntryOffset + index * BlockEntry.Size, SeekOrigin.Begin);

            BlockEntry entry;

            entry.EntryType               = Reader.ReadUInt32(); //public long EntryType;		// Flags for the block entry.  0x200F0000 == Not used.
            entry.FileDataOffset          = Reader.ReadUInt32(); //public long FileDataOffset;		// The offset for the data contained in this block entry in the file.
            entry.FileDataSize            = Reader.ReadUInt32(); //public long FileDataSize;		// The length of the data in this block entry.
            entry.FirstDataBlockIndex     = Reader.ReadUInt32(); //public long FirstDataBlockIndex;	// The index to the first data block of this block entry's data.
            entry.NextBlockEntryIndex     = Reader.ReadUInt32(); //public long NextBlockEntryIndex;	// The next block entry in the series.  (N/A if == BlockCount.)
            entry.PreviousBlockEntryIndex = Reader.ReadUInt32(); //public long PreviousBlockEntryIndex;	// The previous block entry in the series.  (N/A if == BlockCount.)
            entry.DirectoryIndex          = Reader.ReadUInt32(); //public long DirectoryIndex;		// The index of the block entry in the directory.

            return(entry);
        }
Exemple #13
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;
                }
            }
        }