/// <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> /// Writes an object to the buffer. /// </summary> /// <param name="obj"></param> protected void SaveObject(SaveDataObject obj) { int size, preSize, postData; preSize = WorkBuff.Position; WorkBuff.Skip(4); size = Serializer.Write(WorkBuff, obj, FileFormat); postData = WorkBuff.Position; WorkBuff.Seek(preSize); WorkBuff.Write(size); WorkBuff.Seek(postData); WorkBuff.Align4(); Debug.WriteLine($"{obj.GetType().Name}: {size} bytes written."); }
/// <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); }