-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicMessage.cs
1067 lines (941 loc) · 43.4 KB
/
DynamicMessage.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using google.protobuf;
using ProtoBuf;
namespace xresloader.Protobuf {
public partial class DynamicMessage {
static readonly UTF8Encoding encoding = new UTF8Encoding();
public class MsgDiscriptor {
public string Package;
public DescriptorProto Protocol;
public Dictionary<int, FieldDescriptorProto> FieldIdIndex;
public Dictionary<string, FieldDescriptorProto> FieldNameIndex;
}
public struct DynamicDiscriptors {
public Dictionary<string, FileDescriptorProto> FileDescriptors;
public Dictionary<string, MsgDiscriptor> MsgDescriptors;
public Dictionary<string, EnumDescriptorProto> EnumDescriptors;
public Dictionary<string, EnumValueDescriptorProto> EnumValueDescriptors;
}
private DynamicMessage.DynamicDiscriptors descriptors;
private LinkedList<string> lastError = new LinkedList<string>();
private MsgDiscriptor msgDescriptor;
private Dictionary<int, object> fieldData = new Dictionary<int, object>();
public DynamicMessage(MsgDiscriptor proto, DynamicDiscriptors desc) {
descriptors = desc;
msgDescriptor = proto;
}
public string LastError {
get {
return string.Join(", ", lastError.ToArray());
}
}
private void buildIndex() {
if (msgDescriptor.FieldIdIndex.Count != msgDescriptor.Protocol.field.Count) {
msgDescriptor.FieldIdIndex.Clear();
foreach (var fpb in msgDescriptor.Protocol.field) {
msgDescriptor.FieldIdIndex.Add(fpb.number, fpb);
}
}
if (msgDescriptor.FieldNameIndex.Count != msgDescriptor.Protocol.field.Count) {
msgDescriptor.FieldNameIndex.Clear();
foreach (var fpb in msgDescriptor.Protocol.field) {
msgDescriptor.FieldNameIndex.Add(fpb.name, fpb);
}
}
}
private object defaultValue(FieldDescriptorProto desc) {
switch (desc.type) {
case FieldDescriptorProto.Type.TYPE_DOUBLE:
return (double)0.0;
case FieldDescriptorProto.Type.TYPE_FLOAT:
return (float)0.0;
case FieldDescriptorProto.Type.TYPE_INT64:
case FieldDescriptorProto.Type.TYPE_SFIXED64:
case FieldDescriptorProto.Type.TYPE_SINT64:
return (long)0;
case FieldDescriptorProto.Type.TYPE_FIXED64:
case FieldDescriptorProto.Type.TYPE_UINT64:
return (ulong)0;
case FieldDescriptorProto.Type.TYPE_INT32:
case FieldDescriptorProto.Type.TYPE_SINT32:
case FieldDescriptorProto.Type.TYPE_SFIXED32:
return (int)0;
case FieldDescriptorProto.Type.TYPE_FIXED32:
case FieldDescriptorProto.Type.TYPE_UINT32:
return (uint)0;
case FieldDescriptorProto.Type.TYPE_BOOL:
return false;
case FieldDescriptorProto.Type.TYPE_STRING:
return "";
case FieldDescriptorProto.Type.TYPE_GROUP:
return null;
case FieldDescriptorProto.Type.TYPE_MESSAGE:
return null;
case FieldDescriptorProto.Type.TYPE_BYTES:
return new byte[0];
case FieldDescriptorProto.Type.TYPE_ENUM:
return (int)0;
default:
return null;
}
}
public object GetFieldValue(int field_id) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out field)) {
return GetFieldValue(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_id));
return null;
}
public object GetFieldValue(string field_name) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out field)) {
return GetFieldValue(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_name));
return null;
}
public object GetFieldValue(FieldDescriptorProto field_desc) {
lastError.Clear();
object ret;
if (fieldData.TryGetValue(field_desc.number, out ret)) {
if (ret is List<object>) {
lastError.AddLast(string.Format("field {0} is repeated", field_desc.number));
return null;
}
return ret;
}
FieldDescriptorProto field;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_desc.number, out field)) {
return defaultValue(field);
}
return null;
}
public List<object> GetFieldList(int field_id) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out field)) {
return GetFieldList(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_id));
return null;
}
public List<object> GetFieldList(string field_name) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out field)) {
return GetFieldList(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_name));
return null;
}
public List<object> GetFieldList(FieldDescriptorProto field_desc) {
lastError.Clear();
object ret;
if (fieldData.TryGetValue(field_desc.number, out ret)) {
if (!(ret is List<object>)) {
lastError.AddLast(string.Format("field {1} is not repeated", field_desc.number));
return null;
}
return (List<object>)ret;
}
return new List<object>();
}
#region 简化API
public DynamicMessage GetFieldMessage(FieldDescriptorProto desc) {
if (desc.type != FieldDescriptorProto.Type.TYPE_MESSAGE) {
lastError.Clear();
lastError.AddLast(string.Format("field {0}.{1} is not a message", msgDescriptor.Protocol.name, desc.name));
return null;
}
object ret = GetFieldValue(desc);
if (ret is DynamicMessage) {
return (DynamicMessage)ret;
}
return null;
}
public DynamicMessage GetFieldMessage(string field_name) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out field)) {
return MutableMessage(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_name));
return null;
}
public DynamicMessage GetFieldMessage(int field_id) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out field)) {
return MutableMessage(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_id));
return null;
}
#endregion
/// <summary>
/// 输出所有有数据的Fields
/// </summary>
/// <returns></returns>
public List<FieldDescriptorProto> ReflectListFields {
get {
buildIndex();
List<FieldDescriptorProto> ret = new List<FieldDescriptorProto>();
foreach (var data in fieldData) {
FieldDescriptorProto desc;
if (msgDescriptor.FieldIdIndex.TryGetValue(data.Key, out desc)) {
ret.Add(desc);
}
}
return ret;
}
}
/// <summary>
/// Message描述信息
/// </summary>
public DescriptorProto Descriptor {
get {
return msgDescriptor.Protocol;
}
}
/// <summary>
/// Package名称
/// </summary>
public string PackageName {
get {
return msgDescriptor.Package;
}
}
public EnumDescriptorProto GetEnumType(string path) {
EnumDescriptorProto ret;
if (descriptors.EnumDescriptors.TryGetValue(path, out ret)) {
return ret;
}
return null;
}
public EnumValueDescriptorProto GetEnumValue(string path) {
EnumValueDescriptorProto ret;
if (descriptors.EnumValueDescriptors.TryGetValue(path, out ret)) {
return ret;
}
return null;
}
public bool Parse(Stream stream) {
buildIndex();
lastError.Clear();
try {
using (ProtoReader reader = new ProtoReader(stream, null, null)) {
return Parse(reader);
}
} catch (Exception e) {
lastError.AddLast(e.Message);
}
return 0 == lastError.Count;
}
private string pickMsgName(FieldDescriptorProto desc) {
string msg_name = desc.type_name;
if (msg_name.Length > 0 && '.' == msg_name[0]) {
msg_name = msg_name.Substring(1);
}
return msg_name;
}
/// <summary>
/// 创见一个动态Message,和当前的Message共享同一个类型描述集
/// </summary>
/// <param name="path">类型路径</param>
/// <returns>新创建的Message,如果创建失败返回null</returns>
public DynamicMessage CreateMessage(string path) {
MsgDiscriptor sub_desc;
if (false == descriptors.MsgDescriptors.TryGetValue(path, out sub_desc)) {
lastError.Clear();
lastError.AddLast(string.Format("invalid message path {0}", path));
return null;
}
return new DynamicMessage(sub_desc, descriptors);
}
/// <summary>
/// 创见一个动态Message,和当前的Message共享同一个类型描述集
/// </summary>
/// <param name="desc">field描述</param>
/// <returns>新创建的Message,如果创建失败返回null</returns>
public DynamicMessage CreateMessage(FieldDescriptorProto desc) {
if (null == desc) {
lastError.Clear();
lastError.AddLast("FieldDescriptorProto can not be null");
return null;
}
string msg_name = pickMsgName(desc);
return CreateMessage(msg_name);
}
/// <summary>
/// 解析protobuf数据
/// </summary>
/// <param name="reader"></param>
/// <see cref="https://developers.google.com/protocol-buffers/docs/encoding"/>
/// <returns>全部成功返回true,部分失败或全失败返回false,这时候可以通过LastError获取失败信息</returns>
public bool Parse(ProtoReader reader) {
buildIndex();
lastError.Clear();
try {
int field_id;
SubItemToken token;
while ((field_id = reader.ReadFieldHeader()) != 0) {
WireType pb_type = reader.WireType;
FieldDescriptorProto desc;
if (false == msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc)) {
// unknown field skipped
reader.SkipField();
continue;
}
// 类型校验
try {
switch (desc.type) {
case FieldDescriptorProto.Type.TYPE_DOUBLE:
insertField(desc, reader.ReadDouble());
break;
case FieldDescriptorProto.Type.TYPE_FLOAT:
insertField(desc, reader.ReadSingle());
break;
case FieldDescriptorProto.Type.TYPE_INT64:
case FieldDescriptorProto.Type.TYPE_SINT64:
case FieldDescriptorProto.Type.TYPE_SFIXED64:
insertField(desc, reader.ReadInt64());
break;
case FieldDescriptorProto.Type.TYPE_UINT64:
case FieldDescriptorProto.Type.TYPE_FIXED64:
insertField(desc, reader.ReadUInt64());
break;
case FieldDescriptorProto.Type.TYPE_INT32:
case FieldDescriptorProto.Type.TYPE_SINT32:
case FieldDescriptorProto.Type.TYPE_SFIXED32:
insertField(desc, reader.ReadInt32());
break;
case FieldDescriptorProto.Type.TYPE_BOOL:
insertField(desc, reader.ReadBoolean());
break;
case FieldDescriptorProto.Type.TYPE_STRING:
insertField(desc, reader.ReadString());
break;
//case FieldDescriptorProto.Type.TYPE_GROUP: // deprecated
// break;
case FieldDescriptorProto.Type.TYPE_MESSAGE:
token = ProtoReader.StartSubItem(reader);
try {
DynamicMessage sub_msg = CreateMessage(desc);
if (null == sub_msg) {
lastError.AddLast(string.Format("{0}.{1}.{2} => invalid message path {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, pickMsgName(desc)));
break;
}
if (false == sub_msg.Parse(reader)) {
lastError.AddLast(sub_msg.LastError);
} else {
insertField(desc, sub_msg);
}
} catch (Exception e) {
lastError.AddLast(string.Format("{0}.{1}.{2} => {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.Message));
}
ProtoReader.EndSubItem(token, reader);
break;
case FieldDescriptorProto.Type.TYPE_BYTES:
insertField(desc, ProtoReader.AppendBytes(null, reader));
break;
case FieldDescriptorProto.Type.TYPE_FIXED32:
case FieldDescriptorProto.Type.TYPE_UINT32:
insertField(desc, reader.ReadUInt32());
break;
case FieldDescriptorProto.Type.TYPE_ENUM:
insertField(desc, reader.ReadInt32());
break;
default:
// unsupported field
lastError.AddLast(string.Format("field type {0} in {1}.{2}.{3} unsupported", desc.type.ToString(), msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name));
reader.SkipField();
break;
}
} catch (Exception e) {
lastError.AddLast(string.Format("{0}.{1}.{2} {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.ToString()));
reader.SkipField();
}
}
} catch (Exception e) {
lastError.AddLast(e.Message);
}
return 0 == lastError.Count;
}
/// <summary>
/// 转储数据到Stream中
/// </summary>
/// <param name="stream">保存的目标</param>
/// <returns>成功发回true,部分失败返回false,此时可以通过LastError获取失败信息</returns>
public bool Serialize(Stream stream) {
buildIndex();
lastError.Clear();
try {
using (ProtoWriter writer = new ProtoWriter(stream, null, null)) {
return Serialize(writer);
}
} catch (Exception e) {
lastError.AddLast(e.Message);
}
return 0 == lastError.Count;
}
/// <summary>
/// 写出数据
/// </summary>
/// <param name="writer">写出目标</param>
/// <param name="obj">写出对象</param>
/// <param name="field_desc">字段描述</param>
/// <see cref="https://developers.google.com/protocol-buffers/docs/encoding"/>
private void write_object(ProtoWriter writer, object obj, FieldDescriptorProto field_desc) {
try {
switch (field_desc.type) {
case FieldDescriptorProto.Type.TYPE_DOUBLE: {
double val = Convert.ToDouble(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed64, writer);
ProtoWriter.WriteDouble(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_FLOAT: {
float val = Convert.ToSingle(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed32, writer);
ProtoWriter.WriteSingle(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_SINT64:
case FieldDescriptorProto.Type.TYPE_INT64: {
long val = Convert.ToInt64(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteInt64(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_UINT64: {
ulong val = Convert.ToUInt64(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteUInt64(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_SINT32:
case FieldDescriptorProto.Type.TYPE_INT32: {
int val = Convert.ToInt32(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteInt32(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_FIXED64: {
long val = Convert.ToInt64(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed64, writer);
ProtoWriter.WriteInt64(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_FIXED32: {
int val = Convert.ToInt32(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed32, writer);
ProtoWriter.WriteInt32(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_BOOL: {
bool val = Convert.ToBoolean(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteBoolean(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_STRING: {
string val = Convert.ToString(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.String, writer);
ProtoWriter.WriteString(val, writer);
break;
}
//case FieldDescriptorProto.Type.TYPE_GROUP: // deprecated
// break;
case FieldDescriptorProto.Type.TYPE_MESSAGE:
if (!(obj is DynamicMessage)) {
lastError.AddLast(string.Format("try add {0} to {1}.{2}.{3}, but not a message", obj.ToString(), msgDescriptor.Package, msgDescriptor.Protocol.name, field_desc.name));
break;
}
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.String, writer);
SubItemToken token = ProtoWriter.StartSubItem(null, writer);
try {
((DynamicMessage)obj).Serialize(writer);
} catch (Exception e) {
lastError.AddLast(string.Format("{0}.{1}.{2} => {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, field_desc.name, e.Message));
}
ProtoWriter.EndSubItem(token, writer);
break;
case FieldDescriptorProto.Type.TYPE_BYTES: {
if (!(obj is byte[])) {
throw new ArgumentException("{0} should be a byte[]", field_desc.name);
}
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.String, writer);
ProtoWriter.WriteBytes((byte[])obj, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_UINT32: {
uint val = Convert.ToUInt32(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteUInt32(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_ENUM: {
int val = Convert.ToInt32(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Variant, writer);
ProtoWriter.WriteInt32(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_SFIXED32: {
uint val = Convert.ToUInt32(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed32, writer);
ProtoWriter.WriteUInt32(val, writer);
break;
}
case FieldDescriptorProto.Type.TYPE_SFIXED64: {
ulong val = Convert.ToUInt64(obj);
ProtoWriter.WriteFieldHeader(field_desc.number, WireType.Fixed64, writer);
ProtoWriter.WriteUInt64(val, writer);
break;
}
default:
// unsupported field
lastError.AddLast(string.Format("field type {0} in {1}.{2}.{3} unsupported", field_desc.type.ToString(), msgDescriptor.Package, msgDescriptor.Protocol.name, field_desc.name));
break;
}
} catch (Exception e) {
lastError.AddLast(string.Format("{0}.{1}.{2} {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, field_desc.name, e.ToString()));
}
}
/// <summary>
/// 转储数据
/// </summary>
/// <param name="writer">写出目标</param>
/// <returns>全部成功返回true,部分失败或全失败返回false,这时候可以通过LastError获取失败信息</returns>
public bool Serialize(ProtoWriter writer) {
buildIndex();
lastError.Clear();
foreach (var field in fieldData) {
FieldDescriptorProto field_desc = null;
// 不认识的值,直接忽略
if (false == msgDescriptor.FieldIdIndex.TryGetValue(field.Key, out field_desc)) {
continue;
}
if (field_desc.label == FieldDescriptorProto.Label.LABEL_REPEATED) {
foreach (var obj in (List<object>)field.Value) {
write_object(writer, obj, field_desc);
}
} else {
write_object(writer, field.Value, field_desc);
}
}
return 0 == lastError.Count;
}
private void insertField(FieldDescriptorProto desc, object obj) {
object old_data;
if (desc.label == FieldDescriptorProto.Label.LABEL_REPEATED) {
bool has_old = fieldData.TryGetValue(desc.number, out old_data);
if (has_old) {
((List<object>)old_data).Add(obj);
} else {
fieldData.Add(desc.number, new List<object> { obj });
}
} else {
fieldData[desc.number] = obj;
}
}
private void buildString(StringBuilder builder, string ident = "") {
builder.Append("{\r\n");
foreach (var field in fieldData) {
FieldDescriptorProto field_desc = null;
if (msgDescriptor.FieldIdIndex.TryGetValue(field.Key, out field_desc)) {
builder.AppendFormat(" {0}{1} = ", ident, field_desc.name);
} else {
builder.AppendFormat(" {0}{1} = ", ident, field.Key);
}
if (field.Value is List<object>) {
builder.Append("[ ");
foreach (var sub_obj in ((List<object>)field.Value)) {
builder.AppendFormat(" {0} ", ident);
if (sub_obj is DynamicMessage) {
((DynamicMessage)sub_obj).buildString(builder, ident + " ");
builder.Append(", ");
} else {
if (null != field_desc && field_desc.type == FieldDescriptorProto.Type.TYPE_BYTES) {
builder.AppendFormat("{0}, ", BitConverter.ToString((byte[])sub_obj).Replace("-", ""));
} else {
builder.AppendFormat("{0}, ", sub_obj.ToString());
}
}
}
builder.AppendFormat("{0} ],\r\n", ident);
} else {
if (field.Value is DynamicMessage) {
((DynamicMessage)field.Value).buildString(builder, ident + " ");
builder.Append(",");
} else {
if (null != field_desc && field_desc.type == FieldDescriptorProto.Type.TYPE_BYTES) {
builder.AppendFormat("{0},", BitConverter.ToString((byte[])field.Value).Replace("-", ""));
} else {
builder.AppendFormat("{0},", field.Value.ToString());
}
}
builder.AppendFormat("\r\n", ident);
}
}
builder.Append(ident);
builder.Append("}");
}
public override string ToString() {
StringBuilder builder = new StringBuilder();
buildString(builder);
return builder.ToString();
}
/// <summary>
/// 移除字段
/// </summary>
/// <param name="field_id">字段ID</param>
/// <returns>原先不存在返回false,否则返回true</returns>
public bool RemoveField(int field_id) {
if (fieldData.ContainsKey(field_id)) {
fieldData.Remove(field_id);
return true;
}
return false;
}
/// <summary>
/// 移除字段
/// </summary>
/// <param name="field_desc">字段描述</param>
/// <returns>原先不存在返回false,否则返回true</returns>
public bool RemoveField(FieldDescriptorProto field_desc) {
return RemoveField(field_desc.number);
}
/// <summary>
/// 移除字段
/// </summary>
/// <param name="field_name">字段名称</param>
/// <returns>原先不存在返回false,否则返回true</returns>
public bool RemoveField(string field_name) {
buildIndex();
FieldDescriptorProto field;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out field)) {
return RemoveField(field);
}
lastError.Clear();
lastError.AddLast(string.Format("message {0} has no field {1}", msgDescriptor.Protocol.name, field_name));
return false;
}
private bool verifyField(FieldDescriptorProto desc, ref object obj) {
switch (desc.type) {
case FieldDescriptorProto.Type.TYPE_DOUBLE:
return obj is double || obj is float;
case FieldDescriptorProto.Type.TYPE_FLOAT:
return obj is double || obj is float;
case FieldDescriptorProto.Type.TYPE_INT64:
case FieldDescriptorProto.Type.TYPE_SFIXED64:
case FieldDescriptorProto.Type.TYPE_SINT64:
if (obj is ulong || obj is uint || obj is ushort || obj is byte || obj is long || obj is int || obj is short || obj is sbyte)
{
return true;
}
else if (obj is double || obj is float || obj is string) {
obj = Convert.ToInt64(obj);
return true;
}
return false;
case FieldDescriptorProto.Type.TYPE_UINT64:
case FieldDescriptorProto.Type.TYPE_FIXED64:
if (obj is ulong || obj is uint || obj is ushort || obj is byte || obj is long || obj is int || obj is short || obj is sbyte)
{
return true;
}
else if (obj is double || obj is float || obj is string)
{
obj = Convert.ToUInt64(obj);
return true;
}
return false;
case FieldDescriptorProto.Type.TYPE_ENUM:
if (obj is ulong || obj is uint || obj is ushort || obj is byte || obj is long || obj is int || obj is short || obj is sbyte || obj is string)
{
return true;
}
else if (obj is double || obj is float)
{
obj = Convert.ToInt32((double)obj);
return true;
}
return false;
case FieldDescriptorProto.Type.TYPE_INT32:
case FieldDescriptorProto.Type.TYPE_SINT32:
case FieldDescriptorProto.Type.TYPE_SFIXED32:
if (obj is ulong || obj is uint || obj is ushort || obj is byte || obj is long || obj is int || obj is short || obj is sbyte)
{
return true;
}
else if (obj is double || obj is float || obj is string)
{
obj = Convert.ToInt32(obj);
return true;
}
return false;
case FieldDescriptorProto.Type.TYPE_FIXED32:
case FieldDescriptorProto.Type.TYPE_UINT32:
if (obj is ulong || obj is uint || obj is ushort || obj is byte || obj is long || obj is int || obj is short || obj is sbyte)
{
return true;
}
else if (obj is double || obj is float || obj is string)
{
obj = Convert.ToUInt32(obj);
return true;
}
return false;
case FieldDescriptorProto.Type.TYPE_BOOL:
return obj is bool;
case FieldDescriptorProto.Type.TYPE_STRING:
return obj is string;
case FieldDescriptorProto.Type.TYPE_GROUP://
return false;
case FieldDescriptorProto.Type.TYPE_MESSAGE:
return obj is DynamicMessage && pickMsgName(desc) == string.Format("{0}.{1}", ((DynamicMessage)obj).PackageName, ((DynamicMessage)obj).Descriptor.name);
case FieldDescriptorProto.Type.TYPE_BYTES:
return obj is byte[];
default:
return false;
}
}
/// <summary>
/// 设置值类型数据
/// </summary>
/// <param name="desc">key的描述信息</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool SetFieldValue(FieldDescriptorProto desc, object obj) {
if (null == desc) {
return false;
}
buildIndex();
lastError.Clear();
if (desc.label == FieldDescriptorProto.Label.LABEL_REPEATED) {
lastError.AddLast(string.Format("field {0} is repeated, can only use AddFieldList", desc.name));
return false;
}
if (!verifyField(desc, ref obj)) {
lastError.AddLast(string.Format("field {0} type error, must match {1}", desc.name, desc.type.ToString()));
return false;
}
// 对枚举类型特殊处理
if (desc.type == FieldDescriptorProto.Type.TYPE_ENUM && obj is string) {
EnumValueDescriptorProto enum_desc;
if (!descriptors.EnumValueDescriptors.TryGetValue((string)obj, out enum_desc)) {
lastError.AddLast(string.Format("enum value path {0} not found", obj));
return false;
}
obj = enum_desc.number;
}
fieldData[desc.number] = obj;
return true;
}
/// <summary>
/// 设置值类型数据
/// </summary>
/// <param name="desc">key的ID</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool SetFieldValue(int field_id, object obj) {
buildIndex();
lastError.Clear();
FieldDescriptorProto desc;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc)) {
return SetFieldValue(desc, obj);
} else {
lastError.AddLast(string.Format("field {0} not found", field_id));
return false;
}
}
/// <summary>
/// 设置值类型数据
/// </summary>
/// <param name="desc">key的名称</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool SetFieldValue(string field_name, object obj) {
buildIndex();
lastError.Clear();
FieldDescriptorProto desc;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out desc)) {
return SetFieldValue(desc, obj);
} else {
lastError.AddLast(string.Format("field {0} not found", field_name));
return false;
}
}
/// <summary>
/// 添加List类型数据
/// </summary>
/// <param name="desc">key的描述信息</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool AddFieldList(FieldDescriptorProto desc, object obj) {
if (null == desc) {
return false;
}
buildIndex();
lastError.Clear();
if (desc.label != FieldDescriptorProto.Label.LABEL_REPEATED) {
lastError.AddLast(string.Format("field {0} is repeated, can only use SetFieldValue", desc.name));
return false;
}
if (!verifyField(desc, ref obj)) {
lastError.AddLast(string.Format("field {0} type error, must match {1}", desc.name, desc.type.ToString()));
return false;
}
// 对枚举类型特殊处理
if (desc.type == FieldDescriptorProto.Type.TYPE_ENUM && obj is string) {
EnumValueDescriptorProto enum_desc;
if (!descriptors.EnumValueDescriptors.TryGetValue((string)obj, out enum_desc)) {
lastError.AddLast(string.Format("enum value path {0} not found", obj));
return false;
}
obj = enum_desc.number;
}
object old_val;
if (fieldData.TryGetValue(desc.number, out old_val)) {
((List<object>)old_val).Add(obj);
} else {
fieldData.Add(desc.number, new List<object> { obj });
}
return true;
}
/// <summary>
/// 添加List类型数据
/// </summary>
/// <param name="desc">key的ID</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool AddFieldList(int field_id, object obj) {
buildIndex();
lastError.Clear();
FieldDescriptorProto desc;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc)) {
return AddFieldList(desc, obj);
} else {
lastError.AddLast(string.Format("field {0} not found", field_id));
return false;
}
}
/// <summary>
/// 添加List类型数据
/// </summary>
/// <param name="desc">key的名称</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool AddFieldList(string field_name, object obj) {
buildIndex();
lastError.Clear();
FieldDescriptorProto desc;
if (msgDescriptor.FieldNameIndex.TryGetValue(field_name, out desc)) {
return AddFieldList(desc, obj);
} else {
lastError.AddLast(string.Format("field {0} not found", field_name));
return false;
}
}
/// <summary>
/// 添加字段,如果是repeated会追加的末尾,否则是覆盖
/// </summary>
/// <param name="desc">key的描述信息</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool AddField(FieldDescriptorProto desc, object obj) {
if (null == desc) {
return false;
}
if (desc.label == FieldDescriptorProto.Label.LABEL_REPEATED) {
return AddFieldList(desc, obj);
}
return SetFieldValue(desc, obj);
}
/// <summary>
/// 添加字段,如果是repeated会追加的末尾,否则是覆盖
/// </summary>
/// <param name="desc">key的ID</param>
/// <param name="obj">值</param>
/// <returns>返回是否成功</returns>
public bool AddField(int field_id, object obj) {
buildIndex();
lastError.Clear();
FieldDescriptorProto desc;
if (msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc)) {
return AddField(desc, obj);
} else {
lastError.AddLast(string.Format("field {0} not found", field_id));
return false;
}
}
/// <summary>