private void OnReliableReceiveCallback(byte[] payload, int payloadSize)
        {
            //Extract Header Type
            MessageType type = (MessageType)BitConverter.ToInt16(payload, 0);

            if (type == MessageType.Chat)
            {
                var msg = StructTools.RawDeserialize <ChatMessage>(payload, HEADER_OFFSET);
                OnReceiveChatMessage?.Invoke(msg);
            }

            if (type == MessageType.Entity)
            {
                Debug.Log($"Entity: {payloadSize - 2}");
                var entity = StructTools.RawDeserialize <Entity>(payload, 2);
                OnEntityReceived?.Invoke(entity);
            }
        }
Beispiel #2
0
        public void OnChatPacket(PacketReader reader)
        {
            int messageNum = reader.ReadInt16();

            reader.ReadString(4);   // Language

            byte[] packet = reader.GetData();

            switch (messageNum)
            {
            case 0x25:
            {
                int    messageType = reader.ReadInt16();
                string username    = reader.ReadUnicodeString();
                string message     = reader.ReadUnicodeString();

                Match matches = Regex.Match(message, "{(.*)}\\s+(.*)");

                if (matches.Success)
                {
                    string channel = matches.Groups[1].Value;
                    string text    = matches.Groups[2].Value;

                    Messages.Add(new ChatMessage {
                            Username = username, Channel = channel, Text = text
                        });
                    ChatMessageEvent?.Invoke(username, channel, text);
                }

                break;
            }

            case 0x3e8:
            {
                string channel = reader.ReadUnicodeString();

                if (!Channels.Contains(channel))
                {
                    Channels.Add(channel);
                    ChannelCreatedEvent?.Invoke(channel);
                }

                break;
            }

            case 0x3e9:
            {
                string channel = reader.ReadUnicodeString();

                if (Channels.Contains(channel))
                {
                    Channels.Remove(channel);
                    ChannelRemovedEvent?.Invoke(channel);
                }

                break;
            }

            case 0x3ee:
            {
                reader.ReadInt16();
                string userName = reader.ReadUnicodeString();

                if (!Users.Contains(userName))
                {
                    Users.Add(userName);
                    UserJoinedEvent?.Invoke(userName, string.Empty);
                }

                break;
            }

            case 0x3ef:
            {
                string userName = reader.ReadUnicodeString();

                if (Users.Contains(userName))
                {
                    Users.Remove(userName);
                    UserLeftEvent?.Invoke(userName, string.Empty);
                }

                break;
            }

            case 0x3f1:
            {
                CurrentChannel = reader.ReadUnicodeString();
                JoinedChatChannelEvent?.Invoke(CurrentChannel);
                break;
            }

            case 0x3f0:
            {
                Users.Clear();
                ClearUsersEvent?.Invoke();
                break;
            }

            case 0x3f4:
            {
                LeftChatChannelEvent?.Invoke(CurrentChannel);
                CurrentChannel = null;
                break;
            }

            // ReSharper disable once RedundantEmptySwitchSection
            default:
                break;
            }
        }
    public static void Init()
    {
        CallResults = new Dictionary <Type, Action <SteamAPICall_t> >();
        CallResults.Add(typeof(LobbyMatchList_t), (SteamAPICall_t cb) => lobbyList.Set(cb));
        CallResults.Add(typeof(LobbyEnter_t), (SteamAPICall_t cb) => lobbyEnter.Set(cb));
        CallResults.Add(typeof(LobbyCreated_t), (SteamAPICall_t cb) => lobbyCreate.Set(cb));
        CallResults.Add(typeof(PersonaStateChange_t), (SteamAPICall_t cb) => personaStateChange.Set(cb));


        //init callbacks
        lobbyList = CallResult <LobbyMatchList_t> .Create((cb, failure) =>
        {
            if (failure)
            {
                    #if UNITY_EDITOR
                Debug.Log("Failed to received lobby list");
                    #endif
                return;
            }

            if (LobbyListEvent != null)
            {
                LobbyListEvent.Invoke(cb);
            }
        });

        lobbyEnter = CallResult <LobbyEnter_t> .Create((cb, failure) =>
        {
            if (failure)
            {
                    #if UNITY_EDITOR
                Debug.Log("Failed to enter the specified lobby");
                    #endif
                return;
            }

            if (LobbyEnterEvent != null)
            {
                LobbyEnterEvent.Invoke(cb);
            }
        });

        lobbyCreate = CallResult <LobbyCreated_t> .Create((cb, failure) =>
        {
            if (failure)
            {
                    #if UNITY_EDITOR
                Debug.Log("Failed to create a lobby");
                    #endif
                return;
            }

                #if UNITY_EDITOR
            Debug.Log("Lobby creation resulted with: " + cb.m_eResult.ToString());
                #endif

            if (LobbyCreateEvent != null)
            {
                LobbyCreateEvent.Invoke(cb);
            }
        });

        personaStateChange = CallResult <PersonaStateChange_t> .Create((cb, failure) =>
        {
            if (PersonaStateChangeEvent != null)
            {
                PersonaStateChangeEvent.Invoke(cb);
            }
        });

        chatMessage = Callback <LobbyChatMsg_t> .Create(cb =>
        {
            if (ChatMessageEvent != null)
            {
                ChatMessageEvent.Invoke(cb);
            }
        });

        chatUpdate = Callback <LobbyChatUpdate_t> .Create(cb =>
        {
            if (ChatUpdateEvent != null)
            {
                ChatUpdateEvent.Invoke(cb);
            }
        });

        lobbyInvite = Callback <GameLobbyJoinRequested_t> .Create(cb =>
        {
            if (LobbyInviteEvent != null)
            {
                LobbyInviteEvent.Invoke(cb);
            }
        });

        lobbyDataUpdate = Callback <LobbyDataUpdate_t> .Create(cb =>
        {
            if (LobbyDataUpdateEvent != null)
            {
                LobbyDataUpdateEvent.Invoke(cb);
            }
        });

        sessionRequest = Callback <P2PSessionRequest_t> .Create(cb =>
        {
            if (AcceptP2PEvent != null)
            {
                AcceptP2PEvent.Invoke(cb);
            }
        });

        p2pFail = Callback <P2PSessionConnectFail_t> .Create(cb =>
        {
            if (P2PFailEvent != null)
            {
                P2PFailEvent.Invoke(cb);
            }
        });
    }