TechbloxModdingAPI/GamecraftModdingAPI/Commands/CommandManager.cs

73 lines
2 KiB
C#
Raw Normal View History

2019-12-15 07:20:20 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2019-12-15 07:20:20 +00:00
using Svelto.ECS;
2019-12-17 01:55:52 +00:00
using GamecraftModdingAPI.Utility;
2019-12-15 07:20:20 +00:00
namespace GamecraftModdingAPI.Commands
{
/// <summary>
/// Keeps track of custom commands
/// This is used to add, remove and get command engines
/// </summary>
public static class CommandManager
{
private static Dictionary<string, ICustomCommandEngine> _customCommands = new Dictionary<string, ICustomCommandEngine>();
private static EnginesRoot _lastEngineRoot;
2019-12-15 07:20:20 +00:00
public static void AddCommand(ICustomCommandEngine engine)
{
2020-05-01 18:50:55 +00:00
if (ExistsCommand(engine))
{
throw new CommandAlreadyExistsException($"Command {engine.Name} already exists");
2020-05-01 18:50:55 +00:00
}
2019-12-15 07:20:20 +00:00
_customCommands[engine.Name] = engine;
if (_lastEngineRoot != null)
{
Logging.MetaDebugLog($"Registering ICustomCommandEngine {engine.Name}");
_lastEngineRoot.AddEngine(engine);
}
2019-12-15 07:20:20 +00:00
}
public static bool ExistsCommand(string name)
{
return _customCommands.ContainsKey(name);
}
public static bool ExistsCommand(ICustomCommandEngine engine)
{
return ExistsCommand(engine.Name);
}
public static ICustomCommandEngine GetCommand(string name)
{
return _customCommands[name];
}
public static string[] GetCommandNames()
2019-12-15 07:20:20 +00:00
{
return _customCommands.Keys.ToArray();
2019-12-15 07:20:20 +00:00
}
public static void RemoveCommand(string name)
{
_customCommands.Remove(name);
}
public static void RegisterEngines(EnginesRoot enginesRoot)
{
_lastEngineRoot = enginesRoot;
2019-12-15 07:20:20 +00:00
foreach (var key in _customCommands.Keys)
{
2019-12-17 01:55:52 +00:00
Logging.MetaDebugLog($"Registering ICustomCommandEngine {_customCommands[key].Name}");
2019-12-15 07:20:20 +00:00
enginesRoot.AddEngine(_customCommands[key]);
}
}
}
}