public static void Echo(Context context, params object[] message) { foreach (var msg in message) { context.Notify(msg); } }
public static void Cat(Context context, string category) { var cat = category.ToLower().Trim(); if (cat == "") { context.Notify("Current category: " + context.Category); return; } context.Category = cat == "$" ? "" : cat; }
public static void Decrement(Context context, string cvName) { Convar convar; if (!context.RequestConvar(cvName, out convar)) return; var o = convar.Value; if (!Util.Decrement(ref o)) { context.Notify("Convar '" + cvName + "' is not a numeric type."); return; } convar.Value = o; }
public static void Exec(Context context, params string[] files) { foreach (var path in files) { try { using (var reader = new StreamReader(path)) { while (!reader.EndOfStream) { // ReSharper disable once PossibleNullReferenceException // ReadLine() should never be null since the loop breaks at EOF Devcom.Submit(context, reader.ReadLine().Trim()); } } } catch (Exception ex) { context.NotifyFormat("Failed to load {0}: '{1}'", path, ex.Message); } } }
internal CallArgs TranslateArgs(Context context, string[] args) { var currentContextType = context.GetType(); // Check context type compatability if ((!currentContextType.IsSubclassOf(_contextType) && currentContextType != _contextType) || !ContextFilter.Test(currentContextType, Filter)) { throw new ArgumentException(String.Concat("Incompatible context type: '", currentContextType.Name, "'")); } int argc = args.Length; int paramc = _paramList.Length - 1; if ((_hasParamsArgument && argc < paramc) || (argc < paramc - _numOptionalParams) || (!_hasParamsArgument && argc > paramc)) { throw new ArgumentException("Parameter count mismatch."); } var boxed = Enumerable.Repeat(Type.Missing, paramc).ToArray(); // Convert parameters to the proper types for (int i = 0; i < paramc; i++) { boxed[i] = Util.ChangeType(args[i], _paramList[(i >= paramc ? paramc - 1 : i) + 1].ParameterType); } var argsFormatted = new List<object> { context }; // Add all arguments except for any marked as 'params' argsFormatted.AddRange(boxed.Take(_hasParamsArgument ? paramc - 1 : paramc)); // Insert params argument as an array (it needs to be represented as a single object) if (_hasParamsArgument) { argsFormatted.Add(args.Where((o, i) => i >= paramc - 1).ToArray()); } return new CallArgs(_method, argsFormatted); }
internal bool Run(Context context, params string[] args) { var currentContextType = context.GetType(); // Check context type compatability if ((!currentContextType.IsSubclassOf(_contextType) && currentContextType != _contextType) || !ContextFilter.Test(currentContextType, Filter)) { context.PostCommandNotFound(QualifiedName); return false; } int argc = args.Length; int paramc = _paramList.Length - 1; try { if ((_hasParamsArgument && argc < paramc) || (argc < paramc - _numOptionalParams) || (!_hasParamsArgument && argc > paramc)) { context.Notify("Parameter count mismatch."); return false; } var boxed = Enumerable.Repeat(Type.Missing, paramc).ToArray(); // Convert parameters to the proper types for (int i = 0; i < paramc; i++) { boxed[i] = Util.ChangeType(args[i], _paramList[(i >= paramc ? paramc - 1 : i) + 1].ParameterType); } var argsFormatted = new List<object> { context }; // Add all arguments except for any marked as 'params' argsFormatted.AddRange(boxed.Take(_hasParamsArgument ? paramc - 1 : paramc)); // Insert params argument as an array (it needs to be represented as a single object) if (_hasParamsArgument) { argsFormatted.Add(args.Where((o, i) => i >= paramc - 1).ToArray()); } // Call the method with our parameters _method.Invoke(null, argsFormatted.ToArray()); } catch(Exception ex) { if (SystemConvars.Throws) { throw; } context.Notify("Error: " + ex); return false; } return true; }
public static void Help(Context context, string command) { command = command.ToLower(); Command cmd; if (!Devcom.Commands.TryGetValue(Util.Qualify(context.Category, command), out cmd)) { context.Notify("Command not found: '" + command + "'"); return; } var sb = new StringBuilder(); sb.Append(cmd.QualifiedName).Append(": ").Append(cmd.Description).AppendLine(); sb.Append("Syntax: ").Append(cmd.QualifiedName).Append(" ").Append(cmd.ParamHelpString); context.Notify(sb.ToString()); }
public static void LoadConfig(Context context) { ConvarConfig.LoadConvars(); }
/// <summary> /// Translates a command string into a reusable Call object. /// </summary> /// <param name="context">The context to attach to the call.</param> /// <param name="command">The command string to translate.</param> /// <returns></returns> public static Call TranslateCommand(Context context, string command) { if (!_loaded) throw new InvalidOperationException("Cannot translate command calls without loading Devcom."); context = context ?? Context.Default; if (context == null) throw new ArgumentNullException("context"); if (String.IsNullOrEmpty(command)) { return Call.None; } // Cut off spaces from both ends command = command.Trim(); var calls = new List<CallArgs>(); foreach (var cmdstr in command.Split(new[] { '|', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim())) { // Split up the line into arguments var parts = cmdstr.ParseParams().ToArray(); if (!parts.Any()) continue; // The first index will be the command name var first = parts.First().ToLower(); // Check if it's a root marker if (first == "$") { context.Category = ""; continue; } // Make sure the command exists Command cmd; if (!Commands.TryGetValue(first, out cmd)) { throw new ArgumentException(String.Concat("Command not found: '", first, "'")); } if (parts.Length > 1) { for (int i = 1; i < parts.Length; i++) { parts[i] = Regex.Replace(parts[i], @":(?<name>\S+):", m => Util.GetConvarValue(m.Groups["name"].Value, context.Category), RegexOptions.ExplicitCapture); } } calls.Add(cmd.TranslateArgs(context, parts)); } return new Call(calls.ToArray()); }
public static void Toggle(Context context, string cvName) { Convar convar; if (!context.RequestConvar(cvName, out convar)) return; if (convar.Value.GetType() != typeof(bool)) { context.Notify("Convar '" + cvName + "' is not a boolean type."); return; } convar.Value = !((bool)convar.Value); context.NotifyFormat("{0} = {1}", convar.QualifiedName, convar.Value); }
public static void SetConVar(Context context, string cvName, string value) { Convar convar; if (!context.RequestConvar(cvName, out convar)) return; convar.Value = value; }
public static void SaveConfig(Context context) { ConvarConfig.SaveConvars(); }
public static void Root(Context context) { context.Category = ""; }
public static void ResetConvar(Context context, string cvName) { Convar convar; if (!context.RequestConvar(cvName, out convar)) return; convar.Value = convar.DefaultValue; }
/// <summary> /// Executes a command string under the specified context. /// </summary> /// <param name="context">The context under which to execute the command.</param> /// <param name="command">The command to execute.</param> public static void Submit(Context context, string command) { if (!_loaded) return; // Set the context to default if null was passed context = context ?? Context.Default; if (SystemConvars.EchoInput) { context.Notify("> " + command); } // Don't interpret empty commands if (String.IsNullOrEmpty(command)) return; // Cut off spaces from both ends command = command.Trim(); foreach (var cmdstr in command.Split(new[] { '|', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim())) { // Split up the line into arguments var parts = cmdstr.ParseParams().ToArray(); if (!parts.Any()) continue; // The first index will be the command name var first = parts.First().ToLower(); // Check if it's a root marker if (first == "$") { context.Category = ""; continue; } // Check for root marker at the start of command name bool root = false; if (first.StartsWith("$")) { root = true; first = first.Substring(1); } // Get the fully-qualified name, taking into account root markers and current category string qname = root ? first : (context.Category.Length > 0 ? context.Category + "." : "") + first; // Make sure the command exists Command cmd; if (!Commands.TryGetValue(qname, out cmd)) { context.PostCommandNotFound(qname); continue; } if (parts.Length > 1) { for (int i = 1; i < parts.Length; i++) { parts[i] = Regex.Replace(parts[i], @":(?<name>\S+):", m => Util.GetConvarValue(m.Groups["name"].Value, context.Category), RegexOptions.ExplicitCapture); } } // Run the command cmd.Run(context, parts.Where((s, i) => i > 0).ToArray()); } }
/// <summary> /// Executes a command string asynchronously under the specified context. /// </summary> /// <param name="context">The context under which to execute the command.</param> /// <param name="command">The command to execute.</param> public static async void SendCommandAsync(Context context, string command) { await Task.Run(() => Submit(context, command)); }
public static void Date(Context context) { context.Notify(DateTime.Now.ToString("R")); }
public static void Sub(Context context, double a, double b, string cvOut) { Convar o; if (!context.RequestConvar(cvOut, out o)) return; o.Value = a - b; }
public static void ListCommands(Context context) { var contextType = context.GetType(); context.Notify( Devcom.Commands.Where(cmd => ContextFilter.Test(contextType, cmd.Value.Filter) && (cmd.Value.ContextType == contextType || contextType.IsSubclassOf(cmd.Value.ContextType))) .Select(cmd => cmd.Key) .Aggregate((accum, name) => accum + "\n" + name)); }