forked from nikeee/TeamSpeak3QueryApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITypeCaster.cs
142 lines (125 loc) · 3.56 KB
/
ITypeCaster.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using TeamSpeak3QueryApi.Net.Extensions;
namespace TeamSpeak3QueryApi.Net.Interfaces
{
interface ITypeCaster
{
dynamic Cast(object source);
}
class Int16TypeCaster : ITypeCaster
{
public virtual dynamic Cast(object source)
{
if (source == null)
return (short)0;
if (source is short)
return (short)source;
return short.Parse(source.ToString());
}
}
class Int32TypeCaster : ITypeCaster
{
public virtual dynamic Cast(object source)
{
if (source == null)
return 0;
if (source is int)
return (int)source;
return int.Parse(source.ToString());
}
}
class Int64TypeCaster : ITypeCaster
{
public virtual dynamic Cast(object source)
{
if (source == null)
return 0;
if (source is long)
return (long)source;
return long.Parse(source.ToString());
}
}
class EnumTypeCaster<T> : Int32TypeCaster where T : struct
{
public override dynamic Cast(object source)
{
var i = base.Cast(source);
return (T)i;
}
}
class StringTypeCaster : ITypeCaster
{
public dynamic Cast(object source)
{
if (source == null)
return null;
if (source is int)
return ((int)source).ToString().TeamSpeakUnescape();
return source.ToString().TeamSpeakUnescape();
}
}
class BooleanTypeCaster : ITypeCaster
{
public dynamic Cast(object source)
{
if (source == null)
return false;
if (source is int)
return ((int)source) != 0;
return int.Parse(source.ToString()) != 0;
}
}
class TimeSpanTypeCaster : ITypeCaster
{
public dynamic Cast(object source)
{
if (source == null)
return false;
if (source is int)
return TimeSpan.FromSeconds((int)source);
return TimeSpan.FromSeconds(int.Parse(source.ToString()));
}
}
class DateTimeTypeCaster : ITypeCaster
{
public dynamic Cast(object source)
{
if (source == null)
return false;
long unixTimestamp;
if (source is long)
unixTimestamp = (long)source;
else
unixTimestamp = long.Parse(source.ToString());
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime;
}
}
class ReadonlyListIntCaster : ITypeCaster
{
public dynamic Cast(object source)
{
if (source == null)
return false;
if (source is int)
return new List<int> { (int)source };
var sourceStr = source as string;
if (string.IsNullOrWhiteSpace(sourceStr))
return null;
return sourceStr.Split(',').Select(int.Parse).ToList();
}
}
class DoubleTypeCaster : ITypeCaster
{
public virtual dynamic Cast(object source)
{
if (source == null)
return 0.0;
if (source is double)
return (double)source;
return double.Parse(source.ToString(), CultureInfo.InvariantCulture);
}
}
}