// Private Methods private void Read(BinaryReader reader) { Stream stream = reader.BaseStream; int posFileStart = (int)reader.GetPosition(); Spr0Header header = stream.ReadStructure <Spr0Header>(); stream.Seek(posFileStart + header.texturePointerTableOffset, SeekOrigin.Begin); TypePointerTable[] texturePointerTable = stream.ReadStructures <TypePointerTable>(header.numTextures); stream.Seek(posFileStart + header.keyFramePointerTableOffset, SeekOrigin.Begin); TypePointerTable[] keyFramePointerTable = stream.ReadStructures <TypePointerTable>(header.numKeyFrames); mTextures = new List <TmxFile>(header.numTextures); for (int i = 0; i < header.numTextures; i++) { stream.Seek(posFileStart + texturePointerTable[i].offset, SeekOrigin.Begin); mTextures.Add(TmxFile.Load(stream, true)); } mKeyFrames = new List <SprSprite>(header.numKeyFrames); for (int i = 0; i < header.numKeyFrames; i++) { stream.Seek(posFileStart + keyFramePointerTable[i].offset, SeekOrigin.Begin); mKeyFrames.Add(new SprSprite(reader)); } }
// Internal Methods internal override void Write(BinaryWriter writer) { // Save the start position to calculate the filesize and // to write out the header after we know where all the structure offsets are Stream stream = writer.BaseStream; int posFileStart = (int)stream.Position; stream.Seek(Spr0Header.SIZE, SeekOrigin.Current); // Create initial header and tables Spr0Header header = new Spr0Header(TextureCount, KeyFrameCount); TypePointerTable[] keyFramePointerTable = new TypePointerTable[header.numKeyFrames]; TypePointerTable[] texturePointerTable = new TypePointerTable[header.numTextures]; // Set the pointer table offsets and seek past the entries // as the entries will be written later header.texturePointerTableOffset = (int)(stream.Position - posFileStart); stream.Seek(TypePointerTable.SIZE * header.numTextures, SeekOrigin.Current); header.keyFramePointerTableOffset = (int)(stream.Position - posFileStart); stream.Seek(TypePointerTable.SIZE * header.numKeyFrames, SeekOrigin.Current); // Write out the keyframe data and fill up the pointer table for (int i = 0; i < header.numKeyFrames; i++) { //writer.AlignPosition(16); keyFramePointerTable[i].offset = (int)(stream.Position - posFileStart); mKeyFrames[i].Write(writer); } writer.Seek(16, SeekOrigin.Current); writer.AlignPosition(16); // Write out the texture data and fill up the pointer table for (int i = 0; i < header.numTextures; i++) { texturePointerTable[i].offset = (int)(stream.Position - posFileStart); mTextures[i].Write(writer); writer.Seek(16, SeekOrigin.Current); } // Save the end position long posFileEnd = stream.Position; // Seek back to the tables and write out the tables stream.Seek(posFileStart + header.texturePointerTableOffset, SeekOrigin.Begin); stream.WriteStructures(texturePointerTable, header.numTextures); stream.Seek(posFileStart + header.keyFramePointerTableOffset, SeekOrigin.Begin); stream.WriteStructures(keyFramePointerTable, header.numKeyFrames); // Seek back to the file and write out the header with // the offsets to the structures in the file writer.BaseStream.Seek(posFileStart, SeekOrigin.Begin); writer.BaseStream.WriteStructure(header); // Set the file pointer back to the end of the file as expected writer.BaseStream.Seek(posFileEnd, SeekOrigin.Begin); }