private void ExecuteCommand(Command command)
        {
            OnCommandExecuting?.Invoke(this, new CommandEventArgs(command));
            switch (command.Action.ToLowerInvariant())
            {
            case "click":
                GetElement(command.Target).Click();
                break;

            case "open":
                if (!string.Equals(_driver.Url, command.Target))
                {
                    string target = null;
                    if (command.Target.StartsWith("http://") || command.Target.StartsWith("https://"))
                    {
                        target = command.Target;
                    }
                    else
                    {
                        var uri = new Uri(_driver.Url);
                        target = command.Target.StartsWith("/") ? $"{uri.Scheme}://{uri.Host}{target}" : $"{uri.Scheme}://{uri.Host}/{command.Target}";
                    }
                    _driver.Navigate().GoToUrl(target);
                }
                break;

            case "sendkeys":
            case "type":
                var element = GetElement(command.Target);
                if (string.IsNullOrEmpty(command.Value))
                {
                    element.Clear();
                }
                else
                {
                    element.SendKeys(command.Value);
                }
                break;

            case "setwindowsize":
                var dimensions = command.Target.Split('x');
                _driver.Manage().Window.Size = new System.Drawing.Size(int.Parse(dimensions[0]), int.Parse(dimensions[1]));
                break;

            case "waitforelementpresent":
                _ = GetElement(command.Target, string.IsNullOrWhiteSpace(command.Value) || !int.TryParse(command.Target, out var timeout) ? null : TimeSpan.FromMilliseconds(timeout) as TimeSpan?);
                break;
            }

            OnCommandExecuted?.Invoke(this, new CommandEventArgs(command));
        }
예제 #2
0
        public void HandleCommand(string message, ulong steamId, ref bool consumed, bool serverConsole = false)
        {
            if (message.Length < 1 || message[0] != Prefix)
            {
                return;
            }

            consumed = true;

            var player = Torch.CurrentSession.Managers.GetManager <IMultiplayerManagerBase>().GetPlayerBySteamId(steamId);

            if (player == null)
            {
                _log.Error($"Command {message} invoked by nonexistant player");
                return;
            }

            var cmdText = new string(message.Skip(1).ToArray());
            var command = Commands.GetCommand(cmdText, out string argText);

            if (command != null)
            {
                var cmdPath = string.Join(".", command.Path);

                var  defaultPermission = HasPermission(steamId, command);
                bool?cancel            = null;
                OnCommandExecuting?.Invoke(command, player, defaultPermission, ref cancel);

                if (cancel.HasValue)
                {
                    if (!cancel.Value)
                    {
                        return;
                    }
                }
                else
                {
                    if (!defaultPermission)
                    {
                        _log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission");
                        _chatManager.SendMessageAsOther(null, $"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", targetSteamId: steamId);
                        return;
                    }
                }

                var splitArgs = Regex.Matches(argText, "(\"[^\"]+\"|\\S+)").Cast <Match>().Select(x => x.ToString().Replace("\"", "")).ToList();
                _log.Trace($"Invoking {cmdPath} for player {player.DisplayName}");
                var context = new CommandContext(Torch, command.Plugin, steamId, argText, splitArgs);
                Torch.Invoke(() =>
                {
                    if (command.TryInvoke(context))
                    {
                        _log.Info($"Player {player.DisplayName} ran command '{message}'");
                    }
                    else
                    {
                        context.Respond($"Invalid Syntax: {command.SyntaxHelp}");
                    }
                });
            }
        }