Skip to content

Commit

Permalink
Add sync for storage filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Reousa committed Dec 21, 2023
1 parent 29ddab0 commit f7c43e1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
22 changes: 22 additions & 0 deletions NebulaModel/Packets/Factory/Storage/StorageSyncSetFilterPacket.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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<StorageSyncSetFilterPacket>
{
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();
}
}
}
18 changes: 16 additions & 2 deletions NebulaPatcher/Patches/Dynamic/StorageComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<T>(StorageComponent __instance, T packet) where T : class, new()
{
//Skip if change was done in player's inventory
Expand Down

0 comments on commit f7c43e1

Please sign in to comment.