private void DisconnectCommandReceived(NetIncomingMessage msg) { InGameOnlineController.Instance.needToSendUpdates = false; if (msg == null) { PopAllViewControllers(); InGameOnlineController.Instance.DestroyPlayerControllers(); PreviewPlayer.CrossfadeToDefault(); joined = false; _roomNavigationController.DisplayError("Lost connection to the ServerHub!"); } else if (msg.LengthBytes > 3) { string reason = msg.ReadString(); PopAllViewControllers(); InGameOnlineController.Instance.DestroyPlayerControllers(); PreviewPlayer.CrossfadeToDefault(); joined = false; lastSelectedSong = ""; _roomNavigationController.DisplayError(reason); } else { _roomNavigationController.DisplayError("ServerHub refused connection!"); } }
public void ReturnToChannel() { joined = true; Client.Instance.MessageReceived -= MessageReceived; Client.Instance.MessageReceived += MessageReceived; if (Client.Instance.connected) { Client.Instance.RequestChannelInfo(channelId); } else { InGameOnlineController.Instance.needToSendUpdates = false; PopAllViewControllers(); InGameOnlineController.Instance.DestroyPlayerControllers(); PreviewPlayer.CrossfadeToDefault(); joined = false; _radioNavController.DisplayError("Lost connection to the ServerHub!"); } }
private void PacketReceived(NetIncomingMessage msg) { CommandType commandType = (CommandType)msg.ReadByte(); if (!joined) { if (commandType == CommandType.JoinRoom) { switch (msg.ReadByte()) { case 0: { Client.Instance.playerInfo.playerState = PlayerState.DownloadingSongs; Client.Instance.RequestRoomInfo(); Client.Instance.SendPlayerInfo(); joined = true; InGameOnlineController.Instance.needToSendUpdates = true; } break; case 1: { _roomNavigationController.DisplayError("Unable to join room!\nRoom not found"); } break; case 2: { _roomNavigationController.DisplayError("Unable to join room!\nIncorrect password"); } break; case 3: { _roomNavigationController.DisplayError("Unable to join room!\nToo much players"); } break; default: { _roomNavigationController.DisplayError("Unable to join room!\nUnknown error"); } break; } } } else { try { switch (commandType) { case CommandType.GetRoomInfo: { Client.Instance.playerInfo.playerState = PlayerState.Room; roomInfo = new RoomInfo(msg); Client.Instance.isHost = Client.Instance.playerInfo.Equals(roomInfo.roomHost); UpdateUI(roomInfo.roomState); } break; case CommandType.SetSelectedSong: { if (msg.LengthBytes < 16) { roomInfo.roomState = RoomState.SelectingSong; roomInfo.selectedSong = null; PreviewPlayer.CrossfadeToDefault(); UpdateUI(roomInfo.roomState); if (songToDownload != null) { songToDownload.songQueueState = SongQueueState.Error; Client.Instance.playerInfo.playerState = PlayerState.Room; } } else { roomInfo.roomState = RoomState.Preparing; SongInfo selectedSong = new SongInfo(msg); roomInfo.selectedSong = selectedSong; if (songToDownload != null) { songToDownload.songQueueState = SongQueueState.Error; } UpdateUI(roomInfo.roomState); } } break; case CommandType.TransferHost: { roomInfo.roomHost = new PlayerInfo(msg); Client.Instance.isHost = Client.Instance.playerInfo.Equals(roomInfo.roomHost); if (Client.Instance.playerInfo.playerState == PlayerState.DownloadingSongs) { return; } UpdateUI(roomInfo.roomState); } break; case CommandType.StartLevel: { if (Client.Instance.playerInfo.playerState == PlayerState.DownloadingSongs) { return; } lastSelectedSong = ""; Client.Instance.playerInfo.playerComboBlocks = 0; Client.Instance.playerInfo.playerCutBlocks = 0; Client.Instance.playerInfo.playerEnergy = 0f; Client.Instance.playerInfo.playerScore = 0; byte difficulty = msg.ReadByte(); SongInfo songInfo = new SongInfo(msg); MenuSceneSetupDataSO menuSceneSetupData = Resources.FindObjectsOfTypeAll <MenuSceneSetupDataSO>().FirstOrDefault(); if (menuSceneSetupData != null) { GameplayModifiers gameplayModifiers = new GameplayModifiers(); if (Config.Instance.SpectatorMode) { Client.Instance.playerInfo.playerState = PlayerState.Spectating; gameplayModifiers.noFail = true; } else { Client.Instance.playerInfo.playerState = PlayerState.Game; gameplayModifiers.noFail = roomInfo.noFail; } PlayerSpecificSettings playerSettings = Resources.FindObjectsOfTypeAll <PlayerDataModelSO>().FirstOrDefault().currentLocalPlayer.playerSpecificSettings; roomInfo.roomState = RoomState.InGame; Client.Instance.MessageReceived -= PacketReceived; LevelSO level = _levelCollection.levels.First(x => x.levelID.StartsWith(songInfo.levelId)); IDifficultyBeatmap difficultyBeatmap = level.GetDifficultyBeatmap((BeatmapDifficulty)difficulty); Logger.Info($"Starting song: name={level.songName}, levelId={level.levelID}, difficulty={(BeatmapDifficulty)difficulty}"); Client.Instance.MessageReceived -= PacketReceived; menuSceneSetupData.StartStandardLevel(difficultyBeatmap, gameplayModifiers, playerSettings, null, null, (StandardLevelSceneSetupDataSO sender, LevelCompletionResults levelCompletionResults) => { InGameOnlineController.Instance.SongFinished(sender, levelCompletionResults, difficultyBeatmap, gameplayModifiers, false); }); return; } else { Logger.Error("SceneSetupData is null!"); } } break; case CommandType.LeaveRoom: { LeaveRoom(); } break; case CommandType.UpdatePlayerInfo: { if (roomInfo != null) { if (roomInfo.roomState != RoomState.SelectingSong) { float currentTime = msg.ReadFloat(); float totalTime = msg.ReadFloat(); int playersCount = msg.ReadInt32(); List <PlayerInfo> playerInfos = new List <PlayerInfo>(); for (int j = 0; j < playersCount; j++) { try { playerInfos.Add(new PlayerInfo(msg)); } catch (Exception e) { #if DEBUG Misc.Logger.Exception($"Unable to parse PlayerInfo! Excpetion: {e}"); #endif } } switch (roomInfo.roomState) { case RoomState.InGame: playerInfos = playerInfos.Where(x => x.playerScore > 0 && x.playerState == PlayerState.Game).ToList(); UpdateLeaderboard(playerInfos, currentTime, totalTime, false); break; case RoomState.Results: playerInfos = playerInfos.Where(x => x.playerScore > 0 && (x.playerState == PlayerState.Game || x.playerState == PlayerState.Room)).ToList(); UpdateLeaderboard(playerInfos, currentTime, totalTime, true); break; case RoomState.Preparing: _roomManagementViewController.UpdatePlayerList(playerInfos); break; } } } } break; case CommandType.PlayerReady: { int playersReady = msg.ReadInt32(); int playersTotal = msg.ReadInt32(); if (roomInfo.roomState == RoomState.Preparing && _difficultySelectionViewController != null) { _difficultySelectionViewController.SetPlayersReady(playersReady, playersTotal); } } break; case CommandType.Disconnect: { InGameOnlineController.Instance.needToSendUpdates = false; if (msg.LengthBytes > 3) { string reason = msg.ReadString(); PopAllViewControllers(_roomNavigationController); InGameOnlineController.Instance.DestroyAvatars(); PreviewPlayer.CrossfadeToDefault(); joined = false; lastSelectedSong = ""; _roomNavigationController.DisplayError(reason); } else { _roomNavigationController.DisplayError("ServerHub refused connection!"); } } break; } }catch (Exception e) { Logger.Exception("Unable to parse packet!"); if (msg != null) { Logger.Exception($"Packet={commandType}, DataLength={msg.LengthBytes}"); } Logger.Exception(e.ToString()); } } }
private void MessageReceived(NetIncomingMessage msg) { if (msg == null) { InGameOnlineController.Instance.needToSendUpdates = false; PopAllViewControllers(); InGameOnlineController.Instance.DestroyPlayerControllers(); PreviewPlayer.CrossfadeToDefault(); joined = false; _radioNavController.DisplayError("Lost connection to the ServerHub!"); return; } CommandType commandType = (CommandType)msg.ReadByte(); if (!joined) { if (commandType == CommandType.JoinChannel) { switch (msg.ReadByte()) { case 0: { Client.Instance.playerInfo.playerState = PlayerState.Room; Client.Instance.RequestChannelInfo(channelId); Client.Instance.SendPlayerInfo(); joined = true; InGameOnlineController.Instance.needToSendUpdates = true; } break; case 1: { _radioNavController.DisplayError("Unable to join channel!\nChannel not found"); } break; default: { _radioNavController.DisplayError("Unable to join channel!\nUnknown error"); } break; } } } else { try { switch (commandType) { case CommandType.GetChannelInfo: { channelInfo = new ChannelInfo(msg); channelInfo.ip = ip; channelInfo.port = port; UpdateUI(channelInfo.state); } break; case CommandType.SetSelectedSong: { if (msg.LengthBytes > 16) { SongInfo song = new SongInfo(msg); channelInfo.currentSong = song; channelInfo.state = ChannelState.NextSong; UpdateUI(channelInfo.state); } } break; case CommandType.GetSongDuration: { SongInfo requestedSong = new SongInfo(msg); BeatmapLevelSO level = SongLoader.CustomBeatmapLevelPackCollectionSO.beatmapLevelPacks.SelectMany(x => x.beatmapLevelCollection.beatmapLevels).FirstOrDefault(x => x.levelID.StartsWith(channelInfo.currentSong.levelId)) as BeatmapLevelSO; if (level != null) { SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)level, (loadedLevel) => { Client.Instance.SendSongDuration(new SongInfo(loadedLevel)); }); } } break; case CommandType.StartLevel: { if (Client.Instance.playerInfo.playerState == PlayerState.DownloadingSongs) { return; } if (nextSongSkipped) { nextSongSkipped = false; channelInfo.state = ChannelState.InGame; UpdateUI(channelInfo.state); return; } StartLevelInfo levelInfo = new StartLevelInfo(msg); BeatmapCharacteristicSO characteristic = _beatmapCharacteristics.First(x => x.serializedName == levelInfo.characteristicName); SongInfo songInfo = new SongInfo(msg); BeatmapLevelSO level = SongLoader.CustomBeatmapLevelPackCollectionSO.beatmapLevelPacks.SelectMany(x => x.beatmapLevelCollection.beatmapLevels).FirstOrDefault(x => x.levelID.StartsWith(songInfo.levelId)) as BeatmapLevelSO; if (level == null) { Misc.Logger.Error("Unable to start level! Level is null! LevelID=" + songInfo.levelId); return; } SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)level, (levelLoaded) => { try { BS_Utils.Gameplay.Gamemode.NextLevelIsIsolated("Beat Saber Multiplayer"); } catch { } StartLevel(levelLoaded, characteristic, levelInfo.difficulty, levelInfo.modifiers); }); } break; case CommandType.LeaveRoom: { LeaveChannel(); } break; case CommandType.UpdatePlayerInfo: { if (channelInfo != null) { currentTime = msg.ReadFloat(); totalTime = msg.ReadFloat(); switch (channelInfo.state) { case ChannelState.InGame: UpdateInGameScreen(); break; case ChannelState.NextSong: UpdateNextSongScreen(); break; case ChannelState.Results: { UpdateResultsScreen(); int playersCount = msg.ReadInt32(); List <PlayerInfo> playerInfos = new List <PlayerInfo>(); try { for (int j = 0; j < playersCount; j++) { playerInfos.Add(new PlayerInfo(msg)); } } catch (Exception e) { #if DEBUG Misc.Logger.Exception($"Unable to parse PlayerInfo! Excpetion: {e}"); #endif return; } playerInfos = playerInfos.Where(x => x.playerScore > 0 && (x.playerState == PlayerState.Game || x.playerState == PlayerState.Room)).ToList(); } break; } } } break; case CommandType.Disconnect: { InGameOnlineController.Instance.needToSendUpdates = false; if (msg.LengthBytes > 3) { string reason = msg.ReadString(); PopAllViewControllers(); InGameOnlineController.Instance.DestroyPlayerControllers(); PreviewPlayer.CrossfadeToDefault(); joined = false; _radioNavController.DisplayError(reason); } else { _radioNavController.DisplayError("ServerHub refused connection!"); } } break; } } catch (Exception e) { Misc.Logger.Exception($"Unable to parse packet! Packet={commandType}, DataLength={msg.LengthBytes}\nException: {e}"); } } }
private void MessageReceived(NetIncomingMessage msg) { CommandType commandType = (CommandType)msg.ReadByte(); if (!joined) { if (commandType == CommandType.JoinChannel) { switch (msg.ReadByte()) { case 0: { Client.Instance.playerInfo.playerState = PlayerState.Room; Client.Instance.RequestChannelInfo(channelId); Client.Instance.SendPlayerInfo(); joined = true; InGameOnlineController.Instance.needToSendUpdates = true; } break; case 1: { _radioNavController.DisplayError("Unable to join channel!\nChannel not found"); } break; default: { _radioNavController.DisplayError("Unable to join channel!\nUnknown error"); } break; } } } else { try { switch (commandType) { case CommandType.GetChannelInfo: { channelInfo = new ChannelInfo(msg); channelInfo.ip = ip; channelInfo.port = port; UpdateUI(channelInfo.state); } break; case CommandType.SetSelectedSong: { if (msg.LengthBytes > 16) { SongInfo song = new SongInfo(msg); channelInfo.currentSong = song; channelInfo.state = ChannelState.NextSong; UpdateUI(channelInfo.state); } } break; case CommandType.GetSongDuration: { SongInfo requestedSong = new SongInfo(msg); LevelSO level = SongLoader.CustomLevelCollectionSO.levels.FirstOrDefault(x => x.levelID.StartsWith(channelInfo.currentSong.levelId)); if (level != null) { SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)level, (loadedLevel) => { Client.Instance.SendSongDuration(new SongInfo() { songName = $"{level.songName} {level.songSubName}", levelId = level.levelID.Substring(0, Math.Min(32, level.levelID.Length)), songDuration = level.audioClip.length }); }); } } break; case CommandType.StartLevel: { if (Client.Instance.playerInfo.playerState == PlayerState.DownloadingSongs) { return; } if (nextSongSkipped) { nextSongSkipped = false; channelInfo.state = ChannelState.InGame; UpdateUI(channelInfo.state); return; } byte difficulty = msg.ReadByte(); SongInfo songInfo = new SongInfo(msg); LevelSO level = _levelCollection.GetLevelsWithBeatmapCharacteristic(_standardCharacteristics).First(x => x.levelID.StartsWith(songInfo.levelId)); SongLoader.Instance.LoadAudioClipForLevel((CustomLevel)level, (levelLoaded) => { StartLevel(levelLoaded, (BeatmapDifficulty)difficulty); }); } break; case CommandType.LeaveRoom: { LeaveChannel(); } break; case CommandType.UpdatePlayerInfo: { if (channelInfo != null) { currentTime = msg.ReadFloat(); totalTime = msg.ReadFloat(); switch (channelInfo.state) { case ChannelState.InGame: UpdateInGameScreen(); break; case ChannelState.NextSong: UpdateNextSongScreen(); break; case ChannelState.Results: { UpdateResultsScreen(); int playersCount = msg.ReadInt32(); List <PlayerInfo> playerInfos = new List <PlayerInfo>(); for (int j = 0; j < playersCount; j++) { try { playerInfos.Add(new PlayerInfo(msg)); } catch (Exception e) { #if DEBUG Misc.Logger.Exception($"Unable to parse PlayerInfo! Excpetion: {e}"); #endif } } playerInfos = playerInfos.Where(x => x.playerScore > 0 && (x.playerState == PlayerState.Game || x.playerState == PlayerState.Room)).ToList(); } break; } } } break; case CommandType.Disconnect: { InGameOnlineController.Instance.needToSendUpdates = false; if (msg.LengthBytes > 3) { string reason = msg.ReadString(); PopAllViewControllers(); InGameOnlineController.Instance.DestroyAvatars(); PreviewPlayer.CrossfadeToDefault(); joined = false; _radioNavController.DisplayError(reason); } else { _radioNavController.DisplayError("ServerHub refused connection!"); } } break; } } catch (Exception e) { Misc.Logger.Exception("Unable to parse packet!"); if (msg != null) { Misc.Logger.Exception($"Packet={commandType}, DataLength={msg.LengthBytes}"); } Misc.Logger.Exception(e.ToString()); } } }