forked from nikeee/TeamSpeak3QueryApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataProxy.cs
108 lines (98 loc) · 4.02 KB
/
DataProxy.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using TeamSpeak3QueryApi.Net.Enums;
using TeamSpeak3QueryApi.Net.Interfaces;
using TeamSpeak3QueryApi.Net.Query;
namespace TeamSpeak3QueryApi.Net
{
internal static class DataProxy
{
private static readonly ITypeCaster DefaultCaster = new StringTypeCaster();
private static readonly Dictionary<Type, ITypeCaster> Casters = new Dictionary<Type, ITypeCaster>
{
[typeof(int)] = new Int32TypeCaster(),
[typeof(string)] = DefaultCaster,
[typeof(bool)] = new BooleanTypeCaster(),
[typeof(ReasonId)] = new EnumTypeCaster<ReasonId>(),
[typeof(ClientType)] = new EnumTypeCaster<ClientType>(),
[typeof(ServerGroupType)] = new EnumTypeCaster<ServerGroupType>(),
[typeof(TimeSpan)] = new TimeSpanTypeCaster(),
[typeof(DateTime)] = new DateTimeTypeCaster(),
[typeof(long)] = new Int64TypeCaster(),
[typeof(MessageTarget)] = new EnumTypeCaster<MessageTarget>(),
[typeof(short)] = new Int16TypeCaster(),
[typeof(Codec)] = new EnumTypeCaster<Codec>(),
[typeof(IReadOnlyList<int>)] = new ReadonlyListIntCaster(),
[typeof(double)] = new DoubleTypeCaster()
};
public static IReadOnlyList<T> SerializeGeneric<T>(IReadOnlyList<QueryResponseDictionary> response)
where T : ITeamSpeakSerializable
{
if (response == null || response.Count == 0)
return new ReadOnlyCollection<T>(new T[0]);
var fields = typeof(T).GetRuntimeFields(); // Was: .GetFields();
var destList = new List<T>(response.Count);
foreach (var item in response)
{
var destType = Activator.CreateInstance<T>();
foreach (var v in item)
{
var matchedEntry = fields.SingleOrDefault(
fi =>
{
var ca = fi.GetCustomAttributes<QuerySerializeAttribute>(false);
return fi.Name == v.Key || ca.Any(qsa => qsa.Name == v.Key);
});
if (matchedEntry != null)
{
var castedValue = CastForType(matchedEntry.FieldType, v.Value);
try
{
matchedEntry.SetValue(destType, castedValue);
}
catch (Exception)
{
Debugger.Break();
throw;
}
}
}
destList.Add(destType);
}
return new ReadOnlyCollection<T>(destList);
}
private static dynamic CastForType(Type type, object value)
{
if (type.IsArray)
{
if (value == null)
return null;
var arrayOf = type.GetElementType();
var str = value.ToString();
var arr = str.Split('|');
dynamic typedArray = Array.CreateInstance(arrayOf, arr.Length);
for (int i = 0; i < arr.Length; ++i)
typedArray[i] = CastForType(arrayOf, arr[i]); // Multi-Dimensional array are not possible due to escaping limitations, but let's work recursive anyway
return typedArray;
}
if (Casters.ContainsKey(type))
{
try
{
var caster = Casters[type];
return caster.Cast(value);
}
catch (Exception)
{
Debugger.Break();
return DefaultCaster.Cast(value);
}
}
return DefaultCaster.Cast(value);
}
}
}