Browse Source

Add CommandBuilder existing commands function

tags/v1.0.0
NGnius (Graham) 4 years ago
parent
commit
211c9c9c31
5 changed files with 116 additions and 0 deletions
  1. +96
    -0
      GamecraftModdingAPI/Commands/CommandBuilder.cs
  2. +5
    -0
      GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs
  3. +5
    -0
      GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs
  4. +5
    -0
      GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs
  5. +5
    -0
      GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs

+ 96
- 0
GamecraftModdingAPI/Commands/CommandBuilder.cs 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");


+ 5
- 0
GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs View File

@@ -57,5 +57,10 @@ namespace GamecraftModdingAPI.Commands
this.Name = name;
this.Description = description;
}

public void Invoke()
{
runCommand();
}
}
}

+ 5
- 0
GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs 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);
}
}
}

+ 5
- 0
GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs 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);
}
}
}

+ 5
- 0
GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs 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);
}
}
}

Loading…
Cancel
Save