forked from WolvenKit/WolvenKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmUnitTest.cs
360 lines (317 loc) · 12.5 KB
/
frmUnitTest.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WolvenKit;
using System.IO.MemoryMappedFiles;
using System.IO;
using WolvenKit.CR2W;
using WolvenKit.Bundles;
using WolvenKit.Common;
using System.Diagnostics;
using System.Security.Cryptography;
using WolvenKit.Common.Extensions;
using System.Collections.Concurrent;
using System.Threading;
namespace CR2WTests
{
public partial class frmUnitTest : Form
{
private Tuple<long, int> res;
private string ext;
private string bundletag;
private BundleManager bm;
public frmUnitTest(string _ext, string _bundletag, BundleManager _mc)
{
InitializeComponent();
ext = _ext;
bundletag = _bundletag;
bm = _mc;
Setup();
Task.Run(() => Run());
this.Text = ext;
}
private void UpdateRichTextBox1(string val)
{
if (this.richTextBox1.InvokeRequired)
{
this.richTextBox1.BeginInvoke((MethodInvoker)delegate () { this.richTextBox1.AppendText(val);});
}
else
{
this.richTextBox1.AppendText(val);
}
}
private void UpdateRichTextBox2(string val)
{
if (this.richTextBox2.InvokeRequired)
{
this.richTextBox2.BeginInvoke((MethodInvoker)delegate () { this.richTextBox2.AppendText(val); });
}
else
{
this.richTextBox2.AppendText(val);
}
}
private void UpdateProgress()
{
if (this.progressBar1.InvokeRequired)
{
this.progressBar1.BeginInvoke((MethodInvoker)delegate () { this.progressBar1.PerformStep(); });
}
else
{
this.progressBar1.PerformStep();
}
}
private void UpdateMaxProgress(int val)
{
if (this.progressBar1.InvokeRequired)
{
this.progressBar1.BeginInvoke((MethodInvoker)delegate () { this.progressBar1.Maximum = val; });
}
else
{
this.progressBar1.Maximum = val;
}
}
private void Setup()
{
progressBar1.Minimum = 1;
progressBar1.Value = 1;
progressBar1.Step = 1;
}
public async Task<Tuple<long, int>> Run()
{
await Task.Run(() => StressTestExt()).ContinueWith(
antecedent =>
{
res = antecedent.Result;
Thread.Sleep(1000);
try
{
this.Close();
}
catch
{
}
return antecedent.Result;
}
);
return null;
}
public Tuple<long, int> StressTestExt()
{
long unknownbytes = 0;
long totalbytes = 0;
ConcurrentDictionary<string, string> unknownclasses = new ConcurrentDictionary<string, string>();
long filecount = 0;
//This one is trickier, concurrentdict would be of no use since we need += on tuple items, so we will lock.
Dictionary<string, Tuple<long, long>> chunkstate = new Dictionary<string, Tuple<long, long>>();
ConcurrentBag<string> unparsedfiles = new ConcurrentBag<string>();
var processedfiles = new ConcurrentBag<string>();
List<IWitcherFile> files = bm.Items
.SelectMany(_ => _.Value)
.Where(x => x.Name.EndsWith(ext))
.ToList();
filecount = files.Count;
// Set Maximum to the total number of files to copy.
UpdateMaxProgress(files.Count);
Parallel.For (0, files.Count, new ParallelOptions { MaxDegreeOfParallelism = 8 }, i =>
{
IWitcherFile f = (IWitcherFile)files[i];
if (f is BundleItem bi /*&& bi.Name.StartsWith("quests\\minor_quests\\skellige\\mq20")*/)
{
//log
UpdateRichTextBox1($"{i + 1}/{files.Count}: {f.Name}\r\n");
UpdateProgress();
try
{
var fileresult = StressTestFile(bi, ref unknownclasses, ref totalbytes, ref unknownbytes, ref chunkstate);
processedfiles.Add(f.Name);
}
catch (Exception ex)
{
unparsedfiles.Add(f.Name);
//throw ex;
Console.WriteLine($"{f.Name}:{ex.Message}");
UpdateRichTextBox2($"{f.Name}:{ex.Message}\r\n");
}
}
});
Console.WriteLine($"{ext} test completed...");
Console.WriteLine("Results:");
Console.WriteLine($"\t- Parsed {filecount} {ext} files");
Console.WriteLine($"\t- Parsing percentage => {((double)totalbytes - (double)unknownbytes) / (double)totalbytes:0.00%}" +
$" | Couldn't parse: {unparsedfiles.Count} files!");
Console.WriteLine($"Classes: ");
UpdateRichTextBox2($"{ext} test completed...\r\n");
UpdateRichTextBox2("Results:\r\n");
UpdateRichTextBox2($"\t- Parsed {filecount} {ext} files\r\n");
UpdateRichTextBox2($"\t- Parsing percentage => {((double)totalbytes - (double)unknownbytes) / (double)totalbytes:0.00%}\r\n" +
$" | Couldn't parse: {unparsedfiles.Count} files!\r\n");
UpdateRichTextBox2($"Classes: \r\n");
foreach (var c in chunkstate)
{
var percentage = (((double)c.Value.Item1 - (double)c.Value.Item2) / (double)c.Value.Item1);
if (percentage != (double)1)
{
Console.WriteLine($"\t- {c.Key} {percentage:0.00%}");
UpdateRichTextBox2($"\t- {c.Key} {percentage:0.00%}\r\n");
}
}
Console.WriteLine("Files errored during parsing:");
UpdateRichTextBox2("Files errored during parsing:\r\n");
foreach (var f in unparsedfiles)
{
Console.WriteLine($"\t-{f}");
UpdateRichTextBox2($"\t-{f}\r\n");
}
Console.WriteLine("Types unparsed:");
UpdateRichTextBox2("Types unparsed:\r\n");
foreach (var f in unknownclasses)
{
Console.WriteLine($"\t-{f}");
UpdateRichTextBox2($"\t-{f}\r\n");
}
return new Tuple<long, int>(unknownbytes, unparsedfiles.Count);
}
private static int StressTestFile(BundleItem f, ref ConcurrentDictionary<string, string> unknownclasses, ref long totalbytes, ref long unknownbytes, ref Dictionary<string, Tuple<long, long>> chunkstate)
{
var crw = new CR2WFile();
using (var ms = new MemoryStream())
using (var br = new BinaryReader(ms))
{
f.ExtractExistingMMF(ms);
ms.Seek(0, SeekOrigin.Begin);
#region Reading Test A
// reading test
crw.Read(br);
#endregion
#region StringTableTest A.1
// additional tests
(var dict, var strings, var nameslist, var importslist) = crw.GenerateStringtable();
var newdictvalues = dict.Values.ToList();
var dictvalues = crw.StringDictionary.Values.ToList();
var diffDictList = dictvalues.Except(newdictvalues).ToList();
bool isclassicalinconsistentw2anims = false;
bool isclassicalinconsistentw2phase = false;
if (diffDictList.Count != 0)
{
//w2anims inconsistencies
foreach (string str in diffDictList)
{
if (str == "extAnimEvents" ||
str == "array:2,0,handle:CExtAnimEventsFile" ||
str == "CExtAnimEventsFile" ||
str.Contains("sounds\\") ||
str.Contains("sound\\"))
{
isclassicalinconsistentw2anims = true;
break;
}
else
{
continue;
}
}
//w2phase inconsistencies
foreach (string str in diffDictList)
{
if (str == "@SItem" ||
str == "SItem" ||
str == "#CEnvironmentDefinition" ||
str == "CEnvironmentDefinition")
{
isclassicalinconsistentw2phase = true;
break;
}
else
{
continue;
}
}
if (isclassicalinconsistentw2anims)
{
//throw new InvalidBundleException("Classical inconsistent .w2anims - " +
// ".w2animev sound handles left behind in string lists, but actual data is empty");
}
else if (isclassicalinconsistentw2phase)
{
//throw new InvalidBundleException("Inconsistent .w2phase - " +
// "secret e3 files");
// skip test B
}
else
{
throw new InvalidBundleException(" Generated dictionary not equal actual dictionary.");
}
}
#endregion
#region Writing Test B
if (isclassicalinconsistentw2phase || isclassicalinconsistentw2anims)
{
// skip test
// add additional fail-safe test?
}
else
{
byte[] buffer_testB;
byte[] buffer_testB_original;
using (var ms_testB = new MemoryStream())
using (var bw_testB = new BinaryWriter(ms_testB))
{
crw.Write(bw_testB);
buffer_testB = ms_testB.ToArray();
}
// compare
ms.Seek(0, SeekOrigin.Begin);
buffer_testB_original = ms.ToArray();
if (!Enumerable.SequenceEqual(buffer_testB_original, buffer_testB))
{
throw new InvalidBundleException(" Generated cr2w file not equal to original file.");
}
}
#endregion
}
foreach (var ut in crw.UnknownTypes)
{
unknownclasses.TryAdd(ut,ut);
}
foreach (var c in crw.Chunks)
{
var ubsl = c.unknownBytes?.Bytes != null ? c.unknownBytes.Bytes.Length : 0;
lock (chunkstate)
{
if (!chunkstate.ContainsKey(c.REDType))
{
chunkstate.Add(c.REDType, new Tuple<long, long>(0, 0));
}
var already = chunkstate[c.REDType];
chunkstate[c.REDType] = new Tuple<long, long>(
already.Item1 + c.Export.dataSize,
already.Item2 + ubsl
);
}
Interlocked.Add(ref totalbytes, c.Export.dataSize);
Interlocked.Add(ref unknownbytes, ubsl);
}
return 0;
}
internal Tuple<long, int> GetResult()
{
return res;
}
#pragma warning disable CS1998
private async void frmUnitTest_Shown(object sender, EventArgs e)
#pragma warning restore CS1998
{
}
}
}