<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osu.Game.Screens.Play;

namespace osu.Game.Tests.Visual
{
    /// <summary>
    /// A player that exposes many components that would otherwise not be available, for testing purposes.
    /// </summary>
    public class TestPlayer : SoloPlayer
    {
        protected override bool PauseOnFocusLost { get; }

        public new DrawableRuleset DrawableRuleset => base.DrawableRuleset;

        public new Bindable<IReadOnlyList<Mod>> Mods => base.Mods;

        public new HUDOverlay HUDOverlay => base.HUDOverlay;

        public new GameplayClockContainer GameplayClockContainer => base.GameplayClockContainer;

        public new ScoreProcessor ScoreProcessor => base.ScoreProcessor;

        public new HealthProcessor HealthProcessor => base.HealthProcessor;

        public bool TokenCreationRequested { get; private set; }

        public Score SubmittedScore { get; private set; }

        public new bool PauseCooldownActive => base.PauseCooldownActive;

        public readonly List<JudgementResult> Results = new List<JudgementResult>();

        [Resolved]
        private SpectatorClient spectatorClient { get; set; }

        public TestPlayer(bool allowPause = true, bool showResults = true, bool pauseOnFocusLost = false)
            : base(new PlayerConfiguration
            {
                AllowPause = allowPause,
                ShowResults = showResults
            })
        {
            PauseOnFocusLost = pauseOnFocusLost;
        }

        protected override bool HandleTokenRetrievalFailure(Exception exception) => false;

        protected override APIRequest<APIScoreToken> CreateTokenRequest()
        {
            TokenCreationRequested = true;
            return base.CreateTokenRequest();
        }

        protected override APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token)
        {
            SubmittedScore = score;
            return base.CreateSubmissionRequest(score, token);
        }

        protected override void PrepareReplay()
        {
            // Generally, replay generation is handled by whatever is constructing the player.
            // This is implemented locally here to ease migration of test scenes that have some executions
            // running with autoplay and some not, but are not written in a way that lends to instantiating
            // different `Player` types.
            //
            // Eventually we will want to remove this and update all test usages which rely on autoplay to use
            // a `TestReplayPlayer`.
            var autoplayMod = Mods.Value.OfType<ModAutoplay>().FirstOrDefault();

            if (autoplayMod != null)
            {
                DrawableRuleset?.SetReplayScore(autoplayMod.CreateScoreFromReplayData(GameplayState.Beatmap, Mods.Value));
                return;
            }

            base.PrepareReplay();
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            if (!LoadedBeatmapSuccessfully)
                return;

            ScoreProcessor.NewJudgement += r => Results.Add(r);
        }

        protected override void Dispose(bool isDisposing)
        {
            base.Dispose(isDisposing);

            // Specific to tests, the player can be disposed without OnExiting() ever being called.
            // We should make sure that the gameplay session has finished even in this case.
            if (LoadedBeatmapSuccessfully)
                spectatorClient.EndPlaying(GameplayState);
        }
    }
}<commit_msg>Add null check in `TestPlayer`'s disposal code to avoid cascading test failure<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osu.Game.Screens.Play;

namespace osu.Game.Tests.Visual
{
    /// <summary>
    /// A player that exposes many components that would otherwise not be available, for testing purposes.
    /// </summary>
    public class TestPlayer : SoloPlayer
    {
        protected override bool PauseOnFocusLost { get; }

        public new DrawableRuleset DrawableRuleset => base.DrawableRuleset;

        public new Bindable<IReadOnlyList<Mod>> Mods => base.Mods;

        public new HUDOverlay HUDOverlay => base.HUDOverlay;

        public new GameplayClockContainer GameplayClockContainer => base.GameplayClockContainer;

        public new ScoreProcessor ScoreProcessor => base.ScoreProcessor;

        public new HealthProcessor HealthProcessor => base.HealthProcessor;

        public bool TokenCreationRequested { get; private set; }

        public Score SubmittedScore { get; private set; }

        public new bool PauseCooldownActive => base.PauseCooldownActive;

        public readonly List<JudgementResult> Results = new List<JudgementResult>();

        [Resolved]
        private SpectatorClient spectatorClient { get; set; }

        public TestPlayer(bool allowPause = true, bool showResults = true, bool pauseOnFocusLost = false)
            : base(new PlayerConfiguration
            {
                AllowPause = allowPause,
                ShowResults = showResults
            })
        {
            PauseOnFocusLost = pauseOnFocusLost;
        }

        protected override bool HandleTokenRetrievalFailure(Exception exception) => false;

        protected override APIRequest<APIScoreToken> CreateTokenRequest()
        {
            TokenCreationRequested = true;
            return base.CreateTokenRequest();
        }

        protected override APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token)
        {
            SubmittedScore = score;
            return base.CreateSubmissionRequest(score, token);
        }

        protected override void PrepareReplay()
        {
            // Generally, replay generation is handled by whatever is constructing the player.
            // This is implemented locally here to ease migration of test scenes that have some executions
            // running with autoplay and some not, but are not written in a way that lends to instantiating
            // different `Player` types.
            //
            // Eventually we will want to remove this and update all test usages which rely on autoplay to use
            // a `TestReplayPlayer`.
            var autoplayMod = Mods.Value.OfType<ModAutoplay>().FirstOrDefault();

            if (autoplayMod != null)
            {
                DrawableRuleset?.SetReplayScore(autoplayMod.CreateScoreFromReplayData(GameplayState.Beatmap, Mods.Value));
                return;
            }

            base.PrepareReplay();
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            if (!LoadedBeatmapSuccessfully)
                return;

            ScoreProcessor.NewJudgement += r => Results.Add(r);
        }

        protected override void Dispose(bool isDisposing)
        {
            base.Dispose(isDisposing);

            // Specific to tests, the player can be disposed without OnExiting() ever being called.
            // We should make sure that the gameplay session has finished even in this case.
            if (LoadedBeatmapSuccessfully)
                spectatorClient?.EndPlaying(GameplayState);
        }
    }
}
{'fraction_non_alphanumeric': 0.07271400778210117, 'fraction_numerical': 0.0, 'mean_word_length': 2.4299416180150124, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3850473', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2050, 'n_words': 633}