Skip to content

Commit

Permalink
Change custom commands GUI and /compile help to use actual list of IC…
Browse files Browse the repository at this point in the history
…ompilers instead of hardcoding for C#/VB
  • Loading branch information
UnknownShadow200 committed Jul 21, 2022
1 parent 71f21ef commit 3cd44c8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 46 deletions.
80 changes: 56 additions & 24 deletions GUI/Popups/CustomCommands.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions GUI/Popups/CustomCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class CustomCommands : Form {

public CustomCommands() {
InitializeComponent();
LoadCompilers();

//Sigh. I wish there were SOME event to help me.
foreach (Command cmd in Command.allCmds) {
Expand All @@ -38,31 +39,47 @@ void CustomCommands_Load(object sender, EventArgs e) {
GuiUtils.SetIcon(this);
}

void CreateCommand(ICompiler engine) {
void LoadCompilers() {
Button[] buttons = { btnCreate1, btnCreate2, btnCreate3, btnCreate4, btnCreate5 };
List<ICompiler> compilers = ICompiler.Compilers;
int i;

for (i = 0; i < Math.Min(compilers.Count, buttons.Length); i++)
{
// must be copied to local variable because of the way C# for loop closures work,
// as otherwise the delegate { ... compilers[i] ... } uses compiler
// from LAST iteration instead of the current iteration
ICompiler compiler = compilers[i];
buttons[i].Visible = true;
buttons[i].Text = "Create " + compiler.ShortName;
buttons[i].Click += delegate { CreateCommand(compiler); };
}

for (; i < buttons.Length; i++) buttons[i].Visible = false;
}

void CreateCommand(ICompiler compiler) {
string cmdName = txtCmdName.Text.Trim();
if (cmdName.Length == 0) {
Popup.Warning("Command must have a name"); return;
}

string path = engine.CommandPath(cmdName);
string path = compiler.CommandPath(cmdName);
if (File.Exists(path)) {
Popup.Warning("Command already exists"); return;
}

try {
string source = engine.GenExampleCommand(cmdName);
string source = compiler.GenExampleCommand(cmdName);
File.WriteAllText(path, source);
} catch (Exception ex) {
Logger.LogError(ex);
Popup.Error("Failed to generate command. Check error logs for more details.");
return;
}
Popup.Message("Command Cmd" + cmdName + engine.FileExtension + " created.");
Popup.Message("Command Cmd" + cmdName + compiler.FileExtension + " created.");
}

void btnCreateCS_Click(object sender, EventArgs e) { CreateCommand(ICompiler.CS); }
void btnCreateVB_Click(object sender, EventArgs e) { CreateCommand(ICompiler.VB); }

void btnLoad_Click(object sender, EventArgs e) {
Assembly lib;
string path;
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Compiling/Backends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace MCGalaxy.Modules.Compiling
public sealed class CSCompiler : ICodeDomCompiler
{
public override string FileExtension { get { return ".cs"; } }
public override string ShortName { get { return "CS"; } }
public override string ShortName { get { return "C#"; } }
public override string FullName { get { return "CSharp"; } }

protected override CodeDomProvider CreateProvider() {
Expand Down
6 changes: 3 additions & 3 deletions MCGalaxy/Modules/Compiling/CmdCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ protected virtual void CompileCommand(Player p, string[] paths, ICompiler compil
CompilerOperations.Compile(p, compiler, "Command", paths, dstPath);
}

// TODO avoid duplication and use compiler.CommandPath instead
public override void Help(Player p) {
ICompiler compiler = ICompiler.Compilers[0];
p.Message("&T/Compile [command name]");
p.Message("&HCompiles a .cs file containing a C# command into a DLL");
p.Message("&H Compiles from &f" + ICompiler.SOURCE_DIR_COMMANDS + "Cmd&H<name>&f.cs");
p.Message("&H Compiles from &f{0}", compiler.CommandPath("&H<name>&f"));
p.Message("&T/Compile plugin [plugin name]");
p.Message("&HCompiles a .cs file containing a C# plugin into a DLL");
p.Message("&H Compiles from &f" + ICompiler.SOURCE_DIR_PLUGINS + "&H<name>&f.cs");
p.Message("&H Compiles from &f{0}", compiler.PluginPath("&H<name>&f"));
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions MCGalaxy/Modules/Compiling/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace MCGalaxy.Modules.Compiling
/// <summary> Compiles source code files for a particular programming language into a .dll </summary>
public abstract class ICompiler
{
public const string SOURCE_DIR_COMMANDS = "extra/commands/source/";
public const string SOURCE_DIR_PLUGINS = "plugins/";
public const string COMMANDS_SOURCE_DIR = "extra/commands/source/";
public const string PLUGINS_SOURCE_DIR = "plugins/";
public const string ERROR_LOG_PATH = "logs/errors/compiler.log";

/// <summary> Default file extension used for source code files </summary>
Expand All @@ -47,15 +47,12 @@ public abstract class ICompiler
/// <summary> Returns source code for an example Plugin </summary>
public abstract string PluginSkeleton { get; }

public string CommandPath(string name) { return SOURCE_DIR_COMMANDS + "Cmd" + name + FileExtension; }
public string PluginPath(string name) { return SOURCE_DIR_PLUGINS + name + FileExtension; }
public string CommandPath(string name) { return COMMANDS_SOURCE_DIR + "Cmd" + name + FileExtension; }
public string PluginPath(string name) { return PLUGINS_SOURCE_DIR + name + FileExtension; }

/// <summary> C# compiler instance. </summary>
public static ICompiler CS = new CSCompiler();
/// <summary> Visual Basic compiler instance. </summary>
public static ICompiler VB = new VBCompiler();

public static List<ICompiler> Compilers = new List<ICompiler>() { CS, VB };
public static List<ICompiler> Compilers = new List<ICompiler>() {
new CSCompiler(), new VBCompiler()
};


static string FormatSource(string source, params string[] args) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static void EnsureFilesExist() {
EnsureDirectoryExists(Paths.ImportsDir);
EnsureDirectoryExists("blockdefs");
EnsureDirectoryExists(IScripting.COMMANDS_DLL_DIR);
EnsureDirectoryExists(MCGalaxy.Modules.Compiling.ICompiler.SOURCE_DIR_COMMANDS); // TODO move to compiling module
EnsureDirectoryExists(MCGalaxy.Modules.Compiling.ICompiler.COMMANDS_SOURCE_DIR); // TODO move to compiling module
EnsureDirectoryExists("text/discord"); // TODO move to discord plugin
}

Expand Down

0 comments on commit 3cd44c8

Please sign in to comment.