|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- using Svelto.ECS;
-
- using GamecraftModdingAPI.Utility;
-
- namespace GamecraftModdingAPI.Commands
- {
- /// <summary>
- /// A simple implementation of ICustomCommandEngine sufficient for most commands.
- /// This version is for commands which take 1 argument(s)
- /// </summary>
- public class SimpleCustomCommandEngine<A> : ICustomCommandEngine
- {
- public string Name { get; }
-
- public string Description { get; }
-
- private Action<A> runCommand;
-
- public EntitiesDB entitiesDB { set; private get; }
-
- public bool isRemovable => true;
-
- public void Dispose()
- {
- GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
- CommandRegistrationHelper.Unregister(this.Name);
- }
-
- public void Ready()
- {
- GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Registering SimpleCustomCommandEngine {this.Name}");
- CommandRegistrationHelper.Register<A>(this.Name, this.InvokeCatchError, this.Description);
- }
-
- /// <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> command, string name, string description)
- {
- this.runCommand = command;
- this.Name = name;
- this.Description = description;
- }
-
- public void Invoke(A a)
- {
- runCommand(a);
- }
-
- private void InvokeCatchError(A a)
- {
- try
- {
- runCommand(a);
- }
- catch (Exception e)
- {
- CommandRuntimeException wrappedException = new CommandRuntimeException($"Command {Name} threw an exception when executed", e);
- Logging.LogWarning(wrappedException.ToString());
- Logging.CommandLogError(wrappedException.ToString());
- }
- }
- }
- }
|