source: binary-improvements/NamePatcher/NamePatcher.cs@ 91

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

fixes

File size: 9.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using Mono.Cecil;
6using System.Reflection;
7
8namespace NamePatcher
9{
10 class NamePatcher
11 {
12 static void DrawUsage ()
13 {
14 Console.WriteLine ("Usage :");
15 Console.WriteLine ("NamePatcher [input file] Patches an Assembly and creates a backup of the original file.");
16 }
17
18 static bool TryArgs (string[] args)
19 {
20 return args.Length == 1 && (File.Exists (args [0]));
21 }
22
23 static bool makeBackup (string input)
24 {
25 string backupFile_base = input + ".bck";
26 string backupFile = backupFile_base;
27 int backupIndex = 0;
28 while (File.Exists(backupFile)) {
29 backupFile = backupFile_base + "." + (++backupIndex);
30 if (backupIndex > 10) {
31 return false;
32 }
33 }
34 try {
35 File.Copy (input, backupFile);
36 } catch (Exception e) {
37 Console.WriteLine ("Unable to create backup file : ");
38 Console.WriteLine (e.ToString ());
39 return false;
40 }
41 return true;
42 }
43
44 static DefaultAssemblyResolver getAssemblyResolver (string path)
45 {
46 DefaultAssemblyResolver resolver = null;
47 int lastSlash = path.LastIndexOf ("\\");
48 if (lastSlash == -1)
49 lastSlash = path.LastIndexOf ("/");
50 if (lastSlash != -1) {
51 string inputPath = path.Substring (0, lastSlash);
52 resolver = new DefaultAssemblyResolver ();
53 resolver.AddSearchDirectory (inputPath);
54 }
55 return resolver;
56 }
57
58 static void applyManualPatches (ModuleDefinition mainModule)
59 {
60 foreach (FieldDefinition fd in mainModule.GetType ("AdminTools").Fields) {
61 TypeReference fdType = fd.FieldType;
62 if (fdType.FullName.Contains ("List") && fdType.FullName.Contains ("AdminToolsCommandPermissions")) {
63 Console.WriteLine ("Renaming and making public admin tools field -> commandPermissions");
64 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
65 NameNormalizer.setName (fd, "commandPermissions");
66 }
67 }
68
69 foreach (FieldDefinition fd in mainModule.GetType ("World").Fields) {
70 TypeReference fdType = fd.FieldType;
71 if (fdType.FullName.Equals ("System.UInt64")) {
72 Console.WriteLine ("Renaming world field -> gameTime");
73 NameNormalizer.setName (fd, "gameTime");
74 }
75 }
76
77 foreach (FieldDefinition fd in mainModule.GetType ("GameManager").Fields) {
78 TypeReference fdType = fd.FieldType;
79 if (fdType.FullName.Equals ("ConnectionManager")) {
80 Console.WriteLine ("Renaming and making public GameMananger field -> connectionManager");
81 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
82 NameNormalizer.setName (fd, "connectionManager");
83 }
84 }
85
86 foreach (FieldDefinition fd in mainModule.GetType ("ConnectionManager").Fields) {
87 TypeReference fdType = fd.FieldType;
88 if (fdType.FullName.Equals ("GameManager")) {
89 Console.WriteLine ("Renaming and making public ConnectionManager field -> gameManager");
90 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
91 NameNormalizer.setName (fd, "gameManager");
92 }
93 if (fdType.FullName.Contains ("Dictionary") && fdType.FullName.Contains ("ClientInfo")) {
94 Console.WriteLine ("Renaming and making public ConnectionManager field -> connectedClients");
95 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
96 NameNormalizer.setName (fd, "connectedClients");
97 }
98 if (fdType.FullName.Contains ("Dictionary") && fdType.FullName.Contains ("System.Int32,System.Int32")) {
99 Console.WriteLine ("Renaming and making public ConnectionManager field -> mapClientToEntity");
100 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
101 NameNormalizer.setName (fd, "mapClientToEntity");
102 }
103 }
104
105 string consoleTypeName = string.Empty;
106 TypeDefinition typeTelnetServer = mainModule.GetType ("NetTelnetServer");
107 foreach (FieldDefinition fd in typeTelnetServer.Fields) {
108 if (NameNormalizer.makeValidName (fd.FieldType.Name) != null) {
109 Console.WriteLine ("Renaming console class -> ConsoleSdtd");
110 consoleTypeName = fd.FieldType.Name;
111 NameNormalizer.setName (fd.FieldType.Resolve (), "ConsoleSdtd");
112 NameNormalizer.setName (fd, "console");
113 }
114 }
115
116 if (consoleTypeName.Length > 0) {
117 TypeDefinition typeConsole = mainModule.GetType (consoleTypeName);
118 string consoleCommandTypeName = string.Empty;
119 foreach (MethodDefinition md in typeConsole.Methods) {
120 if (!md.IsConstructor) {
121 if (md.Parameters.Count == 3 && md.Parameters [0].ParameterType.Name.Equals ("NetworkPlayer")) {
122 Console.WriteLine ("Renaming console method -> ExecuteCmdFromClient");
123 NameNormalizer.setName (md, "ExecuteCmdFromClient");
124 }
125 }
126 }
127 foreach (FieldDefinition fd in typeConsole.Fields) {
128 TypeReference fdType = fd.FieldType;
129 if (fdType.FullName.Contains ("Generic.List")) {
130 if (fdType.IsGenericInstance) {
131 GenericInstanceType genType = (GenericInstanceType)fdType;
132 TypeReference genRef = genType.GenericArguments [0];
133 if (genRef.Name.Length < 2) {
134 Console.WriteLine ("Renaming console command class -> ConsoleCommand");
135 NameNormalizer.setName (genRef.Resolve (), "ConsoleCommand");
136 NameNormalizer.setName (fd, "commands");
137 consoleCommandTypeName = genRef.Name;
138 }
139 }
140 }
141 if (fdType.FullName.Equals ("GameManager")) {
142 Console.WriteLine ("Renaming and making public console field -> gameManager");
143 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
144 NameNormalizer.setName (fd, "gameManager");
145 }
146 }
147
148 if (consoleCommandTypeName.Length > 0) {
149 foreach (MethodDefinition md in typeConsole.Methods) {
150 if (!md.IsConstructor) {
151 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.Name.Equals (consoleCommandTypeName)) {
152 Console.WriteLine ("Renaming console method -> AddCommand");
153 NameNormalizer.setName (md, "AddCommand");
154 }
155 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.FullName.Equals ("System.String") && md.ReturnType.FullName.Equals (consoleCommandTypeName)) {
156 Console.WriteLine ("Renaming console method -> getCommand");
157 NameNormalizer.setName (md, "getCommand");
158 }
159 }
160 }
161
162 TypeDefinition typeConsoleCommand = mainModule.GetType (consoleCommandTypeName);
163 foreach (MethodDefinition md in typeConsoleCommand.Methods) {
164 if (!md.IsConstructor) {
165 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.Name.Equals (consoleTypeName)) {
166 Console.WriteLine ("Renaming console command method -> Help");
167 NameNormalizer.setName (md, "Help");
168 }
169 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("Int32")) {
170 Console.WriteLine ("Renaming console command method -> Timeout");
171 NameNormalizer.setName (md, "Timeout");
172 }
173 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("String[]")) {
174 Console.WriteLine ("Renaming console command method -> Names");
175 NameNormalizer.setName (md, "Names");
176 }
177 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("String")) {
178 Console.WriteLine ("Renaming console command method -> Description");
179 NameNormalizer.setName (md, "Description");
180 }
181 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.IsArray) {
182 Console.WriteLine ("Renaming console command method -> Run");
183 NameNormalizer.setName (md, "Run");
184 }
185 }
186 }
187 }
188 }
189 }
190
191 static void Main (string[] args)
192 {
193 Console.WriteLine ("NamePatcher for 7dtd's Assembly-CSharp.dll [by DerPopo, modified by Alloc] for Dedi build 320404");
194 if (!TryArgs (args)) {
195 DrawUsage ();
196 return;
197 }
198 string dllPath = args [0];
199
200 if (!makeBackup (dllPath)) {
201 Console.WriteLine ("Could not create a backup file (maybe too many old backups?)");
202 return;
203 }
204
205
206 AssemblyDefinition input;
207 try {
208 input = AssemblyDefinition.ReadAssembly (dllPath, new ReaderParameters { AssemblyResolver = getAssemblyResolver(dllPath), });
209 } catch (Exception e) {
210 Console.WriteLine ("Unable to load the input file : ");
211 Console.WriteLine (e.ToString ());
212 return;
213 }
214 Console.WriteLine ();
215 Console.WriteLine ("Patching assembly " + dllPath + " (" + input.Modules.Count + " module[s]) ...");
216
217
218
219 applyManualPatches (input.MainModule);
220
221
222 try {
223 foreach (ModuleDefinition mdef in input.Modules) {
224 Console.WriteLine ("Patching module " + mdef.Name + " (" + mdef.Types.Count + " type[s]) ...");
225 foreach (TypeDefinition tdef in mdef.Types) {
226 NameNormalizer.CheckNames (tdef);
227 }
228 }
229 } catch (Exception e) {
230 Console.WriteLine ();
231 Console.WriteLine ("Unable to patch the assembly : ");
232 Console.WriteLine (e.ToString ());
233 return;
234 }
235 Console.WriteLine ("Finalizing patches...");
236
237 try {
238 NameNormalizer.FinalizeNormalizing ();
239 } catch (Exception e) {
240 Console.WriteLine ();
241 Console.WriteLine ("Unable to finalize patching : ");
242 Console.WriteLine (e.ToString ());
243 return;
244 }
245 NameNormalizer.clnamestomod.Clear ();
246 NameNormalizer.vclasses.Clear ();
247 Console.WriteLine ("Saving the patched assembly ...");
248
249 try {
250 input.Write (dllPath);
251 } catch (Exception e) {
252 Console.WriteLine ();
253 Console.WriteLine ("Unable to save the assembly : ");
254 Console.WriteLine (e.ToString ());
255 return;
256 }
257
258 Console.WriteLine ();
259 Console.WriteLine ("Success.");
260 }
261 }
262}
Note: See TracBrowser for help on using the repository browser.