/// <summary> /// Loads an object from the buffer. /// </summary> protected int LoadObject <T>(T obj) where T : SaveDataObject { int size = WorkBuff.ReadInt32(); int bytesRead = Serializer.Read(obj, WorkBuff, FileFormat); WorkBuff.Align4(); Debug.WriteLine($"{typeof(T).Name}: {bytesRead} bytes read."); Debug.Assert(bytesRead <= DataBuffer.Align4(size)); return(bytesRead); }
/// <summary> /// Allocates the required space for the specified type, /// then loads in an instance of that type. /// </summary> protected T LoadTypePreAlloc <T>() where T : SaveDataObject { int size = WorkBuff.ReadInt32(); if (!(Activator.CreateInstance(typeof(T), size) is T obj)) { throw new SerializationException(Strings.Error_Serialization_NoPreAlloc, typeof(T)); } Debug.WriteLine($"{typeof(T).Name}: {size} bytes pre-allocated."); int bytesRead = Serializer.Read(obj, WorkBuff, FileFormat); WorkBuff.Align4(); Debug.WriteLine($"{typeof(T).Name}: {bytesRead} bytes read."); Debug.Assert(bytesRead <= DataBuffer.Align4(size)); return(obj); }