public void WriteFormat2Data(SerializationInfo info, StreamingContext context) { DeferredFormatter deferred = context.Context as DeferredFormatter; if (deferred != null) { info.AddValue("deferred", true); deferred.AddDeferredObject(this, this.length); } else { WriteFormat1Data(info, context); } }
private MemoryBlock(SerializationInfo info, StreamingContext context) { disposed = false; // Try to read a 64-bit value, and for backwards compatibility fall back on a 32-bit value. try { this.length = info.GetInt64("length64"); } catch (SerializationException) { this.length = (long)info.GetInt32("length"); } try { this.bitmapWidth = (int)info.GetInt32("bitmapWidth"); this.bitmapHeight = (int)info.GetInt32("bitmapHeight"); if (this.bitmapWidth != 0 || this.bitmapHeight != 0) { long bytes = (long)this.bitmapWidth * (long)this.bitmapHeight * (long)ColorBgra.SizeOf; if (bytes != this.length) { throw new ApplicationException("Invalid file format: width * height * 4 != length"); } } } catch (SerializationException) { this.bitmapWidth = 0; this.bitmapHeight = 0; } bool hasParent = info.GetBoolean("hasParent"); if (hasParent) { this.parentBlock = (MemoryBlock)info.GetValue("parentBlock", typeof(MemoryBlock)); // Try to read a 64-bit value, and for backwards compatibility fall back on a 32-bit value. long parentOffset; try { parentOffset = info.GetInt64("parentOffset64"); } catch (SerializationException) { parentOffset = (long)info.GetInt32("parentOffset"); } this.voidStar = (void *)((byte *)parentBlock.VoidStar + parentOffset); this.valid = true; } else { DeferredFormatter deferredFormatter = context.Context as DeferredFormatter; bool deferred = false; // Was this stream serialized with deferment? foreach (SerializationEntry entry in info) { if (entry.Name == "deferred") { deferred = (bool)entry.Value; break; } } if (deferred && deferredFormatter != null) { // The newest PDN files use deferred deserialization. This lets us read straight from the stream, // minimizing memory use and adding the potential for multithreading // Deserialization will complete in IDeferredDeserializer.FinishDeserialization() deferredFormatter.AddDeferredObject(this, this.length); } else if (deferred && deferredFormatter == null) { throw new InvalidOperationException("stream has deferred serialization streams, but a DeferredFormatter was not provided"); } else { this.voidStar = Allocate(this.length).ToPointer(); this.valid = true; // Non-deferred format serializes one big byte[] chunk. This is also // how PDN files were saved with v2.1 Beta 2 and before. byte[] array = (byte[])info.GetValue("pointerData", typeof(byte[])); fixed(byte *pbArray = array) { Memory.Copy(this.VoidStar, (void *)pbArray, (ulong)array.LongLength); } } } }