Exemple #1
0
        private ICSharpCode.ILSpy.Language OutputLanguage(string language)
        {
            //CONFIGURE OUTPUT LANGUAGE
            ICSharpCode.ILSpy.Language lang;
            switch (language)
            {
                case "CXX":
                    lang = new ICSharpCode.ILSpy.Cpp.CppLanguage();
                    break;
                case "C#":
                    lang = new ICSharpCode.ILSpy.CSharpLanguage();
                    break;
                //Why we can't add VB ??
#if !CORE
                case "VB":
                    lang = new ICSharpCode.ILSpy.VB.VBLanguage();
                    break;
#endif
                case "IL":
                    lang = new ICSharpCode.ILSpy.ILLanguage(true);
                    break;
                default:
                    throw new InvalidOperationException("");
            }

            Utils.WriteToConsole("Output language is " + lang.Name);
            return lang;
        }
Exemple #2
0
        private ICSharpCode.ILSpy.Language OutputLanguage(string language)
        {
            //CONFIGURE OUTPUT LANGUAGE
            ICSharpCode.ILSpy.Language lang;
            switch (language)
            {
            case "CXX":
                lang = new ICSharpCode.ILSpy.Cpp.CppLanguage();
                break;

            case "C#":
                lang = new ICSharpCode.ILSpy.CSharpLanguage();
                break;

                //Why we can't add VB ??
#if !CORE
            case "VB":
                lang = new ICSharpCode.ILSpy.VB.VBLanguage();
                break;
#endif
            case "IL":
                lang = new ICSharpCode.ILSpy.ILLanguage(true);
                break;

            default:
                throw new InvalidOperationException("");
            }

            Utils.WriteToConsole("Output language is " + lang.Name);
            return(lang);
        }
Exemple #3
0
        internal static void Initialize(CompositionContainer composition)
        {
            List <Language> languages = new List <Language>();

            languages.AddRange(composition.GetExportedValues <Language>());
                        #if DEBUG
            languages.AddRange(ILAstLanguage.GetDebugLanguages());
            languages.AddRange(CSharpLanguage.GetDebugLanguages());
                        #endif
            allLanguages = languages.AsReadOnly();
        }
Exemple #4
0
        internal static void Initialize(ExportProvider ep)
        {
            List <Language> languages = new List <Language>();

            languages.AddRange(ep.GetExportedValues <Language>());
            languages.Sort((a, b) => a.Name.CompareTo(b.Name));
#if DEBUG
            languages.AddRange(ILAstLanguage.GetDebugLanguages());
            languages.AddRange(CSharpLanguage.GetDebugLanguages());
#endif
            allLanguages = languages.AsReadOnly();
        }
Exemple #5
0
        internal static void Initialize(CompositionContainer composition)
        {
            List <Language> languages = new List <Language>();

            if (composition != null)
            {
                languages.AddRange(composition.GetExportedValues <Language>());
            }
            else
            {
                languages.Add(new CSharpLanguage());
                languages.Add(new VB.VBLanguage());
            }

            // Fix order: C#, VB, IL
            var langs2 = new List <Language>();
            var lang   = languages.FirstOrDefault(a => a is CSharpLanguage);

            if (lang != null)
            {
                languages.Remove(lang);
                langs2.Add(lang);
            }
            lang = languages.FirstOrDefault(a => a is VB.VBLanguage);
            if (lang != null)
            {
                languages.Remove(lang);
                langs2.Add(lang);
            }
            langs2.Add(new ILLanguage(true));
            for (int i = 0; i < langs2.Count; i++)
            {
                languages.Insert(i, langs2[i]);
            }

#if DEBUG
            languages.AddRange(ILAstLanguage.GetDebugLanguages());
            languages.AddRange(CSharpLanguage.GetDebugLanguages());
#endif
            allLanguages = languages.AsReadOnly();
        }
        public string Decompile(object @object)
        {
            if (@object == null) return String.Empty;
            Language l = new CSharpLanguage();

            ITextOutput output = new RtfTextOutput();
            var options = new DecompilationOptions();

            if (@object is AssemblyDefinition)
                l.DecompileAssembly((AssemblyDefinition)@object, output, options);
            else if (@object is TypeDefinition)
                l.DecompileType((TypeDefinition)@object, output, options);
            else if (@object is MethodDefinition)
                l.DecompileMethod((MethodDefinition)@object, output, options);
            else if (@object is FieldDefinition)
                l.DecompileField((FieldDefinition)@object, output, options);
            else if (@object is PropertyDefinition)
                l.DecompileProperty((PropertyDefinition)@object, output, options);
            else if (@object is EventDefinition)
                l.DecompileEvent((EventDefinition)@object, output, options);
            else if (@object is AssemblyNameReference)
            {
                output.Write("// Assembly Reference ");
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }
            else if(@object is ModuleReference)
            {
                output.Write("// Module Reference ");
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }
            else
            {
                output.Write(String.Format("// {0} ", @object.GetType().Name));
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }

            return output.ToString();
        }
Exemple #7
0
        private string GetCSharpCode(IMemberDefinition member, bool debug)
        {
            PlainTextOutput output = new PlainTextOutput();
            CSharpLanguage csharp = new CSharpLanguage();
            DecompilationOptions options = new DecompilationOptions();
            options.DecompilerSettings = LoadDecompilerSettings();

            if (member is TypeDefinition)
            {
                DecomplieType(csharp, (TypeDefinition)member, output, options);
                return output.ToString();
            }
            else
            {
                MessageBox.Show("Not TypeDefinition");
            }
            return null;
        }
        public string getSourceCode(MethodDefinition methodDefinition)
        {
            try
            {
	            var csharpLanguage = new CSharpLanguage();
				var textOutput = new PlainTextOutput();
				var decompilationOptions = new DecompilationOptions();
 				decompilationOptions.FullDecompilation = true;
				csharpLanguage.DecompileMethod(methodDefinition, textOutput, decompilationOptions);
				return textOutput.ToString();
            	
             /*   ILanguage language = CSharp.GetLanguage(CSharpVersion.V1);
                language.GetWriter(new PlainTextFormatter(writer)).Write(method);
                MemoryStream stream = new MemoryStream();
                StreamWriter writer3 = new StreamWriter(stream);
                language.GetWriter(new PlainTextFormatter(writer3)).Write(method);
                stream.Flush();*/
                
            }
            catch (Exception exception)
            {
                PublicDI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
                return ("Error in creating source code from IL: " + exception.Message);
            }
        }
		public string getSourceCode(TypeDefinition typeDefinition)
        {
            try
            {
	            var csharpLanguage = new CSharpLanguage();
				var textOutput = new PlainTextOutput();
				var decompilationOptions = new DecompilationOptions();
 				decompilationOptions.FullDecompilation = true;
				csharpLanguage.DecompileType(typeDefinition, textOutput, decompilationOptions);
				return textOutput.ToString();
			}
			catch (Exception exception)
            {
                PublicDI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
                return ("Error in creating source code from Type: " + exception.Message);
            }
        }		
Exemple #10
0
		public static void Main (string[] args)
		{

			string appPath = null;
			string slnName = null;
			string libPath = null;
			string expOpt = null;
			string outLanguageType = LAN_TYPE_CSHARP;
			DecompilerSettings ds = new DecompilerSettings ();
			ds.AnonymousMethods = true;
			ds.AsyncAwait = true;
			ds.YieldReturn = true;
			string onlyDecomileClassName = null;

			List<string> onlyDecompilingFileNameList = new List<string> ();
			//parsing args
			foreach (string x in args) {

				if (x.StartsWith ("-")) {
					switch (x) {
					case "-n":
					case "-l":
					case "-t":
					case "-C":
					case "-D":
						expOpt = x;
						continue;
					
					default:

						if (x.StartsWith ("-")) {

							if (x.Length < 2) {
								Console.WriteLine (" Unexpected options " + x);
								showUsage ();
								return;
							}

							for (int i = 0; i < x.Length; i++) {
								if (!praseDecompileSetting (x [i], ds)) {
									Console.WriteLine (" Unexpected options " + x);
									showUsage ();
									return;
								}

							}
							continue;
						} 

						break;
					}

				} else if (expOpt != null) {

					switch (expOpt) {
					case "-n":
						slnName = x;
						expOpt = null;
						break;
					case "-l":
						libPath = x;
						expOpt = null;
						break;
					case "-t":
						if (x != LAN_TYPE_CSHARP && x != LAN_TYPE_IL) {
							Console.WriteLine (" Unexpected Output language type: " + x);
							showUsage ();
							return;
						}
						outLanguageType = x;
						expOpt = null;
						break;
					case "-C":
						onlyDecomileClassName = x;
						expOpt = null;
						break;
					case "-D":
						onlyDecompilingFileNameList.Add (x);
						break;
					default:
						showUsage ();
						expOpt = null;
						return;
					
					}


				} else {
					if (appPath == null) {
						appPath = x;
						continue;
					} else {
						Console.WriteLine (" Unexpected options " + x);
						showUsage ();
						return;
					}
				}
					
			}


			if (appPath == null) {
			
				Console.WriteLine ("directory/to/all/your/dll missing");
				showUsage ();
				return;
			}

			if (slnName == null && outLanguageType==LAN_TYPE_CSHARP) {

				Console.WriteLine ("Solution Name missing");
				showUsage ();
				return;
			}


			Console.WriteLine ("Decompiling all dll in  " + appPath);
			Console.WriteLine ("Please wait...");

			DirectoryInfo di = new DirectoryInfo(appPath);
			appPath = di.FullName;
			FileInfo[] dllFileInfoList = di.GetFiles("*.dll");
			FileInfo[] exeFileInfoList = di.GetFiles ("*.exe");


			AssemblyList asmlist = new AssemblyList ("mylistname");

			foreach (var dllfile in dllFileInfoList)
			{
				bool bDecompile = isDecompilingFile (dllfile.FullName, onlyDecompilingFileNameList);
				asmlist.OpenAssembly (dllfile.FullName,!bDecompile);
			}

			foreach (var dllfile in exeFileInfoList)
			{
				bool bDecompile = isDecompilingFile (dllfile.FullName, onlyDecompilingFileNameList);

				asmlist.OpenAssembly (dllfile.FullName,!bDecompile);
			}


			if (libPath != null) {
				di = new DirectoryInfo(libPath);
				libPath = di.FullName;
				dllFileInfoList = di.GetFiles("*.dll");
				foreach (var dllfile in dllFileInfoList) {
					asmlist.OpenAssembly (dllfile.FullName,true);
				}
			}
			


			StringBuilder projSln = new StringBuilder ();
			projSln.Append ("Microsoft Visual Studio Solution File, Format Version 11.00\n# Visual Studio 2010\n");

			StringBuilder globSec = new StringBuilder ();
			Guid slnProjGuid =  Guid.NewGuid();

			int num = 0;
			LoadedAssembly [] ls = asmlist.GetAssemblies ();
			var decompilationOptions = new DecompilationOptions ();
			decompilationOptions.FullDecompilation = true;
			decompilationOptions.assenmlyList = asmlist;
			decompilationOptions.DecompilerSettings = ds;
			decompilationOptions.IncludedClassName = onlyDecomileClassName;

			if(outLanguageType==LAN_TYPE_CSHARP)
			{
				foreach (LoadedAssembly asm in ls) {
					if (asm.IsAutoLoaded)
						continue;




					string projectPath = appPath + "/"+ asm.ShortName;
					if(!Directory.Exists(projectPath))
					{
						Directory.CreateDirectory (projectPath);
					}
					string projectFileName = projectPath + "/" + asm.ShortName + ".csproj";
					asm.ProjectGuid = Guid.NewGuid();
					asm.ProjectFileName = projectFileName;
				}
			}



			foreach (LoadedAssembly asm in ls) {
				num++;
				Console.WriteLine(asm.FileName + " " + num+"/"+ls.Length);
				if (asm.IsAutoLoaded)
					continue;

				if(outLanguageType==LAN_TYPE_CSHARP)
				{
					var csharpLanguage = new CSharpLanguage ();
					var textOutput = new PlainTextOutput ();
					decompilationOptions.SaveAsProjectDirectory =  appPath + "/"+ asm.ShortName;

					csharpLanguage.DecompileAssembly (asm, textOutput, decompilationOptions);
					File.WriteAllText (asm.ProjectFileName, textOutput.ToString ());

					
					Guid createdProjGuid = asm.ProjectGuid;
					
					projSln.Append("   Project(\"{");
					projSln.Append (slnProjGuid.ToString());
					projSln.Append ("}\") = \"");
					projSln.Append (asm.ShortName);
					projSln.Append ("\", \"");
					projSln.Append (asm.ShortName+"/"+ asm.ShortName + ".csproj");
					projSln.Append ("\", \"{");
					projSln.Append (createdProjGuid.ToString());
					projSln.Append ("}\"\n");
					projSln.Append("EndProject\n");

					globSec.Append ("   {"+createdProjGuid.ToString()+"}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
					globSec.Append ("   {"+createdProjGuid.ToString()+"}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
					globSec.Append ("   {"+createdProjGuid.ToString()+"}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
					globSec.Append ("   {"+createdProjGuid.ToString()+"}.Release|Any CPU.Build.0 = Release|Any CPU\n");
				}
				else
				{
					var ilLanguage = new ILLanguage(true);
					var textOutput = new PlainTextOutput ();
					ilLanguage.DecompileAssembly (asm, textOutput, decompilationOptions);
					string ilFileName = appPath + "/"+ asm.ShortName+".il";
					File.WriteAllText(ilFileName,textOutput.ToString());
				}

			}

			if (outLanguageType == LAN_TYPE_CSHARP) {
				projSln.Append ("Global\n");
				projSln.Append ("GlobalSection(SolutionConfigurationPlatforms) = preSolution\n");
				projSln.Append ("\t\t\t\tDebug|Any CPU = Debug|Any CPU\n");
				projSln.Append ("\t\t\t\tRelease|Any CPU = Release|Any CPU\n");
				projSln.Append ("EndGlobalSection\n");

				projSln.Append ("GlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
				projSln.Append (globSec.ToString ());
				projSln.Append ("EndGlobalSection\n");

				projSln.Append ("GlobalSection(MonoDevelopProperties) = preSolution\n");
				projSln.Append ("\nEndGlobalSection\n");
				projSln.Append ("EndGlobal\n\t\t");

				string slnFileName = appPath + "/" + slnName + ".sln";
				File.WriteAllText (slnFileName, projSln.ToString ());
			}
		

		}
Exemple #11
0
            public MethodWriter(CSharpLanguage lang, ITextOutput output, IMethod method)
            {
                this.lang = lang;
                this.output = output;
                this.typeGenericParams = null;
                this.methodGenericParams = null;
                this.methodSig = method.MethodSig;

                this.md = method as MethodDef;
                var ms = method as MethodSpec;
                var mr = method as MemberRef;
                if (ms != null) {
                    var ts = ms.Method == null ? null : ms.Method.DeclaringType as TypeSpec;
                    if (ts != null) {
                        var gp = ts.TypeSig.RemovePinnedAndModifiers() as GenericInstSig;
                        if (gp != null)
                            typeGenericParams = gp.GenericArguments;
                    }

                    var gsSig = ms.GenericInstMethodSig;
                    if (gsSig != null)
                        methodGenericParams = gsSig.GenericArguments;

                    this.md = ms.Method.ResolveMethodDef();
                }
                else if (mr != null) {
                    var ts = mr.DeclaringType as TypeSpec;
                    if (ts != null) {
                        var gp = ts.TypeSig.RemovePinnedAndModifiers() as GenericInstSig;
                        if (gp != null)
                            typeGenericParams = gp.GenericArguments;
                    }

                    this.md = mr.ResolveMethod();
                }

                if (typeGenericParams != null || methodGenericParams != null)
                    this.methodSig = GenericArgumentResolver.Resolve(methodSig, typeGenericParams, methodGenericParams);
            }
            public myFullDecompiler(CSharpLanguage lang, LoadedAssembly asm, ITextOutput output, DecompilationOptions options)
            {
                iLang = lang;
                iAsm = asm;
                iOutput = output;
                iOptions = options;

                iIsWpfProject = asm.ModuleDefinition.Assembly.CustomAttributes.Any(x => x.AttributeType.FullName == "System.Windows.ThemeInfoAttribute");
                iCsRelativeFileSet = FindCsRelativeResourcesFiles();
            }
Exemple #13
0
		private void SetupDecompilerAndOriginalSource(MethodDefinition method)
		{
			_cSharpDecompiler = new CSharpLanguage();
            var decompilationOutput = new PlainTextOutput();
            _decompilationOptions = new DecompilationOptions();
            _cSharpDecompiler.DecompileMethod(method, decompilationOutput, _decompilationOptions);
            _oldText = decompilationOutput.ToString();
            _differ = new SideBySideDiffBuilder(new Differ());
		}