A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleCustomCommandEngine2.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Svelto.ECS;
  7. using GamecraftModdingAPI.Utility;
  8. namespace GamecraftModdingAPI.Commands
  9. {
  10. /// <summary>
  11. /// A simple implementation of ICustomCommandEngine sufficient for most commands.
  12. /// This version is for commands which take 2 argument(s)
  13. /// </summary>
  14. public class SimpleCustomCommandEngine<A,B> : ICustomCommandEngine
  15. {
  16. public string Name { get; }
  17. public string Description { get; }
  18. private Action<A,B> runCommand;
  19. public EntitiesDB entitiesDB { set; private get; }
  20. public bool isRemovable => true;
  21. public void Dispose()
  22. {
  23. GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
  24. CommandRegistrationHelper.Unregister(this.Name);
  25. }
  26. public void Ready()
  27. {
  28. GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Registering SimpleCustomCommandEngine {this.Name}");
  29. CommandRegistrationHelper.Register<A,B>(this.Name, this.InvokeCatchError, this.Description);
  30. }
  31. /// <summary>
  32. /// Construct the engine
  33. /// </summary>
  34. /// <param name="command">The command's operation</param>
  35. /// <param name="name">The name of the command</param>
  36. /// <param name="description">The command's description, shown in command help messages</param>
  37. public SimpleCustomCommandEngine(Action<A,B> command, string name, string description)
  38. {
  39. this.runCommand = command;
  40. this.Name = name;
  41. this.Description = description;
  42. }
  43. public void Invoke(A a, B b)
  44. {
  45. runCommand(a, b);
  46. }
  47. private void InvokeCatchError(A a, B b)
  48. {
  49. try
  50. {
  51. runCommand(a, b);
  52. }
  53. catch (Exception e)
  54. {
  55. CommandRuntimeException wrappedException = new CommandRuntimeException($"Command {Name} threw an exception when executed", e);
  56. Logging.LogWarning(wrappedException.ToString());
  57. Logging.CommandLogError(wrappedException.ToString());
  58. }
  59. }
  60. }
  61. }