Class that represents a command alias,
Exemple #1
0
        public static Configuration NewSampleConfiguration()
        {
            Configuration newConfig = new Configuration();

            newConfig.CommandAliases.Add(AliasCommand.Create("testparms", "", "0c", "", 0, "/bc Input param 1 2 3: $1 $2 $3", "/bc Input param 1-3: $1-3", "/bc Input param 2 to end of line: $2-"));
            newConfig.CommandAliases.Add(AliasCommand.Create("testrandom", "", "0c", "", 0, "/bc Random Number: $random(1,100)"));
            newConfig.CommandAliases.Add(AliasCommand.Create("impersonate", "", "0c", "", 0, "$runas($1,/me can fit $random(1,100) cocks in their mouth at once.)"));

            return(newConfig);
        }
        public static AliasCommand Create(string CommandAlias, string Permissions, string Cost, string HelpText, int CooldownSeconds, params string[] CommandsToRun) {
            AliasCommand alias = new AliasCommand();

            alias.CommandAlias = CommandAlias;
            alias.Permissions = Permissions;
            alias.UsageHelpText = HelpText;
            alias.Cost = Cost;
            alias.CooldownSeconds = CooldownSeconds;

            alias.CommandsToExecute.AddRange(CommandsToRun);

            return alias;
        }
        public static AliasCommand Create(string CommandAlias, string Permissions, string Cost, string HelpText, int CooldownSeconds, params string[] CommandsToRun)
        {
            AliasCommand alias = new AliasCommand();

            alias.CommandAlias    = CommandAlias;
            alias.Permissions     = Permissions;
            alias.UsageHelpText   = HelpText;
            alias.Cost            = Cost;
            alias.CooldownSeconds = CooldownSeconds;

            alias.CommandsToExecute.AddRange(CommandsToRun);

            return(alias);
        }
        /// <summary>
        /// Executes the AliasCommand.  Will either forward the command to the tshock handler or do something else
        /// </summary>
        void DoCommands(AliasCommand alias, TShockAPI.TSPlayer player, List <string> parameters)
        {
            //loop through each alias and do the commands.
            foreach (string commandToExecute in alias.CommandsToExecute)
            {
                //todo: parse paramaters and dynamics
                string mangledString = commandToExecute;

                //specifies whether the command to run should be executed as a command, or ignored.
                //useful for functions like $msg that does other shit
                bool executeCommand = true;

                //replace parameter markers with actual parameter values
                ReplaceParameterMarkers(parameters, ref mangledString);

                mangledString = mangledString.Replace("$calleraccount", player.UserAccountName);
                mangledString = mangledString.Replace("$callername", player.Name);

                //$random(x,y) support.  Returns a random number between x and y
                if (randomRegex.IsMatch(mangledString))
                {
                    foreach (Match match in randomRegex.Matches(mangledString))
                    {
                        int randomFrom = 0;
                        int randomTo   = 0;

                        if (!string.IsNullOrEmpty(match.Groups[2].Value) && int.TryParse(match.Groups[2].Value, out randomTo) &&
                            !string.IsNullOrEmpty(match.Groups[1].Value) && int.TryParse(match.Groups[1].Value, out randomFrom))
                        {
                            // this is a critical section
                            // Random class is seeded from the system clock if you construct one without a seed.
                            // therefore, calls to Next() at exactly the same point in time is likely to produce the same number.
                            lock (__rndLock) {
                                mangledString = mangledString.Replace(match.ToString(), randomGenerator.Next(randomFrom, randomTo).ToString());
                            }
                        }
                        else
                        {
                            TShockAPI.Log.ConsoleError(match.ToString() + " has some stupid shit in it, have a look at your AliasCmd config file.");
                            mangledString = mangledString.Replace(match.ToString(), "");
                        }
                    }
                }

                // $runas(u,cmd) support.  Run command as user
                if (runasFunctionRegex.IsMatch(mangledString))
                {
                    foreach (Match match in runasFunctionRegex.Matches(mangledString))
                    {
                        string impersonatedName = match.Groups[2].Value;
                        Economy.EconomyPlayer impersonatedPlayer = SEconomyPlugin.GetEconomyPlayerSafe(impersonatedName);

                        if (impersonatedPlayer != null)
                        {
                            string commandToRun = match.Groups[3].Value;
                            player = impersonatedPlayer.TSPlayer;

                            mangledString = commandToRun.Trim();
                        }
                    }
                }

                // $msg(u,msg) support.  Sends the user a non-chat informational message
                if (msgRegex.IsMatch(mangledString))
                {
                    foreach (Match match in msgRegex.Matches(mangledString))
                    {
                        string msgTarget = match.Groups[2].Value.Trim();
                        string message   = match.Groups[3].Value.Trim();
                        Economy.EconomyPlayer destinationPlayer = SEconomyPlugin.GetEconomyPlayerSafe(msgTarget);

                        if (destinationPlayer != null)
                        {
                            //custom command, skip forwarding of the command to the tshock executer
                            executeCommand = false;

                            destinationPlayer.TSPlayer.SendInfoMessage(message);
                        }
                    }
                }


                //and send the command to tshock to do.
                try {
                    //prevent an infinite loop for a subcommand calling the alias again causing a commandloop
                    string command = mangledString.Split(' ')[0].Substring(1);
                    if (!command.Equals(alias.CommandAlias, StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (executeCommand)
                        {
                            HandleCommandWithoutPermissions(player, mangledString);
                        }
                    }
                    else
                    {
                        TShockAPI.Log.ConsoleError(string.Format("cmdalias {0}: calling yourself in an alias will cause an infinite loop. Ignoring.", alias.CommandAlias));
                    }
                } catch {
                    //execute the command disregarding permissions
                    player.SendErrorMessage(alias.UsageHelpText);
                }
            }
        }
        /// <summary>
        /// Executes the AliasCommand.  Will either forward the command to the tshock handler or do something else
        /// </summary>
        void DoCommands(AliasCommand alias, TShockAPI.TSPlayer player, List<string> parameters) {
            
            //loop through each alias and do the commands.
            foreach (string commandToExecute in alias.CommandsToExecute) {
                //todo: parse paramaters and dynamics
                string mangledString = commandToExecute;

                //specifies whether the command to run should be executed as a command, or ignored.
                //useful for functions like $msg that does other shit
                bool executeCommand = true;

                //replace parameter markers with actual parameter values
                ReplaceParameterMarkers(parameters, ref mangledString);

                mangledString = mangledString.Replace("$calleraccount", player.UserAccountName);
                mangledString = mangledString.Replace("$callername", player.Name);

                //$random(x,y) support.  Returns a random number between x and y
                if (randomRegex.IsMatch(mangledString)) {
                    foreach (Match match in randomRegex.Matches(mangledString)) {
                        int randomFrom = 0;
                        int randomTo = 0;

                        if (!string.IsNullOrEmpty(match.Groups[2].Value) && int.TryParse(match.Groups[2].Value, out randomTo)
                            && !string.IsNullOrEmpty(match.Groups[1].Value) && int.TryParse(match.Groups[1].Value, out randomFrom)) {

                            // this is a critical section
                            // Random class is seeded from the system clock if you construct one without a seed.
                            // therefore, calls to Next() at exactly the same point in time is likely to produce the same number.
                                lock (__rndLock) {
                                    mangledString = mangledString.Replace(match.ToString(), randomGenerator.Next(randomFrom, randomTo).ToString());
                                }
                        } else {
                            TShockAPI.Log.ConsoleError(match.ToString() + " has some stupid shit in it, have a look at your AliasCmd config file.");
                            mangledString = mangledString.Replace(match.ToString(), "");
                        }
                    }
                }

                // $runas(u,cmd) support.  Run command as user
                if (runasFunctionRegex.IsMatch(mangledString)) {

                    foreach (Match match in runasFunctionRegex.Matches(mangledString)) {
                        string impersonatedName = match.Groups[2].Value;
                        Economy.EconomyPlayer impersonatedPlayer = SEconomyPlugin.GetEconomyPlayerSafe(impersonatedName);

                        if (impersonatedPlayer != null) {
                            string commandToRun = match.Groups[3].Value;
                            player = impersonatedPlayer.TSPlayer;

                            mangledString = commandToRun.Trim();
                        }
                    }
                }

                // $msg(u,msg) support.  Sends the user a non-chat informational message
                if (msgRegex.IsMatch(mangledString)) {

                    foreach (Match match in msgRegex.Matches(mangledString)) {
                        string msgTarget = match.Groups[2].Value.Trim();
                        string message = match.Groups[3].Value.Trim();
                        Economy.EconomyPlayer destinationPlayer = SEconomyPlugin.GetEconomyPlayerSafe(msgTarget);

                        if (destinationPlayer != null) {
                            //custom command, skip forwarding of the command to the tshock executer
                            executeCommand = false;

                            destinationPlayer.TSPlayer.SendInfoMessage(message);
                        }
                    }
                }


                //and send the command to tshock to do.
                try {
                    //prevent an infinite loop for a subcommand calling the alias again causing a commandloop
                    string command = mangledString.Split(' ')[0].Substring(1);
                    if (!command.Equals(alias.CommandAlias, StringComparison.CurrentCultureIgnoreCase)) {
                        if (executeCommand) {
                            HandleCommandWithoutPermissions(player, mangledString);
                        }
                    } else {
                        TShockAPI.Log.ConsoleError(string.Format("cmdalias {0}: calling yourself in an alias will cause an infinite loop. Ignoring.", alias.CommandAlias));
                    }
                } catch {
                    //execute the command disregarding permissions
                    player.SendErrorMessage(alias.UsageHelpText);
                }
            }
        }