Exemplo n.º 1
0
        public static List <ConObject> GetAllConObjectsThatMatch(string input)
        {
            List <ConObject> objects = new List <ConObject>();

            if (input.Trim() == "")
            {
                return(objects);
            }
            input = input.ToLower();

            foreach (var command in Commands)
            {
                string cleanName = command.GetName().Trim();

                if (cleanName.StartsWith(input) || SourceConsoleHelper.LevenshteinDistance(cleanName, input) < 2 && cleanName != input)
                {
                    objects.Add(command);
                }
            }

            foreach (var convar in Convars)
            {
                string cleanName = convar.GetName().Trim();

                if (cleanName.StartsWith(input) || SourceConsoleHelper.LevenshteinDistance(cleanName, input) < 2 && cleanName != input)
                {
                    objects.Add(convar);
                }
            }

            objects = objects.OrderBy(c => c.GetName()).ToList();

            return(objects);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Given a string, executes it normally as if it were a console-input
        /// </summary>
        /// <param name="input"></param>
        public static void ExecuteString(string input)
        {
            if (input == "")
            {
                return;
            }

            string[] parts       = input.Split(' '); //split the input string into parts
            string   commandName = parts[0];         //get just the command name

            var command = GetConObjectByName(commandName);

            if (command != null) //if the command exists
            {
                string[] cleanParts = SourceConsoleHelper.CleanArgumentsArray(parts, command.GetParametersLength());

                if (command is ConCommand)
                {
                    ExecuteCommand((ConCommand)command, SourceConsoleHelper.CastParameters(cleanParts));
                }
                else //if no command, then convar
                {
                    ExecuteConvar((ConVar)command, SourceConsoleHelper.CastParameters(cleanParts));
                }
            }
        }
Exemplo n.º 3
0
        public static int RefreshCommands(Assembly[] assemblies)
        {
            float startTime = Time.realtimeSinceStartup;

            CommandNamesCache.Clear();

            Commands = new List <ConCommand>();
            Convars  = new List <ConVar>();

            foreach (var command in SourceConsoleHelper.FindMethodAttributes <ConCommand>(assemblies))
            {
                string name = command.GetName();

                if (CommandNamesCache.Contains(name))
                {
                    error($"Failed to load command '{name}'! A command/convar already exists with that name!");
                    throw new DuplicateCommandException($"Failed to load command '{name}'! A command/convar already exists with that name!");
                }

                CommandNamesCache.Add(name);
                Commands.Add(command);
            }
            Commands = Commands.OrderBy(c => c.GetName()).ToList();

            foreach (var convar in SourceConsoleHelper.FindPropertyAttributes <ConVar>(assemblies))
            {
                string name = convar.GetName();

                if (CommandNamesCache.Contains(name))
                {
                    error($"Failed to load convar '{name}'! A command/convar already exists with that name!");
                    throw new DuplicateCommandException($"Failed to load convar '{name}'! A command/convar already exists with that name!");
                }

                if (convar.DefaultValue != null)
                {
                    try
                    {
                        if (convar.PropertyInfo != null)
                        {
                            convar.PropertyInfo.SetValue(null, convar.DefaultValue);
                        }
                        else
                        {
                            convar.FieldInfo.SetValue(null, convar.DefaultValue);
                        }
                    }
                    catch (Exception e)
                    {
                        if (ShowFullErrorStack)
                        {
                            error(e);
                        }
                        else
                        {
                            error($"{e.GetType().Name}: {e.Message}");
                        }
                    }
                }

                CommandNamesCache.Add(name);
                Convars.Add(convar);
            }
            Convars = Convars.OrderBy(c => c.GetName()).ToList();

            print("Refreshed Commands in " + (Time.realtimeSinceStartup - startTime).ToString("0.000") + " seconds");
            return(Commands.Count + Convars.Count);
        }