Browse Source

Search for the Node Separator instead of whitespace

pull/382/head
Christopher F 8 years ago
parent
commit
1158cfd0aa
4 changed files with 14 additions and 17 deletions
  1. +1
    -1
      src/Discord.Net.Commands/CommandService.cs
  2. +1
    -1
      src/Discord.Net.Commands/Info/CommandInfo.cs
  3. +2
    -2
      src/Discord.Net.Commands/Map/CommandMap.cs
  4. +10
    -13
      src/Discord.Net.Commands/Map/CommandMapNode.cs

+ 1
- 1
src/Discord.Net.Commands/CommandService.cs View File

@@ -31,7 +31,6 @@ namespace Discord.Commands
_moduleLock = new SemaphoreSlim(1, 1); _moduleLock = new SemaphoreSlim(1, 1);
_typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>(); _typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>();
_moduleDefs = new ConcurrentBag<ModuleInfo>(); _moduleDefs = new ConcurrentBag<ModuleInfo>();
_map = new CommandMap();
_typeReaders = new ConcurrentDictionary<Type, TypeReader> _typeReaders = new ConcurrentDictionary<Type, TypeReader>
{ {
[typeof(bool)] = new SimpleTypeReader<bool>(), [typeof(bool)] = new SimpleTypeReader<bool>(),
@@ -70,6 +69,7 @@ namespace Discord.Commands
}; };
_defaultRunMode = config.DefaultRunMode; _defaultRunMode = config.DefaultRunMode;
_nodeSeparator = config.NodeSeparator; _nodeSeparator = config.NodeSeparator;
_map = new CommandMap(this);
} }


//Modules //Modules


+ 1
- 1
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -42,6 +42,7 @@ namespace Discord.Commands


// both command and module provide aliases // both command and module provide aliases
if (module.Aliases.Count > 0 && builder.Aliases.Count > 0) if (module.Aliases.Count > 0 && builder.Aliases.Count > 0)
Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => second != null ? first + service._nodeSeparator + second : first).ToImmutableArray();
// only module provides aliases // only module provides aliases
else if (module.Aliases.Count > 0) else if (module.Aliases.Count > 0)
Aliases = module.Aliases.ToImmutableArray(); Aliases = module.Aliases.ToImmutableArray();
@@ -49,7 +50,6 @@ namespace Discord.Commands
else if (builder.Aliases.Count > 0) else if (builder.Aliases.Count > 0)
Aliases = builder.Aliases.ToImmutableArray(); Aliases = builder.Aliases.ToImmutableArray();
// neither provide aliases // neither provide aliases
Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => second != null ? first + " " + second : first).ToImmutableArray();
else else
throw new InvalidOperationException("Cannot build a command without any aliases"); throw new InvalidOperationException("Cannot build a command without any aliases");




+ 2
- 2
src/Discord.Net.Commands/Map/CommandMap.cs View File

@@ -7,9 +7,9 @@ namespace Discord.Commands
private readonly CommandMapNode _root; private readonly CommandMapNode _root;
private static readonly string[] _blankAliases = new[] { "" }; private static readonly string[] _blankAliases = new[] { "" };


public CommandMap()
public CommandMap(CommandService service)
{ {
_root = new CommandMapNode("");
_root = new CommandMapNode("", service);
} }


public void AddCommand(CommandInfo command) public void AddCommand(CommandInfo command)


+ 10
- 13
src/Discord.Net.Commands/Map/CommandMapNode.cs View File

@@ -7,25 +7,25 @@ namespace Discord.Commands
{ {
internal class CommandMapNode internal class CommandMapNode
{ {
private static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };

private readonly ConcurrentDictionary<string, CommandMapNode> _nodes; private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name; private readonly string _name;
private readonly object _lockObj = new object(); private readonly object _lockObj = new object();
private readonly CommandService _service;
private ImmutableArray<CommandInfo> _commands; private ImmutableArray<CommandInfo> _commands;


public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0; public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;


public CommandMapNode(string name)
public CommandMapNode(string name, CommandService service)
{ {
_name = name; _name = name;
_nodes = new ConcurrentDictionary<string, CommandMapNode>(); _nodes = new ConcurrentDictionary<string, CommandMapNode>();
_commands = ImmutableArray.Create<CommandInfo>(); _commands = ImmutableArray.Create<CommandInfo>();
_service = service;
} }


public void AddCommand(string text, int index, CommandInfo command) public void AddCommand(string text, int index, CommandInfo command)
{ {
int nextSpace = NextWhitespace(text, index);
int nextSpace = NextSeparator(text, index);
string name; string name;


lock (_lockObj) lock (_lockObj)
@@ -43,14 +43,14 @@ namespace Discord.Commands
else else
name = text.Substring(index, nextSpace - index); name = text.Substring(index, nextSpace - index);


var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x));
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x, _service));
nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command); nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
} }
} }
} }
public void RemoveCommand(string text, int index, CommandInfo command) public void RemoveCommand(string text, int index, CommandInfo command)
{ {
int nextSpace = NextWhitespace(text, index);
int nextSpace = NextSeparator(text, index);
string name; string name;


lock (_lockObj) lock (_lockObj)
@@ -77,7 +77,7 @@ namespace Discord.Commands


public IEnumerable<CommandInfo> GetCommands(string text, int index) public IEnumerable<CommandInfo> GetCommands(string text, int index)
{ {
int nextSpace = NextWhitespace(text, index);
int nextSpace = NextSeparator(text, index);
string name; string name;


var commands = _commands; var commands = _commands;
@@ -100,15 +100,12 @@ namespace Discord.Commands
} }
} }


private static int NextWhitespace(string text, int startIndex)
private int NextSeparator(string text, int startIndex)
{ {
int lowest = int.MaxValue; int lowest = int.MaxValue;
for (int i = 0; i < _whitespaceChars.Length; i++)
{
int index = text.IndexOf(_whitespaceChars[i], startIndex);
if (index != -1 && index < lowest)
int index = text.IndexOf(_service._nodeSeparator, startIndex);
if (index != -1 && index < lowest)
lowest = index; lowest = index;
}
return (lowest != int.MaxValue) ? lowest : -1; return (lowest != int.MaxValue) ? lowest : -1;
} }
} }


Loading…
Cancel
Save