Ejemplo n.º 1
0
		private void OpenAssemblyRefs()
		{
			IntPtr enumHandle = IntPtr.Zero;
			uint[] assemblyRefs = new uint[NuGenProject.DefaultArrayCount];
			uint count = 0;
			AssemblyImport.EnumAssemblyRefs(ref enumHandle, assemblyRefs, Convert.ToUInt32(assemblyRefs.Length), out count);

			uint systemDirectoryLength;
			Dispenser.GetCORSystemDirectory(NuGenProject.DefaultCharArray, Convert.ToUInt32(NuGenProject.DefaultCharArray.LongLength), out systemDirectoryLength);
			string systemDirectoryPath = NuGenHelperFunctions.GetString(NuGenProject.DefaultCharArray, 0, systemDirectoryLength);
			systemDirectoryPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(systemDirectoryPath)), FrameworkVersion) + "\\";
			string assemblyDirectoryPath = string.Format("{0}\\", Path.GetDirectoryName(FullPath));

			if (count > 0)
			{
				AssemblyReferences = new Dictionary<uint, NuGenAssemblyReference>();
			}

			while (count > 0)
			{
				for (uint assemblyRefsIndex = 0; assemblyRefsIndex < count; assemblyRefsIndex++)
				{
					uint token = assemblyRefs[assemblyRefsIndex];
					IntPtr PublicKeyOrToken;
					uint PublicKeyOrTokenLength;
					uint nameStringLength;
					NuGenAssemblyMetadata metadata = new NuGenAssemblyMetadata();
					IntPtr hashBlob;
					uint hashBlobLength;
					uint assemblyFlags;

					AssemblyImport.GetAssemblyRefProps(token, out PublicKeyOrToken, out PublicKeyOrTokenLength, NuGenProject.DefaultCharArray, Convert.ToUInt32(NuGenProject.DefaultCharArray.LongLength), out nameStringLength, ref metadata, out hashBlob, out hashBlobLength, out assemblyFlags);

					if (nameStringLength > NuGenProject.DefaultCharArray.Length)
					{
						NuGenProject.DefaultCharArray = new char[nameStringLength];

						AssemblyImport.GetAssemblyRefProps(token, out PublicKeyOrToken, out PublicKeyOrTokenLength, NuGenProject.DefaultCharArray, Convert.ToUInt32(NuGenProject.DefaultCharArray.LongLength), out nameStringLength, ref metadata, out hashBlob, out hashBlobLength, out assemblyFlags);
					}

					NuGenAssemblyReference assemblyRef = new NuGenAssemblyReference(this, token, NuGenHelperFunctions.GetString(NuGenProject.DefaultCharArray, 0, nameStringLength), PublicKeyOrToken, PublicKeyOrTokenLength, metadata, hashBlob, hashBlobLength, assemblyFlags);

					string exeAssemblyPath = string.Format("{0}.exe", assemblyRef.Name);
					string dllAssemblyPath = string.Format("{0}.dll", assemblyRef.Name);
					string exeAssemblyRefPath = Path.Combine(assemblyDirectoryPath, exeAssemblyPath);
					string dllAssemblyRefPath = Path.Combine(assemblyDirectoryPath, dllAssemblyPath);
					string dllSystemPath = Path.Combine(systemDirectoryPath, dllAssemblyPath);
					string exeSystemPath = Path.Combine(systemDirectoryPath, dllAssemblyPath);

					if (System.IO.File.Exists(dllAssemblyRefPath))
					{
						assemblyRef.FileName = dllAssemblyPath;
						assemblyRef.FullPath = dllAssemblyRefPath;
					}
					else if (System.IO.File.Exists(exeAssemblyRefPath))
					{
						assemblyRef.FileName = exeAssemblyPath;
						assemblyRef.FullPath = exeAssemblyRefPath;
					}
					else if (System.IO.File.Exists(dllSystemPath))
					{
						assemblyRef.FileName = dllAssemblyPath;
						assemblyRef.FullPath = dllSystemPath;
					}
					else if (System.IO.File.Exists(exeSystemPath))
					{
						assemblyRef.FileName = dllAssemblyPath;
						assemblyRef.FullPath = exeSystemPath;
					}
					else
					{
						AssemblyName assemblyName = new AssemblyName();
						assemblyName.Name = assemblyRef.Name;
						assemblyName.Version = new Version(assemblyRef.Metadata.usMajorVersion, assemblyRef.Metadata.usMinorVersion, assemblyRef.Metadata.usBuildNumber, assemblyRef.Metadata.usRevisionNumber);

						if (assemblyRef.Metadata.szLocale != IntPtr.Zero)
						{
							throw new NotImplementedException("The assembly local is different from default.");
						}

						assemblyName.CultureInfo = CultureInfo.InvariantCulture;

						byte[] publicKey = NuGenHelperFunctions.ReadBlobAsByteArray(assemblyRef.PublicKeyOrToken, assemblyRef.PublicKeyOrTokenLength);

						if ((assemblyRef.Flags & CorAssemblyFlags.afPublicKey) == CorAssemblyFlags.afPublicKey)
						{
							assemblyName.SetPublicKey(publicKey);
						}
						else
						{
							assemblyName.SetPublicKeyToken(publicKey);
						}

						try
						{
							System.Reflection.Assembly referencedAssembly = System.Reflection.Assembly.Load(assemblyName);

							assemblyRef.FullPath = Path.GetFullPath(new Uri(referencedAssembly.CodeBase).AbsolutePath);
							assemblyRef.FileName = Path.GetFileName(assemblyRef.FullPath);
						}
						catch
						{
						}
					}

					AssemblyReferences[assemblyRef.Token] = assemblyRef;
					AllTokens[assemblyRef.Token] = assemblyRef;
				}

				AssemblyImport.EnumAssemblyRefs(ref enumHandle, assemblyRefs, Convert.ToUInt32(assemblyRefs.Length), out count);
			}

			AssemblyImport.CloseEnum(enumHandle);
		}
Ejemplo n.º 2
0
		private void ReadProperties()
		{
			uint token;
			AssemblyImport.GetAssemblyFromScope(out token);
			Token = token;
			IntPtr publicKey;
			uint publicKeyLength;
			uint hashAlgorithmId;
			uint nameLength;
			NuGenAssemblyMetadata metadata = new NuGenAssemblyMetadata();
			uint flags;
			AssemblyImport.GetAssemblyProps(Token, out publicKey, out publicKeyLength, out hashAlgorithmId, NuGenProject.DefaultCharArray, Convert.ToUInt32(NuGenProject.DefaultCharArray.Length), out nameLength, ref metadata, out flags);

			if (nameLength > NuGenProject.DefaultCharArray.Length)
			{
				NuGenProject.DefaultCharArray = new char[nameLength];

				AssemblyImport.GetAssemblyProps(Token, out publicKey, out publicKeyLength, out hashAlgorithmId, NuGenProject.DefaultCharArray, Convert.ToUInt32(NuGenProject.DefaultCharArray.Length), out nameLength, ref metadata, out flags);
			}

			PublicKey = publicKey;
			PublicKeyLength = publicKeyLength;
			HashAlgorithmID = hashAlgorithmId;
			Name = NuGenHelperFunctions.GetString(NuGenProject.DefaultCharArray, 0, nameLength);
			Metadata = metadata;
			Flags = (CorAssemblyFlags)flags;

			if (Flags != CorAssemblyFlags.afPublicKey && Flags != CorAssemblyFlags.afLongevityUnspecified)
			{
				throw new NotImplementedException("Unknown assembly flag value.");
			}

			AssemblyCustomAttributes = NuGenHelperFunctions.EnumCustomAttributes(Import, this, this);
			PermissionSets = NuGenHelperFunctions.EnumPermissionSets(Import, Token);
		}
		public NuGenAssemblyReference(NuGenAssembly assembly, uint token, string name, IntPtr publicKeyOrToken, uint publicKeyOrTokenLength, NuGenAssemblyMetadata metadata, IntPtr hashBlob, uint hashBlobLength, uint flags)
		{
			Assembly = assembly;
			Token = token;
			Name = name;
			PublicKeyOrToken = publicKeyOrToken;
			PublicKeyOrTokenLength = publicKeyOrTokenLength;
			Metadata = metadata;
			HashBlob = hashBlob;
			HashBlobLength = hashBlobLength;
			Flags = (CorAssemblyFlags)flags;
		}