-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageParser.cs
More file actions
704 lines (568 loc) · 34 KB
/
MessageParser.cs
File metadata and controls
704 lines (568 loc) · 34 KB
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
using Optional;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace ToSParser
{
public delegate ref int BufferIndex();
public interface UnknownParser<This>
{
int GetLimit();
This Copy(byte[] buffer, BufferIndex index, int size);
}
public interface UnknownWriter<This>
{
This Copy(byte[] buffer, BufferIndex index);
}
public interface Parser<Type, Parent> : UnknownParser<Parser<Type, Parent>> where Parent : UnknownParser<Parent>
{
Parent Parse(out Type value, int maxLimit);
Parent Parse(out Type value);
Type Parse(Action<Parent> action);
O Parse<I, O>(Func<Type, I, O> merge, Func<Parent, I> parser);
void SetMaxLimit(int limit);
}
public interface Writer<Type, Parent> : UnknownWriter<Writer<Type, Parent>> where Parent : UnknownWriter<Parent>
{
Parent Parse(Type value);
}
public interface RepeatParser<Repeat, Parent> : UnknownParser<RepeatParser<Repeat, Parent>> where Parent : UnknownParser<Parent> where Repeat : UnknownParser<Repeat>
{
Repeat GetRepeat();
Parent GetParent();
Parent Parse<T>(Func<Repeat, T> parser, out IEnumerable<T> values);
IEnumerable<T> Parse<T>(Func<Repeat, T> parser, Action<Parent> action);
}
public interface RepeatWriter<Repeat, Parent> : UnknownWriter<RepeatWriter<Repeat, Parent>> where Parent : UnknownWriter<Parent> where Repeat : UnknownWriter<Repeat>
{
Repeat GetRepeat();
Parent GetParent();
Parent Parse<T>(IEnumerable<T> values, Func<T, Repeat, RootWriter> writer);
}
public class RootParser : UnknownParser<RootParser>
{
private byte[] buffer;
private BufferIndex index;
private int size;
private Option<byte> delimiter;
internal RootParser() { }
internal RootParser(byte delimiter) => this.delimiter = delimiter.Some();
private RootParser(byte[] buffer, BufferIndex index, int size, Option<byte> delimiter)
{
this.buffer = buffer;
this.index = index;
this.size = size;
this.delimiter = delimiter;
}
public int GetLimit() => delimiter.Map(d => GetLimit(d)).ValueOr(size);
public int CheckPadding()
{
if (delimiter.HasValue && size > index() && buffer[index()++] != delimiter.ValueOr(0)) throw new ArgumentException("illegal byte");
return index();
}
private int GetLimit(byte delimiter)
{
for (int offset = index(); offset < size; offset++) if (buffer[offset] == delimiter) return offset;
return size;
}
public RootParser Copy(byte[] buffer, BufferIndex index, int size) => new RootParser(buffer, index, size, delimiter);
}
public class RootWriter : UnknownWriter<RootWriter>
{
private byte[] buffer;
private BufferIndex index;
private Option<byte> delimiter;
internal RootWriter() { }
internal RootWriter(byte delimiter) => this.delimiter = delimiter.Some();
private RootWriter(byte[] buffer, BufferIndex index, Option<byte> delimiter)
{
this.buffer = buffer;
this.index = index;
this.delimiter = delimiter;
}
public int AddPadding()
{
if (delimiter.HasValue) buffer[index()++] = delimiter.ValueOr(0);
return index();
}
public RootWriter Copy(byte[] buffer, BufferIndex index) => new RootWriter(buffer, index, delimiter);
}
public class ConditionalParser<Parent1, Parent2> : UnknownParser<ConditionalParser<Parent1, Parent2>> where Parent1 : UnknownParser<Parent1> where Parent2 : UnknownParser<Parent2>
{
private byte[] buffer;
private BufferIndex index;
private int size;
private Parent1 parent1;
private Parent2 parent2;
internal ConditionalParser(Parent1 parent1, Parent2 parent2)
{
this.parent1 = parent1;
this.parent2 = parent2;
}
private ConditionalParser(byte[] buffer, BufferIndex index, int size, Parent1 parent1, Parent2 parent2) : this(parent1, parent2)
{
this.buffer = buffer;
this.index = index;
this.size = size;
}
public int GetLimit() => parent1.GetLimit();
public Parent1 GetTrue() => parent1;
public Parent2 GetFalse() => parent2;
public RootParser Parse(bool condition, Func<Parent1, RootParser> ifTrue, Func<Parent2, RootParser> ifFalse) => condition ? ifTrue(parent1) : ifFalse(parent2);
public ConditionalParser<Parent1, Parent2> Copy(byte[] buffer, BufferIndex index, int size) => new ConditionalParser<Parent1, Parent2>(buffer, index, size, parent1.Copy(buffer, index, size), parent2.Copy(buffer, index, size));
}
public class ConditionalWriter<Parent1, Parent2> : UnknownWriter<ConditionalWriter<Parent1, Parent2>> where Parent1 : UnknownWriter<Parent1> where Parent2 : UnknownWriter<Parent2>
{
private byte[] buffer;
private BufferIndex index;
private Parent1 parent1;
private Parent2 parent2;
internal ConditionalWriter(Parent1 parent1, Parent2 parent2)
{
this.parent1 = parent1;
this.parent2 = parent2;
}
private ConditionalWriter(byte[] buffer, BufferIndex index, Parent1 parent1, Parent2 parent2) : this(parent1, parent2)
{
this.buffer = buffer;
this.index = index;
}
public RootWriter Parse(bool condition, Func<Parent1, RootWriter> ifTrue, Func<Parent2, RootWriter> ifFalse) => condition ? ifTrue(parent1) : ifFalse(parent2);
public ConditionalWriter<Parent1, Parent2> Copy(byte[] buffer, BufferIndex index) => new ConditionalWriter<Parent1, Parent2>(buffer, index, parent1.Copy(buffer, index), parent2.Copy(buffer, index));
}
public interface ParserBuilder<This1, This2> where This1 : UnknownParser<This1> where This2 : UnknownWriter<This2>
{
ParserBuilder<Parser<R, This1>, Writer<R, This2>> After<R>(Converter<R> converter);
ParserBuilder<Parser<R, This1>, Writer<R, This2>> After<R>(Converter<R> converter, byte delimiter);
ParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>> Repeat<P, W>(int clip, ParserBuilder<P, W> repeat) where P : UnknownParser<P> where W : UnknownWriter<W>;
ParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>> Repeat<P, W>(int clip, ParserBuilder<P, W> repeat, byte delimiter) where P : UnknownParser<P> where W : UnknownWriter<W>;
ParserBuilder<ConditionalParser<This1, P>, ConditionalWriter<This2, W>> Condition<P, W>(ParserBuilder<P, W> ifFalse) where P : UnknownParser<P> where W : UnknownWriter<W>;
This1 Build(byte[] buffer, int index, int size);
This2 Build(byte[] buffer, int index);
}
public class RootBuilder : BaseParserBuilder<RootParser, RootWriter>
{
public RootBuilder() : base(new RootParser(), new RootWriter()) { }
public RootBuilder(byte delimiter) : base(new RootParser(delimiter), new RootWriter(delimiter)) { }
}
public class BaseParserBuilder<This1, This2> : ParserBuilder<This1, This2> where This1 : UnknownParser<This1> where This2 : UnknownWriter<This2>
{
private This1 parser;
private This2 writer;
internal BaseParserBuilder(This1 parser, This2 writer)
{
this.parser = parser;
this.writer = writer;
}
public ParserBuilder<Parser<R, This1>, Writer<R, This2>> After<R>(Converter<R> converter) => new BaseParserBuilder<Parser<R, This1>, Writer<R, This2>>(new BaseParser<R, This1>(parser, converter), new BaseWriter<R, This2>(writer, converter));
public ParserBuilder<Parser<R, This1>, Writer<R, This2>> After<R>(Converter<R> converter, byte delimiter) => new BaseParserBuilder<Parser<R, This1>, Writer<R, This2>>(new DelimitedParser<R, This1>(parser, converter, delimiter), new DelimitedWriter<R, This2>(writer, converter, delimiter));
public ParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>> Repeat<P, W>(int clip, ParserBuilder<P, W> repeat) where P : UnknownParser<P> where W : UnknownWriter<W> => new BaseParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>>(new BaseRepeatParser<P, This1>(parser, clip, repeat.Build(null, 0, 0)), new BaseRepeatWriter<W, This2>(writer, clip, repeat.Build(null, 0)));
public ParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>> Repeat<P, W>(int clip, ParserBuilder<P, W> repeat, byte delimiter) where P : UnknownParser<P> where W : UnknownWriter<W> => new BaseParserBuilder<RepeatParser<P, This1>, RepeatWriter<W, This2>>(new DelimitedRepeatParser<P, This1>(parser, clip, repeat.Build(null, 0, 0), delimiter), new DelimitedRepeatWriter<W, This2>(writer, clip, repeat.Build(null, 0), delimiter));
public ParserBuilder<ConditionalParser<This1, P>, ConditionalWriter<This2, W>> Condition<P, W>(ParserBuilder<P, W> ifFalse) where P : UnknownParser<P> where W : UnknownWriter<W> => new BaseParserBuilder<ConditionalParser<This1, P>, ConditionalWriter<This2, W>>(new ConditionalParser<This1, P>(parser, ifFalse.Build(null, 0, 0)), new ConditionalWriter<This2, W>(writer, ifFalse.Build(null, 0)));
public This1 Build(byte[] buffer, int index, int size) => parser.Copy(buffer, new IndexContainer(index).Index, size);
public This2 Build(byte[] buffer, int index) => writer.Copy(buffer, new IndexContainer(index).Index);
private class IndexContainer
{
private int index;
public IndexContainer(int index) => this.index = index;
public ref int Index() => ref index;
}
}
internal class BaseParser<Type, Parent> : Parser<Type, Parent> where Parent : UnknownParser<Parent>
{
protected byte[] buffer;
protected BufferIndex index;
protected int size;
protected Parent parent;
protected Converter<Type> converter;
internal BaseParser(Parent parent, Converter<Type> converter)
{
this.parent = parent;
this.converter = converter;
}
protected BaseParser(byte[] buffer, BufferIndex index, int size, Parent parent, Converter<Type> converter) : this(parent, converter)
{
this.buffer = buffer;
this.index = index;
this.size = size;
}
public virtual int GetLimit() => parent.GetLimit();
public virtual Parent Parse(out Type value, int maxLimit)
{
SetMaxLimit(maxLimit);
return Parse(out value);
}
public virtual Parent Parse(out Type value)
{
int index2 = index();
while (index2 <= size)
{
try
{
index2 = Math.Min(parent.GetLimit(), size);
int copy = index();
value = converter.FromBytes(buffer, ref copy, index2++ - copy);
index() = copy;
return parent;
}
catch (ArgumentException)
{
index2++;
}
}
throw new ArgumentException("insufficient data");
}
public Type Parse(Action<Parent> action)
{
action(Parse(out Type result));
return result;
}
public O Parse<I, O>(Func<Type, I, O> merge, Func<Parent, I> parser)
{
I r2 = parser(Parse(out Type r1));
return merge(r1, r2);
}
public virtual Parser<Type, Parent> Copy(byte[] buffer, BufferIndex index, int size) => new BaseParser<Type, Parent>(buffer, index, size, parent.Copy(buffer, index, size), converter);
public void SetMaxLimit(int limit) => size = limit;
}
internal class BaseWriter<Type, Parent> : Writer<Type, Parent> where Parent : UnknownWriter<Parent>
{
protected byte[] buffer;
protected BufferIndex index;
protected Parent parent;
protected Converter<Type> converter;
internal BaseWriter(Parent parent, Converter<Type> converter)
{
this.parent = parent;
this.converter = converter;
}
protected BaseWriter(byte[] buffer, BufferIndex index, Parent parent, Converter<Type> converter) : this(parent, converter)
{
this.buffer = buffer;
this.index = index;
}
public virtual Parent Parse(Type value)
{
converter.ToBytes(buffer, ref index(), value);
return parent;
}
public virtual Writer<Type, Parent> Copy(byte[] buffer, BufferIndex index) => new BaseWriter<Type, Parent>(buffer, index, parent.Copy(buffer, index), converter);
}
internal class DelimitedParser<Type, Parent> : BaseParser<Type, Parent> where Parent : UnknownParser<Parent>
{
private byte delimiter;
internal DelimitedParser(Parent parent, Converter<Type> converter, byte delimiter) : base(parent, converter) => this.delimiter = delimiter;
private DelimitedParser(byte[] buffer, BufferIndex index, int size, Parent parent, Converter<Type> converter, byte delimiter) : base(buffer, index, size, parent, converter) => this.delimiter = delimiter;
public override int GetLimit()
{
for (int offset = index(); offset < size; offset++) if (buffer[offset] == delimiter) return offset;
return size;
}
public override Parent Parse(out Type value)
{
if (size > index() && buffer[index()++] != delimiter) throw new ArgumentException("illegal byte");
return base.Parse(out value);
}
public override Parser<Type, Parent> Copy(byte[] buffer, BufferIndex index, int size) => new DelimitedParser<Type, Parent>(buffer, index, size, parent.Copy(buffer, index, size), converter, delimiter);
}
internal class DelimitedWriter<Type, Parent> : BaseWriter<Type, Parent> where Parent : UnknownWriter<Parent>
{
private byte delimiter;
internal DelimitedWriter(Parent parent, Converter<Type> converter, byte delimiter) : base(parent, converter) => this.delimiter = delimiter;
private DelimitedWriter(byte[] buffer, BufferIndex index, Parent parent, Converter<Type> converter, byte delimiter) : base(buffer, index, parent, converter) => this.delimiter = delimiter;
public override Parent Parse(Type value)
{
buffer[index()++] = delimiter;
return base.Parse(value);
}
public override Writer<Type, Parent> Copy(byte[] buffer, BufferIndex index) => new DelimitedWriter<Type, Parent>(buffer, index, parent.Copy(buffer, index), converter, delimiter);
}
internal class BaseRepeatParser<Repeat, Parent> : RepeatParser<Repeat, Parent> where Parent : UnknownParser<Parent> where Repeat : UnknownParser<Repeat>
{
protected byte[] buffer;
protected BufferIndex index;
protected int size;
protected Parent parent;
protected int clip;
protected Repeat repeat;
internal BaseRepeatParser(Parent parent, int clip, Repeat repeat)
{
this.parent = parent;
this.clip = clip;
this.repeat = repeat;
}
protected BaseRepeatParser(byte[] buffer, BufferIndex index, int size, Parent parent, int clip, Repeat repeat) : this(parent, clip, repeat)
{
this.buffer = buffer;
this.index = index;
this.size = size;
}
public virtual int GetLimit() => parent.GetLimit();
public Repeat GetRepeat() => repeat;
public Parent GetParent() => parent;
public virtual Parent Parse<T>(Func<Repeat, T> action, out IEnumerable<T> values)
{
int limit = index();
int copy = index();
List<T> result = new List<T>();
while (limit <= size)
{
limit = parent.GetLimit();
index() = copy;
while (index() < limit)
{
try
{
result.Add(action(repeat));
}
catch (Exception)
{
result.Clear();
continue;
}
}
values = result;
return parent;
}
throw new ArgumentException("insufficient data");
}
public IEnumerable<T> Parse<T>(Func<Repeat, T> parser, Action<Parent> action)
{
action(Parse(parser, out IEnumerable<T> values));
return values;
}
public virtual RepeatParser<Repeat, Parent> Copy(byte[] buffer, BufferIndex index, int size) => new BaseRepeatParser<Repeat, Parent>(buffer, index, size, parent.Copy(buffer, index, size), clip, repeat.Copy(buffer, index, size));
}
internal class BaseRepeatWriter<Repeat, Parent> : RepeatWriter<Repeat, Parent> where Parent : UnknownWriter<Parent> where Repeat : UnknownWriter<Repeat>
{
protected byte[] buffer;
protected BufferIndex index;
protected Parent parent;
protected int clip;
protected Repeat repeat;
public BaseRepeatWriter(Parent parent, int clip, Repeat repeat)
{
this.parent = parent;
this.clip = clip;
this.repeat = repeat;
}
protected BaseRepeatWriter(byte[] buffer, BufferIndex index, Parent parent, int clip, Repeat repeat) : this(parent, clip, repeat)
{
this.buffer = buffer;
this.index = index;
}
public Parent GetParent() => parent;
public Repeat GetRepeat() => repeat;
public virtual Parent Parse<T>(IEnumerable<T> values, Func<T, Repeat, RootWriter> writer)
{
int start = index();
foreach (T value in values) writer(value, repeat).AddPadding();
if (index() > start) index() -= clip;
return parent;
}
public virtual RepeatWriter<Repeat, Parent> Copy(byte[] buffer, BufferIndex index) => new BaseRepeatWriter<Repeat, Parent>(buffer, index, parent.Copy(buffer, index), clip, repeat.Copy(buffer, index));
}
internal class DelimitedRepeatParser<Repeat, Parent> : BaseRepeatParser<Repeat, Parent> where Parent : UnknownParser<Parent> where Repeat : UnknownParser<Repeat>
{
private byte delimiter;
internal DelimitedRepeatParser(Parent parent, int clip, Repeat repeat, byte delimiter) : base(parent, clip, repeat) => this.delimiter = delimiter;
private DelimitedRepeatParser(byte[] buffer, BufferIndex index, int size, Parent parent, int clip, Repeat repeat, byte delimiter) : base(buffer, index, size, parent, clip, repeat) => this.delimiter = delimiter;
public override int GetLimit()
{
for (int offset = index(); offset < size; offset++) if (buffer[offset] == delimiter) return offset;
return size;
}
public override Parent Parse<T>(Func<Repeat, T> action, out IEnumerable<T> values)
{
if (index() < size && buffer[index()++] != delimiter) throw new ArgumentException("illegal byte");
return base.Parse(action, out values);
}
public override RepeatParser<Repeat, Parent> Copy(byte[] buffer, BufferIndex index, int size) => new DelimitedRepeatParser<Repeat, Parent>(buffer, index, size, parent.Copy(buffer, index, size), clip, repeat.Copy(buffer, index, size), delimiter);
}
internal class DelimitedRepeatWriter<Repeat, Parent> : BaseRepeatWriter<Repeat, Parent> where Parent : UnknownWriter<Parent> where Repeat : UnknownWriter<Repeat>
{
private byte delimiter;
internal DelimitedRepeatWriter(Parent parent, int clip, Repeat repeat, byte delimiter) : base(parent, clip, repeat) => this.delimiter = delimiter;
private DelimitedRepeatWriter(byte[] buffer, BufferIndex index, Parent parent, int clip, Repeat repeat, byte delimiter) : base(buffer, index, parent, clip, repeat) => this.delimiter = delimiter;
public override Parent Parse<T>(IEnumerable<T> values, Func<T, Repeat, RootWriter> writer)
{
buffer[index()++] = delimiter;
return base.Parse(values, writer);
}
public override RepeatWriter<Repeat, Parent> Copy(byte[] buffer, BufferIndex index) => new DelimitedRepeatWriter<Repeat, Parent>(buffer, index, parent.Copy(buffer, index), clip, repeat.Copy(buffer, index), delimiter);
}
internal static class Parsers
{
internal static readonly RootBuilder ROOT = new RootBuilder();
internal static readonly RootBuilder ROOT_ASTERISK = new RootBuilder(0x2A);
internal static readonly RootBuilder ROOT_COMMA = new RootBuilder(0x2C);
internal static readonly ParserBuilder<Parser<string, RootParser>, Writer<string, RootWriter>> STRING = ROOT.After(Converters.STRING);
internal static readonly ParserBuilder<Parser<uint, RootParser>, Writer<uint, RootWriter>> UINT = ROOT.After(Converters.UInt<uint>());
internal static readonly ParserBuilder<Parser<byte, RootParser>, Writer<byte, RootWriter>> BYTE = ROOT.After(Converters.Byte<byte>());
internal static readonly ParserBuilder<Parser<bool, RootParser>, Writer<bool, RootWriter>> BOOLEAN = ROOT.After(Converters.BOOLEAN1);
internal static readonly ParserBuilder<Parser<Brand, RootParser>, Writer<Brand, RootWriter>> BRAND = ROOT.After(Converters.Byte<Brand>());
internal static readonly ParserBuilder<Parser<GameMode, RootParser>, Writer<GameMode, RootWriter>> GAMEMODE = ROOT.After(Converters.Byte<GameMode>());
internal static readonly ParserBuilder<Parser<Catalog, RootParser>, Writer<Catalog, RootWriter>> CATALOG = ROOT.After(Converters.Byte<Catalog>());
internal static readonly ParserBuilder<Parser<Player, RootParser>, Writer<Player, RootWriter>> PLAYER = ROOT.After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Role, RootParser>, Writer<Role, RootWriter>> ROLE = ROOT.After(Converters.Byte<Role>());
internal static readonly ParserBuilder<Parser<LocalizationTable, RootParser>, Writer<LocalizationTable, RootWriter>> LOCALIZATION = ROOT.After(Converters.Byte<LocalizationTable>());
internal static readonly ParserBuilder<Parser<byte, Parser<byte, RootParser>>, Writer<byte, Writer<byte, RootWriter>>> BYTE2 = BYTE.After(Converters.Byte<byte>());
internal static readonly ParserBuilder<Parser<Player, Parser<string, RootParser>>, Writer<Player, Writer<string, RootWriter>>> PLAYER_STRING = STRING.After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Player, Parser<byte, RootParser>>, Writer<Player, Writer<byte, RootWriter>>> PLAYER_RAWBYTE = ROOT.After(Converters.Byte<byte>(0u)).After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Player, Parser<Player, RootParser>>, Writer<Player, Writer<Player, RootWriter>>> PLAYER2 = PLAYER.After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Player, Parser<Role, RootParser>>, Writer<Player, Writer<Role, RootWriter>>> PLAYER_ROLE = ROLE.After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Player, Parser<Player, Parser<byte, RootParser>>>, Writer<Player, Writer<Player, Writer<byte, RootWriter>>>> PLAYER2_RAWBYTE = PLAYER_RAWBYTE.After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<Player, Parser<TauntTargetType, Parser<Taunt, RootParser>>>, Writer<Player, Writer<TauntTargetType, Writer<Taunt, RootWriter>>>> PLAYER_TYPE_TAUNT = ROOT.After(Converters.Byte<Taunt>()).After(Converters.Byte<TauntTargetType>()).After(Converters.Byte<Player>());
internal static readonly ParserBuilder<Parser<string, Parser<string, RootParser>>, Writer<string, Writer<string, RootWriter>>> STRING2_ASTERISK = ROOT.After(Converters.STRING, 0x2A).After(Converters.STRING);
internal static readonly ParserBuilder<Parser<string, Parser<uint, RootParser>>, Writer<string, Writer<uint, RootWriter>>> STRING_UINT_ASTERISK = ROOT.After(Converters.UInt<uint>(), 0x2A).After(Converters.STRING);
internal static readonly ParserBuilder<Parser<uint, Parser<OnlineStatus, Parser<bool, RootParser>>>, Writer<uint, Writer<OnlineStatus, Writer<bool, RootWriter>>>> UINT_ONLINE_BOOLEAN_COMMA = ROOT.After(Converters.BOOLEAN2, 0x2A).After(Converters.Byte<OnlineStatus>(), 0x2A).After(Converters.UInt<uint>());
internal static readonly ParserBuilder<Parser<string, Parser<uint, Parser<OnlineStatus, Parser<bool, RootParser>>>>, Writer<string, Writer<uint, Writer<OnlineStatus, Writer<bool, RootWriter>>>>> STRING_UINT_ONLINE_BOOLEAN_COMMA = new RootBuilder(0x2A).After(Converters.BOOLEAN2, 0x2C).After(Converters.Byte<OnlineStatus>(), 0x2C).After(Converters.UInt<uint>(), 0x2C).After(Converters.STRING);
internal static readonly ParserBuilder<Parser<uint, Parser<uint, Parser<uint, Parser<uint, Parser<uint, RootParser>>>>>, Writer<uint, Writer<uint, Writer<uint, Writer<uint, Writer<uint, RootWriter>>>>>> UINT5_COMMA = new RootBuilder(0x2C).After(Converters.UInt<uint>(), 0x2C).After(Converters.UInt<uint>(), 0x2C).After(Converters.UInt<uint>(), 0x2C).After(Converters.UInt<uint>(), 0x2C).After(Converters.UInt<uint>());
internal static readonly ParserBuilder<RepeatParser<Parser<Player, RootParser>, RootParser>, RepeatWriter<Writer<Player, RootWriter>, RootWriter>> REPEAT_PLAYER = ROOT.Repeat(0, PLAYER);
internal static readonly ParserBuilder<RepeatParser<Parser<Achievement, RootParser>, RootParser>, RepeatWriter<Writer<Achievement, RootWriter>, RootWriter>> REPEAT_ACHIEVEMENT = ROOT.Repeat(1, ROOT_COMMA.After(Converters.UInt<Achievement>()));
internal static readonly ParserBuilder<RepeatParser<Parser<Player, Parser<Role, RootParser>>, RootParser>, RepeatWriter<Writer<Player, Writer<Role, RootWriter>>, RootWriter>> REPEAT_PLAYER_ROLE = ROOT.Repeat(0, PLAYER_ROLE);
}
public static class Converters
{
public static readonly Converter<string> STRING = new ConverterImpl<string>(ToString, FromString);
public static readonly Converter<Uri> URI = new ConverterImpl<Uri>(ToURI, FromURI);
public static readonly Converter<bool> BOOLEAN1 = Boolean(Byte<byte>());
public static readonly Converter<bool> BOOLEAN2 = Boolean(UInt<uint>());
public static readonly Converter<double> DOUBLE = new ConverterImpl<double>(ToDouble, FromDouble);
public static Converter<T> Default<T>() => new ConverterImpl<T>(ToDefault<T>, FromDefault);
public static Converter<T> UInt<T>() where T : IConvertible => new ConverterImpl<T>(ToUInt<T>, FromUInt);
public static Converter<T> Byte<T>(uint offset = 1u) where T : IConvertible => new ConverterImpl<T>(ToByte<T>(offset), FromByte<T>(offset));
public static Converter<Option<R>> Optional<R>(Converter<R> converter) => new ConverterImpl<Option<R>>((byte[] buffer, ref int index, int count) => { try { return converter.FromBytes(buffer, ref index, count).SomeNotNull(); } catch (Exception) { return Option.None<R>(); } }, (byte[] buffer, ref int index, Option<R> value) => { if (value.HasValue) converter.ToBytes(buffer, ref index, value.ValueOr(default(R))); });
public static Converter<bool> Optional(byte value) => new ConverterImpl<bool>(ToOptional(value), FromOptional(value));
public static Converter<bool> Boolean<T>(Converter<T> converter) where T : IConvertible => new ConverterImpl<bool>((byte[] buffer, ref int index, int count) => Convert.ToUInt32(converter.FromBytes(buffer, ref index, count)) != 0u, (byte[] buffer, ref int index, bool value) => converter.ToBytes(buffer, ref index, (value ? 1u : 0u).To<T>()));
public static Converter<T?> Safe<T>(Converter<T> converter, string or) where T : struct => new ConverterImpl<T?>((byte[] buffer, ref int index, int count) => { try { return converter.FromBytes(buffer, ref index, count); } catch (Exception) { return null; } }, (byte[] buffer, ref int index, T? value) => { if (value != null) converter.ToBytes(buffer, ref index, (T)value); else FromString(buffer, ref index, or); });
private delegate T FromBytes<T>(byte[] buffer, ref int index, int count);
private delegate void ToBytes<T>(byte[] buffer, ref int index, T value);
private class ConverterImpl<T> : Converter<T>
{
private readonly FromBytes<T> from;
private readonly ToBytes<T> to;
public ConverterImpl(FromBytes<T> from, ToBytes<T> to)
{
this.from = from;
this.to = to;
}
public T FromBytes(byte[] buffer, ref int index, int count)
{
int oldIndex = index;
T result = from(buffer, ref index, count);
if (index > oldIndex + count)
{
index = oldIndex;
throw new ArgumentException("insufficient data");
}
return result;
}
public void ToBytes(byte[] buffer, ref int index, T value) => to(buffer, ref index, value);
}
private static T ToDefault<T>(byte[] buffer, ref int index, int count) => default;
private static void FromDefault<T>(byte[] buffer, ref int index, T value) { }
private static string ToString(byte[] buffer, ref int index, int count)
{
int index2 = index;
index += count;
return Encoding.UTF8.GetString(buffer, index2, count);
}
private static void FromString(byte[] buffer, ref int index, string value) => index += Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, index);
private static Uri ToURI(byte[] buffer, ref int index, int count) => new Uri(ToString(buffer, ref index, count));
private static void FromURI(byte[] buffer, ref int index, Uri uri) => FromString(buffer, ref index, uri.ToString());
private static double ToDouble(byte[] buffer, ref int index, int count) => double.Parse(ToString(buffer, ref index, count));
private static void FromDouble(byte[] buffer, ref int index, double value) => FromString(buffer, ref index, value.ToString());
private static T ToUInt<T>(byte[] buffer, ref int index, int count) where T : IConvertible => uint.Parse(ToString(buffer, ref index, count)).To<T>();
private static void FromUInt<T>(byte[] buffer, ref int index, T value) where T : IConvertible => FromString(buffer, ref index, Convert.ToUInt32(value).ToString());
private static T ToByte<T>(byte[] buffer, ref int index, int count) where T : IConvertible => (buffer[index++] - 1u).To<T>();
private static FromBytes<T> ToByte<T>(uint offset) where T : IConvertible => (byte[] buffer, ref int index, int count) => (buffer[index++] - offset).To<T>();
private static ToBytes<T> FromByte<T>(uint offset) where T : IConvertible => (byte[] buffer, ref int index, T value) => buffer[index++] = (byte)(Convert.ToUInt32(value) + offset);
private static FromBytes<bool> ToOptional(byte value) => (byte[] buffer, ref int index, int count) => buffer[index] == value && index == index++;
private static ToBytes<bool> FromOptional(byte value) => (byte[] buffer, ref int index, bool pad) =>
{
if (pad) buffer[index++] = value;
};
private static T To<T>(this uint value) where T : IConvertible => (T)(typeof(T).GetTypeInfo().IsEnum ? Enum.Parse(typeof(T), value.ToString()) : Convert.ChangeType(value, typeof(T)));
}
public class MessageParser
{
public event Action<byte[], int, int> MessageRead;
public event Action<byte[], int, int> MessageWrite;
private byte[] read;
private byte[] write;
private int length;
public MessageParser()
{
read = new byte[4096];
write = new byte[4096];
}
public void Parse(byte[] buffer, int index, int count)
{
CheckLength(count);
for (; index < count; index++)
{
if (buffer[index] != 0) read[length++] = buffer[index];
else if (length > 0)
{
try
{
MessageRead?.Invoke(read, 0, length);
}
catch (Exception e)
{
Debug.WriteLine("Exception thrown in read callback");
Debug.WriteLine(e);
}
length = 0;
}
}
}
public void Parse(byte type, Func<byte[], int, RootWriter> func)
{
while (true)
{
try
{
int length = 1;
length = func(write, length).AddPadding();
write[0] = type;
write[length++] = 0;
try
{
MessageWrite?.Invoke(write, 0, length);
}
catch (Exception e)
{
Debug.WriteLine("Exception thrown in write callback");
Debug.WriteLine(e);
}
break;
}
catch (IndexOutOfRangeException)
{
byte[] buffer = write;
write = new byte[write.Length * 2];
}
}
}
private void CheckLength(int needed)
{
if (read.Length >= length + needed) return;
byte[] buffer = read;
read = new byte[read.Length * 2];
Array.Copy(buffer, read, length);
}
}
public interface Converter<R>
{
R FromBytes(byte[] buffer, ref int index, int count);
void ToBytes(byte[] buffer, ref int index, R value);
}
internal static class ParserExtensions
{
internal static T? ToNullable<T>(this Option<T> option) where T : struct => option.Map(p => (T?)p).ValueOr((T?)null);
}
}