Exemplo n.º 1
0
        private static void ReadyUp(IBeatmapLevel song)
        {
            if (queuedSong != null || (queuedSong == null && song == null))
            {
                return;
            }
            if (queuedSong == null && song != null)
            {
                queuedSong = song;
                if (autoReady)
                {
                    PracticeSettings pSettings = null;
                    if (SteamAPI.GetSongOffset() > 0f)
                    {
                        pSettings = new PracticeSettings();
                        float offsetTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() - timeRequestedToLaunch;
                        // add 5 seconds to it because that's about how long it takes to launch a song
                        pSettings.startSongTime = SteamAPI.GetSongOffset() + offsetTime + 5f;
                        timeRequestedToLaunch   = 0;
                    }

                    SongListUtils.StartSong(song, SteamAPI.GetSongDifficulty(), SteamAPI.GetGameplayModifiers(), pSettings);
                }
                else
                {
                    SteamAPI.SetReady();
                    PreviewPlayer.CrossfadeTo(song.previewAudioClip, song.previewStartTime, song.previewDuration);
                }
            }
        }
Exemplo n.º 2
0
        void Update()
        {
            uint size;

            try
            {
                while (SteamNetworking.IsP2PPacketAvailable(out size))
                {
                    var      buffer = new byte[size];
                    uint     bytesRead;
                    CSteamID remoteId;
                    if (SteamNetworking.ReadP2PPacket(buffer, size, out bytesRead, out remoteId))
                    {
                        var          message = Encoding.UTF8.GetString(buffer).Replace(" ", "");
                        PlayerPacket info    = new PlayerPacket(message);
                        if (info.playerId != SteamAPI.GetUserID() && SteamAPI.getLobbyID().m_SteamID != 0)
                        {
                            UpsertPlayer(info);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }
Exemplo n.º 3
0
        public static void Init()
        {
            if (Instance == null)
            {
                Instance             = BeatSaberUI.CreateCustomMenu <CustomMenu>("Waiting for players");
                middleViewController = BeatSaberUI.CreateViewController <ListViewController>();

                Instance.SetMainViewController(middleViewController, true, (firstActivation, type) =>
                {
                    try
                    {
                        if (firstActivation)
                        {
                            level           = middleViewController.CreateText("", new Vector2(BASE.x + 40f, BASE.y));
                            level.alignment = TMPro.TextAlignmentOptions.Center;

                            SteamAPI.SendPlayerPacket(Controllers.PlayerController.Instance._playerInfo);
                        }
                        if (firstInit)
                        {
                            firstInit = false;
                            RefreshData();
                        }
                    } catch (Exception e)
                    {
                        Logger.Error(e);
                    }
                });
            }
        }
Exemplo n.º 4
0
        private void SceneLoaded(Scene to, LoadSceneMode mode)
        {
            Logger.Debug($"Scene loaded: \"{to.name}\"");

            if (to.name == "MenuCore")
            {
                PluginUI.Init();

                GameController.Init(to);
                LeaderboardController.Init(to);
                Controllers.PlayerController.Init(to);
                VoiceChatWorker.Init();
            }

            try
            {
                if (to.name == "MenuCore" && SteamAPI.getLobbyID().m_SteamID > 0)
                {
                    Logger.Debug("Creating new lobby");
                    SteamAPI.CreateLobby(!Config.Instance.IsPublic);
                    Controllers.PlayerController.Instance.DestroyAvatars();
                }
            } catch (Exception e)
            {
                Logger.Error(e);
            }
        }
Exemplo n.º 5
0
        IEnumerator RunLobbyCleanup()
        {
            yield return(new WaitUntil(delegate() { Data.Logger.Info("waiting for active: " + WaitingMenu.Instance.isActiveAndEnabled); return WaitingMenu.Instance.isActiveAndEnabled; }));

            Logger.Debug("Finished song, doing cleanup");
            WaitingMenu.Instance.Dismiss();
            WaitingMenu.firstInit  = true;
            WaitingMenu.queuedSong = null;
            SongListUtils.InSong   = false;
            SteamAPI.FinishSong();
        }
Exemplo n.º 6
0
 void BroadcastPlayerPacket()
 {
     try
     {
         UpdatePlayerPacket();
         SteamAPI.SendPlayerPacket(_playerInfo);
     } catch (Exception e)
     {
         Logger.Error(e);
     }
 }
Exemplo n.º 7
0
        public void Awake()
        {
            if (Instance != this)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);

                _playerInfo   = new PlayerPacket(SteamAPI.GetUserName(), SteamAPI.GetUserID());
                _currentScene = SceneManager.GetActiveScene().name;
            }
        }
Exemplo n.º 8
0
        public static void RefreshData(LevelSO song = null)
        {
            try
            {
                if (queuedSong == null)
                {
                    if (song == null)
                    {
                        song = SongListUtils.GetInstalledSong();
                    }
                    Logger.Debug($"Refresh Waiting Menu data - Song is {(song != null ? "not" : "")} loaded");
                    if (song != null)
                    {
                        level.text = $"Queued: { song.songName} by { song.songAuthorName }";
                        if (song is CustomLevel)
                        {
                            SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)song, (customLevel) =>
                            {
                                Logger.Debug($"Loaded audio Clip for {song.songName}");
                                ReadyUp(customLevel);
                            });
                        }
                        else
                        {
                            ReadyUp(song);
                        }
                    }
                    else if (!downloading)
                    {
                        level.text = $"Downloading: { SteamAPI.GetSongName()}";

                        Logger.Debug($"We do not have the song in our library, lets start downloading it.");
                        downloading = true;
                        Instance.StartCoroutine(Utils.SongDownloader.Instance.DownloadSong(SteamAPI.GetSongId(), LevelDownloadProgress, LevelDownloaded));
                    }
                }
                if (Instance && Instance.isActiveAndEnabled)
                {
                    Dictionary <string, float> status = Controllers.PlayerController.Instance.GetConnectedPlayerDownloadStatus();
                    middleViewController.Data.Clear();
                    foreach (KeyValuePair <string, float> user in status.OrderBy(u => u.Value))
                    {
                        CustomCellInfo cell = new CustomCellInfo(user.Key, user.Value == -1f ? "FAILED TO DOWNLOAD": user.Value == 1f ? "Ready" : $"Downloading song ${(int) Math.Round(user.Value * 100, 0)}%", user.Value == 1f ? Sprites.checkmarkIcon : Sprites.crossIcon);
                        middleViewController.Data.Add(cell);
                    }
                    middleViewController._customListTableView.ReloadData();
                    middleViewController._customListTableView.ScrollToRow(0, false);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }
Exemplo n.º 9
0
 private static void ReadyUp(LevelSO song)
 {
     if (queuedSong != null || (queuedSong == null && song == null))
     {
         return;
     }
     if (queuedSong == null && song != null)
     {
         queuedSong = song;
         SteamAPI.SetReady();
         PreviewPlayer.CrossfadeTo(song.audioClip, song.previewStartTime, song.previewDuration);
     }
 }
Exemplo n.º 10
0
 private void SceneLoaded(Scene to, LoadSceneMode mode)
 {
     try
     {
         if (to.name == "Menu" && SteamAPI.getLobbyID().m_SteamID > 0)
         {
             Logger.Debug("Creating new lobby");
             SteamAPI.CreateLobby(!Config.Instance.IsPublic);
             Controllers.PlayerController.Instance.DestroyAvatars();
         }
     } catch (Exception e)
     {
         Logger.Error(e);
     }
 }
Exemplo n.º 11
0
 public void ActiveSceneChanged(Scene from, Scene to)
 {
     try
     {
         if (!SteamAPI.isLobbyConnected())
         {
             return;
         }
         if (to.name == "GameCore" || to.name == "MenuCore")
         {
             try
             {
                 PlayerController.Instance.DestroyAvatars();
                 if (to.name == "GameCore" && SongListUtils.InSong)
                 {
                     Logger.Debug("update scoreboard");
                     SteamAPI.StartGame();
                     InvokeRepeating("UpdateSongOffset", 0f, 1f);
                     Scoreboard.Instance.disabled = false;
                     List <PlayerPacket> connectedPlayers = Controllers.PlayerController.Instance.GetConnectedPlayerPackets();
                     for (int i = 0; i < connectedPlayers.Count; i++)
                     {
                         Scoreboard.Instance.UpsertScoreboardEntry(connectedPlayers[i].playerId, connectedPlayers[i].playerName, 0, 0);
                     }
                 }
                 else if (to.name == "MenuCore")
                 {
                     Scoreboard.Instance.RemoveAll();
                     Scoreboard.Instance.disabled = true;
                     if (from.name == "GameCore" && SongListUtils.InSong)
                     {
                         StartCoroutine(RunLobbyCleanup());
                     }
                 }
             } catch (Exception e)
             {
                 Logger.Debug($"Exception: {e}");
                 Logger.Error(e);
             }
         }
     }
     catch (Exception e)
     {
         Logger.Error($"(OnlineController) Exception on {_currentScene} scene activation! Exception: {e}");
     }
 }
Exemplo n.º 12
0
        public void UpdatePlayerPacket()
        {
            _playerInfo.avatarHash = ModelSaberAPI.cachedAvatars.FirstOrDefault(x => x.Value == CustomAvatar.Plugin.Instance.PlayerAvatarManager.GetCurrentAvatar()).Key;
            if (_playerInfo.avatarHash == null)
            {
                _playerInfo.avatarHash = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
            }

            _playerInfo.playerName = SteamAPI.GetUserName();
            _playerInfo.playerId   = SteamAPI.GetUserID();

            WorldController.CharacterPosition pos = WorldController.GetCharacterInfo();
            _playerInfo.headPos      = pos.headPos;
            _playerInfo.headRot      = pos.headRot;
            _playerInfo.leftHandPos  = pos.leftHandPos;
            _playerInfo.leftHandRot  = pos.leftHandRot;
            _playerInfo.rightHandPos = pos.rightHandPos;
            _playerInfo.rightHandRot = pos.rightHandRot;
        }
Exemplo n.º 13
0
 public void UpdateSongOffset()
 {
     if (!timeSync)
     {
         timeSync = Resources.FindObjectsOfTypeAll <AudioTimeSyncController>().First();
     }
     else
     {
         if ((timeSync.songLength - timeSync.songTime) > 30)
         {
             SteamAPI.SetSongOffset(timeSync.songTime);
         }
         else
         {
             SteamAPI.SetSongOffset(0f);
             CancelInvoke("UpdateSongOffset");
         }
     }
 }
Exemplo n.º 14
0
        IEnumerator RunLobbyCleanup()
        {
            yield return(new WaitUntil(delegate() { return true; }));

            Logger.Debug("Finished song, doing cleanup");
            try
            {
                WaitingMenu.Instance.Dismiss();
            } catch (Exception e)
            {
                Logger.Error($"Error dismissing WaitingMenu {e}");
            }
            WaitingMenu.firstInit  = true;
            WaitingMenu.queuedSong = null;
            WaitingMenu.autoReady  = false;
            SongListUtils.InSong   = false;
            Controllers.PlayerController.Instance._playerInfo.InSong = false;
            SteamAPI.SetSongOffset(0f);
            CancelInvoke("UpdateSongOffset");
            SteamAPI.FinishSong();
        }
Exemplo n.º 15
0
        private static void refreshFriendsList(ListViewController leftViewController)
        {
            friends = SteamAPI.GetOnlineFriends();
            leftViewController.Data.Clear();
            CGameID gameId = SteamAPI.GetGameID();

            foreach (KeyValuePair <CSteamID, string[]> entry in friends)
            {
                if ("" + gameId != entry.Value[1] || entry.Value[1] == "0")
                {
                    continue;
                }
                leftViewController.Data.Add(new CustomCellInfo(entry.Value[0], "Playing Beat Saber"));
            }
            foreach (KeyValuePair <CSteamID, string[]> entry in friends)
            {
                if ("" + gameId == entry.Value[1] || entry.Value[1] == "0")
                {
                    continue;
                }
                leftViewController.Data.Add(new CustomCellInfo(entry.Value[0], "Playing Other Game"));
            }
            foreach (KeyValuePair <CSteamID, string[]> entry in friends)
            {
                if ("0" != entry.Value[1])
                {
                    continue;
                }
                leftViewController.Data.Add(new CustomCellInfo(entry.Value[0], "Online"));
            }

            leftViewController._customListTableView.ReloadData();
            leftViewController._customListTableView.ScrollToRow(0, false);
            leftViewController.DidSelectRowEvent = (TableView view, int row) =>
            {
                selectedPlayer      = friends.Keys.ToArray()[row].m_SteamID;
                invite.interactable = true;
            };
        }
Exemplo n.º 16
0
 public void OnApplicationQuit()
 {
     SteamAPI.Disconnect();
     SceneManager.activeSceneChanged -= ActiveSceneChanged;
 }
Exemplo n.º 17
0
        public static void Init()
        {
            if (Instance == null)
            {
                try
                {
                    Instance = BeatSaberUI.CreateCustomMenu <CustomMenu>("Multiplayer Lobby");

                    CustomViewController middleViewController = BeatSaberUI.CreateViewController <CustomViewController>();
                    ListViewController   leftViewController   = BeatSaberUI.CreateViewController <ListViewController>();
                    rightViewController = BeatSaberUI.CreateViewController <TableViewController>();

                    Instance.SetMainViewController(middleViewController, true, (firstActivation, type) =>
                    {
                        if (firstActivation)
                        {
                            try
                            {
                                Button host = middleViewController.CreateUIButton("CreditsButton", new Vector2(BASE.x, BASE.y + 2.5f), new Vector2(25f, 7f));
                                host.SetButtonTextSize(3f);
                                host.ToggleWordWrapping(false);
                                host.SetButtonText("Disconnect");
                                host.onClick.AddListener(delegate
                                {
                                    try
                                    {
                                        SteamAPI.Disconnect();
                                        Instance.Dismiss();
                                        MultiplayerListing.Instance.Present();
                                    }
                                    catch (Exception e)
                                    {
                                        Logger.Error(e);
                                    }
                                });
                                float offs = 0;
                                offs      += 10f;
                                Button vc  = middleViewController.CreateUIButton("CreditsButton", new Vector2(BASE.x, BASE.y + 2.5f - offs), new Vector2(25f, 7f));
                                vc.SetButtonTextSize(3f);
                                vc.ToggleWordWrapping(false);
                                vc.SetButtonText(Controllers.PlayerController.Instance.VoipEnabled ? "Disable Voice Chat" : "Enable Voice Chat");
                                vc.onClick.AddListener(delegate
                                {
                                    try
                                    {
                                        if (!Controllers.PlayerController.Instance.VoipEnabled)
                                        {
                                            vc.SetButtonText("Disable Voice Chat");
                                            Controllers.PlayerController.Instance.VoipEnabled = true;
                                            SteamUser.StartVoiceRecording();
                                        }
                                        else
                                        {
                                            vc.SetButtonText("Enable Voice Chat");
                                            Controllers.PlayerController.Instance.VoipEnabled = false;
                                            SteamUser.StopVoiceRecording();
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Logger.Error(e);
                                    }
                                });
                                var t        = middleViewController.CreateText("You can use Online Lobby in the Main Menu to choose songs for your lobby. \n\nYou can also control all the default Game Modifiers for the lobby through the Online Lobby Menu as well.", new Vector2(0, BASE.y - 10f));
                                var tt       = middleViewController.CreateText("If something goes wrong, click the disconnect button above and just reconnect to the lobby.", new Vector2(0, 0 - BASE.y));
                                t.alignment  = TMPro.TextAlignmentOptions.Center;
                                tt.alignment = TMPro.TextAlignmentOptions.Center;
                            } catch (Exception e)
                            {
                                Data.Logger.Error(e);
                            }

                            /*   Button g = middleViewController.CreateUIButton("CreditsButton", new Vector2(0, 0), new Vector2(25f, 25f));
                             * g.SetButtonTextSize(7f);
                             * g.ToggleWordWrapping(false);
                             * g.SetButtonText("Select a Song");
                             * g.onClick.AddListener(delegate {
                             *     try
                             *     {
                             *         if (SteamAPI.IsHost())
                             *         {
                             *             SteamAPI.SetSong("112D7FA45FA06F36FF41029099E95B98", "TaKillYa");
                             *             SteamAPI.SetDifficulty((byte)2);
                             *             SteamAPI.RequestPlay(new GameplayModifiers(new GameplayModifiers()));
                             *         }
                             *     }
                             *     catch (Exception e)
                             *     {
                             *         Logger.Error(e);
                             *     }
                             * });*/
                        }
                    });
                    Instance.SetLeftViewController(leftViewController, false, (firstActivation, type) =>
                    {
                        if (firstActivation)
                        {
                            refreshFriendsList(leftViewController);
                            leftViewController.CreateText("Invite Friends", new Vector2(BASE.x + 62.5f, BASE.y));

                            Button b = leftViewController.CreateUIButton("CreditsButton", new Vector2(BASE.x + 80f, BASE.y + 2.5f), new Vector2(25f, 7f));
                            b.SetButtonText("Refresh");
                            b.SetButtonTextSize(3f);
                            b.ToggleWordWrapping(false);
                            b.onClick.AddListener(delegate()
                            {
                                refreshFriendsList(leftViewController);
                            });


                            invite = leftViewController.CreateUIButton("CreditsButton", new Vector2(BASE.x + 80f, 0 - BASE.y + 2.5f), new Vector2(25f, 7f));
                            invite.SetButtonText("Invite");
                            invite.SetButtonTextSize(3f);
                            invite.ToggleWordWrapping(false);
                            invite.interactable = false;
                            invite.onClick.AddListener(delegate()
                            {
                                if (selectedPlayer > 0)
                                {
                                    SteamAPI.InviteUserToLobby(new CSteamID(selectedPlayer));
                                }
                            });
                        }
                    });
                    Instance.SetRightViewController(rightViewController, false, (active, type) => {
                        if (active)
                        {
                            rightViewController.CreateText("Lobby Leaderboard", new Vector2(BASE.x + 62.5f, BASE.y));
                        }
                        RefreshScores();
                    });
                } catch (Exception e)
                {
                    Data.Logger.Error(e);
                }
            }
        }
Exemplo n.º 18
0
        public void UpsertPlayer(PlayerPacket info)
        {
            if (info.playerId == _playerInfo.playerId)
            {
                return;
            }
            try
            {
                if (!_connectedPlayers.Keys.Contains(info.playerId))
                {
                    _connectedPlayers.Add(info.playerId, info);
                    if ((Config.Instance.AvatarsInLobby && Plugin.instance.CurrentScene == "MenuCore") || (Config.Instance.AvatarsInGame && Plugin.instance.CurrentScene == "GameCore"))
                    {
                        AvatarController avatar = new GameObject("AvatarController").AddComponent <AvatarController>();
                        avatar.SetPlayerPacket(info, new Vector3(0, 0, 0), info.playerId == _playerInfo.playerId);
                        _connectedPlayerAvatars.Add(info.playerId, avatar);
                    }
                    MultiplayerLobby.RefreshScores();
                    Scoreboard.Instance.UpsertScoreboardEntry(info.playerId, info.playerName);
                    return;
                }

                if (_connectedPlayers.ContainsKey(info.playerId))
                {
                    if (info.playerScore != _connectedPlayers[info.playerId].playerScore || info.playerComboBlocks != _connectedPlayers[info.playerId].playerComboBlocks)
                    {
                        Scoreboard.Instance.UpsertScoreboardEntry(info.playerId, info.playerName, (int)info.playerScore, (int)info.playerComboBlocks);
                        MultiplayerLobby.RefreshScores();
                    }
                    if (_connectedPlayerAvatars.ContainsKey(info.playerId) && (Config.Instance.AvatarsInLobby && Plugin.instance.CurrentScene == "MenuCore") || (Config.Instance.AvatarsInGame && Plugin.instance.CurrentScene == "GameCore"))
                    {
                        Vector3 offset = new Vector3(0, 0, 0);
                        if (Plugin.instance.CurrentScene == "GameCore")
                        {
                            ulong[] playerInfosByID = new ulong[_connectedPlayers.Count + 1];
                            playerInfosByID[0] = _playerInfo.playerId;
                            _connectedPlayers.Keys.ToList().CopyTo(playerInfosByID, 1);
                            Array.Sort(playerInfosByID);
                            offset = new Vector3((Array.IndexOf(playerInfosByID, info.playerId) - Array.IndexOf(playerInfosByID, _playerInfo.playerId)) * 2f, 0, Math.Abs((Array.IndexOf(playerInfosByID, info.playerId) - Array.IndexOf(playerInfosByID, _playerInfo.playerId)) * 2.5f));
                        }

                        _connectedPlayerAvatars[info.playerId].SetPlayerPacket(info, offset, info.playerId == _playerInfo.playerId);
                    }
                    bool changedReady = (_connectedPlayers[info.playerId].Ready != info.Ready || _connectedPlayers[info.playerId].playerProgress != info.playerProgress);
                    _connectedPlayers[info.playerId] = info;
                    MockPartyViewController.Instance.UpdatePlayButton();
                    if (changedReady)
                    {
                        WaitingMenu.RefreshData();
                        if (SteamAPI.IsHost())
                        {
                            if (info.Ready)
                            {
                                if (_connectedPlayers.Values.ToList().All(u => u.Ready))
                                {
                                    Data.Logger.Debug($"Everyone has confirmed that they are ready to play, broadcast that we want them all to start playing");
                                    SteamAPI.StartPlaying();
                                }
                            }
                            else
                            {
                                if (_connectedPlayers.Values.ToList().All(u => !u.Ready))
                                {
                                    Data.Logger.Debug($"Everyone has confirmed they are in game, set the lobby screen to in game");
                                    SteamAPI.StartGame();
                                }
                            }
                        }
                    }
                }
            } catch (Exception e)
            {
                Logger.Error(e);
            }
        }