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

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

fixes

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