-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
620 lines (530 loc) · 23.3 KB
/
Program.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
/* XNA4 Specification Mismatch Test Program for FNA
* Written by Nick Gravelyn
* http://www.brushfiregames.com/
*
* Updates by Ethan "flibitijibibo" Lee
* http://www.flibitijibibo.com/
*
* Released under public domain.
* No warranty implied; use at your own risk.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace XNA4SpecTest
{
class Program
{
static void Main(string[] args)
{
Results results = new Results();
// Gather up all the XNA types from all redist assemblies. We're not looking at content pipeline stuff.
var xnaTypes = GetAllTypes(
typeof(Microsoft.Xna.Framework.Vector2).Assembly, // Microsoft.Xna.Framework.dll
// lol: typeof(Microsoft.Xna.Framework.GamerServices.AvatarDescription).Assembly, // Microsoft.Xna.Framework.Avatar.dll
typeof(Microsoft.Xna.Framework.Game).Assembly, // Microsoft.Xna.Framework.Game.dll
// FNA.NetStub: typeof(Microsoft.Xna.Framework.GamerServices.GamerServicesDispatcher).Assembly, // Microsoft.Xna.Framework.GamerServices.dll
typeof(Microsoft.Xna.Framework.Graphics.GraphicsDevice).Assembly, // Microsoft.Xna.Framework.Graphics.dll
typeof(Microsoft.Xna.Framework.Input.Touch.TouchCollection).Assembly, // Microsoft.Xna.Framework.Input.Touch.dll
// FNA.NetStub: typeof(Microsoft.Xna.Framework.Net.PacketReader).Assembly, // Microsoft.Xna.Framework.Net.dll
typeof(Microsoft.Xna.Framework.Storage.StorageContainer).Assembly, // Microsoft.Xna.Framework.Storage.dll
typeof(Microsoft.Xna.Framework.Media.Video).Assembly, // Microsoft.Xna.Framework.Video.dll
typeof(Microsoft.Xna.Framework.Audio.WaveBank).Assembly // Microsoft.Xna.Framework.Xact.dll
);
// Gather up the FNA types.
// Assuming here that you've got FNA.dll in the output directory.
var fnaTypes = GetAllTypes(Assembly.LoadFrom("FNA.dll"));
// Figure out if there are any types in XNA that aren't in FNA
results.TypesNotInFNA.AddRange(from p in xnaTypes
where !fnaTypes.ContainsKey(p.Key)
orderby p.Key
select p.Value.FullName);
// Figure out if there are any types in FNA that aren't in XNA
results.TypesExtraInFNA.AddRange(from p in fnaTypes
where !xnaTypes.ContainsKey(p.Key)
// Ignore any types which aren't in the Microsoft.Xna.Framework* namespaces.
where p.Key.StartsWith("Microsoft.Xna.Framework")
// Ignore any EXT types.
where !p.Key.EndsWith("EXT")
orderby p.Key
select p.Value.FullName);
// Get the types that are in both APIs for further comparison
var matchedTypes = from p in xnaTypes
where fnaTypes.ContainsKey(p.Key)
orderby p.Key
select new { XNAType = p.Value, FNAType = fnaTypes[p.Key] };
// Perform per-type comparisons to look for method/field/property equivalence.
foreach (var pair in matchedTypes)
{
TypeResults typeResults = new TypeResults { TypeName = pair.XNAType.FullName };
CompareTypes(pair.XNAType, pair.FNAType, typeResults);
if (!typeResults.IsEmpty())
{
results.TypeComparisons.Add(typeResults);
}
}
using (var writer = new StreamWriter("SpecMismatches.txt"))
{
if (results.TypesNotInFNA.Count > 0)
{
writer.WriteLine("Types Not In FNA:");
foreach (var t in results.TypesNotInFNA)
{
writer.WriteLine("\t{0}", t);
}
writer.WriteLine();
}
if (results.TypesExtraInFNA.Count > 0)
{
writer.WriteLine("Types Extra In FNA:");
foreach (var t in results.TypesExtraInFNA)
{
writer.WriteLine("\t{0}", t);
}
writer.WriteLine();
}
if (results.TypeComparisons.Count > 0)
{
writer.WriteLine("Type Comparisons:");
foreach (var t in results.TypeComparisons)
{
writer.WriteLine("\t{0}", t.TypeName);
if (t.FieldsNotInFNA.Count > 0)
{
writer.WriteLine("\t\tFields Not In FNA:");
foreach (var f in t.FieldsNotInFNA)
{
writer.WriteLine("\t\t\t{0}", f);
}
}
if (t.FieldsExtraInFNA.Count > 0)
{
writer.WriteLine("\t\tFields Extra In FNA:");
foreach (var f in t.FieldsExtraInFNA)
{
writer.WriteLine("\t\t\t{0}", f);
}
}
if (t.PropertiesNotInFNA.Count > 0)
{
writer.WriteLine("\t\tProperties Not In FNA:");
foreach (var p in t.PropertiesNotInFNA)
{
writer.WriteLine("\t\t\t{0}", p);
}
}
if (t.PropertiesExtraInFNA.Count > 0)
{
writer.WriteLine("\t\tProperties Extra In FNA:");
foreach (var p in t.PropertiesExtraInFNA)
{
writer.WriteLine("\t\t\t{0}", p);
}
}
if (t.EventsNotInFNA.Count > 0)
{
writer.WriteLine("\t\tEvents Not In FNA:");
foreach (var e in t.EventsNotInFNA)
{
writer.WriteLine("\t\t\t{0}", e);
}
}
if (t.EventsExtraInFNA.Count > 0)
{
writer.WriteLine("\t\tEvents Extra In FNA:");
foreach (var e in t.EventsExtraInFNA)
{
writer.WriteLine("\t\t\t{0}", e);
}
}
if (t.ConstructorsNotInFNA.Count > 0)
{
writer.WriteLine("\t\tConstructors Not In FNA:");
foreach (var c in t.ConstructorsNotInFNA)
{
writer.WriteLine("\t\t\t{0}", c);
}
}
if (t.ConstructorsExtraInFNA.Count > 0)
{
writer.WriteLine("\t\tConstructors Extra In FNA:");
foreach (var c in t.ConstructorsExtraInFNA)
{
writer.WriteLine("\t\t\t{0}", c);
}
}
if (t.MethodsNotInFNA.Count > 0)
{
writer.WriteLine("\t\tMethods Not In FNA:");
foreach (var m in t.MethodsNotInFNA)
{
writer.WriteLine("\t\t\t{0}", m);
}
}
if (t.MethodsExtraInFNA.Count > 0)
{
writer.WriteLine("\t\tMethods Extra In FNA:");
foreach (var m in t.MethodsExtraInFNA)
{
writer.WriteLine("\t\t\t{0}", m);
}
}
writer.WriteLine();
}
}
}
}
// flibit added this to get around mismatches based on assembly name.
private static string CleanString(string fullName)
{
if (fullName == null)
{
return null;
}
string[] assemblyIndicators = new string[]
{
", Microsoft.Xna.Framework",
", FNA"
};
foreach (string indicator in assemblyIndicators)
{
if (fullName.Contains(indicator))
{
return fullName.Substring(
0,
fullName.LastIndexOf(indicator)
);
}
}
return fullName;
}
private static void CompareTypes(Type xnaType, Type fnaType, TypeResults results)
{
var xnaFields = xnaType.GetFields();
var fnaFields = fnaType.GetFields();
results.FieldsNotInFNA.AddRange(from f in xnaFields
where AreFieldsDifferent(f, fnaFields.FirstOrDefault(f2 => f2.Name == f.Name))
orderby f.Name
select f.GetSignature());
results.FieldsExtraInFNA.AddRange(from f in fnaFields
where !f.Name.EndsWith("EXT")
where AreFieldsDifferent(f, xnaFields.FirstOrDefault(f2 => f2.Name == f.Name))
orderby f.Name
select f.GetSignature());
var xnaProperties = xnaType.GetProperties();
var fnaProperties = fnaType.GetProperties();
results.PropertiesNotInFNA.AddRange(from p in xnaProperties
where ArePropertiesDifferent(p, fnaProperties.FirstOrDefault(p2 => p2.Name == p.Name))
orderby p.Name
select p.GetSignature());
results.PropertiesExtraInFNA.AddRange(from p in fnaProperties
where !p.Name.EndsWith("EXT")
where ArePropertiesDifferent(p, xnaProperties.FirstOrDefault(p2 => p2.Name == p.Name))
orderby p.Name
select p.GetSignature());
var xnaEvents = xnaType.GetEvents();
var fnaEvents = fnaType.GetEvents();
results.EventsNotInFNA.AddRange(from e in xnaEvents
where AreEventsDifferent(e, fnaEvents.FirstOrDefault(e2 => e2.Name == e.Name))
orderby e.Name
select e.GetSignature());
results.EventsExtraInFNA.AddRange(from e in fnaEvents
where !e.Name.EndsWith("EXT")
where AreEventsDifferent(e, xnaEvents.FirstOrDefault(e2 => e2.Name == e.Name))
orderby e.Name
select e.GetSignature());
var xnaConstructors = xnaType.GetConstructors();
var fnaConstructors = fnaType.GetConstructors();
results.ConstructorsNotInFNA.AddRange(from c in xnaConstructors
where AreMethodsDifferent(c, fnaConstructors.FirstOrDefault(m2 => !AreMethodsDifferent(c, m2)))
orderby c.Name
select c.GetSignature());
results.ConstructorsExtraInFNA.AddRange(from c in fnaConstructors
where AreMethodsDifferent(c, xnaConstructors.FirstOrDefault(m2 => !AreMethodsDifferent(c, m2)))
orderby c.Name
select c.GetSignature());
var xnaMethods = xnaType.GetMethods().Where(m => !m.IsSpecialName);
var fnaMethods = fnaType.GetMethods().Where(m => !m.IsSpecialName);
results.MethodsNotInFNA.AddRange(from m in xnaMethods
where AreMethodsDifferent(m, fnaMethods.FirstOrDefault(m2 => !AreMethodsDifferent(m, m2)))
orderby m.Name
select m.GetSignature());
results.MethodsExtraInFNA.AddRange(from m in fnaMethods
where !m.Name.EndsWith("EXT")
where AreMethodsDifferent(m, xnaMethods.FirstOrDefault(m2 => !AreMethodsDifferent(m, m2)))
orderby m.Name
select m.GetSignature());
}
private static bool AreFieldsDifferent(FieldInfo field1, FieldInfo field2)
{
if ((field1 == null && field2 != null) || (field1 != null && field2 == null))
{
return true;
}
if (field1.Name != field2.Name)
{
return true;
}
// Compare type names because XNA types won't match FNA types but their names should
if (CleanString(field1.FieldType.FullName) != CleanString(field2.FieldType.FullName))
{
return true;
}
if (field1.IsStatic != field2.IsStatic)
{
return true;
}
return false;
}
private static bool ArePropertiesDifferent(PropertyInfo prop1, PropertyInfo prop2)
{
if ((prop1 == null && prop2 != null) || (prop1 != null && prop2 == null))
{
return true;
}
if (prop1.Name != prop2.Name)
{
return true;
}
// FIXME: WTF...? -flibit
if (prop1.Name == "Item" && prop2.Name == "Item")
{
return false;
}
// Compare type names because XNA types won't match FNA types but their names should
if (CleanString(prop1.PropertyType.FullName) != CleanString(prop2.PropertyType.FullName))
{
return true;
}
if (AreMethodsDifferent(prop1.GetGetMethod(), prop2.GetGetMethod()))
{
return true;
}
if (AreMethodsDifferent(prop1.GetSetMethod(), prop2.GetSetMethod()))
{
return true;
}
return false;
}
private static bool AreEventsDifferent(EventInfo evt1, EventInfo evt2)
{
if ((evt1 == null && evt2 != null) || (evt1 != null && evt2 == null))
{
return true;
}
if (evt1.Name != evt2.Name)
{
return true;
}
// Compare type names because XNA types won't match FNA types but their names should
if (CleanString(evt1.EventHandlerType.FullName) != CleanString(evt2.EventHandlerType.FullName))
{
return true;
}
if (evt1.GetAddMethod().IsStatic != evt2.GetAddMethod().IsStatic)
{
return true;
}
return false;
}
private static bool AreMethodsDifferent(MethodBase method1, MethodBase method2)
{
if (method1 == null && method2 == null)
{
return false;
}
if ((method1 == null && method2 != null) || (method1 != null && method2 == null))
{
return true;
}
if (method1.Name != method2.Name)
{
return true;
}
var methodInfo1 = method1 as MethodInfo;
var methodInfo2 = method2 as MethodInfo;
if ((methodInfo1 == null && methodInfo2 != null) || (methodInfo1 != null && methodInfo2 == null))
{
return true;
}
if (methodInfo1 != null && methodInfo2 != null)
{
// Compare type names because XNA types won't match FNA types but their names should
if (CleanString(methodInfo1.ReturnType.FullName) != CleanString(methodInfo2.ReturnType.FullName))
{
return true;
}
}
var params1 = method1.GetParameters();
var params2 = method2.GetParameters();
if (params1.Count() != params2.Count())
{
return true;
}
for (int i = 0; i < params1.Count(); i++)
{
var p1 = params1.ElementAt(i);
var p2 = params2.ElementAt(i);
if (CleanString(p1.ParameterType.FullName) != CleanString(p2.ParameterType.FullName))
{
return true;
}
if (p1.IsRetval != p2.IsRetval)
{
return true;
}
if (p1.IsOut != p2.IsOut)
{
return true;
}
if (p1.IsOptional != p2.IsOptional)
{
return true;
}
// TODO: Should we be pedantic and look at parameter names?
}
return false;
}
private static Dictionary<string, Type> GetAllTypes(params Assembly[] assemblies)
{
var types = new Dictionary<string, Type>();
foreach (var a in assemblies)
{
foreach (var t in a.GetExportedTypes())
{
types[t.FullName] = t;
}
}
return types;
}
}
public static class Extensions
{
public static string GetSignature(this FieldInfo field)
{
var sig = string.Format("{0} {1}", field.FieldType, field.Name);
if (field.IsStatic)
{
sig = "static " + sig;
}
return sig;
}
public static string GetSignature(this PropertyInfo property)
{
var sig = string.Format("{0} {1} {{", property.PropertyType, property.Name);
var getter = property.GetGetMethod();
var setter = property.GetSetMethod();
if ((getter != null && getter.IsStatic) ||
(setter != null && setter.IsStatic))
{
sig = "static " + sig;
}
if (getter != null)
{
sig += " get;";
}
if (setter != null)
{
sig += " set;";
}
sig += " }";
return sig;
}
public static string GetSignature(this EventInfo evt)
{
var sig = string.Format("{0} {1}", evt.EventHandlerType, evt.Name);
if (evt.GetAddMethod().IsStatic)
{
sig = "static " + sig;
}
return sig;
}
public static string GetSignature(this ConstructorInfo method) {
var sig = string.Format("{0}(", method.DeclaringType.Name);
if (method.IsStatic) {
sig = "static " + sig;
}
var parameters = method.GetParameters();
for (int i = 0; i < parameters.Count(); i++) {
var p = parameters.ElementAt(i);
var pStr = string.Format("{0} {1}", p.ParameterType, p.Name);
if (p.IsOut) {
pStr = "out " + pStr;
} else if (p.IsRetval) {
pStr = "ref " + pStr;
}
sig += pStr;
if (i < parameters.Count() - 1) {
sig += ", ";
}
}
sig += ")";
return sig;
}
public static string GetSignature(this MethodInfo method)
{
var sig = string.Format("{0} {1}(", method.ReturnType, method.Name);
if (method.IsStatic)
{
sig = "static " + sig;
}
var parameters = method.GetParameters();
for (int i = 0; i < parameters.Count(); i++)
{
var p = parameters.ElementAt(i);
var pStr = string.Format("{0} {1}", p.ParameterType, p.Name);
if (p.IsOut)
{
pStr = "out " + pStr;
}
else if (p.IsRetval)
{
pStr = "ref " + pStr;
}
sig += pStr;
if (i < parameters.Count() - 1)
{
sig += ", ";
}
}
sig += ")";
return sig;
}
}
public class Results
{
public List<string> TypesNotInFNA = new List<string>();
public List<string> TypesExtraInFNA = new List<string>();
public List<TypeResults> TypeComparisons = new List<TypeResults>();
}
public class TypeResults
{
public string TypeName;
public List<string> FieldsNotInFNA = new List<string>();
public List<string> FieldsExtraInFNA = new List<string>();
public List<string> PropertiesNotInFNA = new List<string>();
public List<string> PropertiesExtraInFNA = new List<string>();
public List<string> EventsNotInFNA = new List<string>();
public List<string> EventsExtraInFNA = new List<string>();
public List<string> ConstructorsNotInFNA = new List<string>();
public List<string> ConstructorsExtraInFNA = new List<string>();
public List<string> MethodsNotInFNA = new List<string>();
public List<string> MethodsExtraInFNA = new List<string>();
public bool IsEmpty()
{
return
FieldsNotInFNA.Count == 0 &&
FieldsExtraInFNA.Count == 0 &&
PropertiesNotInFNA.Count == 0 &&
PropertiesExtraInFNA.Count == 0 &&
EventsNotInFNA.Count == 0 &&
EventsExtraInFNA.Count == 0 &&
ConstructorsNotInFNA.Count == 0 &&
ConstructorsExtraInFNA.Count == 0 &&
MethodsNotInFNA.Count == 0 &&
MethodsExtraInFNA.Count == 0;
}
}
}