From c7dff51acd0c2f83778062ebc7c921685e7d375d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 12 Apr 2020 11:40:11 +1000 Subject: [PATCH] Support 'reference' when compiling custom commands from GUI, improve logging of errors compiling custom commands from GUI --- GUI/Popups/CustomCommands.cs | 19 ++++++----------- GUI/PropertyWindow/PropertyWindow.cs | 32 ++++++++++++++-------------- MCGalaxy/Scripting/Scripting.cs | 15 ++++++++----- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/GUI/Popups/CustomCommands.cs b/GUI/Popups/CustomCommands.cs index 68b11dce6..9c4cdd23f 100644 --- a/GUI/Popups/CustomCommands.cs +++ b/GUI/Popups/CustomCommands.cs @@ -13,12 +13,11 @@ permissions and limitations under the Licenses. */ using System; -using System.Collections.Generic; using System.CodeDom.Compiler; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Windows.Forms; -using MCGalaxy.Commands; using MCGalaxy.Scripting; namespace MCGalaxy.Gui.Popups { @@ -66,25 +65,21 @@ public partial class CustomCommands : Form { } if (fileName.CaselessEnds(".dll")) { - byte[] data = File.ReadAllBytes(fileName); + byte[] data = File.ReadAllBytes(fileName); Assembly lib = Assembly.Load(data); commands = IScripting.LoadTypes(lib); } else { IScripting engine = fileName.CaselessEnds(".cs") ? IScripting.CS : IScripting.VB; if (!File.Exists(fileName)) return; + ConsoleHelpPlayer p = new ConsoleHelpPlayer(); CompilerParameters args = new CompilerParameters(); - args.GenerateInMemory = true; - CompilerResults result = engine.CompileSource(File.ReadAllText(fileName), args); + args.GenerateInMemory = true; + CompilerResults result = engine.Compile(fileName, args, p); if (result.Errors.HasErrors) { - foreach (CompilerError err in result.Errors) { - Logger.Log(LogType.Warning, "Error #" + err.ErrorNumber); - Logger.Log(LogType.Warning, "Message: " + err.ErrorText); - Logger.Log(LogType.Warning, "Line: " + err.Line); - Logger.Log(LogType.Warning, "================================="); - } - Popup.Error("Error compiling from source. Check logs for more details."); + string body = "\r\n\r\n" + Colors.Strip(p.Messages); + Popup.Error("Compilation error. See logs/errors/compiler.log for more details." + body); return; } commands = IScripting.LoadTypes(result.CompiledAssembly); diff --git a/GUI/PropertyWindow/PropertyWindow.cs b/GUI/PropertyWindow/PropertyWindow.cs index 84010e16e..ba345e59f 100644 --- a/GUI/PropertyWindow/PropertyWindow.cs +++ b/GUI/PropertyWindow/PropertyWindow.cs @@ -113,22 +113,22 @@ public partial class PropertyWindow : Form { void btnDiscard_Click(object sender, EventArgs e) { Dispose(); } void GetHelp(string toHelp) { - ConsoleHelpPlayer player = new ConsoleHelpPlayer(); - Command.Find("Help").Use(player, toHelp); - Popup.Message(Colors.Strip(player.HelpOutput), "Help for /" + toHelp); - } - - sealed class ConsoleHelpPlayer : Player { - public string HelpOutput = ""; - - public ConsoleHelpPlayer() : base("(console)") { - group = Group.NobodyRank; - SuperName = "Console"; - } - - public override void Message(byte id, string message) { - HelpOutput += message + "\r\n"; - } + ConsoleHelpPlayer p = new ConsoleHelpPlayer(); + Command.Find("Help").Use(p, toHelp); + Popup.Message(Colors.Strip(p.Messages), "Help for /" + toHelp); } } + + sealed class ConsoleHelpPlayer : Player { + public string Messages = ""; + + public ConsoleHelpPlayer() : base("(console)") { + group = Group.NobodyRank; + SuperName = "Console"; + } + + public override void Message(byte id, string message) { + Messages += message + "\r\n"; + } + } } diff --git a/MCGalaxy/Scripting/Scripting.cs b/MCGalaxy/Scripting/Scripting.cs index 35c1b8e73..66c7551f5 100644 --- a/MCGalaxy/Scripting/Scripting.cs +++ b/MCGalaxy/Scripting/Scripting.cs @@ -75,18 +75,23 @@ public abstract class IScripting { } } - const int maxLog = 2; /// Attempts to compile source code from the given file. /// Logs errors to player (summarised) and to IScripting.ErrorPath. public bool Compile(string srcPath, string dstPath, Player p) { CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; - args.OutputAssembly = dstPath; - + args.OutputAssembly = dstPath; + return !Compile(srcPath, args, p).Errors.HasErrors; + } + + const int maxLog = 2; + /// Attempts to compile source code from the given file. + /// Logs errors to player (summarised) and to IScripting.ErrorPath. + public CompilerResults Compile(string srcPath, CompilerParameters args, Player p) { int offset = 0; List source = ReadSource(srcPath, args, ref offset); CompilerResults results = CompileSource(source.Join(Environment.NewLine), args); - if (!results.Errors.HasErrors) return true; + if (!results.Errors.HasErrors) return results; StringBuilder sb = new StringBuilder(); sb.AppendLine("############################################################"); @@ -120,7 +125,7 @@ public abstract class IScripting { using (StreamWriter w = new StreamWriter(ErrorPath, true)) { w.Write(sb.ToString()); } - return false; + return results; } static List ReadSource(string path, CompilerParameters args, ref int offset) {