forked from the-dargon-project/PortableObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PofSerializer.cs
49 lines (42 loc) · 1.45 KB
/
PofSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.IO;
namespace Dargon.PortableObjects
{
public class PofSerializer
{
private readonly PofContext context;
public PofSerializer(PofContext context)
{
this.context = context;
}
public void Serialize<T>(Stream stream, T portableObject) where T : IPortableObject
{
var slotDestination = new SlotDestination();
var pofWriter = new PofWriter(context, slotDestination);
pofWriter.WriteToSlots(portableObject);
slotDestination.WriteToStream(stream);
}
public void Serialize<T>(BinaryWriter writer, T portableObject) where T : IPortableObject
{
var slotDestination = new SlotDestination();
var pofWriter = new PofWriter(context, slotDestination);
pofWriter.WriteObject(0, portableObject);
var data = slotDestination[0];
writer.Write((int)data.Length);
writer.Write(data);
}
public T Deserialize<T>(Stream stream)
where T : IPortableObject
{
using (var reader = new BinaryReader(stream))
return Deserialize<T>(reader);
}
public T Deserialize<T>(BinaryReader reader)
where T : IPortableObject
{
var dataLength = reader.ReadInt32();
var data = reader.ReadBytes(dataLength);
var pofReader = new PofReader(context, SlotSourceFactory.CreateWithSingleSlot(data));
return pofReader.ReadObject<T>(0);
}
}
}