public override Task <DecompileTypeResponse> DecompileType(DecompileTypeRequest request, ServerCallContext context) { if (!this.decompilationContext.DecompilationContext.FilePathToType.ContainsKey(request.FilePath)) { throw new RpcException(new Status(StatusCode.NotFound, "No type to corresponding file path")); } TypeDefinition type = this.decompilationContext.DecompilationContext.FilePathToType[request.FilePath]; IExceptionFormatter exceptionFormatter = SimpleExceptionFormatter.Instance; ILanguage language = LanguageFactory.GetLanguage(CSharpVersion.V7); StringWriter theWriter = new StringWriter(); CodeFormatter formatter = new CodeFormatter(theWriter); IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true, writeFullyQualifiedNames: false, writeDocumentation: true, showCompilerGeneratedMembers: false, writeLargeNumbersInHex: false); ILanguageWriter writer = language.GetWriter(formatter, exceptionFormatter, settings); IWriterContextService writerContextService = new TypeCollisionWriterContextService(new ProjectGenerationDecompilationCacheService(), true); try { List <WritingInfo> infos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService); DecompiledTypeMetadata decompiledTypeMetadata = new DecompiledTypeMetadata(); decompiledTypeMetadata.CodeSpanToMemberReference.AddRange(formatter.CodeSpanToMemberReference); foreach (WritingInfo info in infos) { decompiledTypeMetadata.MemberDeclarationToCodeSpan.AddRange(info.MemberDeclarationToCodeSpan); decompiledTypeMetadata.CodeMappingInfo.NodeToCodeMap.AddRange(info.CodeMappingInfo.NodeToCodeMap); decompiledTypeMetadata.CodeMappingInfo.InstructionToCodeMap.AddRange(info.CodeMappingInfo.InstructionToCodeMap); decompiledTypeMetadata.CodeMappingInfo.FieldConstantValueToCodeMap.AddRange(info.CodeMappingInfo.FieldConstantValueToCodeMap); decompiledTypeMetadata.CodeMappingInfo.VariableToCodeMap.AddRange(info.CodeMappingInfo.VariableToCodeMap); decompiledTypeMetadata.CodeMappingInfo.ParameterToCodeMap.AddRange(info.CodeMappingInfo.ParameterToCodeMap); decompiledTypeMetadata.CodeMappingInfo.MethodDefinitionToMethodReturnTypeCodeMap.AddRange(info.CodeMappingInfo.MethodDefinitionToMethodReturnTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.FieldDefinitionToFieldTypeCodeMap.AddRange(info.CodeMappingInfo.FieldDefinitionToFieldTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.PropertyDefinitionToPropertyTypeCodeMap.AddRange(info.CodeMappingInfo.PropertyDefinitionToPropertyTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.EventDefinitionToEventTypeCodeMap.AddRange(info.CodeMappingInfo.EventDefinitionToEventTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.ParameterDefinitionToParameterTypeCodeMap.AddRange(info.CodeMappingInfo.ParameterDefinitionToParameterTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.VariableDefinitionToVariableTypeCodeMap.AddRange(info.CodeMappingInfo.VariableDefinitionToVariableTypeCodeMap); } this.decompilationContext.AddTypeMetadataToCache(type, decompiledTypeMetadata); return(Task.FromResult(new DecompileTypeResponse() { SourceCode = theWriter.ToString() })); } catch (Exception e) { string[] exceptionMessageLines = exceptionFormatter.Format(e, type.FullName, null); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); return(Task.FromResult(new DecompileTypeResponse() { SourceCode = commentedExceptionMessage })); } }
public bool WriteTypeToFile(TypeDefinition type, IProjectItemFileWriter itemWriter, Dictionary <string, ICollection <string> > membersToSkip, bool shouldBePartial, ILanguage language, out List <WritingInfo> writingInfos, out string theCodeString) { theCodeString = string.Empty; writingInfos = null; StringWriter theWriter = new StringWriter(); bool showCompilerGeneratedMembers = Utilities.IsVbInternalTypeWithoutRootNamespace(type) || Utilities.IsVbInternalTypeWithRootNamespace(type); IFormatter formatter = GetFormatter(theWriter); IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true, writeFullyQualifiedNames: decompilationPreferences.WriteFullNames, writeDocumentation: decompilationPreferences.WriteDocumentation, showCompilerGeneratedMembers: showCompilerGeneratedMembers, writeLargeNumbersInHex: decompilationPreferences.WriteLargeNumbersInHex); ILanguageWriter writer = language.GetWriter(formatter, this.exceptionFormater, settings); IWriterContextService writerContextService = this.GetWriterContextService(); writer.ExceptionThrown += OnExceptionThrown; writerContextService.ExceptionThrown += OnExceptionThrown; bool exceptionOccurred = false; try { if (!(writer is INamespaceLanguageWriter)) { writingInfos = writer.Write(type, writerContextService); } else { if (shouldBePartial) { writingInfos = (writer as INamespaceLanguageWriter).WritePartialTypeAndNamespaces(type, writerContextService, membersToSkip); } else { writingInfos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService); } } this.RecordGeneratedFileData(type, itemWriter.FullSourceFilePath, theWriter, formatter, writerContextService, writingInfos); MemoryStream sourceFileStream = new MemoryStream(Encoding.UTF8.GetBytes(theWriter.ToString())); itemWriter.CreateProjectSourceFile(sourceFileStream); sourceFileStream.Close(); theWriter.Close(); } catch (Exception e) { exceptionOccurred = true; string[] exceptionMessageLines = exceptionFormater.Format(e, type.FullName, itemWriter.FullSourceFilePath); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); itemWriter.CreateProjectSourceFile(new MemoryStream(Encoding.UTF8.GetBytes(commentedExceptionMessage))); OnExceptionThrown(this, e); } theCodeString = theWriter.ToString(); writer.ExceptionThrown -= OnExceptionThrown; writerContextService.ExceptionThrown -= OnExceptionThrown; return(exceptionOccurred || writerContextService.ExceptionsWhileDecompiling.Any()); }
public bool WriteTypeToFile(TypeDefinition type, IProjectItemFileWriter itemWriter, Dictionary<string, ICollection<string>> membersToSkip, bool shouldBePartial, ILanguage language, out List<WritingInfo> writingInfos, out string theCodeString) { theCodeString = string.Empty; writingInfos = null; StringWriter theWriter = new StringWriter(); IFormatter formatter = GetFormatter(theWriter); ILanguageWriter writer = language.GetWriter(formatter, this.exceptionFormater, true); IWriterContextService writerContextService = this.GetWriterContextService(); bool exceptionOccurred = false; try { bool showCompilerGeneratedMembers = Utilities.IsVbInternalTypeWithoutRootNamespace(type) || Utilities.IsVbInternalTypeWithRootNamespace(type); if (!(writer is INamespaceLanguageWriter)) { writingInfos = writer.Write(type, writerContextService, decompilationPreferences.WriteDocumentation, showCompilerGeneratedMembers); } else { if (shouldBePartial) { writingInfos = (writer as INamespaceLanguageWriter).WritePartialTypeAndNamespaces(type, writerContextService, showCompilerGeneratedMembers, decompilationPreferences.WriteFullNames, decompilationPreferences.WriteDocumentation, membersToSkip); } else { writingInfos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService, decompilationPreferences.WriteDocumentation, showCompilerGeneratedMembers, decompilationPreferences.WriteFullNames); } } RecordGeneratedFileData(type, itemWriter.FullSourceFilePath, theWriter, formatter, writerContextService, writingInfos); MemoryStream sourceFileStream = new MemoryStream(Encoding.UTF8.GetBytes(theWriter.ToString())); itemWriter.CreateProjectSourceFile(sourceFileStream); sourceFileStream.Close(); theWriter.Close(); } catch (Exception e) { exceptionOccurred = true; string[] exceptionMessageLines = exceptionFormater.Format(e, type.FullName, itemWriter.FullSourceFilePath); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); itemWriter.CreateProjectSourceFile(new MemoryStream(Encoding.UTF8.GetBytes(commentedExceptionMessage))); } theCodeString = theWriter.ToString(); return exceptionOccurred || writerContextService.ExceptionsWhileDecompiling.Any(); }