Skip to content

Commit f63c5d6

Browse files
committed
Fixed the LoadFolder() method. (Thanks César!)
1 parent ecc1b2f commit f63c5d6

File tree

1 file changed

+21
-54
lines changed

1 file changed

+21
-54
lines changed

Source/Orts.Simulation/Common/Scripting/ScriptManager.cs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ScriptManager
4141
typeof(System.Diagnostics.Debug).GetTypeInfo().Assembly.Location,
4242
typeof(ORTS.Common.ElapsedTime).GetTypeInfo().Assembly.Location,
4343
typeof(ORTS.Scripting.Api.Timer).GetTypeInfo().Assembly.Location,
44+
typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location,
4445
};
4546
static MetadataReference[] References = ReferenceAssemblies.Select(r => MetadataReference.CreateFromFile(r)).ToArray();
4647
static CSharpCompilationOptions CompilationOptions = new CSharpCompilationOptions(
@@ -61,7 +62,7 @@ public object Load(string[] pathArray, string name, string nameSpace = "ORTS.Scr
6162
if (pathArray == null || pathArray.Length == 0 || name == null || name == "")
6263
return null;
6364

64-
if (Path.GetExtension(name) != ".cs")
65+
if (Path.GetExtension(name).ToLower() != ".cs")
6566
name += ".cs";
6667

6768
var path = ORTSPaths.GetFileFromFolders(pathArray, name);
@@ -74,20 +75,19 @@ public object Load(string[] pathArray, string name, string nameSpace = "ORTS.Scr
7475
var type = String.Format("{0}.{1}", nameSpace, Path.GetFileNameWithoutExtension(path).Replace('-', '_'));
7576

7677
if (!Scripts.ContainsKey(path))
77-
Scripts[path] = CompileScript(path);
78+
Scripts[path] = CompileScript(new string[] { path });
7879
return Scripts[path]?.CreateInstance(type, true);
7980
}
8081

81-
private static Assembly CompileScript(string path)
82+
private static Assembly CompileScript(string[] path)
8283
{
8384
try
8485
{
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)));
8888
var compilation = CSharpCompilation.Create(
8989
scriptName,
90-
new[] { syntaxTree },
90+
syntaxTrees,
9191
References,
9292
CompilationOptions);
9393
var ms = new MemoryStream();
@@ -125,23 +125,25 @@ private static Assembly CompileScript(string path)
125125
}
126126
catch (InvalidDataException error)
127127
{
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);
129132
return null;
130133
}
131134
catch (Exception error)
132135
{
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));
135138
else
136-
Trace.TraceWarning("Ignored missing script file {0}", path);
139+
Trace.TraceWarning("Ignored missing script file {0}", path[0]);
137140
return null;
138141
}
139142
}
140143

141144
public Assembly LoadFolder(string path)
142145
{
143-
return null;
144-
/*
146+
145147
if (Thread.CurrentThread.Name != "Loader Process")
146148
Trace.TraceError("ScriptManager.Load incorrectly called by {0}; must be Loader Process or crashes will occur.", Thread.CurrentThread.Name);
147149

@@ -154,51 +156,16 @@ public Assembly LoadFolder(string path)
154156

155157
if (files == null || files.Length == 0) return null;
156158

157-
try
159+
if (!Scripts.ContainsKey(path))
158160
{
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)
176163
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-
}
191164

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];
200168
}
201-
*/
202169

203170
[CallOnThread("Updater")]
204171
public string GetStatus()

0 commit comments

Comments
 (0)