source: binary-improvements/NamePatcher/ManualPatches.cs@ 108

Last change on this file since 108 was 107, checked in by alloc, 10 years ago

fixes

File size: 12.5 KB
RevLine 
[107]1using System;
2using Mono.Cecil;
3using Mono.Collections.Generic;
4
5namespace NamePatcher
6{
7 public class ManualPatches
8 {
9 delegate bool MatchField (FieldDefinition fd,TypeReference fdType);
10
11 delegate bool MatchMethod (MethodDefinition md,bool isConstructor,TypeReference retType,int paramCount,Collection<ParameterDefinition> paramList);
12
13 static FieldDefinition getFieldInType (TypeDefinition type, MatchField matcher)
14 {
15 FieldDefinition field = null;
16 if (type != null) {
17 foreach (FieldDefinition fd in type.Fields) {
18 TypeReference fdType = fd.FieldType;
19 if (matcher (fd, fdType)) {
20 if (field != null) {
21 Console.WriteLine ("ERROR: Multiple matching fields found!");
22 return null;
23 }
24 field = fd;
25 }
26 }
27 }
28 return field;
29 }
30
31 static MethodDefinition getMethodInType (TypeDefinition type, MatchMethod matcher)
32 {
33 MethodDefinition method = null;
34 if (type != null) {
35 foreach (MethodDefinition md in type.Methods) {
36 bool cons = md.IsConstructor;
37 TypeReference ret = md.ReturnType;
38 int pCount = md.Parameters.Count;
39 if (matcher (md, cons, ret, pCount, md.Parameters)) {
40 if (method != null) {
41 Console.WriteLine ("ERROR: Multiple matching methods found!");
42 return null;
43 }
44 method = md;
45 }
46 }
47 }
48 return method;
49 }
50
51 static void renameMember (IMemberDefinition fd, string newName)
52 {
53 NameNormalizer.setName (fd, newName);
54 }
55
56 static void makeFieldPublic (FieldDefinition fd)
57 {
58 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
59 }
60
61 public static void applyManualPatches (ModuleDefinition mainModule)
62 {
63 renameMember (mainModule.GetType ("ItemBlock").BaseType.Resolve (), "ItemBase");
64
65 FieldDefinition fd;
66 MethodDefinition md;
67
68
69 fd = getFieldInType (mainModule.GetType ("Authenticator"), (field, fieldType) => {
70 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("System.String"); }
71 );
72 if (fd != null) {
73 makeFieldPublic (fd);
74 renameMember (fd, "usersToIDs");
75 } else {
76 Console.WriteLine ("FAILED: Authenticator.usersToIDs");
77 }
78
79
80 fd = getFieldInType (mainModule.GetType ("PlayerDataFile"), (field, fieldType) => {
81 return field.Name.Equals ("inventory") && fieldType.IsArray; }
82 );
83 if (fd != null) {
84 TypeReference fdType = fd.FieldType;
85 TypeDefinition fdTypeDef = fdType.Resolve ();
86
87 FieldDefinition fd2 = getFieldInType (fdTypeDef, (field, fieldType) => {
88 return fieldType.FullName.Equals ("System.Int32"); }
89 );
90 if (fd2 != null) {
91 renameMember (fd2, "count");
92 } else {
93 Console.WriteLine ("FAILED: InventoryField.count");
94 }
95
96 fd2 = getFieldInType (fdTypeDef, (field, fieldType) => {
97 return fieldType.FullName.Equals ("ItemValue"); }
98 );
99 if (fd2 != null) {
100 renameMember (fd2, "itemValue");
101 } else {
102 Console.WriteLine ("FAILED: InventoryField.itemValue");
103 }
104
105 renameMember (fdTypeDef, "InventoryField");
106 } else {
107 Console.WriteLine ("FAILED: Locate PlayerDataFile.inventory");
108 }
109
110
111 fd = getFieldInType (mainModule.GetType ("AdminTools"), (field, fieldType) => {
112 return fieldType.FullName.Contains ("List") && fieldType.FullName.Contains ("AdminToolsCommandPermissions"); }
113 );
114 if (fd != null) {
115 makeFieldPublic (fd);
116 renameMember (fd, "commandPermissions");
117 } else {
118 Console.WriteLine ("FAILED: AdminTools.commandPermissions");
119 }
120
121
122 fd = getFieldInType (mainModule.GetType ("World"), (field, fieldType) => {
123 return fieldType.FullName.Equals ("System.UInt64"); }
124 );
125 if (fd != null) {
126 renameMember (fd, "gameTime");
127 } else {
128 Console.WriteLine ("FAILED: World.gameTime");
129 }
130
131
132 fd = getFieldInType (mainModule.GetType ("GameManager"), (field, fieldType) => {
133 return fieldType.FullName.Equals ("ConnectionManager"); }
134 );
135 if (fd != null) {
136 makeFieldPublic (fd);
137 renameMember (fd, "connectionManager");
138 } else {
139 Console.WriteLine ("FAILED: GameMananger.connectionManager");
140 }
141
142
143 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
144 return fieldType.FullName.Equals ("GameManager"); }
145 );
146 if (fd != null) {
147 makeFieldPublic (fd);
148 renameMember (fd, "gameManager");
149 } else {
150 Console.WriteLine ("FAILED: ConnectionManager.gameManager");
151 }
152
153
154 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
155 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("ClientInfo"); }
156 );
157 if (fd != null) {
158 makeFieldPublic (fd);
159 renameMember (fd, "connectedClients");
160 } else {
161 Console.WriteLine ("FAILED: ConnectionManager.connectedClients");
162 }
163
164
165 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
166 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("System.Int32,System.Int32"); }
167 );
168 if (fd != null) {
169 makeFieldPublic (fd);
170 renameMember (fd, "mapClientToEntity");
171 } else {
172 Console.WriteLine ("FAILED: ConnectionManager.mapClientToEntity");
173 }
174
175
176 TypeDefinition typeConsole = null;
177
178 fd = getFieldInType (mainModule.GetType ("NetTelnetServer"), (field, fieldType) => {
179 return NameNormalizer.makeValidName (fieldType.Name) != null; }
180 );
181 if (fd != null) {
182 typeConsole = fd.FieldType.Resolve ();
183 renameMember (fd, "console");
184 } else {
185 Console.WriteLine ("FAILED: NetTelnetServer.console");
186 }
187
188
189 if (typeConsole != null) {
190 // Rename class ConsoleSdtd
191 renameMember (typeConsole, "ConsoleSdtd");
192
193 TypeDefinition typeConsoleCommand = null;
194
195 // Rename methods in ConsoleSdtd
196 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
197 return !isConstructor && paramCount == 3 && paramList [0].ParameterType.Name.Equals ("NetworkPlayer");
198 }
199 );
200 if (md != null) {
201 renameMember (md, "ExecuteCmdFromClient");
202 } else {
203 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteCmdFromClient()");
204 }
205
206
207 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
208 return !isConstructor && method.IsPublic && method.IsVirtual && retType.Name.Equals ("Void") && paramCount == 0 && method.Body.CodeSize > 20;
209 }
210 );
211 if (md != null) {
212 renameMember (md, "Run");
213 } else {
214 Console.WriteLine ("FAILED: ConsoleSdtd.Run()");
215 }
216
217
218 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
219 return !isConstructor && method.IsPublic && retType.Name.Equals ("Void") &&
220 paramCount == 1 && paramList [0].ParameterType.Name.Equals ("String") && paramList [0].Name.Equals ("_line");
221 }
222 );
223 if (md != null) {
224 renameMember (md, "SendResult");
225 } else {
226 Console.WriteLine ("FAILED: ConsoleSdtd.SendResult()");
227 }
228
229
230 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
231 return !isConstructor && method.IsPrivate && retType.Name.Equals ("Void") &&
232 paramCount == 2 && paramList [0].ParameterType.Name.Equals ("String") && paramList [1].ParameterType.Name.Equals ("String");
233 }
234 );
235 if (md != null) {
236 md.IsPrivate = false;
237 md.IsPublic = true;
238 renameMember (md, "ExecuteClientCmdInternal");
239 } else {
240 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteClientCmdInternal()");
241 }
242
243
244 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
245 return !isConstructor && method.IsPrivate && retType.Name.Equals ("Void") &&
246 paramCount == 2 && paramList [0].ParameterType.Name.Equals ("String") && paramList [1].ParameterType.Name.Equals ("Boolean");
247 }
248 );
249 if (md != null) {
250 md.IsPrivate = false;
251 md.IsPublic = true;
252 renameMember (md, "ExecuteRemoteCmdInternal");
253 } else {
254 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteRemoteCmdInternal()");
255 }
256
257
258 // Rename fields in ConsoleSdtd
259 fd = getFieldInType (typeConsole, (field, fieldType) => {
260 return fieldType.FullName.Equals ("GameManager"); }
261 );
262 if (fd != null) {
263 makeFieldPublic (fd);
264 renameMember (fd, "gameManager");
265 } else {
266 Console.WriteLine ("FAILED: ConsoleSdtd.gameManager");
267 }
268
269
270 fd = getFieldInType (typeConsole, (field, fieldType) => {
271 if (fieldType.IsGenericInstance) {
272 GenericInstanceType genType = (GenericInstanceType)fieldType;
273 TypeReference genRef = genType.GenericArguments [0];
274 return fieldType.FullName.Contains ("Generic.List") &&
275 genRef.Name.Length < 2 &&
276 NameNormalizer.makeValidName (genRef.Name) != null;
277 }
278 return false;
279 }
280 );
281 if (fd != null) {
282 GenericInstanceType genType = (GenericInstanceType)fd.FieldType;
283 TypeReference genRef = genType.GenericArguments [0];
284 makeFieldPublic (fd);
285 renameMember (fd, "commands");
286
287 typeConsoleCommand = genRef.Resolve ();
288 } else {
289 Console.WriteLine ("FAILED: ConsoleSdtd.commands");
290 }
291
292
293 fd = getFieldInType (typeConsole, (field, fieldType) => {
294 return fieldType.Name.Equals ("NetworkPlayer"); }
295 );
296 if (fd != null) {
297 makeFieldPublic (fd);
298 renameMember (fd, "issuerOfCurrentClientCommand");
299 } else {
300 Console.WriteLine ("FAILED: ConsoleSdtd.issuerOfCurrentClientCommand");
301 }
302
303
304 fd = getFieldInType (typeConsole, (field, fieldType) => {
305 return fieldType.Name.Equals ("NetTelnetServer"); }
306 );
307 if (fd != null) {
308 makeFieldPublic (fd);
309 renameMember (fd, "telnetServer");
310 } else {
311 Console.WriteLine ("FAILED: ConsoleSdtd.telnetServer");
312 }
313
314
315 if (typeConsoleCommand != null) {
316 // Rename class ConsoleCommand
317 renameMember (typeConsoleCommand, "ConsoleCommand");
318
319 // Rename methods in ConsoleSdtd which have parameter or return type ConsoleCommand
320 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
321 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.FullName.Equals ("System.String") && retType.Resolve () == typeConsoleCommand; }
322 );
323 if (md != null) {
324 renameMember (md, "getCommand");
325 } else {
326 Console.WriteLine ("FAILED: ConsoleSdtd.getCommand()");
327 }
328
329
330 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
331 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Resolve () == typeConsoleCommand; }
332 );
333 if (md != null) {
334 renameMember (md, "AddCommand");
335 } else {
336 Console.WriteLine ("FAILED: ConsoleSdtd.AddCommand()");
337 }
338
339
340 // Rename methods in ConsoleCommand
341 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
342 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Resolve () == typeConsole; }
343 );
344 if (md != null) {
345 renameMember (md, "Help");
346 } else {
347 Console.WriteLine ("FAILED: ConsoleCommand.Help()");
348 }
349
350
351 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
352 return !isConstructor && paramCount == 0 && retType.Name.Equals ("Int32"); }
353 );
354 if (md != null) {
355 renameMember (md, "RepeatInterval");
356 } else {
357 Console.WriteLine ("FAILED: ConsoleCommand.RepeatInterval()");
358 }
359
360
361 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
362 return !isConstructor && paramCount == 0 && retType.Name.Equals ("String[]"); }
363 );
364 if (md != null) {
365 renameMember (md, "Names");
366 } else {
367 Console.WriteLine ("FAILED: ConsoleCommand.Names()");
368 }
369
370
371 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
372 return !isConstructor && paramCount == 0 && retType.Name.Equals ("String"); }
373 );
374 if (md != null) {
375 renameMember (md, "Description");
376 } else {
377 Console.WriteLine ("FAILED: ConsoleCommand.Description()");
378 }
379
380
381 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
382 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.IsArray; }
383 );
384 if (md != null) {
385 renameMember (md, "Run");
386 } else {
387 Console.WriteLine ("FAILED: ConsoleCommand.Run()");
388 }
389
390
391
392 } else {
393 Console.WriteLine ("ERROR: ConsoleCommand not found");
394 }
395
396 } else {
397 Console.WriteLine ("ERROR: ConsoleSdtd not found");
398 }
399
400 }
401
402 }
403}
404
Note: See TracBrowser for help on using the repository browser.