2 using System.Reflection;
3 using System.Collections;
4 using System.Collections.Generic;
7 using Mono.CompilerServices.SymbolWriter;
10 namespace Mono.MdbRebase
15 public string OutputDirectory { get; set; }
16 public string InputPattern { get; set; }
17 public string OutputPattern { get; set; }
19 public bool Validate ()
21 return InputPattern != null && OutputPattern != null;
24 public string Replace (string input)
26 if (input.StartsWith (InputPattern))
27 return input.Replace (InputPattern, OutputPattern);
36 public MdbRebase (Settings settings)
38 this.settings = settings;
41 public void RewriteMdbFile (string inputFile)
43 Console.WriteLine ("Processing {0}", inputFile);
44 var input = MonoSymbolFile.ReadSymbolFile (inputFile);
46 var output = new MonoSymbolFile ();
48 foreach (var s in input.Sources) {
49 s.FileName = settings.Replace (s.FileName);
53 foreach (var cu in input.CompileUnits) {
55 output.AddCompileUnit (cu);
58 foreach (var m in input.Methods) {
64 var mdbName = new FileInfo (inputFile).Name;
65 var tmpMdb = Path.Combine (Path.GetTempPath (), mdbName);
66 var finalMdb = inputFile;
67 if (settings.OutputDirectory != null)
68 finalMdb = Path.Combine (settings.OutputDirectory, mdbName);
70 using (var stream = new FileStream (tmpMdb, FileMode.Create)) {
71 output.CreateSymbolFile (input.Guid, stream);
75 File.Delete (finalMdb);
76 new FileInfo (tmpMdb).MoveTo (finalMdb);
81 static void Usage (OptionSet options)
83 Console.WriteLine (@"Usage: mdbrebase [options] <ASSEMBLY_TO_FIX>");
84 if (options != null) {
86 Console.WriteLine ("Available options:");
87 options.WriteOptionDescriptions (Console.Out);
91 Environment.Exit (-1);
94 static int Main (string[] args) {
95 var s = new Settings ();
96 bool showHelp = false;
98 var p = new OptionSet () {
99 { "d=|output=", "Output directory to the mdb file, replace existing one if ommited", v => s.OutputDirectory = v },
100 { "i=|input-pattern=", "Input pattern to replace (must not be a prefix to output-pattern)(required)", v => s.InputPattern = v },
101 { "o=|output-pattern=", "Output pattern to replace (required)", v => s.OutputPattern = v },
102 { "h|?|help", v => showHelp = true },
105 List <string> extra = p.Parse (args);
107 if (showHelp || extra.Count < 1 || !s.Validate ())
110 var m = new MdbRebase (s);
111 foreach (var a in extra)
112 m.RewriteMdbFile (a);