2020-05-26 14:47:20 -05:00
|
|
|
using System;
|
2020-05-26 21:06:31 -05:00
|
|
|
using System.Collections;
|
2020-05-26 14:47:20 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using SlatedGameToolkit.Tools.System.Interaction;
|
|
|
|
|
|
|
|
namespace SlatedGameToolkit.Tools.System
|
|
|
|
{
|
2020-05-27 00:20:41 -05:00
|
|
|
public class CommandMap : ICollection<IInvocable>, IDisposable {
|
2020-05-26 14:47:20 -05:00
|
|
|
Dictionary<string, IInvocable> invocations;
|
2020-05-26 21:06:31 -05:00
|
|
|
HashSet<IInvocable> values;
|
|
|
|
|
|
|
|
public int Count => invocations.Count;
|
|
|
|
|
|
|
|
public bool IsReadOnly => false;
|
|
|
|
|
2020-05-26 14:47:20 -05:00
|
|
|
public IInvocable this[string invocation] {
|
|
|
|
get {
|
|
|
|
IInvocable result = null;
|
|
|
|
invocations.TryGetValue(invocation, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public CommandMap(params IInvocable[] invokables) {
|
|
|
|
invocations = new Dictionary<string, IInvocable>();
|
2020-05-26 21:06:31 -05:00
|
|
|
values = new HashSet<IInvocable>();
|
2020-05-26 14:47:20 -05:00
|
|
|
foreach (IInvocable item in invokables)
|
|
|
|
{
|
2020-05-26 21:06:31 -05:00
|
|
|
Add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator<IInvocable> GetEnumerator()
|
|
|
|
{
|
|
|
|
return values.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return values.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Add(IInvocable item)
|
|
|
|
{
|
|
|
|
values.Add(item);
|
|
|
|
string[] invokers = item.GetInvokers();
|
|
|
|
foreach (string invoker in invokers)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
invocations.Add(invoker, item);
|
|
|
|
} catch (ArgumentException e) {
|
|
|
|
throw new ArgumentException("A duplicate invocation string was found!", e);
|
2020-05-26 14:47:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 21:06:31 -05:00
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
invocations.Clear();
|
|
|
|
values.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(IInvocable item)
|
|
|
|
{
|
|
|
|
return values.Contains(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(IInvocable[] array, int arrayIndex)
|
|
|
|
{
|
|
|
|
values.CopyTo(array, arrayIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Remove(IInvocable item)
|
|
|
|
{
|
|
|
|
string[] invokers = item.GetInvokers();
|
|
|
|
foreach (string invoker in invokers)
|
|
|
|
{
|
|
|
|
if (!invocations.Remove(invoker)) return false;
|
|
|
|
}
|
|
|
|
values.Remove(item);
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-27 00:20:41 -05:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
foreach (IInvocable invocable in this)
|
|
|
|
{
|
|
|
|
invocable.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~CommandMap() {
|
|
|
|
Dispose();
|
|
|
|
}
|
2020-05-26 14:47:20 -05:00
|
|
|
}
|
|
|
|
}
|