A stable modding interface between Techblox and mods https://mod.exmods.org/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

73 wiersze
2.1KB

  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 TechbloxModdingAPI.Utility;
  8. namespace TechbloxModdingAPI.Commands
  9. {
  10. /// <summary>
  11. /// A simple implementation of ICustomCommandEngine sufficient for most commands.
  12. /// This version is for commands which take 1 argument(s)
  13. /// </summary>
  14. public class SimpleCustomCommandEngine<A> : ICustomCommandEngine
  15. {
  16. public string Name { get; }
  17. public string Description { get; }
  18. private Action<A> runCommand;
  19. public EntitiesDB entitiesDB { set; private get; }
  20. public bool isRemovable => true;
  21. public void Dispose()
  22. {
  23. Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
  24. CommandRegistrationHelper.Unregister(this.Name);
  25. }
  26. public void Ready()
  27. {
  28. Logging.MetaDebugLog($"Registering SimpleCustomCommandEngine {this.Name}");
  29. CommandRegistrationHelper.Register<A>(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> 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)
  44. {
  45. runCommand(a);
  46. }
  47. private void InvokeCatchError(A a)
  48. {
  49. try
  50. {
  51. runCommand(a);
  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. }