-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
AnimationKeyValuePairArraySerializer.cs
54 lines (49 loc) · 1.97 KB
/
AnimationKeyValuePairArraySerializer.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
50
51
52
53
54
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using Stride.Core;
using Stride.Core.Serialization;
namespace Stride.Animations
{
public class AnimationKeyValuePairArraySerializer<T> : DataSerializer<AnimationKeyValuePair<T>[]> where T : struct
{
private DataSerializer<AnimationKeyValuePair<T>> itemDataSerializer;
/// <inheritdoc/>
public override void Initialize(SerializerSelector serializerSelector)
{
itemDataSerializer = MemberSerializer<AnimationKeyValuePair<T>>.Create(serializerSelector);
}
public override void PreSerialize(ref AnimationKeyValuePair<T>[] obj, ArchiveMode mode, SerializationStream stream)
{
if (mode == ArchiveMode.Serialize)
{
stream.Write(obj.Length);
}
else if (mode == ArchiveMode.Deserialize)
{
int length = stream.ReadInt32();
obj = new AnimationKeyValuePair<T>[length];
}
}
public unsafe override void Serialize(ref AnimationKeyValuePair<T>[] obj, ArchiveMode mode, SerializationStream stream)
{
if (mode == ArchiveMode.Deserialize)
{
int count = obj.Length;
var rawData = stream.ReadBytes(Utilities.SizeOf<AnimationKeyValuePair<T>>() * count);
fixed (void* rawDataPtr = rawData)
{
Utilities.Read((IntPtr)rawDataPtr, obj, 0, count);
}
}
else if (mode == ArchiveMode.Serialize)
{
int count = obj.Length;
for (int i = 0; i < count; ++i)
{
itemDataSerializer.Serialize(ref obj[i], mode, stream);
}
}
}
}
}