static void InternalExecuteCommand(string command) { // Supress timestamps when in command execution context SupressTimestamps = true; typingCommand.Clear(); Command rcmd; // Get the actual command with its arguments string cmd; List <string> args = ParseCommand(command, out cmd); // Find the command. if (commands.TryGetValue(cmd, out rcmd)) { if (args.Count >= 1 && (args[0].ToLower() == "--syntax" || args[0].ToLower() == "--?" || args[0].ToLower() == "/?")) { ShowSyntax(rcmd); } else { try { rcmd.callback(args.ToArray()); WriteInputLine(); } catch (Exception e) { WriteError(e.ToString()); } } } else { if (args.Count > 0) { if (TryModVar(cmd, args[0])) { WriteStandard("{0} -> {1}", cmd, args[0]); } else { WriteError("Failed to change {0} to {1}. (Wrong datatype?)", cmd, args[0]); } } else if (args.Count == 0 && CVars.ContainsKey(cmd)) { WriteStandard("{0} = {1}", cmd, CVars[cmd].value); } else { WriteError("Command not defined: {0}", cmd); } } SupressTimestamps = false; }
/// <summary> /// Removes a CVar. /// </summary> /// <param name="name">The name of the CVar</param> public static void RemoveCVar(string name) { if (name == null) { throw new ArgumentNullException("name"); } if (CVars.ContainsKey(name)) { CVars.Remove(name); } else { throw new Exception(String.Format("Failed to remove CVar \"{0}\", it does not exist!", name)); } }
/// <summary> /// Sets or Adds a CVar. /// </summary> /// <typeparam name="T">The datatype of the cvar.</typeparam> /// <param name="name">The cvar's name.</param> /// <param name="value">The cvar's value.</param> public static void SetCVar <T>(string name, T value) { if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } if (CVars.ContainsKey(name)) { CVars[name] = new CVar(CVars[name].dtype, value); } else { CVars.Add(name, new CVar(typeof(T), value)); } }
/// <summary> /// Adds a CVar. /// </summary> /// <typeparam name="T">The datatype of the cvar.</typeparam> /// <param name="name">The cvar's name.</param> /// <param name="value">The cvar's value.</param> public static void AddCVar <T>(string name, T value) { if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } if (CVars.ContainsKey(name)) { throw new Exception(String.Format("CVar \"{0}\" already exists!", name)); } else { CVars.Add(name, new CVar(typeof(T), value)); } }
static bool TryModVar(string cvar, object val) { if (CVars.ContainsKey(cvar)) { try { SetCVar(cvar, val); return(true); } catch (Exception) { //LogError(e); return(false); } } else { return(false); } }
/// <summary> /// Sets a CVar (infers datatype, slower than SetCVar<T>). /// </summary> /// <param name="name">The cvar's name.</param> /// <param name="value">The cvar's value.</param> static void SetCVar(string name, object value) { if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } if (CVars.ContainsKey(name)) { CVars[name] = new CVar(CVars[name].dtype, Convert.ChangeType(value, CVars[name].dtype)); } else { throw new Exception( String.Format("CVar \"{0}\" doesnt exist, and cannot be added! (To add, use AddCVar Or SetCVar<T>)", name)); } }
/// <summary> /// Gets whether or not the specified CVar exists. /// </summary> /// <param name="name">The name of the CVar.</param> public static bool IsCVarDefined(string name) { return(CVars.ContainsKey(name)); }