Skip to content

Commit

Permalink
Cleanup CustomCommands GUI and use IScripting.LoadAssembly instead
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Mar 23, 2021
1 parent b26e4f9 commit 45fe8e9
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions GUI/Popups/CustomCommands.cs
Expand Up @@ -55,7 +55,6 @@ public partial class CustomCommands : Form {
}

void btnLoad_Click(object sender, EventArgs e) {
List<Command> commands = null;
string fileName;
using (FileDialog dialog = new OpenFileDialog()) {
dialog.RestoreDirectory = true;
Expand All @@ -65,41 +64,25 @@ public partial class CustomCommands : Form {
}

if (fileName.CaselessEnds(".dll")) {
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.Compile(fileName, args, p);

if (result.Errors.HasErrors) {
string body = "\r\n\r\n" + Colors.StripUsed(p.Messages);
Popup.Error("Compilation error. See logs/errors/compiler.log for more details." + body);
return;
}
commands = IScripting.LoadTypes<Command>(result.CompiledAssembly);
}

if (commands == null) {
Popup.Error("Error compiling files. Check logs for more details"); return;
Assembly lib = IScripting.LoadAssembly(fileName);
LoadCommands(lib);
return;
}
for (int i = 0; i < commands.Count; i++) {
Command cmd = commands[i];

if (lstCommands.Items.Contains(cmd.name)) {
Popup.Warning("Command " + cmd.name + " already exists, so was not loaded");
continue;
}

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.Compile(fileName, args, p);

lstCommands.Items.Add(cmd.name);
Command.Register(cmd);
Logger.Log(LogType.SystemActivity, "Added " + cmd.name + " to commands");
if (result.Errors.HasErrors) {
string body = "\r\n\r\n" + Colors.StripUsed(p.Messages);
Popup.Error("Compilation error. See logs/errors/compiler.log for more details." + body);
return;
}
LoadCommands(result.CompiledAssembly);
}

void btnUnload_Click(object sender, EventArgs e) {
Expand All @@ -117,5 +100,26 @@ public partial class CustomCommands : Form {
void lstCommands_SelectedIndexChanged(object sender, EventArgs e) {
btnUnload.Enabled = lstCommands.SelectedIndex != -1;
}


void LoadCommands(Assembly assembly) {
List<Command> commands = IScripting.LoadTypes<Command>(assembly);
if (commands == null) {
Popup.Error("Error compiling files. Check logs for more details"); return;
}

for (int i = 0; i < commands.Count; i++) {
Command cmd = commands[i];

if (lstCommands.Items.Contains(cmd.name)) {
Popup.Warning("Command " + cmd.name + " already exists, so was not loaded");
continue;
}

lstCommands.Items.Add(cmd.name);
Command.Register(cmd);
Logger.Log(LogType.SystemActivity, "Added " + cmd.name + " to commands");
}
}
}
}

0 comments on commit 45fe8e9

Please sign in to comment.