mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +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
|
try
|
||||||
{
|
{
|
||||||
CSharpRunner.Run(this, lines, args, localVars);
|
CSharpRunner.Run(this, lines, args, localVars, scriptName: file!);
|
||||||
}
|
}
|
||||||
catch (CSharpException e)
|
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>
|
/// <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>
|
/// <exception cref="CSharpException">Thrown if an error occured</exception>
|
||||||
/// <returns>Result of the execution, returned by the script</returns>
|
/// <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
|
//Script compatibility check for handling future versions differently
|
||||||
if (lines.Length < 1 || lines[0] != "//MCCScript 1.0")
|
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
|
//Compile the C# class in memory using all the currently loaded assemblies
|
||||||
var result = compiler.Compile(code, Guid.NewGuid().ToString());
|
var result = compiler.Compile(code, Guid.NewGuid().ToString());
|
||||||
|
|
||||||
//Process compile warnings and errors
|
//Process compile warnings and errors
|
||||||
if (result.Failures != null)
|
if (result.Failures != null) {
|
||||||
throw new CSharpException(CSErrorType.LoadError,
|
|
||||||
new InvalidOperationException(result.Failures[0].GetMessage()));
|
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
|
//Retrieve compiled assembly
|
||||||
assembly = result.Assembly;
|
assembly = result.Assembly;
|
||||||
|
|
@ -385,7 +396,7 @@ namespace MinecraftClient
|
||||||
{
|
{
|
||||||
throw new CSharpException(CSErrorType.FileReadError, e);
|
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();
|
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;
|
return assemblyLoadContextWeakRef.Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,11 @@ namespace DynamicRun.Builder
|
||||||
{
|
{
|
||||||
public CompileResult Compile(string filepath, string fileName)
|
public CompileResult Compile(string filepath, string fileName)
|
||||||
{
|
{
|
||||||
ConsoleIO.WriteLogLine($"Starting compilation...");
|
|
||||||
|
|
||||||
using var peStream = new MemoryStream();
|
using var peStream = new MemoryStream();
|
||||||
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
||||||
|
|
||||||
if (!result.Success)
|
if (!result.Success)
|
||||||
{
|
{
|
||||||
ConsoleIO.WriteLogLine("Compilation done with error.");
|
|
||||||
|
|
||||||
var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
|
var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
|
||||||
|
|
||||||
return new CompileResult()
|
return new CompileResult()
|
||||||
|
|
@ -41,9 +37,7 @@ namespace DynamicRun.Builder
|
||||||
Failures = failures.ToList()
|
Failures = failures.ToList()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ConsoleIO.WriteLogLine("Compilation done without any error.");
|
|
||||||
|
|
||||||
peStream.Seek(0, SeekOrigin.Begin);
|
peStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
return new CompileResult()
|
return new CompileResult()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue