Changeset 75 for binary-improvements/assembly-patcher/Main.cs
- Timestamp:
- Jul 16, 2014, 7:36:46 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/assembly-patcher/Main.cs
r73 r75 12 12 ModuleDefinition module = ModuleDefinition.ReadModule ("Assembly-CSharp.dll"); 13 13 telnetPatch (module); 14 connectLogPatch(module); 14 15 module.Write ("Assembly-CSharp.dll"); 15 16 Console.WriteLine ("Done"); 17 } 18 19 private static void connectLogPatch (ModuleDefinition module) 20 { 21 TypeDefinition type = module.GetType ("GameManager"); 22 23 if (isPatched (type)) { 24 return; 25 } 26 27 markTypePatched (module, type); 28 addHook (type, "RequestToSpawnPlayer", true, 5, typeof(AllocsRequestToSpawnPlayer).GetMethod ("RequestToSpawnPlayer")); 16 29 } 17 30 … … 25 38 26 39 markTypePatched (module, type); 27 patchMethod (type, ".ctor", 1, typeof(AllocsNetTelnetServer).GetMethod ("init"));28 patchMethod (type, "Disconnect", 0, typeof(AllocsNetTelnetServer).GetMethod ("Disconnect"));29 patchMethod (type, "SetConsole", 1, typeof(AllocsNetTelnetServer).GetMethod ("SetConsole"));30 patchMethod (type, "WriteToClient", 1, typeof(AllocsNetTelnetServer).GetMethod ("WriteToClient"));40 replaceMethod (type, ".ctor", 1, typeof(AllocsNetTelnetServer).GetMethod ("init")); 41 replaceMethod (type, "Disconnect", 0, typeof(AllocsNetTelnetServer).GetMethod ("Disconnect")); 42 replaceMethod (type, "SetConsole", 1, typeof(AllocsNetTelnetServer).GetMethod ("SetConsole")); 43 replaceMethod (type, "WriteToClient", 1, typeof(AllocsNetTelnetServer).GetMethod ("WriteToClient")); 31 44 } 32 45 … … 36 49 } 37 50 38 private static void patchMethod (TypeDefinition type, string methodName, int opCount, MethodBase targetMethod) 51 private static void addHook (TypeDefinition type, string methodName, bool addThisRef, int opCount, MethodBase targetMethod) 52 { 53 foreach (MethodDefinition method in type.Methods) { 54 if (method.Name.Equals (methodName)) { 55 Console.WriteLine ("Patching " + methodName); 56 var il = method.Body.GetILProcessor (); 57 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod)); 58 var i = 0; 59 if (addThisRef) 60 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0)); 61 for (int op = 0; op < opCount; op++) { 62 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1)); 63 } 64 il.InsertBefore (method.Body.Instructions [i++], call); 65 } 66 } 67 } 68 69 private static void replaceMethod (TypeDefinition type, string methodName, int opCount, MethodBase targetMethod) 39 70 { 40 71 foreach (MethodDefinition method in type.Methods) {
Note:
See TracChangeset
for help on using the changeset viewer.