Changeset 224 for binary-improvements/assembly-patcher/Main.cs
- Timestamp:
- Apr 2, 2015, 9:16:34 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/assembly-patcher/Main.cs
r213 r224 7 7 namespace dtdfixer 8 8 { 9 class MainClass 10 { 11 public static void Main (string[] args) 12 { 9 class MainClass { 10 public static void Main (string[] args) { 13 11 ModuleDefinition module = ModuleDefinition.ReadModule ("Assembly-CSharp.dll"); 14 12 … … 24 22 telnetPatch (module); 25 23 connectLogPatch (module); 26 publicCommandPermissionsPatch (module);27 24 playerDataPatch (module); 28 25 … … 32 29 } 33 30 34 private static void mappingPatch (ModuleDefinition module) 35 { 31 private static void mappingPatch (ModuleDefinition module) { 36 32 TypeDefinition type = module.GetType ("Chunk"); 37 addHook (type, "CalcMapColors", true, 0, true, typeof(AllocsFixes.M apRendering.MapRendering).GetMethod ("RenderSingleChunk"));33 addHook (type, "CalcMapColors", true, 0, true, typeof(AllocsFixes.Mods).GetMethod ("CallCalcMapColors")); 38 34 } 39 35 40 private static void consoleOutputPatch (ModuleDefinition module) 41 { 36 private static void consoleOutputPatch (ModuleDefinition module) { 42 37 TypeDefinition type = module.GetType ("ConsoleSdtd"); 43 38 replaceMethod (type, "ExecuteCmdFromClient", true, 4, typeof(AllocsFixes.NetConnections.ConsoleOutputSeparator).GetMethod ("C_ExecuteCmdFromClient")); … … 46 41 } 47 42 48 private static void playerDataPatch (ModuleDefinition module) 49 { 43 private static void playerDataPatch (ModuleDefinition module) { 50 44 TypeDefinition type = module.GetType ("GameManager"); 51 45 addHook (type, "SavePlayerData", true, 2, true, typeof(AllocsFixes.PlayerDataStuff).GetMethod ("GM_SavePlayerData")); 52 46 addHook (type, "Awake", true, 0, true, typeof(AllocsFixes.StateManager).GetMethod ("Awake")); 53 addHook (type, " Shutdown", true, 0, false, typeof(AllocsFixes.StateManager).GetMethod ("Shutdown"));47 addHook (type, "Cleanup", true, 0, false, typeof(AllocsFixes.StateManager).GetMethod ("Shutdown")); 54 48 } 55 49 56 private static void publicCommandPermissionsPatch (ModuleDefinition module) 57 { 58 TypeDefinition type = module.GetType ("AdminTools"); 59 replaceMethod (type, "GetAllowedCommandsList", true, 1, typeof(AllocsFixes.AdminToolsStuff).GetMethod ("GetAllowedCommandsList")); 60 } 61 62 private static void connectLogPatch (ModuleDefinition module) 63 { 50 private static void connectLogPatch (ModuleDefinition module) { 64 51 TypeDefinition type = module.GetType ("GameManager"); 65 52 addHook (type, "RequestToSpawnPlayer", true, 4, true, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("RequestToSpawnPlayer")); 66 53 type = module.GetType ("ConnectionManager"); 67 addHook (type, " RemovePlayer", true, 2, false, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("PlayerDisconnected"));54 addHook (type, "DisconnectClient", true, 2, false, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("PlayerDisconnected")); 68 55 } 69 56 70 private static void telnetPatch (ModuleDefinition module) 71 { 57 private static void telnetPatch (ModuleDefinition module) { 72 58 TypeDefinition type = module.GetType ("NetTelnetServer"); 73 59 replaceMethod (type, ".ctor", false, 1, typeof(AllocsFixes.NetConnections.NetTelnetServer).GetMethod ("init")); … … 77 63 } 78 64 79 private static void markTypePatched (ModuleDefinition module, TypeDefinition type) 80 { 65 private static void markTypePatched (ModuleDefinition module, TypeDefinition type) { 81 66 type.Fields.Add (new FieldDefinition ("AllocsPatch", Mono.Cecil.FieldAttributes.Private | Mono.Cecil.FieldAttributes.SpecialName, module.Import (typeof(int)))); 82 67 } 83 68 84 private static void addHook (TypeDefinition type, string methodName, bool addThisRef, int opCount, bool atEnd, MethodBase targetMethod) 85 { 86 foreach (MethodDefinition method in type.Methods) { 87 if (method.Name.Equals (methodName)) { 88 var il = method.Body.GetILProcessor (); 89 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod)); 90 if (atEnd) { 91 int insBefore = method.Body.Instructions.Count; 92 if (addThisRef) 93 il.Append (il.Create (OpCodes.Ldarg, 0)); 94 for (int op = 0; op < opCount; op++) { 95 il.Append (il.Create (OpCodes.Ldarg, op + 1)); 96 } 97 il.Append (call); 98 il.Remove (method.Body.Instructions [insBefore - 1]); 99 il.Append (il.Create (OpCodes.Ret)); 100 } else { 101 var i = 0; 102 if (addThisRef) 103 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0)); 104 for (int op = 0; op < opCount; op++) { 105 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1)); 106 } 107 il.InsertBefore (method.Body.Instructions [i++], call); 69 private static void addHook (TypeDefinition type, string methodName, bool addThisRef, int opCount, bool atEnd, MethodBase targetMethod) { 70 MethodDefinition method = findMethod (type, methodName, opCount); 71 if (method != null) { 72 var il = method.Body.GetILProcessor (); 73 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod)); 74 if (atEnd) { 75 int insBefore = method.Body.Instructions.Count; 76 if (addThisRef) { 77 il.Append (il.Create (OpCodes.Ldarg, 0)); 108 78 } 109 return; 110 } 111 } 112 Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()"); 113 } 114 115 private static void replaceMethod (TypeDefinition type, string methodName, bool addThisRef, int opCount, MethodBase targetMethod) 116 { 117 foreach (MethodDefinition method in type.Methods) { 118 if (method.Name.Equals (methodName)) { 119 var il = method.Body.GetILProcessor (); 120 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod)); 79 for (int op = 0; op < opCount; op++) { 80 il.Append (il.Create (OpCodes.Ldarg, op + 1)); 81 } 82 il.Append (call); 83 il.Remove (method.Body.Instructions [insBefore - 1]); 84 il.Append (il.Create (OpCodes.Ret)); 85 } else { 121 86 var i = 0; 122 if (addThisRef) 87 if (addThisRef) { 123 88 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0)); 89 } 124 90 for (int op = 0; op < opCount; op++) { 125 91 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1)); 126 92 } 127 93 il.InsertBefore (method.Body.Instructions [i++], call); 128 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ret)); 129 return; 94 } 95 return; 96 } 97 } 98 99 private static void replaceMethod (TypeDefinition type, string methodName, bool addThisRef, int opCount, MethodBase targetMethod) { 100 MethodDefinition method = findMethod (type, methodName, opCount); 101 if (method != null) { 102 var il = method.Body.GetILProcessor (); 103 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod)); 104 var i = 0; 105 if (addThisRef) { 106 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0)); 107 } 108 for (int op = 0; op < opCount; op++) { 109 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1)); 110 } 111 il.InsertBefore (method.Body.Instructions [i++], call); 112 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ret)); 113 return; 114 } 115 } 116 117 private static MethodDefinition findMethod (TypeDefinition type, string methodName, int opCount) { 118 foreach (MethodDefinition method in type.Methods) { 119 if (method.Name.Equals (methodName) && method.Parameters.Count == opCount) { 120 return method; 130 121 } 131 122 } 132 Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()"); 123 Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "() with " + opCount + " parameters"); 124 return null; 133 125 } 134 126 135 private static bool isPatched (TypeDefinition type) 136 { 127 private static bool isPatched (TypeDefinition type) { 137 128 foreach (FieldDefinition fd in type.Fields) { 138 129 if (fd.Name.Equals ("AllocsPatch")) {
Note:
See TracChangeset
for help on using the changeset viewer.