public static MapPacket FromBytes(byte[] bytes) { using (MemoryStream stream = new MemoryStream(bytes)) { byte[] tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int mapID = BitConverter.ToInt32(tempBytes, 0); stream.Read(tempBytes, 0, sizeof(int)); int dataSize = BitConverter.ToInt32(tempBytes, 0); tempBytes = new byte[dataSize]; stream.Read(tempBytes, 0, dataSize); MapPacket mapPacket = new MapPacket(mapID, MapData.FromBytes(tempBytes)); //add in the map players here tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int enemiesCount = BitConverter.ToInt32(tempBytes, 0); for (int i = 0; i < enemiesCount; i++) { tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int size = BitConverter.ToInt32(tempBytes, 0); tempBytes = new byte[size]; stream.Read(tempBytes, 0, size); MapEnemy enemy = MapEnemy.FromBytes(tempBytes); mapPacket.Enemies.Add(enemy); } tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int projectilesCount = BitConverter.ToInt32(tempBytes, 0); for (int i = 0; i < projectilesCount; i++) { tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int size = BitConverter.ToInt32(tempBytes, 0); tempBytes = new byte[size]; stream.Read(tempBytes, 0, size); MapProjectile projectile = MapProjectile.FromBytes(tempBytes); mapPacket.Projectiles.Add(projectile); } tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int itemsCount = BitConverter.ToInt32(tempBytes, 0); for (int i = 0; i < itemsCount; i++) { tempBytes = new byte[sizeof(int)]; stream.Read(tempBytes, 0, sizeof(int)); int size = BitConverter.ToInt32(tempBytes, 0); tempBytes = new byte[size]; stream.Read(tempBytes, 0, size); MapItem item = MapItem.FromBytes(tempBytes); mapPacket.Items.Add(item); } return(mapPacket); } }