Пример #1
0
        public void PacketReceived(NetIncomingMessage msg)
        {
            if (msg == null)
            {
                if (_currentScene == _gameSceneName && _loaded)
                {
                    PropertyInfo property = typeof(StandardLevelGameplayManager).GetProperty("gameState");
                    property.DeclaringType.GetProperty("gameState");
                    property.GetSetMethod(true).Invoke(_gameManager, new object[] { StandardLevelGameplayManager.GameState.Failed });
                }
                return;
            }
            msg.Position = 0;

            switch ((CommandType)msg.ReadByte())
            {
            case CommandType.UpdatePlayerInfo:
            {
                msg.Position += 64;

                bool fullUpdate = (msg.ReadByte() == 1);

                int playersCount = msg.ReadInt32();

                if (_playerIds == null)
                {
                    _playerIds = new List <ulong>(playersCount);
                }
                else if (_playerIds.Count > playersCount)
                {
                    _playerIds.Clear();
                }

                if (playerScores == null)
                {
                    playerScores = new List <PlayerScore>(playersCount);
                }
                else if (playerScores.Count > playersCount)
                {
                    playerScores.Clear();
                }

                _spectatorInRoom = false;
                for (int i = 0; i < playersCount; i++)
                {
                    ulong playerId = msg.ReadUInt64();

                    if (_playerIds.Count > i)
                    {
                        _playerIds[i] = playerId;
                    }
                    else
                    {
                        _playerIds.Add(playerId);
                    }

                    if (players.TryGetValue(playerId, out OnlinePlayerController player))
                    {
                        if (player == null)
                        {
                            player            = new GameObject("OnlinePlayerController").AddComponent <OnlinePlayerController>();
                            players[playerId] = player;
                        }

                        if (fullUpdate)
                        {
                            PlayerInfo playerInfo = new PlayerInfo(msg);
                            player.noInterpolation = true;
                            player.playerInfo      = playerInfo;
                            player.UpdateInfo(playerInfo.updateInfo);
                            _spectatorInRoom |= playerInfo.updateInfo.playerState == PlayerState.Spectating;
                        }
                        else
                        {
                            PlayerUpdate update = new PlayerUpdate(msg);
                            player.noInterpolation = false;
                            player.UpdateInfo(update);

                            byte hitCount = msg.ReadByte();

                            if (player.playerInfo.hitsLastUpdate.Count > 0)
                            {
                                player.playerInfo.hitsLastUpdate.Clear();
                            }

                            for (int j = 0; j < hitCount; j++)
                            {
                                player.playerInfo.hitsLastUpdate.Add(new HitData(msg));
                            }

                            _spectatorInRoom |= update.playerState == PlayerState.Spectating;
                        }
                    }
                    else
                    {
                        if (fullUpdate)
                        {
                            player = new GameObject("OnlinePlayerController").AddComponent <OnlinePlayerController>();
                            PlayerInfo playerInfo = new PlayerInfo(msg);
                            player.playerInfo = playerInfo;
                            _spectatorInRoom |= playerInfo.updateInfo.playerState == PlayerState.Spectating;
                            players.Add(playerId, player);
                        }
                        else
                        {
                            Plugin.log.Error("Not enough info to create new player controller! Waiting for full update...");
                            sendFullUpdate = true;
                            new PlayerUpdate(msg);
                            byte hitCount = msg.ReadByte();
                            msg.ReadBytes(hitCount * 5);
                        }
                    }

                    if (player != null)
                    {
                        player.SetAvatarState(!Client.Instance.inRadioMode && IsPlayerVisible(player.playerInfo.playerId));
                        player.SetBlocksState(!Client.Instance.inRadioMode && _currentScene == _gameSceneName && Config.Instance.ShowOtherPlayersBlocks && !Client.Instance.playerInfo.Equals(player.playerInfo) && !Config.Instance.SpectatorMode && player.playerInfo.updateInfo.playerState == PlayerState.Game);
                    }
                }

                _playerIds.Sort();

                int  localPlayerIndex   = _playerIds.IndexOf(Client.Instance.playerInfo.playerId);
                bool needToRemovePlayer = false;

                for (int i = 0; i < playerScores.Count; i++)
                {
                    playerScores[i] = default;
                }

                foreach (var pair in players)
                {
                    if (!_playerIds.Contains(pair.Key))
                    {
                        needToRemovePlayer = true;
                    }
                    else
                    {
                        int playerIndex = _playerIds.IndexOf(pair.Key);
                        pair.Value.avatarOffset = (playerIndex - localPlayerIndex) * (_currentScene == _gameSceneName ? 5f : 0f);

                        while (playerScores.Count <= playerIndex)
                        {
                            playerScores.Add(default);
                        }

                        playerScores[playerIndex] = new PlayerScore(pair.Value);
                    }
        public void SetPlayerInfo(PlayerInfo _playerInfo, float offset, bool isLocal)
        {
            if (_playerInfo == default)
            {
                if (playerNameText != null)
                {
                    playerNameText.gameObject.SetActive(false);
                }
                if (playerSpeakerIcon != null)
                {
                    playerSpeakerIcon.gameObject.SetActive(false);
                }
                if (avatar != null && avatar.eventsPlayer != null)
                {
                    avatar.Destroy();
                    avatar = null;
                }
                return;
            }

            try
            {
                playerInfo       = _playerInfo.updateInfo;
                playerId         = _playerInfo.playerId;
                playerAvatarHash = _playerInfo.avatarHash;
                playerName       = _playerInfo.playerName;

                if (playerNameText != null && playerSpeakerIcon != null)
                {
                    if (isLocal)
                    {
                        playerNameText.gameObject.SetActive(false);
                        playerSpeakerIcon.gameObject.SetActive(false);
#if !DEBUG
                        if (avatar != null && avatar.eventsPlayer != null)
                        {
                            avatar.Destroy();
                        }
#endif
                    }
                    else
                    {
                        playerNameText.gameObject.SetActive(true);
                        playerNameText.alignment = TextAlignmentOptions.Center;
                        playerSpeakerIcon.gameObject.SetActive(InGameOnlineController.Instance.VoiceChatIsTalking(playerId));
                    }
                }
                else
                {
                    return;
                }
#if !DEBUG
                if ((avatar == null || currentAvatarHash != playerAvatarHash) && !isLocal)
#else
                if ((avatar == null || currentAvatarHash != playerAvatarHash))
#endif
                {
                    if (ModelSaberAPI.cachedAvatars.ContainsKey(playerAvatarHash))
                    {
                        LoadedAvatar cachedAvatar = ModelSaberAPI.cachedAvatars[playerAvatarHash];

                        if (cachedAvatar != null)
                        {
                            if (avatar != null && avatar.eventsPlayer != null)
                            {
                                avatar.Destroy();
                                avatar = null;
                            }

                            avatar = AvatarManager.SpawnAvatar(cachedAvatar, avatarInput);
                            avatar.SetChildrenToLayer(10);

                            currentAvatarHash = playerAvatarHash;
                        }
                    }
                    else
                    {
                        if (Config.Instance.DownloadAvatars)
                        {
                            ModelSaberAPI.avatarDownloaded -= AvatarDownloaded;
                            ModelSaberAPI.avatarDownloaded += AvatarDownloaded;

                            if (!ModelSaberAPI.queuedAvatars.Contains(playerAvatarHash))
                            {
                                SharedCoroutineStarter.instance.StartCoroutine(ModelSaberAPI.DownloadAvatarCoroutine(playerAvatarHash));

                                if (avatar != null && avatar.eventsPlayer != null)
                                {
                                    avatar.Destroy();
                                }

                                avatar = AvatarManager.SpawnAvatar(defaultAvatarInstance, avatarInput);
                                avatar.SetChildrenToLayer(10);
                            }
                        }
                    }
                }

                Vector3 offsetVector = new Vector3(offset, 0f, 0f);

                avatarInput.headPos      = playerInfo.headPos + offsetVector;
                avatarInput.rightHandPos = playerInfo.rightHandPos + offsetVector;
                avatarInput.leftHandPos  = playerInfo.leftHandPos + offsetVector;

                avatarInput.headRot      = playerInfo.headRot;
                avatarInput.rightHandRot = playerInfo.rightHandRot;
                avatarInput.leftHandRot  = playerInfo.leftHandRot;

                avatarInput.poseValid = true;

                avatarInput.fullBodyTracking = playerInfo.fullBodyTracking;
                if (playerInfo.fullBodyTracking)
                {
                    avatarInput.rightLegPos = playerInfo.rightLegPos + offsetVector;
                    avatarInput.leftLegPos  = playerInfo.leftLegPos + offsetVector;
                    avatarInput.pelvisPos   = playerInfo.pelvisPos + offsetVector;
                    avatarInput.rightLegRot = playerInfo.rightLegRot;
                    avatarInput.leftLegRot  = playerInfo.leftLegRot;
                    avatarInput.pelvisRot   = playerInfo.pelvisRot;
                }

                transform.position = avatarInput.headPos;

                playerNameText.text = playerName;

                if (playerInfo.playerFlags.rainbowName && !rainbowName)
                {
                    playerNameText.color = playerInfo.playerNameColor;
                    nameColor            = HSBColor.FromColor(playerInfo.playerNameColor);
                }
                else if (!playerInfo.playerFlags.rainbowName && playerNameText.color != playerInfo.playerNameColor)
                {
                    playerNameText.color = playerInfo.playerNameColor;
                }

                rainbowName = playerInfo.playerFlags.rainbowName;
            }
            catch (Exception e)
            {
                Plugin.log.Critical(e);
            }
        }
        public void SetPlayerInfo(PlayerInfo _playerInfo, float offset, bool isLocal)
        {
            if (_playerInfo == default)
            {
                if (playerNameText != null)
                {
                    playerNameText.gameObject.SetActive(false);
                }
                if (playerSpeakerIcon != null)
                {
                    playerSpeakerIcon.gameObject.SetActive(false);
                }
                if (avatar != null && avatar.GameObject != null)
                {
                    Destroy(avatar.GameObject);
                }
                return;
            }

            try
            {
                playerInfo       = _playerInfo.updateInfo;
                playerId         = _playerInfo.playerId;
                playerAvatarHash = _playerInfo.avatarHash;
                playerName       = _playerInfo.playerName;

                if (playerNameText != null && playerSpeakerIcon != null)
                {
                    if (isLocal)
                    {
                        playerNameText.gameObject.SetActive(false);
                        playerSpeakerIcon.gameObject.SetActive(false);
#if !DEBUG
                        if (avatar != null)
                        {
                            Destroy(avatar.GameObject);
                        }
#endif
                    }
                    else
                    {
                        playerNameText.gameObject.SetActive(true);
                        playerNameText.alignment = TextAlignmentOptions.Center;
                        playerSpeakerIcon.gameObject.SetActive(InGameOnlineController.Instance.VoiceChatIsTalking(playerId));
                    }
                }
                else
                {
                    return;
                }
#if !DEBUG
                if ((avatar == null || currentAvatarHash != playerAvatarHash) && !isLocal)
#else
                if ((avatar == null || currentAvatarHash != playerAvatarHash))
#endif
                {
                    if (ModelSaberAPI.cachedAvatars.ContainsKey(playerAvatarHash))
                    {
                        CustomAvatar.CustomAvatar cachedAvatar = ModelSaberAPI.cachedAvatars[playerAvatarHash];

                        if (cachedAvatar != null)
                        {
                            if (pendingAvatars.Contains(cachedAvatar))
                            {
                                AvatarLoaded -= AvatarController_AvatarLoaded;
                                AvatarLoaded += AvatarController_AvatarLoaded;
                            }
                            else if (!pendingAvatars.Contains(cachedAvatar) && !cachedAvatar.IsLoaded)
                            {
                                if (avatar != null)
                                {
                                    Destroy(avatar.GameObject);
                                }

                                avatar          = AvatarSpawner.SpawnAvatar(defaultAvatarInstance, this);
                                exclusionScript = avatar.GameObject.GetComponentsInChildren <AvatarScriptPack.FirstPersonExclusion>().FirstOrDefault();
                                if (exclusionScript != null)
                                {
                                    exclusionScript.SetVisible();
                                }

                                pendingAvatars.Add(cachedAvatar);
                                AvatarLoaded -= AvatarController_AvatarLoaded;
                                AvatarLoaded += AvatarController_AvatarLoaded;
                                cachedAvatar.Load((CustomAvatar.CustomAvatar loadedAvatar, AvatarLoadResult result) =>
                                {
                                    if (result == AvatarLoadResult.Completed)
                                    {
                                        pendingAvatars.Remove(ModelSaberAPI.cachedAvatars[playerAvatarHash]);
                                        AvatarLoaded?.Invoke(ModelSaberAPI.cachedAvatars.First(x => x.Value == loadedAvatar).Key);
                                    }
                                });
                            }
                            else
                            {
                                if (avatar != null)
                                {
                                    Destroy(avatar.GameObject);
                                }

                                avatar          = AvatarSpawner.SpawnAvatar(cachedAvatar, this);
                                exclusionScript = avatar.GameObject.GetComponentsInChildren <AvatarScriptPack.FirstPersonExclusion>().FirstOrDefault();
                                if (exclusionScript != null)
                                {
                                    exclusionScript.SetVisible();
                                }

                                currentAvatarHash = playerAvatarHash;
                            }
                        }
                    }
                    else
                    {
                        if (Config.Instance.DownloadAvatars)
                        {
                            if (ModelSaberAPI.queuedAvatars.Contains(playerAvatarHash))
                            {
                                ModelSaberAPI.avatarDownloaded -= AvatarDownloaded;
                                ModelSaberAPI.avatarDownloaded += AvatarDownloaded;
                            }
                            else
                            {
                                ModelSaberAPI.avatarDownloaded -= AvatarDownloaded;
                                ModelSaberAPI.avatarDownloaded += AvatarDownloaded;
                                SharedCoroutineStarter.instance.StartCoroutine(ModelSaberAPI.DownloadAvatarCoroutine(playerAvatarHash));

                                if (avatar != null)
                                {
                                    Destroy(avatar.GameObject);
                                }

                                avatar          = AvatarSpawner.SpawnAvatar(defaultAvatarInstance, this);
                                exclusionScript = avatar.GameObject.GetComponentsInChildren <AvatarScriptPack.FirstPersonExclusion>().FirstOrDefault();
                                if (exclusionScript != null)
                                {
                                    exclusionScript.SetVisible();
                                }
                            }
                        }
                    }
                }

                Vector3 offsetVector = new Vector3(offset, 0f, 0f);

                HeadPos      = playerInfo.headPos + offsetVector;
                RightHandPos = playerInfo.rightHandPos + offsetVector;
                LeftHandPos  = playerInfo.leftHandPos + offsetVector;

                HeadRot      = playerInfo.headRot;
                RightHandRot = playerInfo.rightHandRot;
                LeftHandRot  = playerInfo.leftHandRot;

                if (playerInfo.fullBodyTracking)
                {
                    RightLegPos = playerInfo.rightLegPos + offsetVector;
                    LeftLegPos  = playerInfo.leftLegPos + offsetVector;
                    PelvisPos   = playerInfo.pelvisPos + offsetVector;
                    RightLegRot = playerInfo.rightLegRot;
                    LeftLegRot  = playerInfo.leftLegRot;
                    PelvisRot   = playerInfo.pelvisRot;
                }
                else
                {
                    RightLegPos = new Vector3();
                    LeftLegPos  = new Vector3();
                    PelvisPos   = new Vector3();
                    RightLegRot = new Quaternion();
                    LeftLegRot  = new Quaternion();
                    PelvisRot   = new Quaternion();
                }

                transform.position = HeadPos;

                playerNameText.text = playerName;

                if (playerInfo.playerFlags.rainbowName && !rainbowName)
                {
                    playerNameText.color = playerInfo.playerNameColor;
                    nameColor            = HSBColor.FromColor(playerInfo.playerNameColor);
                }
                else if (!playerInfo.playerFlags.rainbowName && playerNameText.color != playerInfo.playerNameColor)
                {
                    playerNameText.color = playerInfo.playerNameColor;
                }

                rainbowName = playerInfo.playerFlags.rainbowName;
            }
            catch (Exception e)
            {
                Plugin.log.Critical(e);
            }
        }