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 |
|
---|
11 | class NamePatcher
|
---|
12 | {
|
---|
13 | static void DrawUsage ()
|
---|
14 | {
|
---|
15 | Console.WriteLine ("Usage :");
|
---|
16 | Console.WriteLine ("NamePatcher [input file] Patches an Assembly and creates a backup of the original file.");
|
---|
17 | }
|
---|
18 |
|
---|
19 | static bool TryArgs (string[] args)
|
---|
20 | {
|
---|
21 | return args.Length == 1 && (File.Exists (args [0]));
|
---|
22 | }
|
---|
23 |
|
---|
24 | static bool makeBackup (string input)
|
---|
25 | {
|
---|
26 | string backupFile_base = input + ".bck";
|
---|
27 | string backupFile = backupFile_base;
|
---|
28 | int backupIndex = 0;
|
---|
29 | while (File.Exists(backupFile)) {
|
---|
30 | backupFile = backupFile_base + "." + (++backupIndex);
|
---|
31 | if (backupIndex > 10) {
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | try {
|
---|
36 | File.Copy (input, backupFile);
|
---|
37 | } catch (Exception e) {
|
---|
38 | Console.WriteLine ("Unable to create backup file : ");
|
---|
39 | Console.WriteLine (e.ToString ());
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | static DefaultAssemblyResolver getAssemblyResolver (string path)
|
---|
46 | {
|
---|
47 | DefaultAssemblyResolver resolver = null;
|
---|
48 | int lastSlash = path.LastIndexOf ("\\");
|
---|
49 | if (lastSlash == -1)
|
---|
50 | lastSlash = path.LastIndexOf ("/");
|
---|
51 | if (lastSlash != -1) {
|
---|
52 | string inputPath = path.Substring (0, lastSlash);
|
---|
53 | resolver = new DefaultAssemblyResolver ();
|
---|
54 | resolver.AddSearchDirectory (inputPath);
|
---|
55 | }
|
---|
56 | return resolver;
|
---|
57 | }
|
---|
58 |
|
---|
59 | static void Main (string[] args)
|
---|
60 | {
|
---|
61 | Console.WriteLine ("NamePatcher for 7dtd's Assembly-CSharp.dll [by DerPopo, modified by Alloc]");
|
---|
62 | if (!TryArgs (args)) {
|
---|
63 | DrawUsage ();
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | string dllPath = args [0];
|
---|
67 |
|
---|
68 | if (!makeBackup (dllPath)) {
|
---|
69 | Console.WriteLine ("Could not create a backup file (maybe too many old backups?)");
|
---|
70 | return;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | AssemblyDefinition input;
|
---|
75 | try {
|
---|
76 | input = AssemblyDefinition.ReadAssembly (dllPath, new ReaderParameters { AssemblyResolver = getAssemblyResolver(dllPath), });
|
---|
77 | } catch (Exception e) {
|
---|
78 | Console.WriteLine ("Unable to load the input file : ");
|
---|
79 | Console.WriteLine (e.ToString ());
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | Console.WriteLine ();
|
---|
83 | Console.WriteLine ("Patching assembly " + dllPath + " (" + input.Modules.Count + " module[s]) ...");
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | ManualPatches.applyManualPatches (input.MainModule);
|
---|
88 |
|
---|
89 |
|
---|
90 | try {
|
---|
91 | foreach (ModuleDefinition mdef in input.Modules) {
|
---|
92 | Console.WriteLine ("Patching module " + mdef.Name + " (" + mdef.Types.Count + " type[s]) ...");
|
---|
93 | foreach (TypeDefinition tdef in mdef.Types) {
|
---|
94 | NameNormalizer.CheckNames (tdef);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | } catch (Exception e) {
|
---|
98 | Console.WriteLine ();
|
---|
99 | Console.WriteLine ("Unable to patch the assembly : ");
|
---|
100 | Console.WriteLine (e.ToString ());
|
---|
101 | return;
|
---|
102 | }
|
---|
103 | Console.WriteLine ("Finalizing patches...");
|
---|
104 |
|
---|
105 | try {
|
---|
106 | NameNormalizer.FinalizeNormalizing ();
|
---|
107 | } catch (Exception e) {
|
---|
108 | Console.WriteLine ();
|
---|
109 | Console.WriteLine ("Unable to finalize patching : ");
|
---|
110 | Console.WriteLine (e.ToString ());
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | NameNormalizer.clnamestomod.Clear ();
|
---|
114 | NameNormalizer.vclasses.Clear ();
|
---|
115 | Console.WriteLine ("Saving the patched assembly ...");
|
---|
116 |
|
---|
117 | try {
|
---|
118 | input.Write (dllPath);
|
---|
119 | } catch (Exception e) {
|
---|
120 | Console.WriteLine ();
|
---|
121 | Console.WriteLine ("Unable to save the assembly : ");
|
---|
122 | Console.WriteLine (e.ToString ());
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | Console.WriteLine ();
|
---|
127 | Console.WriteLine ("Success.");
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|