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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static ShiftOS.Engine.SaveSystem;
namespace ShiftOS.Engine
{
public static class TerminalBackend
{
public static bool Elevated { get; set; }
public static Dictionary<string, object> GetArgs(ref string text)
{
bool shouldParse = false;
int argStart = 0;
if (text.Contains("{"))
{
shouldParse = true;
argStart = text.IndexOf('{');
}
if (shouldParse == false)
{
string replacement = Regex.Replace(text, @"\t|\n|\r", "");
text = replacement + "{}";
shouldParse = true;
argStart = text.IndexOf('{');
}
string args = text.Substring(argStart, text.Length - argStart);
text = text.Remove(argStart, text.Length - argStart).Replace(" ", "");
return JsonConvert.DeserializeObject<Dictionary<string, object>>(args);
}
public static string LastCommand = "";
public static void InvokeCommand(string text)
{
try
{
if (string.IsNullOrWhiteSpace(text))
return;
var args = GetArgs(ref text);
bool commandWasClient = RunClient(text, args);
if (!commandWasClient && !string.IsNullOrWhiteSpace(text))
{
PrefixEnabled = false;
ServerManager.SendMessage("script", $@"{{
user: ""{text.Split('.')[0]}"",
script: ""{text.Split('.')[1]}"",
args: ""{JsonConvert.SerializeObject(args)}""
}}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Command parse error: {ex.Message}");
PrefixEnabled = true;
}
}
public static bool PrefixEnabled { get; set; }
public static bool InStory { get; set; }
public static string latestCommmand = "";
public static event EmptyEventHandler TerminalRequested;
internal static void OpenTerminal()
{
TerminalRequested?.Invoke();
}
public static bool RunClient(string text, Dictionary<string, object> args)
{
latestCommmand = text;
foreach (var asmExec in System.IO.Directory.GetFiles(Environment.CurrentDirectory))
{
try
{
var asm = Assembly.LoadFile(asmExec);
var types = asm.GetTypes();
foreach (var type in types)
{
if (Shiftorium.UpgradeAttributesUnlocked(type))
{
foreach (var a in type.GetCustomAttributes(false))
{
if (a is Namespace)
{
var ns = a as Namespace;
if (text.Split('.')[0] == ns.name)
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
if (Shiftorium.UpgradeAttributesUnlocked(method))
{
foreach (var ma in method.GetCustomAttributes(false))
{
if (ma is Command)
{
var cmd = ma as Command;
if (text.Split('.')[1] == cmd.name)
{
var attr = method.GetCustomAttribute<CommandObsolete>();
if (attr != null)
{
string newcommand = attr.newcommand;
if (attr.warn)
{
Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary<string, string>() {
{"%newcommand", newcommand}
}));
}
if (newcommand != "")
{
// redo the entire process running newcommand
return RunClient(newcommand, args);
}
}
var requiresArgs = method.GetCustomAttributes<RequiresArgument>();
bool error = false;
bool providedusage = false;
foreach (RequiresArgument argument in requiresArgs)
{
if (!args.ContainsKey(argument.argument))
{
if (!providedusage)
{
string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}";
if (usageparse == Localization.Parse(usageparse))
usageparse = "";
else
usageparse = Shiftorium.UpgradeInstalled("help_usage") ? Localization.Parse("{ERROR}{USAGE}" + usageparse, new Dictionary<string, string>() {
{"%ns", ns.name},
{"%cmd", cmd.name}
}) : "";
Console.WriteLine(usageparse);
providedusage = true;
}
if (Shiftorium.UpgradeInstalled("help_usage"))
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary<string, string>() {
{"%argument", argument.argument}
}));
}
else
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}"));
}
error = true;
}
}
if (error)
{
throw new Exception("{ERROR_COMMAND_WRONG}");
}
try
{
return (bool)method.Invoke(null, new[] { args });
}
catch
{
return (bool)method.Invoke(null, new object[] { });
}
}
}
}
}
}
}
}
}
}
}
}
catch { }
}
return false;
}
}
}
|