protected override IEnumerator RespondToCommandInternal(string inputCommand)
    {
        if (ProcessMethod == null)
        {
            Debug.LogError("A declared TwitchPlays CoroutineModComponentSolver process method is <null>, yet a component solver has been created; command invokation will not continue.");
            yield break;
        }

        IEnumerator responseCoroutine = null;

        try
        {
            responseCoroutine = (IEnumerator)ProcessMethod.Invoke(CommandComponent, new object[] { inputCommand });
            if (responseCoroutine == null)
            {
                yield break;
            }
        }
        catch (Exception ex)
        {
            Debug.LogErrorFormat("An exception occurred while trying to invoke {0}.{1}; the command invokation will not continue.", ProcessMethod.DeclaringType.FullName, ProcessMethod.Name);
            Debug.LogException(ex);
            yield break;
        }

        //Previous changelists mentioned that people using the TPAPI were not following strict rules about how coroutine implementations should be done, w.r.t. required yield returning first before doing things.
        //From the TPAPI side of things, this was *never* an explicit requirement. This yield return is here to explicitly follow the internal design for how component solvers are structured, so that external
        //code would never be executed until absolutely necessary.
        //There is the side-effect though that invalid commands sent to the module will appear as if they were 'correctly' processed, by executing the focus.
        //I'd rather have interactions that are not broken by timing mismatches, even if the tradeoff is that it looks like it accepted invalid commands.
        yield return("modcoroutine");

        while (true)
        {
            try
            {
                if (!responseCoroutine.MoveNext())
                {
                    yield break;
                }
            }
            catch (Exception ex)
            {
                Debug.LogErrorFormat(
                    "An exception occurred while trying to invoke {0}.{1}; the command invokation will not continue.",
                    ProcessMethod.DeclaringType.FullName, ProcessMethod.Name);
                Debug.LogException(ex);
                yield break;
            }

            object currentObject = responseCoroutine.Current;
            if (currentObject is KMSelectable)
            {
                KMSelectable selectable = (KMSelectable)currentObject;
                if (HeldSelectables.Contains(selectable))
                {
                    DoInteractionEnd(selectable);
                    HeldSelectables.Remove(selectable);
                }
                else
                {
                    DoInteractionStart(selectable);
                    HeldSelectables.Add(selectable);
                }
            }
            if (currentObject is KMSelectable[])
            {
                KMSelectable[] selectables = (KMSelectable[])currentObject;
                foreach (KMSelectable selectable in selectables)
                {
                    DoInteractionClick(selectable);
                    yield return(new WaitForSeconds(0.1f));
                }
            }
            if (currentObject is string)
            {
                string str = (string)currentObject;
                if (str.Equals("cancelled", StringComparison.InvariantCultureIgnoreCase))
                {
                    Canceller.ResetCancel();
                    TryCancel = false;
                }
            }
            yield return(currentObject);

            if (Canceller.ShouldCancel)
            {
                TryCancel = true;
            }
        }
    }
Exemple #2
0
    public IEnumerator RespondToCommand(string userNickName, string cmdStr, bool isWhisper, IEnumerator processCommand = null)
    {
        if (HandlerMethod == null && processCommand == null)
        {
            IRCConnection.SendMessage("Sorry @{0}, this holdable is not supported by Twitch Plays.", userNickName, !isWhisper, userNickName);
            yield break;
        }

        _disableOnStrike     = false;
        Strike               = false;
        StrikeCount          = 0;
        _currentUserNickName = userNickName;

        FloatingHoldable.HoldStateEnum holdState = Holdable.HoldState;

        if (holdState != FloatingHoldable.HoldStateEnum.Held)
        {
            if (TwitchGame.BombActive)
            {
                BombCommands.Drop(TwitchGame.Instance.Bombs[TwitchGame.Instance._currentBomb == -1 ? 0 : TwitchGame.Instance._currentBomb]);
            }
            IEnumerator holdCoroutine = Hold();
            while (holdCoroutine.MoveNext() && !Strike)
            {
                yield return(holdCoroutine.Current);
            }
        }

        DebugHelper.Log("Running RespondToCommandInternal()");
        if (HandlerMethod != null && processCommand == null)
        {
            processCommand = MakeCoroutine(HandlerMethod.Invoke(CommandComponent, new object[] { cmdStr }));
        }

        bool cancelled  = false;
        bool parseError = false;
        bool cancelling = false;

        if (processCommand == null || !processCommand.MoveNext())
        {
            if (!Strike)
            {
                SendToChat(null, userNickName, isWhisper, ref parseError);
            }
        }
        else
        {
            ProcessIEnumerators.Push(processCommand);
            ProcessIEnumerators.Push(FirstItem(processCommand.Current));
        }

        do
        {
            try
            {
                bool result = false;
                while (!result && !Strike)
                {
                    if (ProcessIEnumerators.Count > 0)
                    {
                        processCommand = ProcessIEnumerators.Pop();
                        result         = processCommand.MoveNext();
                        if (result)
                        {
                            ProcessIEnumerators.Push(processCommand);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (Strike)
                {
                    DebugHelper.Log("A strike was caused by the command. Invocation will not continue.");
                }
                if (!result || Strike)
                {
                    break;
                }
            }
            catch (Exception ex)
            {
                DebugHelper.LogException(ex, "Error processing command due to an exception. Invocation will not continue.:");
                break;
            }

            switch (processCommand.Current)
            {
            case IEnumerator iEnumerator:
                if (iEnumerator != null)
                {
                    ProcessIEnumerators.Push(iEnumerator);
                }
                continue;

            case KMSelectable kmSelectable when kmSelectable != null:
                if (HeldSelectables.Contains(kmSelectable))
                {
                    DoInteractionEnd(kmSelectable);
                    HeldSelectables.Remove(kmSelectable);
                }
                else
                {
                    DoInteractionStart(kmSelectable);
                    HeldSelectables.Add(kmSelectable);
                }
                break;

            case KMSelectable[] kmSelectables:
                foreach (KMSelectable selectable in kmSelectables)
                {
                    if (selectable != null)
                    {
                        yield return(DoInteractionClick(selectable));

                        if (Strike)
                        {
                            break;
                        }
                    }
                    else
                    {
                        yield return(new WaitForSeconds(0.1f));
                    }
                }

                break;

            case Quaternion quaternion:
                RotateByLocalQuaternion(quaternion);
                break;

            case string currentString when !string.IsNullOrEmpty(currentString):
                if (currentString.Equals("trycancel", StringComparison.InvariantCultureIgnoreCase) &&
                    CoroutineCanceller.ShouldCancel)
                {
                    CoroutineCanceller.ResetCancel();
                    cancelled = true;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("elevator music", "hold music", "waiting music"))
                {
                    if (_musicPlayer == null)
                    {
                        _musicPlayer = MusicPlayer.StartRandomMusic();
                    }
                }
                else if (currentString.EqualsIgnoreCase("cancelled") && cancelling)
                {
                    CancelBool?.SetValue(CommandComponent, false);
                    CoroutineCanceller.ResetCancel();
                    cancelled = true;
                }
                else if (currentString.StartsWith("strikemessage ",
                                                  StringComparison.InvariantCultureIgnoreCase) &&
                         !string.IsNullOrEmpty(currentString.Substring(14).Trim()))
                {
                    StrikeMessage = currentString.Substring(14);
                }
                else if (currentString.Equals("strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedStrikeUserNickName = _currentUserNickName;
                }
                else if (currentString.Equals("multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    _disableOnStrike = true;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("detonate", "explode") && TwitchGame.BombActive)
                {
                    var bomb = TwitchGame.Instance.Bombs[0];
                    AwardStrikes(_currentUserNickName, bomb.StrikeLimit - bomb.StrikeCount);
                    bomb.CauseExplosionByModuleCommand(string.Empty,
                                                       ID);
                    Strike = true;
                }
                else if (currentString.EqualsIgnoreCase("show front"))
                {
                    ProcessIEnumerators.Push(Hold());
                }
                else if (currentString.EqualsIgnoreCase("show back"))
                {
                    ProcessIEnumerators.Push(Hold(false));
                }
                else
                {
                    SendToChat(currentString, userNickName, isWhisper, ref parseError);
                }

                break;

            case string[] currentStrings:
                if (currentStrings.Length >= 1)
                {
                    if (currentStrings[0].ToLowerInvariant().EqualsAny("detonate", "explode") &&
                        TwitchGame.BombActive)
                    {
                        TwitchBomb bombs = TwitchGame.Instance.Bombs[0];
                        AwardStrikes(_currentUserNickName, bombs.StrikeLimit - bombs.StrikeCount);
                        switch (currentStrings.Length)
                        {
                        case 2:
                            bombs.CauseExplosionByModuleCommand(currentStrings[1], ID);
                            break;

                        case 3:
                            bombs.CauseExplosionByModuleCommand(currentStrings[1], currentStrings[2]);
                            break;

                        default:
                            bombs.CauseExplosionByModuleCommand(string.Empty, ID);
                            break;
                        }
                    }
                }

                break;

            case Dictionary <string, bool> permissions:
                foreach (KeyValuePair <string, bool> pair in permissions)
                {
                    if (TwitchPlaySettings.data.ModPermissions.ContainsKey(pair.Key))
                    {
                        continue;
                    }
                    TwitchPlaySettings.data.ModPermissions.Add(pair.Key, pair.Value);
                    TwitchPlaySettings.WriteDataToFile();
                }
                break;

            case KMMission mission:
                TwitchPlaysService.Instance.RunMission(mission);
                break;

            case object[] objects:
                if (objects == null)
                {
                    break;
                }
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (objects.Length)
                {
                case 3 when objects[0] is string objstr:
                    if (IsAskingPermission(objstr, userNickName, out bool permissionGranted))
                    {
                        if (permissionGranted)
                        {
                            switch (objects[1])
                            {
                            case Action actionTrue:
                                actionTrue.Invoke();
                                break;

                            case IEnumerator iEnumerator when iEnumerator != null:
                                ProcessIEnumerators.Push(iEnumerator);
                                yield return(null);

                                continue;
                            }
                        }
                        else
                        {
                            switch (objects[2])
                            {
                            case Action actionFalse:
                                actionFalse.Invoke();
                                break;

                            case string objStr2 when !string.IsNullOrEmpty(objStr2):
                                SendToChat(objStr2, userNickName, isWhisper, ref parseError);
                                break;

                            case IEnumerator iEnumerator when iEnumerator != null:
                                ProcessIEnumerators.Push(iEnumerator);
                                yield return(null);

                                continue;
                            }
                        }
                    }
                    break;
                }
                break;
            }
    public IEnumerator RespondToCommand(string userNickName, string message)
    {
        TryCancel  = false;
        _responded = false;
        _zoom      = false;
        _processingTwitchCommand = true;
        if (Solved)
        {
            _processingTwitchCommand = false;
            yield break;
        }

        _currentUserNickName = userNickName;

        int beforeStrikeCount = StrikeCount;

        IEnumerator subcoroutine = null;

        if (message.StartsWith("send to module ", StringComparison.InvariantCultureIgnoreCase))
        {
            message = message.Substring(15);
        }
        else
        {
            subcoroutine = RespondToCommandCommon(message, userNickName);
        }

        if (subcoroutine == null || !subcoroutine.MoveNext())
        {
            if (_responded)
            {
                yield break;
            }

            if (_zoom)
            {
                message = message.Substring(4).Trim();
            }

            try
            {
                subcoroutine = RespondToCommandInternal(message);
            }
            catch (Exception e)
            {
                HandleModuleException(e);
                yield break;
            }

            bool moved = false;
            if (subcoroutine != null)
            {
                try
                {
                    moved = subcoroutine.MoveNext();
                    if (moved && modInfo.DoesTheRightThing)
                    {
                        //Handle No-focus API commands. In order to focus the module, the first thing yielded cannot be one of the things handled here, as the solver will yield break if
                        //it is one of these API commands returned.
                        switch (subcoroutine.Current)
                        {
                        case string currentString:
                            if (SendToTwitchChat(currentString, userNickName) && !currentString.StartsWith("strikemessage", StringComparison.InvariantCultureIgnoreCase))
                            {
                                yield break;
                            }
                            break;
                        }
                        _responded = true;
                    }
                }
                catch (Exception e)
                {
                    HandleModuleException(e);
                    yield break;
                }
            }

            if (subcoroutine == null || !moved || Solved || beforeStrikeCount != StrikeCount)
            {
                if (Solved || beforeStrikeCount != StrikeCount)
                {
                    IRCConnection.Instance.SendMessage("Please submit an issue at https://github.com/samfun123/KtaneTwitchPlays/issues regarding module !{0} ({1}) attempting to solve prematurely.", ComponentHandle.Code, ComponentHandle.HeaderText);
                    if (modInfo != null)
                    {
                        modInfo.DoesTheRightThing = false;
                        ModuleData.DataHasChanged = true;
                        ModuleData.WriteDataToFile();
                    }

                    IEnumerator focusDefocus = BombCommander.Focus(Selectable, FocusDistance, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));

                    focusDefocus = BombCommander.Defocus(Selectable, FrontFace);
                    while (focusDefocus.MoveNext())
                    {
                        yield return(focusDefocus.Current);
                    }
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (!_responded)
                {
                    ComponentHandle.CommandInvalid(userNickName);
                }

                _currentUserNickName     = null;
                _processingTwitchCommand = false;
                yield break;
            }
        }

        IEnumerator focusCoroutine = BombCommander.Focus(Selectable, FocusDistance, FrontFace);

        while (focusCoroutine.MoveNext())
        {
            yield return(focusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        IEnumerator unzoom = null;

        if (_zoom)
        {
            IEnumerator zoom = BombMessageResponder.moduleCameras?.ZoomCamera(BombComponent, 1);
            unzoom = BombMessageResponder.moduleCameras?.UnzoomCamera(BombComponent, 1);
            if (zoom == null || unzoom == null)
            {
                _zoom = false;
            }
            else
            {
                while (zoom.MoveNext())
                {
                    yield return(zoom.Current);
                }
            }
        }

        int  previousStrikeCount = StrikeCount;
        bool parseError          = false;
        bool needQuaternionReset = false;
        bool hideCamera          = false;
        bool exceptionThrown     = false;
        bool trycancelsequence   = false;

        while ((previousStrikeCount == StrikeCount && !Solved) || DisableOnStrike)
        {
            try
            {
                if (!subcoroutine.MoveNext())
                {
                    break;
                }
                else
                {
                    _responded = true;
                }
            }
            catch (Exception e)
            {
                exceptionThrown = true;
                HandleModuleException(e);
                break;
            }

            object currentValue = subcoroutine.Current;
            if (currentValue is string currentString)
            {
                if (currentString.Equals("strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedStrikeUserNickName = userNickName;
                }
                else if (currentString.Equals("solve", StringComparison.InvariantCultureIgnoreCase))
                {
                    _delegatedSolveUserNickName = userNickName;
                }
                else if (currentString.Equals("unsubmittablepenalty", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (TwitchPlaySettings.data.UnsubmittablePenaltyPercent <= 0)
                    {
                        continue;
                    }

                    int penalty = Math.Max((int)(modInfo.moduleScore * TwitchPlaySettings.data.UnsubmittablePenaltyPercent), 1);
                    Leaderboard.Instance.AddScore(_currentUserNickName, -penalty);
                    IRCConnection.Instance.SendMessage(TwitchPlaySettings.data.UnsubmittableAnswerPenalty, _currentUserNickName, Code, modInfo.moduleDisplayName, penalty, penalty > 1 ? "s" : "");
                }
                else if (currentString.Equals("parseerror", StringComparison.InvariantCultureIgnoreCase))
                {
                    parseError = true;
                    break;
                }
                else if (currentString.RegexMatch(out Match match, "^trycancel((?: (?:.|\\n)+)?)$"))
                {
                    if (CoroutineCanceller.ShouldCancel)
                    {
                        CoroutineCanceller.ResetCancel();
                        if (!string.IsNullOrEmpty(match.Groups[1].Value))
                        {
                            IRCConnection.Instance.SendMessage($"Sorry @{userNickName}, {match.Groups[1].Value.Trim()}");
                        }
                        break;
                    }
                }
                else if (currentString.RegexMatch(out match, "^trycancelsequence((?: (?:.|\\n)+)?)$"))
                {
                    trycancelsequence = true;
                    yield return(currentValue);

                    continue;
                }
                else if (currentString.RegexMatch(out match, "^trywaitcancel ([0-9]+(?:\\.[0-9])?)((?: (?:.|\\n)+)?)$") && float.TryParse(match.Groups[1].Value, out float waitCancelTime))
                {
                    yield return(new WaitForSecondsWithCancel(waitCancelTime, false));

                    if (CoroutineCanceller.ShouldCancel)
                    {
                        CoroutineCanceller.ResetCancel();
                        if (!string.IsNullOrEmpty(match.Groups[2].Value))
                        {
                            IRCConnection.Instance.SendMessage($"Sorry @{userNickName}, {match.Groups[2].Value.Trim()}");
                        }
                        break;
                    }
                }
                // Commands that allow messages to be sent to the chat.
                else if (SendToTwitchChat(currentString, userNickName))
                {
                    //handled
                }
                else if (currentString.StartsWith("add strike", StringComparison.InvariantCultureIgnoreCase))
                {
                    OnStrike(null);
                }
                else if (currentString.Equals("multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    DisableOnStrike = true;
                }
                else if (currentString.Equals("end multiple strikes", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (previousStrikeCount == StrikeCount)
                    {
                        DisableOnStrike = false;
                        if (Solved)
                        {
                            OnPass(null);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else if (currentString.StartsWith("autosolve", StringComparison.InvariantCultureIgnoreCase))
                {
                    HandleModuleException(new Exception(currentString));
                    break;
                }
                else if (currentString.ToLowerInvariant().EqualsAny("detonate", "explode"))
                {
                    AwardStrikes(_currentUserNickName, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                    BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                    break;
                }
                else if (currentString.RegexMatch(out match, "^(end |toggle )?(?:elevator|hold|waiting) music$"))
                {
                    if (match.Groups.Count > 1 && _musicPlayer != null)
                    {
                        _musicPlayer.StopMusic();
                        _musicPlayer = null;
                    }
                    else if (!currentString.StartsWith("end ", StringComparison.InvariantCultureIgnoreCase) && _musicPlayer == null)
                    {
                        _musicPlayer = MusicPlayer.StartRandomMusic();
                    }
                }
                else if (currentString.ToLowerInvariant().Equals("hide camera"))
                {
                    if (!hideCamera)
                    {
                        BombMessageResponder.moduleCameras?.Hide();
                        BombMessageResponder.moduleCameras?.HideHUD();
                        IEnumerator hideUI = BombCommander.twitchBombHandle.HideMainUIWindow();
                        while (hideUI.MoveNext())
                        {
                            yield return(hideUI.Current);
                        }
                    }
                    hideCamera = true;
                }
                else if (currentString.Equals("cancelled", StringComparison.InvariantCultureIgnoreCase) && CoroutineCanceller.ShouldCancel)
                {
                    CoroutineCanceller.ResetCancel();
                    TryCancel = false;
                    break;
                }
                else
                {
                    if (TwitchPlaySettings.data.EnableDebuggingCommands)
                    {
                        DebugHelper.Log($"Unprocessed string: {currentString}");
                    }
                }
            }
            else if (currentValue is KMSelectable selectable1)
            {
                if (HeldSelectables.Contains(selectable1))
                {
                    DoInteractionEnd(selectable1);
                    HeldSelectables.Remove(selectable1);
                }
                else
                {
                    DoInteractionStart(selectable1);
                    HeldSelectables.Add(selectable1);
                }
            }
            else if (currentValue is IList <KMSelectable> selectables)
            {
                foreach (KMSelectable selectable in selectables)
                {
                    yield return(DoInteractionClick(selectable));

                    if ((previousStrikeCount != StrikeCount && !DisableOnStrike) || Solved || (trycancelsequence && CoroutineCanceller.ShouldCancel))
                    {
                        break;
                    }
                }
                if (trycancelsequence && CoroutineCanceller.ShouldCancel)
                {
                    CoroutineCanceller.ResetCancel();
                    break;
                }
            }
            else if (currentValue is Quaternion localQuaternion)
            {
                BombCommander.RotateByLocalQuaternion(localQuaternion);
                //Whitelist perspective pegs as it only returns Quaternion.Euler(x, 0, 0), which is compatible with the RotateCamaraByQuaternion.
                if (BombComponent.GetComponent <KMBombModule>()?.ModuleType.Equals("spwizPerspectivePegs") ?? false)
                {
                    BombCommander.RotateCameraByLocalQuaternion(BombComponent, localQuaternion);
                }
                needQuaternionReset = true;
            }
            else if (currentValue is Quaternion[] localQuaternions)
            {
                if (localQuaternions.Length == 2)
                {
                    BombCommander.RotateByLocalQuaternion(localQuaternions[0]);
                    BombCommander.RotateCameraByLocalQuaternion(BombComponent, localQuaternions[1]);
                    needQuaternionReset = true;
                }
            }
            else if (currentValue is string[] currentStrings)
            {
                if (currentStrings.Length >= 1)
                {
                    if (currentStrings[0].ToLowerInvariant().EqualsAny("detonate", "explode"))
                    {
                        AwardStrikes(_currentUserNickName, BombCommander.StrikeLimit - BombCommander.StrikeCount);
                        switch (currentStrings.Length)
                        {
                        case 2:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], modInfo.moduleDisplayName);
                            break;

                        case 3:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(currentStrings[1], currentStrings[2]);
                            break;

                        default:
                            BombCommander.twitchBombHandle.CauseExplosionByModuleCommand(string.Empty, modInfo.moduleDisplayName);
                            break;
                        }
                        break;
                    }
                }
            }
            yield return(currentValue);

            if (CoroutineCanceller.ShouldCancel)
            {
                TryCancel = true;
            }

            trycancelsequence = false;
        }

        if (!_responded && !exceptionThrown)
        {
            ComponentHandle.CommandInvalid(userNickName);
        }

        if (needQuaternionReset)
        {
            BombCommander.RotateByLocalQuaternion(Quaternion.identity);
            BombCommander.RotateCameraByLocalQuaternion(BombComponent, Quaternion.identity);
        }

        if (hideCamera)
        {
            BombMessageResponder.moduleCameras?.Show();
            BombMessageResponder.moduleCameras?.ShowHUD();
            IEnumerator showUI = BombCommander.twitchBombHandle.ShowMainUIWindow();
            while (showUI.MoveNext())
            {
                yield return(showUI.Current);
            }
        }

        if (_musicPlayer != null)
        {
            _musicPlayer.StopMusic();
            _musicPlayer = null;
        }

        if (DisableOnStrike)
        {
            DisableOnStrike = false;
            BombMessageResponder.moduleCameras?.UpdateStrikes(true);
            if (Solved)
            {
                OnPass(null);
            }
            AwardStrikes(_currentUserNickName, StrikeCount - previousStrikeCount);
        }

        if (!parseError)
        {
            yield return(new WaitForSeconds(0.5f));
        }

        if (_zoom)
        {
            while (unzoom?.MoveNext() ?? false)
            {
                yield return(unzoom.Current);
            }
        }

        IEnumerator defocusCoroutine = BombCommander.Defocus(Selectable, FrontFace);

        while (defocusCoroutine.MoveNext())
        {
            yield return(defocusCoroutine.Current);
        }

        yield return(new WaitForSeconds(0.5f));

        _currentUserNickName     = null;
        _processingTwitchCommand = false;
    }