-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (59 loc) · 3.03 KB
/
Program.cs
File metadata and controls
66 lines (59 loc) · 3.03 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
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Faker {
class Section {
public string type { get; set; }
public string name { get; set; }
internal string body { get; set; }
public int size { get; set; }
public List<Section> sections { get; set; }
public override string ToString () => $"{name} = {size}";
}
public static void Main (string[] args) {
//var regex = new Regex(@"(^.assembly .*?)\n+\{\n(.*?)^\}\n*", RegexOptions.Multiline | RegexOptions.Singleline);
var classRegex = new Regex (@"^[.](class) (.*?)\n+\{\n(.*?)^\}\n*", RegexOptions.Multiline | RegexOptions.Singleline);
var methodRegex = new Regex (@"^ [.]method ([^\{]*)\{[^\/]*// Code size\s*(\d+)",RegexOptions.Multiline | RegexOptions.Singleline);
///var regex = new Regex(@"^[.]([\w]+) (.*?)\n+\{\n(.*?)^\}\n*", RegexOptions.Multiline | RegexOptions.Singleline);
var methodNameRegex = new Regex (@"^(\w* )*(.*) cil managed");
var assemblyRegex = new Regex(@"^[.](assembly) (.*?)\n+\{\n(.*?)^\}\n*", RegexOptions.Multiline | RegexOptions.Singleline);
var assemblies = args.Select (arg => {
var data = File.ReadAllText (arg);
var name = assemblyRegex.Matches(data).Select ((Match m) => m.Groups[2].ToString()).First (n => !n.Contains ("extern"));
var sections = new List<Section>();
var matches = classRegex.Matches (data);
foreach (Match match in matches) {
var c = new Section {
type = match.Groups [1].ToString (),
name = Regex.Replace (match.Groups [2].ToString (), @"\s+", " "),
body = match.Groups [3].ToString ()
};
var methods = methodRegex.Matches (c.body);
c.sections = methods.Select ((Match method) => {
return new Section {
type = "method",
name = methodNameRegex.Matches (Regex.Replace (method.Groups [1].ToString (), @"\s+", " ")).Select ((Match n) => $"{n.Groups[1]}{n.Groups[2]}").First(),
size = Int32.Parse (method.Groups [2].ToString ())
};
}).ToList ();
c.size = c.sections.Any() ? c.sections.Select (s => s.size).Aggregate ((total, item) => total + item) : 0;
sections.Add (c);
}
return new Section {
type = "assembly",
name = name,
sections = sections,
size = sections.Select (s => s.size).Aggregate ((total, item) => total + item)
};
}).ToList ();
var options = new JsonSerializerOptions {
WriteIndented = true
};
var text = JsonSerializer.Serialize(assemblies, options);
Console.WriteLine (text);
}
}