forked from citizenfx/msgpack-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryFormatter.cs
More file actions
248 lines (202 loc) · 8.63 KB
/
DictionaryFormatter.cs
File metadata and controls
248 lines (202 loc) · 8.63 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Reflection;
using static CitizenFX.MsgPack.Detail.Helper;
using static CitizenFX.MsgPack.Detail.SerializerAccess;
namespace CitizenFX.MsgPack.Formatters
{
internal static class DictionaryFormatter
{
public static Tuple<Serializer, MethodInfo> Build(Type typeKey, Type typeValue)
{
#if IS_FXSERVER
MethodInfo methodSerialize, methodDeserialize, methodObjectSerialize;
#else
MethodInfo methodDeserialize;
#endif
Type typeKeyValuePair = typeof(KeyValuePair<,>).MakeGenericType(typeKey, typeValue);
Type typeDictionary = typeof(Dictionary<,>).MakeGenericType(typeKey, typeValue);
Type typeIDictionary = typeof(IDictionary<,>).MakeGenericType(typeKey, typeValue);
Type typeIReadOnlyDictionary = typeof(IReadOnlyDictionary<,>).MakeGenericType(typeKey, typeValue);
string name = $"DictionaryFormatter_{typeKey.FullName}_{typeValue.FullName}";
Type buildType = MsgPackRegistry.m_moduleBuilder.GetType(name);
if (buildType == null)
{
TypeBuilder typeBuilder = MsgPackRegistry.m_moduleBuilder.DefineType(name);
#if IS_FXSERVER
methodSerialize = BuildSerializer(typeKey, typeValue, typeKeyValuePair, typeIDictionary, typeBuilder);
BuildDeserializer(typeKey, typeValue, typeDictionary, typeBuilder);
BuildObjectSerializer(typeIDictionary, methodSerialize, typeBuilder);
#else
BuildDeserializer(typeKey, typeValue, typeDictionary, typeBuilder);
#endif
buildType = typeBuilder.CreateType();
}
#if IS_FXSERVER
methodSerialize = buildType.GetMethod("Serialize", new[] { typeof(MsgPackSerializer), typeIDictionary });
methodDeserialize = buildType.GetMethod("Deserialize");
methodObjectSerialize = buildType.GetMethod("Serialize", new[] { typeof(MsgPackSerializer), typeof(object) });
#else
methodDeserialize = buildType.GetMethod("Deserialize");
#endif
#if IS_FXSERVER
Serializer serializeMethod = new Serializer(methodSerialize, methodObjectSerialize);
MsgPackRegistry.RegisterSerializer(typeDictionary, serializeMethod);
MsgPackRegistry.RegisterSerializer(typeIDictionary, serializeMethod);
MsgPackRegistry.RegisterSerializer(typeIReadOnlyDictionary, serializeMethod);
#else
Serializer serializeMethod = new Serializer();
#endif
MsgPackRegistry.RegisterDeserializer(typeDictionary, methodDeserialize);
MsgPackRegistry.RegisterDeserializer(typeIDictionary, methodDeserialize);
MsgPackRegistry.RegisterDeserializer(typeIReadOnlyDictionary, methodDeserialize);
return new Tuple<Serializer, MethodInfo>(serializeMethod, methodDeserialize);
}
/// <summary>
/// Simply unpacks and calls <paramref name="methodSerialize"/>
/// </summary>
/// <param name="typeIDictionary">Type we're serializing</param>
/// <param name="methodSerialize">Method to call once the object is unpacked</param>
/// <param name="typeBuilder">Building type to add this method to</param>
/// <returns></returns>
private static MethodInfo BuildObjectSerializer(Type typeIDictionary, MethodInfo methodSerialize, TypeBuilder typeBuilder)
{
MethodBuilder methodSerializeObject = typeBuilder.DefineMethod("Serialize",
MethodAttributes.Public | MethodAttributes.Static,
typeof(void), new[] { typeof(MsgPackSerializer), typeof(object) });
var g = methodSerializeObject.GetILGenerator();
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldarg_1);
g.Emit(OpCodes.Unbox_Any, typeIDictionary);
g.EmitCall(OpCodes.Call, methodSerialize, null);
g.Emit(OpCodes.Ret);
return methodSerializeObject;
}
private static MethodInfo BuildSerializer(Type typeKey, Type typeValue, Type typeKeyValuePair, Type typeIDictionary, TypeBuilder typeBuilder)
{
Type typeIEnumerator = typeof(IEnumerator<>).MakeGenericType(typeKeyValuePair);
Type typeIEnumerable = typeof(IEnumerable<>).MakeGenericType(typeKeyValuePair);
Type typeIReadOnlyCollection = typeof(IReadOnlyCollection<>).MakeGenericType(typeKeyValuePair);
// IDictionary<K, V> (de)serialization
MethodBuilder methodSerialize = typeBuilder.DefineMethod("Serialize", MethodAttributes.Public | MethodAttributes.Static,
typeof(void), new[] { typeof(MsgPackSerializer), typeIDictionary });
var g = methodSerialize.GetILGenerator();
g.DeclareLocal(typeIEnumerator);
g.DeclareLocal(typeKeyValuePair);
// if (dictionary == null) goto WriteNil()
Label nilWrite = g.DefineLabel();
g.Emit(OpCodes.Ldarg_1);
g.Emit(OpCodes.Ldnull);
g.Emit(OpCodes.Beq, nilWrite);
g.Emit(OpCodes.Ldarg_0);
// get count
g.Emit(OpCodes.Ldarg_1);
g.EmitCall(OpCodes.Callvirt, typeIReadOnlyCollection.GetProperty("Count", Type.EmptyTypes).GetGetMethod(), null);
// write header
g.EmitCall(OpCodes.Call, GetVoidMethod<uint>(WriteMapHeader), null);
// get dictionary enumerator
g.Emit(OpCodes.Ldarg_1);
g.EmitCall(OpCodes.Callvirt, typeIEnumerable.GetMethod("GetEnumerator", Type.EmptyTypes), null);
g.Emit(OpCodes.Stloc_0);
// while (enumerator.MoveNext())
{
Label whileCond = g.DefineLabel();
Label whileLoop = g.DefineLabel();
g.Emit(OpCodes.Br_S, whileCond);
g.MarkLabel(whileLoop);
// enumerator.Current
g.Emit(OpCodes.Ldloc_0);
g.EmitCall(OpCodes.Callvirt, typeIEnumerator.GetProperty("Current", Type.EmptyTypes).GetGetMethod(), null);
g.Emit(OpCodes.Stloc_1);
// serialize .Key
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldloca_S, (byte)1);
g.EmitCall(OpCodes.Call, typeKeyValuePair.GetProperty("Key", Type.EmptyTypes).GetGetMethod(), null);
g.EmitCall(OpCodes.Call, MsgPackRegistry.GetOrCreateSerializer(typeKey), null);
// serialize .Value
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldloca_S, (byte)1);
g.EmitCall(OpCodes.Call, typeKeyValuePair.GetProperty("Value", Type.EmptyTypes).GetGetMethod(), null);
g.EmitCall(OpCodes.Call, MsgPackRegistry.GetOrCreateSerializer(typeValue), null);
// enumerator.MoveNext() condition
g.MarkLabel(whileCond);
g.Emit(OpCodes.Ldloc_0);
g.EmitCall(OpCodes.Callvirt, typeof(IEnumerator).GetMethod("MoveNext", Type.EmptyTypes), null);
g.Emit(OpCodes.Brtrue, whileLoop);
}
g.Emit(OpCodes.Ret);
// write nil
g.MarkLabel(nilWrite);
g.Emit(OpCodes.Ldarg_0);
g.EmitCall(OpCodes.Call, GetVoidMethod(WriteNil), null);
g.Emit(OpCodes.Ret);
return methodSerialize;
}
private static MethodInfo BuildDeserializer(Type typeKey, Type typeValue, Type typeDictionary, TypeBuilder typeBuilder)
{
MethodBuilder methodDeserialize = typeBuilder.DefineMethod("Deserialize",
MethodAttributes.Public | MethodAttributes.Static,
typeDictionary, new[] { typeof(MsgPackDeserializer).MakeByRefType() });
var g = methodDeserialize.GetILGenerator();
g.DeclareLocal(typeof(uint)); // type first size after
g.DeclareLocal(typeof(uint));
g.DeclareLocal(typeDictionary);
// get type
g.Emit(OpCodes.Ldarg_0);
g.EmitCall(OpCodes.Call, GetResultMethod(ReadByte), null);
g.Emit(OpCodes.Stloc_0);
// if (array == null) return null
Label nilWrite = g.DefineLabel();
g.Emit(OpCodes.Ldloc_0);
g.Emit(OpCodes.Ldc_I4, (int)MsgPackCode.Nil);
g.Emit(OpCodes.Beq, nilWrite);
// get size and create array with the read size
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldloc_0);
g.EmitCall(OpCodes.Call, GetResultMethod<byte, uint>(ReadMapSize), null);
g.Emit(OpCodes.Stloc_0); // use loc_0 as size now
// loc_2 = new Dictionary<K, V>();
g.Emit(OpCodes.Newobj, typeDictionary.GetConstructor(Type.EmptyTypes));
g.Emit(OpCodes.Stloc_2);
// i = 0
g.Emit(OpCodes.Ldc_I4_0);
g.Emit(OpCodes.Stloc_1);
// for (uint i = 0; i < length; ++i)
{
Label whileCond = g.DefineLabel();
Label whileLoop = g.DefineLabel();
g.Emit(OpCodes.Br_S, whileCond);
g.MarkLabel(whileLoop);
// dictionary prestacking [ array, value ]
g.Emit(OpCodes.Ldloc_2);
// deserialize key
g.Emit(OpCodes.Ldarg_0);
g.EmitCall(OpCodes.Call, MsgPackRegistry.GetOrCreateDeserializer(typeKey), null);
// deserialize value
g.Emit(OpCodes.Ldarg_0);
g.EmitCall(OpCodes.Call, MsgPackRegistry.GetOrCreateDeserializer(typeValue), null);
// store it
g.EmitCall(OpCodes.Call, typeDictionary.GetMethod("Add", new[] { typeKey, typeValue }), null);
// ++i
g.Emit(OpCodes.Ldloc_1);
g.Emit(OpCodes.Ldc_I4_1);
g.Emit(OpCodes.Add);
g.Emit(OpCodes.Stloc_1);
// i < length
g.MarkLabel(whileCond);
g.Emit(OpCodes.Ldloc_1);
g.Emit(OpCodes.Ldloc_0);
g.Emit(OpCodes.Blt_Un, whileLoop);
}
g.Emit(OpCodes.Ldloc_2);
g.Emit(OpCodes.Ret);
// return null
g.MarkLabel(nilWrite);
g.Emit(OpCodes.Ldnull);
g.Emit(OpCodes.Ret);
return methodDeserialize;
}
}
}