1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Reflection;
|
---|
4 | using Mono.Cecil;
|
---|
5 | using Mono.Cecil.Cil;
|
---|
6 |
|
---|
7 | namespace 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.ConsoleOutputSeparator).GetMethod ("C_ExecuteCmdFromClient"));
|
---|
44 | addHook (type, "Run", true, 0, true, typeof(AllocsFixes.ConsoleOutputSeparator).GetMethod ("C_Run"));
|
---|
45 | replaceMethod (type, "SendResult", true, 1, typeof(AllocsFixes.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.CommandExtensions).GetMethod ("InitCommandExtensions"));
|
---|
53 | }
|
---|
54 |
|
---|
55 | private static void publicCommandPermissionsPatch (ModuleDefinition module)
|
---|
56 | {
|
---|
57 | TypeDefinition type = module.GetType ("AdminTools");
|
---|
58 | replaceMethod (type, "GetAllowedCommandsList", true, 1, typeof(AllocsFixes.AdminToolsStuff).GetMethod ("GetAllowedCommandsList"));
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static void connectLogPatch (ModuleDefinition module)
|
---|
62 | {
|
---|
63 | TypeDefinition type = module.GetType ("GameManager");
|
---|
64 | addHook (type, "RequestToSpawnPlayer", true, 5, true, typeof(AllocsFixes.AllocsLogFunctions).GetMethod ("RequestToSpawnPlayer"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static void telnetPatch (ModuleDefinition module)
|
---|
68 | {
|
---|
69 | TypeDefinition type = module.GetType ("NetTelnetServer");
|
---|
70 | replaceMethod (type, ".ctor", false, 1, typeof(AllocsFixes.AllocsNetTelnetServer).GetMethod ("init"));
|
---|
71 | replaceMethod (type, "Disconnect", false, 0, typeof(AllocsFixes.AllocsNetTelnetServer).GetMethod ("Disconnect"));
|
---|
72 | replaceMethod (type, "SetConsole", false, 1, typeof(AllocsFixes.AllocsNetTelnetServer).GetMethod ("SetConsole"));
|
---|
73 | replaceMethod (type, "WriteToClient", false, 1, typeof(AllocsFixes.AllocsNetTelnetServer).GetMethod ("WriteToClient"));
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static void markTypePatched (ModuleDefinition module, TypeDefinition type)
|
---|
77 | {
|
---|
78 | type.Fields.Add (new FieldDefinition ("AllocsPatch", Mono.Cecil.FieldAttributes.Private | Mono.Cecil.FieldAttributes.SpecialName, module.Import (typeof(int))));
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static void addHook (TypeDefinition type, string methodName, bool addThisRef, int opCount, bool atEnd, MethodBase targetMethod)
|
---|
82 | {
|
---|
83 | foreach (MethodDefinition method in type.Methods) {
|
---|
84 | if (method.Name.Equals (methodName)) {
|
---|
85 | var il = method.Body.GetILProcessor ();
|
---|
86 | var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod));
|
---|
87 | if (atEnd) {
|
---|
88 | int insBefore = method.Body.Instructions.Count;
|
---|
89 | if (addThisRef)
|
---|
90 | il.Append (il.Create (OpCodes.Ldarg, 0));
|
---|
91 | for (int op = 0; op < opCount; op++) {
|
---|
92 | il.Append (il.Create (OpCodes.Ldarg, op + 1));
|
---|
93 | }
|
---|
94 | il.Append (call);
|
---|
95 | il.Remove (method.Body.Instructions [insBefore - 1]);
|
---|
96 | il.Append (il.Create (OpCodes.Ret));
|
---|
97 | } else {
|
---|
98 | var i = 0;
|
---|
99 | if (addThisRef)
|
---|
100 | il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0));
|
---|
101 | for (int op = 0; op < opCount; op++) {
|
---|
102 | il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1));
|
---|
103 | }
|
---|
104 | il.InsertBefore (method.Body.Instructions [i++], call);
|
---|
105 | }
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()");
|
---|
110 | }
|
---|
111 |
|
---|
112 | private static void replaceMethod (TypeDefinition type, string methodName, bool addThisRef, int opCount, MethodBase targetMethod)
|
---|
113 | {
|
---|
114 | foreach (MethodDefinition method in type.Methods) {
|
---|
115 | if (method.Name.Equals (methodName)) {
|
---|
116 | var il = method.Body.GetILProcessor ();
|
---|
117 | var call = il.Create (OpCodes.Call, method.Module.Import (targetMethod));
|
---|
118 | var i = 0;
|
---|
119 | if (addThisRef)
|
---|
120 | il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, 0));
|
---|
121 | for (int op = 0; op < opCount; op++) {
|
---|
122 | il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ldarg, op + 1));
|
---|
123 | }
|
---|
124 | il.InsertBefore (method.Body.Instructions [i++], call);
|
---|
125 | il.InsertBefore (method.Body.Instructions [i++], il.Create (OpCodes.Ret));
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | Console.WriteLine ("ERROR: Did not find " + type.Name + "." + methodName + "()");
|
---|
130 | }
|
---|
131 |
|
---|
132 | private static bool isPatched (TypeDefinition type)
|
---|
133 | {
|
---|
134 | foreach (FieldDefinition fd in type.Fields) {
|
---|
135 | if (fd.Name.Equals ("AllocsPatch")) {
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|