public byte[] SerializePlayerInfo() { var writer = new ByteWriter(); writer.WriteInt32(id); writer.WriteString(Username); writer.WriteInt32(Latency); writer.WriteByte((byte)type); writer.WriteByte((byte)status); writer.WriteULong(steamId); writer.WriteString(steamPersonaName); writer.WriteInt32(ticksBehind); writer.WriteBool(simulating); writer.WriteByte(color.r); writer.WriteByte(color.g); writer.WriteByte(color.b); return(writer.ToArray()); }
public void HandleCursor(ByteReader data) { if (Player.lastCursorTick == Server.net.NetTimer) { return; } var writer = new ByteWriter(); byte seq = data.ReadByte(); byte map = data.ReadByte(); writer.WriteInt32(Player.id); writer.WriteByte(seq); writer.WriteByte(map); if (map < byte.MaxValue) { byte icon = data.ReadByte(); short x = data.ReadShort(); short z = data.ReadShort(); writer.WriteByte(icon); writer.WriteShort(x); writer.WriteShort(z); short dragX = data.ReadShort(); writer.WriteShort(dragX); if (dragX != -1) { short dragZ = data.ReadShort(); writer.WriteShort(dragZ); } } Player.lastCursorTick = Server.net.NetTimer; Server.SendToAll(Packets.Server_Cursor, writer.ToArray(), reliable: false, excluding: Player); }
public void SendPlayerList() { var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.List); writer.WriteInt32(Server.PlayingPlayers.Count()); foreach (var player in Server.PlayingPlayers) { writer.WriteRaw(player.SerializePlayerInfo()); } conn.Send(Packets.Server_PlayerList, writer.ToArray()); }
public void HandleClientUsername(ByteReader data) { if (connection.username != null && connection.username.Length != 0) { return; } string username = data.ReadString(); if (username.Length < 3 || username.Length > 15) { Player.Disconnect(MpDisconnectReason.UsernameLength); return; } if (!Player.IsArbiter && !UsernamePattern.IsMatch(username)) { Player.Disconnect(MpDisconnectReason.UsernameChars); return; } if (Server.GetPlayer(username) != null) { Player.Disconnect(MpDisconnectReason.UsernameAlreadyOnline); return; } connection.username = username; Server.SendNotification("MpPlayerConnected", Player.Username); Server.SendChat($"{Player.Username} has joined."); if (!Player.IsArbiter) { if (!givenColors.TryGetValue(username, out ColorRGB color)) { givenColors[username] = color = PlayerColors[givenColors.Count % PlayerColors.Length]; } Player.color = color; } var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.Add); writer.WriteRaw(Player.SerializePlayerInfo()); Server.SendToAll(Packets.Server_PlayerList, writer.ToArray()); SendWorldData(); }
public void HandleDefs(ByteReader data) { var count = data.ReadInt32(); if (count > 512) { Player.Disconnect(MpDisconnectReason.Generic); return; } var response = new ByteWriter(); var disconnect = false; for (int i = 0; i < count; i++) { var defType = data.ReadString(128); var defCount = data.ReadInt32(); var defHash = data.ReadInt32(); var status = DefCheckStatus.OK; if (!Server.defInfos.TryGetValue(defType, out DefInfo info)) { status = DefCheckStatus.Not_Found; } else if (info.count != defCount) { status = DefCheckStatus.Count_Diff; } else if (info.hash != defHash) { status = DefCheckStatus.Hash_Diff; } if (status != DefCheckStatus.OK) { disconnect = true; } response.WriteByte((byte)status); } if (disconnect) { Player.Disconnect(MpDisconnectReason.Defs, response.ToArray()); return; } connection.Send(Packets.Server_DefsOK, Server.settings.gameName, Player.id); }
private void SendLatencies() { var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.Latencies); writer.WriteInt32(PlayingPlayers.Count()); foreach (var player in PlayingPlayers) { writer.WriteInt32(player.Latency); writer.WriteInt32(player.ticksBehind); } SendToAll(Packets.Server_PlayerList, writer.ToArray()); }
public void HandleCursor(ByteReader data) { var writer = new ByteWriter(); byte seq = data.ReadByte(); byte map = data.ReadByte(); writer.WriteInt32(Player.id); writer.WriteByte(seq); writer.WriteByte(map); if (map < byte.MaxValue) { byte icon = data.ReadByte(); short x = data.ReadShort(); short z = data.ReadShort(); writer.WriteByte(icon); writer.WriteShort(x); writer.WriteShort(z); } Server.SendToAll(Packets.Server_Cursor, writer.GetArray(), reliable: false); }
public void HandleClientUsername(ByteReader data) { if (connection.username != null && connection.username.Length != 0) { return; } string username = data.ReadString(); if (username.Length < 3 || username.Length > 15) { Player.Disconnect("MpInvalidUsernameLength"); return; } if (!Player.IsArbiter && !UsernamePattern.IsMatch(username)) { Player.Disconnect("MpInvalidUsernameChars"); return; } if (Server.GetPlayer(username) != null) { Player.Disconnect("MpInvalidUsernameAlreadyPlaying"); return; } connection.username = username; Server.SendNotification("MpPlayerConnected", Player.Username); Server.SendChat($"{Player.Username} has joined."); var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.Add); writer.WriteRaw(Player.SerializePlayerInfo()); Server.SendToAll(Packets.Server_PlayerList, writer.GetArray()); }