Skip to content

Commit 78f9c35

Browse files
committed
Fix scripting system to provide more information in errors
Also make log lines for scripting more uniform
1 parent 0e7423d commit 78f9c35

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

MinecraftClient/ChatBots/Script.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public override void Update()
158158
{
159159
try
160160
{
161-
CSharpRunner.Run(this, lines, args, localVars);
161+
CSharpRunner.Run(this, lines, args, localVars, scriptName: file!);
162162
}
163163
catch (CSharpException e)
164164
{

MinecraftClient/Scripting/CSharpRunner.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSharpRunner
2626
/// <param name="run">Set to false to compile and cache the script without launching it</param>
2727
/// <exception cref="CSharpException">Thrown if an error occured</exception>
2828
/// <returns>Result of the execution, returned by the script</returns>
29-
public static object? Run(ChatBot apiHandler, string[] lines, string[] args, Dictionary<string, object>? localVars, bool run = true)
29+
public static object? Run(ChatBot apiHandler, string[] lines, string[] args, Dictionary<string, object>? localVars, bool run = true, string scriptName = "Unknown Script")
3030
{
3131
//Script compatibility check for handling future versions differently
3232
if (lines.Length < 1 || lines[0] != "//MCCScript 1.0")
@@ -102,13 +102,24 @@ class CSharpRunner
102102
"}}",
103103
});
104104

105+
ConsoleIO.WriteLogLine($"[Script] Starting compilation for {scriptName}...");
106+
105107
//Compile the C# class in memory using all the currently loaded assemblies
106108
var result = compiler.Compile(code, Guid.NewGuid().ToString());
107109

108110
//Process compile warnings and errors
109-
if (result.Failures != null)
110-
throw new CSharpException(CSErrorType.LoadError,
111-
new InvalidOperationException(result.Failures[0].GetMessage()));
111+
if (result.Failures != null) {
112+
113+
ConsoleIO.WriteLogLine("[Script] Compilation failed with error(s):");
114+
115+
foreach (var failure in result.Failures) {
116+
ConsoleIO.WriteLogLine($"[Script] Error in {scriptName}, line:col{failure.Location.GetMappedLineSpan()}: [{failure.Id}] {failure.GetMessage()}");
117+
}
118+
119+
throw new CSharpException(CSErrorType.InvalidScript, new InvalidProgramException("Compilation failed due to error."));
120+
}
121+
122+
ConsoleIO.WriteLogLine("[Script] Compilation done with no errors.");
112123

113124
//Retrieve compiled assembly
114125
assembly = result.Assembly;
@@ -385,7 +396,7 @@ public bool SetServer(string server, bool andReconnect = false)
385396
{
386397
throw new CSharpException(CSErrorType.FileReadError, e);
387398
}
388-
return CSharpRunner.Run(this, lines, args, localVars);
399+
return CSharpRunner.Run(this, lines, args, localVars, scriptName: script);
389400
}
390401
}
391402
}

MinecraftClient/Scripting/DynamicRun/Builder/CompileRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class CompileRunner
2424
GC.WaitForPendingFinalizers();
2525
}
2626

27-
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "Script continues to run." : "Script finished!");
27+
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "[Script] Script continues to run." : "[Script] Script finished!");
2828
return assemblyLoadContextWeakRef.Item2;
2929
}
3030

MinecraftClient/Scripting/DynamicRun/Builder/Compiler.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ internal class Compiler
2323
{
2424
public CompileResult Compile(string filepath, string fileName)
2525
{
26-
ConsoleIO.WriteLogLine($"Starting compilation...");
27-
2826
using var peStream = new MemoryStream();
2927
var result = GenerateCode(filepath, fileName).Emit(peStream);
3028

3129
if (!result.Success)
3230
{
33-
ConsoleIO.WriteLogLine("Compilation done with error.");
34-
3531
var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
3632

3733
return new CompileResult()
@@ -41,9 +37,7 @@ public CompileResult Compile(string filepath, string fileName)
4137
Failures = failures.ToList()
4238
};
4339
}
44-
45-
ConsoleIO.WriteLogLine("Compilation done without any error.");
46-
40+
4741
peStream.Seek(0, SeekOrigin.Begin);
4842

4943
return new CompileResult()

0 commit comments

Comments
 (0)