Browse Source

Fix CommandMapNode.GetCommands and RemoveCommand

I'm absolutely sure I tested this. In fact, I even have my test code
for this which somehow broke. It works now, though.
pull/421/head
FiniteReality 8 years ago
parent
commit
67b16b1ae3
3 changed files with 28 additions and 25 deletions
  1. +2
    -2
      src/Discord.Net.Commands/CommandService.cs
  2. +2
    -2
      src/Discord.Net.Commands/Info/ModuleInfo.cs
  3. +24
    -21
      src/Discord.Net.Commands/Map/CommandMapNode.cs

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

@@ -157,8 +157,8 @@ namespace Discord.Commands
try try
{ {
ModuleInfo module; ModuleInfo module;
_typedModuleDefs.TryGetValue(typeof(T), out module);
if (module == default(ModuleInfo))
if (!_typedModuleDefs.TryRemove(typeof(T), out module))
return false; return false;


return RemoveModuleInternal(module); return RemoveModuleInternal(module);


+ 2
- 2
src/Discord.Net.Commands/Info/ModuleInfo.cs View File

@@ -15,7 +15,7 @@ namespace Discord.Commands
public string Remarks { get; } public string Remarks { get; }


public IReadOnlyList<string> Aliases { get; } public IReadOnlyList<string> Aliases { get; }
public IEnumerable<CommandInfo> Commands { get; }
public IReadOnlyList<CommandInfo> Commands { get; }
public IReadOnlyList<PreconditionAttribute> Preconditions { get; } public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
public IReadOnlyList<ModuleInfo> Submodules { get; } public IReadOnlyList<ModuleInfo> Submodules { get; }
public ModuleInfo Parent { get; } public ModuleInfo Parent { get; }
@@ -31,7 +31,7 @@ namespace Discord.Commands
Parent = parent; Parent = parent;


Aliases = BuildAliases(builder, service).ToImmutableArray(); Aliases = BuildAliases(builder, service).ToImmutableArray();
Commands = builder.Commands.Select(x => x.Build(this, service));
Commands = builder.Commands.Select(x => x.Build(this, service)).ToImmutableArray();
Preconditions = BuildPreconditions(builder).ToImmutableArray(); Preconditions = BuildPreconditions(builder).ToImmutableArray();


Submodules = BuildSubmodules(builder, service).ToImmutableArray(); Submodules = BuildSubmodules(builder, service).ToImmutableArray();


+ 24
- 21
src/Discord.Net.Commands/Map/CommandMapNode.cs View File

@@ -25,7 +25,7 @@ namespace Discord.Commands


public void AddCommand(CommandService service, string text, int index, CommandInfo command) public void AddCommand(CommandService service, string text, int index, CommandInfo command)
{ {
int nextSpace = NextWhitespace(service, text, index);
int nextSegment = NextCommandSegment(service, text, index);
string name; string name;


lock (_lockObj) lock (_lockObj)
@@ -38,19 +38,19 @@ namespace Discord.Commands
} }
else else
{ {
if (nextSpace == -1)
if (nextSegment == -1)
name = text.Substring(index); name = text.Substring(index);
else else
name = text.Substring(index, nextSpace - index);
name = text.Substring(index, nextSegment - index);


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


lock (_lockObj) lock (_lockObj)
@@ -59,15 +59,15 @@ namespace Discord.Commands
_commands = _commands.Remove(command); _commands = _commands.Remove(command);
else else
{ {
if (nextSpace == -1)
if (nextSegment == -1)
name = text.Substring(index); name = text.Substring(index);
else else
name = text.Substring(index, nextSpace - index);
name = text.Substring(index, nextSegment - index);


CommandMapNode nextNode; CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode)) if (_nodes.TryGetValue(name, out nextNode))
{ {
nextNode.RemoveCommand(service, nextSpace == -1 ? "" : text, nextSpace + 1, command);
nextNode.RemoveCommand(service, nextSegment == -1 ? "" : text, nextSegment + 1, command);
if (nextNode.IsEmpty) if (nextNode.IsEmpty)
_nodes.TryRemove(name, out nextNode); _nodes.TryRemove(name, out nextNode);
} }
@@ -75,40 +75,43 @@ namespace Discord.Commands
} }
} }


public IEnumerable<CommandInfo> GetCommands(CommandService service, string text, int index)
public IEnumerable<CommandInfo> GetCommands(CommandService service, string text, int index, bool lastLevel = false)
{ {
int nextCommand = NextCommandSegment(service, text, index); int nextCommand = NextCommandSegment(service, text, index);
string name = null; string name = null;


//got all command segments or base-level command
//got all command segments
if (nextCommand == -1) if (nextCommand == -1)
{ {
var commands = _commands;
for (int i = 0; i < commands.Length; i++)
yield return _commands[i];

//are we a base-level command?
//do we have parameters?
int nextSpace = NextWhitespace(service, text, index); int nextSpace = NextWhitespace(service, text, index);
if (nextSpace != -1)
{

if (nextSpace != -1 && !lastLevel)
name = text.Substring(index, nextSpace - index); name = text.Substring(index, nextSpace - index);
}
else else
{
name = text.Substring(index); name = text.Substring(index);
}

lastLevel = true;
nextCommand = nextSpace;
} }
else else
{ {
name = text.Substring(index, nextCommand - index); name = text.Substring(index, nextCommand - index);
} }


if (nextCommand == -1 || lastLevel)
{
var commands = _commands;
for (int i = 0; i < commands.Length; i++)
yield return _commands[i];
}

if (name != null) if (name != null)
{ {
CommandMapNode nextNode; CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode)) if (_nodes.TryGetValue(name, out nextNode))
{ {
foreach (var cmd in nextNode.GetCommands(service, text, nextCommand + 1))
foreach (var cmd in nextNode.GetCommands(service, text, nextCommand + 1, lastLevel))
yield return cmd; yield return cmd;
} }
} }


Loading…
Cancel
Save