-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
263 lines (234 loc) · 9.29 KB
/
Copy pathProgram.cs
File metadata and controls
263 lines (234 loc) · 9.29 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
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Reflection;
namespace SignatureBuilder
{
static class Program
{
public static int CurrentLine { get; set; } = 0;
static LogLevel ExitLevel = LogLevel.WARN | LogLevel.ERR;
static void Main(string[] cmdArgs)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
AssemblyDescriptionAttribute releaseDate = (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyDescriptionAttribute));
Console.WriteLine($"============== Signature Builder - For Parser rev5 ==============\r\n" +
$"\t+ Build: {version} [{releaseDate.Description}]\r\n\r\n");
var file = (cmdArgs.Length >= 1 ? cmdArgs[0] : ".") + "/parser.txt";
var outFile = (cmdArgs.Length >= 2 ? cmdArgs[1] : ".") + "/_pkts.v5.ignore.json";
if (!System.IO.File.Exists(file))
{
Log(LogLevel.ERR | LogLevel.EXIT, "File not found @ " + file);
}
string[] lines = System.IO.File.ReadAllLines(file);
/* Example of the built output (JSON):
{
// layer 1 - packet header bytes (AKA: delimiter)
"6F": {
// layer 2 - packet infos
"name": "Hero Effect", // - packet main name
"signature": {
// layer 3 - packet signature (determines all the variants of packet body)
"$4 [REPS] $4 $1 $1 [REPE]": {
// layer 4 - infos of the matched packet signature
"desc": "", - description of this packet signature
"params": [ // - infos of each param(data) in the packet signature
{ "name": "HeroID" }, // dict<string, dynamic>
{ "name": "EffectID", "func": ["eHeroEffect", ] },
// more data ...
]
}
}
}
}*/
bool parserSectionStart = false;
ParserCode obj = null;
foreach (string tmp in lines)
{
++CurrentLine;
var line = tmp.Replace("/// ", "").Trim();
if (line == "" || line.StartsWith("//"))
{
continue;
}
// split the actual code, remove comment (//) part.
if (line.Contains("//"))
{
line = line.Split("//")[0].Trim();
}
if (line == "<parser>")
{
// start of parser tag
parserSectionStart = true;
continue;
}
else if (line == "</parser>")
{
// end of parser tag
parserSectionStart = false;
obj = null;
continue;
}
if (!parserSectionStart)
{
// </parser> exists before but no start tag after that.
Log(LogLevel.ERR | LogLevel.EXIT,
"Unexpected line outside <parser> tag.",
"Raw: " + line
);
}
if (line.StartsWith("Fragment["))
{
var m = ParserCode.regexFrag.Match(line).Groups;
if (m[1].Value == "")
{
Log(LogLevel.ERR | LogLevel.EXIT,
"Malform Fragment declaration found.",
"Captured: [" + string.Join(", ", m) + "]",
"Raw: " + line
);
}
var frag = new Fragment(m[1].Value);
if (!Fragment.FragmentPool.TryAdd(frag.Name, frag))
{
Log(LogLevel.ERR | LogLevel.EXIT,
"Failed to declare Fragment.",
"Fragment Name: " + frag.Name,
"Raw: " + line
);
}
Log(LogLevel.NOR, $"New Fragment[{frag.Name}]");
obj = frag;
}
else if (line.StartsWith("Packet["))
{
// packet declaration
var m = ParserCode.regexPkt.Match(line).Groups;
if (m[1].Value == "" || m[2].Value == "")
{
Log(LogLevel.WARN,
$"Skipped mismatch Packet Declaration",
"Captured: {" + string.Join(", ", m) + "}",
"Raw: " + line
);
continue;
}
var pkt = new Packet(m[1].Value, m[2].Value.ToUpper());
if (!Packet.PacketPool.TryAdd(pkt.Header, pkt))
{
Log(LogLevel.ERR | LogLevel.EXIT, $"Duplicated Packet Header[{pkt.Header}]");
}
Log(LogLevel.NOR, $"New Packet[{pkt.Header,-4}] Desc[{pkt.Desc}]");
obj = pkt;
}
else
{
if (obj == null)
{
Log(LogLevel.ERR | LogLevel.EXIT,
"Unexpected line before Packet / Fragment declaration.",
"Raw: " + line
);
}
// push to obj's List for further parsing.
obj.AddLine(CurrentLine, line);
}
}
Console.WriteLine("");
// loop all objects to do parsing
//Fragment.FragmentPool.Values.ToArray()[1].BuildSignature();
JObject mainDict = new JObject();
Fragment.FragmentPool.Values.ToList().ForEach(frag => frag.BuildSignature());
Packet.PacketPool.Values.ToList().ForEach(pkt =>
{
pkt.BuildSignature();
mainDict.Add(pkt.Header, new JObject {
{ "desc", pkt.Desc },
{ "signature", pkt.Signatures }
});
});
// add version & timestamp
mainDict.Add("_timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds() * 1000);
mainDict.Add("_builder", new JArray { releaseDate.Description.Split(" ")[0], "build " + version, "5" });
System.IO.File.WriteAllText(outFile, JsonConvert.SerializeObject(mainDict));
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[+] Build Success! Output File: " + outFile);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
public static void Log(LogLevel lv, params string[] msgs)
{
Log(lv, 0, msgs);
}
public static void Log(LogLevel lv, int indentLv, params string[] msgs)
{
Console.ForegroundColor = lv.GetColor();
string indent = "";
for (int i = 0; i < indentLv; i++)
{
indent += "\t";
}
Console.WriteLine($"{indent}[{lv.GetSymbol()}] {msgs[0]} [@Line:{CurrentLine}]");
for (int i = 1; i < msgs.Length; i++)
{
Console.WriteLine($"{indent}\t+ {msgs[i]}");
}
if (msgs.Length > 1)
{
Console.WriteLine($"");
}
if (lv.HasFlag(LogLevel.EXIT) || lv.HasFlag(ExitLevel))
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
Environment.Exit(1);
}
}
}
public enum LogLevel
{
NOR = 1 << 0,
WARN = 1 << 1,
ERR = 1 << 2,
DEBUG = 1 << 3,
EXIT = 1 << 4,
}
public static class LogLevelMethods
{
public static string GetSymbol(this LogLevel lv)
{
switch (lv)
{
default:
case LogLevel.NOR:
return "+";
case LogLevel.WARN:
return "!";
case LogLevel.ERR:
return "-";
case LogLevel.DEBUG:
return "*";
}
}
public static ConsoleColor GetColor(this LogLevel lv)
{
if (lv.HasFlag(LogLevel.WARN))
{
return ConsoleColor.Yellow;
}
if (lv.HasFlag(LogLevel.ERR))
{
return ConsoleColor.Red;
}
if (lv.HasFlag(LogLevel.DEBUG))
{
return ConsoleColor.Blue;
}
return ConsoleColor.Gray;
}
}
}