From f7c43e12bc64e9e4e67de395748190850f4329f7 Mon Sep 17 00:00:00 2001 From: Karim Rizk Date: Thu, 21 Dec 2023 04:27:56 +0200 Subject: [PATCH] Add sync for storage filters --- .../Storage/StorageSyncSetFilterPacket.cs | 22 ++++++++++ .../Storage/StorageSyncSetFilterProcessor.cs | 43 +++++++++++++++++++ .../Patches/Dynamic/StorageComponent_Patch.cs | 18 +++++++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 NebulaModel/Packets/Factory/Storage/StorageSyncSetFilterPacket.cs create mode 100644 NebulaNetwork/PacketProcessors/Factory/Storage/StorageSyncSetFilterProcessor.cs diff --git a/NebulaModel/Packets/Factory/Storage/StorageSyncSetFilterPacket.cs b/NebulaModel/Packets/Factory/Storage/StorageSyncSetFilterPacket.cs new file mode 100644 index 000000000..785123509 --- /dev/null +++ b/NebulaModel/Packets/Factory/Storage/StorageSyncSetFilterPacket.cs @@ -0,0 +1,22 @@ +namespace NebulaModel.Packets.Factory.Storage; + +public class StorageSyncSetFilterPacket +{ + public StorageSyncSetFilterPacket() { } + + // @TODO: This can potentially be moved up the call stack - in UI input - for storage boxes- + // to avoid sending redundant data like `StorageIndex` & `StorageType` per every grid slot. + public StorageSyncSetFilterPacket(int storageIndex, int planetId, int gridIndex, int filterId, EStorageType storageType) + { + StorageIndex = storageIndex; + PlanetId = planetId; + GridIndex = gridIndex; + FilterId = filterId; + StorageType = storageType; + } + public int StorageIndex { get; set; } + public int PlanetId { get; set; } + public int GridIndex { get; set; } + public int FilterId { get; set; } + public EStorageType StorageType { get; set; } +} diff --git a/NebulaNetwork/PacketProcessors/Factory/Storage/StorageSyncSetFilterProcessor.cs b/NebulaNetwork/PacketProcessors/Factory/Storage/StorageSyncSetFilterProcessor.cs new file mode 100644 index 000000000..d7fc9775b --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Factory/Storage/StorageSyncSetFilterProcessor.cs @@ -0,0 +1,43 @@ +#region + +using NebulaAPI.Packets; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Factory.Storage; +using NebulaWorld; + +#endregion + +namespace NebulaNetwork.PacketProcessors.Factory.Storage; + +[RegisterPacketProcessor] +public class StorageSyncSetFilterProcessor : PacketProcessor +{ + protected override void ProcessPacket(StorageSyncSetFilterPacket packet, NebulaConnection conn) + { + StorageComponent storage = null; + var pool = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory?.factoryStorage?.storagePool; + + var poolIsInvalid = pool is null; + if (poolIsInvalid) + { + return; + } + + var storageIsValid = packet.StorageIndex != -1 && packet.StorageIndex < pool.Length; + if (storageIsValid) + { + storage = pool[packet.StorageIndex]; + } + + if (storage is null) + return; + + using (Multiplayer.Session.Storage.IsIncomingRequest.On()) + { + storage.type = packet.StorageType; + storage.SetFilter(packet.GridIndex, packet.FilterId); + storage.NotifyStorageChange(); + } + } +} diff --git a/NebulaPatcher/Patches/Dynamic/StorageComponent_Patch.cs b/NebulaPatcher/Patches/Dynamic/StorageComponent_Patch.cs index 8934e7b65..3647f8658 100644 --- a/NebulaPatcher/Patches/Dynamic/StorageComponent_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/StorageComponent_Patch.cs @@ -38,7 +38,7 @@ public static bool AddItem_Prefix(StorageComponent __instance, int itemId, int c [HarmonyPatch(nameof(StorageComponent.AddItemStacked))] public static bool AddItemStacked_Prefix(StorageComponent __instance, int itemId, int count, int inc, out int remainInc) { - //Run only in MP, if it is not triggered remotly and if this event was triggered manually by an user + //Run only in MP, if it is not triggered remotely and if this event was triggered manually by an user if (Multiplayer.IsActive && !Multiplayer.Session.Storage.IsIncomingRequest && Multiplayer.Session.Storage.IsHumanInput && GameMain.data.localPlanet != null) { @@ -55,7 +55,7 @@ public static bool AddItemStacked_Prefix(StorageComponent __instance, int itemId public static bool TakeItemFromGrid_Prefix(StorageComponent __instance, int gridIndex, ref int itemId, ref int count, out int inc) { - //Run only in MP, if it is not triggered remotly and if this event was triggered manually by an user + //Run only in MP, if it is not triggered remotely and if this event was triggered manually by an user if (Multiplayer.IsActive && !Multiplayer.Session.Storage.IsIncomingRequest && Multiplayer.Session.Storage.IsHumanInput && GameMain.data.localPlanet != null) { @@ -110,6 +110,20 @@ public static bool TakeTailItems_Prefix(StorageComponent __instance, ref int cou } + [HarmonyPostfix] + [HarmonyPatch(typeof(StorageComponent), nameof(StorageComponent.SetFilter))] + private static void SetFilter_Postfix(StorageComponent __instance, int gridIndex, int filterId) + { + //Run only in MP, if it is not triggered remotely and if this event was triggered manually by an user + if (Multiplayer.IsActive && !Multiplayer.Session.Storage.IsIncomingRequest && + Multiplayer.Session.Storage.IsHumanInput && GameMain.data.localPlanet is not null) + { + HandleUserInteraction(__instance, + new StorageSyncSetFilterPacket(__instance.id, GameMain.data.localPlanet.id, gridIndex, filterId, __instance.type)); + } + // return true; + } + private static void HandleUserInteraction(StorageComponent __instance, T packet) where T : class, new() { //Skip if change was done in player's inventory