forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleCommand.cs
More file actions
80 lines (70 loc) · 2.04 KB
/
Copy pathConsoleCommand.cs
File metadata and controls
80 lines (70 loc) · 2.04 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using L2dotNET.ConsoleCommand.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace L2dotNET.ConsoleCommand
{
public class ConsoleCommandController
{
private readonly ICollection<Command> _commands;
private Thread _commandThread;
public ConsoleCommandController()
{
_commands = new List<Command>
{
new ObjectsCount(),
new PlayersCount()
};
}
public void Start()
{
if (_commandThread != null)
{
return;
}
_commandThread = new Thread(EnterCommand)
{
Priority = ThreadPriority.Lowest,
IsBackground = true
};
_commandThread.Start();
}
public void Stop()
{
if (_commandThread == null)
{
return;
}
_commandThread.Abort();
_commandThread = null;
}
private void EnterCommand()
{
Console.WriteLine("Command engine is running. Type help to get the info about available commands.");
while (true)
{
string key = Console.ReadLine()?.ToLower();
if (key == "help")
{
Console.WriteLine("Available commands:");
_commands.Select(x => $"{x.Name} - {x.Description}")
.ToList()
.ForEach(Console.WriteLine);
}
else
{
Command cmd = _commands.FirstOrDefault(x => x.Name.ToLower() == key);
if (cmd != null)
{
cmd.Execute();
}
else
{
Console.WriteLine("Unrecognized command");
}
}
}
}
}
}