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.SetSelectedSong(song);
            _difficultySelectionViewController.UpdateViewController(Client.Instance.isHost, roomInfo.perPlayerDifficulty);

            BeatmapLevelSO selectedLevel = SongLoader.CustomBeatmapLevelPackCollectionSO.beatmapLevelPacks.Where(x => x.packID == "CustomMaps" || CustomExtensions.basePackIDs.Contains(x.packID)).SelectMany(x => x.beatmapLevelCollection.beatmapLevels).FirstOrDefault(x => x.levelID.StartsWith(song.levelId)) as BeatmapLevelSO;

            if (selectedLevel != null)
            {
                if (selectedLevel is CustomLevel)
                {
                    _difficultySelectionViewController.SetPlayButtonInteractable(false);
                    SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)selectedLevel, SongLoaded);
                }
                else
                {
                    _difficultySelectionViewController.SetPlayButtonInteractable(true);
                    PreviewPlayer.CrossfadeTo(selectedLevel.beatmapLevelData.audioClip, selectedLevel.previewStartTime, (selectedLevel.beatmapLevelData.audioClip.length - selectedLevel.previewStartTime), 1f);
                }
                Client.Instance.SendPlayerReady(true);
                Client.Instance.playerInfo.playerState = PlayerState.Room;
            }
            else
            {
                Client.Instance.playerInfo.playerState = PlayerState.DownloadingSongs;
                Client.Instance.SendPlayerReady(false);
                _difficultySelectionViewController.SetPlayButtonInteractable(false);
                SongDownloader.Instance.RequestSongByLevelID(song.levelId, (info) =>
                {
                    Client.Instance.playerInfo.playerState = PlayerState.DownloadingSongs;

                    songToDownload = info;

                    SongDownloader.Instance.DownloadSong(songToDownload, "MultiplayerSongs", () =>
                    {
                        Action <SongLoader, List <CustomLevel> > onLoaded = null;
                        onLoaded = (sender, songs) =>
                        {
                            SongLoader.SongsLoadedEvent           -= onLoaded;
                            Client.Instance.playerInfo.playerState = PlayerState.Room;
                            selectedLevel = songs.FirstOrDefault(x => x.levelID.StartsWith(roomInfo.selectedSong.levelId));
                            if (selectedLevel != null)
                            {
                                SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)selectedLevel,
                                                                          (levelLoaded) =>
                                {
                                    PreviewPlayer.CrossfadeTo(levelLoaded.beatmapLevelData.audioClip, levelLoaded.previewStartTime, (levelLoaded.beatmapLevelData.audioClip.length - levelLoaded.previewStartTime));
                                });
                                _difficultySelectionViewController.SetSelectedSong(song);
                                _difficultySelectionViewController.SetPlayButtonInteractable(true);
                                _difficultySelectionViewController.SetProgressBarState(false, 1f);
                                Client.Instance.SendPlayerReady(true);
                                Client.Instance.playerInfo.playerState = PlayerState.Room;
                            }
                        };

                        SongLoader.SongsLoadedEvent += onLoaded;
                        songToDownload = null;
                    },
                                                         (progress) =>
                    {
                        float clampedProgress = Math.Min(progress, 0.99f);
                        _difficultySelectionViewController.SetProgressBarState(true, clampedProgress);
                        Client.Instance.playerInfo.playerProgress = 100f * clampedProgress;
                    });
                });
            }
        }
Example #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;
                    });
                });
            }
        }
        public void ShowDifficultySelection(SongInfo song)
        {
            if (_difficultySelectionViewController == null)
            {
                _difficultySelectionViewController = BeatSaberUI.CreateViewController <DifficultySelectionViewController>();
                _difficultySelectionViewController.DiscardPressed += DiscardPressed;
                _difficultySelectionViewController.PlayPressed    += PlayPressed;
            }

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

            _difficultySelectionViewController.SetSelectedSong(song);
            _difficultySelectionViewController.UpdateViewController(Client.Instance.isHost);

            LevelSO selectedLevel = _levelCollection.levels.FirstOrDefault(x => x.levelID.StartsWith(song.levelId));

            if (selectedLevel != null)
            {
                if (selectedLevel is CustomLevel)
                {
                    SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)selectedLevel, SongLoaded);
                }
                else
                {
                    _difficultySelectionViewController.SetPlayButtonInteractable(true);
                    PreviewPlayer.CrossfadeTo(selectedLevel.audioClip, selectedLevel.previewStartTime, (selectedLevel.audioClip.length - selectedLevel.previewStartTime), 1f);
                }
                Client.Instance.SendPlayerReady(true);
                Client.Instance.playerInfo.playerState = PlayerState.Room;
            }
            else
            {
                Client.Instance.playerInfo.playerState = PlayerState.DownloadingSongs;
                Client.Instance.SendPlayerReady(false);
                SongDownloader.Instance.RequestSongByLevelID(song.levelId, (info) =>
                {
                    Client.Instance.playerInfo.playerState = PlayerState.DownloadingSongs;

                    songToDownload = info;

                    SongDownloader.Instance.DownloadSong(songToDownload, "MultiplayerSongs", () =>
                    {
                        SongLoader.Instance.RefreshSongs(false);

                        Action <SongLoader, List <CustomLevel> > onLoaded = null;
                        onLoaded = (sender, songs) =>
                        {
                            SongLoader.SongsLoadedEvent           -= onLoaded;
                            Client.Instance.playerInfo.playerState = PlayerState.Room;
                            selectedLevel = songs.FirstOrDefault(x => x.levelID.StartsWith(roomInfo.selectedSong.levelId));
                            if (selectedLevel != null)
                            {
                                SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)selectedLevel,
                                                                          (levelLoaded) =>
                                {
                                    PreviewPlayer.CrossfadeTo(levelLoaded.audioClip, levelLoaded.previewStartTime, (levelLoaded.audioClip.length - levelLoaded.previewStartTime));
                                });
                                _difficultySelectionViewController.SetSelectedSong(song);
                                Client.Instance.SendPlayerReady(true);
                                Client.Instance.playerInfo.playerState = PlayerState.Room;
                            }
                        };

                        SongLoader.SongsLoadedEvent += onLoaded;
                        songToDownload = null;
                    },
                                                         (progress) =>
                    {
                        float clampedProgress = Math.Min(progress, 0.99f);
                        _difficultySelectionViewController.SetProgressBarState(true, clampedProgress);
                        Client.Instance.playerInfo.playerProgress = 100f * clampedProgress;
                    });
                });
            }
        }