-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramework.cs
More file actions
263 lines (234 loc) · 9.06 KB
/
Copy pathFramework.cs
File metadata and controls
263 lines (234 loc) · 9.06 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using EasyConsoleCommands.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace EasyConsoleCommands
{
public class Framework
{
public List<ICommand> Commands = new List<ICommand>();
public List<VariableInfo> Variables = new List<VariableInfo>();
public Dictionary<string, ICommand> CommandAliases = new Dictionary<string, ICommand>();
public Framework()
{
AddCommandsToList();
}
internal void AddCommandsToList()
{
Commands.Add(new CommandHelp(Commands));
Commands.Add(new CommandColor());
Commands.Add(new CommandJoke());
Commands.Add(new CommandIP());
Commands.Add(new CommandPing());
Commands.Add(new CommandSystem());
Commands.Add(new CommandHacker());
Commands.Add(new CommandClear());
Commands.Add(new CommandExit());
Commands.Add(new CommandScript(this));
Commands.Add(new CommandSleep());
Commands.Add(new CommandPrint(this));
Commands.Add(new CommandVar(this));
Commands.Add(new CommandGet(this));
Commands.Add(new CommandCalc());
Commands.Add(new CommandRm(this));
Commands.Add(new CommandSetValue(this));
Commands.Add(new CommandSetName(this));
Commands.Add(new CommandProcess());
Commands.Add(new CommandKill());
Commands.Add(new CommandRead(this));
Commands.Add(new CommandAlias(this));
Commands.Add(new CommandAdd());
Commands.Add(new CommandSub());
}
public void HandleError(Exception e)
{
ConsoleColor currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + e.Message);
Console.ForegroundColor = currentColor;
}
public (string, string) ParseCommand(string input)
{
string[] inputArray = input.Split("-", 2);
string cmd = inputArray[0];
string parameters = (inputArray.Length > 1) ? inputArray[1] : string.Empty;
return (cmd, parameters);
}
public List<VariableInfo> ParseParameters(ICommand command, string input)
{
string[] inputArray = input.Split("-", StringSplitOptions.RemoveEmptyEntries);
if (inputArray.Length != command.ParameterTypes.Count)
throw new Exception($"Invalid count of parameters. Type 'help' for a list of commands.");
List<VariableInfo> result = new List<VariableInfo>();
int varPos = 0;
foreach (var paramType in command.ParameterTypes)
{
string value = inputArray[varPos];
VariableInfo varTypeInfo = Activator.CreateInstance(paramType) as VariableInfo;
switch (varTypeInfo.Type)
{
case VariableType.String:
result.Add(new StringInfo("", GetValueAsString(value)));
break;
case VariableType.Int:
result.Add(new IntInfo("", Convert.ToInt32(value)));
break;
case VariableType.Bool:
result.Add(new BoolInfo("", Convert.ToBoolean(value)));
break;
default:
throw new Exception();
}
varPos++;
}
return result;
}
private string GetValueAsString(string value)
{
if (string.IsNullOrEmpty(value))
throw new Exception("String is empty");
VariableInfo varInfo = GetVariableOrDefault(value);
if(varInfo != null)
{
if(varInfo.Type != VariableType.String)
throw new Exception($"Variable '{varInfo.Name}' is not a string");
return varInfo.GetValueAsString();
}
if (!value.StartsWith("\"") && !value.EndsWith("\""))
throw new Exception("String is in wrong format");
return value.Substring(1, value.Length - 2);
}
public void Execute(string userInput)
{
try
{
var (cmd, parameters) = ParseCommand(userInput);
ICommand command = null;
if (CommandAliases.ContainsKey(cmd))
command = CommandAliases[cmd];
else
command = Commands.FirstOrDefault(x => x.Name == cmd);
if (command == null)
throw new Exception($"Unknown command: {userInput}. Type 'help' for a list of commands.");
List<VariableInfo> paramInfos = ParseParameters(command, parameters);
command.Execute(paramInfos);
}
catch (Exception ex)
{
HandleError(ex);
}
}
internal void AddVariable(VariableInfo variableInfo)
{
Variables.Add(variableInfo);
}
public VariableInfo GetVariable(string variableName)
{
try
{
VariableInfo variableGet = GetVariableOrDefault(variableName);
if (variableGet == null)
{
throw new Exception("Variable not found");
}
return variableGet;
}
catch (Exception ex)
{
throw new Exception("Could not get variable");
}
}
public VariableInfo GetVariableOrDefault(string variableName)
{
return Variables.Find(variable => variable.Name == variableName);
}
public void DeleteVariable(string variableName)
{
try
{
Variables.Remove(GetVariable(variableName));
}
catch(Exception ex)
{
throw new Exception("Couldnt delete variable");
}
}
public void SetVariableValue(string variableName, string newValue)
{
VariableType variableType = GetVariable(variableName).Type;
try
{
DeleteVariable(variableName);
try
{
switch (variableType)
{
case VariableType.String:
string newString = Convert.ToString(newValue);
AddVariable(new StringInfo(variableName, newString));
break;
case VariableType.Int:
int newInt = Convert.ToInt32(newValue);
AddVariable(new IntInfo(variableName, newInt));
break;
case VariableType.Bool:
bool newBool = Convert.ToBoolean(newValue);
AddVariable(new BoolInfo(variableName, newBool));
break;
default:
throw new Exception("Invalid type");
}
}
catch (Exception ex)
{
throw new Exception("Invalid type");
}
}
catch(Exception ex)
{
throw new Exception("Couldnt set new variable value");
}
}
public void SetVariableName(string oldName, string newName)
{
VariableInfo variableType = GetVariable(oldName);
var variableValue = GetVariable(oldName).GetValueAsString();
try
{
DeleteVariable(oldName);
try
{
switch (variableType.Type)
{
case VariableType.String:
AddVariable(new StringInfo(newName, variableValue));
break;
case VariableType.Int:
int newInt = Convert.ToInt32(variableValue);
AddVariable(new IntInfo(newName, newInt));
break;
case VariableType.Bool:
bool newBool = Convert.ToBoolean(variableValue);
AddVariable(new BoolInfo(newName, newBool));
break;
default:
throw new Exception("Invalid name");
}
}
catch (Exception ex)
{
throw new Exception("Invalid name");
}
}
catch (Exception ex)
{
throw new Exception("Couldnt set new variable name");
}
}
}
}