source: binary-improvements/assembly-patcher/Main.cs@ 148

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

Fixes

File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using Mono.Cecil;
5using Mono.Cecil.Cil;
6
7namespace dtdfixer
8{
9 class MainClass
10 {
11 public static void Main (string[] args)
12 {
13 ModuleDefinition module = ModuleDefinition.ReadModule ("Assembly-CSharp.dll");
14
15 TypeDefinition type = module.GetType ("GameManager");
16 if (isPatched (type)) {
17 Console.WriteLine ("Assembly already patched");
18 return;
19 }
20 markTypePatched (module, type);
21
22 mappingPatch (module);
23 consoleOutputPatch (module);
24 telnetPatch (module);
25 connectLogPatch (module);
26 publicCommandPermissionsPatch (module);
27 playerDataPatch (module);
28
29 module.Write ("Assembly-CSharp.patched.dll");
30 Console.WriteLine ("Done");
31
32 }
33
34 private static void mappingPatch (ModuleDefinition module)
35 {
36 TypeDefinition type = module.GetType ("Chunk");
37 addHook (type, "CalcMapColors", true, 0, true, typeof(AllocsFixes.MapRendering.MapRendering).GetMethod ("RenderSingleChunk"));
38 }
39
40 private static void consoleOutputPatch (ModuleDefinition module)
41 {
42 TypeDefinition type = module.GetType ("ConsoleSdtd");
43 replaceMethod (type, "ExecuteCmdFromClient", true, 3, typeof(AllocsFixes.NetConnections.ConsoleOutputSeparator).GetMethod ("C_ExecuteCmdFromClient"));
44 addHook (type, "Run", true, 0, true, typeof(AllocsFixes.NetConnections.ConsoleOutputSeparator).GetMethod ("C_Run"));
45 replaceMethod (type, "SendResult", true, 1, typeof(AllocsFixes.NetConnections.ConsoleOutputSeparator).GetMethod ("C_SendResult"));
46 }
47
48 private static void playerDataPatch (ModuleDefinition module)
49 {
50 TypeDefinition type = module.GetType ("GameManager");
51 addHook (type, "SavePlayerData", true, 2, true, typeof(AllocsFixes.PlayerDataStuff).GetMethod ("GM_SavePlayerData"));
52 addHook (type, "Awake", true, 0, true, typeof(AllocsFixes.StateManager).GetMethod ("Awake"));
53 addHook (type, "Shutdown", true, 0, false, typeof(AllocsFixes.StateManager).GetMethod ("Shutdown"));
54 }
55
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 {
64 TypeDefinition type = module.GetType ("GameManager");
65 addHook (type, "RequestToSpawnPlayer", true, 5, true, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("RequestToSpawnPlayer"));
66 addHook (type, "PlayerDisconnected", true, 1, false, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("PlayerDisconnected"));
67 }
68
69 private static void telnetPatch (ModuleDefinition module)
70 {
71 TypeDefinition type = module.GetType ("NetTelnetServer");
72 replaceMethod (type, ".ctor", false, 1, typeof(AllocsFixes.NetConnections.NetTelnetServer).GetMethod ("init"));
73 replaceMethod (type, "Disconnect", false, 0, typeof(AllocsFixes.NetConnections.NetTelnetServer).GetMethod ("Disconnect"));
74 replaceMethod (type, "SetConsole", false, 1, typeof(AllocsFixes.NetConnections.NetTelnetServer).GetMethod ("SetConsole"));
75 replaceMethod (type, "WriteToClient", false, 1, typeof(AllocsFixes.NetConnections.NetTelnetServer).GetMethod ("WriteToClient"));
76 }
77
78 private static void markTypePatched (ModuleDefinition module, TypeDefinition type)
79 {
80 type.Fields.Add (new FieldDefinition ("AllocsPatch", Mono.Cecil.FieldAttributes.Private | Mono.Cecil.FieldAttributes.SpecialName, module.Import (typeof(int))));
81 }
82
83 private static void addHook (TypeDefinition type, string methodName, bool addThisRef, int opCount, bool atEnd, MethodBase targetMethod)
84 {
85 foreach (MethodDefinition method in type.Methods) {
86 if (method.Name.Equals (methodName)) {
87 var il = method.Body.GetILProcessor ();
88 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod));
89 if (atEnd) {
90 int insBefore = method.Body.Instructions.Count;
91 if (addThisRef)
92 il.Append (il.Create (OpCodes.Ldarg, 0));
93 for (int op = 0; op < opCount; op++) {
94 il.Append (il.Create (OpCodes.Ldarg, op + 1));
95 }
96 il.Append (call);
97 il.Remove (method.Body.Instructions [insBefore - 1]);
98 il.Append (il.Create (OpCodes.Ret));
99 } else {
100 var i = 0;
101 if (addThisRef)
102 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0));
103 for (int op = 0; op < opCount; op++) {
104 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1));
105 }
106 il.InsertBefore (method.Body.Instructions [i++], call);
107 }
108 return;
109 }
110 }
111 Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()");
112 }
113
114 private static void replaceMethod (TypeDefinition type, string methodName, bool addThisRef, int opCount, MethodBase targetMethod)
115 {
116 foreach (MethodDefinition method in type.Methods) {
117 if (method.Name.Equals (methodName)) {
118 var il = method.Body.GetILProcessor ();
119 var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod));
120 var i = 0;
121 if (addThisRef)
122 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0));
123 for (int op = 0; op < opCount; op++) {
124 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1));
125 }
126 il.InsertBefore (method.Body.Instructions [i++], call);
127 il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ret));
128 return;
129 }
130 }
131 Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()");
132 }
133
134 private static bool isPatched (TypeDefinition type)
135 {
136 foreach (FieldDefinition fd in type.Fields) {
137 if (fd.Name.Equals ("AllocsPatch")) {
138 return true;
139 }
140 }
141 return false;
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.