Add CommandBuilder existing commands function

This commit is contained in:
NGnius (Graham) 2020-05-12 17:00:41 -04:00
parent e3b3fd5ef4
commit 211c9c9c31
5 changed files with 116 additions and 0 deletions

View file

@ -19,6 +19,8 @@ namespace GamecraftModdingAPI.Commands
private ICustomCommandEngine commandEngine;
private bool fromExisting = false;
/// <summary>
/// Create a new command builder.
/// </summary>
@ -138,6 +140,96 @@ namespace GamecraftModdingAPI.Commands
return this;
}
/// <summary>
/// Build the command from an existing command.
/// </summary>
/// <returns>The command. Use Invoke() to execute it.</returns>
public SimpleCustomCommandEngine FromExisting()
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException("Command name must be defined before FromExisting() is called");
}
if (!ExistingCommands.Exists(name))
{
throw new InvalidOperationException("Command cannot be built from existing because it does not exist.");
}
fromExisting = true;
return new SimpleCustomCommandEngine(
() => { ExistingCommands.Call(name); },
name,
description);
}
/// <summary>
/// Build the command from an existing command.
/// </summary>
/// <returns>The command. Use Invoke() to execute it.</returns>
/// <typeparam name="A">The 1st parameter's type.</typeparam>
public SimpleCustomCommandEngine<A> FromExisting<A>()
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException("Command name must be defined before FromExisting() is called");
}
if (!ExistingCommands.Exists(name))
{
throw new InvalidOperationException("Command cannot be built from existing because it does not exist.");
}
fromExisting = true;
return new SimpleCustomCommandEngine<A>(
(A a) => { ExistingCommands.Call<A>(name, a); },
name,
description);
}
/// <summary>
/// Build the command from an existing command.
/// </summary>
/// <returns>The command. Use Invoke() to execute it.</returns>
/// <typeparam name="A">The 1st parameter's type.</typeparam>
/// <typeparam name="B">The 2nd parameter's type.</typeparam>
public SimpleCustomCommandEngine<A,B> FromExisting<A,B>()
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException("Command name must be defined before FromExisting() is called");
}
if (!ExistingCommands.Exists(name))
{
throw new InvalidOperationException("Command cannot be built from existing because it does not exist.");
}
fromExisting = true;
return new SimpleCustomCommandEngine<A,B>(
(A a, B b) => { ExistingCommands.Call<A,B>(name, a, b); },
name,
description);
}
/// <summary>
/// Build the command from an existing command.
/// </summary>
/// <returns>The command. Use Invoke() to execute it.</returns>
/// <typeparam name="A">The 1st parameter's type.</typeparam>
/// <typeparam name="B">The 2nd parameter's type.</typeparam>
/// <typeparam name="C">The 3rd parameter's type.</typeparam>
public SimpleCustomCommandEngine<A,B,C> FromExisting<A,B,C>()
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException("Command name must be defined before FromExisting() is called");
}
if (!ExistingCommands.Exists(name))
{
throw new InvalidOperationException("Command cannot be built from existing because it does not exist.");
}
fromExisting = true;
return new SimpleCustomCommandEngine<A,B,C>(
(A a, B b, C c) => { ExistingCommands.Call<A,B,C>(name, a, b, c); },
name,
description);
}
/// <summary>
/// Build the command.
/// </summary>
@ -149,6 +241,10 @@ namespace GamecraftModdingAPI.Commands
{
throw new InvalidOperationException("Command name must be defined before Build() is called");
}
if (fromExisting)
{
throw new InvalidOperationException("Command was already built by FromExisting()");
}
if (commandEngine == null)
{
throw new InvalidOperationException("Command action must be defined before Build() is called");

View file

@ -57,5 +57,10 @@ namespace GamecraftModdingAPI.Commands
this.Name = name;
this.Description = description;
}
public void Invoke()
{
runCommand();
}
}
}

View file

@ -47,6 +47,11 @@ namespace GamecraftModdingAPI.Commands
this.runCommand = command;
this.Name = name;
this.Description = description;
}
public void Invoke(A a)
{
runCommand(a);
}
}
}

View file

@ -47,6 +47,11 @@ namespace GamecraftModdingAPI.Commands
this.runCommand = command;
this.Name = name;
this.Description = description;
}
public void Invoke(A a, B b)
{
runCommand(a, b);
}
}
}

View file

@ -47,6 +47,11 @@ namespace GamecraftModdingAPI.Commands
this.runCommand = command;
this.Name = name;
this.Description = description;
}
public void Invoke(A a, B b, C c)
{
runCommand(a, b, c);
}
}
}