-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValuePacket.Read.cs
501 lines (455 loc) · 16.5 KB
/
ValuePacket.Read.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Collections.Generic;
using System.Text;
namespace Zenvin.Settings.Framework.Serialization {
public partial class ValuePacket {
/// <summary>
/// Tries to read an <see cref="sbyte"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not an <see cref="sbyte"/>.
/// </summary>
public bool TryRead (string key, out sbyte value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (sbyte)) {
value = (sbyte)raw[0];
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="byte"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="byte"/>.
/// </summary>
public bool TryRead (string key, out byte value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (byte)) {
value = raw[0];
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="short"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="short"/>.
/// </summary>
public bool TryRead (string key, out short value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (short)) {
value = BitConverter.ToInt16 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read an <see cref="ushort"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not an <see cref="ushort"/>.
/// </summary>
public bool TryRead (string key, out ushort value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (ushort)) {
value = BitConverter.ToUInt16 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read an <see cref="int"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not an <see cref="int"/>.
/// </summary>
public bool TryRead (string key, out int value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (int)) {
value = BitConverter.ToInt32 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read an <see cref="uint"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not an <see cref="uint"/>.
/// </summary>
public bool TryRead (string key, out uint value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (uint)) {
value = BitConverter.ToUInt32 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="long"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="long"/>.
/// </summary>
public bool TryRead (string key, out long value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (long)) {
value = BitConverter.ToInt64 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read an <see cref="ulong"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not an <see cref="ulong"/>.
/// </summary>
public bool TryRead (string key, out ulong value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (ulong)) {
value = BitConverter.ToUInt64 (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="float"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="float"/>.
/// </summary>
public bool TryRead (string key, out float value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (float)) {
value = BitConverter.ToSingle (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="double"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="double"/>.
/// </summary>
public bool TryRead (string key, out double value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (double)) {
value = BitConverter.ToDouble (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="bool"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="bool"/>.
/// </summary>
public bool TryRead (string key, out bool value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (bool)) {
value = BitConverter.ToBoolean (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="string"/> value with the given key and <see cref="Encoding"/>.<br></br>
/// If no encoding is given, <see cref="Encoding.UTF8"/> will be used.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was empty.
/// </summary>
public bool TryRead (string key, out string value, Encoding encoding = null) {
encoding = encoding ?? Encoding.UTF8;
if (TryRead (key, out byte[] raw) && raw != null && raw.Length > 0) {
value = encoding.GetString (raw);
return true;
}
value = "";
return false;
}
/// <summary>
/// Tries to read a <see cref="char"/> value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found, or the value associated with the key was not a <see cref="char"/>.
/// </summary>
public bool TryRead (string key, out char value) {
if (TryRead (key, out byte[] raw) && raw != null && raw.Length == sizeof (char)) {
value = BitConverter.ToChar (raw, 0);
return true;
}
value = default;
return false;
}
/// <summary>
/// Tries to read a <see cref="byte"/>[] value with the given key.<br></br>
/// Returns <see langword="false"/> if the key was not found.<br></br>
/// Returned value may be <see langword="null"/>.
/// </summary>
public bool TryRead (string key, out byte[] value) {
return data.TryGetValue (key, out value);
}
/// <summary>
/// Reads an <see cref="sbyte"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public sbyte ReadSByte (string key) {
if (TryRead (key, out sbyte value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read SByte value '{key}'");
}
/// <summary>
/// Reads a <see cref="byte"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public byte ReadByte (string key) {
if (TryRead (key, out byte value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Byte value '{key}'");
}
/// <summary>
/// Reads a <see cref="short"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public short ReadInt16 (string key) {
if (TryRead (key, out short value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Int16 value '{key}'");
}
/// <summary>
/// Reads an <see cref="ushort"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public ushort ReadUInt16 (string key) {
if (TryRead (key, out ushort value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read UInt16 value '{key}'");
}
/// <summary>
/// Reads an <see cref="int"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public int ReadInt32 (string key) {
if (TryRead (key, out int value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Int32 value '{key}'");
}
/// <summary>
/// Reads an <see cref="uint"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public uint ReadUInt32 (string key) {
if (TryRead (key, out uint value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read UInt32 value '{key}'");
}
/// <summary>
/// Reads a <see cref="long"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public long ReadInt64 (string key) {
if (TryRead (key, out long value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Int64 value '{key}'");
}
/// <summary>
/// Reads an <see cref="ulong"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public ulong ReadUInt64 (string key) {
if (TryRead (key, out ulong value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read UInt64 value '{key}'");
}
/// <summary>
/// Reads a <see cref="float"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public float ReadSingle (string key) {
if (TryRead (key, out float value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Single value '{key}'");
}
/// <summary>
/// Reads a <see cref="double"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public double ReadDouble (string key) {
if (TryRead (key, out double value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Double value '{key}'");
}
/// <summary>
/// Reads a <see cref="bool"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public bool ReadBoolean (string key) {
if (TryRead (key, out bool value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Boolean value '{key}'");
}
/// <summary>
/// Reads a <see cref="char"/> value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public char ReadChar (string key) {
if (TryRead (key, out char value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Char value '{key}'");
}
/// <summary>
/// Reads a <see cref="string"/> value with the given key and <see cref="Encoding"/>.<br></br>
/// If no encoding is given, <see cref="Encoding.UTF8"/> will be used.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public string ReadString (string key, Encoding encoding = null) {
if (TryRead (key, out string value, encoding)) {
return value;
}
throw new KeyNotFoundException ($"Could not read String value '{key}'");
}
/// <summary>
/// Reads a <see cref="byte"/>[] value with the given key.
/// </summary>
/// <exception cref="KeyNotFoundException"/>
public byte[] ReadBytes (string key) {
if (TryRead (key, out byte[] value)) {
return value;
}
throw new KeyNotFoundException ($"Could not read Byte[] value '{key}'");
}
/// <summary>
/// Reads an <see cref="sbyte"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="sbyte"/>.
/// </summary>
public sbyte Read (string key, sbyte fallback) {
if (TryRead (key, out sbyte value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="byte"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="byte"/>.
/// </summary>
public byte Read (string key, byte fallback) {
if (TryRead (key, out byte value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="short"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="short"/>.
/// </summary>
public short Read (string key, short fallback) {
if (TryRead (key, out short value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="ushort"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="ushort"/>.
/// </summary>
public ushort Read (string key, ushort fallback) {
if (TryRead (key, out ushort value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="int"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="int"/>.
/// </summary>
public int Read (string key, int fallback) {
if (TryRead (key, out int value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="uint"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="uint"/>.
/// </summary>
public uint Read (string key, uint fallback) {
if (TryRead (key, out uint value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="long"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="long"/>.
/// </summary>
public long Read (string key, long fallback) {
if (TryRead (key, out long value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="ulong"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="ulong"/>.
/// </summary>
public ulong Read (string key, ulong fallback) {
if (TryRead (key, out ulong value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="float"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not an <see cref="float"/>.
/// </summary>
public float Read (string key, float fallback) {
if (TryRead (key, out float value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="double"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="double"/>.
/// </summary>
public double Read (string key, double fallback) {
if (TryRead (key, out double value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="bool"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="bool"/>.
/// </summary>
public bool Read (string key, bool fallback) {
if (TryRead (key, out bool value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="char"/> value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="char"/>.
/// </summary>
public char Read (string key, char fallback) {
if (TryRead (key, out char value)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads an <see cref="sbyte"/> value with the given key and <see cref="Encoding"/>.<br></br>
/// If no encoding is given, <see cref="Encoding.UTF8"/> will be used.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was empty.
/// </summary>
public string Read (string key, string fallback, Encoding encoding = null) {
if (TryRead (key, out string value, encoding)) {
return value;
}
return fallback;
}
/// <summary>
/// Reads a <see cref="byte"/>[] value with the given key.<br></br>
/// Returns <paramref name="fallback"/>, if the key was not found or the associated value was not a <see cref="byte"/>[].
/// </summary>
public byte[] Read (string key, byte[] fallback) {
if (TryRead (key, out byte[] value)) {
return value;
}
return fallback;
}
}
}