1515// You should have received a copy of the GNU General Public License
1616// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717
18- using Microsoft . CodeDom . Providers . DotNetCompilerPlatform ;
1918using Orts . Simulation ;
2019using ORTS . Common ;
2120using System ;
22- using System . CodeDom . Compiler ;
2321using System . Collections . Generic ;
2422using System . Diagnostics ;
2523using System . IO ;
24+ using System . Linq ;
2625using System . Reflection ;
2726using System . Text ;
2827using System . Threading ;
28+ using Microsoft . CodeAnalysis ;
29+ using Microsoft . CodeAnalysis . CSharp ;
2930
3031namespace Orts . Common . Scripting
3132{
@@ -34,22 +35,17 @@ public class ScriptManager
3435 {
3536 readonly Simulator Simulator ;
3637 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 [ ]
4139 {
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 ) ;
5349
5450 [ CallOnThread ( "Loader" ) ]
5551 internal ScriptManager ( Simulator simulator )
@@ -86,22 +82,40 @@ private static Assembly CompileScript(string path)
8682 {
8783 try
8884 {
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 )
9196 {
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);
93101 if ( script == null )
94102 Trace . TraceWarning ( $ "Script file { path } could not be loaded into the process.") ;
95103 return script ;
96104 }
97105 else
98106 {
107+ var errors = result . Diagnostics . Where ( diagnostic => diagnostic . IsWarningAsError || diagnostic . Severity == DiagnosticSeverity . Error ) ;
108+
99109 var errorString = new StringBuilder ( ) ;
100110 errorString . AppendFormat ( "Skipped script {0} with error:" , path ) ;
101111 errorString . Append ( Environment . NewLine ) ;
102- foreach ( CompilerError error in compilerResults . Errors )
112+ foreach ( var error in errors )
103113 {
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 ) ;
105119 errorString . Append ( Environment . NewLine ) ;
106120 }
107121
@@ -126,6 +140,8 @@ private static Assembly CompileScript(string path)
126140
127141 public Assembly LoadFolder ( string path )
128142 {
143+ return null ;
144+ /*
129145 if (Thread.CurrentThread.Name != "Loader Process")
130146 Trace.TraceError("ScriptManager.Load incorrectly called by {0}; must be Loader Process or crashes will occur.", Thread.CurrentThread.Name);
131147
@@ -152,7 +168,7 @@ public Assembly LoadFolder(string path)
152168 errorString.Append(Environment.NewLine);
153169 foreach (CompilerError error in compilerResults.Errors)
154170 {
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);
156172 errorString.Append(Environment.NewLine);
157173 }
158174
@@ -170,6 +186,7 @@ public Assembly LoadFolder(string path)
170186 Trace.WriteLine(new FileLoadException(path, error));
171187 return null;
172188 }
189+ */
173190 }
174191
175192 /*
0 commit comments