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.

182 lines
5.5KB

  1. using System;
  2. using Svelto.ECS;
  3. using GamecraftModdingAPI.Utility;
  4. namespace GamecraftModdingAPI.Commands
  5. {
  6. /// <summary>
  7. /// Custom Command builder.
  8. /// </summary>
  9. public class CommandBuilder
  10. {
  11. private string name;
  12. private string description;
  13. private short parameterCount;
  14. private ICustomCommandEngine commandEngine;
  15. /// <summary>
  16. /// Create a new command builder.
  17. /// </summary>
  18. public CommandBuilder()
  19. {
  20. name = "";
  21. description = null;
  22. parameterCount = -1;
  23. }
  24. /// <summary>
  25. /// Create and return a command builder.
  26. /// </summary>
  27. /// <returns>The builder.</returns>
  28. public static CommandBuilder Builder()
  29. {
  30. return new CommandBuilder();
  31. }
  32. /// <summary>
  33. /// Create a new command builder.
  34. /// </summary>
  35. /// <param name="name">The command name.</param>
  36. /// <param name="description">The command description (shown in help).</param>
  37. public CommandBuilder(string name, string description = null)
  38. {
  39. this.name = name;
  40. this.description = description;
  41. parameterCount = -1;
  42. }
  43. /// <summary>
  44. /// Create and return a command builder.
  45. /// If name and description are provided, this is equivalent to <code>Builder().Name(name).Description(description)</code>
  46. /// </summary>
  47. /// <param name="name">The command name.</param>
  48. /// <param name="description">The command description (shown in help).</param>
  49. /// <returns>The builder.</returns>
  50. public static CommandBuilder Builder(string name, string description = null)
  51. {
  52. return new CommandBuilder(name, description);
  53. }
  54. /// <summary>
  55. /// Name the command.
  56. /// </summary>
  57. /// <returns>The builder.</returns>
  58. /// <param name="name">The command name.</param>
  59. public CommandBuilder Name(string name)
  60. {
  61. this.name = name;
  62. return this;
  63. }
  64. /// <summary>
  65. /// Describe the command.
  66. /// </summary>
  67. /// <returns>The builder.</returns>
  68. /// <param name="description">The command description (shown in help).</param>
  69. public CommandBuilder Description(string description)
  70. {
  71. this.description = description;
  72. return this;
  73. }
  74. /// <summary>
  75. /// Set the action the command performs.
  76. /// </summary>
  77. /// <returns>The builder.</returns>
  78. /// <param name="action">The action to perform when the command is called.</param>
  79. public CommandBuilder Action(Action action)
  80. {
  81. parameterCount = 0;
  82. commandEngine = new SimpleCustomCommandEngine(action, name, description);
  83. return this;
  84. }
  85. /// <summary>
  86. /// Set the action the command performs.
  87. /// </summary>
  88. /// <returns>The builder.</returns>
  89. /// <param name="action">The action to perform when the command is called.</param>
  90. /// <typeparam name="A">The 1st parameter's type.</typeparam>
  91. public CommandBuilder Action<A>(Action<A> action)
  92. {
  93. parameterCount = 1;
  94. commandEngine = new SimpleCustomCommandEngine<A>(action, name, description);
  95. return this;
  96. }
  97. /// <summary>
  98. /// Set the action the command performs.
  99. /// </summary>
  100. /// <returns>The builder.</returns>
  101. /// <param name="action">The action to perform when the command is called.</param>
  102. /// <typeparam name="A">The 1st parameter's type.</typeparam>
  103. /// <typeparam name="B">The 2nd parameter's type.</typeparam>
  104. public CommandBuilder Action<A,B>(Action<A,B> action)
  105. {
  106. parameterCount = 2;
  107. commandEngine = new SimpleCustomCommandEngine<A,B>(action, name, description);
  108. return this;
  109. }
  110. /// <summary>
  111. /// Set the action the command performs.
  112. /// </summary>
  113. /// <returns>The builder.</returns>
  114. /// <param name="action">The action to perform when the command is called.</param>
  115. /// <typeparam name="A">The 1st parameter's type.</typeparam>
  116. /// <typeparam name="B">The 2nd parameter's type.</typeparam>
  117. /// <typeparam name="C">The 3rd parameter's type.</typeparam>
  118. public CommandBuilder Action<A,B,C>(Action<A,B,C> action)
  119. {
  120. parameterCount = 3;
  121. commandEngine = new SimpleCustomCommandEngine<A,B,C>(action, name, description);
  122. return this;
  123. }
  124. /// <summary>
  125. /// Build the command.
  126. /// </summary>
  127. /// <returns>The built command.</returns>
  128. /// <param name="register">Automatically register the command with CommandManager.AddCommand()?</param>
  129. public ICustomCommandEngine Build(bool register = true)
  130. {
  131. if (string.IsNullOrWhiteSpace(name))
  132. {
  133. throw new InvalidOperationException("Command name must be defined before Build() is called");
  134. }
  135. if (commandEngine == null)
  136. {
  137. throw new InvalidOperationException("Command action must be defined before Build() is called");
  138. }
  139. if (string.IsNullOrWhiteSpace(description))
  140. {
  141. Logging.LogWarning($"Command {name} was built without a description");
  142. }
  143. if (register)
  144. {
  145. CommandManager.AddCommand(commandEngine);
  146. Logging.MetaDebugLog($"Command {FullName()} was automatically registered");
  147. }
  148. return commandEngine;
  149. }
  150. /// <summary>
  151. /// Get the full command name, in the form [name]::[description]::[# of parameters]
  152. /// </summary>
  153. /// <returns>The name.</returns>
  154. public string FullName()
  155. {
  156. if (string.IsNullOrWhiteSpace(description))
  157. {
  158. return name + "::" + parameterCount;
  159. }
  160. return name + "::" + description + "::" + parameterCount;
  161. }
  162. }
  163. }