Skip to content

Commit

Permalink
Support 'reference' when compiling custom commands from GUI, improve …
Browse files Browse the repository at this point in the history
…logging of errors compiling custom commands from GUI
  • Loading branch information
UnknownShadow200 committed Apr 12, 2020
1 parent e1e3e7f commit c7dff51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
19 changes: 7 additions & 12 deletions GUI/Popups/CustomCommands.cs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Command>(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<Command>(result.CompiledAssembly);
Expand Down
32 changes: 16 additions & 16 deletions GUI/PropertyWindow/PropertyWindow.cs
Expand Up @@ -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";
}
}
}
15 changes: 10 additions & 5 deletions MCGalaxy/Scripting/Scripting.cs
Expand Up @@ -75,18 +75,23 @@ public abstract class IScripting {
}
}

const int maxLog = 2;
/// <summary> Attempts to compile source code from the given file. </summary>
/// <remarks> Logs errors to player (summarised) and to IScripting.ErrorPath. </remarks>
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;
/// <summary> Attempts to compile source code from the given file. </summary>
/// <remarks> Logs errors to player (summarised) and to IScripting.ErrorPath. </remarks>
public CompilerResults Compile(string srcPath, CompilerParameters args, Player p) {
int offset = 0;
List<string> 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("############################################################");
Expand Down Expand Up @@ -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<string> ReadSource(string path, CompilerParameters args, ref int offset) {
Expand Down

0 comments on commit c7dff51

Please sign in to comment.