Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/AssemblyFactory.cs

raw
1 //
2 // AssemblyFactory.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // (C) 2005 Jb Evain
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 namespace Mono.Cecil {
30
31 using System;
32 using System.IO;
33 using SR = System.Reflection;
34
35 using Mono.Cecil.Binary;
36
37 internal sealed class AssemblyFactory {
38
39 AssemblyFactory ()
40 {
41 }
42
43 static AssemblyDefinition GetAssembly (ImageReader irv, bool manifestOnly)
44 {
45 StructureReader srv = new StructureReader (irv, manifestOnly);
46 AssemblyDefinition asm = new AssemblyDefinition (
47 new AssemblyNameDefinition (), srv);
48
49 asm.Accept (srv);
50 return asm;
51 }
52
53 static AssemblyDefinition GetAssembly (ImageReader reader)
54 {
55 return GetAssembly (reader, false);
56 }
57
58 static AssemblyDefinition GetAssemblyManifest (ImageReader reader)
59 {
60 return GetAssembly (reader, true);
61 }
62
63 public static AssemblyDefinition GetAssembly (string file)
64 {
65 return GetAssembly (ImageReader.Read (file));
66 }
67
68 public static AssemblyDefinition GetAssembly (byte [] assembly)
69 {
70 return GetAssembly (ImageReader.Read (assembly));
71 }
72
73 public static AssemblyDefinition GetAssembly (Stream stream)
74 {
75 return GetAssembly (ImageReader.Read (stream));
76 }
77
78 public static AssemblyDefinition GetAssemblyManifest (string file)
79 {
80 return GetAssemblyManifest (ImageReader.Read (file));
81 }
82
83 public static AssemblyDefinition GetAssemblyManifest (byte [] assembly)
84 {
85 return GetAssemblyManifest (ImageReader.Read (assembly));
86 }
87
88 public static AssemblyDefinition GetAssemblyManifest (Stream stream)
89 {
90 return GetAssemblyManifest (ImageReader.Read (stream));
91 }
92
93 static TargetRuntime CurrentRuntime ()
94 {
95 Version corlib = typeof (object).Assembly.GetName ().Version;
96
97 switch (corlib.Major) {
98 case 1:
99 return corlib.Minor == 0 ? TargetRuntime.NET_1_0 : TargetRuntime.NET_1_1;
100 case 2:
101 return TargetRuntime.NET_2_0;
102 case 4:
103 return TargetRuntime.NET_4_0;
104 default:
105 throw new NotSupportedException ();
106 }
107 }
108
109 public static AssemblyDefinition DefineAssembly (string name, AssemblyKind kind)
110 {
111 return DefineAssembly (name, name, CurrentRuntime (), kind);
112 }
113
114 public static AssemblyDefinition DefineAssembly (string name, TargetRuntime rt, AssemblyKind kind)
115 {
116 return DefineAssembly (name, name, rt, kind);
117 }
118
119 public static AssemblyDefinition DefineAssembly (string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
120 {
121 AssemblyNameDefinition asmName = new AssemblyNameDefinition ();
122 asmName.Name = assemblyName;
123 AssemblyDefinition asm = new AssemblyDefinition (asmName);
124 asm.Runtime = rt;
125 asm.Kind = kind;
126 ModuleDefinition main = new ModuleDefinition (moduleName, asm, true);
127 asm.Modules.Add (main);
128 return asm;
129 }
130
131 static void WriteAssembly (AssemblyDefinition asm, BinaryWriter bw)
132 {
133 asm.Accept (new StructureWriter (asm, bw));
134 }
135
136 public static void SaveAssembly (AssemblyDefinition asm, string file)
137 {
138 using (FileStream fs = new FileStream (
139 file, FileMode.Create, FileAccess.Write, FileShare.None)) {
140
141 SaveAssembly (asm, fs);
142 asm.MainModule.Image.SetFileInfo (new FileInfo (file));
143 }
144 }
145
146 public static void SaveAssembly (AssemblyDefinition asm, out byte [] assembly)
147 {
148 MemoryBinaryWriter bw = new MemoryBinaryWriter ();
149 SaveAssembly (asm, bw.BaseStream);
150 assembly = bw.ToArray ();
151 }
152
153 public static void SaveAssembly (AssemblyDefinition asm, Stream stream)
154 {
155 BinaryWriter bw = new BinaryWriter (stream);
156 try {
157 WriteAssembly (asm, bw);
158 } finally {
159 bw.Close ();
160 }
161
162 foreach (ModuleDefinition module in asm.Modules)
163 if (module.Controller.Writer.SaveSymbols)
164 module.Controller.Writer.WriteSymbols (module);
165 }
166
167 #if !CF_1_0 && !CF_2_0
168 public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm, AppDomain domain)
169 {
170 using (MemoryBinaryWriter writer = new MemoryBinaryWriter ()) {
171
172 WriteAssembly (asm, writer);
173 return domain.Load (writer.ToArray ());
174 }
175 }
176
177 public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm)
178 {
179 return CreateReflectionAssembly (asm, AppDomain.CurrentDomain);
180 }
181 #endif
182 }
183 }