| 1 |
/* |
| 2 |
Copyright (C) 2008-2013 Jeroen Frijters |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely, subject to the following restrictions: |
| 11 |
|
| 12 |
1. The origin of this software must not be misrepresented; you must not |
| 13 |
claim that you wrote the original software. If you use this software |
| 14 |
in a product, an acknowledgment in the product documentation would be |
| 15 |
appreciated but is not required. |
| 16 |
2. Altered source versions must be plainly marked as such, and must not be |
| 17 |
misrepresented as being the original software. |
| 18 |
3. This notice may not be removed or altered from any source distribution. |
| 19 |
|
| 20 |
Jeroen Frijters |
| 21 |
[email protected] |
| 22 |
|
| 23 |
*/ |
| 24 |
using System; |
| 25 |
using System.Collections.Generic; |
| 26 |
using System.Configuration.Assemblies; |
| 27 |
using System.IO; |
| 28 |
using System.Diagnostics; |
| 29 |
using System.Globalization; |
| 30 |
using System.Resources; |
| 31 |
using System.Security.Cryptography; |
| 32 |
using System.Security; |
| 33 |
using IKVM.Reflection.Metadata; |
| 34 |
using IKVM.Reflection.Impl; |
| 35 |
using IKVM.Reflection.Writer; |
| 36 |
|
| 37 |
namespace IKVM.Reflection.Emit |
| 38 |
{ |
| 39 |
public sealed class AssemblyBuilder : Assembly |
| 40 |
{ |
| 41 |
private readonly string name; |
| 42 |
private ushort majorVersion; |
| 43 |
private ushort minorVersion; |
| 44 |
private ushort buildVersion; |
| 45 |
private ushort revisionVersion; |
| 46 |
private string culture; |
| 47 |
private AssemblyNameFlags flags; |
| 48 |
private AssemblyHashAlgorithm hashAlgorithm; |
| 49 |
private StrongNameKeyPair keyPair; |
| 50 |
private byte[] publicKey; |
| 51 |
internal readonly string dir; |
| 52 |
private PEFileKinds fileKind = PEFileKinds.Dll; |
| 53 |
private MethodInfo entryPoint; |
| 54 |
private VersionInfo versionInfo; |
| 55 |
private byte[] win32icon; |
| 56 |
private byte[] win32manifest; |
| 57 |
private byte[] win32resources; |
| 58 |
private string imageRuntimeVersion; |
| 59 |
internal int mdStreamVersion = 0x20000; |
| 60 |
private Module pseudoManifestModule; |
| 61 |
private readonly List<ResourceFile> resourceFiles = new List<ResourceFile>(); |
| 62 |
private readonly List<ModuleBuilder> modules = new List<ModuleBuilder>(); |
| 63 |
private readonly List<Module> addedModules = new List<Module>(); |
| 64 |
private readonly List<CustomAttributeBuilder> customAttributes = new List<CustomAttributeBuilder>(); |
| 65 |
private readonly List<CustomAttributeBuilder> declarativeSecurity = new List<CustomAttributeBuilder>(); |
| 66 |
private readonly List<TypeForwarder> typeForwarders = new List<TypeForwarder>(); |
| 67 |
|
| 68 |
struct TypeForwarder |
| 69 |
{ |
| 70 |
internal readonly Type Type; |
| 71 |
internal readonly bool IncludeNested; |
| 72 |
|
| 73 |
internal TypeForwarder(Type type, bool includeNested) |
| 74 |
{ |
| 75 |
this.Type = type; |
| 76 |
this.IncludeNested = includeNested; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private struct ResourceFile |
| 81 |
{ |
| 82 |
internal string Name; |
| 83 |
internal string FileName; |
| 84 |
internal ResourceAttributes Attributes; |
| 85 |
#if !CORECLR |
| 86 |
internal ResourceWriter Writer; |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, IEnumerable<CustomAttributeBuilder> customAttributes) |
| 91 |
: base(universe) |
| 92 |
{ |
| 93 |
this.name = name.Name; |
| 94 |
SetVersionHelper(name.Version); |
| 95 |
if (!string.IsNullOrEmpty(name.Culture)) |
| 96 |
{ |
| 97 |
this.culture = name.Culture; |
| 98 |
} |
| 99 |
this.flags = name.RawFlags; |
| 100 |
this.hashAlgorithm = name.HashAlgorithm; |
| 101 |
if (this.hashAlgorithm == AssemblyHashAlgorithm.None) |
| 102 |
{ |
| 103 |
this.hashAlgorithm = AssemblyHashAlgorithm.SHA1; |
| 104 |
} |
| 105 |
this.keyPair = name.KeyPair; |
| 106 |
if (this.keyPair != null) |
| 107 |
{ |
| 108 |
this.publicKey = this.keyPair.PublicKey; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
byte[] publicKey = name.GetPublicKey(); |
| 113 |
if (publicKey != null && publicKey.Length != 0) |
| 114 |
{ |
| 115 |
this.publicKey = (byte[])publicKey.Clone(); |
| 116 |
} |
| 117 |
} |
| 118 |
this.dir = dir ?? "."; |
| 119 |
if (customAttributes != null) |
| 120 |
{ |
| 121 |
this.customAttributes.AddRange(customAttributes); |
| 122 |
} |
| 123 |
if (universe.HasMscorlib && !universe.Mscorlib.__IsMissing && universe.Mscorlib.ImageRuntimeVersion != null) |
| 124 |
{ |
| 125 |
this.imageRuntimeVersion = universe.Mscorlib.ImageRuntimeVersion; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
this.imageRuntimeVersion = typeof(object).Assembly.ImageRuntimeVersion; |
| 130 |
} |
| 131 |
universe.RegisterDynamicAssembly(this); |
| 132 |
} |
| 133 |
|
| 134 |
private void SetVersionHelper(Version version) |
| 135 |
{ |
| 136 |
if (version == null) |
| 137 |
{ |
| 138 |
majorVersion = 0; |
| 139 |
minorVersion = 0; |
| 140 |
buildVersion = 0; |
| 141 |
revisionVersion = 0; |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
majorVersion = (ushort)version.Major; |
| 146 |
minorVersion = (ushort)version.Minor; |
| 147 |
buildVersion = version.Build == -1 ? (ushort)0 : (ushort)version.Build; |
| 148 |
revisionVersion = version.Revision == -1 ? (ushort)0 : (ushort)version.Revision; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private void Rename(AssemblyName oldName) |
| 153 |
{ |
| 154 |
this.fullName = null; |
| 155 |
universe.RenameAssembly(this, oldName); |
| 156 |
} |
| 157 |
|
| 158 |
public void __SetAssemblyVersion(Version version) |
| 159 |
{ |
| 160 |
AssemblyName oldName = GetName(); |
| 161 |
SetVersionHelper(version); |
| 162 |
Rename(oldName); |
| 163 |
} |
| 164 |
|
| 165 |
public void __SetAssemblyCulture(string cultureName) |
| 166 |
{ |
| 167 |
AssemblyName oldName = GetName(); |
| 168 |
this.culture = cultureName; |
| 169 |
Rename(oldName); |
| 170 |
} |
| 171 |
|
| 172 |
public void __SetAssemblyKeyPair(StrongNameKeyPair keyPair) |
| 173 |
{ |
| 174 |
AssemblyName oldName = GetName(); |
| 175 |
this.keyPair = keyPair; |
| 176 |
if (keyPair != null) |
| 177 |
{ |
| 178 |
this.publicKey = keyPair.PublicKey; |
| 179 |
} |
| 180 |
Rename(oldName); |
| 181 |
} |
| 182 |
|
| 183 |
// this is used in combination with delay signing |
| 184 |
public void __SetAssemblyPublicKey(byte[] publicKey) |
| 185 |
{ |
| 186 |
AssemblyName oldName = GetName(); |
| 187 |
this.publicKey = publicKey == null ? null : (byte[])publicKey.Clone(); |
| 188 |
Rename(oldName); |
| 189 |
} |
| 190 |
|
| 191 |
public void __SetAssemblyAlgorithmId(AssemblyHashAlgorithm hashAlgorithm) |
| 192 |
{ |
| 193 |
this.hashAlgorithm = hashAlgorithm; |
| 194 |
} |
| 195 |
|
| 196 |
[Obsolete("Use __AssemblyFlags property instead.")] |
| 197 |
public void __SetAssemblyFlags(AssemblyNameFlags flags) |
| 198 |
{ |
| 199 |
this.__AssemblyFlags = flags; |
| 200 |
} |
| 201 |
|
| 202 |
protected override AssemblyNameFlags GetAssemblyFlags() |
| 203 |
{ |
| 204 |
return flags; |
| 205 |
} |
| 206 |
|
| 207 |
public new AssemblyNameFlags __AssemblyFlags |
| 208 |
{ |
| 209 |
get { return flags; } |
| 210 |
set |
| 211 |
{ |
| 212 |
AssemblyName oldName = GetName(); |
| 213 |
this.flags = value; |
| 214 |
Rename(oldName); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
internal string Name |
| 219 |
{ |
| 220 |
get { return name; } |
| 221 |
} |
| 222 |
|
| 223 |
public override AssemblyName GetName() |
| 224 |
{ |
| 225 |
AssemblyName n = new AssemblyName(); |
| 226 |
n.Name = name; |
| 227 |
n.Version = new Version(majorVersion, minorVersion, buildVersion, revisionVersion); |
| 228 |
n.Culture = culture ?? ""; |
| 229 |
n.HashAlgorithm = hashAlgorithm; |
| 230 |
n.RawFlags = flags; |
| 231 |
n.SetPublicKey(publicKey != null ? (byte[])publicKey.Clone() : Empty<byte>.Array); |
| 232 |
n.KeyPair = keyPair; |
| 233 |
return n; |
| 234 |
} |
| 235 |
|
| 236 |
public override string Location |
| 237 |
{ |
| 238 |
get { throw new NotSupportedException(); } |
| 239 |
} |
| 240 |
|
| 241 |
public ModuleBuilder DefineDynamicModule(string name, string fileName) |
| 242 |
{ |
| 243 |
return DefineDynamicModule(name, fileName, false); |
| 244 |
} |
| 245 |
|
| 246 |
public ModuleBuilder DefineDynamicModule(string name, string fileName, bool emitSymbolInfo) |
| 247 |
{ |
| 248 |
ModuleBuilder module = new ModuleBuilder(this, name, fileName, emitSymbolInfo); |
| 249 |
modules.Add(module); |
| 250 |
return module; |
| 251 |
} |
| 252 |
|
| 253 |
public ModuleBuilder GetDynamicModule(string name) |
| 254 |
{ |
| 255 |
foreach (ModuleBuilder module in modules) |
| 256 |
{ |
| 257 |
if (module.Name == name) |
| 258 |
{ |
| 259 |
return module; |
| 260 |
} |
| 261 |
} |
| 262 |
return null; |
| 263 |
} |
| 264 |
|
| 265 |
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) |
| 266 |
{ |
| 267 |
SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute)); |
| 268 |
} |
| 269 |
|
| 270 |
public void SetCustomAttribute(CustomAttributeBuilder customBuilder) |
| 271 |
{ |
| 272 |
customAttributes.Add(customBuilder); |
| 273 |
} |
| 274 |
|
| 275 |
public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder) |
| 276 |
{ |
| 277 |
declarativeSecurity.Add(customBuilder); |
| 278 |
} |
| 279 |
|
| 280 |
public void __AddTypeForwarder(Type type) |
| 281 |
{ |
| 282 |
__AddTypeForwarder(type, true); |
| 283 |
} |
| 284 |
|
| 285 |
public void __AddTypeForwarder(Type type, bool includeNested) |
| 286 |
{ |
| 287 |
typeForwarders.Add(new TypeForwarder(type, includeNested)); |
| 288 |
} |
| 289 |
|
| 290 |
public void SetEntryPoint(MethodInfo entryMethod) |
| 291 |
{ |
| 292 |
SetEntryPoint(entryMethod, PEFileKinds.ConsoleApplication); |
| 293 |
} |
| 294 |
|
| 295 |
public void SetEntryPoint(MethodInfo entryMethod, PEFileKinds fileKind) |
| 296 |
{ |
| 297 |
this.entryPoint = entryMethod; |
| 298 |
this.fileKind = fileKind; |
| 299 |
} |
| 300 |
|
| 301 |
public void __Save(Stream stream, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine) |
| 302 |
{ |
| 303 |
if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek || stream.Position != 0) |
| 304 |
{ |
| 305 |
throw new ArgumentException("Stream must support read/write/seek and current position must be zero.", "stream"); |
| 306 |
} |
| 307 |
if (modules.Count != 1) |
| 308 |
{ |
| 309 |
throw new NotSupportedException("Saving to a stream is only supported for single module assemblies."); |
| 310 |
} |
| 311 |
SaveImpl(modules[0].fileName, stream, portableExecutableKind, imageFileMachine); |
| 312 |
} |
| 313 |
|
| 314 |
public void Save(string assemblyFileName) |
| 315 |
{ |
| 316 |
Save(assemblyFileName, PortableExecutableKinds.ILOnly, ImageFileMachine.I386); |
| 317 |
} |
| 318 |
|
| 319 |
public void Save(string assemblyFileName, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine) |
| 320 |
{ |
| 321 |
SaveImpl(assemblyFileName, null, portableExecutableKind, imageFileMachine); |
| 322 |
} |
| 323 |
|
| 324 |
private void SaveImpl(string assemblyFileName, Stream streamOrNull, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine) |
| 325 |
{ |
| 326 |
ModuleBuilder manifestModule = null; |
| 327 |
|
| 328 |
foreach (ModuleBuilder moduleBuilder in modules) |
| 329 |
{ |
| 330 |
moduleBuilder.SetIsSaved(); |
| 331 |
moduleBuilder.PopulatePropertyAndEventTables(); |
| 332 |
|
| 333 |
if (manifestModule == null |
| 334 |
&& string.Compare(moduleBuilder.fileName, assemblyFileName, StringComparison.OrdinalIgnoreCase) == 0) |
| 335 |
{ |
| 336 |
manifestModule = moduleBuilder; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (manifestModule == null) |
| 341 |
{ |
| 342 |
manifestModule = DefineDynamicModule("RefEmit_OnDiskManifestModule", assemblyFileName, false); |
| 343 |
} |
| 344 |
|
| 345 |
AssemblyTable.Record assemblyRecord = new AssemblyTable.Record(); |
| 346 |
assemblyRecord.HashAlgId = (int)hashAlgorithm; |
| 347 |
assemblyRecord.Name = manifestModule.Strings.Add(name); |
| 348 |
assemblyRecord.MajorVersion = majorVersion; |
| 349 |
assemblyRecord.MinorVersion = minorVersion; |
| 350 |
assemblyRecord.BuildNumber = buildVersion; |
| 351 |
assemblyRecord.RevisionNumber = revisionVersion; |
| 352 |
if (publicKey != null) |
| 353 |
{ |
| 354 |
assemblyRecord.PublicKey = manifestModule.Blobs.Add(ByteBuffer.Wrap(publicKey)); |
| 355 |
assemblyRecord.Flags = (int)(flags | AssemblyNameFlags.PublicKey); |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
assemblyRecord.Flags = (int)(flags & ~AssemblyNameFlags.PublicKey); |
| 360 |
} |
| 361 |
if (culture != null) |
| 362 |
{ |
| 363 |
assemblyRecord.Culture = manifestModule.Strings.Add(culture); |
| 364 |
} |
| 365 |
manifestModule.AssemblyTable.AddRecord(assemblyRecord); |
| 366 |
|
| 367 |
ResourceSection unmanagedResources = versionInfo != null || win32icon != null || win32manifest != null || win32resources != null |
| 368 |
? new ResourceSection() |
| 369 |
: null; |
| 370 |
|
| 371 |
if (versionInfo != null) |
| 372 |
{ |
| 373 |
versionInfo.SetName(GetName()); |
| 374 |
versionInfo.SetFileName(assemblyFileName); |
| 375 |
foreach (CustomAttributeBuilder cab in customAttributes) |
| 376 |
{ |
| 377 |
// .NET doesn't support copying blob custom attributes into the version info |
| 378 |
if (!cab.HasBlob || universe.DecodeVersionInfoAttributeBlobs) |
| 379 |
{ |
| 380 |
versionInfo.SetAttribute(this, cab); |
| 381 |
} |
| 382 |
} |
| 383 |
ByteBuffer versionInfoData = new ByteBuffer(512); |
| 384 |
versionInfo.Write(versionInfoData); |
| 385 |
unmanagedResources.AddVersionInfo(versionInfoData); |
| 386 |
} |
| 387 |
|
| 388 |
if (win32icon != null) |
| 389 |
{ |
| 390 |
unmanagedResources.AddIcon(win32icon); |
| 391 |
} |
| 392 |
|
| 393 |
if (win32manifest != null) |
| 394 |
{ |
| 395 |
unmanagedResources.AddManifest(win32manifest, fileKind == PEFileKinds.Dll ? (ushort)2 : (ushort)1); |
| 396 |
} |
| 397 |
|
| 398 |
if (win32resources != null) |
| 399 |
{ |
| 400 |
unmanagedResources.ExtractResources(win32resources); |
| 401 |
} |
| 402 |
|
| 403 |
foreach (CustomAttributeBuilder cab in customAttributes) |
| 404 |
{ |
| 405 |
// we intentionally don't filter out the version info (pseudo) custom attributes (to be compatible with .NET) |
| 406 |
manifestModule.SetCustomAttribute(0x20000001, cab); |
| 407 |
} |
| 408 |
|
| 409 |
manifestModule.AddDeclarativeSecurity(0x20000001, declarativeSecurity); |
| 410 |
|
| 411 |
foreach (TypeForwarder fwd in typeForwarders) |
| 412 |
{ |
| 413 |
manifestModule.AddTypeForwarder(fwd.Type, fwd.IncludeNested); |
| 414 |
} |
| 415 |
|
| 416 |
foreach (ResourceFile resfile in resourceFiles) |
| 417 |
{ |
| 418 |
#if !CORECLR |
| 419 |
if (resfile.Writer != null) |
| 420 |
{ |
| 421 |
resfile.Writer.Generate(); |
| 422 |
resfile.Writer.Close(); |
| 423 |
} |
| 424 |
#endif |
| 425 |
int fileToken = AddFile(manifestModule, resfile.FileName, 1 /*ContainsNoMetaData*/); |
| 426 |
ManifestResourceTable.Record rec = new ManifestResourceTable.Record(); |
| 427 |
rec.Offset = 0; |
| 428 |
rec.Flags = (int)resfile.Attributes; |
| 429 |
rec.Name = manifestModule.Strings.Add(resfile.Name); |
| 430 |
rec.Implementation = fileToken; |
| 431 |
manifestModule.ManifestResource.AddRecord(rec); |
| 432 |
} |
| 433 |
|
| 434 |
int entryPointToken = 0; |
| 435 |
|
| 436 |
foreach (ModuleBuilder moduleBuilder in modules) |
| 437 |
{ |
| 438 |
moduleBuilder.FillAssemblyRefTable(); |
| 439 |
moduleBuilder.EmitResources(); |
| 440 |
if (moduleBuilder != manifestModule) |
| 441 |
{ |
| 442 |
int fileToken; |
| 443 |
if (entryPoint != null && entryPoint.Module == moduleBuilder) |
| 444 |
{ |
| 445 |
ModuleWriter.WriteModule(null, null, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, moduleBuilder.unmanagedResources, entryPoint.MetadataToken); |
| 446 |
entryPointToken = fileToken = AddFile(manifestModule, moduleBuilder.fileName, 0 /*ContainsMetaData*/); |
| 447 |
} |
| 448 |
else |
| 449 |
{ |
| 450 |
ModuleWriter.WriteModule(null, null, moduleBuilder, fileKind, portableExecutableKind, imageFileMachine, moduleBuilder.unmanagedResources, 0); |
| 451 |
fileToken = AddFile(manifestModule, moduleBuilder.fileName, 0 /*ContainsMetaData*/); |
| 452 |
} |
| 453 |
moduleBuilder.ExportTypes(fileToken, manifestModule); |
| 454 |
} |
| 455 |
moduleBuilder.CloseResources(); |
| 456 |
} |
| 457 |
|
| 458 |
foreach (Module module in addedModules) |
| 459 |
{ |
| 460 |
int fileToken = AddFile(manifestModule, module.FullyQualifiedName, 0 /*ContainsMetaData*/); |
| 461 |
module.ExportTypes(fileToken, manifestModule); |
| 462 |
} |
| 463 |
|
| 464 |
if (entryPointToken == 0 && entryPoint != null) |
| 465 |
{ |
| 466 |
entryPointToken = entryPoint.MetadataToken; |
| 467 |
} |
| 468 |
|
| 469 |
// finally, write the manifest module |
| 470 |
ModuleWriter.WriteModule(keyPair, publicKey, manifestModule, fileKind, portableExecutableKind, imageFileMachine, unmanagedResources ?? manifestModule.unmanagedResources, entryPointToken, streamOrNull); |
| 471 |
} |
| 472 |
|
| 473 |
private int AddFile(ModuleBuilder manifestModule, string fileName, int flags) |
| 474 |
{ |
| 475 |
using (var hash = SHA1.Create()) |
| 476 |
{ |
| 477 |
string fullPath = fileName; |
| 478 |
if (dir != null) |
| 479 |
{ |
| 480 |
fullPath = Path.Combine(dir, fileName); |
| 481 |
} |
| 482 |
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) |
| 483 |
{ |
| 484 |
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write)) |
| 485 |
{ |
| 486 |
byte[] buf = new byte[8192]; |
| 487 |
ModuleWriter.HashChunk(fs, cs, buf, (int)fs.Length); |
| 488 |
} |
| 489 |
} |
| 490 |
return manifestModule.__AddModule(flags, Path.GetFileName(fileName), hash.Hash); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
public void AddResourceFile(string name, string fileName) |
| 495 |
{ |
| 496 |
AddResourceFile(name, fileName, ResourceAttributes.Public); |
| 497 |
} |
| 498 |
|
| 499 |
public void AddResourceFile(string name, string fileName, ResourceAttributes attribs) |
| 500 |
{ |
| 501 |
ResourceFile resfile = new ResourceFile(); |
| 502 |
resfile.Name = name; |
| 503 |
resfile.FileName = fileName; |
| 504 |
resfile.Attributes = attribs; |
| 505 |
resourceFiles.Add(resfile); |
| 506 |
} |
| 507 |
|
| 508 |
#if !CORECLR |
| 509 |
public IResourceWriter DefineResource(string name, string description, string fileName) |
| 510 |
{ |
| 511 |
return DefineResource(name, description, fileName, ResourceAttributes.Public); |
| 512 |
} |
| 513 |
|
| 514 |
public IResourceWriter DefineResource(string name, string description, string fileName, ResourceAttributes attribute) |
| 515 |
{ |
| 516 |
// FXBUG we ignore the description, because there is no such thing |
| 517 |
|
| 518 |
string fullPath = fileName; |
| 519 |
if (dir != null) |
| 520 |
{ |
| 521 |
fullPath = Path.Combine(dir, fileName); |
| 522 |
} |
| 523 |
ResourceWriter rw = new ResourceWriter(fullPath); |
| 524 |
ResourceFile resfile; |
| 525 |
resfile.Name = name; |
| 526 |
resfile.FileName = fileName; |
| 527 |
resfile.Attributes = attribute; |
| 528 |
resfile.Writer = rw; |
| 529 |
resourceFiles.Add(resfile); |
| 530 |
return rw; |
| 531 |
} |
| 532 |
#endif |
| 533 |
|
| 534 |
public void DefineVersionInfoResource() |
| 535 |
{ |
| 536 |
if (versionInfo != null || win32resources != null) |
| 537 |
{ |
| 538 |
throw new ArgumentException("Native resource has already been defined."); |
| 539 |
} |
| 540 |
versionInfo = new VersionInfo(); |
| 541 |
} |
| 542 |
|
| 543 |
public void DefineVersionInfoResource(string product, string productVersion, string company, string copyright, string trademark) |
| 544 |
{ |
| 545 |
if (versionInfo != null || win32resources != null) |
| 546 |
{ |
| 547 |
throw new ArgumentException("Native resource has already been defined."); |
| 548 |
} |
| 549 |
versionInfo = new VersionInfo(); |
| 550 |
versionInfo.product = product; |
| 551 |
versionInfo.informationalVersion = productVersion; |
| 552 |
versionInfo.company = company; |
| 553 |
versionInfo.copyright = copyright; |
| 554 |
versionInfo.trademark = trademark; |
| 555 |
} |
| 556 |
|
| 557 |
public void __DefineIconResource(byte[] iconFile) |
| 558 |
{ |
| 559 |
if (win32icon != null || win32resources != null) |
| 560 |
{ |
| 561 |
throw new ArgumentException("Native resource has already been defined."); |
| 562 |
} |
| 563 |
win32icon = (byte[])iconFile.Clone(); |
| 564 |
} |
| 565 |
|
| 566 |
public void __DefineManifestResource(byte[] manifest) |
| 567 |
{ |
| 568 |
if (win32manifest != null || win32resources != null) |
| 569 |
{ |
| 570 |
throw new ArgumentException("Native resource has already been defined."); |
| 571 |
} |
| 572 |
win32manifest = (byte[])manifest.Clone(); |
| 573 |
} |
| 574 |
|
| 575 |
public void __DefineUnmanagedResource(byte[] resource) |
| 576 |
{ |
| 577 |
if (versionInfo != null || win32icon != null || win32manifest != null || win32resources != null) |
| 578 |
{ |
| 579 |
throw new ArgumentException("Native resource has already been defined."); |
| 580 |
} |
| 581 |
// The standard .NET DefineUnmanagedResource(byte[]) is useless, because it embeds "resource" (as-is) as the .rsrc section, |
| 582 |
// but it doesn't set the PE file Resource Directory entry to point to it. That's why we have a renamed version, which behaves |
| 583 |
// like DefineUnmanagedResource(string). |
| 584 |
win32resources = (byte[])resource.Clone(); |
| 585 |
} |
| 586 |
|
| 587 |
public void DefineUnmanagedResource(string resourceFileName) |
| 588 |
{ |
| 589 |
// This method reads the specified resource file (Win32 .res file) and converts it into the appropriate format and embeds it in the .rsrc section, |
| 590 |
// also setting the Resource Directory entry. |
| 591 |
__DefineUnmanagedResource(File.ReadAllBytes(resourceFileName)); |
| 592 |
} |
| 593 |
|
| 594 |
public override Type[] GetTypes() |
| 595 |
{ |
| 596 |
List<Type> list = new List<Type>(); |
| 597 |
foreach (ModuleBuilder module in modules) |
| 598 |
{ |
| 599 |
module.GetTypesImpl(list); |
| 600 |
} |
| 601 |
foreach (Module module in addedModules) |
| 602 |
{ |
| 603 |
module.GetTypesImpl(list); |
| 604 |
} |
| 605 |
return list.ToArray(); |
| 606 |
} |
| 607 |
|
| 608 |
internal override Type FindType(TypeName typeName) |
| 609 |
{ |
| 610 |
foreach (ModuleBuilder mb in modules) |
| 611 |
{ |
| 612 |
Type type = mb.FindType(typeName); |
| 613 |
if (type != null) |
| 614 |
{ |
| 615 |
return type; |
| 616 |
} |
| 617 |
} |
| 618 |
foreach (Module module in addedModules) |
| 619 |
{ |
| 620 |
Type type = module.FindType(typeName); |
| 621 |
if (type != null) |
| 622 |
{ |
| 623 |
return type; |
| 624 |
} |
| 625 |
} |
| 626 |
return null; |
| 627 |
} |
| 628 |
|
| 629 |
internal override Type FindTypeIgnoreCase(TypeName lowerCaseName) |
| 630 |
{ |
| 631 |
foreach (ModuleBuilder mb in modules) |
| 632 |
{ |
| 633 |
Type type = mb.FindTypeIgnoreCase(lowerCaseName); |
| 634 |
if (type != null) |
| 635 |
{ |
| 636 |
return type; |
| 637 |
} |
| 638 |
} |
| 639 |
foreach (Module module in addedModules) |
| 640 |
{ |
| 641 |
Type type = module.FindTypeIgnoreCase(lowerCaseName); |
| 642 |
if (type != null) |
| 643 |
{ |
| 644 |
return type; |
| 645 |
} |
| 646 |
} |
| 647 |
return null; |
| 648 |
} |
| 649 |
|
| 650 |
public override string ImageRuntimeVersion |
| 651 |
{ |
| 652 |
get { return imageRuntimeVersion; } |
| 653 |
} |
| 654 |
|
| 655 |
public void __SetImageRuntimeVersion(string imageRuntimeVersion, int mdStreamVersion) |
| 656 |
{ |
| 657 |
this.imageRuntimeVersion = imageRuntimeVersion; |
| 658 |
this.mdStreamVersion = mdStreamVersion; |
| 659 |
} |
| 660 |
|
| 661 |
public override Module ManifestModule |
| 662 |
{ |
| 663 |
get |
| 664 |
{ |
| 665 |
if (pseudoManifestModule == null) |
| 666 |
{ |
| 667 |
pseudoManifestModule = new ManifestModule(this); |
| 668 |
} |
| 669 |
return pseudoManifestModule; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
public override MethodInfo EntryPoint |
| 674 |
{ |
| 675 |
get { return entryPoint; } |
| 676 |
} |
| 677 |
|
| 678 |
public override AssemblyName[] GetReferencedAssemblies() |
| 679 |
{ |
| 680 |
return Empty<AssemblyName>.Array; |
| 681 |
} |
| 682 |
|
| 683 |
public override Module[] GetLoadedModules(bool getResourceModules) |
| 684 |
{ |
| 685 |
return GetModules(getResourceModules); |
| 686 |
} |
| 687 |
|
| 688 |
public override Module[] GetModules(bool getResourceModules) |
| 689 |
{ |
| 690 |
List<Module> list = new List<Module>(); |
| 691 |
foreach (ModuleBuilder module in modules) |
| 692 |
{ |
| 693 |
if (getResourceModules || !module.IsResource()) |
| 694 |
{ |
| 695 |
list.Add(module); |
| 696 |
} |
| 697 |
} |
| 698 |
foreach (Module module in addedModules) |
| 699 |
{ |
| 700 |
if (getResourceModules || !module.IsResource()) |
| 701 |
{ |
| 702 |
list.Add(module); |
| 703 |
} |
| 704 |
} |
| 705 |
return list.ToArray(); |
| 706 |
} |
| 707 |
|
| 708 |
public override Module GetModule(string name) |
| 709 |
{ |
| 710 |
foreach (ModuleBuilder module in modules) |
| 711 |
{ |
| 712 |
if (module.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) |
| 713 |
{ |
| 714 |
return module; |
| 715 |
} |
| 716 |
} |
| 717 |
foreach (Module module in addedModules) |
| 718 |
{ |
| 719 |
if (module.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) |
| 720 |
{ |
| 721 |
return module; |
| 722 |
} |
| 723 |
} |
| 724 |
return null; |
| 725 |
} |
| 726 |
|
| 727 |
public Module __AddModule(RawModule module) |
| 728 |
{ |
| 729 |
Module mod = module.ToModule(this); |
| 730 |
addedModules.Add(mod); |
| 731 |
return mod; |
| 732 |
} |
| 733 |
|
| 734 |
public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) |
| 735 |
{ |
| 736 |
throw new NotSupportedException(); |
| 737 |
} |
| 738 |
|
| 739 |
public override string[] GetManifestResourceNames() |
| 740 |
{ |
| 741 |
throw new NotSupportedException(); |
| 742 |
} |
| 743 |
|
| 744 |
public override Stream GetManifestResourceStream(string resourceName) |
| 745 |
{ |
| 746 |
throw new NotSupportedException(); |
| 747 |
} |
| 748 |
|
| 749 |
public override bool IsDynamic |
| 750 |
{ |
| 751 |
get { return true; } |
| 752 |
} |
| 753 |
|
| 754 |
public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) |
| 755 |
{ |
| 756 |
return new Universe().DefineDynamicAssembly(name, access); |
| 757 |
} |
| 758 |
|
| 759 |
public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable<CustomAttributeBuilder> assemblyAttributes) |
| 760 |
{ |
| 761 |
return new Universe().DefineDynamicAssembly(name, access, assemblyAttributes); |
| 762 |
} |
| 763 |
|
| 764 |
internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType) |
| 765 |
{ |
| 766 |
List<CustomAttributeData> list = new List<CustomAttributeData>(); |
| 767 |
foreach (CustomAttributeBuilder cab in customAttributes) |
| 768 |
{ |
| 769 |
if (attributeType == null || attributeType.IsAssignableFrom(cab.Constructor.DeclaringType)) |
| 770 |
{ |
| 771 |
list.Add(cab.ToData(this)); |
| 772 |
} |
| 773 |
} |
| 774 |
return list; |
| 775 |
} |
| 776 |
|
| 777 |
internal bool IsWindowsRuntime |
| 778 |
{ |
| 779 |
get { return (flags & (AssemblyNameFlags)0x200) != 0; } |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
sealed class ManifestModule : NonPEModule |
| 784 |
{ |
| 785 |
private readonly AssemblyBuilder assembly; |
| 786 |
private readonly Guid guid = Guid.NewGuid(); |
| 787 |
|
| 788 |
internal ManifestModule(AssemblyBuilder assembly) |
| 789 |
: base(assembly.universe) |
| 790 |
{ |
| 791 |
this.assembly = assembly; |
| 792 |
} |
| 793 |
|
| 794 |
public override int MDStreamVersion |
| 795 |
{ |
| 796 |
get { return assembly.mdStreamVersion; } |
| 797 |
} |
| 798 |
|
| 799 |
public override Assembly Assembly |
| 800 |
{ |
| 801 |
get { return assembly; } |
| 802 |
} |
| 803 |
|
| 804 |
internal override Type FindType(TypeName typeName) |
| 805 |
{ |
| 806 |
return null; |
| 807 |
} |
| 808 |
|
| 809 |
internal override Type FindTypeIgnoreCase(TypeName lowerCaseName) |
| 810 |
{ |
| 811 |
return null; |
| 812 |
} |
| 813 |
|
| 814 |
internal override void GetTypesImpl(List<Type> list) |
| 815 |
{ |
| 816 |
} |
| 817 |
|
| 818 |
public override string FullyQualifiedName |
| 819 |
{ |
| 820 |
get { return Path.Combine(assembly.dir, "RefEmit_InMemoryManifestModule"); } |
| 821 |
} |
| 822 |
|
| 823 |
public override string Name |
| 824 |
{ |
| 825 |
get { return "<In Memory Module>"; } |
| 826 |
} |
| 827 |
|
| 828 |
public override Guid ModuleVersionId |
| 829 |
{ |
| 830 |
get { return guid; } |
| 831 |
} |
| 832 |
|
| 833 |
public override string ScopeName |
| 834 |
{ |
| 835 |
get { return "RefEmit_InMemoryManifestModule"; } |
| 836 |
} |
| 837 |
|
| 838 |
protected override Exception NotSupportedException() |
| 839 |
{ |
| 840 |
return new InvalidOperationException(); |
| 841 |
} |
| 842 |
} |
| 843 |
} |