private InventoryDeltaInformation WriteInventory(ref InventoryDeltaInformation packetInfo, BitStream stream, byte packetId, int maxBitPosition, out bool needsSplit) { InventoryDeltaInformation sendPacketInfo = PrepareSendData(ref packetInfo, stream, maxBitPosition, out needsSplit); if (sendPacketInfo.HasChanges == false) { stream.WriteBool(false); return(sendPacketInfo); } sendPacketInfo.MessageId = packetInfo.MessageId; stream.WriteBool(true); stream.WriteUInt32(sendPacketInfo.MessageId); stream.WriteBool(sendPacketInfo.ChangedItems != null); if (sendPacketInfo.ChangedItems != null) { stream.WriteInt32(sendPacketInfo.ChangedItems.Count); foreach (var item in sendPacketInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); } } stream.WriteBool(sendPacketInfo.RemovedItems != null); if (sendPacketInfo.RemovedItems != null) { stream.WriteInt32(sendPacketInfo.RemovedItems.Count); foreach (var item in sendPacketInfo.RemovedItems) { stream.WriteUInt32(item); } } stream.WriteBool(sendPacketInfo.NewItems != null); if (sendPacketInfo.NewItems != null) { stream.WriteInt32(sendPacketInfo.NewItems.Count); foreach (var item in sendPacketInfo.NewItems) { stream.WriteInt32(item.Key); MyPhysicalInventoryItem itemTosend = item.Value; VRage.Serialization.MySerializer.Write(stream, ref itemTosend, MyObjectBuilderSerializer.Dynamic); } } stream.WriteBool(sendPacketInfo.SwappedItems != null); if (sendPacketInfo.SwappedItems != null) { stream.WriteInt32(sendPacketInfo.SwappedItems.Count); foreach (var item in sendPacketInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); } } return(sendPacketInfo); }
InventoryDeltaInformation WriteInventory(ref InventoryDeltaInformation packetInfo, BitStream stream, byte packetId, int maxBitPosition, out bool needsSplit) { var delta = PrepareSendData(ref packetInfo, stream, maxBitPosition, out needsSplit); delta.MessageId = packetInfo.MessageId; stream.WriteBool(delta.HasChanges); stream.WriteUInt32(delta.MessageId); if (!delta.HasChanges) { return(delta); } stream.WriteBool(delta.ChangedItems != null); if (delta.ChangedItems != null) { stream.WriteInt32(delta.ChangedItems.Count); foreach (var keyValuePair in delta.ChangedItems) { stream.WriteUInt32(keyValuePair.Key); stream.WriteInt64(keyValuePair.Value.RawValue); } } stream.WriteBool(delta.RemovedItems != null); if (delta.RemovedItems != null) { stream.WriteInt32(delta.RemovedItems.Count); foreach (var value in delta.RemovedItems) { stream.WriteUInt32(value); } } stream.WriteBool(delta.NewItems != null); if (delta.NewItems != null) { stream.WriteInt32(delta.NewItems.Count); foreach (var keyValuePair2 in delta.NewItems) { stream.WriteInt32(keyValuePair2.Key); var value2 = keyValuePair2.Value; MySerializer.Write(stream, ref value2, MyObjectBuilderSerializer.Dynamic); } } stream.WriteBool(delta.SwappedItems != null); if (delta.SwappedItems != null) { stream.WriteInt32(delta.SwappedItems.Count); foreach (var keyValuePair3 in delta.SwappedItems) { stream.WriteUInt32(keyValuePair3.Key); stream.WriteInt32(keyValuePair3.Value); } } return(delta); }
public bool Serialize(BitStream stream, EndpointId forClient, uint timestamp, byte packetId, int maxBitPosition) { if (stream.Writing) { InventoryClientData clientData = m_clientInventoryUpdate[forClient.Value]; bool needsSplit = false; if (clientData.FailedIncompletePackets.Count > 0) { InventoryDeltaInformation failedPacket = clientData.FailedIncompletePackets[0]; clientData.FailedIncompletePackets.RemoveAtFast(0); InventoryDeltaInformation reSendPacket = WriteInventory(ref failedPacket, stream, packetId, maxBitPosition, out needsSplit); if (needsSplit) { //resend split doesnt generate new id becaose it was part of allreadt sent message clientData.FailedIncompletePackets.Add(CreateSplit(ref failedPacket, ref reSendPacket)); } if (reSendPacket.HasChanges) { clientData.SendPackets[packetId] = reSendPacket; } } else { InventoryDeltaInformation difference = CalculateInventoryDiff(ref clientData); difference.MessageId = clientData.CurrentMessageId; clientData.MainSendingInfo = WriteInventory(ref difference, stream, packetId, maxBitPosition, out needsSplit); if (needsSplit) { //split generate new id becaose its different message clientData.CurrentMessageId++; InventoryDeltaInformation split = CreateSplit(ref difference, ref clientData.MainSendingInfo); split.MessageId = clientData.CurrentMessageId; clientData.FailedIncompletePackets.Add(split); } if (clientData.MainSendingInfo.HasChanges) { clientData.SendPackets[packetId] = clientData.MainSendingInfo; clientData.CurrentMessageId++; } clientData.Dirty = false; } } else { ReadInventory(stream); } return(true); }
static void ApplyChangesToClientItems(InventoryClientData clientData, ref InventoryDeltaInformation delta) { if (delta.RemovedItems != null) { foreach (var num in delta.RemovedItems) { var num2 = -1; for (var i = 0; i < clientData.ClientItems.Count; i++) { if (clientData.ClientItems[i].Item.ItemId == num) { num2 = i; break; } } if (num2 != -1) { clientData.ClientItems.RemoveAt(num2); } } } if (delta.NewItems != null) { foreach (var keyValuePair in delta.NewItems) { var item = new ClientInvetoryData { Item = keyValuePair.Value, Amount = keyValuePair.Value.Amount }; if (keyValuePair.Key >= clientData.ClientItems.Count) { clientData.ClientItems.Add(item); } else { clientData.ClientItems.Insert(keyValuePair.Key, item); } } } }
private void ApplyChangesToClientItems(InventoryClientData clientData, ref InventoryDeltaInformation delta) { if (delta.RemovedItems != null) { foreach (var item in delta.RemovedItems) { int index = -1; for (int i = 0; i < clientData.ClientItems.Count; ++i) { if (clientData.ClientItems[i].Item.ItemId == item) { index = i; break; } } if (index != -1) { clientData.ClientItems.RemoveAt(index); } } } if (delta.NewItems != null) { foreach (var item in delta.NewItems) { ClientInvetoryData newitem = new ClientInvetoryData() { Item = item.Value, Amount = item.Value.Amount }; if (item.Key >= clientData.ClientItems.Count) { clientData.ClientItems.Add(newitem); } else { clientData.ClientItems.Insert(item.Key, newitem); } } } }
private void CalculateAddsAndRemovals(InventoryClientData clientData, out InventoryDeltaInformation delta, List <MyPhysicalInventoryItem> items) { delta = new InventoryDeltaInformation(); delta.HasChanges = false; int serverPos = 0; foreach (var serverItem in items) { ClientInvetoryData clientItem; if (clientData.ClientItemsSorted.TryGetValue(serverItem.ItemId, out clientItem)) { if (clientItem.Item.Content.TypeId == serverItem.Content.TypeId && clientItem.Item.Content.SubtypeId == serverItem.Content.SubtypeId) { m_foundDeltaItems.Add(serverItem.ItemId); MyFixedPoint serverAmount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { serverAmount = (MyFixedPoint)gasItem.GasLevel; } if (clientItem.Amount != serverAmount) { MyFixedPoint contentDelta = serverAmount - clientItem.Amount; if (delta.ChangedItems == null) { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); } delta.ChangedItems[serverItem.ItemId] = contentDelta; delta.HasChanges = true; } } } else { if (delta.NewItems == null) { delta.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); } delta.NewItems[serverPos] = serverItem; delta.HasChanges = true; } serverPos++; } foreach (var clientItem in clientData.ClientItemsSorted) { if (delta.RemovedItems == null) { delta.RemovedItems = new List <uint>(); } if (m_foundDeltaItems.Contains(clientItem.Key) == false) { delta.RemovedItems.Add(clientItem.Key); delta.HasChanges = true; } } }
InventoryDeltaInformation CreateSplit(ref InventoryDeltaInformation originalData, ref InventoryDeltaInformation sentData) { var delta = new InventoryDeltaInformation { MessageId = sentData.MessageId }; if (originalData.ChangedItems != null) { if (sentData.ChangedItems == null) { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); using (var enumerator = originalData.ChangedItems.GetEnumerator()) { while (enumerator.MoveNext()) { var keyValuePair = enumerator.Current; delta.ChangedItems[keyValuePair.Key] = keyValuePair.Value; } goto IL_102; } } if (originalData.ChangedItems.Count != sentData.ChangedItems.Count) { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); foreach (var keyValuePair2 in originalData.ChangedItems) { if (!sentData.ChangedItems.ContainsKey(keyValuePair2.Key)) { delta.ChangedItems[keyValuePair2.Key] = keyValuePair2.Value; } } } } IL_102: if (originalData.RemovedItems != null) { if (sentData.RemovedItems == null) { delta.RemovedItems = new List <uint>(); using (var enumerator2 = originalData.RemovedItems.GetEnumerator()) { while (enumerator2.MoveNext()) { var item = enumerator2.Current; delta.RemovedItems.Add(item); } goto IL_1D0; } } if (originalData.RemovedItems.Count != sentData.RemovedItems.Count) { delta.RemovedItems = new List <uint>(); foreach (var item2 in originalData.RemovedItems) { if (!sentData.RemovedItems.Contains(item2)) { delta.RemovedItems.Add(item2); } } } } IL_1D0: if (originalData.NewItems != null) { if (sentData.NewItems == null) { delta.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); using (var enumerator3 = originalData.NewItems.GetEnumerator()) { while (enumerator3.MoveNext()) { var keyValuePair3 = enumerator3.Current; delta.NewItems[keyValuePair3.Key] = keyValuePair3.Value; } goto IL_2BE; } } if (originalData.NewItems.Count != sentData.NewItems.Count) { delta.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); foreach (var keyValuePair4 in originalData.NewItems) { if (!sentData.NewItems.ContainsKey(keyValuePair4.Key)) { delta.NewItems[keyValuePair4.Key] = keyValuePair4.Value; } } } } IL_2BE: if (originalData.SwappedItems == null) { return(delta); } if (sentData.SwappedItems == null) { delta.SwappedItems = new Dictionary <uint, int>(); using (var enumerator4 = originalData.SwappedItems.GetEnumerator()) { while (enumerator4.MoveNext()) { var keyValuePair5 = enumerator4.Current; delta.SwappedItems[keyValuePair5.Key] = keyValuePair5.Value; } return(delta); } } if (originalData.SwappedItems.Count != sentData.SwappedItems.Count) { delta.SwappedItems = new Dictionary <uint, int>(); foreach (var keyValuePair6 in originalData.SwappedItems) { if (!sentData.SwappedItems.ContainsKey(keyValuePair6.Key)) { delta.SwappedItems[keyValuePair6.Key] = keyValuePair6.Value; } } } return(delta); }
InventoryDeltaInformation PrepareSendData(ref InventoryDeltaInformation packetInfo, BitStream stream, int maxBitPosition, out bool needsSplit) { needsSplit = false; var bitPosition = stream.BitPosition; var delta = new InventoryDeltaInformation { HasChanges = false }; stream.WriteBool(false); stream.WriteUInt32(packetInfo.MessageId); stream.WriteBool(packetInfo.ChangedItems != null); if (packetInfo.ChangedItems != null) { stream.WriteInt32(packetInfo.ChangedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); foreach (var keyValuePair in packetInfo.ChangedItems) { stream.WriteUInt32(keyValuePair.Key); stream.WriteInt64(keyValuePair.Value.RawValue); if (stream.BitPosition <= maxBitPosition) { delta.ChangedItems[keyValuePair.Key] = keyValuePair.Value; delta.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.RemovedItems != null); if (packetInfo.RemovedItems != null) { stream.WriteInt32(packetInfo.RemovedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { delta.RemovedItems = new List <uint>(); foreach (var num in packetInfo.RemovedItems) { stream.WriteUInt32(num); if (stream.BitPosition <= maxBitPosition) { delta.RemovedItems.Add(num); delta.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.NewItems != null); if (packetInfo.NewItems != null) { stream.WriteInt32(packetInfo.NewItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { delta.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); foreach (var keyValuePair2 in packetInfo.NewItems) { var value = keyValuePair2.Value; stream.WriteInt32(keyValuePair2.Key); MySerializer.Write(stream, ref value, MyObjectBuilderSerializer.Dynamic); if (stream.BitPosition <= maxBitPosition) { delta.NewItems[keyValuePair2.Key] = value; delta.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.SwappedItems != null); if (packetInfo.SwappedItems != null) { stream.WriteInt32(packetInfo.SwappedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { delta.SwappedItems = new Dictionary <uint, int>(); foreach (var keyValuePair3 in packetInfo.SwappedItems) { stream.WriteUInt32(keyValuePair3.Key); stream.WriteInt32(keyValuePair3.Value); if (stream.BitPosition <= maxBitPosition) { delta.SwappedItems[keyValuePair3.Key] = keyValuePair3.Value; delta.HasChanges = true; } else { needsSplit = true; } } } } stream.SetBitPositionWrite(bitPosition); return(delta); }
InventoryDeltaInformation CalculateInventoryDiff(ref InventoryClientData clientData) { if (m_itemsToSend == null) { m_itemsToSend = new List <MyPhysicalInventoryItem>(); } if (m_foundDeltaItems == null) { m_foundDeltaItems = new HashSet <uint>(); } m_foundDeltaItems.Clear(); InventoryDeltaInformation delta = new InventoryDeltaInformation(); delta.HasChanges = false; List <MyPhysicalInventoryItem> items = Inventory.GetItems(); int serverPos = 0; foreach (var serverItem in items) { ClientInvetoryData clientItem; if (clientData.ClientItems.TryGetValue(serverItem.ItemId, out clientItem)) { if (clientItem.Item.Content.TypeId == serverItem.Content.TypeId && clientItem.Item.Content.SubtypeId == serverItem.Content.SubtypeId) { m_foundDeltaItems.Add(serverItem.ItemId); delta.HasChanges = true; MyFixedPoint serverAmount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { serverAmount = (MyFixedPoint)gasItem.GasLevel; } if (clientItem.Amount != serverAmount) { MyFixedPoint contentDelta = serverAmount - clientItem.Amount; if (delta.ChangedItems == null) { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); } delta.ChangedItems[serverItem.ItemId] = contentDelta; } } } else { if (delta.NewItems == null) { delta.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); } delta.NewItems[serverPos] = serverItem; delta.HasChanges = true; } serverPos++; } foreach (var clientItem in clientData.ClientItems) { if (delta.RemovedItems == null) { delta.RemovedItems = new List <uint>(); } if (m_foundDeltaItems.Contains(clientItem.Key) == false) { delta.RemovedItems.Add(clientItem.Key); delta.HasChanges = true; } } clientData.ClientItems.Clear(); foreach (var serverItem in items) { MyFixedPoint amount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { amount = (MyFixedPoint)gasItem.GasLevel; } clientData.ClientItems[serverItem.ItemId] = new ClientInvetoryData() { Item = serverItem, Amount = amount }; } return(delta); }
void CalculateAddsAndRemovals(InventoryClientData clientData, out InventoryDeltaInformation delta, List <MyPhysicalInventoryItem> items) { delta = new InventoryDeltaInformation { HasChanges = false }; var num = 0; foreach (var myPhysicalInventoryItem in items) { if (clientData.ClientItemsSorted.TryGetValue(myPhysicalInventoryItem.ItemId, out var data)) { if (data.Item.Content.TypeId == myPhysicalInventoryItem.Content.TypeId && data.Item.Content.SubtypeId == myPhysicalInventoryItem.Content.SubtypeId) { m_foundDeltaItems.Add(myPhysicalInventoryItem.ItemId); var myFixedPoint = myPhysicalInventoryItem.Amount; var obj = myPhysicalInventoryItem.Content as MyObjectBuilder_GasContainerObject; if (obj != null) { myFixedPoint = (MyFixedPoint)obj.GasLevel; } if (data.Amount != myFixedPoint) { var value = myFixedPoint - data.Amount; if (delta.ChangedItems == null) { delta.ChangedItems = new Dictionary <uint, MyFixedPoint>(); } delta.ChangedItems[myPhysicalInventoryItem.ItemId] = value; delta.HasChanges = true; } } } else { delta.NewItems ??= new SortedDictionary <int, MyPhysicalInventoryItem>(); delta.NewItems[num] = myPhysicalInventoryItem; delta.HasChanges = true; } num++; } foreach (var keyValuePair in clientData.ClientItemsSorted) { if (delta.RemovedItems == null) { delta.RemovedItems = new List <uint>(); } if (m_foundDeltaItems.Contains(keyValuePair.Key)) { continue; } delta.RemovedItems.Add(keyValuePair.Key); delta.HasChanges = true; } }
private InventoryDeltaInformation CreateSplit(ref InventoryDeltaInformation originalData, ref InventoryDeltaInformation sentData) { InventoryDeltaInformation split = new InventoryDeltaInformation(); split.MessageId = sentData.MessageId; if(originalData.ChangedItems != null) { if (sentData.ChangedItems == null) { split.ChangedItems = new Dictionary<uint, MyFixedPoint>(); foreach (var item in originalData.ChangedItems) { split.ChangedItems[item.Key] = item.Value; } } else if(originalData.ChangedItems.Count != sentData.ChangedItems.Count) { split.ChangedItems = new Dictionary<uint, MyFixedPoint>(); foreach (var item in originalData.ChangedItems) { if (sentData.ChangedItems.ContainsKey(item.Key) == false) { split.ChangedItems[item.Key] = item.Value; } } } } if (originalData.RemovedItems != null) { if (sentData.RemovedItems == null) { split.RemovedItems = new List<uint>(); foreach (var item in originalData.RemovedItems) { split.RemovedItems.Add(item); } } else if(originalData.RemovedItems.Count != sentData.RemovedItems.Count) { split.RemovedItems = new List<uint>(); foreach (var item in originalData.RemovedItems) { if (sentData.RemovedItems.Contains(item) == false) { split.RemovedItems.Add(item); } } } } if (originalData.NewItems != null) { if (sentData.NewItems == null) { split.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); foreach (var item in originalData.NewItems) { split.NewItems[item.Key] = item.Value; } } else if (originalData.NewItems.Count != sentData.NewItems.Count) { split.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); foreach (var item in originalData.NewItems) { if (sentData.NewItems.ContainsKey(item.Key) == false) { split.NewItems[item.Key] = item.Value; } } } } if (originalData.SwappedItems != null) { if (sentData.SwappedItems == null) { split.SwappedItems = new Dictionary<int, int>(); foreach (var item in originalData.SwappedItems) { split.SwappedItems[item.Key] = item.Value; } } else if (originalData.SwappedItems.Count != sentData.SwappedItems.Count) { split.SwappedItems = new Dictionary<int, int>(); foreach (var item in originalData.SwappedItems) { if (sentData.SwappedItems.ContainsKey(item.Key) == false) { split.SwappedItems[item.Key] = item.Value; } } } } return split; }
private InventoryDeltaInformation PrepareSendData(ref InventoryDeltaInformation packetInfo, BitStream stream, int maxBitPosition,out bool needsSplit) { needsSplit = false; int startStreamPosition = stream.BitPosition; InventoryDeltaInformation sentData = new InventoryDeltaInformation(); sentData.HasChanges = false; stream.WriteBool(false); stream.WriteUInt32(packetInfo.MessageId); stream.WriteBool(packetInfo.ChangedItems != null); if (packetInfo.ChangedItems != null) { stream.WriteInt32(packetInfo.ChangedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.ChangedItems = new Dictionary<uint, MyFixedPoint>(); foreach (var item in packetInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); if (stream.BitPosition <= maxBitPosition) { sentData.ChangedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.RemovedItems != null); if (packetInfo.RemovedItems != null) { stream.WriteInt32(packetInfo.RemovedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.RemovedItems = new List<uint>(); foreach (var item in packetInfo.RemovedItems) { stream.WriteUInt32(item); if (stream.BitPosition <= maxBitPosition) { sentData.RemovedItems.Add(item); sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.NewItems != null); if (packetInfo.NewItems != null) { stream.WriteInt32(packetInfo.NewItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); foreach (var item in packetInfo.NewItems) { MyPhysicalInventoryItem inventoryItem = item.Value; VRage.Serialization.MySerializer.Write(stream, ref inventoryItem, MyObjectBuilderSerializer.Dynamic); if (stream.BitPosition <= maxBitPosition) { sentData.NewItems[item.Key] = inventoryItem; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.SwappedItems != null); if (packetInfo.SwappedItems != null) { stream.WriteInt32(packetInfo.SwappedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.SwappedItems = new Dictionary<int, int>(); foreach (var item in packetInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); if (stream.BitPosition <= maxBitPosition) { sentData.SwappedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.SetBitPositionWrite(startStreamPosition); return sentData; }
private InventoryDeltaInformation WriteInventory(ref InventoryDeltaInformation packetInfo, BitStream stream, byte packetId, int maxBitPosition, out bool needsSplit) { InventoryDeltaInformation sendPacketInfo = PrepareSendData(ref packetInfo, stream, maxBitPosition, out needsSplit); if (sendPacketInfo.HasChanges == false) { stream.WriteBool(false); return sendPacketInfo; } sendPacketInfo.MessageId = packetInfo.MessageId; stream.WriteBool(true); stream.WriteUInt32(sendPacketInfo.MessageId); stream.WriteBool(sendPacketInfo.ChangedItems != null); if (sendPacketInfo.ChangedItems != null) { stream.WriteInt32(sendPacketInfo.ChangedItems.Count); foreach (var item in sendPacketInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); } } stream.WriteBool(sendPacketInfo.RemovedItems != null); if (sendPacketInfo.RemovedItems != null) { stream.WriteInt32(sendPacketInfo.RemovedItems.Count); foreach (var item in sendPacketInfo.RemovedItems) { stream.WriteUInt32(item); } } stream.WriteBool(sendPacketInfo.NewItems != null); if (sendPacketInfo.NewItems != null) { stream.WriteInt32(sendPacketInfo.NewItems.Count); foreach (var item in sendPacketInfo.NewItems) { stream.WriteInt32(item.Key); MyPhysicalInventoryItem itemTosend = item.Value; VRage.Serialization.MySerializer.Write(stream, ref itemTosend, MyObjectBuilderSerializer.Dynamic); } } stream.WriteBool(sendPacketInfo.SwappedItems != null); if (sendPacketInfo.SwappedItems != null) { stream.WriteInt32(sendPacketInfo.SwappedItems.Count); foreach (var item in sendPacketInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); } } return sendPacketInfo; }
private void CalculateAddsAndRemovals(InventoryClientData clientData, out InventoryDeltaInformation delta, List<MyPhysicalInventoryItem> items) { delta = new InventoryDeltaInformation(); delta.HasChanges = false; int serverPos = 0; foreach (var serverItem in items) { ClientInvetoryData clientItem; if (clientData.ClientItemsSorted.TryGetValue(serverItem.ItemId, out clientItem)) { if (clientItem.Item.Content.TypeId == serverItem.Content.TypeId && clientItem.Item.Content.SubtypeId == serverItem.Content.SubtypeId) { m_foundDeltaItems.Add(serverItem.ItemId); MyFixedPoint serverAmount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { serverAmount = (MyFixedPoint)gasItem.GasLevel; } if (clientItem.Amount != serverAmount) { MyFixedPoint contentDelta = serverAmount - clientItem.Amount; if (delta.ChangedItems == null) { delta.ChangedItems = new Dictionary<uint, MyFixedPoint>(); } delta.ChangedItems[serverItem.ItemId] = contentDelta; delta.HasChanges = true; } } } else { if (delta.NewItems == null) { delta.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); } delta.NewItems[serverPos] = serverItem; delta.HasChanges = true; } serverPos++; } foreach (var clientItem in clientData.ClientItemsSorted) { if (delta.RemovedItems == null) { delta.RemovedItems = new List<uint>(); } if (m_foundDeltaItems.Contains(clientItem.Key) == false) { delta.RemovedItems.Add(clientItem.Key); delta.HasChanges = true; } } }
private void ApplyChangesToClientItems(InventoryClientData clientData,ref InventoryDeltaInformation delta) { if (delta.RemovedItems != null) { foreach (var item in delta.RemovedItems) { int index = -1; for (int i = 0; i < clientData.ClientItems.Count; ++i) { if (clientData.ClientItems[i].Item.ItemId == item) { index = i; break; } } if (index != -1) { clientData.ClientItems.RemoveAt(index); } } } if (delta.NewItems != null) { foreach (var item in delta.NewItems) { ClientInvetoryData newitem = new ClientInvetoryData() { Item = item.Value, Amount = item.Value.Amount }; if (item.Key >= clientData.ClientItems.Count) { clientData.ClientItems.Add(newitem); } else { clientData.ClientItems.Insert(item.Key, newitem); } } } }
private InventoryDeltaInformation PrepareSendData(ref InventoryDeltaInformation packetInfo, BitStream stream, int maxBitPosition, out bool needsSplit) { needsSplit = false; int startStreamPosition = stream.BitPosition; InventoryDeltaInformation sentData = new InventoryDeltaInformation(); sentData.HasChanges = false; stream.WriteBool(false); stream.WriteUInt32(packetInfo.MessageId); stream.WriteBool(packetInfo.ChangedItems != null); if (packetInfo.ChangedItems != null) { stream.WriteInt32(packetInfo.ChangedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.ChangedItems = new Dictionary <uint, MyFixedPoint>(); foreach (var item in packetInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); if (stream.BitPosition <= maxBitPosition) { sentData.ChangedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.RemovedItems != null); if (packetInfo.RemovedItems != null) { stream.WriteInt32(packetInfo.RemovedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.RemovedItems = new List <uint>(); foreach (var item in packetInfo.RemovedItems) { stream.WriteUInt32(item); if (stream.BitPosition <= maxBitPosition) { sentData.RemovedItems.Add(item); sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.NewItems != null); if (packetInfo.NewItems != null) { stream.WriteInt32(packetInfo.NewItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); foreach (var item in packetInfo.NewItems) { MyPhysicalInventoryItem inventoryItem = item.Value; VRage.Serialization.MySerializer.Write(stream, ref inventoryItem, MyObjectBuilderSerializer.Dynamic); if (stream.BitPosition <= maxBitPosition) { sentData.NewItems[item.Key] = inventoryItem; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.SwappedItems != null); if (packetInfo.SwappedItems != null) { stream.WriteInt32(packetInfo.SwappedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.SwappedItems = new Dictionary <int, int>(); foreach (var item in packetInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); if (stream.BitPosition <= maxBitPosition) { sentData.SwappedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.SetBitPositionWrite(startStreamPosition); return(sentData); }
private InventoryDeltaInformation CreateSplit(ref InventoryDeltaInformation originalData, ref InventoryDeltaInformation sentData) { InventoryDeltaInformation split = new InventoryDeltaInformation(); split.MessageId = sentData.MessageId; if (originalData.ChangedItems != null) { if (sentData.ChangedItems == null) { split.ChangedItems = new Dictionary <uint, MyFixedPoint>(); foreach (var item in originalData.ChangedItems) { split.ChangedItems[item.Key] = item.Value; } } else if (originalData.ChangedItems.Count != sentData.ChangedItems.Count) { split.ChangedItems = new Dictionary <uint, MyFixedPoint>(); foreach (var item in originalData.ChangedItems) { if (sentData.ChangedItems.ContainsKey(item.Key) == false) { split.ChangedItems[item.Key] = item.Value; } } } } if (originalData.RemovedItems != null) { if (sentData.RemovedItems == null) { split.RemovedItems = new List <uint>(); foreach (var item in originalData.RemovedItems) { split.RemovedItems.Add(item); } } else if (originalData.RemovedItems.Count != sentData.RemovedItems.Count) { split.RemovedItems = new List <uint>(); foreach (var item in originalData.RemovedItems) { if (sentData.RemovedItems.Contains(item) == false) { split.RemovedItems.Add(item); } } } } if (originalData.NewItems != null) { if (sentData.NewItems == null) { split.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); foreach (var item in originalData.NewItems) { split.NewItems[item.Key] = item.Value; } } else if (originalData.NewItems.Count != sentData.NewItems.Count) { split.NewItems = new SortedDictionary <int, MyPhysicalInventoryItem>(); foreach (var item in originalData.NewItems) { if (sentData.NewItems.ContainsKey(item.Key) == false) { split.NewItems[item.Key] = item.Value; } } } } if (originalData.SwappedItems != null) { if (sentData.SwappedItems == null) { split.SwappedItems = new Dictionary <int, int>(); foreach (var item in originalData.SwappedItems) { split.SwappedItems[item.Key] = item.Value; } } else if (originalData.SwappedItems.Count != sentData.SwappedItems.Count) { split.SwappedItems = new Dictionary <int, int>(); foreach (var item in originalData.SwappedItems) { if (sentData.SwappedItems.ContainsKey(item.Key) == false) { split.SwappedItems[item.Key] = item.Value; } } } } return(split); }
InventoryDeltaInformation CalculateInventoryDiff(ref InventoryClientData clientData) { if (m_itemsToSend == null) { m_itemsToSend = new List<MyPhysicalInventoryItem>(); } if(m_foundDeltaItems == null ) { m_foundDeltaItems = new HashSet<uint>(); } m_foundDeltaItems.Clear(); InventoryDeltaInformation delta = new InventoryDeltaInformation(); delta.HasChanges = false; List<MyPhysicalInventoryItem> items = Inventory.GetItems(); int serverPos = 0; foreach (var serverItem in items) { ClientInvetoryData clientItem; if (clientData.ClientItems.TryGetValue(serverItem.ItemId,out clientItem)) { if (clientItem.Item.Content.TypeId == serverItem.Content.TypeId && clientItem.Item.Content.SubtypeId == serverItem.Content.SubtypeId) { m_foundDeltaItems.Add(serverItem.ItemId); delta.HasChanges = true; MyFixedPoint serverAmount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { serverAmount = (MyFixedPoint)gasItem.GasLevel; } if (clientItem.Amount != serverAmount) { MyFixedPoint contentDelta = serverAmount - clientItem.Amount; if (delta.ChangedItems == null) { delta.ChangedItems = new Dictionary<uint, MyFixedPoint>(); } delta.ChangedItems[serverItem.ItemId] = contentDelta; } } } else { if (delta.NewItems == null) { delta.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); } delta.NewItems[serverPos] = serverItem; delta.HasChanges = true; } serverPos++; } foreach (var clientItem in clientData.ClientItems) { if (delta.RemovedItems == null) { delta.RemovedItems = new List<uint>(); } if (m_foundDeltaItems.Contains(clientItem.Key) == false) { delta.RemovedItems.Add(clientItem.Key); delta.HasChanges = true; } } clientData.ClientItems.Clear(); foreach (var serverItem in items) { MyFixedPoint amount = serverItem.Amount; var gasItem = serverItem.Content as MyObjectBuilder_GasContainerObject; if (gasItem != null) { amount = (MyFixedPoint)gasItem.GasLevel; } clientData.ClientItems[serverItem.ItemId] = new ClientInvetoryData() { Item = serverItem, Amount = amount }; } return delta; }