diff --git a/Subsurface/Source/Characters/Character.cs b/Subsurface/Source/Characters/Character.cs index 5e3b47dd2..3748bd367 100644 --- a/Subsurface/Source/Characters/Character.cs +++ b/Subsurface/Source/Characters/Character.cs @@ -623,7 +623,7 @@ namespace Barotrauma if (item == null) continue; item.Pick(this, true, true, true); - inventory.TryPutItem(item, i, false); + inventory.TryPutItem(item, i, false, false); } } } @@ -1926,60 +1926,77 @@ namespace Barotrauma { if (GameMain.Server != null) return; - msg.Write((byte)ClientNetObject.CHARACTER_INPUT); - - if (memInput.Count > 60) + if (extraData != null && (NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState) { - memInput.RemoveRange(60,memInput.Count - 60); - memMousePos.RemoveRange(60,memMousePos.Count - 60); + inventory.ClientWrite(msg, extraData); } - - msg.Write(LastNetworkUpdateID); - byte inputCount = Math.Min((byte)memInput.Count, (byte)60); - msg.Write(inputCount); - for (int i = 0; i < inputCount; i++) + else { - msg.Write(memInput[i]); - if ((memInput[i] & 0x40) > 0) + msg.Write((byte)ClientNetObject.CHARACTER_INPUT); + + if (memInput.Count > 60) { - msg.Write(memMousePos[i].X); - msg.Write(memMousePos[i].Y); + memInput.RemoveRange(60,memInput.Count - 60); + memMousePos.RemoveRange(60,memMousePos.Count - 60); + } + + msg.Write(LastNetworkUpdateID); + byte inputCount = Math.Min((byte)memInput.Count, (byte)60); + msg.Write(inputCount); + for (int i = 0; i < inputCount; i++) + { + msg.Write(memInput[i]); + if ((memInput[i] & 0x40) > 0) + { + msg.Write(memMousePos[i].X); + msg.Write(memMousePos[i].Y); + } } } } - public virtual void ServerRead(NetIncomingMessage msg, Client c) + public virtual void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c) { if (GameMain.Server == null) return; - UInt32 networkUpdateID = msg.ReadUInt32(); - byte inputCount = msg.ReadByte(); + switch (type) + { + case ClientNetObject.CHARACTER_INPUT: + + UInt32 networkUpdateID = msg.ReadUInt32(); + byte inputCount = msg.ReadByte(); + + for (int i = 0; i < inputCount; i++) + { + byte newInput = msg.ReadByte(); + Vector2 newMousePos = Position; + if ((newInput & 0x40) > 0) + { + newMousePos.X = msg.ReadSingle(); + newMousePos.Y = msg.ReadSingle(); + } + if ((i < ((long)networkUpdateID - (long)LastNetworkUpdateID)) && (i < 60)) + { + memInput.Insert(i, newInput); + memMousePos.Insert(i, newMousePos); + } + } - for (int i = 0; i < inputCount; i++) - { - byte newInput = msg.ReadByte(); - Vector2 newMousePos = Position; - if ((newInput & 0x40) > 0) - { - newMousePos.X = msg.ReadSingle(); - newMousePos.Y = msg.ReadSingle(); - } - if ((i < ((long)networkUpdateID - (long)LastNetworkUpdateID)) && (i < 60)) - { - memInput.Insert(i, newInput); - memMousePos.Insert(i, newMousePos); - } - } - - if (networkUpdateID > LastNetworkUpdateID) - { - LastNetworkUpdateID = networkUpdateID; - } - if (memInput.Count > 60) - { - //deleting inputs from the queue here means the server is way behind and data needs to be dropped - //we'll make the server drop down to 30 inputs for good measure - memInput.RemoveRange(30, memInput.Count - 30); - memMousePos.RemoveRange(30, memMousePos.Count - 30); + if (networkUpdateID > LastNetworkUpdateID) + { + LastNetworkUpdateID = networkUpdateID; + } + if (memInput.Count > 60) + { + //deleting inputs from the queue here means the server is way behind and data needs to be dropped + //we'll make the server drop down to 30 inputs for good measure + memInput.RemoveRange(30, memInput.Count - 30); + memMousePos.RemoveRange(30, memMousePos.Count - 30); + } + break; + + case ClientNetObject.ENTITY_STATE: + inventory.ServerRead(type, msg, c); + break; } } @@ -1987,65 +2004,80 @@ namespace Barotrauma { if (GameMain.Server == null) return; - msg.Write(ID); - - if (this == c.Character) + if (extraData != null && (NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState) { - //length of the message - msg.Write((byte)(4+4+4+1)); - msg.Write(true); - msg.Write((UInt32)(LastNetworkUpdateID - memInput.Count)); + inventory.ServerWrite(msg, c, extraData); } else { - //length of the message - msg.Write((byte)(4+4+1)); - msg.Write(false); + msg.Write(ID); + + if (this == c.Character) + { + //length of the message + msg.Write((byte)(4+4+4+1)); + msg.Write(true); + msg.Write((UInt32)(LastNetworkUpdateID - memInput.Count)); + } + else + { + //length of the message + msg.Write((byte)(4+4+1)); + msg.Write(false); + } + + msg.Write(AnimController.TargetDir == Direction.Right); + + msg.Write(SimPosition.X); + msg.Write(SimPosition.Y); + + msg.WritePadBits(); } - - msg.Write(AnimController.TargetDir == Direction.Right); - - msg.Write(SimPosition.X); - msg.Write(SimPosition.Y); - - msg.WritePadBits(); } - public virtual void ClientRead(NetIncomingMessage msg, float sendingTime) + public virtual void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) { if (GameMain.Server != null) return; - UInt32 networkUpdateID = 0; - if (msg.ReadBoolean()) + switch (type) { - networkUpdateID = msg.ReadUInt32(); - } + case ServerNetObject.ENTITY_POSITION: + UInt32 networkUpdateID = 0; + if (msg.ReadBoolean()) + { + networkUpdateID = msg.ReadUInt32(); + } - bool facingRight = msg.ReadBoolean(); - Vector2 pos = new Vector2(msg.ReadFloat(), msg.ReadFloat()); + bool facingRight = msg.ReadBoolean(); + Vector2 pos = new Vector2(msg.ReadFloat(), msg.ReadFloat()); - var posInfo = - GameMain.NetworkMember.Character == this ? - new PosInfo(pos, facingRight ? Direction.Right : Direction.Left, networkUpdateID) : - new PosInfo(pos, facingRight ? Direction.Right : Direction.Left, sendingTime); + var posInfo = + GameMain.NetworkMember.Character == this ? + new PosInfo(pos, facingRight ? Direction.Right : Direction.Left, networkUpdateID) : + new PosInfo(pos, facingRight ? Direction.Right : Direction.Left, sendingTime); - int index = 0; - if (GameMain.NetworkMember.Character == this) - { - while (index < memPos.Count && posInfo.ID > memPos[index].ID) - { - index++; - } - } - else - { - while (index < memPos.Count && posInfo.Timestamp > memPos[index].Timestamp) - { - index++; - } - } + int index = 0; + if (GameMain.NetworkMember.Character == this) + { + while (index < memPos.Count && posInfo.ID > memPos[index].ID) + { + index++; + } + } + else + { + while (index < memPos.Count && posInfo.Timestamp > memPos[index].Timestamp) + { + index++; + } + } - memPos.Insert(index, posInfo); + memPos.Insert(index, posInfo); + break; + case ServerNetObject.ENTITY_STATE: + inventory.ClientRead(type, msg, sendingTime); + break; + } } public void WriteSpawnData(NetBuffer msg) diff --git a/Subsurface/Source/Items/CharacterInventory.cs b/Subsurface/Source/Items/CharacterInventory.cs index d26e28d29..08f4d584e 100644 --- a/Subsurface/Source/Items/CharacterInventory.cs +++ b/Subsurface/Source/Items/CharacterInventory.cs @@ -141,7 +141,7 @@ namespace Barotrauma /// /// If there is room, puts the item in the inventory and returns true, otherwise returns false /// - public override bool TryPutItem(Item item, List allowedSlots = null) + public override bool TryPutItem(Item item, List allowedSlots = null, bool createNetworkEvent = true) { if (allowedSlots == null || ! allowedSlots.Any()) return false; @@ -152,7 +152,7 @@ namespace Barotrauma { if (Items[i] != null || limbSlots[i] != InvSlotType.Any) continue; - PutItem(item, i); + PutItem(item, i, true, createNetworkEvent); item.Unequip(character); return true; } @@ -178,7 +178,7 @@ namespace Barotrauma { if (allowedSlot.HasFlag(limbSlots[i]) && Items[i] == null) { - PutItem(item, i, !placed); + PutItem(item, i, !placed, createNetworkEvent); item.Equip(character); placed = true; } @@ -194,7 +194,7 @@ namespace Barotrauma return placed; } - public override bool TryPutItem(Item item, int index, bool allowSwapping) + public override bool TryPutItem(Item item, int index, bool allowSwapping, bool createNetworkEvent = true) { //there's already an item in the slot if (Items[index] != null) @@ -224,8 +224,8 @@ namespace Barotrauma Items[currentIndex] = null; Items[index] = null; //if the item in the slot can be moved to the slot of the moved item - if (TryPutItem(existingItem, currentIndex, false) && - TryPutItem(item, index, false)) + if (TryPutItem(existingItem, currentIndex, false, createNetworkEvent) && + TryPutItem(item, index, false, createNetworkEvent)) { } @@ -235,8 +235,8 @@ namespace Barotrauma Items[index] = null; //swapping the items failed -> move them back to where they were - TryPutItem(item, currentIndex, false); - TryPutItem(existingItem, index, false); + TryPutItem(item, currentIndex, false, createNetworkEvent); + TryPutItem(existingItem, index, false, createNetworkEvent); } } @@ -248,7 +248,7 @@ namespace Barotrauma if (!item.AllowedSlots.Contains(InvSlotType.Any)) return false; if (Items[index] != null) return Items[index] == item; - PutItem(item, index, true); + PutItem(item, index, true, createNetworkEvent); return true; } @@ -274,12 +274,12 @@ namespace Barotrauma if (!slotsFree) return false; - return TryPutItem(item, new List() {placeToSlots}); + return TryPutItem(item, new List() {placeToSlots}, createNetworkEvent); } - protected override void PutItem(Item item, int i, bool removeItem = true) + protected override void PutItem(Item item, int i, bool removeItem = true, bool createNetworkEvent = true) { - base.PutItem(item, i, removeItem); + base.PutItem(item, i, removeItem, createNetworkEvent); CreateSlots(); } @@ -321,7 +321,7 @@ namespace Barotrauma { if (doubleClickedItem.ParentInventory != this) { - TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots); + TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots, true); } else { @@ -330,24 +330,24 @@ namespace Barotrauma var selectedContainer = character.SelectedConstruction.GetComponent(); if (selectedContainer != null && selectedContainer.Inventory != null) { - selectedContainer.Inventory.TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots); + selectedContainer.Inventory.TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots, true); } } else if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null) { - character.SelectedCharacter.Inventory.TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots); + character.SelectedCharacter.Inventory.TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots, true); } else //doubleclicked and no other inventory is selected { //not equipped -> attempt to equip if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any)) { - TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any)); + TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true); } //equipped -> attempt to unequip else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any)) { - TryPutItem(doubleClickedItem, new List() { InvSlotType.Any }); + TryPutItem(doubleClickedItem, new List() { InvSlotType.Any }, true); } } } diff --git a/Subsurface/Source/Items/Components/Door.cs b/Subsurface/Source/Items/Components/Door.cs index fc2e60560..b26cee5e7 100644 --- a/Subsurface/Source/Items/Components/Door.cs +++ b/Subsurface/Source/Items/Components/Door.cs @@ -505,7 +505,7 @@ namespace Barotrauma.Items.Components msg.WriteRangedSingle(stuck, 0.0f, 100.0f, 8); } - public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage msg, float sendingTime) { SetState(msg.ReadBoolean(), true); Stuck = msg.ReadRangedSingle(0.0f, 100.0f, 8); diff --git a/Subsurface/Source/Items/Components/ItemContainer.cs b/Subsurface/Source/Items/Components/ItemContainer.cs index 8846f70c6..d9d4c4c68 100644 --- a/Subsurface/Source/Items/Components/ItemContainer.cs +++ b/Subsurface/Source/Items/Components/ItemContainer.cs @@ -263,7 +263,7 @@ namespace Barotrauma.Items.Components Item item = MapEntity.FindEntityByID(itemIds[i]) as Item; if (item == null) continue; - Inventory.TryPutItem(item, i, false); + Inventory.TryPutItem(item, i, false, false); } itemIds = null; diff --git a/Subsurface/Source/Items/Components/Machines/Pump.cs b/Subsurface/Source/Items/Components/Machines/Pump.cs index cf7def032..9f1292aea 100644 --- a/Subsurface/Source/Items/Components/Machines/Pump.cs +++ b/Subsurface/Source/Items/Components/Machines/Pump.cs @@ -234,7 +234,7 @@ namespace Barotrauma.Items.Components msg.Write(IsActive); } - public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) + public void ServerRead(ClientNetObject type, Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) { float flowPercentage = msg.ReadRangedInteger(-10, 10) * 10.0f; bool isActive = msg.ReadBoolean(); @@ -252,7 +252,7 @@ namespace Barotrauma.Items.Components msg.Write(IsActive); } - public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage msg, float sendingTime) { FlowPercentage = msg.ReadRangedInteger(-10, 10) * 10.0f; IsActive = msg.ReadBoolean(); diff --git a/Subsurface/Source/Items/Components/Machines/Radar.cs b/Subsurface/Source/Items/Components/Machines/Radar.cs index c7c6a449a..ba1ab4797 100644 --- a/Subsurface/Source/Items/Components/Machines/Radar.cs +++ b/Subsurface/Source/Items/Components/Machines/Radar.cs @@ -431,7 +431,7 @@ namespace Barotrauma.Items.Components msg.Write(IsActive); } - public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) + public void ServerRead(ClientNetObject type, Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) { bool isActive = msg.ReadBoolean(); @@ -448,7 +448,7 @@ namespace Barotrauma.Items.Components msg.Write(IsActive); } - public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage msg, float sendingTime) { IsActive = msg.ReadBoolean(); isActiveTickBox.Selected = IsActive; diff --git a/Subsurface/Source/Items/Components/Machines/Reactor.cs b/Subsurface/Source/Items/Components/Machines/Reactor.cs index 6c4a5f286..e3a33ffe8 100644 --- a/Subsurface/Source/Items/Components/Machines/Reactor.cs +++ b/Subsurface/Source/Items/Components/Machines/Reactor.cs @@ -561,7 +561,7 @@ namespace Barotrauma.Items.Components msg.WriteRangedSingle(fissionRate, 0.0f, 100.0f, 8); } - public void ServerRead(NetIncomingMessage msg, Client c) + public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c) { bool autoTemp = msg.ReadBoolean(); float shutDownTemp = msg.ReadRangedSingle(0.0f, 10000.0f, 8); @@ -591,7 +591,7 @@ namespace Barotrauma.Items.Components msg.WriteRangedSingle(fissionRate, 0.0f, 100.0f, 8); } - public void ClientRead(NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) { Temperature = msg.ReadRangedSingle(0.0f, 10000.0f, 16); diff --git a/Subsurface/Source/Items/Components/Machines/Steering.cs b/Subsurface/Source/Items/Components/Machines/Steering.cs index 4b856330a..f0a685afa 100644 --- a/Subsurface/Source/Items/Components/Machines/Steering.cs +++ b/Subsurface/Source/Items/Components/Machines/Steering.cs @@ -486,7 +486,7 @@ namespace Barotrauma.Items.Components } } - public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) + public void ServerRead(ClientNetObject type, Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) { bool autoPilot = msg.ReadBoolean(); Vector2 newTargetVelocity = targetVelocity; @@ -569,7 +569,7 @@ namespace Barotrauma.Items.Components } } - public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage msg, float sendingTime) { bool autoPilot = msg.ReadBoolean(); Vector2 newTargetVelocity = targetVelocity; diff --git a/Subsurface/Source/Items/Inventory.cs b/Subsurface/Source/Items/Inventory.cs index 7b453022a..e2d6b85b3 100644 --- a/Subsurface/Source/Items/Inventory.cs +++ b/Subsurface/Source/Items/Inventory.cs @@ -172,31 +172,31 @@ namespace Barotrauma /// /// If there is room, puts the item in the inventory and returns true, otherwise returns false /// - public virtual bool TryPutItem(Item item, List allowedSlots = null) + public virtual bool TryPutItem(Item item, List allowedSlots = null, bool createNetworkEvent = true) { int slot = FindAllowedSlot(item); if (slot < 0) return false; - PutItem(item, slot); + PutItem(item, slot, true, createNetworkEvent); return true; } - public virtual bool TryPutItem(Item item, int i, bool allowSwapping) + public virtual bool TryPutItem(Item item, int i, bool allowSwapping, bool createNetworkEvent = true) { if (Owner == null) return false; if (CanBePut(item,i)) { - PutItem(item, i); + PutItem(item, i, true, createNetworkEvent); return true; } else { - if (slots != null) slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f); + if (slots != null && createNetworkEvent) slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f); return false; } } - protected virtual void PutItem(Item item, int i, bool removeItem = true) + protected virtual void PutItem(Item item, int i, bool removeItem = true, bool createNetworkEvent = true) { if (Owner == null) return; @@ -215,6 +215,23 @@ namespace Barotrauma { item.body.Enabled = false; } + + if (createNetworkEvent) + { + CreateNetworkEvent(); + } + } + + private void CreateNetworkEvent() + { + if (GameMain.Server != null) + { + GameMain.Server.CreateEntityEvent(Owner as IServerSerializable, new object[] { NetEntityEvent.Type.InventoryState }); + } + else if (GameMain.Client != null) + { + GameMain.Client.CreateEntityEvent(Owner as IClientSerializable, new object[] { NetEntityEvent.Type.InventoryState }); + } } public Item FindItem(string itemName) @@ -232,8 +249,9 @@ namespace Barotrauma for (int n = 0; n < capacity; n++) { if (Items[n] != item) continue; + Items[n] = null; - item.ParentInventory = null; + item.ParentInventory = null; } } @@ -285,10 +303,7 @@ namespace Barotrauma { if (!PlayerInput.LeftButtonHeld()) { - if (Owner != null) - { - - } + CreateNetworkEvent(); DropItem(draggingItem); } @@ -529,7 +544,7 @@ namespace Barotrauma ServerWrite(msg, null); } - public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) + public void ServerRead(ClientNetObject type, Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c) { List prevItems = new List(Items); ushort[] newItemIDs = new ushort[capacity]; @@ -559,10 +574,12 @@ namespace Barotrauma { if (!c.Character.CanAccessItem(item)) continue; } - TryPutItem(item, i, true); + TryPutItem(item, i, true, false); } } + GameMain.Server.CreateEntityEvent(Owner as IServerSerializable, new object[] { NetEntityEvent.Type.InventoryState }); + foreach (Item item in Items) { if (item == null) continue; @@ -589,7 +606,7 @@ namespace Barotrauma } } - public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage msg, float sendingTime) { ushort[] newItemIDs = new ushort[capacity]; @@ -609,7 +626,7 @@ namespace Barotrauma var item = Entity.FindEntityByID(newItemIDs[i]) as Item; if (item == null) continue; - TryPutItem(item, i, true); + TryPutItem(item, i, true, false); } } } diff --git a/Subsurface/Source/Items/Item.cs b/Subsurface/Source/Items/Item.cs index 930cd9810..1742e6832 100644 --- a/Subsurface/Source/Items/Item.cs +++ b/Subsurface/Source/Items/Item.cs @@ -1638,36 +1638,78 @@ namespace Barotrauma public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null) { - if (extraData == null) return; + if (extraData == null || extraData.Length == 0 || !(extraData[0] is NetEntityEvent.Type)) + { + return; + } + else if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.ComponentState) + { + msg.Write(true); - int componentIndex = (int)extraData[0]; - msg.Write((byte)componentIndex); + int componentIndex = (int)extraData[1]; + msg.Write((byte)componentIndex); - (components[componentIndex] as IServerSerializable).ServerWrite(msg, c, extraData); + (components[componentIndex] as IServerSerializable).ServerWrite(msg, c, extraData); + } + else if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState) + { + msg.Write(false); + + ownInventory.ServerWrite(msg, c, extraData); + } } - public void ClientRead(NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) { - int componentIndex = msg.ReadByte(); + bool isComponentUpdate = msg.ReadBoolean(); - (components[componentIndex] as IServerSerializable).ClientRead(msg, sendingTime); + if (isComponentUpdate) + { + int componentIndex = msg.ReadByte(); + (components[componentIndex] as IServerSerializable).ClientRead(type, msg, sendingTime); + } + else + { + ownInventory.ClientRead(type, msg, sendingTime); + } } public void ClientWrite(NetBuffer msg, object[] extraData = null) { - if (extraData == null) return; + if (extraData == null || extraData.Length == 0 || !(extraData[0] is NetEntityEvent.Type)) + { + return; + } + else if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.ComponentState) + { + msg.Write(true); - int componentIndex = (int)extraData[0]; - msg.Write((byte)componentIndex); + int componentIndex = (int)extraData[1]; + msg.Write((byte)componentIndex); - (components[componentIndex] as IClientSerializable).ClientWrite(msg, extraData); + (components[componentIndex] as IClientSerializable).ClientWrite(msg, extraData); + } + else if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState) + { + msg.Write(false); + + ownInventory.ClientWrite(msg, extraData); + } } - public void ServerRead(NetIncomingMessage msg, Client c) + public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c) { - int componentIndex = msg.ReadByte(); - - (components[componentIndex] as IClientSerializable).ServerRead(msg, c); + bool isComponentUpdate = msg.ReadBoolean(); + + if (isComponentUpdate) + { + int componentIndex = msg.ReadByte(); + (components[componentIndex] as IClientSerializable).ServerRead(type, msg, c); + } + else + { + ownInventory.ServerRead(type, msg, c); + } } public void WriteSpawnData(NetBuffer msg) @@ -1773,11 +1815,11 @@ namespace Barotrauma if (inventory != null) { if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 && - inventory.TryPutItem(item, inventorySlotIndex, false)) + inventory.TryPutItem(item, inventorySlotIndex, false, false)) { return null; } - inventory.TryPutItem(item, item.AllowedSlots); + inventory.TryPutItem(item, item.AllowedSlots, false); } return item; @@ -1896,7 +1938,7 @@ namespace Barotrauma if (GameMain.Server == null) return; int index = components.IndexOf(ic); - GameMain.Server.CreateEntityEvent(this, new object[] { index }); + GameMain.Server.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ComponentState, index }); } public void CreateClientEvent(T ic) where T : ItemComponent, IClientSerializable @@ -1904,7 +1946,7 @@ namespace Barotrauma if (GameMain.Client == null) return; int index = components.IndexOf(ic); - GameMain.Client.CreateEntityEvent(this, new object[] { index }); + GameMain.Client.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ComponentState, index }); } public override void ShallowRemove() diff --git a/Subsurface/Source/Items/ItemInventory.cs b/Subsurface/Source/Items/ItemInventory.cs index 18e8a1cde..a583dee83 100644 --- a/Subsurface/Source/Items/ItemInventory.cs +++ b/Subsurface/Source/Items/ItemInventory.cs @@ -45,9 +45,9 @@ namespace Barotrauma } - public override bool TryPutItem(Item item, System.Collections.Generic.List allowedSlots = null) + public override bool TryPutItem(Item item, System.Collections.Generic.List allowedSlots = null, bool createNetworkEvent = true) { - bool wasPut = base.TryPutItem(item, allowedSlots); + bool wasPut = base.TryPutItem(item, allowedSlots, createNetworkEvent); if (wasPut) { @@ -66,9 +66,9 @@ namespace Barotrauma return wasPut; } - public override bool TryPutItem(Item item, int i, bool allowSwapping) + public override bool TryPutItem(Item item, int i, bool allowSwapping, bool createNetworkEvent = true) { - bool wasPut = base.TryPutItem(item, i, allowSwapping); + bool wasPut = base.TryPutItem(item, i, allowSwapping, createNetworkEvent); if (wasPut) { diff --git a/Subsurface/Source/Map/Hull.cs b/Subsurface/Source/Map/Hull.cs index 7aa4533c8..d68135eaf 100644 --- a/Subsurface/Source/Map/Hull.cs +++ b/Subsurface/Source/Map/Hull.cs @@ -843,7 +843,7 @@ namespace Barotrauma lastSentOxygen = OxygenPercentage; } - public void ClientRead(NetIncomingMessage message, float sendingTime) + public void ClientRead(ServerNetObject type, NetIncomingMessage message, float sendingTime) { Volume = message.ReadRangedSingle(0.0f, 1.5f, 8) * FullVolume; OxygenPercentage = message.ReadRangedSingle(0.0f, 100.0f, 8); diff --git a/Subsurface/Source/Map/Structure.cs b/Subsurface/Source/Map/Structure.cs index eda1f080a..8f35e98ab 100644 --- a/Subsurface/Source/Map/Structure.cs +++ b/Subsurface/Source/Map/Structure.cs @@ -855,7 +855,7 @@ namespace Barotrauma } } - public void ClientRead(NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) { for (int i = 0; i < sections.Count(); i++) { diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index 359043c35..060612c36 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -1176,7 +1176,7 @@ namespace Barotrauma msg.Write(PhysicsBody.SimPosition.Y); } - public void ClientRead(NetIncomingMessage msg, float sendingTime) + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) { var newTargetPosition = new Vector2( msg.ReadFloat(), diff --git a/Subsurface/Source/Networking/EntitySpawner.cs b/Subsurface/Source/Networking/EntitySpawner.cs index 2e0baeca6..3eb6a8d35 100644 --- a/Subsurface/Source/Networking/EntitySpawner.cs +++ b/Subsurface/Source/Networking/EntitySpawner.cs @@ -186,7 +186,7 @@ namespace Barotrauma } } - public void ClientRead(Lidgren.Network.NetIncomingMessage message, float sendingTime) + public void ClientRead(ServerNetObject type, Lidgren.Network.NetIncomingMessage message, float sendingTime) { if (GameMain.Server != null) return; diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 5c5844c19..820bcc16e 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -724,7 +724,7 @@ namespace Barotrauma.Networking } else { - entity.ClientRead(inc, sendingTime); + entity.ClientRead(objHeader, inc, sendingTime); } inc.ReadPadBits(); @@ -736,7 +736,7 @@ namespace Barotrauma.Networking ChatMessage.ClientRead(inc); break; case ServerNetObject.ENTITY_SPAWN: - Item.Spawner.ClientRead(inc, sendingTime); + Item.Spawner.ClientRead(objHeader, inc, sendingTime); inc.ReadPadBits(); break; default: diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 9ecdb1859..3a2860e97 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -638,7 +638,7 @@ namespace Barotrauma.Networking case ClientNetObject.CHARACTER_INPUT: if (c.Character != null && !c.Character.IsDead && !c.Character.IsUnconscious) { - c.Character.ServerRead(inc, c); + c.Character.ServerRead(objHeader, inc, c); } break; case ClientNetObject.ENTITY_STATE: diff --git a/Subsurface/Source/Networking/INetSerializable.cs b/Subsurface/Source/Networking/INetSerializable.cs index f54d34854..cecc8c72d 100644 --- a/Subsurface/Source/Networking/INetSerializable.cs +++ b/Subsurface/Source/Networking/INetSerializable.cs @@ -11,7 +11,7 @@ namespace Barotrauma.Networking interface IClientSerializable : INetSerializable { void ClientWrite(NetBuffer msg, object[] extraData = null); - void ServerRead(NetIncomingMessage msg, Client c); + void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c); } /// @@ -20,6 +20,6 @@ namespace Barotrauma.Networking interface IServerSerializable : INetSerializable { void ServerWrite(NetBuffer msg, Client c, object[] extraData = null); - void ClientRead(NetIncomingMessage msg, float sendingTime); + void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime); } } diff --git a/Subsurface/Source/Networking/NetEntityEvent/ClientEntityEventManager.cs b/Subsurface/Source/Networking/NetEntityEvent/ClientEntityEventManager.cs index b2c26331b..a6ef737b3 100644 --- a/Subsurface/Source/Networking/NetEntityEvent/ClientEntityEventManager.cs +++ b/Subsurface/Source/Networking/NetEntityEvent/ClientEntityEventManager.cs @@ -65,7 +65,7 @@ namespace Barotrauma.Networking break; } - eventsToSync.Add(events[i]); + eventsToSync.Insert(0, events[i]); } if (eventsToSync.Count == 0) return; @@ -96,7 +96,7 @@ namespace Barotrauma.Networking var serverEntity = entity as IServerSerializable; if (serverEntity == null) return; - serverEntity.ClientRead(buffer, sendingTime); + serverEntity.ClientRead(ServerNetObject.ENTITY_STATE, buffer, sendingTime); } public void Clear() diff --git a/Subsurface/Source/Networking/NetEntityEvent/NetEntityEvent.cs b/Subsurface/Source/Networking/NetEntityEvent/NetEntityEvent.cs index 0abc69fda..6e5a43e86 100644 --- a/Subsurface/Source/Networking/NetEntityEvent/NetEntityEvent.cs +++ b/Subsurface/Source/Networking/NetEntityEvent/NetEntityEvent.cs @@ -9,6 +9,11 @@ namespace Barotrauma.Networking { abstract class NetEntityEvent { + public enum Type + { + Default, ComponentState, InventoryState + } + public readonly Entity Entity; public readonly UInt32 ID; diff --git a/Subsurface/Source/Networking/NetEntityEvent/ServerEntityEventManager.cs b/Subsurface/Source/Networking/NetEntityEvent/ServerEntityEventManager.cs index 612af11f3..326aa71ae 100644 --- a/Subsurface/Source/Networking/NetEntityEvent/ServerEntityEventManager.cs +++ b/Subsurface/Source/Networking/NetEntityEvent/ServerEntityEventManager.cs @@ -24,7 +24,7 @@ namespace Barotrauma.Networking public void CreateEvent(IServerSerializable entity, object[] extraData = null) { - if (!(entity is Entity)) + if (entity == null || !(entity is Entity)) { DebugConsole.ThrowError("Can't create an entity event for " + entity + "!"); return; @@ -91,7 +91,7 @@ namespace Barotrauma.Networking var clientEntity = entity as IClientSerializable; if (clientEntity == null) return; - clientEntity.ServerRead(buffer, sender); + clientEntity.ServerRead(ClientNetObject.ENTITY_STATE, buffer, sender); } public void Read(NetIncomingMessage msg, Client client)