Warnings shouldn't prevent custom command from compiling

This commit is contained in:
UnknownShadow200 2019-06-03 23:38:57 +10:00
parent 69fffc9cce
commit a96d45a30a
4 changed files with 19 additions and 42 deletions

View File

@ -75,10 +75,7 @@ namespace MCGalaxy.Gui.Popups {
CompilerParameters args = new CompilerParameters(); CompilerParameters args = new CompilerParameters();
args.GenerateInMemory = true; args.GenerateInMemory = true;
var result = engine.CompileSource(File.ReadAllText(fileName), args); CompilerResults result = engine.CompileSource(fileName, args);
if (result == null) {
Popup.Error("Error compiling files. Check logs for more details."); return;
}
if (result.Errors.HasErrors) { if (result.Errors.HasErrors) {
foreach (CompilerError err in result.Errors) { foreach (CompilerError err in result.Errors) {

View File

@ -39,13 +39,12 @@ namespace MCGalaxy.Commands.Scripting {
Help(p); return; Help(p); return;
} }
string path = engine.SourcePath(args[0]); string srcPath = engine.SourcePath(args[0]);
if (!File.Exists(path)) {
p.Message("File &9{0} %Snot found.", path); return;
}
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."); p.Message("Command compiled successfully.");
} else { } else {
p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information."); p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information.");

View File

@ -59,15 +59,13 @@ namespace MCGalaxy.Commands.Scripting {
string srcPath = "plugins/" + name + engine.Ext; string srcPath = "plugins/" + name + engine.Ext;
string dstPath = IScripting.PluginPath(name); string dstPath = IScripting.PluginPath(name);
if (File.Exists(srcPath)) { if (!File.Exists(srcPath)) {
if (engine.Compile(srcPath, dstPath)) { p.Message("File &9{0} %Snot found.", srcPath);
} else if (engine.Compile(srcPath, dstPath)) {
p.Message("Plugin compiled successfully."); p.Message("Plugin compiled successfully.");
} else { } else {
p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information."); p.Message("%WCompilation error. See " + IScripting.ErrorPath + " for more information.");
} }
} else {
p.Message("File &9{0} %Snot found.", srcPath);
}
} }
static void LoadPlugin(Player p, string name) { static void LoadPlugin(Player p, string name) {

View File

@ -72,29 +72,19 @@ namespace MCGalaxy.Scripting {
} }
public bool Compile(string srcPath, string dstPath) { 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(); CompilerParameters args = new CompilerParameters();
args.GenerateExecutable = false; args.GenerateExecutable = false;
args.OutputAssembly = dstPath; args.OutputAssembly = dstPath;
List<string> source = ReadSourceCode(srcPath, args); List<string> source = ReadSource(srcPath, args);
CompilerResults results = CompileSource(source.Join(Environment.NewLine), 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(); StringBuilder sb = new StringBuilder();
AppendHeader(sb, srcPath); sb.AppendLine("############################################################");
sb.AppendLine("Errors when compiling " + srcPath);
sb.AppendLine("############################################################");
sb.AppendLine();
foreach (CompilerError err in results.Errors) { foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error"; string type = err.IsWarning ? "Warning" : "Error";
@ -115,7 +105,7 @@ namespace MCGalaxy.Scripting {
return !results.Errors.HasErrors; return !results.Errors.HasErrors;
} }
List<string> ReadSourceCode(string path, CompilerParameters args) { List<string> ReadSource(string path, CompilerParameters args) {
List<string> lines = Utils.ReadAllLinesList(path); List<string> lines = Utils.ReadAllLinesList(path);
// Allow referencing other assemblies using 'Reference [assembly name]' at top of the file // Allow referencing other assemblies using 'Reference [assembly name]' at top of the file
for (int i = 0; i < lines.Count; i++) { for (int i = 0; i < lines.Count; i++) {
@ -129,13 +119,6 @@ namespace MCGalaxy.Scripting {
return lines; 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) { public CompilerResults CompileSource(string source, CompilerParameters args) {
args.ReferencedAssemblies.Add("MCGalaxy_.dll"); args.ReferencedAssemblies.Add("MCGalaxy_.dll");
source = source.Replace("MCLawl", "MCGalaxy"); source = source.Replace("MCLawl", "MCGalaxy");