15
15
// You should have received a copy of the GNU General Public License
16
16
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17
17
18
- using Microsoft . CodeDom . Providers . DotNetCompilerPlatform ;
19
18
using Orts . Simulation ;
20
19
using ORTS . Common ;
21
20
using System ;
22
- using System . CodeDom . Compiler ;
23
21
using System . Collections . Generic ;
24
22
using System . Diagnostics ;
25
23
using System . IO ;
24
+ using System . Linq ;
26
25
using System . Reflection ;
27
26
using System . Text ;
28
27
using System . Threading ;
28
+ using Microsoft . CodeAnalysis ;
29
+ using Microsoft . CodeAnalysis . CSharp ;
29
30
30
31
namespace Orts . Common . Scripting
31
32
{
@@ -34,22 +35,17 @@ public class ScriptManager
34
35
{
35
36
readonly Simulator Simulator ;
36
37
readonly IDictionary < string , Assembly > Scripts = new Dictionary < string , Assembly > ( ) ;
37
- static readonly ProviderOptions ProviderOptions = new ProviderOptions ( Path . Combine ( new Uri ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . CodeBase ) ) . LocalPath , "roslyn" , "csc.exe" ) , 10 ) ;
38
- static readonly CSharpCodeProvider Compiler = new CSharpCodeProvider ( ProviderOptions ) ;
39
-
40
- static CompilerParameters GetCompilerParameters ( )
38
+ static readonly string [ ] ReferenceAssemblies = new [ ]
41
39
{
42
- var cp = new CompilerParameters ( )
43
- {
44
- GenerateInMemory = true ,
45
- IncludeDebugInformation = Debugger . IsAttached ,
46
- } ;
47
- cp . ReferencedAssemblies . Add ( "System.dll" ) ;
48
- cp . ReferencedAssemblies . Add ( "System.Core.dll" ) ;
49
- cp . ReferencedAssemblies . Add ( "ORTS.Common.dll" ) ;
50
- cp . ReferencedAssemblies . Add ( "Orts.Simulation.dll" ) ;
51
- return cp ;
52
- }
40
+ typeof ( System . Object ) . GetTypeInfo ( ) . Assembly . Location ,
41
+ typeof ( System . Diagnostics . Debug ) . GetTypeInfo ( ) . Assembly . Location ,
42
+ typeof ( ORTS . Common . ElapsedTime ) . GetTypeInfo ( ) . Assembly . Location ,
43
+ typeof ( ORTS . Scripting . Api . Timer ) . GetTypeInfo ( ) . Assembly . Location ,
44
+ } ;
45
+ static MetadataReference [ ] References = ReferenceAssemblies . Select ( r => MetadataReference . CreateFromFile ( r ) ) . ToArray ( ) ;
46
+ static CSharpCompilationOptions CompilationOptions = new CSharpCompilationOptions (
47
+ OutputKind . DynamicallyLinkedLibrary ,
48
+ optimizationLevel : Debugger . IsAttached ? OptimizationLevel . Debug : OptimizationLevel . Release ) ;
53
49
54
50
[ CallOnThread ( "Loader" ) ]
55
51
internal ScriptManager ( Simulator simulator )
@@ -86,22 +82,40 @@ private static Assembly CompileScript(string path)
86
82
{
87
83
try
88
84
{
89
- var compilerResults = Compiler . CompileAssemblyFromFile ( GetCompilerParameters ( ) , path ) ;
90
- if ( ! compilerResults . Errors . HasErrors )
85
+ var scriptName = Path . GetFileName ( path ) ;
86
+ var scriptCode = File . ReadAllText ( path ) ;
87
+ var syntaxTree = CSharpSyntaxTree . ParseText ( scriptCode ) ;
88
+ var compilation = CSharpCompilation . Create (
89
+ scriptName ,
90
+ new [ ] { syntaxTree } ,
91
+ References ,
92
+ CompilationOptions ) ;
93
+ var ms = new MemoryStream ( ) ;
94
+ var result = compilation . Emit ( ms ) ;
95
+ if ( result . Success )
91
96
{
92
- var script = compilerResults . CompiledAssembly ;
97
+ ms . Seek ( 0 , SeekOrigin . Begin ) ;
98
+ var script = Assembly . Load ( ms . ToArray ( ) ) ;
99
+ // in netcore:
100
+ //var script = AssemblyLoadContext.Default.LoadFromStream(ms);
93
101
if ( script == null )
94
102
Trace . TraceWarning ( $ "Script file { path } could not be loaded into the process.") ;
95
103
return script ;
96
104
}
97
105
else
98
106
{
107
+ var errors = result . Diagnostics . Where ( diagnostic => diagnostic . IsWarningAsError || diagnostic . Severity == DiagnosticSeverity . Error ) ;
108
+
99
109
var errorString = new StringBuilder ( ) ;
100
110
errorString . AppendFormat ( "Skipped script {0} with error:" , path ) ;
101
111
errorString . Append ( Environment . NewLine ) ;
102
- foreach ( CompilerError error in compilerResults . Errors )
112
+ foreach ( var error in errors )
103
113
{
104
- errorString . AppendFormat ( " {0}, line: {1}, column: {2}" , error . ErrorText , error . Line /*- prefixLines*/ , error . Column ) ;
114
+ var textSpan = error . Location . SourceSpan ;
115
+ var lineSpan = error . Location . SourceTree . GetLineSpan ( textSpan ) ;
116
+ var line = lineSpan . StartLinePosition . Line + 1 ;
117
+ var column = lineSpan . StartLinePosition . Character ;
118
+ errorString . AppendFormat ( "\t {0}: {1}, line: {2}, column: {3}" , error . Id , error . GetMessage ( ) , line , column ) ;
105
119
errorString . Append ( Environment . NewLine ) ;
106
120
}
107
121
@@ -126,6 +140,8 @@ private static Assembly CompileScript(string path)
126
140
127
141
public Assembly LoadFolder ( string path )
128
142
{
143
+ return null ;
144
+ /*
129
145
if (Thread.CurrentThread.Name != "Loader Process")
130
146
Trace.TraceError("ScriptManager.Load incorrectly called by {0}; must be Loader Process or crashes will occur.", Thread.CurrentThread.Name);
131
147
@@ -152,7 +168,7 @@ public Assembly LoadFolder(string path)
152
168
errorString.Append(Environment.NewLine);
153
169
foreach (CompilerError error in compilerResults.Errors)
154
170
{
155
- errorString . AppendFormat ( " {0}, file: {1}, line: {2}, column: {3}" , error . ErrorText , error . FileName , error . Line /*- prefixLines*/ , error . Column ) ;
171
+ errorString.AppendFormat(" {0}, file: {1}, line: {2}, column: {3}", error.ErrorText, error.FileName, error.Line, error.Column);
156
172
errorString.Append(Environment.NewLine);
157
173
}
158
174
@@ -170,6 +186,7 @@ public Assembly LoadFolder(string path)
170
186
Trace.WriteLine(new FileLoadException(path, error));
171
187
return null;
172
188
}
189
+ */
173
190
}
174
191
175
192
/*
0 commit comments