22using ICSharpCode . Decompiler . Disassembler ;
33using Microsoft . CodeAnalysis ;
44using Microsoft . CodeAnalysis . CSharp ;
5- using Mono . Cecil ;
65using Newtonsoft . Json ;
76using System ;
87using System . Collections . Generic ;
1514using System . Text ;
1615using System . Text . RegularExpressions ;
1716using System . Threading ;
17+ using ICSharpCode . Decompiler . Metadata ;
1818
1919namespace CSDiscordService . Eval
2020{
@@ -75,17 +75,21 @@ public object Main()
7575 }}
7676 " ;
7777
78- var opts = CSharpParseOptions . Default . WithLanguageVersion ( LanguageVersion . Preview ) . WithKind ( SourceCodeKind . Regular ) ;
78+ var opts = CSharpParseOptions . Default
79+ . WithLanguageVersion ( LanguageVersion . Preview )
80+ . WithKind ( SourceCodeKind . Regular ) ;
7981
8082 var scriptSyntaxTree = CSharpSyntaxTree . ParseText ( toExecute , opts ) ;
81- var compOpts = new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) . WithOptimizationLevel ( OptimizationLevel . Debug ) . WithAllowUnsafe ( true ) . WithPlatform ( Platform . AnyCpu ) ;
83+ var compOpts = new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary )
84+ . WithOptimizationLevel ( OptimizationLevel . Debug )
85+ . WithAllowUnsafe ( true ) . WithPlatform ( Platform . AnyCpu ) ;
8286
83- var compilation = CSharpCompilation . Create ( Guid . NewGuid ( ) . ToString ( ) , options : compOpts , references : References ) . AddSyntaxTrees ( scriptSyntaxTree ) ;
87+ var compilation = CSharpCompilation . Create ( Guid . NewGuid ( ) . ToString ( ) , options : compOpts , references : References )
88+ . AddSyntaxTrees ( scriptSyntaxTree ) ;
8489
8590 var sb = new StringBuilder ( ) ;
86- using var pdb = new MemoryStream ( ) ;
8791 using var dll = new MemoryStream ( ) ;
88- var result = compilation . Emit ( dll , pdb ) ;
92+ var result = compilation . Emit ( dll ) ;
8993 if ( ! result . Success )
9094 {
9195 sb . AppendLine ( "Emit Failed" ) ;
@@ -94,19 +98,22 @@ public object Main()
9498 else
9599 {
96100 dll . Seek ( 0 , SeekOrigin . Begin ) ;
97- using var module = ModuleDefinition . ReadModule ( dll ) ;
101+ using var file = new PEFile ( compilation . AssemblyName ! , dll ) ;
98102 using var writer = new StringWriter ( sb ) ;
99- module . Name = compilation . AssemblyName ;
100103 var plainOutput = new PlainTextOutput ( writer ) ;
101104 var rd = new ReflectionDisassembler ( plainOutput , CancellationToken . None )
102105 {
103106 DetectControlStructure = true
104107 } ;
105108 var ignoredMethods = new [ ] { ".ctor" } ;
106- var methods = module . Types . SelectMany ( a => a . Methods ) . Where ( a => ! ignoredMethods . Contains ( a . Name ) ) ;
109+ var methods = file . Metadata . MethodDefinitions . Where ( a =>
110+ {
111+ var methodName = file . Metadata . GetString ( file . Metadata . GetMethodDefinition ( a ) . Name ) ;
112+ return ! ignoredMethods . Contains ( methodName ) ;
113+ } ) ;
107114 foreach ( var method in methods )
108115 {
109- rd . DisassembleMethod ( method ) ;
116+ rd . DisassembleMethod ( file , method ) ;
110117 plainOutput . WriteLine ( ) ;
111118 }
112119 }
0 commit comments