/// <summary> /// Executes the command. /// </summary> /// <param name="arguments"> Passes the arguments.</param> public override void ExecuteCommand(string arguments) { try { object[] objs = ParseArguments(arguments); if (objs.Length > 0) { ParameterInfo[] parameterInfo = Method.GetParameters(); for (int i = 0; i < objs.Length; i++) { if (objs[i] == null) { // Add default objs[i] = parameterInfo[i].DefaultValue; } } Method.Invoke(null, objs); } else { Method.Invoke(null, null); } } catch (Exception e) { DevConsole.LogError("An execute error as occured, this could be the method raising an error (or causing an error). We could not locate the specific error however."); UnityDebugger.Debugger.LogError("DevConsole", e.ToString()); } }
/// <summary> /// Execute this command. /// </summary> /// <param name="arguments"> The arguments to pass in. </param> public override void ExecuteCommand(string arguments) { try { FunctionsManager.DevConsole.CallWithError(FunctionName, ParseArguments(arguments)); } catch (Exception e) { DevConsole.LogError(e.Message); } }
/// <summary> /// Executes the command. /// </summary> /// <param name="arguments"> Passes the arguments.</param> public void ExecuteCommand(string arguments) { try { Method(ParseArguments(arguments)); } catch (Exception e) { DevConsole.LogError(Errors.UnknownError(this)); UnityDebugger.Debugger.LogError("DevConsole", e.ToString()); } }
/// <summary> /// Executes the command. /// </summary> /// <param name="arguments"> Passes the arguments.</param> public void ExecuteCommand(string arguments) { try { Method.Method.Invoke(Method.Target, ParseArguments(arguments)); } catch (Exception e) { // Debug Error DevConsole.LogError(Errors.ExecuteConsoleError.Description(this)); Debug.ULogErrorChannel("DevConsole", e.ToString()); } }
public static void SetFontSize(int size) { if (size < 10) { DevConsole.LogError("Font size would be too small"); } else if (size > 20) { DevConsole.LogError("Font size would be too big"); } else { DevConsole.TextObject().fontSize = size; DevConsole.Log("Change successful :D", "green"); } }
protected override object[] ParseArguments(string args) { try { if (args.Length > 0) { return(new object[] { GetValueType <T0>(args) }); } else { DevConsole.LogError(Errors.ParameterMissingConsoleError.Description(this)); throw new Exception("Command Missing Parameter"); } } catch (Exception e) { Debug.ULogErrorChannel("DevConsole", e.ToString()); return(new object[] { }); } }
public static void SetFontSize(int size = 14) { if (size < 10) { DevConsole.LogError("Font size would be too small"); } else if (size > 20) { DevConsole.LogError("Font size would be too big"); } else { Text textObj = DevConsole.TextObject(); if (textObj != null) { textObj.fontSize = size; } DevConsole.Log("Change successful :D", "green"); } }
protected override object[] ParseArguments(string message) { try { string[] args = RegexToStandardPattern(message); if (args.Length == 3) { return(new object[] { GetValueType <T0>(args[0]), GetValueType <T1>(args[1]) }); } else { DevConsole.LogError(Errors.ParameterMissingConsoleError.Description(this)); throw new Exception("Command Missing Parameter"); } } catch (Exception e) { Debug.ULogErrorChannel("DevConsole", e.ToString()); return(new object[] { }); } }
/// <summary> /// Parse the arguments. /// </summary> /// <param name="arguments"> Arguments to parse.</param> /// <returns> The parsed arguments.</returns> protected virtual object[] ParseArguments(string args) { object[] convertedArgs = new object[] { }; try { string[] arguments = RegexToStandardPattern(args); convertedArgs = new object[TypeInfo.Length]; // If TypeInfo null then no new parameters to pass (we'll we will pass an array of strings, which could be empty) if (TypeInfo == null || TypeInfo.Length == 0) { return(arguments); } for (int i = 0; i < TypeInfo.Length; i++) { if (arguments.Length > i && arguments[i] != null) { convertedArgs[i] = DevConsole.Parsers[TypeInfo[i]](arguments[i]); if (convertedArgs[i] == null) { string errorMessage = "The entered parameters do not conform to the types (in order): " + this.Parameters; DevConsole.LogError(errorMessage); throw new Exception(errorMessage); } } else { convertedArgs[i] = null; } } } catch (Exception e) { UnityDebugger.Debugger.LogError("DevConsole", e.ToString()); } return(convertedArgs); }
/// <summary> /// Get the value type of the argument. /// </summary> /// <typeparam name="T"> The type of the argument. </typeparam> /// <param name="arg"> The argument to find the value type. </param> /// <returns> The type of the argument given. </returns> /// <exception cref="Exception"> Throws exception if arg is not type T, SHOULD BE CAUGHT by command. </exception> protected T GetValueType <T>(string arg, Type typeVariable = null) { Type typeOfT; if (typeVariable != null) { typeOfT = typeVariable; } else { typeOfT = typeof(T); } try { T returnValue; if (typeof(bool) == typeOfT) { // I'm wanting a boolean bool result; if (ValueToBool(arg, out result)) { returnValue = (T)(object)result; } else { throw new Exception("The entered value is not a valid " + typeOfT + " value"); } } else if (typeOfT == typeof(string)) { return((T)(object)arg.Trim('"')); } else if (arg.Contains('[')) { arg = arg.Trim().Trim('[', ']'); string pattern = @"\,?((?:\"".*?\"")|(?:[^\,]*))\,?"; string[] args = Regex.Matches(arg, pattern) .Cast <Match>() .Where(m => m.Groups.Count >= 2 && m.Groups[1].Value != string.Empty) .Select(m => m.Groups[1].Value.Trim().Trim('"')) .ToArray(); // This is a list because then we can go parameters.Count to get the current 'non nil' parameters List <object> parameters = new List <object>(); ConstructorInfo[] possibleConstructors = typeOfT.GetConstructors().Where(x => x.GetParameters().Length == args.Length).ToArray(); ConstructorInfo chosenConstructor = null; for (int i = 0; i < possibleConstructors.Length; i++) { parameters = new List <object>(); ParameterInfo[] possibleParameters = possibleConstructors[i].GetParameters(); for (int j = 0; j < possibleParameters.Length; j++) { try { if (possibleParameters[j].ParameterType == typeof(string)) { parameters.Add(args[j]); } else { parameters.Add(Convert.ChangeType(args[j], possibleParameters[j].ParameterType)); } } catch { break; } } if (parameters.Count == possibleParameters.Length) { // We have all our parameters chosenConstructor = possibleConstructors[i]; break; } } if (chosenConstructor == null) { throw new Exception("The entered value is not a valid " + typeOfT + " value"); } else { returnValue = (T)chosenConstructor.Invoke(parameters.ToArray()); } } else { returnValue = (T)Convert.ChangeType(arg, typeOfT); } return(returnValue); } catch (Exception e) { DevConsole.LogError(Errors.ParametersNotInFormat(this)); throw e; } }
/// <summary> /// Standard constructor. /// </summary> /// <param name="title"> The title of the command. </param> /// <param name="functionName"> The function name of the command (can be C# or LUA). </param> /// <param name="descriptiveText"> The text that describes this command. </param> /// <param name="helpFunctionName"> The help function name of the command (can be C# or LUA). </param> /// <param name="parameters"> The parameters ( should be invokeable format of (using [any character];)? type variableName, ... ). </param> public InvokeCommand(string title, string functionName, string descriptiveText, string helpFunctionName, string parameters) { Title = title; FunctionName = functionName; DescriptiveText = descriptiveText; HelpFunctionName = helpFunctionName; HelpMethod = delegate { if (HelpFunctionName == string.Empty) { DevConsole.Log("<color=yellow>Command Info:</color> " + ((DescriptiveText == string.Empty) ? " < color=red>There's no help for this command</color>" : DescriptiveText)); return; } try { FunctionsManager.DevConsole.CallWithError(HelpFunctionName); } catch (Exception e) { DevConsole.LogError(e.Message); } }; // If the parameters contains a ';' then it'll exclude the 'using' statement. // Just makes the declaration help look nicer. if (parameters.Contains(';')) { int indexOfSemiColon = parameters.IndexOf(';') + 1; if (parameters.Length > indexOfSemiColon) { Parameters = parameters.Substring(indexOfSemiColon).Trim(); } else { // Something weird happened here so we are just setting it to '' // This will only happen if the semi colon is the last element in the string Parameters = string.Empty; UnityDebugger.Debugger.LogWarning("DevConsole", "Parameters had a semicolon as a last character this is an illegal string."); } } else { // No ';' exists so free to just copy it over // We can't just do a substring cause it'll return an error and this isn't safely done Parameters = parameters; } // Parse the parameters // We are using regex since we only want a very specific part of the parameters // This is relatively fast, (actually quite fast compared to other methods and is in start) // Something like text: String, value: Int, on: Bool // Old Regex: \s*(.*?\;)?.*?\:(.*?)\s*(?:\,|$) // Koosemoose's Regex: /using\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/ // My Adjustments to Koosemoose's Regex (optimises for groups not needed): (using\s+[^\s]+)?\s*([^\s]+)\s+[^\s]+ // Note: Regex would be faster than using a for loop, cause it would require a lot of splits, and other heavily costing operations. string regexExpression = @"(using\s+[^\s]+)?\s*([^\s]+)\s+[^\s]+"; // This will just get the types string[] parameterTypes = Regex.Matches(parameters, regexExpression) .Cast <Match>() .Where(m => m.Groups.Count >= 2 && m.Groups[2].Value != string.Empty) .Select(m => (m.Groups[1].Value != string.Empty ? m.Groups[1].Value.Trim() : "using System;") + m.Groups[2].Value.Trim()) .ToArray(); Type[] types = new Type[parameterTypes.Length]; // Now we just cycle through and get types for (int i = 0; i < parameterTypes.Length; i++) { if (parameterTypes[i] == string.Empty) { types[i] = typeof(object); } else { // This is just to have a safety, that may trigger in some cases?? Better than nothing I guess // Could try to remove, but in most cases won't be part of DevConsole, till you open, or it starts. try { // We just split, since its a decently appropriate solution. string[] parameterSections = parameterTypes[i].Split(';'); types[i] = GetFriendlyType(parameterSections[1], parameterSections[0]); } catch (Exception e) { // This means invalid type, so we set it to object. // This in most cases is fine, just means that when you call it, // it won't work (unless the type is object) types[i] = typeof(object); UnityDebugger.Debugger.LogError("DevConsole", e.Message); } } } // Assign array Types = types; }