Cleaned up command builders and async func names

This commit is contained in:
RogueException
2016-11-18 07:28:30 -04:00
parent 7505831998
commit 593ba46f1c
11 changed files with 273 additions and 318 deletions

View File

@@ -1,95 +1,39 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
namespace Discord.Commands
{
internal class CommandMap
{
static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
private readonly object _lockObj = new object();
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly CommandMapNode _root;
private static readonly string[] _blankAliases = new[] { "" };
public CommandMap()
{
_nodes = new ConcurrentDictionary<string, CommandMapNode>();
_root = new CommandMapNode("");
}
public void AddCommand(CommandInfo command)
{
foreach (string text in command.Aliases)
{
int nextSpace = NextWhitespace(text);
string name;
if (nextSpace == -1)
name = text;
else
name = text.Substring(0, nextSpace);
lock (_lockObj)
{
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x));
nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
}
}
foreach (string text in GetAliases(command))
_root.AddCommand(text, 0, command);
}
public void RemoveCommand(CommandInfo command)
{
foreach (string text in command.Aliases)
{
int nextSpace = NextWhitespace(text);
string name;
if (nextSpace == -1)
name = text;
else
name = text.Substring(0, nextSpace);
lock (_lockObj)
{
CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode))
{
nextNode.RemoveCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
if (nextNode.IsEmpty)
_nodes.TryRemove(name, out nextNode);
}
}
}
foreach (string text in GetAliases(command))
_root.RemoveCommand(text, 0, command);
}
public IEnumerable<CommandInfo> GetCommands(string text)
{
int nextSpace = NextWhitespace(text);
string name;
if (nextSpace == -1)
name = text;
else
name = text.Substring(0, nextSpace);
lock (_lockObj)
{
CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode))
return nextNode.GetCommands(text, nextSpace + 1);
else
return Enumerable.Empty<CommandInfo>();
}
return _root.GetCommands(text, 0);
}
private static int NextWhitespace(string text)
private IReadOnlyList<string> GetAliases(CommandInfo command)
{
int lowest = int.MaxValue;
for (int i = 0; i < _whitespaceChars.Length; i++)
{
int index = text.IndexOf(_whitespaceChars[i]);
if (index != -1 && index < lowest)
lowest = index;
}
return (lowest != int.MaxValue) ? lowest : -1;
var aliases = command.Aliases;
if (aliases.Count == 0)
return _blankAliases;
return aliases;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -6,6 +7,8 @@ namespace Discord.Commands
{
internal class CommandMapNode
{
private static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name;
private readonly object _lockObj = new object();
@@ -22,13 +25,17 @@ namespace Discord.Commands
public void AddCommand(string text, int index, CommandInfo command)
{
int nextSpace = text.IndexOf(' ', index);
int nextSpace = NextWhitespace(text, index);
string name;
lock (_lockObj)
{
if (text == "")
{
if (_name == "")
throw new InvalidOperationException("Cannot add commands to the root node.");
_commands = _commands.Add(command);
}
else
{
if (nextSpace == -1)
@@ -43,7 +50,7 @@ namespace Discord.Commands
}
public void RemoveCommand(string text, int index, CommandInfo command)
{
int nextSpace = text.IndexOf(' ', index);
int nextSpace = NextWhitespace(text, index);
string name;
lock (_lockObj)
@@ -70,7 +77,7 @@ namespace Discord.Commands
public IEnumerable<CommandInfo> GetCommands(string text, int index)
{
int nextSpace = text.IndexOf(' ', index);
int nextSpace = NextWhitespace(text, index);
string name;
var commands = _commands;
@@ -92,5 +99,17 @@ namespace Discord.Commands
}
}
}
private static int NextWhitespace(string text, int startIndex)
{
int lowest = int.MaxValue;
for (int i = 0; i < _whitespaceChars.Length; i++)
{
int index = text.IndexOf(_whitespaceChars[i], startIndex);
if (index != -1 && index < lowest)
lowest = index;
}
return (lowest != int.MaxValue) ? lowest : -1;
}
}
}