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.

72 line
2.0KB

  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. /// Keeps track of custom commands
  12. /// This is used to add, remove and get command engines
  13. /// </summary>
  14. public static class CommandManager
  15. {
  16. private static Dictionary<string, ICustomCommandEngine> _customCommands = new Dictionary<string, ICustomCommandEngine>();
  17. private static EnginesRoot _lastEngineRoot;
  18. public static void AddCommand(ICustomCommandEngine engine)
  19. {
  20. if (ExistsCommand(engine))
  21. {
  22. throw new CommandAlreadyExistsException($"Command {engine.Name} already exists");
  23. }
  24. _customCommands[engine.Name] = engine;
  25. if (_lastEngineRoot != null)
  26. {
  27. Logging.MetaDebugLog($"Registering ICustomCommandEngine {engine.Name}");
  28. _lastEngineRoot.AddEngine(engine);
  29. }
  30. }
  31. public static bool ExistsCommand(string name)
  32. {
  33. return _customCommands.ContainsKey(name);
  34. }
  35. public static bool ExistsCommand(ICustomCommandEngine engine)
  36. {
  37. return ExistsCommand(engine.Name);
  38. }
  39. public static ICustomCommandEngine GetCommand(string name)
  40. {
  41. return _customCommands[name];
  42. }
  43. public static string[] GetCommandNames()
  44. {
  45. return _customCommands.Keys.ToArray();
  46. }
  47. public static void RemoveCommand(string name)
  48. {
  49. _customCommands.Remove(name);
  50. }
  51. public static void RegisterEngines(EnginesRoot enginesRoot)
  52. {
  53. _lastEngineRoot = enginesRoot;
  54. foreach (var key in _customCommands.Keys)
  55. {
  56. Logging.MetaDebugLog($"Registering ICustomCommandEngine {_customCommands[key].Name}");
  57. enginesRoot.AddEngine(_customCommands[key]);
  58. }
  59. }
  60. }
  61. }