-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
169 lines (155 loc) · 5.61 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
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
namespace AttributeFinder
{
class Program
{
static int Main(string[] args)
{
if(args.Length == 0)
{
return 1;
}
ReaderParameters readParams = AssertBepinexReference();
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(args[0],readParams);
ModuleDefinition a = assembly.MainModule;
var jsonDict = new JsonObject();
foreach(TypeDefinition typ in a.Types)
{
foreach(CustomAttribute attr in typ.CustomAttributes)
{
if( attr.AttributeType.FullName == "BepInEx.BepInPlugin")
{
jsonDict.Add("PluginInfo",PluginJson(attr.ConstructorArguments));
}
if (attr.AttributeType.FullName == "BepInEx.BepInDependency")
{
JsonArray array = (JsonArray) jsonDict.Add("Dependencies", new JsonArray());
array.Add(DependencyJson(attr.ConstructorArguments));
}
if (attr.AttributeType.FullName == "BepInEx.BepInIncompatibility")
{
JsonArray array = (JsonArray)jsonDict.Add("Incompatibilities", new JsonArray());
array.Add(attr.ConstructorArguments[0].Value.ToString());
}
}
}
Console.WriteLine(jsonDict);
return 0;
}
static ReaderParameters AssertBepinexReference()
{
string tempPath = Path.Combine(Path.GetTempPath(), "AttributeFinder");
Directory.CreateDirectory(tempPath);
string tempBepPath = Path.Combine(tempPath, "Bepinex.dll");
if (!File.Exists(tempBepPath))
{
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("AttributeFinder.BepInEx.dll");
FileStream fileStream = new FileStream(tempBepPath, FileMode.CreateNew);
for (int i = 0; i < stream.Length; i++)
fileStream.WriteByte((byte)stream.ReadByte());
fileStream.Close();
}
var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory(tempPath);
return new ReaderParameters{AssemblyResolver = resolver};
}
static JsonObject PluginJson(Mono.Collections.Generic.Collection<CustomAttributeArgument> arguments)
{
JsonObject obj = new JsonObject();
obj.Add("GUID", arguments[0].Value.ToString());
obj.Add("Name", arguments[1].Value.ToString());
obj.Add("Version", arguments[2].Value.ToString());
return obj;
}
static JsonThing DependencyJson(Mono.Collections.Generic.Collection<CustomAttributeArgument> arguments)
{
JsonObject dep = new JsonObject();
dep.Add("Name", arguments[0].Value.ToString());
string type = "";
if (arguments.Count <= 1 || arguments[1].Value.ToString() == "1")
type = "hard";
else
type = "soft";
dep.Add("Type",type);
return dep;
}
private abstract class JsonThing
{
}
private class JsonArray : JsonThing
{
private List<string> Objects;
public JsonArray()
{
Objects = new List<string>();
}
public void Add(string thing)
{
Objects.Add($"\"{thing}\"");
}
public void Add(JsonThing thing)
{
Objects.Add(thing.ToString());
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
sb.Append(string.Join(", ", Objects.ToArray()));
sb.Append("]");
return sb.ToString();
}
}
private class JsonObject : JsonThing
{
private Dictionary<string,JsonThing> Objects;
public JsonObject()
{
Objects = new Dictionary<string, JsonThing>();
}
public JsonThing Add(string key, JsonThing thing)
{
if (Objects.ContainsKey(key))
{
return Objects[key];
}
Objects.Add(key, thing);
return thing;
}
public JsonThing Add(string key, string thing)
{
return Add(key, new JsonString(thing));
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
List<string> list = new List<string>();
foreach (var pair in Objects)
{
list.Add($"\"{pair.Key}\" : {pair.Value}");
}
sb.Append(string.Join(", ",list.ToArray()));
sb.Append("}");
return sb.ToString();
}
}
private class JsonString : JsonThing
{
string Value;
public JsonString(string value)
{
Value = value;
}
public override string ToString()
{
return $"\"{Value}\"";
}
}
}
}