diff options
| author | pfg <[email protected]> | 2017-03-12 09:29:17 -0700 |
|---|---|---|
| committer | pfg <[email protected]> | 2017-03-12 09:29:17 -0700 |
| commit | 6460ccee378e15408768337dcdc1bc77da07da53 (patch) | |
| tree | f32e7a443d23492343fec0881115cbcad15f6324 /ShiftOS_TheReturn/TerminalBackend.cs | |
| parent | b20d77edd1227b2ce53b65b0752972bd65a36e84 (diff) | |
| download | shiftos_thereturn-6460ccee378e15408768337dcdc1bc77da07da53.tar.gz shiftos_thereturn-6460ccee378e15408768337dcdc1bc77da07da53.tar.bz2 shiftos_thereturn-6460ccee378e15408768337dcdc1bc77da07da53.zip | |
Custom Command Syntax
Diffstat (limited to 'ShiftOS_TheReturn/TerminalBackend.cs')
| -rw-r--r-- | ShiftOS_TheReturn/TerminalBackend.cs | 213 |
1 files changed, 97 insertions, 116 deletions
diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index 8be54d0..fd2524f 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -32,26 +32,21 @@ using System.Threading.Tasks; using Newtonsoft.Json; using static ShiftOS.Engine.SaveSystem; -namespace ShiftOS.Engine -{ - public static class TerminalBackend - { +namespace ShiftOS.Engine { + public static class TerminalBackend { public static event Action<string, string> CommandProcessed; public static bool Elevated { get; set; } - public static Dictionary<string, object> GetArgs(ref string text) - { + public static Dictionary<string, string> GetArgs(ref string text) { bool shouldParse = false; int argStart = 0; - if (text.Contains("{")) - { + if (text.Contains("{")) { shouldParse = true; argStart = text.IndexOf('{'); } - if (shouldParse == false) - { + if (shouldParse == false) { string replacement = Regex.Replace(text, @"\t|\n|\r", ""); text = replacement + "{}"; shouldParse = true; @@ -61,15 +56,47 @@ namespace ShiftOS.Engine string args = text.Substring(argStart, text.Length - argStart); text = text.Remove(argStart, text.Length - argStart).Replace(" ", ""); - return JsonConvert.DeserializeObject<Dictionary<string, object>>(args); + return JsonConvert.DeserializeObject<Dictionary<string, string>>(args); } public static string LastCommand = ""; - public static void InvokeCommand(string text, bool isRemote = false) - { - try - { + public static void InvokeCommand(string ns, string command, Dictionary<string, string> arguments, bool isRemote = false) { + try { + if (string.IsNullOrWhiteSpace(ns)) + return; + + + bool commandWasClient = RunClient(ns, command, arguments, isRemote); + + if (!commandWasClient && !string.IsNullOrWhiteSpace(ns)) { + PrefixEnabled = false; + + ServerManager.SendMessage("script", $@"{{ + user: ""{ns}"", + script: ""{command}"", + args: ""{GetSentArgs(arguments)}"" +}}"); + } + + CommandProcessed?.Invoke(ns + "." + command, JsonConvert.SerializeObject(arguments)); + } catch (Exception ex) { + Console.WriteLine($"Command parse error: {ex.Message}"); // This shouldn't ever be called now + PrefixEnabled = true; + + } + } + + public static string GetSentArgs(Dictionary<string, string> argss) { + Dictionary<string, object> args = new Dictionary<string, object>(); + foreach (KeyValuePair<string, string> arg in argss) { + args[arg.Key] = arg.Value; + } + return JsonConvert.SerializeObject(args); + } + + public static void InvokeCommand(string text, bool isRemote = false) { + try { if (string.IsNullOrWhiteSpace(text)) return; @@ -77,19 +104,17 @@ namespace ShiftOS.Engine bool commandWasClient = RunClient(text, args, isRemote); - if (!commandWasClient && !string.IsNullOrWhiteSpace(text)) - { + if (!commandWasClient) { PrefixEnabled = false; + ServerManager.SendMessage("script", $@"{{ user: ""{text.Split('.')[0]}"", script: ""{text.Split('.')[1]}"", - args: ""{JsonConvert.SerializeObject(args)}"" + args: ""{GetSentArgs(args)}"" }}"); } - CommandProcessed?.Invoke(text, JsonConvert.SerializeObject(args)); - } - catch (Exception ex) - { + CommandProcessed?.Invoke(text, GetSentArgs(args)); + } catch (Exception ex) { Console.WriteLine($"Command parse error: {ex.Message}"); PrefixEnabled = true; @@ -104,18 +129,15 @@ namespace ShiftOS.Engine public static event EmptyEventHandler TerminalRequested; - internal static void OpenTerminal() - { + internal static void OpenTerminal() { TerminalRequested?.Invoke(); } - public static bool CanRunRemotely(MethodInfo mth, bool isRemote) - { + public static bool CanRunRemotely(MethodInfo mth, bool isRemote) { if (!isRemote) return true; - foreach(var attr in mth.GetCustomAttributes(false)) - { + foreach (var attr in mth.GetCustomAttributes(false)) { if (attr is RemoteLockAttribute) return false; } @@ -123,60 +145,45 @@ namespace ShiftOS.Engine return true; } - public static bool RunClient(string text, Dictionary<string, object> args, bool isRemote = false) - { + public static bool RunClient(string ns, string cmd, Dictionary<string, string> args, bool isRemote = false) { + return RunClient(ns + "." + cmd, args, isRemote); + } + + public static bool RunClient(string text, Dictionary<string, string> args, bool isRemote = false) { latestCommmand = text; - foreach (var asmExec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - try - { + 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)) - { - if (KernelWatchdog.IsSafe(type)) - { - foreach (var a in type.GetCustomAttributes(false)) - { - if (a is Namespace) - { + foreach (var type in types) { + if (Shiftorium.UpgradeAttributesUnlocked(type)) { + if (KernelWatchdog.IsSafe(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)) - { - if (KernelWatchdog.IsSafe(method)) - { - if (CanRunRemotely(method, isRemote)) - { - foreach (var ma in method.GetCustomAttributes(false)) - { - if (ma is Command) - { + if (text.Split('.')[0] == ns.name) { + foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { + if (Shiftorium.UpgradeAttributesUnlocked(method)) { + if (KernelWatchdog.IsSafe(method)) { + if (CanRunRemotely(method, isRemote)) { + foreach (var ma in method.GetCustomAttributes(false)) { + if (ma is Command) { var cmd = ma as Command; - if (text.Split('.')[1] == cmd.name) - { + if (text.Split('.')[1] == cmd.name) { var attr = method.GetCustomAttribute<CommandObsolete>(); - if (attr != null) - { + if (attr != null) { string newcommand = attr.newcommand; - if (attr.warn) - { + if (attr.warn) { Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary<string, string>() { {"%newcommand", newcommand} })); } - if (newcommand != "") - { + if (newcommand != "") { // redo the entire process running newcommand return RunClient(newcommand, args); @@ -188,13 +195,10 @@ namespace ShiftOS.Engine bool error = false; bool providedusage = false; - foreach (RequiresArgument argument in requiresArgs) - { - if (!args.ContainsKey(argument.argument)) - { + foreach (RequiresArgument argument in requiresArgs) { + if (!args.ContainsKey(argument.argument)) { - if (!providedusage) - { + if (!providedusage) { string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}"; if (usageparse == Localization.Parse(usageparse)) usageparse = ""; @@ -209,14 +213,11 @@ namespace ShiftOS.Engine providedusage = true; } - if (Shiftorium.UpgradeInstalled("help_usage")) - { + if (Shiftorium.UpgradeInstalled("help_usage")) { Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary<string, string>() { {"%argument", argument.argument} })); - } - else - { + } else { Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}")); } @@ -224,89 +225,69 @@ namespace ShiftOS.Engine } } - if (error) - { + if (error) { throw new Exception("{ERROR_COMMAND_WRONG}"); } - try - { + try { return (bool)method.Invoke(null, new[] { args }); - } - catch (TargetInvocationException e) - { + } catch (TargetInvocationException e) { Console.WriteLine(Localization.Parse("{ERROR_EXCEPTION_THROWN_IN_METHOD}")); Console.WriteLine(e.InnerException.Message); Console.WriteLine(e.InnerException.StackTrace); return true; - } - catch - { + } catch { return (bool)method.Invoke(null, new object[] { }); } } } } - } - else - { + } else { Console.WriteLine(text + " cannot be ran in a remote session"); return true; } } - + } } } } } } - + } } - } - catch { } + } catch { } } return false; } - - static TerminalBackend() - { - ServerMessageReceived onMessageReceived = (msg) => - { - if (msg.Name == "trm_invokecommand") - { + + static TerminalBackend() { + ServerMessageReceived onMessageReceived = (msg) => { + if (msg.Name == "trm_invokecommand") { string text3 = ""; string text4 = msg.Contents; - if (TerminalBackend.PrefixEnabled) - { + if (TerminalBackend.PrefixEnabled) { text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length); } IsForwardingConsoleWrites = true; - if (TerminalBackend.InStory == false) - { + if (TerminalBackend.InStory == false) { TerminalBackend.InvokeCommand(text3, true); } - if (TerminalBackend.PrefixEnabled) - { + if (TerminalBackend.PrefixEnabled) { Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ "); } IsForwardingConsoleWrites = false; - } - else if (msg.Name == "pleasewrite") - { + } else if (msg.Name == "pleasewrite") { Console.Write(msg.Contents); - } - else if(msg.Name == "handshake_from") - { + } else if (msg.Name == "handshake_from") { var a = JsonConvert.DeserializeObject<Dictionary<string, object>>(msg.Contents); string uName = a["username"] as string; string pass = a["password"] as string; string sys = a["sysname"] as string; string guid = msg.GUID; - if(SaveSystem.CurrentSave.Username == uName && SaveSystem.CurrentSave.Password == pass && CurrentSave.SystemName == sys) - { + if (SaveSystem.CurrentSave.Username == uName && SaveSystem.CurrentSave.Password == pass && CurrentSave.SystemName == sys) { ForwardGUID = guid; ServerManager.SendMessage("trm_handshake_accept", $@"{{ guid: ""{ServerManager.thisGuid}"", @@ -326,6 +307,6 @@ namespace ShiftOS.Engine public static bool IsForwardingConsoleWrites { get; internal set; } public static string ForwardGUID { get; internal set; } - + } } |
