Example #1
0
        /// <summary>
        /// Takes a string input and executes that as a command.
        /// </summary>
        public static void ExecuteCommand(string input)
        {
            consoleOutput?.LogCommand(input);

            if (string.IsNullOrWhiteSpace(input))
            {
                return;
            }

            var args = DevConsoleHelper.ParseCommand(input);
            var arg  = args[0].ToLower();

            if (registeredConVars.ContainsKey(arg))
            {
                if (args.Length == 1)
                {
                    LogMessage($"{arg}: {registeredConVars[arg].FieldInfo.GetValue(null)}");
                }
                else
                {
                    SetConVarValue(registeredConVars[arg].FieldInfo, args[1]);
                }
            }
            else if (registeredCommands.ContainsKey(arg))
            {
                registeredCommands[arg].Execute(args);
            }
            else
            {
                consoleOutput?.LogError($"Invalid Command: {args[0]}");
            }
        }
Example #2
0
        private static void SetConVarValue(FieldInfo conVar, string arg)
        {
            if (DevConsoleHelper.TryParseBool(conVar.FieldType, arg, out var boolVal))
            {
                conVar.SetValue(null, boolVal);
                LogMessage($"Set {conVar.Name} value to {boolVal}");
            }

            if (DevConsoleHelper.TryParseByte(conVar.FieldType, arg, out var byteVal))
            {
                conVar.SetValue(null, byteVal);
                LogMessage($"Set {conVar.Name} value to {byteVal}");
            }

            if (DevConsoleHelper.TryParseSByte(conVar.FieldType, arg, out var sbyteVal))
            {
                conVar.SetValue(null, sbyteVal);
                LogMessage($"Set {conVar.Name} value to {sbyteVal}");
            }

            if (DevConsoleHelper.TryParseSByte(conVar.FieldType, arg, out var charVal))
            {
                conVar.SetValue(null, charVal);
                LogMessage($"Set {conVar.Name} value to {charVal}");
            }

            if (DevConsoleHelper.TryParseDecimal(conVar.FieldType, arg, out var decimalVal))
            {
                conVar.SetValue(null, decimalVal);
                LogMessage($"Set {conVar.Name} value to {decimalVal}");
            }

            if (DevConsoleHelper.TryParseDecimal(conVar.FieldType, arg, out var doubleVal))
            {
                conVar.SetValue(null, doubleVal);
                LogMessage($"Set {conVar.Name} value to {doubleVal}");
            }

            if (DevConsoleHelper.TryParseFloat(conVar.FieldType, arg, out var floatVal))
            {
                conVar.SetValue(null, floatVal);
                LogMessage($"Set {conVar.Name} value to {floatVal}");
            }

            if (DevConsoleHelper.TryParseInt(conVar.FieldType, arg, out var intVal))
            {
                conVar.SetValue(null, intVal);
                LogMessage($"Set {conVar.Name} value to {intVal}");
            }

            if (DevConsoleHelper.TryParseUInt(conVar.FieldType, arg, out var uintVal))
            {
                conVar.SetValue(null, uintVal);
                LogMessage($"Set {conVar.Name} value to {uintVal}");
            }

            if (DevConsoleHelper.TryParseLong(conVar.FieldType, arg, out var longVal))
            {
                conVar.SetValue(null, longVal);
                LogMessage($"Set {conVar.Name} value to {longVal}");
            }

            if (DevConsoleHelper.TryParseULong(conVar.FieldType, arg, out var ulongVal))
            {
                conVar.SetValue(null, ulongVal);
                LogMessage($"Set {conVar.Name} value to {ulongVal}");
            }

            if (DevConsoleHelper.TryParseShort(conVar.FieldType, arg, out var shortVal))
            {
                conVar.SetValue(null, shortVal);
                LogMessage($"Set {conVar.Name} value to {shortVal}");
            }

            if (DevConsoleHelper.TryParseUshort(conVar.FieldType, arg, out var ushortVal))
            {
                conVar.SetValue(null, ushortVal);
                LogMessage($"Set {conVar.Name} value to {ushortVal}");
            }

            if (DevConsoleHelper.TryParseString(conVar.FieldType))
            {
                conVar.SetValue(null, arg);
                LogMessage($"Set {conVar.Name} value to {arg}");
            }
        }
Example #3
0
 public static void Initialize()
 {
     registeredConVars  = DevConsoleHelper.RegisterConVars();
     registeredCommands = DevConsoleHelper.RegisterCommands();
 }
        public void Execute(string[] args)
        {
            bool success = true;

            if (parameters == null || parameters.Length == 0)
            {
                methodInfo.Invoke(null, null);
                return;
            }

            object[] objectParams = new object[parameters.Length];

            int nextArg = 0;

            for (int i = 0; i < parameters.Length; i++)
            {
                // Increase next arg index here to make sure indexing order does not go out of sync.
                nextArg++;

                // If not enough args have been passed parse failed. Inform user.
                if (nextArg >= args.Length)
                {
                    success = InformFailedParse(i, FailReason.NotEnoughArgs);
                    break;
                }

                if (DevConsoleHelper.TryParseBool(parameters[i], args[nextArg], out var boolValue))
                {
                    objectParams[i] = boolValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseByte(parameters[i], args[nextArg], out var byteValue))
                {
                    objectParams[i] = byteValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseSByte(parameters[i], args[nextArg], out var sbyteValue))
                {
                    objectParams[i] = sbyteValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseChar(parameters[i], args[nextArg], out var charValue))
                {
                    objectParams[i] = charValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseDecimal(parameters[i], args[nextArg], out var decimalValue))
                {
                    objectParams[i] = decimalValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseFloat(parameters[i], args[nextArg], out var floatValue))
                {
                    objectParams[i] = floatValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseDouble(parameters[i], args[nextArg], out var doubleValue))
                {
                    objectParams[i] = doubleValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseInt(parameters[i], args[nextArg], out var intValue))
                {
                    objectParams[i] = intValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseUInt(parameters[i], args[nextArg], out var uintValue))
                {
                    objectParams[i] = uintValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseLong(parameters[i], args[nextArg], out var longValue))
                {
                    objectParams[i] = longValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseULong(parameters[i], args[nextArg], out var ulongValue))
                {
                    objectParams[i] = ulongValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseShort(parameters[i], args[nextArg], out var shortValue))
                {
                    objectParams[i] = shortValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseUshort(parameters[i], args[nextArg], out var ushortValue))
                {
                    objectParams[i] = ushortValue;
                    continue;
                }

                if (DevConsoleHelper.TryParseString(parameters[i]))
                {
                    objectParams[i] = args[nextArg];
                    continue;
                }

                // Failed to parse command.
                success = InformFailedParse(i, FailReason.ParseFailed);
            }

            if (success)
            {
                methodInfo.Invoke(null, objectParams);
            }
        }