public override void Read(BinaryReader stream) { var chunkStart = 0; while (stream.BaseStream.Position != stream.BaseStream.Length) { stream.BaseStream.Seek(chunkStart, SeekOrigin.Begin); //afid sfid bfid var chunkMagic = string.Concat(stream.ReadBytes(4).Select(_ => (char)_)); var chunkSize = stream.ReadInt32(); chunkStart += chunkSize + 8; using (var reader = new BinaryReader(new MemoryStream(stream.ReadBytes(chunkSize)))) { var chunk = M2Chunk.CreateFromMagic(chunkMagic, chunkSize); chunk.Read(reader); Chunks.Add(chunk); } } }
private static void ReadM2(string path, uint fileDataId) { using (FileStream ms = File.OpenRead(path)) using (BinaryReader br = new BinaryReader(ms)) { while (ms.Position < ms.Length) { M2Chunk ChunkId = (M2Chunk)br.ReadUInt32(); uint ChunkSize = br.ReadUInt32(); switch (ChunkId) { case M2Chunk.MD21: M2Reader.ReadMD20(br, fileDataId); br.BaseStream.Position = 0; br.ReadBytes((int)ChunkSize + 8); break; case M2Chunk.LDV1: M2Reader.ReadLDV1(br); break; case M2Chunk.SFID: M2Reader.ReadSFID(br, fileDataId); break; case M2Chunk.TXID: M2Reader.ReadTXID(br, ChunkSize, fileDataId); break; default: SkipUnknownChunk(ms, ChunkSize); break; } } } }
public bool Read(string filename, out M2Model model) { if (!uint.TryParse(Path.GetFileNameWithoutExtension(filename).Split('_').Last(), out uint fileDataId)) { model = null; return(false); } model = new M2Model(fileDataId); using (var fs = File.OpenRead(filename)) using (var br = new BinaryReader(fs)) { if ((M2Chunk)br.ReadUInt32() != M2Chunk.MD21) { return(false); } br.BaseStream.Position = 0; while (fs.Position < fs.Length) { M2Chunk chunkId = (M2Chunk)br.ReadUInt32(); int chunkSize = br.ReadInt32(); switch (chunkId) { case M2Chunk.MD21: ReadMD20(br, model); br.BaseStream.Position = 8 + chunkSize; break; case M2Chunk.LDV1: ReadLDV1(br, model); break; case M2Chunk.SFID: ReadSFID(br, model); break; case M2Chunk.TXID: ReadTXID(br, model, chunkSize); break; case M2Chunk.AFID: ReadAFID(br, model, chunkSize); break; case M2Chunk.PFID: model.PhysFileId = br.ReadUInt32(); break; case M2Chunk.SKID: model.SkelFileId = br.ReadUInt32(); break; case M2Chunk.BFID: ReadBFID(br, model, chunkSize); break; default: br.BaseStream.Position += chunkSize; break; } } } return(true); }