Ejemplo n.º 1
0
        public string FindAssembly(string name)
        {
            var asm = assembly.LookupReferencedAssembly(name);

            return(asm == null ? null : asm.FileName);
        }
Ejemplo n.º 2
0
        void WriteProjectFile(TextWriter writer, IEnumerable <Tuple <string, string> > files, LoadedAssembly assembly, DecompilationOptions options)
        {
            var          module       = assembly.ModuleDefinition;
            const string ns           = "http://schemas.microsoft.com/developer/msbuild/2003";
            string       platformName = CSharpLanguage.GetPlatformName(module);
            Guid         guid         = (App.CommandLineArguments == null ? null : App.CommandLineArguments.FixedGuid) ?? Guid.NewGuid();

            using (XmlTextWriter w = new XmlTextWriter(writer)) {
                var asmRefs = CSharpLanguage.GetAssemblyRefs(options, assembly);

                w.Formatting = Formatting.Indented;
                w.WriteStartDocument();
                w.WriteStartElement("Project", ns);
                w.WriteAttributeString("ToolsVersion", "4.0");
                w.WriteAttributeString("DefaultTargets", "Build");

                w.WriteStartElement("PropertyGroup");
                w.WriteElementString("ProjectGuid", (options.ProjectGuid ?? guid).ToString("B").ToUpperInvariant());

                w.WriteStartElement("Configuration");
                w.WriteAttributeString("Condition", " '$(Configuration)' == '' ");
                w.WriteValue("Debug");
                w.WriteEndElement();                 // </Configuration>

                w.WriteStartElement("Platform");
                w.WriteAttributeString("Condition", " '$(Platform)' == '' ");
                w.WriteValue(platformName);
                w.WriteEndElement();                 // </Platform>

                switch (module.Kind)
                {
                case ModuleKind.Windows:
                    w.WriteElementString("OutputType", "WinExe");
                    break;

                case ModuleKind.Console:
                    w.WriteElementString("OutputType", "Exe");
                    break;

                default:
                    w.WriteElementString("OutputType", "Library");
                    break;
                }

                if (module.Assembly != null)
                {
                    w.WriteElementString("AssemblyName", IdentifierEscaper.Escape(module.Assembly.Name));
                }
                bool useTargetFrameworkAttribute = false;
                var  targetFrameworkAttribute    = module.Assembly == null ? null : module.Assembly.CustomAttributes.FirstOrDefault(a => a.TypeFullName == "System.Runtime.Versioning.TargetFrameworkAttribute");
                if (targetFrameworkAttribute != null && targetFrameworkAttribute.ConstructorArguments.Any())
                {
                    string   frameworkName    = (targetFrameworkAttribute.ConstructorArguments[0].Value as UTF8String) ?? string.Empty;
                    string[] frameworkParts   = frameworkName.Split(',');
                    string   frameworkVersion = frameworkParts.FirstOrDefault(a => a.StartsWith("Version="));
                    if (frameworkVersion != null)
                    {
                        w.WriteElementString("TargetFrameworkVersion", frameworkVersion.Substring("Version=".Length));
                        useTargetFrameworkAttribute = true;
                    }
                    string frameworkProfile = frameworkParts.FirstOrDefault(a => a.StartsWith("Profile="));
                    if (frameworkProfile != null)
                    {
                        w.WriteElementString("TargetFrameworkProfile", frameworkProfile.Substring("Profile=".Length));
                    }
                }
                if (!useTargetFrameworkAttribute)
                {
                    if (module.IsClr10)
                    {
                        w.WriteElementString("TargetFrameworkVersion", "v1.0");
                    }
                    else if (module.IsClr11)
                    {
                        w.WriteElementString("TargetFrameworkVersion", "v1.1");
                    }
                    else if (module.IsClr20)
                    {
                        w.WriteElementString("TargetFrameworkVersion", "v2.0");
                        // TODO: Detect when .NET 3.0/3.5 is required
                    }
                    else
                    {
                        w.WriteElementString("TargetFrameworkVersion", "v4.0");
                    }
                }
                w.WriteElementString("WarningLevel", "4");

                w.WriteEndElement();                  // </PropertyGroup>

                w.WriteStartElement("PropertyGroup"); // platform-specific
                w.WriteAttributeString("Condition", " '$(Platform)' == '" + platformName + "' ");
                w.WriteElementString("PlatformTarget", platformName);
                w.WriteEndElement();                  // </PropertyGroup> (platform-specific)

                w.WriteStartElement("PropertyGroup"); // Debug
                w.WriteAttributeString("Condition", " '$(Configuration)' == 'Debug' ");
                w.WriteElementString("OutputPath", "bin\\Debug\\");
                w.WriteElementString("DebugSymbols", "true");
                w.WriteElementString("DebugType", "full");
                w.WriteElementString("Optimize", "false");
                if (options.DontReferenceStdLib)
                {
                    w.WriteStartElement("NoStdLib");
                    w.WriteString("true");
                    w.WriteEndElement();
                }
                w.WriteEndElement();                  // </PropertyGroup> (Debug)

                w.WriteStartElement("PropertyGroup"); // Release
                w.WriteAttributeString("Condition", " '$(Configuration)' == 'Release' ");
                w.WriteElementString("OutputPath", "bin\\Release\\");
                w.WriteElementString("DebugSymbols", "true");
                w.WriteElementString("DebugType", "pdbonly");
                w.WriteElementString("Optimize", "true");
                if (options.DontReferenceStdLib)
                {
                    w.WriteStartElement("NoStdLib");
                    w.WriteString("true");
                    w.WriteEndElement();
                }
                w.WriteEndElement();                 // </PropertyGroup> (Release)


                w.WriteStartElement("ItemGroup");                 // References
                foreach (var r in asmRefs)
                {
                    if (r.Name != "mscorlib")
                    {
                        var asm = assembly.LookupReferencedAssembly(r, module);
                        if (asm != null && CSharpLanguage.ExistsInProject(options, asm.FileName))
                        {
                            continue;
                        }
                        w.WriteStartElement("Reference");
                        w.WriteAttributeString("Include", IdentifierEscaper.Escape(r.Name));
                        var hintPath = CSharpLanguage.GetHintPath(options, asm);
                        if (hintPath != null)
                        {
                            w.WriteStartElement("HintPath");
                            w.WriteString(hintPath);
                            w.WriteEndElement();
                        }
                        w.WriteEndElement();
                    }
                }
                w.WriteEndElement();                 // </ItemGroup> (References)

                foreach (IGrouping <string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g))
                {
                    w.WriteStartElement("ItemGroup");
                    foreach (string file in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase))
                    {
                        w.WriteStartElement(gr.Key);
                        w.WriteAttributeString("Include", file);
                        w.WriteEndElement();
                    }
                    w.WriteEndElement();
                }

                w.WriteStartElement("ItemGroup");                 // ProjectReference
                foreach (var r in asmRefs)
                {
                    var asm = assembly.LookupReferencedAssembly(r, module);
                    if (asm == null)
                    {
                        continue;
                    }
                    var otherProj = CSharpLanguage.FindOtherProject(options, asm.FileName);
                    if (otherProj != null)
                    {
                        var relPath = CSharpLanguage.GetRelativePath(options.SaveAsProjectDirectory, otherProj.ProjectFileName);
                        w.WriteStartElement("ProjectReference");
                        w.WriteAttributeString("Include", relPath);
                        w.WriteStartElement("Project");
                        w.WriteString(otherProj.ProjectGuid.ToString("B").ToUpperInvariant());
                        w.WriteEndElement();
                        w.WriteStartElement("Name");
                        w.WriteString(IdentifierEscaper.Escape(otherProj.AssemblySimpleName));
                        w.WriteEndElement();
                        w.WriteEndElement();
                    }
                }
                w.WriteEndElement();                 // </ItemGroup> (ProjectReference)

                w.WriteStartElement("ItemGroup");    // Imports
                foreach (var import in projectImports.OrderBy(x => x))
                {
                    w.WriteStartElement("Import");
                    w.WriteAttributeString("Include", import);
                    w.WriteEndElement();
                }
                w.WriteEndElement();                 // </ItemGroup> (Imports)

                w.WriteStartElement("Import");
                w.WriteAttributeString("Project", "$(MSBuildToolsPath)\\Microsoft.VisualBasic.targets");
                w.WriteEndElement();

                w.WriteEndDocument();
            }
        }
Ejemplo n.º 3
0
		void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, LoadedAssembly assembly, DecompilationOptions options)
		{
			var module = assembly.ModuleDefinition;
			const string ns = "http://schemas.microsoft.com/developer/msbuild/2003";
			string platformName = CSharpLanguage.GetPlatformName(module);
			Guid guid = (App.CommandLineArguments == null ? null : App.CommandLineArguments.FixedGuid) ?? Guid.NewGuid();
			using (XmlTextWriter w = new XmlTextWriter(writer)) {
				var asmRefs = CSharpLanguage.GetAssemblyRefs(options, assembly);

				w.Formatting = Formatting.Indented;
				w.WriteStartDocument();
				w.WriteStartElement("Project", ns);
				w.WriteAttributeString("ToolsVersion", "4.0");
				w.WriteAttributeString("DefaultTargets", "Build");

				w.WriteStartElement("PropertyGroup");
				w.WriteElementString("ProjectGuid", (options.ProjectGuid ?? guid).ToString("B").ToUpperInvariant());

				w.WriteStartElement("Configuration");
				w.WriteAttributeString("Condition", " '$(Configuration)' == '' ");
				w.WriteValue("Debug");
				w.WriteEndElement(); // </Configuration>

				w.WriteStartElement("Platform");
				w.WriteAttributeString("Condition", " '$(Platform)' == '' ");
				w.WriteValue(platformName);
				w.WriteEndElement(); // </Platform>

				switch (module.Kind) {
					case ModuleKind.Windows:
						w.WriteElementString("OutputType", "WinExe");
						break;
					case ModuleKind.Console:
						w.WriteElementString("OutputType", "Exe");
						break;
					default:
						w.WriteElementString("OutputType", "Library");
						break;
				}

				if (module.Assembly != null)
					w.WriteElementString("AssemblyName", IdentifierEscaper.Escape(module.Assembly.Name));
				bool useTargetFrameworkAttribute = false;
				var targetFrameworkAttribute = module.Assembly == null ? null : module.Assembly.CustomAttributes.FirstOrDefault(a => a.TypeFullName == "System.Runtime.Versioning.TargetFrameworkAttribute");
				if (targetFrameworkAttribute != null && targetFrameworkAttribute.ConstructorArguments.Any()) {
					string frameworkName = (targetFrameworkAttribute.ConstructorArguments[0].Value as UTF8String) ?? string.Empty;
					string[] frameworkParts = frameworkName.Split(',');
					string frameworkVersion = frameworkParts.FirstOrDefault(a => a.StartsWith("Version="));
					if (frameworkVersion != null) {
						w.WriteElementString("TargetFrameworkVersion", frameworkVersion.Substring("Version=".Length));
						useTargetFrameworkAttribute = true;
					}
					string frameworkProfile = frameworkParts.FirstOrDefault(a => a.StartsWith("Profile="));
					if (frameworkProfile != null)
						w.WriteElementString("TargetFrameworkProfile", frameworkProfile.Substring("Profile=".Length));
				}
				if (!useTargetFrameworkAttribute) {
					if (module.IsClr10) {
						w.WriteElementString("TargetFrameworkVersion", "v1.0");
					} else if (module.IsClr11) {
						w.WriteElementString("TargetFrameworkVersion", "v1.1");
					} else if (module.IsClr20) {
						w.WriteElementString("TargetFrameworkVersion", "v2.0");
						// TODO: Detect when .NET 3.0/3.5 is required
					} else {
						w.WriteElementString("TargetFrameworkVersion", "v4.0");
					}
				}
				w.WriteElementString("WarningLevel", "4");

				w.WriteEndElement(); // </PropertyGroup>

				w.WriteStartElement("PropertyGroup"); // platform-specific
				w.WriteAttributeString("Condition", " '$(Platform)' == '" + platformName + "' ");
				w.WriteElementString("PlatformTarget", platformName);
				w.WriteEndElement(); // </PropertyGroup> (platform-specific)

				w.WriteStartElement("PropertyGroup"); // Debug
				w.WriteAttributeString("Condition", " '$(Configuration)' == 'Debug' ");
				w.WriteElementString("OutputPath", "bin\\Debug\\");
				w.WriteElementString("DebugSymbols", "true");
				w.WriteElementString("DebugType", "full");
				w.WriteElementString("Optimize", "false");
				if (options.DontReferenceStdLib) {
					w.WriteStartElement("NoStdLib");
					w.WriteString("true");
					w.WriteEndElement();
				}
				w.WriteEndElement(); // </PropertyGroup> (Debug)

				w.WriteStartElement("PropertyGroup"); // Release
				w.WriteAttributeString("Condition", " '$(Configuration)' == 'Release' ");
				w.WriteElementString("OutputPath", "bin\\Release\\");
				w.WriteElementString("DebugSymbols", "true");
				w.WriteElementString("DebugType", "pdbonly");
				w.WriteElementString("Optimize", "true");
				if (options.DontReferenceStdLib) {
					w.WriteStartElement("NoStdLib");
					w.WriteString("true");
					w.WriteEndElement();
				}
				w.WriteEndElement(); // </PropertyGroup> (Release)


				w.WriteStartElement("ItemGroup"); // References
				foreach (var r in asmRefs) {
					if (r.Name != "mscorlib") {
						var asm = assembly.LookupReferencedAssembly(r, module);
						if (asm != null && CSharpLanguage.ExistsInProject(options, asm.FileName))
							continue;
						w.WriteStartElement("Reference");
						w.WriteAttributeString("Include", IdentifierEscaper.Escape(r.Name));
						var hintPath = CSharpLanguage.GetHintPath(options, asm);
						if (hintPath != null) {
							w.WriteStartElement("HintPath");
							w.WriteString(hintPath);
							w.WriteEndElement();
						}
						w.WriteEndElement();
					}
				}
				w.WriteEndElement(); // </ItemGroup> (References)

				foreach (IGrouping<string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g)) {
					w.WriteStartElement("ItemGroup");
					foreach (string file in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)) {
						w.WriteStartElement(gr.Key);
						w.WriteAttributeString("Include", file);
						w.WriteEndElement();
					}
					w.WriteEndElement();
				}

				w.WriteStartElement("ItemGroup"); // ProjectReference
				foreach (var r in asmRefs) {
					var asm = assembly.LookupReferencedAssembly(r, module);
					if (asm == null)
						continue;
					var otherProj = CSharpLanguage.FindOtherProject(options, asm.FileName);
					if (otherProj != null) {
						var relPath = CSharpLanguage.GetRelativePath(options.SaveAsProjectDirectory, otherProj.ProjectFileName);
						w.WriteStartElement("ProjectReference");
						w.WriteAttributeString("Include", relPath);
						w.WriteStartElement("Project");
						w.WriteString(otherProj.ProjectGuid.ToString("B").ToUpperInvariant());
						w.WriteEndElement();
						w.WriteStartElement("Name");
						w.WriteString(IdentifierEscaper.Escape(otherProj.AssemblySimpleName));
						w.WriteEndElement();
						w.WriteEndElement();
					}
				}
				w.WriteEndElement(); // </ItemGroup> (ProjectReference)
				
				w.WriteStartElement("ItemGroup"); // Imports
				foreach (var import in projectImports.OrderBy(x => x)) {
					w.WriteStartElement("Import");
					w.WriteAttributeString("Include", import);
					w.WriteEndElement();
				}
				w.WriteEndElement(); // </ItemGroup> (Imports)

				w.WriteStartElement("Import");
				w.WriteAttributeString("Project", "$(MSBuildToolsPath)\\Microsoft.VisualBasic.targets");
				w.WriteEndElement();

				w.WriteEndDocument();
			}
		}