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.

83 lines
2.4KB

  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 0 argument(s)
  13. /// </summary>
  14. public class SimpleCustomCommandEngine : ICustomCommandEngine
  15. {
  16. /// <summary>
  17. /// The name of the command
  18. /// </summary>
  19. public string Name { get; }
  20. /// <summary>
  21. /// The command's description, shown in command help messages
  22. /// </summary>
  23. public string Description { get; }
  24. /// <summary>
  25. /// The operation to execute when the command is called by the player
  26. /// </summary>
  27. private Action runCommand;
  28. public EntitiesDB entitiesDB { set; private get; }
  29. public bool isRemovable => true;
  30. public void Dispose()
  31. {
  32. GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
  33. CommandRegistrationHelper.Unregister(this.Name);
  34. }
  35. public void Ready()
  36. {
  37. GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Registering SimpleCustomCommandEngine {this.Name}");
  38. CommandRegistrationHelper.Register(this.Name, this.InvokeCatchError, this.Description);
  39. }
  40. /// <summary>
  41. /// Construct the engine
  42. /// </summary>
  43. /// <param name="command">The command's operation</param>
  44. /// <param name="name">The name of the command</param>
  45. /// <param name="description">The command's description, shown in command help messages</param>
  46. public SimpleCustomCommandEngine(Action command, string name, string description)
  47. {
  48. this.runCommand = command;
  49. this.Name = name;
  50. this.Description = description;
  51. }
  52. public void Invoke()
  53. {
  54. runCommand();
  55. }
  56. private void InvokeCatchError()
  57. {
  58. try
  59. {
  60. runCommand();
  61. }
  62. catch (Exception e)
  63. {
  64. CommandRuntimeException wrappedException = new CommandRuntimeException($"Command {Name} threw an exception when executed", e);
  65. Logging.LogWarning(wrappedException.ToString());
  66. Logging.CommandLogError(wrappedException.ToString());
  67. }
  68. }
  69. }
  70. }