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