source: binary-improvements/NamePatcher/NameNormalizer.cs@ 103

Last change on this file since 103 was 77, checked in by alloc, 10 years ago

NamePatcher

File size: 9.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Mono.Cecil;
5
6namespace NamePatcher
7{
8 class NameNormalizer
9 {
10 public static void CheckType (TypeDefinition tdef)
11 {
12 CheckNames (tdef);
13 }
14
15 public class vmdGroupInfo
16 {
17 public vmdGroupInfo (string newname, TypeDefinition baseclass)
18 {
19 this.newname = newname;
20 this.baseclass = baseclass;
21 }
22
23 public List<MethodDefinition> applyingmdefs = new List<MethodDefinition> ();
24 public string newname;
25 public TypeDefinition baseclass;
26 };
27
28 public static void setName (IMemberDefinition def, string name)
29 {
30 foreach (KeyValuePair<IMemberDefinition, string> entry in clnamestomod) {
31 if (def.Equals (entry.Key)) {
32 clnamestomod.Remove (def);
33 break;
34 }
35 }
36 clnamestomod.Add (def, name);
37 }
38
39 public static string getName (IMemberDefinition def)
40 {
41 foreach (KeyValuePair<IMemberDefinition, string> entry in clnamestomod) {
42 if (def.Equals (entry.Key)) {
43 return entry.Value;
44 }
45 }
46 return def.Name;
47 }
48
49 private static Boolean paramlistEquals (Mono.Collections.Generic.Collection<ParameterDefinition> _pars1,
50 Mono.Collections.Generic.Collection<ParameterDefinition> _pars2)
51 {
52 if ((_pars1.Count != _pars2.Count) || _pars1.Count < 0)
53 return false;
54 ParameterDefinition[] pars1 = _pars1.ToArray ();
55 ParameterDefinition[] pars2 = _pars2.ToArray ();
56 for (int i = 0; i < _pars1.Count; i++) {
57 ParameterDefinition par1 = pars1 [i];
58 ParameterDefinition par2 = pars2 [i];
59 if (par1 == null) {
60 if (par2 != null)
61 return false;
62 } else if (par2 == null) {
63 if (par1 != null)
64 return false;
65 } else if (!par1.ParameterType.Resolve ().Equals (par2.ParameterType.Resolve ()))
66 return false;
67 }
68 return true;
69 }
70
71 public static int vmethid = 1;
72 public static int classid = 1;
73 public static List<vmdGroupInfo> vclasses = new List<vmdGroupInfo> ();
74 public static Dictionary<IMemberDefinition, string> clnamestomod = new Dictionary<IMemberDefinition, string> ();
75
76 public static void CheckNames (TypeDefinition tdef)
77 {
78 String newTName = makeValidName (getName (tdef));
79 if (newTName != null) {
80 setName (tdef, "" + (tdef.IsClass ? "cl" : "tp") + String.Format ("{0:x4}", classid)/*newTName*/);//tdef.Name = (tdef.IsClass ? "cl" : "tp") + newTName;
81 classid++;
82 }
83 if (tdef.IsEnum)
84 return;
85
86 int cmid = 0;
87 try {
88 if (tdef.HasInterfaces)
89 cmid += checkTypeReferences (tdef.Interfaces, "if", cmid, tdef);
90 if (tdef.HasNestedTypes)
91 cmid += checkLocalDefinitions<TypeDefinition> (tdef.NestedTypes, "scl", cmid, tdef);
92 } catch (Exception e) {
93 throw new Exception ("occured while patching 1", e);
94 }
95 try {
96 if (tdef.HasMethods)
97 cmid += checkLocalDefinitions<MethodDefinition> (tdef.Methods, "md", cmid, tdef);
98 } catch (Exception e) {
99 throw new Exception ("occured while patching 2", e);
100 }
101 try {
102 if (tdef.HasFields)
103 cmid += checkLocalDefinitions<FieldDefinition> (tdef.Fields, "fd", cmid, tdef);
104 } catch (Exception e) {
105 throw new Exception ("occured while patching 3", e);
106 }
107 try {
108 if (tdef.HasEvents)
109 cmid += checkLocalDefinitions<EventDefinition> (tdef.Events, "event", cmid, tdef);
110 } catch (Exception e) {
111 throw new Exception ("occured while patching 4", e);
112 }
113 try {
114 if (tdef.HasProperties)
115 cmid += checkLocalDefinitions<PropertyDefinition> (tdef.Properties, "prop", cmid, tdef);
116 } catch (Exception e) {
117 throw new Exception ("occured while patching 5", e);
118 }
119
120 }
121
122 static void checkLocalDefinition<T> (T def, string prefix, int cmid, TypeDefinition btdef) where T : IMemberDefinition
123 {
124 String newName = makeValidName (getName (def));
125 if (typeof(T) == typeof(MethodDefinition)) {
126 MethodDefinition mdef = def as MethodDefinition;
127 Mono.Collections.Generic.Collection<ParameterDefinition> pardef = mdef.Parameters;
128 if (pardef == null) {
129 pardef = new Mono.Collections.Generic.Collection<ParameterDefinition> ();
130 }
131
132 int parid = 1;
133 if (mdef.IsVirtual && newName != null) {
134 newName = null;
135 prefix = "mdv";
136 List<MethodDefinition> baseVmdefList = new List<MethodDefinition> ();
137 baseVmdefList.Add (mdef);
138 TypeDefinition baseclass = null;
139 try {
140 TypeReference curBaseType = btdef.BaseType;
141 while (curBaseType != null) {
142 TypeDefinition basetdef = curBaseType.Resolve ();
143 if (basetdef == null)
144 break;
145 if (basetdef.HasMethods && basetdef.Methods != null) {
146 foreach (MethodDefinition basemdef in basetdef.Methods) {
147 if (basemdef == null)
148 continue;
149 Mono.Collections.Generic.Collection<ParameterDefinition> basepardef = basemdef.Parameters;
150 if (basepardef == null) {
151 basepardef = new Mono.Collections.Generic.Collection<ParameterDefinition> ();
152 }
153 try {
154 if (basemdef.Name != null && mdef.Name != null && basemdef.Name.Equals (mdef.Name) && basemdef.IsVirtual && paramlistEquals (basepardef, pardef)) {
155 if (makeValidName (getName (basemdef)) == null)
156 newName = getName (basemdef);
157 baseVmdefList.Add (basemdef);
158 baseclass = basetdef;
159 }
160 } catch (Exception) { /*throw new Exception("2.1");*/
161 }
162 }
163 }
164 curBaseType = basetdef.BaseType;
165 }
166
167 } catch (NotSupportedException) {
168 }
169 if (baseclass != null) {
170 vmdGroupInfo vmGroup = null;
171 foreach (vmdGroupInfo curGroupInfo in vclasses) {
172 if (curGroupInfo.applyingmdefs.Count < 1)
173 continue;
174 if (curGroupInfo.applyingmdefs.ToArray () [0].Name.Equals (mdef.Name)
175 && curGroupInfo.baseclass.Name.Equals (baseclass.Name)) {
176 vmGroup = curGroupInfo;
177 break;
178 }
179 }
180 if (vmGroup == null) {
181 if (newName != null)
182 vmGroup = new vmdGroupInfo (newName, baseclass);
183 else
184 vmGroup = new vmdGroupInfo (String.Format ("{0}{1:x4}", prefix, vmethid), baseclass);
185 vclasses.Add (vmGroup);
186 ++vmethid;
187 }
188 object[] baseVmdefs = baseVmdefList.ToArray ();
189 for (int i = baseVmdefs.Length - 1; i >= 0; i--) {
190 MethodDefinition curBaseDef = baseVmdefs [i] as MethodDefinition;
191 foreach (MethodDefinition curSubDef in vmGroup.applyingmdefs) {
192 if ((curBaseDef.DeclaringType == curSubDef.DeclaringType) && paramlistEquals (curSubDef.Parameters, mdef.Parameters)) {
193 curBaseDef = null;
194 break;
195 }
196 }
197 if (curBaseDef != null)
198 vmGroup.applyingmdefs.Add (curBaseDef);
199 }
200 newName = null;
201 }
202 }
203 if (mdef.HasParameters) {
204 foreach (ParameterDefinition pdef in mdef.Parameters) {
205 String parName = makeValidName (pdef.Name);
206 if (parName != null)
207 pdef.Name = String.Format ("par{0:x4}", parid);
208 ++parid;
209 }
210 }
211 }
212 newName = (newName == null ? null : String.Format ("{0}{1:x4}", prefix, cmid));
213 if (newName != null) {
214 setName (def, newName);
215 }
216 if (typeof(T) == typeof(TypeDefinition)) {
217 TypeDefinition tdef = def as TypeDefinition;
218 if (!tdef.IsEnum)
219 CheckNames (tdef);
220 }
221 if (typeof(T) == typeof(PropertyDefinition)) {
222 }
223 }
224
225 static int checkLocalDefinitions<T> (Mono.Collections.Generic.Collection<T> memDef, string prefix, int cmid, TypeDefinition btdef)
226 where T : IMemberDefinition
227 {
228 if (memDef == null)
229 return cmid;
230 foreach (T def in memDef) {
231 checkLocalDefinition<T> (def, prefix, cmid, btdef);
232 ++cmid;
233 }
234 return cmid;
235 }
236
237 static int checkTypeReferences<T> (Mono.Collections.Generic.Collection<T> tRefs, string prefix, int cmid, TypeDefinition btdef)
238 where T : MemberReference
239 {
240 if (tRefs == null)
241 return cmid;
242 foreach (MemberReference mref in tRefs) {
243 try {
244 if (typeof(T) == typeof(TypeReference)) {
245 TypeDefinition tDef = (mref as TypeReference).Resolve ();
246 checkLocalDefinition<TypeDefinition> (tDef, prefix, cmid, btdef);
247 } else if (typeof(T) == typeof(MethodReference)) {
248 MethodDefinition mdDef = (mref as MethodReference).Resolve ();
249 checkLocalDefinition<MethodDefinition> (mdDef, prefix, cmid, btdef);
250 }
251 } catch (NotSupportedException) {
252 }
253 }
254 return cmid;
255 }
256 /*
257 * Made public by Alloc
258 */
259 public static String makeValidName (String origName)
260 {
261 if (origName == null)
262 return "nullname";
263 StringBuilder namebuilder = new StringBuilder ();
264 bool modname = false;
265 foreach (char ch in origName) {
266 if (
267 (
268 ((ch & 0x00FF) > 0x7F) || (((ch & 0xFF00) >> 8) > 0x7F)
269 ) ||
270 (("" + ch).Normalize ().ToCharArray () [0] > 0x00FF) ||
271 (((("" + ch).Normalize ().ToCharArray () [0] & 0x00FF)) <= 0x20)
272 ) {
273 namebuilder.Append (String.Format ("u{0:x4}", (ushort)ch));
274 modname = true;
275 } else {
276 namebuilder.Append (ch);
277 }
278 }
279 if (modname)
280 return namebuilder.ToString ();
281 return null;
282 }
283
284 public static void FinalizeNormalizing ()
285 {
286 foreach (KeyValuePair<IMemberDefinition, string> vce in NameNormalizer.clnamestomod) {
287 try {
288 vce.Key.Name = vce.Value;
289 } catch (Exception e) {
290 Console.WriteLine ("An exception occured : ");
291 Console.WriteLine (e.ToString ());
292 }
293 }
294 foreach (NameNormalizer.vmdGroupInfo curGroupEntry in NameNormalizer.vclasses) {
295 try {
296 foreach (MethodDefinition curkey in curGroupEntry.applyingmdefs) {
297 curkey.Name = curGroupEntry.newname;
298 }
299 } catch (Exception e) {
300 Console.WriteLine ("An exception occured : ");
301 Console.WriteLine (e.ToString ());
302 }
303 }
304 }
305 }
306}
Note: See TracBrowser for help on using the repository browser.