mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Fix scripting system to provide more information in errors
Also make log lines for scripting more uniform
This commit is contained in:
parent
0e7423d1d9
commit
78f9c35800
4 changed files with 19 additions and 14 deletions
|
|
@ -158,7 +158,7 @@ namespace MinecraftClient.ChatBots
|
|||
{
|
||||
try
|
||||
{
|
||||
CSharpRunner.Run(this, lines, args, localVars);
|
||||
CSharpRunner.Run(this, lines, args, localVars, scriptName: file!);
|
||||
}
|
||||
catch (CSharpException e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ namespace MinecraftClient
|
|||
/// <param name="run">Set to false to compile and cache the script without launching it</param>
|
||||
/// <exception cref="CSharpException">Thrown if an error occured</exception>
|
||||
/// <returns>Result of the execution, returned by the script</returns>
|
||||
public static object? Run(ChatBot apiHandler, string[] lines, string[] args, Dictionary<string, object>? localVars, bool run = true)
|
||||
public static object? Run(ChatBot apiHandler, string[] lines, string[] args, Dictionary<string, object>? localVars, bool run = true, string scriptName = "Unknown Script")
|
||||
{
|
||||
//Script compatibility check for handling future versions differently
|
||||
if (lines.Length < 1 || lines[0] != "//MCCScript 1.0")
|
||||
|
|
@ -102,13 +102,24 @@ namespace MinecraftClient
|
|||
"}}",
|
||||
});
|
||||
|
||||
ConsoleIO.WriteLogLine($"[Script] Starting compilation for {scriptName}...");
|
||||
|
||||
//Compile the C# class in memory using all the currently loaded assemblies
|
||||
var result = compiler.Compile(code, Guid.NewGuid().ToString());
|
||||
|
||||
//Process compile warnings and errors
|
||||
if (result.Failures != null)
|
||||
throw new CSharpException(CSErrorType.LoadError,
|
||||
new InvalidOperationException(result.Failures[0].GetMessage()));
|
||||
if (result.Failures != null) {
|
||||
|
||||
ConsoleIO.WriteLogLine("[Script] Compilation failed with error(s):");
|
||||
|
||||
foreach (var failure in result.Failures) {
|
||||
ConsoleIO.WriteLogLine($"[Script] Error in {scriptName}, line:col{failure.Location.GetMappedLineSpan()}: [{failure.Id}] {failure.GetMessage()}");
|
||||
}
|
||||
|
||||
throw new CSharpException(CSErrorType.InvalidScript, new InvalidProgramException("Compilation failed due to error."));
|
||||
}
|
||||
|
||||
ConsoleIO.WriteLogLine("[Script] Compilation done with no errors.");
|
||||
|
||||
//Retrieve compiled assembly
|
||||
assembly = result.Assembly;
|
||||
|
|
@ -385,7 +396,7 @@ namespace MinecraftClient
|
|||
{
|
||||
throw new CSharpException(CSErrorType.FileReadError, e);
|
||||
}
|
||||
return CSharpRunner.Run(this, lines, args, localVars);
|
||||
return CSharpRunner.Run(this, lines, args, localVars, scriptName: script);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ namespace DynamicRun.Builder
|
|||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "Script continues to run." : "Script finished!");
|
||||
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "[Script] Script continues to run." : "[Script] Script finished!");
|
||||
return assemblyLoadContextWeakRef.Item2;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,15 +23,11 @@ namespace DynamicRun.Builder
|
|||
{
|
||||
public CompileResult Compile(string filepath, string fileName)
|
||||
{
|
||||
ConsoleIO.WriteLogLine($"Starting compilation...");
|
||||
|
||||
using var peStream = new MemoryStream();
|
||||
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
ConsoleIO.WriteLogLine("Compilation done with error.");
|
||||
|
||||
var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
return new CompileResult()
|
||||
|
|
@ -41,9 +37,7 @@ namespace DynamicRun.Builder
|
|||
Failures = failures.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
ConsoleIO.WriteLogLine("Compilation done without any error.");
|
||||
|
||||
|
||||
peStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return new CompileResult()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue