public void PostDepacketize(IReadablePacket packet) { if (_depacketizer == null) { throw new InvalidOperationException("No deserializer specified."); } _cells.Clear(); var cellCount = packet.ReadInt32(); for (var i = 0; i < cellCount; ++i) { var cellId = packet.ReadUInt64(); var tree = new Collections.DynamicQuadTree <T>( _maxEntriesPerNode, _minNodeBounds, _boundExtension, _movingBoundMultiplier, _packetizer, _depacketizer); packet.ReadPacketizableInto(tree); _cells.Add(cellId, tree); } _entryBounds.Clear(); var entryCount = packet.ReadInt32(); for (var i = 0; i < entryCount; ++i) { var key = _depacketizer(packet); TRectangle value; packet.Read(out value); _entryBounds[key] = value; } }
public void Depacketize(IReadablePacket packet) { ComponentsToDisable.Clear(); var componentCount = packet.ReadInt32(); for (var i = 0; i < componentCount; i++) { ComponentsToDisable.Add(packet.ReadInt32()); } }
/// <summary>Reads a rectangle value.</summary> /// <param name="packet">The packet.</param> /// <returns>The read value.</returns> /// <exception cref="PacketException">The packet has not enough available data for the read operation.</exception> public static Rectangle ReadRectangle(this IReadablePacket packet) { Rectangle result; result.X = packet.ReadInt32(); result.Y = packet.ReadInt32(); result.Width = packet.ReadInt32(); result.Height = packet.ReadInt32(); return(result); }
public void Depacketize(IReadablePacket packet) { Targets.Clear(); var targetCount = packet.ReadInt32(); for (var i = 0; i < targetCount; i++) { Targets.Add(packet.ReadInt32()); } }
public void Depacketize(IReadablePacket packet) { _reusableIds.Clear(); var reusableIdCount = packet.ReadInt32(); for (var i = 0; i < reusableIdCount; i++) { _reusableIds.Add(packet.ReadInt32()); } }
public void Depacketize(IReadablePacket packet) { _cellSpawns.Clear(); var spawnCount = packet.ReadInt32(); for (var i = 0; i < spawnCount; i++) { var id = packet.ReadUInt64(); var count = packet.ReadInt32(); _cellSpawns.Add(Tuple.Create(id, count)); } }
public void Depacketize(IReadablePacket packet) { _cooldowns.Clear(); var cooldownsCount = packet.ReadInt32(); for (var i = 0; i < cooldownsCount; i++) { var key = packet.ReadInt32(); var value = packet.ReadInt32(); _cooldowns.Add(key, value); } }
/// <summary>Reads a far value.</summary> /// <param name="packet">The packet to read from.</param> /// <returns>The read value.</returns> /// <exception cref="PacketException">The packet has not enough available data for the read operation.</exception> public static FarValue ReadFarValue(this IReadablePacket packet) { var segment = packet.ReadInt32(); var offset = packet.ReadSingle(); return(new FarValue(segment, offset)); }
public void Depacketize(IReadablePacket packet) { for (var i = 0; i < _squadIds.Count; i++) { var id = packet.ReadInt32(); var data = _squads[id] = new SquadData(); data.Formation = packet.ReadPacketizableWithTypeInfo <AbstractFormation>(); data.Cache = new FormationCache(data.Formation); data.Spacing = packet.ReadSingle(); var count = packet.ReadInt32(); for (var j = 0; j < count; j++) { data.Members.Add(packet.ReadInt32()); } } }
public void Depacketize(IReadablePacket packet) { _changedShape.Clear(); var changedShapeCount = packet.ReadInt32(); for (var i = 0; i < changedShapeCount; i++) { _changedShape.Add(packet.ReadInt32()); } _changedMass.Clear(); var changedMassCount = packet.ReadInt32(); for (var i = 0; i < changedMassCount; i++) { _changedMass.Add(packet.ReadInt32()); } }
public void Depacketize(IReadablePacket packet) { _items.Clear(); var capacity = packet.ReadInt32(); _items.Capacity = capacity; for (var i = 0; i < capacity; i++) { _items.Add(0); } var itemCount = packet.ReadInt32(); for (var i = 0; i < itemCount; i++) { var index = packet.ReadInt32(); _items[index] = packet.ReadInt32(); } }
public void Depacketize(IReadablePacket packet) { // Unwrap the trailing state and mirror it to all the newer ones. packet.ReadPacketizableInto(_simulations[_simulations.Length - 1]); MirrorSimulation(_simulations[_simulations.Length - 1], _simulations.Length - 2); // Find adds / removes / commands that our out of date now, but keep // newer ones (that might be locally generated). PrunePastEvents(); // Continue with reading the list of removes. var removeCount = packet.ReadInt32(); for (var removeIndex = 0; removeIndex < removeCount; ++removeIndex) { var key = packet.ReadInt64(); if (!_removes.ContainsKey(key)) { _removes.Add(key, new List <int>()); } var valueCount = packet.ReadInt32(); for (var valueIndex = 0; valueIndex < valueCount; ++valueIndex) { _removes[key].Add(packet.ReadInt32()); } } // And finally the commands. var commandCount = packet.ReadInt32(); for (var commandIndex = 0; commandIndex < commandCount; ++commandIndex) { var key = packet.ReadInt64(); if (!_commands.ContainsKey(key)) { _commands.Add(key, new List <Command>()); } _commands[key].AddRange(packet.ReadPacketizablesWithTypeInfo <Command>()); } // Got a valid state. WaitingForSynchronization = false; }
public void Depacketize(IReadablePacket packet) { var attributeCount = packet.ReadInt32(); for (var i = 0; i < attributeCount; i++) { var type = (AttributeType)packet.ReadByte(); var value = packet.ReadSingle(); Attributes.Add(type, value); } Projectiles = packet.ReadPacketizables <ProjectileFactory>(); }
/// <summary> /// Corresponds to <c>PacketizeLocal</c>, only reads own data and leaves the base class alone. /// </summary> /// <param name="packet">The packet to read from.</param> public void DepacketizeLocal(IReadablePacket packet) { var baseAttributeCount = packet.ReadInt32(); for (var i = 0; i < baseAttributeCount; i++) { var key = (TAttribute)Enum.Parse(typeof(TAttribute), packet.ReadString()); var value = packet.ReadSingle(); _baseAttributes[key] = value; } var modifiedAttributeCount = packet.ReadInt32(); for (var i = 0; i < modifiedAttributeCount; i++) { var key = (TAttribute)Enum.Parse(typeof(TAttribute), packet.ReadString()); var values = new float[2]; values[0] = packet.ReadSingle(); values[1] = packet.ReadSingle(); _modifiedAttributes[key] = values; } }
public void Depacketize(IReadablePacket packet) { _cellInfo.Clear(); var cellCount = packet.ReadInt32(); for (var i = 0; i < cellCount; i++) { var key = packet.ReadUInt64(); var value = packet.ReadPacketizable <CellInfo>(); _cellInfo.Add(key, value); } }
public void PostDepacketize(IReadablePacket packet) { // Restore entity objects (which we use for faster lookup of components // on an entity). This in particular re-creates empty entities, i.e. // entities with no components. foreach (var entityId in _entityIds) { _entities[entityId] = AllocateEntity(); } // Read back all components. var componentCount = packet.ReadInt32(); for (var i = 0; i < componentCount; ++i) { var type = packet.ReadType(); var component = AllocateComponent(type); packet.ReadPacketizableInto(component); component.Manager = this; _components[component.Id] = component; _entities[component.Entity].Add(component); } // Read back all systems. var systemCount = packet.ReadInt32(); for (var i = 0; i < systemCount; ++i) { var type = packet.ReadType(); var instance = _systemsByTypeId[GetSystemTypeId(type)]; Debug.Assert(instance != null); packet.ReadPacketizableInto(instance); } // All done, send message to allow post-processing, e.g. for fetching // their components again, which is why this has to come last. Initialize message; SendMessage(message); }
public void Depacketize(IReadablePacket packet) { foreach (var tree in _trees.Where(tree => tree != null)) { tree.Clear(); } foreach (var changed in _changed.Where(changed => changed != null)) { changed.Clear(); } var indexCount = packet.ReadInt32(); for (var i = 0; i < indexCount; ++i) { var index = packet.ReadInt32(); if (_trees[index] == null) { #if FARMATH _trees[index] = new FarCollections.SpatialHashedQuadTree <int>(_maxEntriesPerNode, _minNodeBounds, 0.1f, 2f, (p, v) => p.Write(v), p => p.ReadInt32()); #else _trees[index] = new DynamicQuadTree <int>(_maxEntriesPerNode, _minNodeBounds, 0.1f, 2f, (p, v) => p.Write(v), p => p.ReadInt32()); #endif } if (_changed[index] == null) { _changed[index] = new HashSet <int>(); } packet.ReadPacketizableInto(_trees[index]); var changedCount = packet.ReadInt32(); for (var j = 0; j < changedCount; ++j) { _changed[index].Add(packet.ReadInt32()); } } }
public void Depacketize(IReadablePacket packet) { _newCollisions.Clear(); var newCollisionCount = packet.ReadInt32(); for (var i = 0; i < newCollisionCount; ++i) { var key = packet.ReadUInt64(); var value = packet.ReadPacketizable <NewCollisionInfo>(); _newCollisions.Add(key, value); } _activeCollisions.Clear(); var activeCollisionCount = packet.ReadInt32(); for (var i = 0; i < activeCollisionCount; ++i) { var key = packet.ReadUInt64(); var value = packet.ReadPacketizable <ActiveCollisionInfo>(); _activeCollisions.Add(key, value); } }
public void Depacketize(IReadablePacket packet) { _livingCells.Clear(); var livingCellCount = packet.ReadInt32(); for (var i = 0; i < livingCellCount; i++) { _livingCells.Add(packet.ReadUInt64()); } _pendingCells.Clear(); var pendingCellCount = packet.ReadInt32(); for (var i = 0; i < pendingCellCount; i++) { var cell = packet.ReadUInt64(); var frame = packet.ReadInt64(); _pendingCells.Add(cell, frame); } _livingSubCells.Clear(); var livingSubCellCount = packet.ReadInt32(); for (var i = 0; i < livingSubCellCount; i++) { _livingSubCells.Add(packet.ReadUInt64()); } _pendingSubCells.Clear(); var pendingSubCellCount = packet.ReadInt32(); for (var i = 0; i < pendingSubCellCount; i++) { var cell = packet.ReadUInt64(); var frame = packet.ReadInt64(); _pendingSubCells.Add(cell, frame); } }
public void Depacketize(IReadablePacket packet) { _currentBehaviors.Clear(); var behaviorCount = packet.ReadInt32(); for (var i = 0; i < behaviorCount; i++) { _currentBehaviors.Push((BehaviorType)packet.ReadByte()); } foreach (var behaviorType in _behaviors.Keys) { var behavior = _behaviors[behaviorType]; packet.ReadPacketizableInto(behavior); } }
private static T[] ReadPacketizables <T>(this IReadablePacket packet, Func <IReadablePacket, T> reader) where T : class { var packetizableCount = packet.ReadInt32(); if (packetizableCount < 0) { return(null); } var result = new T[packetizableCount]; for (var i = 0; i < packetizableCount; i++) { result[i] = reader(packet); } return(result); }
public static IReadablePacket Read(this IReadablePacket packet, out int data) { data = packet.ReadInt32(); return(packet); }
/// <summary>Got some data via our TCP connection, so it's from the server.</summary> private void HandleTcpData(SessionMessage type, IReadablePacket packet) { switch (type) { case SessionMessage.JoinResponse: // Got a reply from a server for a join response. if (ConnectionState == ClientState.Connecting) { // Get our number. _localPlayerNumber = packet.ReadInt32(); // Get info about other players in the session. PlayerCount = packet.ReadInt32(); // Maximum number of players in the session? MaxPlayers = packet.ReadInt32(); // Sanity checks. if (_localPlayerNumber < 0 || PlayerCount < 0 || MaxPlayers < 0 || MaxPlayers < PlayerCount || _localPlayerNumber >= MaxPlayers) { throw new PacketException("Inconsistent session info."); } // Allocate array for the players in the session. Players = new Player[MaxPlayers]; // Get other game relevant data. using (var joinData = packet.ReadPacketizable <Packet>()) { // Get info on players already in the session, including us. for (var i = 0; i < PlayerCount; i++) { // Get player number. var playerNumber = packet.ReadInt32(); // Sanity checks. if (playerNumber < 0 || playerNumber >= MaxPlayers || Players[playerNumber] != null) { throw new PacketException("Invalid player number."); } // Get player name. var playerName = packet.ReadString(); // Get additional player data. var playerData = packet.ReadPacketizable <TPlayerData>(); // All OK, add the player. Players[playerNumber] = new Player(playerNumber, playerName, playerData); } // New state :) ConnectionState = ClientState.Connected; if (_tcp != null) { Logger.Debug( "Successfully joined game at '{0}'.", (IPEndPoint)_tcp.Client.RemoteEndPoint); } else { Logger.Debug("Successfully joined local game."); } // OK, let the program know. OnJoinResponse(new JoinResponseEventArgs(joinData)); } // Also, fire one join event for each player in the game. Except for // the local player, because that'll likely need special treatment anyway. foreach (var player in AllPlayers) { if (!player.Equals(LocalPlayer)) { OnPlayerJoined(new PlayerEventArgs(player)); } } } break; case SessionMessage.PlayerJoined: // Some player joined the session. if (ConnectionState == ClientState.Connected) { // Get player number. var playerNumber = packet.ReadInt32(); // Sanity checks. if (playerNumber < 0 || playerNumber >= MaxPlayers || Players[playerNumber] != null) { throw new PacketException("Invalid player number."); } // Get player name. var playerName = packet.ReadString(); // Get additional player data. var playerData = packet.ReadPacketizable <TPlayerData>(); // All OK, add the player. Players[playerNumber] = new Player(playerNumber, playerName, playerData); // The the local program about it. OnPlayerJoined(new PlayerEventArgs(Players[playerNumber])); } break; case SessionMessage.PlayerLeft: // Some player left the session. if (ConnectionState == ClientState.Connected) { // Get player number. var playerNumber = packet.ReadInt32(); // Sanity checks. if (!HasPlayer(playerNumber)) { throw new PacketException("Invalid player number."); } if (playerNumber == _localPlayerNumber) { // We were removed from the game. Reset(); } else { // OK, remove the player. var player = Players[playerNumber]; Players[playerNumber] = null; // Tell the local program about it. OnPlayerLeft(new PlayerEventArgs(player)); } } break; case SessionMessage.Data: // Custom data, just forward it if we're in a session. if (ConnectionState == ClientState.Connected) { OnData(new ClientDataEventArgs(packet, true)); } break; default: // Ignore everything else. Logger.Trace("Unknown SessionMessage via TCP: {0}.", type); break; } }