| | 1 | | using System; |
| | 2 | | using VRC.SDK3.Data; |
| | 3 | |
|
| | 4 | | namespace Kemocade.Vrc.Data.Extensions |
| | 5 | | { |
| | 6 | | public static partial class DataTokenExtensions |
| | 7 | | { |
| | 8 | | private const string BOOL = "System.Boolean"; |
| | 9 | | private const string SBYTE = "System.SByte"; |
| | 10 | | private const string BYTE = "System.Byte"; |
| | 11 | | private const string SHORT = "System.Int16"; |
| | 12 | | private const string USHORT = "System.UInt16"; |
| | 13 | | private const string INT = "System.Int32"; |
| | 14 | | private const string UINT = "System.UInt32"; |
| | 15 | | private const string LONG = "System.Int64"; |
| | 16 | | private const string ULONG = "System.UInt64"; |
| | 17 | | private const string FLOAT = "System.Single"; |
| | 18 | | private const string DOUBLE = "System.Double"; |
| | 19 | | private const string STRING = "System.String"; |
| | 20 | | private const string DATA_LIST = "VRC.SDK3.Data.DataList"; |
| | 21 | | private const string DATA_DICTIONARY = "VRC.SDK3.Data.DataDictionary"; |
| | 22 | | private const string OBJECT = "System.Object"; |
| | 23 | | private const string DECIMAL = "System.Decimal"; |
| | 24 | |
|
| | 25 | | // TODO: special error message if passed in array type (use TryCastArray instead!) |
| | 26 | | public static bool TryCast<T>(this DataToken input, out T result) |
| 370 | 27 | | { |
| 370 | 28 | | object data = null; |
| 370 | 29 | | Type type = typeof(T); |
| | 30 | |
|
| 370 | 31 | | switch (input.TokenType) |
| | 32 | | { |
| | 33 | | case TokenType.Boolean: |
| 4 | 34 | | switch (typeof(T).FullName) |
| | 35 | | { |
| | 36 | | // Self |
| | 37 | | case BOOL: |
| 4 | 38 | | case OBJECT: data = input.Boolean; break; |
| | 39 | | // String |
| 2 | 40 | | case STRING: data = input.Boolean.ToString(); break; |
| | 41 | | } |
| 4 | 42 | | break; |
| | 43 | | case TokenType.SByte: |
| 14 | 44 | | switch (typeof(T).FullName) |
| | 45 | | { |
| | 46 | | // Self |
| | 47 | | case SBYTE: |
| 4 | 48 | | case OBJECT: data = input.SByte; break; |
| | 49 | | // Implicit |
| 2 | 50 | | case SHORT: data = (short)input.SByte; break; |
| 4 | 51 | | case INT: data = (int)input.SByte; break; |
| 2 | 52 | | case LONG: data = (long)input.SByte; break; |
| 2 | 53 | | case FLOAT: data = (float)input.SByte; break; |
| 2 | 54 | | case DOUBLE: data = (double)input.SByte; break; |
| 2 | 55 | | case DECIMAL: data = (decimal)input.SByte; break; |
| | 56 | | // Explicit |
| 2 | 57 | | case BYTE: data = (byte)input.SByte; break; |
| 2 | 58 | | case USHORT: data = (ushort)input.SByte; break; |
| 2 | 59 | | case UINT: data = (uint)input.SByte; break; |
| 2 | 60 | | case ULONG: data = (ulong)input.SByte; break; |
| | 61 | | // String |
| 2 | 62 | | case STRING: data = input.SByte.ToString(); break; |
| | 63 | | } |
| 14 | 64 | | break; |
| | 65 | | case TokenType.Byte: |
| 14 | 66 | | switch (typeof(T).FullName) |
| | 67 | | { |
| | 68 | | // Self |
| | 69 | | case BYTE: |
| 4 | 70 | | case OBJECT: data = input.Byte; break; |
| | 71 | | // Implicit |
| 2 | 72 | | case SHORT: data = (short)input.Byte; break; |
| 2 | 73 | | case USHORT: data = (ushort)input.Byte; break; |
| 4 | 74 | | case INT: data = (int)input.Byte; break; |
| 2 | 75 | | case UINT: data = (uint)input.Byte; break; |
| 2 | 76 | | case LONG: data = (long)input.Byte; break; |
| 2 | 77 | | case ULONG: data = (ulong)input.Byte; break; |
| 2 | 78 | | case FLOAT: data = (float)input.Byte; break; |
| 2 | 79 | | case DOUBLE: data = (double)input.Byte; break; |
| 2 | 80 | | case DECIMAL: data = (decimal)input.Byte; break; |
| | 81 | | // Explicit |
| 2 | 82 | | case SBYTE: data = (sbyte)input.Byte; break; |
| | 83 | | // String |
| 2 | 84 | | case STRING: data = input.Byte.ToString(); break; |
| | 85 | | } |
| 14 | 86 | | break; |
| | 87 | | case TokenType.Short: |
| 14 | 88 | | switch (typeof(T).FullName) |
| | 89 | | { |
| | 90 | | // Self |
| | 91 | | case SHORT: |
| 4 | 92 | | case OBJECT: data = input.Short; break; |
| | 93 | | // Implicit |
| 4 | 94 | | case INT: data = (int)input.Short; break; |
| 2 | 95 | | case LONG: data = (long)input.Short; break; |
| 2 | 96 | | case FLOAT: data = (float)input.Short; break; |
| 2 | 97 | | case DOUBLE: data = (double)input.Short; break; |
| 2 | 98 | | case DECIMAL: data = (decimal)input.Short; break; |
| | 99 | | // Explicit |
| 2 | 100 | | case SBYTE: data = (sbyte)input.Short; break; |
| 2 | 101 | | case BYTE: data = (byte)input.Short; break; |
| 2 | 102 | | case USHORT: data = (ushort)input.Short; break; |
| 2 | 103 | | case UINT: data = (uint)input.Short; break; |
| 2 | 104 | | case ULONG: data = (ulong)input.Short; break; |
| | 105 | | // String |
| 2 | 106 | | case STRING: data = input.Short.ToString(); break; |
| | 107 | | } |
| 14 | 108 | | break; |
| | 109 | | case TokenType.UShort: |
| 14 | 110 | | switch (typeof(T).FullName) |
| | 111 | | { |
| | 112 | | // Self |
| | 113 | | case USHORT: |
| 4 | 114 | | case OBJECT: data = input.UShort; break; |
| | 115 | | // Implicit |
| 4 | 116 | | case INT: data = (int)input.UShort; break; |
| 2 | 117 | | case UINT: data = (uint)input.UShort; break; |
| 2 | 118 | | case LONG: data = (long)input.UShort; break; |
| 2 | 119 | | case ULONG: data = (ulong)input.UShort; break; |
| 2 | 120 | | case FLOAT: data = (float)input.UShort; break; |
| 2 | 121 | | case DOUBLE: data = (double)input.UShort; break; |
| 2 | 122 | | case DECIMAL: data = (decimal)input.UShort; break; |
| | 123 | | // Explicit |
| 2 | 124 | | case SBYTE: data = (sbyte)input.UShort; break; |
| 2 | 125 | | case BYTE: data = (byte)input.UShort; break; |
| 2 | 126 | | case SHORT: data = (short)input.UShort; break; |
| | 127 | | // String |
| 2 | 128 | | case STRING: data = input.UShort.ToString(); break; |
| | 129 | | } |
| 14 | 130 | | break; |
| | 131 | | case TokenType.Int: |
| 30 | 132 | | switch (typeof(T).FullName) |
| | 133 | | { |
| | 134 | | // Self |
| | 135 | | case INT: |
| 34 | 136 | | case OBJECT: data = input.Int; break; |
| | 137 | | // Implicit |
| 2 | 138 | | case LONG: data = (long)input.Int; break; |
| 4 | 139 | | case FLOAT: data = (float)input.Int; break; |
| 2 | 140 | | case DOUBLE: data = (double)input.Int; break; |
| 2 | 141 | | case DECIMAL: data = (decimal)input.Int; break; |
| | 142 | | // Explicit |
| 2 | 143 | | case SBYTE: data = (sbyte)input.Int; break; |
| 2 | 144 | | case BYTE: data = (byte)input.Int; break; |
| 2 | 145 | | case SHORT: data = (short)input.Int; break; |
| 2 | 146 | | case USHORT: data = (ushort)input.Int; break; |
| 2 | 147 | | case UINT: data = (uint)input.Int; break; |
| 2 | 148 | | case ULONG: data = (ulong)input.Int; break; |
| | 149 | | // String |
| 4 | 150 | | case STRING: data = input.Int.ToString(); break; |
| | 151 | | } |
| 30 | 152 | | break; |
| | 153 | | case TokenType.UInt: |
| 14 | 154 | | switch (typeof(T).FullName) |
| | 155 | | { |
| | 156 | | // Self |
| | 157 | | case UINT: |
| 4 | 158 | | case OBJECT: data = input.UInt; break; |
| | 159 | | // Implicit |
| 2 | 160 | | case LONG: data = (long)input.UInt; break; |
| 2 | 161 | | case ULONG: data = (ulong)input.UInt; break; |
| 2 | 162 | | case FLOAT: data = (float)input.UInt; break; |
| 2 | 163 | | case DOUBLE: data = (double)input.UInt; break; |
| 2 | 164 | | case DECIMAL: data = (decimal)input.UInt; break; |
| | 165 | | // Explicit |
| 2 | 166 | | case SBYTE: data = (sbyte)input.UInt; break; |
| 2 | 167 | | case BYTE: data = (byte)input.UInt; break; |
| 2 | 168 | | case SHORT: data = (short)input.UInt; break; |
| 2 | 169 | | case USHORT: data = (ushort)input.UInt; break; |
| 4 | 170 | | case INT: data = (int)input.UInt; break; |
| | 171 | | // String |
| 2 | 172 | | case STRING: data = input.UInt.ToString(); break; |
| | 173 | | } |
| 14 | 174 | | break; |
| | 175 | | case TokenType.Long: |
| 14 | 176 | | switch (typeof(T).FullName) |
| | 177 | | { |
| | 178 | | // Self |
| | 179 | | case LONG: |
| 4 | 180 | | case OBJECT: data = input.Long; break; |
| | 181 | | // Implicit |
| 2 | 182 | | case FLOAT: data = (float)input.Long; break; |
| 2 | 183 | | case DOUBLE: data = (double)input.Long; break; |
| 2 | 184 | | case DECIMAL: data = (decimal)input.Long; break; |
| | 185 | | // Explicit |
| 2 | 186 | | case SBYTE: data = (sbyte)input.Long; break; |
| 2 | 187 | | case BYTE: data = (byte)input.Long; break; |
| 2 | 188 | | case SHORT: data = (short)input.Long; break; |
| 2 | 189 | | case USHORT: data = (ushort)input.Long; break; |
| 4 | 190 | | case INT: data = (int)input.Long; break; |
| 2 | 191 | | case UINT: data = (uint)input.Long; break; |
| 2 | 192 | | case ULONG: data = (ulong)input.Long; break; |
| | 193 | | // String |
| 2 | 194 | | case STRING: data = input.Long.ToString(); break; |
| | 195 | | } |
| 14 | 196 | | break; |
| | 197 | | case TokenType.ULong: |
| 14 | 198 | | switch (typeof(T).FullName) |
| | 199 | | { |
| | 200 | | // Self |
| | 201 | | case ULONG: |
| 4 | 202 | | case OBJECT: data = input.ULong; break; |
| | 203 | | // Implicit |
| 2 | 204 | | case FLOAT: data = (float)input.ULong; break; |
| 2 | 205 | | case DOUBLE: data = (double)input.ULong; break; |
| 2 | 206 | | case DECIMAL: data = (decimal)input.ULong; break; |
| | 207 | | // Explicit |
| 2 | 208 | | case SBYTE: data = (sbyte)input.ULong; break; |
| 2 | 209 | | case BYTE: data = (byte)input.ULong; break; |
| 2 | 210 | | case SHORT: data = (short)input.ULong; break; |
| 2 | 211 | | case USHORT: data = (ushort)input.ULong; break; |
| 4 | 212 | | case INT: data = (int)input.ULong; break; |
| 2 | 213 | | case UINT: data = (uint)input.ULong; break; |
| 2 | 214 | | case LONG: data = (long)input.ULong; break; |
| | 215 | | // String |
| 2 | 216 | | case STRING: data = input.ULong.ToString(); break; |
| | 217 | | } |
| 14 | 218 | | break; |
| | 219 | | case TokenType.Float: |
| 15 | 220 | | switch (typeof(T).FullName) |
| | 221 | | { |
| | 222 | | // Self |
| | 223 | | case FLOAT: |
| 4 | 224 | | case OBJECT: data = input.Float; break; |
| | 225 | | // Implicit |
| 2 | 226 | | case DOUBLE: data = (double)input.Float; break; |
| | 227 | | // Explicit |
| 2 | 228 | | case SBYTE: data = (sbyte)input.Float; break; |
| 2 | 229 | | case BYTE: data = (byte)input.Float; break; |
| 2 | 230 | | case SHORT: data = (short)input.Float; break; |
| 2 | 231 | | case USHORT: data = (ushort)input.Float; break; |
| 6 | 232 | | case INT: data = (int)input.Float; break; |
| 2 | 233 | | case UINT: data = (uint)input.Float; break; |
| 2 | 234 | | case LONG: data = (long)input.Float; break; |
| 2 | 235 | | case ULONG: data = (ulong)input.Float; break; |
| 2 | 236 | | case DECIMAL: data = (decimal)input.Float; break; |
| | 237 | | // String |
| 2 | 238 | | case STRING: data = input.Float.ToString(); break; |
| | 239 | | } |
| 15 | 240 | | break; |
| | 241 | | case TokenType.Double: |
| 29 | 242 | | switch (typeof(T).FullName) |
| | 243 | | { |
| | 244 | | // Self |
| | 245 | | case DOUBLE: |
| 4 | 246 | | case OBJECT: data = input.Double; break; |
| | 247 | | // Explicit |
| 2 | 248 | | case SBYTE: data = (sbyte)input.Double; break; |
| 2 | 249 | | case BYTE: data = (byte)input.Double; break; |
| 2 | 250 | | case SHORT: data = (short)input.Double; break; |
| 2 | 251 | | case USHORT: data = (ushort)input.Double; break; |
| 30 | 252 | | case INT: data = (int)input.Double; break; |
| 2 | 253 | | case UINT: data = (uint)input.Double; break; |
| 2 | 254 | | case LONG: data = (long)input.Double; break; |
| 2 | 255 | | case ULONG: data = (ulong)input.Double; break; |
| 2 | 256 | | case FLOAT: data = (float)input.Double; break; |
| 2 | 257 | | case DECIMAL: data = (decimal)input.Double; break; |
| | 258 | | // String |
| 2 | 259 | | case STRING: data = input.Double.ToString(); break; |
| | 260 | | } |
| 29 | 261 | | break; |
| | 262 | | case TokenType.String: |
| 119 | 263 | | switch (typeof(T).FullName) |
| | 264 | | { |
| | 265 | | // Self |
| | 266 | | case STRING: |
| 210 | 267 | | case OBJECT: data = input.String; break; |
| | 268 | | // Parsed |
| 5 | 269 | | case BOOL: if (bool.TryParse(input.String, out bool boolP)) { data = boolP; } break; |
| 5 | 270 | | case SBYTE: if (sbyte.TryParse(input.String, out sbyte sbyteP)) { data = sbyteP; } break; |
| 5 | 271 | | case BYTE: if (byte.TryParse(input.String, out byte byteP)) { data = byteP; } break; |
| 5 | 272 | | case SHORT: if (short.TryParse(input.String, out short shortP)) { data = shortP; } break; |
| 5 | 273 | | case USHORT: if (ushort.TryParse(input.String, out ushort ushortP)) { data = ushortP; } break; |
| 12 | 274 | | case INT: if (int.TryParse(input.String, out int intP)) { data = intP; } break; |
| 5 | 275 | | case UINT: if (uint.TryParse(input.String, out uint uintP)) { data = uintP; } break; |
| 5 | 276 | | case LONG: if (long.TryParse(input.String, out long longP)) { data = longP; } break; |
| 5 | 277 | | case ULONG: if (ulong.TryParse(input.String, out ulong ulongP)) { data = ulongP; } break; |
| 5 | 278 | | case FLOAT: if (float.TryParse(input.String, out float floatP)) { data = floatP; } break; |
| 5 | 279 | | case DOUBLE: if (double.TryParse(input.String, out double doubleP)) { data = doubleP; } break; |
| 5 | 280 | | case DECIMAL: if (decimal.TryParse(input.String, out decimal decimalP)) { data = decimalP; } bre |
| | 281 | | } |
| 119 | 282 | | break; |
| | 283 | | case TokenType.DataList: |
| 4 | 284 | | switch (typeof(T).FullName) |
| | 285 | | { |
| | 286 | | // Self |
| | 287 | | case DATA_LIST: |
| 4 | 288 | | case OBJECT: data = input.DataList; break; |
| | 289 | | } |
| 4 | 290 | | break; |
| | 291 | | case TokenType.DataDictionary: |
| 69 | 292 | | switch (typeof(T).FullName) |
| | 293 | | { |
| | 294 | | // Self |
| | 295 | | case DATA_DICTIONARY: |
| 136 | 296 | | case OBJECT: data = input.DataDictionary; break; |
| | 297 | | } |
| 69 | 298 | | break; |
| | 299 | | case TokenType.Reference: |
| | 300 | | // Self |
| 2 | 301 | | switch (typeof(T).FullName) |
| | 302 | | { |
| | 303 | | // Self |
| 2 | 304 | | case OBJECT: data = input.Reference; break; |
| | 305 | | } |
| 2 | 306 | | break; |
| | 307 | | } |
| | 308 | |
|
| 370 | 309 | | if (data == null) |
| 8 | 310 | | { |
| 8 | 311 | | result = default; |
| 8 | 312 | | return false; |
| | 313 | | } |
| | 314 | |
|
| 362 | 315 | | result = (T)data; |
| 362 | 316 | | return true; |
| 370 | 317 | | } |
| | 318 | | } |
| | 319 | | } |