Skip to content

Commit a6e8147

Browse files
committed
Compile symbols too for the debug (thanks Cedric!)
1 parent 12e53b1 commit a6e8147

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Threading;
2828
using Microsoft.CodeAnalysis;
2929
using Microsoft.CodeAnalysis.CSharp;
30+
using Microsoft.CodeAnalysis.Emit;
3031

3132
namespace Orts.Common.Scripting
3233
{
@@ -81,20 +82,34 @@ private static Assembly CompileScript(string[] path)
8182
{
8283
var scriptPath = path.Length > 1 ? Path.GetDirectoryName(path[0]) : path[0];
8384
var scriptName = Path.GetFileName(scriptPath);
85+
var symbolsName = Path.ChangeExtension(scriptName, "pdb");
8486
try
8587
{
86-
var syntaxTrees = path.Select(file => CSharpSyntaxTree.ParseText(File.ReadAllText(file), null, file));
88+
var syntaxTrees = path.Select(file => CSharpSyntaxTree.ParseText(File.ReadAllText(file), null, file, Encoding.UTF8));
8789
var compilation = CSharpCompilation.Create(
8890
scriptName,
8991
syntaxTrees,
9092
References,
9193
CompilationOptions);
92-
var ms = new MemoryStream();
93-
var result = compilation.Emit(ms);
94+
95+
var emitOptions = new EmitOptions(
96+
debugInformationFormat: DebugInformationFormat.PortablePdb,
97+
pdbFilePath: symbolsName);
98+
99+
var assemblyStream = new MemoryStream();
100+
var symbolsStream = new MemoryStream();
101+
102+
var result = compilation.Emit(
103+
assemblyStream,
104+
symbolsStream,
105+
options: emitOptions);
106+
94107
if (result.Success)
95108
{
96-
ms.Seek(0, SeekOrigin.Begin);
97-
var script = Assembly.Load(ms.ToArray());
109+
assemblyStream.Seek(0, SeekOrigin.Begin);
110+
symbolsStream.Seek(0, SeekOrigin.Begin);
111+
112+
var script = Assembly.Load(assemblyStream.ToArray(), symbolsStream.ToArray());
98113
// in netcore:
99114
//var script = AssemblyLoadContext.Default.LoadFromStream(ms);
100115
if (script == null)

0 commit comments

Comments
 (0)