-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEparser.cs
337 lines (289 loc) · 11.1 KB
/
DEparser.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
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace De_Roll
{
class DEparser
{
public string recTEXT;
public string playerTEXT;
public List<string> playerDB = new List<string>();
public List<string> chatDB = new List<string>();
public string currentFileName;
public string newFileName;
public string dePATH = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Games\Age of Empires 2 DE";
public string GetStrings(string myrec)
{
if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "strings.exe"))
{
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "CMD.EXE";
string args = "/C " + System.AppDomain.CurrentDomain.BaseDirectory + "strings.exe -n 15 " + "\"" + myrec + "\"";
p.StartInfo.Arguments = args;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
recTEXT = output;
return output;
}
else
{
MessageBox.Show("strings.exe not found!", "error!");
return null;
}
}
public bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
public static void CopyStreamHeader(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = null;
byte[] bufferHeaderlen = new byte[4];
byte[] bufferSaved_chapter = new byte[4];
int len;
int lencnt = 0;
int header_len = 0;
int saved_chapter = 0;
//. header_len, saved_chapter
lencnt = input.Read(bufferHeaderlen, 0, 4);
if (lencnt > 0)
{
header_len = BitConverter.ToInt32(bufferHeaderlen, 0);
}
lencnt = input.Read(bufferSaved_chapter, 0, 4);
if (lencnt > 0)
{
saved_chapter = BitConverter.ToInt32(bufferSaved_chapter, 0);
}
//. read header buffer
header_len = header_len - 8;
buffer = new byte[header_len + 2];
buffer[0] = 0x78;
buffer[1] = 0x9c;
if ((len = input.Read(buffer, 2, header_len - 6)) > 0)
{
output.Write(buffer, 0, header_len - 6);
}
output.Flush();
}
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = 0;
if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
}
}
public void decompressFile(string inFile, string outFile)
{
var outputStream = new MemoryStream();
using (var compressedStream = new MemoryStream(File.ReadAllBytes(inFile)))
using (var inputStream = new InflaterInputStream(compressedStream))
{
inputStream.CopyTo(outputStream);
outputStream.Position = 0;
File.WriteAllBytes("test.txt", ReadToEnd(outputStream));
}
}
private byte[] DeREC(MemoryStream ms)
{
ms.Seek(0, SeekOrigin.Begin);
Inflater inflater = new Inflater(true);
InflaterInputStream inStream = new InflaterInputStream(ms, inflater);
byte[] buf = new byte[5000000];
int buf_pos = 0;
int count = buf.Length;
while (true)
{
int numRead = inStream.Read(buf, buf_pos, count);
if (numRead <= 0)
{
break;
}
buf_pos += numRead;
count -= numRead;
}
File.WriteAllBytes("test.txt", buf);
return buf;
}
public string GetPlayernfp(string myrec)
{
string rosText = File.ReadAllText(myrec);
Regex word = new Regex(@"[^\s]{2}[^?](.*).hki");
Match m = word.Match(rosText);
MessageBox.Show("Result:" + m.Value.Substring(16).Replace(".hki", ""));
playerTEXT = m.Value.Substring(16).Replace(".hki", "");
return m.Value.Substring(16).Replace(".hki", "");
//if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "strings.exe"))
//{
// Process p = new Process();
// p.StartInfo.CreateNoWindow = true;
// p.StartInfo.UseShellExecute = false;
// p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
// p.StartInfo.RedirectStandardOutput = true;
// p.StartInfo.FileName = "CMD.EXE";
// string args = "/C " + "\"" + System.AppDomain.CurrentDomain.BaseDirectory + "strings.exe\" " + "\"" + myrec + "\"";
// System.Windows.Forms.Clipboard.SetText(args);
// p.StartInfo.Arguments = args;
// p.Start();
// string output = p.StandardOutput.ReadToEnd();
// p.WaitForExit();
// playerTEXT = output;
// MessageBox.Show(output);
// return output;
//}
//else
//{
// MessageBox.Show("strings.exe not found!", "error!");
// return null;
//}
}
public string GrabID(string verstring)
{
return verstring.Split('(', ')')[2];
}
public string RetName(string newline)
{
// Part 1: the input string.
string input = newline;
// Part 2: call Regex.Match.
Match match = Regex.Match(input, @"@#\d+ (.*) advanced",
RegexOptions.IgnoreCase);
// Part 3: check the Match for Success.
if (match.Success)
{
// Part 4: get the Group value and display it.
string key = match.Groups[1].Value;
return key;
}
return null;
}
public void CollectPlayers(string rec)
{
foreach (string str in rec.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
if (str.Contains("advanced"))
{
if (!playerDB.Contains(RetName(str)) && RetName(str) != null)
playerDB.Add(RetName(str));
// do something with the line
}
else if(str.Contains(":"))
{
Regex pattern = new Regex(@"@#\d+ (.*)");
Match match = pattern.Match(str);
playerDB.Add(match.Groups[1].Value.Split(':').FirstOrDefault());
}
else { continue; }
}
}
public void CollectChat(string rec)
{
foreach (string str in rec.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
if (str.Contains(@"@#"))
{
foreach (string ln in playerDB)
{
if (!str.Contains("advanced"))
{
Regex pattern = new Regex(@"@#\d+ (.*)");
Match match = pattern.Match(str);
chatDB.Add(match.Groups[1].Value);
}
}
// do something with the line
}
else { continue; }
}
}
public void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
currentFileName = fileToDecompress.FullName;
newFileName = currentFileName.Replace(@".nfp", ".db");
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
//File.WriteAllBytes("test.txt", output.ToArray());
//Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
}
}
}
}
public string CollectPlayerName(string nfp)
{
foreach (string str in nfp.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
if (!str.StartsWith(@"??") && str.Contains(@".hki"))
{
return str;
// do something with the line
}
}
return null;
}
}
}