TechbloxModdingAPI/GamecraftModdingAPI/Commands/CommandBuilder.cs

182 lines
5.5 KiB
C#
Raw Normal View History

2020-05-01 18:50:55 +00:00
using System;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Commands
{
/// <summary>
/// Custom Command builder.
/// </summary>
public class CommandBuilder
{
private string name;
private string description;
private short parameterCount;
private ICustomCommandEngine commandEngine;
/// <summary>
/// Create a new command builder.
/// </summary>
public CommandBuilder()
{
name = "";
description = null;
parameterCount = -1;
}
/// <summary>
/// Create and return a command builder.
/// </summary>
/// <returns>The builder.</returns>
public static CommandBuilder Builder()
{
return new CommandBuilder();
}
/// <summary>
/// Create a new command builder.
/// </summary>
/// <param name="name">The command name.</param>
/// <param name="description">The command description (shown in help).</param>
public CommandBuilder(string name, string description = null)
{
this.name = name;
this.description = description;
parameterCount = -1;
}
/// <summary>
/// Create and return a command builder.
/// If name and description are provided, this is equivalent to <code>Builder().Name(name).Description(description)</code>
/// </summary>
/// <param name="name">The command name.</param>
/// <param name="description">The command description (shown in help).</param>
/// <returns>The builder.</returns>
public static CommandBuilder Builder(string name, string description = null)
{
return new CommandBuilder(name, description);
}
/// <summary>
/// Name the command.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="name">The command name.</param>
public CommandBuilder Name(string name)
{
this.name = name;
return this;
}
/// <summary>
/// Describe the command.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="description">The command description (shown in help).</param>
public CommandBuilder Description(string description)
{
this.description = description;
return this;
}
/// <summary>
/// Set the action the command performs.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="action">The action to perform when the command is called.</param>
public CommandBuilder Action(Action action)
{
parameterCount = 0;
commandEngine = new SimpleCustomCommandEngine(action, name, description);
return this;
}
/// <summary>
/// Set the action the command performs.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="action">The action to perform when the command is called.</param>
/// <typeparam name="A">The 1st parameter's type.</typeparam>
public CommandBuilder Action<A>(Action<A> action)
{
parameterCount = 1;
commandEngine = new SimpleCustomCommandEngine<A>(action, name, description);
return this;
}
/// <summary>
/// Set the action the command performs.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="action">The action to perform when the command is called.</param>
/// <typeparam name="A">The 1st parameter's type.</typeparam>
/// <typeparam name="B">The 2nd parameter's type.</typeparam>
public CommandBuilder Action<A,B>(Action<A,B> action)
{
parameterCount = 2;
commandEngine = new SimpleCustomCommandEngine<A,B>(action, name, description);
return this;
}
/// <summary>
/// Set the action the command performs.
/// </summary>
/// <returns>The builder.</returns>
/// <param name="action">The action to perform when the command is called.</param>
/// <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 CommandBuilder Action<A,B,C>(Action<A,B,C> action)
{
parameterCount = 3;
commandEngine = new SimpleCustomCommandEngine<A,B,C>(action, name, description);
return this;
}
/// <summary>
/// Build the command.
/// </summary>
/// <returns>The built command.</returns>
/// <param name="register">Automatically register the command with CommandManager.AddCommand()?</param>
public ICustomCommandEngine Build(bool register = true)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException("Command name must be defined before Build() is called");
}
if (commandEngine == null)
{
throw new InvalidOperationException("Command action must be defined before Build() is called");
}
if (string.IsNullOrWhiteSpace(description))
{
Logging.LogWarning($"Command {name} was built without a description");
}
if (register)
{
CommandManager.AddCommand(commandEngine);
Logging.MetaDebugLog($"Command {FullName()} was automatically registered");
}
return commandEngine;
}
/// <summary>
/// Get the full command name, in the form [name]::[description]::[# of parameters]
/// </summary>
/// <returns>The name.</returns>
public string FullName()
{
if (string.IsNullOrWhiteSpace(description))
{
return name + "::" + parameterCount;
}
return name + "::" + description + "::" + parameterCount;
}
}
}