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

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

server fixes

File size: 8.4 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 ("World").Fields) {
61 TypeReference fdType = fd.FieldType;
62 if (fdType.FullName.Equals ("System.UInt64")) {
63 Console.WriteLine ("Renaming world field -> gameTime");
64 NameNormalizer.setName (fd, "gameTime");
65 }
66 }
67
68 foreach (FieldDefinition fd in mainModule.GetType ("GameManager").Fields) {
69 TypeReference fdType = fd.FieldType;
70 if (fdType.FullName.Equals ("ConnectionManager")) {
71 Console.WriteLine ("Renaming and making public GameMananger field -> connectionManager");
72 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
73 NameNormalizer.setName (fd, "connectionManager");
74 }
75 }
76
77 foreach (FieldDefinition fd in mainModule.GetType ("ConnectionManager").Fields) {
78 TypeReference fdType = fd.FieldType;
79 if (fdType.FullName.Contains ("Dictionary") && fdType.FullName.Contains ("ClientInfo")) {
80 Console.WriteLine ("Renaming and making public ConnectionManager field -> connectedClients");
81 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
82 NameNormalizer.setName (fd, "connectedClients");
83 }
84 if (fdType.FullName.Contains ("Dictionary") && fdType.FullName.Contains ("System.Int32,System.Int32")) {
85 Console.WriteLine ("Renaming and making public ConnectionManager field -> mapClientToEntity");
86 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
87 NameNormalizer.setName (fd, "mapClientToEntity");
88 }
89 }
90
91 string consoleTypeName = string.Empty;
92 TypeDefinition typeTelnetServer = mainModule.GetType ("NetTelnetServer");
93 foreach (FieldDefinition fd in typeTelnetServer.Fields) {
94 if (NameNormalizer.makeValidName (fd.FieldType.Name) != null) {
95 Console.WriteLine ("Renaming console class -> ConsoleSdtd");
96 consoleTypeName = fd.FieldType.Name;
97 NameNormalizer.setName (fd.FieldType.Resolve (), "ConsoleSdtd");
98 NameNormalizer.setName (fd, "console");
99 }
100 }
101
102 if (consoleTypeName.Length > 0) {
103 TypeDefinition typeConsole = mainModule.GetType (consoleTypeName);
104 string consoleCommandTypeName = string.Empty;
105 foreach (MethodDefinition md in typeConsole.Methods) {
106 if (!md.IsConstructor) {
107 if (md.Parameters.Count == 3 && md.Parameters [0].ParameterType.Name.Equals ("NetworkPlayer")) {
108 Console.WriteLine ("Renaming console method -> ExecuteCmdFromClient");
109 NameNormalizer.setName (md, "ExecuteCmdFromClient");
110 }
111 }
112 }
113 foreach (FieldDefinition fd in typeConsole.Fields) {
114 TypeReference fdType = fd.FieldType;
115 if (fdType.FullName.Contains ("Generic.List")) {
116 if (fdType.IsGenericInstance) {
117 GenericInstanceType genType = (GenericInstanceType)fdType;
118 TypeReference genRef = genType.GenericArguments [0];
119 if (genRef.Name.Length < 2) {
120 Console.WriteLine ("Renaming console command class -> ConsoleCommand");
121 NameNormalizer.setName (genRef.Resolve (), "ConsoleCommand");
122 NameNormalizer.setName (fd, "commands");
123 consoleCommandTypeName = genRef.Name;
124 }
125 }
126 }
127 if (fdType.FullName.Equals ("GameManager")) {
128 Console.WriteLine ("Renaming and making public console field -> gameManager");
129 fd.Attributes = fd.Attributes & (~Mono.Cecil.FieldAttributes.Private) | Mono.Cecil.FieldAttributes.Public;
130 NameNormalizer.setName (fd, "gameManager");
131 }
132 }
133
134 if (consoleCommandTypeName.Length > 0) {
135 foreach (MethodDefinition md in typeConsole.Methods) {
136 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.Name.Equals (consoleCommandTypeName)) {
137 Console.WriteLine ("Renaming console method -> AddCommand");
138 NameNormalizer.setName (md, "AddCommand");
139 }
140 }
141
142 TypeDefinition typeConsoleCommand = mainModule.GetType (consoleCommandTypeName);
143 foreach (MethodDefinition md in typeConsoleCommand.Methods) {
144 if (!md.IsConstructor) {
145 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.Name.Equals (consoleTypeName)) {
146 Console.WriteLine ("Renaming console command method -> Help");
147 NameNormalizer.setName (md, "Help");
148 }
149 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("Int32")) {
150 Console.WriteLine ("Renaming console command method -> Timeout");
151 NameNormalizer.setName (md, "Timeout");
152 }
153 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("String[]")) {
154 Console.WriteLine ("Renaming console command method -> Names");
155 NameNormalizer.setName (md, "Names");
156 }
157 if (md.Parameters.Count == 0 && md.ReturnType.Name.Equals ("String")) {
158 Console.WriteLine ("Renaming console command method -> Description");
159 NameNormalizer.setName (md, "Description");
160 }
161 if (md.Parameters.Count == 1 && md.Parameters [0].ParameterType.IsArray) {
162 Console.WriteLine ("Renaming console command method -> Run");
163 NameNormalizer.setName (md, "Run");
164 }
165 }
166 }
167 }
168 }
169 }
170
171 static void Main (string[] args)
172 {
173 Console.WriteLine ("NamePatcher for 7dtd's Assembly-CSharp.dll [by DerPopo, modified by Alloc] for Dedi build 320404");
174 if (!TryArgs (args)) {
175 DrawUsage ();
176 return;
177 }
178 string dllPath = args [0];
179
180 if (!makeBackup (dllPath)) {
181 Console.WriteLine ("Could not create a backup file (maybe too many old backups?)");
182 return;
183 }
184
185
186 AssemblyDefinition input;
187 try {
188 input = AssemblyDefinition.ReadAssembly (dllPath, new ReaderParameters { AssemblyResolver = getAssemblyResolver(dllPath), });
189 } catch (Exception e) {
190 Console.WriteLine ("Unable to load the input file : ");
191 Console.WriteLine (e.ToString ());
192 return;
193 }
194 Console.WriteLine ();
195 Console.WriteLine ("Patching assembly " + dllPath + " (" + input.Modules.Count + " module[s]) ...");
196
197
198
199 applyManualPatches (input.MainModule);
200
201
202 try {
203 foreach (ModuleDefinition mdef in input.Modules) {
204 Console.WriteLine ("Patching module " + mdef.Name + " (" + mdef.Types.Count + " type[s]) ...");
205 foreach (TypeDefinition tdef in mdef.Types) {
206 NameNormalizer.CheckNames (tdef);
207 }
208 }
209 } catch (Exception e) {
210 Console.WriteLine ();
211 Console.WriteLine ("Unable to patch the assembly : ");
212 Console.WriteLine (e.ToString ());
213 return;
214 }
215 Console.WriteLine ("Finalizing patches...");
216
217 try {
218 NameNormalizer.FinalizeNormalizing ();
219 } catch (Exception e) {
220 Console.WriteLine ();
221 Console.WriteLine ("Unable to finalize patching : ");
222 Console.WriteLine (e.ToString ());
223 return;
224 }
225 NameNormalizer.clnamestomod.Clear ();
226 NameNormalizer.vclasses.Clear ();
227 Console.WriteLine ("Saving the patched assembly ...");
228
229 try {
230 input.Write (dllPath);
231 } catch (Exception e) {
232 Console.WriteLine ();
233 Console.WriteLine ("Unable to save the assembly : ");
234 Console.WriteLine (e.ToString ());
235 return;
236 }
237
238 Console.WriteLine ();
239 Console.WriteLine ("Success.");
240 }
241 }
242}
Note: See TracBrowser for help on using the repository browser.