forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (92 loc) · 3.31 KB
/
Program.cs
File metadata and controls
106 lines (92 loc) · 3.31 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Text;
namespace LibSample
{
public static class Program
{
private static readonly IExample[] Examples = {
new EchoMessagesTest(),
new HolePunchServerTest(),
new BroadcastTest(),
new SerializerBenchmark(),
new SpeedBench(),
new PacketProcessorExample(),
new AesEncryptionTest(),
new NtpTest(),
};
static void Main(string[] args)
{
AppendExampleMenu(MenuStringBuilder);
WriteAndClean(MenuStringBuilder);
do
{
Console.Write("Write command: ");
var input = Console.ReadLine();
if (input != null)
{
var lcInput = input.ToLower();
if (lcInput == "help" || lcInput == "h")
{
AppendFullHelpMenu(MenuStringBuilder);
WriteAndClean(MenuStringBuilder);
continue;
}
if (lcInput == "quit" || lcInput == "exit" || lcInput == "q" || lcInput == "e")
{
break;
}
if (int.TryParse(input, out var optionKey))
{
if (optionKey < 0 || optionKey >= Examples.Length)
{
PrintInvalidCommand(input);
continue;
}
((IExample)Activator.CreateInstance(Examples[optionKey].GetType())).Run();
}
else
{
PrintInvalidCommand(input);
}
}
else
{
PrintInvalidCommand(string.Empty);
}
} while (true);
}
private static void PrintInvalidCommand(string invalidInput)
{
AppendInvalidCommand(MenuStringBuilder, invalidInput);
WriteAndClean(MenuStringBuilder);
}
private static readonly StringBuilder MenuStringBuilder = new StringBuilder();
private static void WriteAndClean(StringBuilder sb)
{
Console.WriteLine(sb.ToString());
sb.Clear();
}
private static void AppendInvalidCommand(StringBuilder sb, string invalidInput)
{
sb.Append("Invalid input \"");
sb.Append(string.IsNullOrWhiteSpace(invalidInput) ? "[Whitespace/Empty Line]" : invalidInput);
sb.AppendLine("\" command. Write \"help\" command for more information.");
}
private static void AppendFullHelpMenu(StringBuilder sb)
{
sb.AppendLine();
sb.AppendLine("\"help/h\" - write helper text for this console menu.");
sb.AppendLine("\"exit/e/quit/q\" - close app");
AppendExampleMenu(sb);
sb.AppendLine();
}
private static void AppendExampleMenu(StringBuilder sb)
{
for (var i = 0; i < Examples.Length; i++)
{
var example = Examples[i];
sb.AppendLine($"\"{i}\" - Example of {example.GetType().Name}");
}
}
}
}