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{
@@ -65,7 +65,7 @@ public string GetIl(string code)
6565
6666 namespace Eval
6767 {{
68- public class Code
68+ public unsafe class Code
6969 {{
7070 public object Main()
7171 {{
@@ -75,17 +75,22 @@ 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 )
86+ . WithPlatform ( Platform . AnyCpu ) ;
8287
83- var compilation = CSharpCompilation . Create ( Guid . NewGuid ( ) . ToString ( ) , options : compOpts , references : References ) . AddSyntaxTrees ( scriptSyntaxTree ) ;
88+ var compilation = CSharpCompilation . Create ( Guid . NewGuid ( ) . ToString ( ) , options : compOpts , references : References )
89+ . AddSyntaxTrees ( scriptSyntaxTree ) ;
8490
8591 var sb = new StringBuilder ( ) ;
86- using var pdb = new MemoryStream ( ) ;
8792 using var dll = new MemoryStream ( ) ;
88- var result = compilation . Emit ( dll , pdb ) ;
93+ var result = compilation . Emit ( dll ) ;
8994 if ( ! result . Success )
9095 {
9196 sb . AppendLine ( "Emit Failed" ) ;
@@ -94,19 +99,22 @@ public object Main()
9499 else
95100 {
96101 dll . Seek ( 0 , SeekOrigin . Begin ) ;
97- using var module = ModuleDefinition . ReadModule ( dll ) ;
102+ using var file = new PEFile ( compilation . AssemblyName ! , dll ) ;
98103 using var writer = new StringWriter ( sb ) ;
99- module . Name = compilation . AssemblyName ;
100104 var plainOutput = new PlainTextOutput ( writer ) ;
101105 var rd = new ReflectionDisassembler ( plainOutput , CancellationToken . None )
102106 {
103107 DetectControlStructure = true
104108 } ;
105109 var ignoredMethods = new [ ] { ".ctor" } ;
106- var methods = module . Types . SelectMany ( a => a . Methods ) . Where ( a => ! ignoredMethods . Contains ( a . Name ) ) ;
110+ var methods = file . Metadata . MethodDefinitions . Where ( a =>
111+ {
112+ var methodName = file . Metadata . GetString ( file . Metadata . GetMethodDefinition ( a ) . Name ) ;
113+ return ! ignoredMethods . Contains ( methodName ) ;
114+ } ) ;
107115 foreach ( var method in methods )
108116 {
109- rd . DisassembleMethod ( method ) ;
117+ rd . DisassembleMethod ( file , method ) ;
110118 plainOutput . WriteLine ( ) ;
111119 }
112120 }
0 commit comments