private void SongLoaded(BeatmapLevelSO song)
 {
     if (_difficultySelectionViewController != null)
     {
         _difficultySelectionViewController.SetPlayButtonInteractable(true);
     }
     PreviewPlayer.CrossfadeTo(song.beatmapLevelData.audioClip, song.previewStartTime, (song.beatmapLevelData.audioClip.length - song.previewStartTime), 1f);
 }
Exemple #2
0
        public void ShowDifficultySelection(SongInfo song)
        {
            if (song == null)
            {
                return;
            }

            if (_difficultySelectionViewController == null)
            {
                _difficultySelectionViewController = BeatSaberUI.CreateViewController <DifficultySelectionViewController>();
                _difficultySelectionViewController.discardPressed      += DiscardPressed;
                _difficultySelectionViewController.playPressed         += (level, characteristic, difficulty) => { PlayPressed(level, characteristic, difficulty, _playerManagementViewController.modifiers); };
                _difficultySelectionViewController.levelOptionsChanged += UpdateLevelOptions;
            }

            if (!_roomNavigationController.viewControllers.Contains(_difficultySelectionViewController))
            {
                PushViewControllerToNavigationController(_roomNavigationController, _difficultySelectionViewController, null, true);
            }

            _difficultySelectionViewController.UpdateViewController(Client.Instance.isHost, roomInfo.perPlayerDifficulty);

            IPreviewBeatmapLevel selectedLevel = SongCore.Loader.CustomBeatmapLevelPackCollectionSO.beatmapLevelPacks.SelectMany(x => x.beatmapLevelCollection.beatmapLevels).FirstOrDefault(x => x.levelID == song.levelId);

            if (selectedLevel != null)
            {
                _difficultySelectionViewController.SetPlayButtonInteractable(false);
                _difficultySelectionViewController.SetLoadingState(true);

                LoadBeatmapLevelAsync(selectedLevel,
                                      (status, success, level) =>
                {
                    if (status == AdditionalContentModelSO.EntitlementStatus.NotOwned)
                    {
                        _difficultySelectionViewController.SetSelectedSong(selectedLevel);
                        _difficultySelectionViewController.SetPlayButtonInteractable(false);
                        Client.Instance.SendPlayerReady(false);
                        Client.Instance.playerInfo.updateInfo.playerState    = PlayerState.DownloadingSongs;
                        Client.Instance.playerInfo.updateInfo.playerProgress = 0f; selectedLevel.GetPreviewAudioClipAsync(new CancellationToken()).ContinueWith(
                            (res) =>
                        {
                            if (!res.IsFaulted)
                            {
                                PreviewPlayer.CrossfadeTo(res.Result, selectedLevel.previewStartTime, (res.Result.length - selectedLevel.previewStartTime));
                                _difficultySelectionViewController.SetSongDuration(res.Result.length);
                            }
                        });
                    }
                    else if (success)
                    {
                        _difficultySelectionViewController.SetSelectedSong(level);

                        if (level.beatmapLevelData.audioClip != null)
                        {
                            PreviewPlayer.CrossfadeTo(level.beatmapLevelData.audioClip, selectedLevel.previewStartTime, (level.beatmapLevelData.audioClip.length - selectedLevel.previewStartTime), 1f);
                            _difficultySelectionViewController.SetPlayButtonInteractable(true);
                            Client.Instance.SendPlayerReady(true);
                        }
                        else
                        {
                            _difficultySelectionViewController.SetPlayButtonInteractable(false);
                            Client.Instance.SendPlayerReady(false);
                        }

                        Client.Instance.playerInfo.updateInfo.playerState = PlayerState.Room;
                    }
                    else
                    {
                        _difficultySelectionViewController.SetSelectedSong(song);
                        _difficultySelectionViewController.SetPlayButtonInteractable(false);
                        Client.Instance.SendPlayerReady(false);
                        Client.Instance.playerInfo.updateInfo.playerState = PlayerState.Room;
                    }
                });
            }
            else
            {
                _difficultySelectionViewController.SetSelectedSong(song);
                Client.Instance.playerInfo.updateInfo.playerState = PlayerState.DownloadingSongs;
                Client.Instance.SendPlayerReady(false);
                _difficultySelectionViewController.SetPlayButtonInteractable(false);
                SongDownloader.Instance.RequestSongByLevelID(song.hash, (info) =>
                {
                    Client.Instance.playerInfo.updateInfo.playerState = PlayerState.DownloadingSongs;

                    songToDownload = info;

                    SongDownloader.Instance.DownloadSong(songToDownload,
                                                         (success) =>
                    {
                        void onLoaded(SongCore.Loader sender, Dictionary <string, CustomPreviewBeatmapLevel> songs)
                        {
                            SongCore.Loader.SongsLoadedEvent -= onLoaded;
                            Client.Instance.playerInfo.updateInfo.playerState = PlayerState.Room;
                            roomInfo.selectedSong.UpdateLevelId();
                            selectedLevel = songs.FirstOrDefault(x => x.Value.levelID == roomInfo.selectedSong.levelId).Value;
                            if (selectedLevel != null)
                            {
                                LoadBeatmapLevelAsync(selectedLevel,
                                                      (status, loaded, level) =>
                                {
                                    if (loaded)
                                    {
                                        PreviewPlayer.CrossfadeTo(level.beatmapLevelData.audioClip, level.previewStartTime, (level.beatmapLevelData.audioClip.length - level.previewStartTime));
                                        _difficultySelectionViewController.SetSelectedSong(level);
                                        _difficultySelectionViewController.SetPlayButtonInteractable(true);
                                        _difficultySelectionViewController.SetProgressBarState(false, 1f);
                                        Client.Instance.SendPlayerReady(true);
                                        Client.Instance.playerInfo.updateInfo.playerState = PlayerState.Room;
                                    }
                                    else
                                    {
                                        Plugin.log.Error($"Unable to load level!");
                                    }
                                });
                            }
                            else
                            {
                                Plugin.log.Error($"Level with ID {roomInfo.selectedSong.levelId} not found!");
                            }
                        }

                        SongCore.Loader.SongsLoadedEvent += onLoaded;

                        SongCore.Loader.Instance.RefreshSongs(false);
                        songToDownload = null;
                    },
                                                         (progress) =>
                    {
                        float clampedProgress = Math.Min(progress, 0.99f);
                        _difficultySelectionViewController.SetProgressBarState(true, clampedProgress);
                        Client.Instance.playerInfo.updateInfo.playerProgress = 100f * clampedProgress;
                    });
                });
            }
        }