Пример #1
0
        public static object ExecuteCommand(ConCommand command, params object[] args)
        {
            //This code is for automatically putting in default parameter values if they exist and weren't specified explicitly by the user
            var parameters = command.MethodInfo.GetParameters();

            if (args == null || args.Length < parameters.Length)
            {
                object[] newArgs = new object[parameters.Length];

                for (int i = 0; i < newArgs.Length; i++)
                {
                    //if we have inputted args and current i is less than the length of original user-inputted args
                    if (args != null && i <= args.Length)
                    {
                        //Copy old args to new args array
                        newArgs[i] = args[i];
                    }
                    else //if args is null or we have passed the length of original user-inputted args
                    {
                        if (parameters[i].HasDefaultValue)
                        {
                            newArgs[i] = parameters[i].DefaultValue;
                        }
                    }
                }
                args = newArgs;
            }

            try
            {
                if (command.MethodInfo.ReturnType == typeof(void))
                {
                    command.MethodInfo.Invoke(null, args);
                }
                else
                {
                    return(command.MethodInfo.Invoke(null, args));
                }
            }catch (Exception e)
            {
                if (ShowFullErrorStack)
                {
                    error(e);
                }
                else
                {
                    error($"{e.GetType().Name}: {e.Message}");
                }
            }

            return(null);
        }
Пример #2
0
        public static ConObject GetConObjectByName(string name)
        {
            ConCommand command = GetConCommandByName(name);

            if (command != null)
            {
                return(command);
            }
            ConVar convar = GetConVarByName(name);

            if (convar != null)
            {
                return(convar);
            }

            return(null);
        }