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

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

fixes

File size: 15.1 KB
Line 
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 FieldDefinition fd;
64 MethodDefinition md;
65 bool renamed = false;
66
67
68 renamed = false;
69 foreach (TypeDefinition td in mainModule.Types) {
70 md = getMethodInType (td, (method, isConstructor, retType, paramCount, paramList) => {
71 return !isConstructor && method.IsPublic && method.IsStatic && paramCount == 0 && retType.Name.Equals ("Boolean") && method.Name.Equals ("CheckIfStartedAsDedicatedServer");
72 }
73 );
74 if (md != null) {
75 renameMember (td, "StaticDirectories");
76 renamed = true;
77 break;
78 }
79 }
80 if (!renamed)
81 Console.WriteLine ("FAILED: StaticDirectories");
82 renamed = false;
83
84
85 md = getMethodInType (mainModule.GetType ("EntityPlayer").BaseType.Resolve (), (method, isConstructor, retType, paramCount, paramList) => {
86 return !isConstructor && method.IsPublic && paramCount == 3 && method.Name.Equals ("DamageEntity");
87 }
88 );
89 if (md != null) {
90 TypeDefinition typeDamageSource = md.Parameters[0].ParameterType.Resolve();
91 renameMember (typeDamageSource, "DamageSource");
92 } else {
93 Console.WriteLine ("FAILED: DamageSource");
94 }
95
96
97 renameMember (mainModule.GetType ("ItemBlock").BaseType.Resolve (), "ItemBase");
98
99
100 fd = getFieldInType (mainModule.GetType ("PersistentPlayerList"), (field, fieldType) => {
101 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("Vector3i") && fieldType.FullName.Contains ("PersistentPlayerData"); }
102 );
103 if (fd != null) {
104 makeFieldPublic (fd);
105 renameMember (fd, "positionToLPBlockOwner");
106 } else {
107 Console.WriteLine ("FAILED: PersistentPlayerList.positionToLPBlockOwner");
108 }
109
110
111 fd = getFieldInType (mainModule.GetType ("Authenticator"), (field, fieldType) => {
112 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("System.String"); }
113 );
114 if (fd != null) {
115 makeFieldPublic (fd);
116 renameMember (fd, "usersToIDs");
117 } else {
118 Console.WriteLine ("FAILED: Authenticator.usersToIDs");
119 }
120
121
122 // Fields in PlayerDataFile
123 fd = getFieldInType (mainModule.GetType ("PlayerDataFile"), (field, fieldType) => {
124 return field.Name.Equals ("inventory") && fieldType.IsArray; }
125 );
126 if (fd != null) {
127 TypeReference fdType = fd.FieldType;
128 TypeDefinition fdTypeDef = fdType.Resolve ();
129
130 FieldDefinition fd2 = getFieldInType (fdTypeDef, (field, fieldType) => {
131 return fieldType.FullName.Equals ("System.Int32"); }
132 );
133 if (fd2 != null) {
134 renameMember (fd2, "count");
135 } else {
136 Console.WriteLine ("FAILED: InventoryField.count");
137 }
138
139 fd2 = getFieldInType (fdTypeDef, (field, fieldType) => {
140 return fieldType.FullName.Equals ("ItemValue"); }
141 );
142 if (fd2 != null) {
143 renameMember (fd2, "itemValue");
144 } else {
145 Console.WriteLine ("FAILED: InventoryField.itemValue");
146 }
147
148 renameMember (fdTypeDef, "InventoryField");
149 } else {
150 Console.WriteLine ("FAILED: Locate PlayerDataFile.inventory");
151 }
152
153
154 // Fields in AdminTools
155 fd = getFieldInType (mainModule.GetType ("AdminTools"), (field, fieldType) => {
156 return fieldType.FullName.Contains ("List") && fieldType.FullName.Contains ("AdminToolsCommandPermissions"); }
157 );
158 if (fd != null) {
159 makeFieldPublic (fd);
160 renameMember (fd, "commandPermissions");
161 } else {
162 Console.WriteLine ("FAILED: AdminTools.commandPermissions");
163 }
164
165
166 // Fields and methods in World
167 fd = getFieldInType (mainModule.GetType ("World"), (field, fieldType) => {
168 return fieldType.FullName.Equals ("System.UInt64"); }
169 );
170 if (fd != null) {
171 renameMember (fd, "gameTime");
172 } else {
173 Console.WriteLine ("FAILED: World.gameTime");
174 }
175
176
177 md = getMethodInType (mainModule.GetType ("World"), (method, isConstructor, retType, paramCount, paramList) => {
178 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Name.Equals ("PersistentPlayerData") && method.IsPrivate && retType.Name.Equals ("Boolean");
179 }
180 );
181 if (md != null) {
182 md.IsPrivate = false;
183 md.IsPublic = true;
184 renameMember (md, "LandClaimIsActive");
185 } else {
186 Console.WriteLine ("FAILED: World.LandClaimIsActive()");
187 }
188
189
190 md = getMethodInType (mainModule.GetType ("World"), (method, isConstructor, retType, paramCount, paramList) => {
191 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Name.Equals ("PersistentPlayerData") && method.IsPrivate && retType.Name.Equals ("Single");
192 }
193 );
194 if (md != null) {
195 md.IsPrivate = false;
196 md.IsPublic = true;
197 renameMember (md, "LandClaimPower");
198 } else {
199 Console.WriteLine ("FAILED: World.LandClaimPower()");
200 }
201
202
203 // Fields in GameManager
204 fd = getFieldInType (mainModule.GetType ("GameManager"), (field, fieldType) => {
205 return fieldType.FullName.Equals ("ConnectionManager"); }
206 );
207 if (fd != null) {
208 makeFieldPublic (fd);
209 renameMember (fd, "connectionManager");
210 } else {
211 Console.WriteLine ("FAILED: GameMananger.connectionManager");
212 }
213
214
215 // Fields in ConnectionManager
216 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
217 return fieldType.FullName.Equals ("GameManager"); }
218 );
219 if (fd != null) {
220 makeFieldPublic (fd);
221 renameMember (fd, "gameManager");
222 } else {
223 Console.WriteLine ("FAILED: ConnectionManager.gameManager");
224 }
225
226
227 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
228 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("ClientInfo"); }
229 );
230 if (fd != null) {
231 makeFieldPublic (fd);
232 renameMember (fd, "connectedClients");
233 } else {
234 Console.WriteLine ("FAILED: ConnectionManager.connectedClients");
235 }
236
237
238 fd = getFieldInType (mainModule.GetType ("ConnectionManager"), (field, fieldType) => {
239 return fieldType.FullName.Contains ("Dictionary") && fieldType.FullName.Contains ("System.Int32,System.Int32"); }
240 );
241 if (fd != null) {
242 makeFieldPublic (fd);
243 renameMember (fd, "mapClientToEntity");
244 } else {
245 Console.WriteLine ("FAILED: ConnectionManager.mapClientToEntity");
246 }
247
248
249 TypeDefinition typeConsole = null;
250
251 // Fields in NetTelnetServer
252 fd = getFieldInType (mainModule.GetType ("NetTelnetServer"), (field, fieldType) => {
253 return NameNormalizer.makeValidName (fieldType.Name) != null; }
254 );
255 if (fd != null) {
256 typeConsole = fd.FieldType.Resolve ();
257 renameMember (fd, "console");
258 } else {
259 Console.WriteLine ("FAILED: NetTelnetServer.console");
260 }
261
262
263 if (typeConsole != null) {
264 // Rename class ConsoleSdtd
265 renameMember (typeConsole, "ConsoleSdtd");
266
267 TypeDefinition typeConsoleCommand = null;
268
269 // Rename methods in ConsoleSdtd
270 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
271 return !isConstructor && paramCount == 3 && paramList [0].ParameterType.Name.Equals ("NetworkPlayer");
272 }
273 );
274 if (md != null) {
275 renameMember (md, "ExecuteCmdFromClient");
276 } else {
277 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteCmdFromClient()");
278 }
279
280
281 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
282 return !isConstructor && method.IsPublic && method.IsVirtual && retType.Name.Equals ("Void") && paramCount == 0 && method.Body.CodeSize > 20;
283 }
284 );
285 if (md != null) {
286 renameMember (md, "Run");
287 } else {
288 Console.WriteLine ("FAILED: ConsoleSdtd.Run()");
289 }
290
291
292 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
293 return !isConstructor && method.IsPublic && retType.Name.Equals ("Void") &&
294 paramCount == 1 && paramList [0].ParameterType.Name.Equals ("String") && paramList [0].Name.Equals ("_line");
295 }
296 );
297 if (md != null) {
298 renameMember (md, "SendResult");
299 } else {
300 Console.WriteLine ("FAILED: ConsoleSdtd.SendResult()");
301 }
302
303
304 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
305 return !isConstructor && method.IsPrivate && retType.Name.Equals ("Void") &&
306 paramCount == 2 && paramList [0].ParameterType.Name.Equals ("String") && paramList [1].ParameterType.Name.Equals ("String");
307 }
308 );
309 if (md != null) {
310 md.IsPrivate = false;
311 md.IsPublic = true;
312 renameMember (md, "ExecuteClientCmdInternal");
313 } else {
314 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteClientCmdInternal()");
315 }
316
317
318 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
319 return !isConstructor && method.IsPrivate && retType.Name.Equals ("Void") &&
320 paramCount == 2 && paramList [0].ParameterType.Name.Equals ("String") && paramList [1].ParameterType.Name.Equals ("Boolean");
321 }
322 );
323 if (md != null) {
324 md.IsPrivate = false;
325 md.IsPublic = true;
326 renameMember (md, "ExecuteRemoteCmdInternal");
327 } else {
328 Console.WriteLine ("FAILED: ConsoleSdtd.ExecuteRemoteCmdInternal()");
329 }
330
331
332 // Rename fields in ConsoleSdtd
333 fd = getFieldInType (typeConsole, (field, fieldType) => {
334 return fieldType.FullName.Equals ("GameManager"); }
335 );
336 if (fd != null) {
337 makeFieldPublic (fd);
338 renameMember (fd, "gameManager");
339 } else {
340 Console.WriteLine ("FAILED: ConsoleSdtd.gameManager");
341 }
342
343
344 fd = getFieldInType (typeConsole, (field, fieldType) => {
345 if (fieldType.IsGenericInstance) {
346 GenericInstanceType genType = (GenericInstanceType)fieldType;
347 TypeReference genRef = genType.GenericArguments [0];
348 return fieldType.FullName.Contains ("Generic.List") &&
349 genRef.Name.Length < 2 &&
350 NameNormalizer.makeValidName (genRef.Name) != null;
351 }
352 return false;
353 }
354 );
355 if (fd != null) {
356 GenericInstanceType genType = (GenericInstanceType)fd.FieldType;
357 TypeReference genRef = genType.GenericArguments [0];
358 makeFieldPublic (fd);
359 renameMember (fd, "commands");
360
361 typeConsoleCommand = genRef.Resolve ();
362 } else {
363 Console.WriteLine ("FAILED: ConsoleSdtd.commands");
364 }
365
366
367 fd = getFieldInType (typeConsole, (field, fieldType) => {
368 return fieldType.Name.Equals ("NetworkPlayer"); }
369 );
370 if (fd != null) {
371 makeFieldPublic (fd);
372 renameMember (fd, "issuerOfCurrentClientCommand");
373 } else {
374 Console.WriteLine ("FAILED: ConsoleSdtd.issuerOfCurrentClientCommand");
375 }
376
377
378 fd = getFieldInType (typeConsole, (field, fieldType) => {
379 return fieldType.Name.Equals ("NetTelnetServer"); }
380 );
381 if (fd != null) {
382 makeFieldPublic (fd);
383 renameMember (fd, "telnetServer");
384 } else {
385 Console.WriteLine ("FAILED: ConsoleSdtd.telnetServer");
386 }
387
388
389 if (typeConsoleCommand != null) {
390 // Rename class ConsoleCommand
391 renameMember (typeConsoleCommand, "ConsoleCommand");
392
393 // Rename methods in ConsoleSdtd which have parameter or return type ConsoleCommand
394 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
395 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.FullName.Equals ("System.String") && retType.Resolve () == typeConsoleCommand; }
396 );
397 if (md != null) {
398 renameMember (md, "getCommand");
399 } else {
400 Console.WriteLine ("FAILED: ConsoleSdtd.getCommand()");
401 }
402
403
404 md = getMethodInType (typeConsole, (method, isConstructor, retType, paramCount, paramList) => {
405 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Resolve () == typeConsoleCommand; }
406 );
407 if (md != null) {
408 renameMember (md, "AddCommand");
409 } else {
410 Console.WriteLine ("FAILED: ConsoleSdtd.AddCommand()");
411 }
412
413
414 // Rename methods in ConsoleCommand
415 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
416 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.Resolve () == typeConsole; }
417 );
418 if (md != null) {
419 renameMember (md, "Help");
420 } else {
421 Console.WriteLine ("FAILED: ConsoleCommand.Help()");
422 }
423
424
425 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
426 return !isConstructor && paramCount == 0 && retType.Name.Equals ("Int32"); }
427 );
428 if (md != null) {
429 renameMember (md, "RepeatInterval");
430 } else {
431 Console.WriteLine ("FAILED: ConsoleCommand.RepeatInterval()");
432 }
433
434
435 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
436 return !isConstructor && paramCount == 0 && retType.Name.Equals ("String[]"); }
437 );
438 if (md != null) {
439 renameMember (md, "Names");
440 } else {
441 Console.WriteLine ("FAILED: ConsoleCommand.Names()");
442 }
443
444
445 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
446 return !isConstructor && paramCount == 0 && retType.Name.Equals ("String"); }
447 );
448 if (md != null) {
449 renameMember (md, "Description");
450 } else {
451 Console.WriteLine ("FAILED: ConsoleCommand.Description()");
452 }
453
454
455 md = getMethodInType (typeConsoleCommand, (method, isConstructor, retType, paramCount, paramList) => {
456 return !isConstructor && paramCount == 1 && paramList [0].ParameterType.IsArray; }
457 );
458 if (md != null) {
459 renameMember (md, "Run");
460 } else {
461 Console.WriteLine ("FAILED: ConsoleCommand.Run()");
462 }
463
464
465
466 } else {
467 Console.WriteLine ("ERROR: ConsoleCommand not found");
468 }
469
470 } else {
471 Console.WriteLine ("ERROR: ConsoleSdtd not found");
472 }
473
474 }
475
476 }
477}
478
Note: See TracBrowser for help on using the repository browser.