2019-12-16 00:35:59 +00:00
|
|
|
|
using System;
|
2019-12-15 07:20:20 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2019-12-16 00:35:59 +00:00
|
|
|
|
using Svelto.ECS;
|
|
|
|
|
|
2020-05-13 20:52:06 +00:00
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
|
2019-12-15 07:20:20 +00:00
|
|
|
|
namespace GamecraftModdingAPI.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A simple implementation of ICustomCommandEngine sufficient for most commands.
|
|
|
|
|
/// This version is for commands which take 3 argument(s)
|
|
|
|
|
/// </summary>
|
2019-12-19 20:42:50 +00:00
|
|
|
|
public class SimpleCustomCommandEngine<A,B,C> : ICustomCommandEngine
|
2019-12-15 07:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
|
|
|
|
public string Description { get; }
|
|
|
|
|
|
|
|
|
|
private Action<A,B,C> runCommand;
|
|
|
|
|
|
2020-03-12 22:36:23 +00:00
|
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
2019-12-15 07:20:20 +00:00
|
|
|
|
|
2020-05-12 00:28:26 +00:00
|
|
|
|
public bool isRemovable => true;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
2019-12-15 07:20:20 +00:00
|
|
|
|
{
|
2019-12-25 21:16:17 +00:00
|
|
|
|
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
|
2019-12-15 07:20:20 +00:00
|
|
|
|
CommandRegistrationHelper.Unregister(this.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Ready()
|
|
|
|
|
{
|
2019-12-25 21:16:17 +00:00
|
|
|
|
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Registering SimpleCustomCommandEngine {this.Name}");
|
2020-05-13 20:52:06 +00:00
|
|
|
|
CommandRegistrationHelper.Register<A,B,C>(this.Name, this.InvokeCatchError, this.Description);
|
2019-12-15 07:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Construct the engine
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command">The command's operation</param>
|
|
|
|
|
/// <param name="name">The name of the command</param>
|
|
|
|
|
/// <param name="description">The command's description, shown in command help messages</param>
|
|
|
|
|
public SimpleCustomCommandEngine(Action<A,B,C> command, string name, string description)
|
|
|
|
|
{
|
|
|
|
|
this.runCommand = command;
|
|
|
|
|
this.Name = name;
|
|
|
|
|
this.Description = description;
|
2020-05-12 21:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Invoke(A a, B b, C c)
|
|
|
|
|
{
|
|
|
|
|
runCommand(a, b, c);
|
2020-05-13 20:52:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InvokeCatchError(A a, B b, C c)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
runCommand(a, b, c);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
CommandRuntimeException wrappedException = new CommandRuntimeException($"Command {Name} threw an exception when executed", e);
|
|
|
|
|
Logging.LogWarning(wrappedException.ToString());
|
|
|
|
|
Logging.CommandLogError(wrappedException.ToString());
|
|
|
|
|
}
|
2019-12-15 07:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|