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; }
private void ReadData(Ibasa.IO.BinaryReader reader) { LowResImage = new SharpIL.Resource( new Size3i(LowResImageWidth, LowResImageHeight, 1), 1, 1, EncodingFromFormat(LowResImageFormat)); HighResImage = new SharpIL.Resource( new Size3i(Width, Height, Depth), MipmapCount, Frames * Faces, EncodingFromFormat(ImageFormat)); if (VersionMinor < 3) { reader.Read(LowResImage[0, 0], 0, LowResImage[0, 0].Length); for (int mip = MipmapCount - 1; mip >= 0; --mip) { Size3f size = HighResImage.ComputeMipSliceSize(mip); for (int frame = 0; frame < Frames; ++frame) { for (int face = 0; face < Faces; ++face) { int array = face + (Faces * frame); reader.Read( HighResImage[mip, array], 0, HighResImage[mip, array].Length); } } } } else { //low res int lowResOffset = Resources.Find((resource) => resource.Type == ResourceType.LowResImage).Data; reader.Seek((uint)lowResOffset, SeekOrigin.Begin); reader.Read(LowResImage[0, 0], 0, LowResImage[0, 0].Length); //high res int highResOffset = Resources.Find((resource) => resource.Type == ResourceType.Image).Data; reader.Seek((uint)highResOffset, SeekOrigin.Begin); for (int mip = MipmapCount - 1; mip >= 0; --mip) { Size3f size = HighResImage.ComputeMipSliceSize(mip); for (int frame = 0; frame < Frames; ++frame) { for (int face = 0; face < Faces; ++face) { byte[] data = HighResImage[mip, face + (Faces * frame)]; reader.Read(data, 0, data.Length); } } } } }
ImmutableArray <Texture> LoadTextures(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Texture[] textures = new Texture[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; long nameOffset = offset + reader.ReadInt32(); int flags = reader.ReadInt32(); int used = reader.ReadInt32(); int unused = reader.ReadInt32(); reader.Seek(12 * 4, SeekOrigin.Current); //skip 12 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = nameOffset; string name = LoadString(reader); textures[i] = new Texture(name, flags); } return(new ImmutableArray <Texture>(textures)); }
ImmutableArray <HitBox> LoadHitBoxes(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; HitBox[] hitBoxes = new HitBox[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; int bone = reader.ReadInt32(); int group = reader.ReadInt32(); Boxf box = new Boxf( new Point3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), new Size3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); long nameOffset = offset + reader.ReadInt32(); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = nameOffset; string name = LoadString(reader); hitBoxes[i] = new HitBox(bone, group, box, name); } return(new ImmutableArray <HitBox>(hitBoxes)); }
ImmutableArray <Bone> LoadBones(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Bone[] bones = new Bone[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; long nameOffset = offset + reader.ReadInt32(); int parent = reader.ReadInt32(); ImmutableArray <int> boneControllers = new ImmutableArray <int>(new int[] { reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32() }); Vector3f position = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Quaternionf quaternion = new Quaternionf(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3f rotation = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3f positionScale = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3f rotationScale = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Matrix4x4f poseToBone = new Matrix4x4f( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0, 0, 0, 1); Quaternionf alignment = new Quaternionf(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); int flags = reader.ReadInt32(); int procType = reader.ReadInt32(); int procIndex = reader.ReadInt32(); int physicsBone = reader.ReadInt32(); int surfacePropIdx = reader.ReadInt32(); int contents = reader.ReadInt32(); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = nameOffset; string name = LoadString(reader); bones[i] = new Bone(name, parent, boneControllers, position, quaternion, rotation, positionScale, rotationScale, poseToBone, alignment, flags, procType, procIndex, physicsBone, surfacePropIdx, contents); } return(new ImmutableArray <Bone>(bones)); }
ImmutableArray <BoneController> LoadBoneControllers(Ibasa.IO.BinaryReader reader, int count) { BoneController[] boneControllers = new BoneController[count]; for (int i = 0; i < count; ++i) { boneControllers[i] = new BoneController( reader.ReadInt32(), (BoneControllerType)reader.ReadInt32(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadInt32(), reader.ReadInt32()); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints } return(new ImmutableArray <BoneController>(boneControllers)); }
ImmutableArray <Mesh> LoadMeshes(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Mesh[] meshes = new Mesh[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; int material = reader.ReadInt32(); int modelIndex = reader.ReadInt32(); int vertexCount = reader.ReadInt32(); int vertexOffset = reader.ReadInt32(); int flexCount = reader.ReadInt32(); long flexOffset = offset + reader.ReadInt32(); int materialType = reader.ReadInt32(); int materialParam = reader.ReadInt32(); int meshId = reader.ReadInt32(); Vector3f center = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); int modelvertexdata = reader.ReadInt32(); int[] numLODVertexes = new int[8]; for (int l = 0; l < 8; ++l) { numLODVertexes[l] = reader.ReadInt32(); } ImmutableArray <int> lods = new ImmutableArray <int>(numLODVertexes); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = flexOffset; ImmutableArray <Flex> flexes = LoadFlexes(reader, flexCount); meshes[i] = new Mesh(material, vertexCount, vertexOffset, flexes, materialType, materialParam, meshId, center, lods); } return(new ImmutableArray <Mesh>(meshes)); }
ImmutableArray <Model> LoadModels(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Model[] models = new Model[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; string name = Encoding.ASCII.GetString(reader.ReadBytes(64)).Trim('\0'); int type = reader.ReadInt32(); float boundingRadius = reader.ReadSingle(); int meshCount = reader.ReadInt32(); long meshOffset = offset + reader.ReadInt32(); int vertexCount = reader.ReadInt32(); int vertexIndex = reader.ReadInt32(); int tangentIndex = reader.ReadInt32(); int numattachments = reader.ReadInt32(); int attachmentindex = reader.ReadInt32(); int eyeballCount = reader.ReadInt32(); long eyeballOffset = offset + reader.ReadInt32(); int vertexData = reader.ReadInt32(); int indexData = reader.ReadInt32(); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = meshOffset; ImmutableArray <Mesh> meshes = LoadMeshes(reader, meshCount); reader.BaseStream.Position = meshOffset; ImmutableArray <Eyeball> eyeballs = LoadEyeballs(reader, meshCount); models[i] = new Model(name, type, boundingRadius, meshes, vertexCount, vertexIndex, tangentIndex, eyeballs); } return(new ImmutableArray <Model>(models)); }
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); }
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(); } }
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(); } }
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(); } }
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; } } }
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(); } }
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; } } }