From a96d45a30aa2bc160df9675ce0ae1241d3f74ed9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 3 Jun 2019 23:38:57 +1000 Subject: [PATCH] Warnings shouldn't prevent custom command from compiling --- GUI/Popups/CustomCommands.cs | 5 +--- MCGalaxy/Commands/Scripting/CmdCompile.cs | 11 ++++---- MCGalaxy/Commands/Scripting/CmdPlugin.cs | 12 ++++----- MCGalaxy/Scripting/Scripting.cs | 33 ++++++----------------- 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/GUI/Popups/CustomCommands.cs b/GUI/Popups/CustomCommands.cs index 80c3e384c..3520262df 100644 --- a/GUI/Popups/CustomCommands.cs +++ b/GUI/Popups/CustomCommands.cs @@ -75,10 +75,7 @@ namespace MCGalaxy.Gui.Popups { CompilerParameters args = new CompilerParameters(); args.GenerateInMemory = true; - var result = engine.CompileSource(File.ReadAllText(fileName), args); - if (result == null) { - Popup.Error("Error compiling files. Check logs for more details."); return; - } + CompilerResults result = engine.CompileSource(fileName, args); if (result.Errors.HasErrors) { foreach (CompilerError err in result.Errors) { diff --git a/MCGalaxy/Commands/Scripting/CmdCompile.cs b/MCGalaxy/Commands/Scripting/CmdCompile.cs index 93b2c2611..d232a69f3 100644 --- a/MCGalaxy/Commands/Scripting/CmdCompile.cs +++ b/MCGalaxy/Commands/Scripting/CmdCompile.cs @@ -39,13 +39,12 @@ namespace MCGalaxy.Commands.Scripting { Help(p); return; } - string path = engine.SourcePath(args[0]); - if (!File.Exists(path)) { - p.Message("File &9{0} %Snot found.", path); return; - } + string srcPath = engine.SourcePath(args[0]); + string dstPath = IScripting.DllPath(args[0]); - string dstPath = IScripting.DllPath(args[0]); - if (engine.Compile(path, dstPath)) { + if (!File.Exists(srcPath)) { + p.Message("File &9{0} %Snot found.", srcPath); + } else if (engine.Compile(srcPath, dstPath)) { p.Message("Command compiled successfully."); } else { p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information."); diff --git a/MCGalaxy/Commands/Scripting/CmdPlugin.cs b/MCGalaxy/Commands/Scripting/CmdPlugin.cs index 9371eaa0a..eaf6f1730 100644 --- a/MCGalaxy/Commands/Scripting/CmdPlugin.cs +++ b/MCGalaxy/Commands/Scripting/CmdPlugin.cs @@ -59,14 +59,12 @@ namespace MCGalaxy.Commands.Scripting { string srcPath = "plugins/" + name + engine.Ext; string dstPath = IScripting.PluginPath(name); - if (File.Exists(srcPath)) { - if (engine.Compile(srcPath, dstPath)) { - p.Message("Plugin compiled successfully."); - } else { - p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information."); - } - } else { + if (!File.Exists(srcPath)) { p.Message("File &9{0} %Snot found.", srcPath); + } else if (engine.Compile(srcPath, dstPath)) { + p.Message("Plugin compiled successfully."); + } else { + p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information."); } } diff --git a/MCGalaxy/Scripting/Scripting.cs b/MCGalaxy/Scripting/Scripting.cs index 843f05a34..85c784b9d 100644 --- a/MCGalaxy/Scripting/Scripting.cs +++ b/MCGalaxy/Scripting/Scripting.cs @@ -72,29 +72,19 @@ namespace MCGalaxy.Scripting { } public bool Compile(string srcPath, string dstPath) { - StringBuilder sb = null; - - if (!File.Exists(srcPath)) { - sb = new StringBuilder(); - using (StreamWriter w = new StreamWriter(ErrorPath, true)) { - AppendHeader(sb, srcPath); - sb.AppendLine("File not found: " + srcPath); - sb.AppendLine(); - w.Write(sb.ToString()); - } - return false; - } - CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.OutputAssembly = dstPath; - List source = ReadSourceCode(srcPath, args); + List source = ReadSource(srcPath, args); CompilerResults results = CompileSource(source.Join(Environment.NewLine), args); - if (results.Errors.Count == 0) return true; + if (!results.Errors.HasErrors) return true; - sb = new StringBuilder(); - AppendHeader(sb, srcPath); + StringBuilder sb = new StringBuilder(); + sb.AppendLine("############################################################"); + sb.AppendLine("Errors when compiling " + srcPath); + sb.AppendLine("############################################################"); + sb.AppendLine(); foreach (CompilerError err in results.Errors) { string type = err.IsWarning ? "Warning" : "Error"; @@ -115,7 +105,7 @@ namespace MCGalaxy.Scripting { return !results.Errors.HasErrors; } - List ReadSourceCode(string path, CompilerParameters args) { + List ReadSource(string path, CompilerParameters args) { List lines = Utils.ReadAllLinesList(path); // Allow referencing other assemblies using 'Reference [assembly name]' at top of the file for (int i = 0; i < lines.Count; i++) { @@ -129,13 +119,6 @@ namespace MCGalaxy.Scripting { return lines; } - void AppendHeader(StringBuilder sb, string path) { - sb.AppendLine("############################################################"); - sb.AppendLine("Errors when compiling " + path); - sb.AppendLine("############################################################"); - sb.AppendLine(); - } - public CompilerResults CompileSource(string source, CompilerParameters args) { args.ReferencedAssemblies.Add("MCGalaxy_.dll"); source = source.Replace("MCLawl", "MCGalaxy");