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 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); }
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 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); return(writer.ToArray()); }
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) { 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); }
private void SendWorldData() { int factionId = MultiplayerServer.instance.coopFactionId; MultiplayerServer.instance.playerFactions[connection.username] = factionId; /*if (!MultiplayerServer.instance.playerFactions.TryGetValue(connection.Username, out int factionId)) * { * factionId = MultiplayerServer.instance.nextUniqueId++; * MultiplayerServer.instance.playerFactions[connection.Username] = factionId; * * byte[] extra = ByteWriter.GetBytes(factionId); * MultiplayerServer.instance.SendCommand(CommandType.SETUP_FACTION, ScheduledCommand.NoFaction, ScheduledCommand.Global, extra); * }*/ if (Server.PlayingPlayers.Count(p => p.FactionId == factionId) == 1) { byte[] extra = ByteWriter.GetBytes(factionId); MultiplayerServer.instance.SendCommand(CommandType.FactionOnline, ScheduledCommand.NoFaction, ScheduledCommand.Global, extra); } ByteWriter writer = new ByteWriter(); writer.WriteInt32(factionId); writer.WriteInt32(MultiplayerServer.instance.gameTimer); writer.WritePrefixedBytes(MultiplayerServer.instance.savedGame); writer.WriteInt32(MultiplayerServer.instance.mapCmds.Count); foreach (var kv in MultiplayerServer.instance.mapCmds) { int mapId = kv.Key; //MultiplayerServer.instance.SendCommand(CommandType.CreateMapFactionData, ScheduledCommand.NoFaction, mapId, ByteWriter.GetBytes(factionId)); List <byte[]> mapCmds = kv.Value; writer.WriteInt32(mapId); writer.WriteInt32(mapCmds.Count); foreach (var arr in mapCmds) { writer.WritePrefixedBytes(arr); } } writer.WriteInt32(MultiplayerServer.instance.mapData.Count); foreach (var kv in MultiplayerServer.instance.mapData) { int mapId = kv.Key; byte[] mapData = kv.Value; writer.WriteInt32(mapId); writer.WritePrefixedBytes(mapData); } connection.State = ConnectionStateEnum.ServerPlaying; byte[] packetData = writer.ToArray(); connection.SendFragmented(Packets.Server_WorldData, packetData); Player.SendPlayerList(); MpLog.Log("World response sent: " + packetData.Length); }
public bool DoAutosave(string saveName = "") { if (tmpMapCmds != null) { return(false); } if (settings.pauseOnAutosave) { SendCommand(CommandType.WorldTimeSpeed, ScheduledCommand.NoFaction, ScheduledCommand.Global, new byte[] { (byte)Verse.TimeSpeed.Paused }); } ByteWriter writer = new ByteWriter(); writer.WriteString(saveName); SendCommand(CommandType.Autosave, ScheduledCommand.NoFaction, ScheduledCommand.Global, writer.ToArray()); tmpMapCmds = new Dictionary <int, List <byte[]> >(); SendChat("Autosaving..."); autosaveCountdown = settings.autosaveInterval * 2500 * 24; return(true); }