/// <inheritdoc/> void IDataSerializable.Serialize(BinarySerializer serializer) { // Starts the whole EffectData by the magiccode "TKFX" // If the serializer don't find the TKFX, It will throw an // exception that will be caught by Load method. serializer.BeginChunk(MagicCode); // Writes the version if (serializer.Mode == SerializerMode.Read) { int version = serializer.Reader.ReadInt32(); if (version != Version) { throw new NotSupportedException(string.Format("EffectData version [0x{0:X}] is not supported. Expecting [0x{1:X}]", version, Version)); } } else { serializer.Writer.Write(Version); } // Shaders section serializer.BeginChunk("SHDR"); serializer.Serialize(ref Shaders); serializer.EndChunk(); // Effects section serializer.BeginChunk("EFFX"); serializer.Serialize(ref Description); serializer.EndChunk(); // Close TKFX section serializer.EndChunk(); }
/// <summary> /// Saves the specified pixel buffers in TKTX format. /// </summary> /// <param name="pixelBuffers">The pixel buffers.</param> /// <param name="count">The count.</param> /// <param name="description">The description.</param> /// <param name="imageStream">The image stream.</param> internal static void SaveTKTX(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream) { var serializer = new BinarySerializer(imageStream, SerializerMode.Write); var beginPosition = imageStream.Position; // Write MagicCode for TKTX serializer.BeginChunk(MagicCodeTKTX); // Serialize Description serializer.Serialize(ref description); // Serialize Size int size = 0; for (int i = 0; i < count; i++) size += pixelBuffers[i].BufferStride; serializer.Serialize(ref size); // Pad to align pixel buffer on 16 bytes (fixed offset at 48 bytes from the beginning of the file). var padBuffer = new byte[OffsetBufferTKTX - (int) (imageStream.Position - beginPosition)]; if (padBuffer.Length > 0) imageStream.Write(padBuffer, 0, padBuffer.Length); // Write the whole pixel buffer for (int i = 0; i < count; i++) serializer.SerializeMemoryRegion(pixelBuffers[i].DataPointer, pixelBuffers[i].BufferStride); serializer.EndChunk(); }
/// <inheritdoc/> void IDataSerializable.Serialize(BinarySerializer serializer) { // Starts the whole ModelData by the magiccode "TKMD" // If the serializer don't find the TKMD, It will throw an // exception that will be caught by Load method. // This code should not be modified without modifying the serialize code in Model. serializer.BeginChunk(MagicCode); // Writes the version if (serializer.Mode == SerializerMode.Read) { int version = serializer.Reader.ReadInt32(); if (version != Version) { throw new NotSupportedException(string.Format("ModelData version [0x{0:X}] is not supported. Expecting [0x{1:X}]", version, Version)); } } else { serializer.Writer.Write(Version); } // Serialize the maximum buffer size used when loading this model. serializer.Serialize(ref MaximumBufferSizeInBytes); // Texture section serializer.BeginChunk("TEXS"); if (serializer.Mode == SerializerMode.Read) { int textureCount = serializer.Reader.ReadInt32(); Textures = new List<byte[]>(textureCount); for (int i = 0; i < textureCount; i++) { byte[] textureData = null; serializer.Serialize(ref textureData); Textures.Add(textureData); } } else { serializer.Writer.Write(Textures.Count); for (int i = 0; i < Textures.Count; i++) { byte[] textureData = Textures[i]; serializer.Serialize(ref textureData); } } serializer.EndChunk(); // Material section serializer.BeginChunk("MATL"); serializer.Serialize(ref Materials); serializer.EndChunk(); // Bones section serializer.BeginChunk("BONE"); serializer.Serialize(ref Bones); serializer.EndChunk(); // Mesh section serializer.BeginChunk("MESH"); serializer.Serialize(ref Meshes); serializer.EndChunk(); // Animation section serializer.BeginChunk("ANIM"); serializer.Serialize(ref Animations); serializer.EndChunk(); // Serialize attributes serializer.Serialize(ref Attributes); // Close TKMD section serializer.EndChunk(); }
/// <summary> /// Saves the specified pixel buffers in TKTX format. /// </summary> internal static Image LoadTKTX(IntPtr dataPointer, int dataSize, bool makeACopy, GCHandle? handle) { // Make a copy? if (makeACopy) { var temp = Utilities.AllocateMemory(dataSize); Utilities.CopyMemory(temp, dataPointer, dataSize); dataPointer = temp; } // Use DataStream to load from memory pointer var imageStream = new DataStream(dataPointer, dataSize, true, true); var beginPosition = imageStream.Position; var serializer = new BinarySerializer(imageStream, SerializerMode.Read); var description = default(ImageDescription); // Load MagicCode TKTX try { serializer.BeginChunk(MagicCodeTKTX); } catch (InvalidChunkException ex) { // If magic code not found, return null if (ex.ExpectedChunkId == MagicCodeTKTX) return null; throw; } // Read description serializer.Serialize(ref description); // Read size of pixel buffer int size = 0; serializer.Serialize(ref size); // Pad to align pixel buffer on 16 bytes (fixed offset at 48 bytes from the beginning of the file). var padBuffer = new byte[OffsetBufferTKTX - (int)(imageStream.Position - beginPosition)]; if (padBuffer.Length > 0) { if (imageStream.Read(padBuffer, 0, padBuffer.Length) != padBuffer.Length) throw new EndOfStreamException(); } // Check that current offset is exactly our fixed offset. int pixelBufferOffsets = (int)serializer.Stream.Position; if (pixelBufferOffsets != OffsetBufferTKTX) throw new InvalidOperationException(string.Format("Unexpected offset [{0}] for pixel buffers. Must be {1}", pixelBufferOffsets, OffsetBufferTKTX)); // Seek to the end of the stream to the number of pixel buffers imageStream.Seek(size, SeekOrigin.Current); // Close the chunk to verify that we did read the whole chunk serializer.EndChunk(); return new Image(description, dataPointer, pixelBufferOffsets, handle, makeACopy); }