-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.cs
More file actions
139 lines (122 loc) · 4.99 KB
/
CLI.cs
File metadata and controls
139 lines (122 loc) · 4.99 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using BlockCounterCLI.command;
using BlockCounterCLI.helpers;
using BlockCounterCLI.program;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace BlockCounterCLI
{
internal class CLI
{
public static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
# if DEBUG
public static bool IsDebugMode = true;
#else
public static bool IsDebugMode = false;
#endif
public void Setup()
{
// check OS
if (!IsWindows && !IsLinux)
{
throw new Exception("Current OS '" + RuntimeInformation.OSDescription + "' is not supported");
}
// create singletons
if (DataStore.Instance == null || CommandRegistry.Instance == null || ProgramRegistry.Instance == null)
{
throw new Exception("Could not create singletons");
}
CommandRegistry.Instance.RegisterCommand(typeof(HelpCommand));
CommandRegistry.Instance.RegisterCommand(typeof(StatusCommand));
CommandRegistry.Instance.RegisterCommand(typeof(SetupCommand));
CommandRegistry.Instance.RegisterCommand(typeof(ReinstallCommand));
CommandRegistry.Instance.RegisterCommand(typeof(CleanCommand));
CommandRegistry.Instance.RegisterCommand(typeof(GetOldMappings));
CommandRegistry.Instance.RegisterCommand(typeof(CountCommand));
CommandRegistry.Instance.RegisterCommand(typeof(ClearCommand));
CommandRegistry.Instance.RegisterCommand(typeof(ExitCommand));
ProgramRegistry.Instance.RegisterProgram(new JavaProgram());
ProgramRegistry.Instance.RegisterProgram(new PrismProgram());
ProgramRegistry.Instance.RegisterProgram(new PythonProgram());
ProgramRegistry.Instance.RegisterProgram(new McServerWrapperProgram());
ProgramRegistry.Instance.RegisterProgram(new MinecraftBlockcounterProgram());
AppDomain.CurrentDomain.ProcessExit +=
(sender, eventArgs) => AtExit();
}
public void RunLoop()
{
while (true)
{
Console.WriteLine(string.Concat(Enumerable.Repeat("-", 80)));
Console.Write("> ");
string userIn = Console.ReadLine();
Console.WriteLine(string.Concat(Enumerable.Repeat("-", 80)));
try
{
string result = HandleCommand(userIn);
if (result != null)
{
Console.WriteLine(result);
}
}
catch (Exception ex) {
Console.WriteLine("Exception during command handling: " + ex.Message);
if (IsDebugMode)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
public static void AtExit()
{
try
{
McServerWrapperProgram mcServerWrapper = ProgramRegistry.Instance.GetProgram(typeof(McServerWrapperProgram));
mcServerWrapper?.Stop();
}
catch { }
foreach (BaseProgram program in ProgramRegistry.Instance.GetPrograms())
{
program.AtExit();
}
}
private string HandleCommand(string rawCommand)
{
string prefix = rawCommand;
string[] args = Array.Empty<string>();
if (rawCommand.Contains(' '))
{
// code from https://stackoverflow.com/a/4780801/15436169
string[] parts = Regex.Split(rawCommand, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
parts = parts.Select(f => f.Trim('\"')).ToArray();
prefix = parts[0];
args = parts.Skip(1).ToArray();
}
Type commandType = CommandRegistry.Instance.GetCommandType(prefix);
if (commandType == null)
{
return "Command '" + rawCommand + "' not found";
}
BaseCommand cmd = Activator.CreateInstance(commandType, [args]) as BaseCommand;
foreach (Type dep in cmd.DependsOn)
{
BaseProgram program = ProgramRegistry.Instance.GetProgram(dep);
if (program == null || !program.IsSetup())
{
return "Command depends on missing program " + program.Name;
}
}
cmd.Execute();
if (cmd.HasErrored())
{
// return error message
// this is currently stored the same way as a normal result message
return cmd.GetResultMessage();
}
return cmd.GetResultMessage();
}
}
}