コード例 #1
0
 public ValidCommands?GetCommand(string arg)
 {
     try
     {
         ValidCommands command = (ValidCommands)Enum.Parse(typeof(ValidCommands), arg, true);
         return(command);
     }
     catch
     {
         return(null);
     }
 }
コード例 #2
0
        /// <summary>
        /// The default ctor that parses the input string to extract the command and the optional value.
        /// </summary>
        /// <param name="parseableCommandString"></param>
        /// <exception cref="ApplicationException"></exception>
        /// <remarks>
        /// The ConsoleCommand has been written specifically for the Mako Runner application and makes use
        /// of the specialized syntax defined for the console commands. It is not worth the time or effort
        /// to make this reusable for other applications at this time.
        /// </remarks>
        public ConsoleCommand(string parseableCommandString)
        {
            try
            {
                // Make a List with the CSV list of command strings.
                ValidCommandList = ValidCommands.Split(',').ToList();

                // Cleanup the input.
                var cleanString = parseableCommandString.Trim();

                // Ensure that the first character of the command string is a "-".
                if (!cleanString.StartsWith("-"))
                {
                    throw new ApplicationException($"Invalid command line argument. All commands must start with \"-\".");
                }

                // Determine if the command value is present in the command string.
                var valueIndex = cleanString.IndexOf(":", StringComparison.Ordinal);
                if (valueIndex == -1)
                {
                    throw new ApplicationException($"Command value not found in command string: {cleanString}");
                }

                // Extract the command portion of the command line argument.
                Command = cleanString.Substring(1, valueIndex - 1);

                // Make sure the command is one that has been specified in the Settings.
                if (!ValidCommandList.Contains(Command))
                {
                    throw new ApplicationException($"Invalid command: {Command}");
                }

                // Extract the command value from the command string.
                if (valueIndex > 1 && cleanString.Length > valueIndex + 1)
                {
                    Value = cleanString.Substring(valueIndex + 1);
                }
                else
                {
                    throw new ApplicationException("Invalid command value. Company ID not found.");
                }
            }
            catch (Exception e)
            {
                // Throw an exception with a detailed message.
                throw new ApplicationException($"Error parsing command line argument, {parseableCommandString}. Original error: {e.Message}");
            }
        }
コード例 #3
0
        void cmdWhitelist(IPlayer player, string command, string[] args)
        {
            if (args.Length == 0) // If the user types /whitelist and nothing else.
            {
                player.Reply(lang.GetMessage("CommandList", this, player.Id));
                return;
            }
            else if (GetCommand(args[0]) == null) // If the user types /whitelist <arg> and that arg isn't in our ValidCommands enum.
            {
                player.Reply(lang.GetMessage("InvalidSyntax", this, player.Id));
                return;
            }

            ValidCommands Command = (ValidCommands)GetCommand(args[0]);

            if (args.Length == 1 && (Command == ValidCommands.grant || Command == ValidCommands.revoke || Command == ValidCommands.toggle))
            {
                player.Reply(lang.GetMessage("MissingArgument", this, player.Id));
                return;
            }

            ulong targetSteamID;

            ulong.TryParse(args.Length == 2 ? args[1] : "0", out targetSteamID);
            if ((Command == ValidCommands.grant || Command == ValidCommands.revoke) && targetSteamID < 70000000000000000)
            {
                player.Reply(lang.GetMessage("InvalidSteamID", this, player.Id)); // If we aren't toggling the whitelist system, we need to check the SteamID length.
                return;
            }

            switch (Command)
            {
            case ValidCommands.help:
                player.Reply(lang.GetMessage("CommandList", this, player.Id));
                break;

            case ValidCommands.grant:
                if (!permission.UserHasPermission(player.Id, permWhitelistAdd))
                {
                    player.Reply(lang.GetMessage("NoPerm", this, player.Id));
                    return;
                }

                permission.GrantUserPermission(args[1], permUserWhitelisted, this);
                player.Reply(lang.GetMessage("WhitelistGranted", this, player.Id));
                break;

            case ValidCommands.revoke:
                if (!permission.UserHasPermission(player.Id, permWhitelistRemove))
                {
                    player.Reply(lang.GetMessage("NoPerm", this, player.Id));
                    return;
                }
                else if (players.FindPlayerById(args[1]) != null)
                {
                    IPlayer foundPlayer = players.FindPlayerById(args[1]);
                    foundPlayer.Kick(lang.GetMessage("RevokedKick", this, foundPlayer.Id));
                }

                permission.RevokeUserPermission(args[1], permUserWhitelisted);
                player.Reply(lang.GetMessage("WhitelistRevoked", this, player.Id));
                break;

            case ValidCommands.toggle:
                if (!permission.UserHasPermission(player.Id, permWhitelistToggle))
                {
                    player.Reply(lang.GetMessage("NoPerm", this, player.Id));
                    return;
                }

                bool userInput;
                if (args.Length == 2 && Boolean.TryParse(args[1], out userInput))
                {
                    bWhitelistEnabled = userInput;
                    Config["Whitelist System Enabled (true/false)"] = userInput;
                    SaveConfig();
                    player.Reply(string.Format(lang.GetMessage("WhitelistToggle", this, player.Id), userInput == true ? "On" : "Off"));
                }
                else
                {
                    player.Reply(lang.GetMessage("InvalidSyntax", this, player.Id));
                    break;
                }
                break;

            default:
                player.Reply(lang.GetMessage("InvalidSyntax", this, player.Id));
                break;
            }
        }