This repository has been archived by the owner on Oct 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse message pack.linq
141 lines (129 loc) · 2.38 KB
/
parse message pack.linq
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
<Query Kind="Program" />
void Main()
{
var msgPack = new Message()
{
Type = "msgPack",
Contents = GetPackContents()
};
ParsePack(msgPack);
}
// Define other methods and classes here
List<object> GetPackContents()
{
return @"c
17
650145
AIChase
1
-2.29586997996464E-304
-999847
949892
4635
-3257
5670
True
False
False
False
650047
0
c
17
650124
AIIdle
0
-2.29586997996464E-304
-964927
197432
0
0
4379
False
False
False
False
0
0
c
17
650123
AIIdle
0
-2.29586997996464E-304
-1102401
-485026
0
0
4724
False
False
False
False
0
0
c
17
650066
AITeleportExit
0
-2.29586997996464E-304
-1969241
-334023
-2063
-3966
4232
False
False
False
False
0
0".Trim().Split('\n').Select(x => x.Trim()).Cast<object>().ToList();
}
void ParsePack(Message pack)
{
var type = default(string);
var length = default(int);
try
{
for (int i = 0; i < pack.Contents.Count - 2; i += length)
{
type = pack.Get<string>(i);
length = pack.GetLength(i + 1);
switch (type)
{
case "c": Message.FromMessagePack(pack, "asd", i + 2, length - 2).Dump(); break;
default:
type.Dump();
break;
}
}
}
catch (Exception e)
{
Console.WriteLine($"Failed to parse `msgPack`: {e}");
Console.WriteLine($"Type: {type}");
}
}
public class Message
{
public string Type { get; set; }
public List<object> Contents { get; set; } = new List<object>();
public T Get<T>(int index) => (T)Contents[index];
public int GetLength(int index) => int.Parse((string)Contents[index]);
public static Message FromMessagePack(Message pack, string type, int index, int length)
{
var message = new Message
{
Type = type,
Contents = pack.Contents.Skip(index).Take(length).ToList()
};
if (message.Contents.Count != length)
throw new DataException("message length mismatch");
return message;
}
public override string ToString()
{
return $"Message[{Type}]: length={Contents.Count}";
}
}