Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mdbrebase.cs « mdbrebase « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: caed4813d8cc7dd37d013c54a2d7338b98f0e9f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

using Mono.Cecil;
using Mono.Cecil.Mdb;
using Mono.Options;

namespace Mono.MdbRebase
{

class Settings
{
	public string OutputDirectory { get; set; }
	public string InputPattern { get; set; }
	public string OutputPattern { get; set; }

	public bool Validate ()
	{
		return InputPattern != null && OutputPattern != null && !OutputPattern.StartsWith (InputPattern);
	}

	public string Replace (string input)
	{
		if (input.StartsWith (InputPattern))
			return input.Replace (InputPattern, OutputPattern);
		return input;
	}
}

class MdbRebase
{
	Settings settings;

	public MdbRebase (Settings settings)
	{
		this.settings = settings;
	}

	public void RewriteMdbFile (string inputAssembly)
	{
		var assemblyName = new FileInfo (inputAssembly).Name;
	
		var readParams = new ReaderParameters () {
			SymbolReaderProvider = new MdbReaderProvider (),
			ReadSymbols = true,
		};

		var assembly = AssemblyDefinition.ReadAssembly (inputAssembly, readParams);

		foreach (var m in assembly.Modules) {
			foreach (var t in m.Types) {
				ProcessType (t);
				foreach (var inner in t.NestedTypes)
					ProcessType (inner);
			}
		}

		var writeParms = new WriterParameters () {
			SymbolWriterProvider = new MdbWriterProvider (),
			WriteSymbols = true,
		};
	
		var tmpdir = Path.GetTempPath ();
		var tmpAsm = tmpdir + assemblyName;
		string finalMdb = inputAssembly + ".mdb";
		if (settings.OutputDirectory != null)
			finalMdb = Path.Combine (settings.OutputDirectory, assemblyName) + ".mdb";

		assembly.Write (tmpAsm, writeParms);	
		Console.WriteLine (tmpAsm);
		File.Delete (tmpAsm);
		File.Delete (finalMdb);
		Console.WriteLine ("Moving {0} to {1}", tmpAsm + ".mdb", finalMdb);
		new FileInfo (tmpAsm + ".mdb").MoveTo (finalMdb);
	}

	void ProcessMethod (MethodDefinition m)
	{
		foreach (var i in m.Body.Instructions) {
			if (i.SequencePoint != null)
				i.SequencePoint.Document.Url = settings.Replace (i.SequencePoint.Document.Url);
		}
	}

	void ProcessType (TypeDefinition t)
	{
		foreach (var m in t.Methods)
			ProcessMethod (m);
	}

}

class Driver {
	static void Usage (OptionSet options)
	{
		Console.WriteLine (@"Usage: mdbrebase [options] <ASSEMBLY_TO_FIX>");
		if (options != null) {
			Console.WriteLine ();
			Console.WriteLine ("Available options:");
			options.WriteOptionDescriptions (Console.Out);
		}
		Console.WriteLine ();
		
		Environment.Exit (-1);
	}

	static int Main (string[] args) {
		var s = new Settings ();
		bool showHelp = false;

		var p = new OptionSet () {
			{ "d=|output=",  "Output directory to the mdb file, replace existing one if ommited", v => s.OutputDirectory = v },
			{ "i=|input-pattern=", "Input pattern to replace (must not be a prefix to output-pattern)(required)", v => s.InputPattern = v },
			{ "o=|output-pattern=", "Output pattern to replace (required)", v => s.OutputPattern = v },
			{ "h|?|help", v => showHelp = true },
		};

		List <string> extra = p.Parse (args);

		if (showHelp || extra.Count < 1 || !s.Validate ())
			Usage (p);

		var m = new MdbRebase (s);
		foreach (var a in extra)
			m.RewriteMdbFile (a);
		return 0;

	}
}
}