From c66098ca4e7dd8f3233e3592809e61b4e617ca42 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 30 Jul 2018 11:00:21 +0300 Subject: [PATCH] Fixed only the first inventory of an item being synced (e.g. clients wouldn't get notified when an item is contained in the "output inventory" of a fabricator or deconstructor). --- .../BarotraumaClient/Source/Items/Item.cs | 8 ++++--- .../BarotraumaShared/Source/DebugConsole.cs | 2 +- .../Source/Items/Inventory.cs | 2 +- .../BarotraumaShared/Source/Items/Item.cs | 22 +++++++++++++++++-- .../Source/Items/ItemInventory.cs | 22 +++++++++++++++++++ 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/Item.cs b/Barotrauma/BarotraumaClient/Source/Items/Item.cs index b360b4475..437e302a3 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Item.cs @@ -377,7 +377,8 @@ namespace Barotrauma (components[componentIndex] as IServerSerializable).ClientRead(type, msg, sendingTime); break; case NetEntityEvent.Type.InventoryState: - ownInventory.ClientRead(type, msg, sendingTime); + int containerIndex = msg.ReadRangedInteger(0, components.Count - 1); + (components[containerIndex] as ItemContainer).Inventory.ClientRead(type, msg, sendingTime); break; case NetEntityEvent.Type.Status: condition = msg.ReadRangedSingle(0.0f, prefab.Health, 8); @@ -425,11 +426,12 @@ namespace Barotrauma case NetEntityEvent.Type.ComponentState: int componentIndex = (int)extraData[1]; msg.WriteRangedInteger(0, components.Count - 1, componentIndex); - (components[componentIndex] as IClientSerializable).ClientWrite(msg, extraData); break; case NetEntityEvent.Type.InventoryState: - ownInventory.ClientWrite(msg, extraData); + int containerIndex = (int)extraData[1]; + msg.WriteRangedInteger(0, components.Count - 1, containerIndex); + (components[containerIndex] as ItemContainer).Inventory.ClientWrite(msg, extraData); break; case NetEntityEvent.Type.Repair: if (FixRequirements.Count > 0) diff --git a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs index 927c61e70..ebeaf4076 100644 --- a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs @@ -1723,7 +1723,7 @@ namespace Barotrauma var itemContainer = item.GetComponent(); if (itemContainer != null) { - GameMain.Server.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.InventoryState }); + GameMain.Server.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.InventoryState, 0 }); } GameMain.Server.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.Status }); diff --git a/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs b/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs index eedb8504a..43a9a8efa 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs @@ -140,7 +140,7 @@ namespace Barotrauma } } - private void CreateNetworkEvent() + protected virtual void CreateNetworkEvent() { if (GameMain.Server != null) { diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index f37865b53..9625a01f7 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -1330,7 +1330,24 @@ namespace Barotrauma (components[componentIndex] as IServerSerializable).ServerWrite(msg, c, extraData); break; case NetEntityEvent.Type.InventoryState: - ownInventory.ServerWrite(msg, c, extraData); + if (extraData.Length < 2 || !(extraData[1] is int)) + { + errorMsg = "Failed to write an inventory state event for the item \"" + Name + "\" - component index not given."; + break; + } + int containerIndex = (int)extraData[1]; + if (containerIndex < 0 || containerIndex >= components.Count) + { + errorMsg = "Failed to write an inventory state event for the item \"" + Name + "\" - container index out of range (" + containerIndex + ")."; + break; + } + else if (!(components[containerIndex] is ItemContainer)) + { + errorMsg = "Failed to write an inventory state event for the item \"" + Name + "\" - component \"" + components[containerIndex] + "\" is not server serializable."; + break; + } + msg.WriteRangedInteger(0, components.Count - 1, containerIndex); + (components[containerIndex] as ItemContainer).Inventory.ServerWrite(msg, c); break; case NetEntityEvent.Type.Status: //clamp to (MaxHealth / 255.0f) if condition > 0.0f @@ -1389,7 +1406,8 @@ namespace Barotrauma (components[componentIndex] as IClientSerializable).ServerRead(type, msg, c); break; case NetEntityEvent.Type.InventoryState: - ownInventory.ServerRead(type, msg, c); + int containerIndex = msg.ReadRangedInteger(0, components.Count - 1); + (components[containerIndex] as ItemContainer).Inventory.ServerRead(type, msg, c); break; case NetEntityEvent.Type.Repair: if (FixRequirements.Count == 0) return; diff --git a/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs b/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs index 728eb9390..eb6039bc6 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs @@ -1,4 +1,5 @@ using Barotrauma.Items.Components; +using Barotrauma.Networking; using Microsoft.Xna.Framework; using System.Collections.Generic; @@ -84,6 +85,27 @@ namespace Barotrauma return wasPut; } + protected override void CreateNetworkEvent() + { + int componentIndex = container.Item.components.IndexOf(container); + if (componentIndex == -1) + { + DebugConsole.Log("Creating a network event for the item \"" + container.Item + "\" failed, ItemContainer not found in components"); + return; + } + + if (GameMain.Server != null) + { + GameMain.Server.CreateEntityEvent(Owner as IServerSerializable, new object[] { NetEntityEvent.Type.InventoryState, componentIndex }); + } +#if CLIENT + else if (GameMain.Client != null) + { + GameMain.Client.CreateEntityEvent(Owner as IClientSerializable, new object[] { NetEntityEvent.Type.InventoryState, componentIndex }); + } +#endif + } + public override void RemoveItem(Item item) { base.RemoveItem(item);