Merge pull request #4248 from Unity-Technologies/boehm-gc-alloc-fixed
[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
44                 public CmdOptions ()
45                 {
46                         ResourcesStrings = new List<string> ();
47                 }
48         }
49
50         public static int Main (string[] args)
51         {
52                 var options = new CmdOptions ();
53
54                 var p = new OptionSet () {
55                         { "r|resourcestrings=", "File with string resource in key=value format",
56                                 v => options.ResourcesStrings.Add (v) },
57                         { "h|help",  "Display available options", 
58                                 v => options.ShowHelp = v != null },
59                         { "v|verbose",  "Use verbose output", 
60                                 v => options.Verbose = v != null },                     
61                 };
62
63                 List<string> extra;
64                 try {
65                         extra = p.Parse (args);
66                 }
67                 catch (OptionException e) {
68                         Console.WriteLine (e.Message);
69                         Console.WriteLine ("Try 'cil-stringreplacer -help' for more information.");
70                         return 1;
71                 }
72
73                 if (options.ShowHelp) {
74                         ShowHelp (p);
75                         return 0;
76                 }
77
78                 if (extra.Count != 1) {
79                         ShowHelp (p);
80                         return 2;
81                 }
82
83                 var resourcesStrings = new Dictionary<string, string> ();
84                 if (!LoadGetResourceStrings (resourcesStrings, options))
85                         return 3;
86
87                 RewriteAssembly (extra [0], resourcesStrings, options);
88
89                 return 0;
90         }
91
92         static void ShowHelp (OptionSet p)
93         {
94                 Console.WriteLine ("Usage: cil-stringreplacer [options] assembly");
95                 Console.WriteLine ("Rewrites all occurences of string keys with their values from string resource file");
96                 Console.WriteLine ();
97                 Console.WriteLine ("Options:");
98                 p.WriteOptionDescriptions (Console.Out);
99         }
100
101         static void RewriteAssembly (string assemblyLocation, Dictionary<string, string> resourcesStrings, CmdOptions options)
102         {
103                 var debugSymbols = Path.ChangeExtension (assemblyLocation, "pdb");
104                 var useDebugSymbols = File.Exists (debugSymbols);
105
106                 var readerParameters = new ReaderParameters {
107                         ReadWrite = true,
108                 };
109
110                 if (useDebugSymbols) {
111                         readerParameters.SymbolReaderProvider = new PortablePdbReaderProvider ();
112                 }
113
114                 using (var assembly = AssemblyDefinition.ReadAssembly (assemblyLocation, readerParameters)) {
115                         foreach (var module in assembly.Modules) {
116                                 foreach (var type in module.GetTypes ()) {
117                                         foreach (var method in type.Methods) {
118                                                 if (!method.HasBody)
119                                                         continue;
120
121                                                 foreach (var instr in method.Body.Instructions) {
122                                                         if (instr.OpCode != OpCodes.Ldstr)
123                                                                 continue;
124
125                                                         string value;
126                                                         if (resourcesStrings.TryGetValue ((string)instr.Operand, out value)) {
127                                                                 if (options.Verbose) {
128                                                                         Console.WriteLine ($"Replacing '{instr.Operand}' with '{value}'");
129                                                                 }
130
131                                                                 instr.Operand = value;
132                                                         }
133                                                 }
134                                         }
135                                 }
136                         }
137
138                         var writerParameters = new WriterParameters ();
139
140                         if (useDebugSymbols) {
141                                 writerParameters.SymbolWriterProvider = new PortablePdbWriterProvider ();
142                         }
143
144                         assembly.Write (writerParameters);
145                 }
146         }
147
148         static bool LoadGetResourceStrings (Dictionary<string, string> resourcesStrings, CmdOptions options)
149         {
150                 foreach (var fileName in options.ResourcesStrings) {
151                         if (!File.Exists (fileName)) {
152                                 Console.Error.WriteLine ($"Error reading resource file '{fileName}'");
153                                 return false;
154                         }
155
156                         foreach (var l in File.ReadLines (fileName)) {
157                                 var line = l.Trim ();
158                                 if (line.Length == 0 || line [0] == '#' || line [0] == ';')
159                                         continue;
160
161                                 var epos = line.IndexOf ('=');
162                                 if (epos < 0)
163                                         continue;
164
165                                 var key = line.Substring (0, epos).Trim ();
166                                 var value = line.Substring (epos + 1).Trim ();
167
168                                 resourcesStrings [key] = value;
169                         }
170                 }
171
172                 return true;
173         }
174 }