Bump corefx
[mono.git] / mcs / tools / cil-stringreplacer / cil-stringreplacer.cs
1 //
2 // cil-stringreplacer.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2016 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using Mono.Cecil;
32 using Mono.Cecil.Cil;
33 using Mono.Options;
34 using System.Collections.Generic;
35
36 public class Program
37 {
38         class CmdOptions
39         {
40                 public bool ShowHelp { get; set; }
41                 public bool Verbose { get; set; }
42                 public List<string> ResourcesStrings { get; }
43                 public string ILFile { get; set; }
44
45                 public CmdOptions ()
46                 {
47                         ResourcesStrings = new List<string> ();
48                 }
49         }
50
51         public static int Main (string[] args)
52         {
53                 var options = new CmdOptions ();
54
55                 var p = new OptionSet () {
56                         { "r|resourcestrings=", "File with string resource in key=value format",
57                                 v => options.ResourcesStrings.Add (v) },
58                         { "h|help",  "Display available options",
59                                 v => options.ShowHelp = v != null },
60                         { "v|verbose",  "Use verbose output",
61                                 v => options.Verbose = v != null },
62                         { "ilreplace=", "File with IL code to be used instead",
63                                 v => options.ILFile = v },
64                 };
65
66                 List<string> extra;
67                 try {
68                         extra = p.Parse (args);
69                 }
70                 catch (OptionException e) {
71                         Console.WriteLine (e.Message);
72                         Console.WriteLine ("Try 'cil-stringreplacer -help' for more information.");
73                         return 1;
74                 }
75
76                 if (options.ShowHelp) {
77                         ShowHelp (p);
78                         return 0;
79                 }
80
81                 if (extra.Count != 1) {
82                         ShowHelp (p);
83                         return 2;
84                 }
85
86                 var resourcesStrings = new Dictionary<string, string> ();
87                 if (!LoadGetResourceStrings (resourcesStrings, options))
88                         return 3;
89
90                 RewriteAssembly (extra [0], resourcesStrings, options);
91
92                 return 0;
93         }
94
95         static void ShowHelp (OptionSet p)
96         {
97                 Console.WriteLine ("Usage: cil-stringreplacer [options] assembly");
98                 Console.WriteLine ("Rewrites all occurences of string keys with their values from string resource file");
99                 Console.WriteLine ();
100                 Console.WriteLine ("Options:");
101                 p.WriteOptionDescriptions (Console.Out);
102         }
103
104         static void RewriteAssembly (string assemblyLocation, Dictionary<string, string> resourcesStrings, CmdOptions options)
105         {
106                 var methods = new Dictionary<string, MethodBody> (StringComparer.Ordinal);
107                 if (options.ILFile != null) {
108                         var rp = new ReaderParameters {
109                                 InMemory = true
110                         };
111
112                         using (var module = ModuleDefinition.ReadModule (options.ILFile,rp)) {
113                                 foreach (var type in module.GetTypes ()) {
114                                         foreach (var method in type.Methods) {
115                                                 if (!method.HasBody)
116                                                         continue;
117
118                                                 methods.Add (method.FullName, method.Body);
119                                         }
120                                 }
121                         }
122                 }
123
124                 var readerParameters = new ReaderParameters {
125                         ReadSymbols = true,
126                         ReadWrite = true,
127                         SymbolReaderProvider = new DefaultSymbolReaderProvider (false)
128                 };
129
130                 using (var assembly = AssemblyDefinition.ReadAssembly (assemblyLocation, readerParameters)) {
131                         foreach (var module in assembly.Modules) {
132                                 foreach (var type in module.GetTypes ()) {
133                                         foreach (var method in type.Methods) {
134                                                 if (!method.HasBody)
135                                                         continue;
136
137                                                 MethodBody newBody;
138                                                 if (methods.TryGetValue (method.FullName, out newBody)) {
139                                                         var mbody = method.Body;
140                                                         mbody.Instructions.Clear ();
141                                                         foreach (var instr in newBody.Instructions) {
142                                                                 switch (instr.OpCode.OperandType) {
143                                                                 case OperandType.InlineType:
144                                                                         var tr = (TypeReference)instr.Operand;
145                                                                         foreach (var t in method.GenericParameters) {
146                                                                                 if (tr.FullName == t.FullName) {
147                                                                                         instr.Operand = t;
148                                                                                         break;
149                                                                                 }
150                                                                         }
151
152                                                                         break;
153                                                                 }
154
155                                                                 mbody.Instructions.Add (instr);
156                                                         }
157
158                                                         method.Body.Variables.Clear ();
159                                                         foreach (var variable in newBody.Variables) {
160                                                                 mbody.Variables.Add (variable);
161                                                         }
162                                                 }
163
164                                                 foreach (var instr in method.Body.Instructions) {
165                                                         if (instr.OpCode != OpCodes.Ldstr)
166                                                                 continue;
167
168                                                         string value;
169                                                         if (resourcesStrings.TryGetValue ((string)instr.Operand, out value)) {
170                                                                 if (options.Verbose) {
171                                                                         Console.WriteLine ($"Replacing '{instr.Operand}' with '{value}'");
172                                                                 }
173
174                                                                 instr.Operand = value;
175                                                         }
176                                                 }
177                                         }
178                                 }
179                         }
180
181                         var writerParameters = new WriterParameters () {
182                                 WriteSymbols = assembly.MainModule.HasSymbols
183                         };
184
185                         assembly.Write (writerParameters);
186                 }
187         }
188
189         static bool LoadGetResourceStrings (Dictionary<string, string> resourcesStrings, CmdOptions options)
190         {
191                 foreach (var fileName in options.ResourcesStrings) {
192                         if (!File.Exists (fileName)) {
193                                 Console.Error.WriteLine ($"Error reading resource file '{fileName}'");
194                                 return false;
195                         }
196
197                         foreach (var l in File.ReadLines (fileName)) {
198                                 var line = l.Trim ();
199                                 if (line.Length == 0 || line [0] == '#' || line [0] == ';')
200                                         continue;
201
202                                 var epos = line.IndexOf ('=');
203                                 if (epos < 0)
204                                         continue;
205
206                                 var key = line.Substring (0, epos).Trim ();
207                                 var value = line.Substring (epos + 1).Trim ();
208
209                                 resourcesStrings [key] = value;
210                         }
211                 }
212
213                 return true;
214         }
215 }