-
Notifications
You must be signed in to change notification settings - Fork 43
/
TypeModel.cs
330 lines (297 loc) · 9.56 KB
/
TypeModel.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Concurrent;
using Nino.Shared.Util;
using System.Reflection;
using System.Collections.Generic;
using System.Threading;
using Nino.Shared.IO;
// ReSharper disable CognitiveComplexity
namespace Nino.Serialization
{
/// <summary>
/// A model of a serialized type
/// </summary>
internal class TypeModel
{
private const string HelperName = "NinoSerializationHelper";
private const BindingFlags ReflectionFlags =
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default;
private const BindingFlags StaticReflectionFlags = ReflectionFlags | BindingFlags.Static;
private const BindingFlags FlattenHierarchyReflectionFlags = ReflectionFlags | BindingFlags.FlattenHierarchy;
private static readonly Type NinoSerializeType = typeof(NinoSerializeAttribute);
private static readonly Type NinoMemberType = typeof(NinoMemberAttribute);
private static readonly Type NinoIgnoreType = typeof(NinoIgnoreAttribute);
public HashSet<MemberInfo> Members;
public bool Valid;
public bool IncludeAll;
/// <summary>
/// Cached Models
/// </summary>
private static readonly Dictionary<int, TypeModel> TypeModels = new Dictionary<int, TypeModel>(10);
/// <summary>
/// Cached Models
/// </summary>
internal static Dictionary<int, Type> AllTypes = new Dictionary<int, Type>(100);
static TypeModel()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
AllTypes[type.GetHashCode()] = type;
}
}
}
/// <summary>
/// Cached Models
/// </summary>
private static readonly Dictionary<int, TypeCode> TypeCodes = new Dictionary<int, TypeCode>(30)
{
{ typeof(byte).GetTypeHashCode(), TypeCode.Byte },
{ typeof(sbyte).GetTypeHashCode(), TypeCode.SByte },
{ typeof(short).GetTypeHashCode(), TypeCode.Int16 },
{ typeof(ushort).GetTypeHashCode(), TypeCode.UInt16 },
{ typeof(int).GetTypeHashCode(), TypeCode.Int32 },
{ typeof(uint).GetTypeHashCode(), TypeCode.UInt32 },
{ typeof(long).GetTypeHashCode(), TypeCode.Int64 },
{ typeof(ulong).GetTypeHashCode(), TypeCode.UInt64 },
{ typeof(float).GetTypeHashCode(), TypeCode.Single },
{ typeof(double).GetTypeHashCode(), TypeCode.Double },
{ typeof(decimal).GetTypeHashCode(), TypeCode.Decimal },
{ typeof(char).GetTypeHashCode(), TypeCode.Char },
{ typeof(bool).GetTypeHashCode(), TypeCode.Boolean },
{ typeof(string).GetTypeHashCode(), TypeCode.String },
{ typeof(object).GetTypeHashCode(), TypeCode.Object },
{ typeof(DateTime).GetTypeHashCode(), TypeCode.DateTime },
};
/// <summary>
/// Generated helpers
/// </summary>
private static readonly Dictionary<int, INinoWrapper> GeneratedWrapper = new Dictionary<int, INinoWrapper>(50)
{
{ typeof(byte).GetTypeHashCode(), null },
{ typeof(sbyte).GetTypeHashCode(), null },
{ typeof(short).GetTypeHashCode(), null },
{ typeof(ushort).GetTypeHashCode(), null },
{ typeof(int).GetTypeHashCode(), null },
{ typeof(uint).GetTypeHashCode(), null },
{ typeof(long).GetTypeHashCode(), null },
{ typeof(ulong).GetTypeHashCode(), null },
{ typeof(bool).GetTypeHashCode(), null },
{ typeof(float).GetTypeHashCode(), null },
{ typeof(double).GetTypeHashCode(), null },
{ typeof(decimal).GetTypeHashCode(), null },
{ typeof(char).GetTypeHashCode(), null },
{ typeof(string).GetTypeHashCode(), null },
{ typeof(DateTime).GetTypeHashCode(), null },
};
/// <summary>
/// No compression types
/// </summary>
private static readonly HashSet<int> NoCompressionTypes = new HashSet<int>()
{
typeof(int).GetTypeHashCode(),
typeof(uint).GetTypeHashCode(),
typeof(long).GetTypeHashCode(),
typeof(ulong).GetTypeHashCode(),
typeof(byte).GetTypeHashCode(),
typeof(sbyte).GetTypeHashCode(),
typeof(short).GetTypeHashCode(),
typeof(ushort).GetTypeHashCode(),
typeof(bool).GetTypeHashCode(),
typeof(char).GetTypeHashCode(),
typeof(decimal).GetTypeHashCode(),
typeof(double).GetTypeHashCode(),
typeof(float).GetTypeHashCode(),
typeof(DateTime).GetTypeHashCode(),
};
/// <summary>
/// Cached Models
/// </summary>
private static readonly ConcurrentDictionary<Type, bool> IsEnumTypeCache =
new ConcurrentDictionary<Type, bool>(3, 30);
/// <summary>
/// Whether or not the type is a non compress type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsNonCompressibleType(Type type)
{
return NoCompressionTypes.Contains(type.GetTypeHashCode()) || IsEnum(type);
}
/// <summary>
/// Get a type code
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static TypeCode GetTypeCode(Type type)
{
#if ILRuntime
if (IsEnum(type) && type is ILRuntime.Reflection.ILRuntimeType)
{
type = Enum.GetUnderlyingType(type);
}
#endif
var hash = type.GetTypeHashCode();
if (TypeCodes.TryGetValue(hash, out var ret))
{
return ret;
}
TypeCodes[hash] = ret = Type.GetTypeCode(type);
return ret;
}
/// <summary>
/// Get whether or not a type is enum
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsEnum(Type type)
{
if (IsEnumTypeCache.TryGetValue(type, out var ret)) return ret;
IsEnumTypeCache[type] = ret = type.IsEnum;
return ret;
}
/// <summary>
/// Get whether or not a type is a code gen type
/// </summary>
/// <param name="type"></param>
/// <param name="helper"></param>
/// <returns></returns>
internal static bool TryGetWrapper(Type type, out INinoWrapper helper)
{
var hash = type.GetTypeHashCode();
if (GeneratedWrapper.TryGetValue(hash, out helper)) return helper != null;
var field = type.GetField(HelperName, StaticReflectionFlags);
helper = (INinoWrapper)field?.GetValue(null);
GeneratedWrapper[hash] = helper;
return helper != null;
}
/// <summary>
/// Try get cached model
/// </summary>
/// <param name="type"></param>
/// <param name="model"></param>
/// <returns></returns>
internal static void TryGetModel(Type type, out TypeModel model)
{
var hash = type.GetTypeHashCode();
if (TypeModels.TryGetValue(hash, out model)) return;
object[] ns = type.GetCustomAttributes(NinoSerializeType, false);
if (ns.Length != 0) return;
model = new TypeModel()
{
Valid = false
};
TypeModels.Add(hash, model);
throw new InvalidOperationException(
$"The type {type.FullName} does not have NinoSerialize attribute or custom importer/exporter");
}
/// <summary>
/// Thread safe lock
/// </summary>
private static SpinLock _createLock;
/// <summary>
/// Create a typeModel using given type
/// </summary>
/// <param name="type"></param>
/// <exception cref="InvalidOperationException"></exception>
// ReSharper disable CognitiveComplexity
internal static TypeModel CreateModel(Type type)
// ReSharper restore CognitiveComplexity
{
TypeModel model;
//thread safe
bool lockTaken = false;
try
{
_createLock.Enter(ref lockTaken);
TryGetModel(type, out model);
if (model != null) return model;
model = new TypeModel
{
Valid = true,
//fetch members
Members = new HashSet<MemberInfo>(),
};
//include all or not
object[] ns = type.GetCustomAttributes(NinoSerializeType, true);
model.IncludeAll = ((NinoSerializeAttribute)ns[0]).IncludeAll;
//flag
ushort index = 0;
//dict
SortedDictionary<uint, MemberInfo> dict = ObjectPool<SortedDictionary<uint, MemberInfo>>.Request();
dict.Clear();
//fetch fields (only public and private fields that declared in the type)
FieldInfo[] fs = type.GetFields(FlattenHierarchyReflectionFlags);
//iterate fields
foreach (var f in fs)
{
if (model.IncludeAll)
{
//skip nino ignore
var ig = f.GetCustomAttributes(NinoIgnoreType, true);
if (ig.Length > 0) continue;
index++;
}
else
{
ns = f.GetCustomAttributes(NinoMemberType, true);
//not fetch all and no attribute => skip this member
if (ns.Length != 1) continue;
index = ((NinoMemberAttribute)ns[0]).Index;
}
//record field
dict.Add(index, f);
}
//fetch properties (only public and private properties that declared in the type)
PropertyInfo[] ps = type.GetProperties(FlattenHierarchyReflectionFlags);
//iterate properties
foreach (var p in ps)
{
//has to have getter and setter
if (!(p.CanRead && p.CanWrite))
{
throw new InvalidOperationException(
$"Cannot read or write property {p.Name} in {type.FullName}, cannot Serialize or Deserialize this property");
}
if (model.IncludeAll)
{
//skip nino ignore
var ig = p.GetCustomAttributes(NinoIgnoreType, true);
if (ig.Length > 0) continue;
index++;
}
else
{
ns = p.GetCustomAttributes(NinoMemberType, true);
//not fetch all and no attribute => skip this member
if (ns.Length != 1) continue;
index = ((NinoMemberAttribute)ns[0]).Index;
}
//record property
dict.Add(index, p);
}
//add members to model
foreach (var pair in dict)
{
model.Members.Add(pair.Value);
}
//release dict
dict.Clear();
ObjectPool<SortedDictionary<uint, MemberInfo>>.Return(dict);
if (model.Members.Count == 0)
{
model.Valid = false;
}
TypeModels[type.GetTypeHashCode()] = model;
}
finally
{
if (lockTaken) _createLock.Exit(false);
}
return model;
}
}
}