@@ -41,6 +41,7 @@ public class ScriptManager
41
41
typeof ( System . Diagnostics . Debug ) . GetTypeInfo ( ) . Assembly . Location ,
42
42
typeof ( ORTS . Common . ElapsedTime ) . GetTypeInfo ( ) . Assembly . Location ,
43
43
typeof ( ORTS . Scripting . Api . Timer ) . GetTypeInfo ( ) . Assembly . Location ,
44
+ typeof ( System . Linq . Enumerable ) . GetTypeInfo ( ) . Assembly . Location ,
44
45
} ;
45
46
static MetadataReference [ ] References = ReferenceAssemblies . Select ( r => MetadataReference . CreateFromFile ( r ) ) . ToArray ( ) ;
46
47
static CSharpCompilationOptions CompilationOptions = new CSharpCompilationOptions (
@@ -61,7 +62,7 @@ public object Load(string[] pathArray, string name, string nameSpace = "ORTS.Scr
61
62
if ( pathArray == null || pathArray . Length == 0 || name == null || name == "" )
62
63
return null ;
63
64
64
- if ( Path . GetExtension ( name ) != ".cs" )
65
+ if ( Path . GetExtension ( name ) . ToLower ( ) != ".cs" )
65
66
name += ".cs" ;
66
67
67
68
var path = ORTSPaths . GetFileFromFolders ( pathArray , name ) ;
@@ -74,20 +75,19 @@ public object Load(string[] pathArray, string name, string nameSpace = "ORTS.Scr
74
75
var type = String . Format ( "{0}.{1}" , nameSpace , Path . GetFileNameWithoutExtension ( path ) . Replace ( '-' , '_' ) ) ;
75
76
76
77
if ( ! Scripts . ContainsKey ( path ) )
77
- Scripts [ path ] = CompileScript ( path ) ;
78
+ Scripts [ path ] = CompileScript ( new string [ ] { path } ) ;
78
79
return Scripts [ path ] ? . CreateInstance ( type , true ) ;
79
80
}
80
81
81
- private static Assembly CompileScript ( string path )
82
+ private static Assembly CompileScript ( string [ ] path )
82
83
{
83
84
try
84
85
{
85
- var scriptName = Path . GetFileName ( path ) ;
86
- var scriptCode = File . ReadAllText ( path ) ;
87
- var syntaxTree = CSharpSyntaxTree . ParseText ( scriptCode ) ;
86
+ var scriptName = Path . GetFileName ( path [ 0 ] ) ;
87
+ var syntaxTrees = path . Select ( file => CSharpSyntaxTree . ParseText ( File . ReadAllText ( file ) ) ) ;
88
88
var compilation = CSharpCompilation . Create (
89
89
scriptName ,
90
- new [ ] { syntaxTree } ,
90
+ syntaxTrees ,
91
91
References ,
92
92
CompilationOptions ) ;
93
93
var ms = new MemoryStream ( ) ;
@@ -125,23 +125,25 @@ private static Assembly CompileScript(string path)
125
125
}
126
126
catch ( InvalidDataException error )
127
127
{
128
- Trace . TraceWarning ( "Skipped script {0} with error: {1}" , path , error . Message ) ;
128
+ if ( path . Length > 1 )
129
+ Trace . TraceWarning ( "Skipped script folder {0} with error: {1}" , Path . GetDirectoryName ( path [ 0 ] ) , error . Message ) ;
130
+ else
131
+ Trace . TraceWarning ( "Skipped script {0} with error: {1}" , path [ 0 ] , error . Message ) ;
129
132
return null ;
130
133
}
131
134
catch ( Exception error )
132
135
{
133
- if ( File . Exists ( path ) )
134
- Trace . WriteLine ( new FileLoadException ( path , error ) ) ;
136
+ if ( File . Exists ( path [ 0 ] ) )
137
+ Trace . WriteLine ( new FileLoadException ( path [ 0 ] , error ) ) ;
135
138
else
136
- Trace . TraceWarning ( "Ignored missing script file {0}" , path ) ;
139
+ Trace . TraceWarning ( "Ignored missing script file {0}" , path [ 0 ] ) ;
137
140
return null ;
138
141
}
139
142
}
140
143
141
144
public Assembly LoadFolder ( string path )
142
145
{
143
- return null ;
144
- /*
146
+
145
147
if ( Thread . CurrentThread . Name != "Loader Process" )
146
148
Trace . TraceError ( "ScriptManager.Load incorrectly called by {0}; must be Loader Process or crashes will occur." , Thread . CurrentThread . Name ) ;
147
149
@@ -154,51 +156,16 @@ public Assembly LoadFolder(string path)
154
156
155
157
if ( files == null || files . Length == 0 ) return null ;
156
158
157
- try
159
+ if ( ! Scripts . ContainsKey ( path ) )
158
160
{
159
- var compilerResults = Compiler.CompileAssemblyFromFile(GetCompilerParameters(), files);
160
- if (!compilerResults.Errors.HasErrors)
161
- {
162
- return compilerResults.CompiledAssembly;
163
- }
164
- else
165
- {
166
- var errorString = new StringBuilder();
167
- errorString.AppendFormat("Skipped script folder {0} with error:", path);
168
- errorString.Append(Environment.NewLine);
169
- foreach (CompilerError error in compilerResults.Errors)
170
- {
171
- errorString.AppendFormat(" {0}, file: {1}, line: {2}, column: {3}", error.ErrorText, error.FileName, error.Line, error.Column);
172
- errorString.Append(Environment.NewLine);
173
- }
174
-
175
- Trace.TraceWarning(errorString.ToString());
161
+ var assembly = CompileScript ( files ) ;
162
+ if ( assembly == null )
176
163
return null ;
177
- }
178
- }
179
- catch (InvalidDataException error)
180
- {
181
- Trace.TraceWarning("Skipped script folder {0} with error: {1}", path, error.Message);
182
- return null;
183
- }
184
- catch (Exception error)
185
- {
186
- Trace.WriteLine(new FileLoadException(path, error));
187
- return null;
188
- }
189
- */
190
- }
191
164
192
- /*
193
- static ClassType CreateInstance<ClassType>(Assembly assembly) where ClassType : class
194
- {
195
- foreach (var type in assembly.GetTypes())
196
- if (typeof(ClassType).IsAssignableFrom(type))
197
- return Activator.CreateInstance(type) as ClassType;
198
-
199
- return default(ClassType);
165
+ Scripts [ path ] = assembly ;
166
+ }
167
+ return Scripts [ path ] ;
200
168
}
201
- */
202
169
203
170
[ CallOnThread ( "Updater" ) ]
204
171
public string GetStatus ( )
0 commit comments