-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandRegistry.cs
More file actions
47 lines (36 loc) · 1.4 KB
/
Copy pathCommandRegistry.cs
File metadata and controls
47 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections.Generic;
namespace UnityRPG.DeveloperTools
{
public sealed class CommandRegistry
{
private readonly Dictionary<string, IConsoleCommand> commands = new();
public IReadOnlyCollection<IConsoleCommand> Commands => commands.Values;
public bool Register(IConsoleCommand command)
{
if (command == null || string.IsNullOrWhiteSpace(command.Name))
return false;
string name = command.Name.ToLowerInvariant();
if (commands.ContainsKey(name))
return false;
commands.Add(name, command);
return true;
}
public bool TryGet(string name, out IConsoleCommand command)
{
if (string.IsNullOrWhiteSpace(name))
{
command = null;
return false;
}
return commands.TryGetValue(name.ToLowerInvariant(), out command);
}
public ConsoleCommandResult Execute(string input)
{
if (!CommandParser.TryParse(input, out ParsedCommand parsed))
return ConsoleCommandResult.Fail("명령어를 입력하세요.");
if (!TryGet(parsed.Name, out IConsoleCommand command))
return ConsoleCommandResult.Fail($"알 수 없는 명령어입니다: {parsed.Name}");
return command.Execute(parsed.Args);
}
}
}