Exemple #1
0
		IEnumerable<Tuple<string, string>> WriteCodeFilesInProject(ModuleDefinition module, DecompilationOptions options, HashSet<string> directories)
		{
			var files = module.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy(
				delegate(TypeDefinition type) {
					string file = TextView.DecompilerTextView.CleanUpName(type.Name) + this.FileExtension;
					if (string.IsNullOrEmpty(type.Namespace)) {
						return file;
					} else {
						string dir = TextView.DecompilerTextView.CleanUpName(type.Namespace);
						if (directories.Add(dir))
							Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dir));
						return Path.Combine(dir, file);
					}
				}, StringComparer.OrdinalIgnoreCase).ToList();
			AstMethodBodyBuilder.ClearUnhandledOpcodes();
			Parallel.ForEach(
				files,
				new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
				delegate(IGrouping<string, TypeDefinition> file) {
					using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) {
						AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
						foreach (TypeDefinition type in file) {
							codeDomBuilder.AddType(type);
						}
						RunTransformsAndGenerateCode(codeDomBuilder, new PlainTextOutput(w), options, module);
					}
				});
			AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes();
			return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(module, options, directories));
		}
Exemple #2
0
        IEnumerable <Tuple <string, string> > WriteCodeFilesInProject(ModuleDefinition module, DecompilationOptions options, HashSet <string> directories)
        {
            var files = module.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy(
                delegate(TypeDefinition type) {
                string file = TextView.DecompilerTextView.CleanUpName(type.Name) + this.FileExtension;
                // TODO Find more proper way to know root namespace?
                string rootNs = type.Module.Assembly.Name.Name;
                // Cut root namespace from sub-directory name for decompiled source files
                // TODO Control namespaces cutting with some assembly settings option?
                if (string.IsNullOrEmpty(type.Namespace) || type.Namespace.Equals(rootNs, StringComparison.Ordinal))
                {
                    return(file);
                }
                else
                {
                    string dir = type.Namespace;
                    if (dir.StartsWith(rootNs + ".", StringComparison.Ordinal))
                    {
                        dir = dir.Substring(rootNs.Length + 1);
                    }
                    // Create sub-directories for each namespace part
                    dir = TextView.DecompilerTextView.CleanUpName(dir).Replace('.', Path.DirectorySeparatorChar);
                    if (directories.Add(dir))
                    {
                        Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dir));
                    }
                    return(Path.Combine(dir, file));
                }
            }, StringComparer.OrdinalIgnoreCase).ToList();

            AstMethodBodyBuilder.ClearUnhandledOpcodes();
            Parallel.ForEach(
                files,
                new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            },
                delegate(IGrouping <string, TypeDefinition> file) {
                using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) {
                    AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
                    foreach (TypeDefinition type in file)
                    {
                        codeDomBuilder.AddType(type);
                    }
                    codeDomBuilder.RunTransformations(transformAbortCondition);
                    codeDomBuilder.GenerateCode(new PlainTextOutput(w));
                }
            });
            AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes();
            return(files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(module, options, directories)));
        }
Exemple #3
0
        IEnumerable <Tuple <string, string> > WriteCodeFilesInProject(ModuleDefinition module, DecompilationOptions options, HashSet <string> directories)
        {
            var files = module.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy(
                delegate(TypeDefinition type)
            {
                string file = CleanUpName(type.Name);
                if (string.IsNullOrEmpty(type.Namespace))
                {
                    return(file);
                }
                else
                {
                    return(file);

                    /*string dir = CleanUpName(type.Namespace);
                     * if (OnlyIncludeNameSpace == null || OnlyIncludeNameSpace == type.Namespace)
                     * {
                     *  if (directories.Add(dir))
                     *      Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dir));
                     * }*/
                    //return Path.Combine(dir, file);
                }
            }, StringComparer.OrdinalIgnoreCase).ToList();

            AstMethodBodyBuilder.ClearUnhandledOpcodes();
            Parallel.ForEach(
                files,
                new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            },
                delegate(IGrouping <string, TypeDefinition> file)
            {
                var path     = options.SaveAsProjectDirectory;
                var t        = file.First <TypeDefinition>();
                string fname = file.Key;
                if (IsNeedWriteHpp(t, OnlyIncludeNameSpace))
                {
                    var HppPath = Path.Combine(path, "include");
                    HppPath     = Path.Combine(HppPath, "QuantKit");
                    Directory.CreateDirectory(HppPath);
                    using (StreamWriter w = new StreamWriter(Path.Combine(HppPath, file.Key + HppFileExtension)))
                    {
/*                            AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
 *                          foreach (TypeDefinition type in file)
 *                          {
 *                              codeDomBuilder.AddType(type);
 *                          }
 *                          PreProcess(module);
 *                          codeDomBuilder.RunTransformations(transformAbortCondition);*/
                        //WriteHppCode(t, FindDeclaration(t, codeDomBuilder), new PlainTextOutput(w));
                        WriteHppCode(t, new PlainTextOutput(w));
                    }
                }

                if (IsNeedWriteHxx(t, OnlyIncludeNameSpace))
                {
                    var HxxPath = Path.Combine(path, "src");
                    Directory.CreateDirectory(HxxPath);
                    using (StreamWriter w = new StreamWriter(Path.Combine(HxxPath, file.Key + HxxFileExtension)))
                    {
                        AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
                        foreach (TypeDefinition type in file)
                        {
                            codeDomBuilder.AddType(type);
                        }
                        PreProcess(module);
                        codeDomBuilder.RunTransformations(transformAbortCondition);
                        WriteHxxCode(t, new PlainTextOutput(w));
                    }
                }
                if (IsNeedWriteCpp(t, OnlyIncludeNameSpace))
                {
                    var CppPath = Path.Combine(path, "src");
                    Directory.CreateDirectory(CppPath);
                    using (StreamWriter w = new StreamWriter(Path.Combine(CppPath, file.Key + CppFileExtension)))
                    {
                        AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
                        foreach (TypeDefinition type in file)
                        {
                            codeDomBuilder.AddType(type);
                        }
                        PreProcess(module);
                        codeDomBuilder.RunTransformations(transformAbortCondition);
                        WriteCppCode(t, new PlainTextOutput(w));
                    }
                }

                /*using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key)))
                 * {
                 *  AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
                 *  foreach (TypeDefinition type in file)
                 *  {
                 *      codeDomBuilder.AddType(type);
                 *  }
                 *  PreProcess(module);
                 *  codeDomBuilder.RunTransformations(transformAbortCondition);
                 *  GenerateCplusplusCode(codeDomBuilder, new PlainTextOutput(w), "SmartQuant");
                 * }*/
            });
            AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes();
            return(files.Select(f => Tuple.Create("Compile", f.Key)));
        }