diff --git a/NebulaModel/Packets/Logistics/RemoteOrderUpdate.cs b/NebulaModel/Packets/Logistics/RemoteOrderUpdate.cs new file mode 100644 index 000000000..a9c726f14 --- /dev/null +++ b/NebulaModel/Packets/Logistics/RemoteOrderUpdate.cs @@ -0,0 +1,15 @@ +namespace NebulaModel.Packets.Logistics +{ + public class RemoteOrderUpdate + { + public int StationGId { get; set; } + public int[] RemoteOrder { get; set; } + + public RemoteOrderUpdate() { } + public RemoteOrderUpdate(int stationGid, int[] remoteOrder) + { + StationGId = stationGid; + RemoteOrder = remoteOrder; + } + } +} diff --git a/NebulaModel/Packets/Logistics/StationSubscribeUIUpdates.cs b/NebulaModel/Packets/Logistics/StationSubscribeUIUpdates.cs deleted file mode 100644 index b749187b4..000000000 --- a/NebulaModel/Packets/Logistics/StationSubscribeUIUpdates.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace NebulaModel.Packets.Logistics -{ - public class StationSubscribeUIUpdates - { - public int PlanetId { get; set; } - public int StationId { get; set; } - public int StationGId { get; set; } - public bool Subscribe { get; set; } - public StationSubscribeUIUpdates() { } - public StationSubscribeUIUpdates(bool subscribe, int planetId, int stationId, int stationGId) - { - PlanetId = planetId; - StationId = stationId; - StationGId = stationGId; - Subscribe = subscribe; - } - } -} diff --git a/NebulaModel/Packets/Logistics/StationUI.cs b/NebulaModel/Packets/Logistics/StationUI.cs index 2d186b536..39604783e 100644 --- a/NebulaModel/Packets/Logistics/StationUI.cs +++ b/NebulaModel/Packets/Logistics/StationUI.cs @@ -4,6 +4,7 @@ public class StationUI { public enum EUISettings { + None, MaxChargePower, MaxTripDrones, MaxTripVessel, @@ -15,44 +16,21 @@ public enum EUISettings SetDroneCount, SetShipCount, SetWarperCount, - AddOrRemoveItemFromStorageRequest, - AddOrRemoveItemFromStorageResponse + PilerCount, + MaxMiningSpeed } public int PlanetId { get; set; } public int StationId { get; set; } public int StationGId { get; set; } - public bool IsStorageUI { get; set; } public StationUI.EUISettings SettingIndex { get; set; } public float SettingValue { get; set; } - public int StorageIdx { get; set; } - public int ItemId { get; set; } - public int ItemCountMax { get; set; } - public ELogisticStorage LocalLogic { get; set; } - public ELogisticStorage RemoteLogic { get; set; } - public bool ShouldMimic { get; set; } public bool WarperShouldTakeFromStorage { get; set; } + public bool ShouldRefund { get; set; } public StationUI() { } - public StationUI(int planetId, int stationId, int stationGId, int storageIdx, int itemId, int itemCountMax, ELogisticStorage localLogic, ELogisticStorage remoteLogic) - { - IsStorageUI = true; - ShouldMimic = false; - WarperShouldTakeFromStorage = false; - - PlanetId = planetId; - StationId = stationId; - StationGId = stationGId; - StorageIdx = storageIdx; - ItemId = itemId; - ItemCountMax = itemCountMax; - LocalLogic = localLogic; - RemoteLogic = remoteLogic; - } public StationUI(int planetId, int stationId, int stationGId, StationUI.EUISettings settingIndex, float value, bool warperShouldTakeFromStorage = false) { - IsStorageUI = false; - PlanetId = planetId; StationId = stationId; StationGId = stationGId; @@ -60,17 +38,5 @@ public StationUI(int planetId, int stationId, int stationGId, StationUI.EUISetti SettingValue = value; WarperShouldTakeFromStorage = warperShouldTakeFromStorage; } - public StationUI(int planetId, int stationId, int stationGId, int storageIdx, StationUI.EUISettings settingIndex, int itemId, int settingValue) - { - WarperShouldTakeFromStorage = false; - - PlanetId = planetId; - StationId = stationId; - StationGId = stationGId; - StorageIdx = storageIdx; - SettingIndex = settingIndex; - ItemId = itemId; - SettingValue = settingValue; - } } } diff --git a/NebulaModel/Packets/Logistics/StationUIInitialSync.cs b/NebulaModel/Packets/Logistics/StationUIInitialSync.cs index 459d18be3..03ad8da54 100644 --- a/NebulaModel/Packets/Logistics/StationUIInitialSync.cs +++ b/NebulaModel/Packets/Logistics/StationUIInitialSync.cs @@ -14,9 +14,11 @@ public class StationUIInitialSync public bool IncludeOrbitCollector { get; set; } public long Energy { get; set; } public long EnergyPerTick { get; set; } + public int PilerCount { get; set; } public int[] ItemId { get; set; } public int[] ItemCountMax { get; set; } public int[] ItemCount { get; set; } + public int[] ItemInc { get; set; } public int[] LocalLogic { get; set; } public int[] RemoteLogic { get; set; } public int[] RemoteOrder { get; set; } @@ -35,9 +37,11 @@ public StationUIInitialSync( bool includeOrbitCollector, long energy, long energyPerTick, + int pilerCount, int[] itemId, int[] itemCountMax, int[] itemCount, + int[] itemInc, int[] localLogic, int[] remoteLogic, int[] remoteOrder @@ -55,10 +59,12 @@ int[] remoteOrder IncludeOrbitCollector = includeOrbitCollector; Energy = energy; EnergyPerTick = energyPerTick; + PilerCount = pilerCount; ItemId = itemId; ItemCountMax = itemCountMax; ItemCount = itemCount; + ItemInc = itemInc; LocalLogic = localLogic; RemoteLogic = remoteLogic; RemoteOrder = remoteOrder; diff --git a/NebulaModel/Packets/Logistics/StorageUI.cs b/NebulaModel/Packets/Logistics/StorageUI.cs new file mode 100644 index 000000000..47b56acb9 --- /dev/null +++ b/NebulaModel/Packets/Logistics/StorageUI.cs @@ -0,0 +1,43 @@ +namespace NebulaModel.Packets.Logistics +{ + public class StorageUI + { + public int PlanetId { get; set; } + public int StationId { get; set; } + public int StationGId { get; set; } + public int StorageIdx { get; set; } + public int ItemId { get; set; } + public int ItemCountMax { get; set; } + public ELogisticStorage LocalLogic { get; set; } + public ELogisticStorage RemoteLogic { get; set; } + public int ItemCount { get; set; } + public int ItemInc { get; set; } + public bool ShouldRefund { get; set; } + + public StorageUI() { } + public StorageUI(int planetId, int stationId, int stationGId, int storageIdx, int itemId, int itemCountMax, ELogisticStorage localLogic, ELogisticStorage remoteLogic) + { + ItemCount = -1; //Indicate it is SetStationStorage() + + PlanetId = planetId; + StationId = stationId; + StationGId = stationGId; + StorageIdx = storageIdx; + ItemId = itemId; + ItemCountMax = itemCountMax; + LocalLogic = localLogic; + RemoteLogic = remoteLogic; + } + public StorageUI(int planetId, int stationId, int stationGId, int storageIdx, int itemCount, int itemInc) + { + ShouldRefund = false; + + PlanetId = planetId; + StationId = stationId; + StationGId = stationGId; + StorageIdx = storageIdx; + ItemCount = itemCount; + ItemInc = itemInc; + } + } +} diff --git a/NebulaNetwork/PacketProcessors/Logistics/RemoteOrderUpdateProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/RemoteOrderUpdateProcessor.cs new file mode 100644 index 000000000..f82213fbd --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Logistics/RemoteOrderUpdateProcessor.cs @@ -0,0 +1,45 @@ +using NebulaAPI; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Logistics; +using NebulaWorld; + +namespace NebulaNetwork.PacketProcessors.Logistics +{ + [RegisterPacketProcessor] + public class RemoteOrderUpdateProcessor : PacketProcessor + { + public override void ProcessPacket(RemoteOrderUpdate packet, NebulaConnection conn) + { + if (IsHost) + { + StationComponent stationComponent = GameMain.data.galacticTransport.stationPool[packet.StationGId]; + StationStore[] storage = stationComponent?.storage; + if (stationComponent == null || storage == null) + { + return; + } + int[] remoteOrder = new int[storage.Length]; + for (int i = 0; i < stationComponent.storage.Length; i++) + { + remoteOrder[i] = storage[i].remoteOrder; + } + packet.RemoteOrder = remoteOrder; + conn.SendPacket(packet); + } + if (IsClient) + { + StationComponent stationComponent = GameMain.data.galacticTransport.stationPool[packet.StationGId]; + StationStore[] storage = stationComponent?.storage; + if (stationComponent == null || storage == null) + { + return; + } + for (int i = 0; i < storage.Length; i++) + { + storage[i].remoteOrder = packet.RemoteOrder[i]; + } + } + } + } +} \ No newline at end of file diff --git a/NebulaNetwork/PacketProcessors/Logistics/StationSubscribeUIUpdatesProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/StationSubscribeUIUpdatesProcessor.cs deleted file mode 100644 index 76219b944..000000000 --- a/NebulaNetwork/PacketProcessors/Logistics/StationSubscribeUIUpdatesProcessor.cs +++ /dev/null @@ -1,29 +0,0 @@ -using NebulaAPI; -using NebulaModel.Networking; -using NebulaModel.Packets; -using NebulaModel.Packets.Logistics; -using NebulaWorld; - -namespace NebulaNetwork.PacketProcessors.Logistics -{ - [RegisterPacketProcessor] - internal class StationSubscribeUIUpdatesProcessor : PacketProcessor - { - public override void ProcessPacket(StationSubscribeUIUpdates packet, NebulaConnection conn) - { - if (IsClient) - { - return; - } - - if (packet.Subscribe) - { - Multiplayer.Session.StationsUI.AddSubscriber(packet.PlanetId, packet.StationId, packet.StationGId, conn); - } - else - { - Multiplayer.Session.StationsUI.RemoveSubscriber(packet.PlanetId, packet.StationId, packet.StationGId, conn); - } - } - } -} diff --git a/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncProcessor.cs index a3665b383..873613765 100644 --- a/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncProcessor.cs @@ -8,7 +8,6 @@ /* * When the client opens the UI of a station (ILS/PLS/Collector) the contents gets updated and shown to * the player once this packet is received. He will see a loading text before that. - * This will also subscribe to live updates syncing changes made by other players to the station while the UI is opened */ namespace NebulaNetwork.PacketProcessors.Logistics { @@ -29,47 +28,38 @@ public override void ProcessPacket(StationUIInitialSync packet, NebulaConnection return; } - if (Multiplayer.Session.StationsUI.UIIsSyncedStage == 1) - { - UIStationWindow stationWindow = UIRoot.instance.uiGame.stationWindow; - - stationComponent.tripRangeDrones = packet.TripRangeDrones; - stationComponent.tripRangeShips = packet.TripRangeShips; - stationComponent.deliveryDrones = packet.DeliveryDrones; - stationComponent.deliveryShips = packet.DeliveryShips; - stationComponent.warpEnableDist = packet.WarperEnableDistance; - stationComponent.warperNecessary = packet.WarperNecessary; - stationComponent.includeOrbitCollector = packet.IncludeOrbitCollector; - stationComponent.energy = packet.Energy; - stationComponent.energyPerTick = packet.EnergyPerTick; + stationComponent.tripRangeDrones = packet.TripRangeDrones; + stationComponent.tripRangeShips = packet.TripRangeShips; + stationComponent.deliveryDrones = packet.DeliveryDrones; + stationComponent.deliveryShips = packet.DeliveryShips; + stationComponent.warpEnableDist = packet.WarperEnableDistance; + stationComponent.warperNecessary = packet.WarperNecessary; + stationComponent.includeOrbitCollector = packet.IncludeOrbitCollector; + stationComponent.energy = packet.Energy; + stationComponent.energyPerTick = packet.EnergyPerTick; + stationComponent.pilerCount = packet.PilerCount; - for (int i = 0; i < packet.ItemId.Length; i++) + for (int i = 0; i < packet.ItemId.Length; i++) + { + if (stationComponent.storage == null) { - if (stationComponent.storage == null) - { - stationComponent.storage = new StationStore[packet.ItemId.Length]; - } - - stationComponent.storage[i].itemId = packet.ItemId[i]; - stationComponent.storage[i].max = packet.ItemCountMax[i]; - stationComponent.storage[i].count = packet.ItemCount[i]; - stationComponent.storage[i].remoteOrder = packet.RemoteOrder[i]; - stationComponent.storage[i].localLogic = (ELogisticStorage)packet.LocalLogic[i]; - stationComponent.storage[i].remoteLogic = (ELogisticStorage)packet.RemoteLogic[i]; + stationComponent.storage = new StationStore[packet.ItemId.Length]; } - if (stationWindow != null && stationWindow.active) - { - conn.SendPacket(new StationSubscribeUIUpdates(true, stationComponent.planetId, stationComponent.id, stationComponent.gid)); - Multiplayer.Session.StationsUI.UIIsSyncedStage++; - stationWindow._Free(); - stationWindow._Init(stationComponent); - stationWindow._stationId = stationComponent.id; - stationWindow._Open(); - stationWindow._Update(); - } + stationComponent.storage[i].itemId = packet.ItemId[i]; + stationComponent.storage[i].max = packet.ItemCountMax[i]; + stationComponent.storage[i].count = packet.ItemCount[i]; + stationComponent.storage[i].inc = packet.ItemInc[i]; + stationComponent.storage[i].remoteOrder = packet.RemoteOrder[i]; + stationComponent.storage[i].localLogic = (ELogisticStorage)packet.LocalLogic[i]; + stationComponent.storage[i].remoteLogic = (ELogisticStorage)packet.RemoteLogic[i]; + } - Multiplayer.Session.StationsUI.UIStationId = stationComponent.id; + UIStationWindow stationWindow = UIRoot.instance.uiGame.stationWindow; + if (stationWindow.active && stationWindow.factory?.planetId == packet.PlanetId && stationWindow.stationId == packet.StationId) + { + //Trigger OnStationIdChange() to refresh window + stationWindow.OnStationIdChange(); } } } diff --git a/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncRequestProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncRequestProcessor.cs index 145922ad5..168fcf2ea 100644 --- a/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncRequestProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncRequestProcessor.cs @@ -20,7 +20,7 @@ public override void ProcessPacket(StationUIInitialSyncRequest packet, NebulaCon return; } - StationComponent stationComponent = null; + StationComponent stationComponent; StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool; StationComponent[] stationPool = GameMain.data.galaxy?.PlanetById(packet.PlanetId)?.factory?.transport?.stationPool; @@ -37,6 +37,7 @@ public override void ProcessPacket(StationUIInitialSyncRequest packet, NebulaCon int[] itemId = new int[storage.Length]; int[] itemCountMax = new int[storage.Length]; int[] itemCount = new int[storage.Length]; + int[] itemInc = new int[storage.Length]; int[] localLogic = new int[storage.Length]; int[] remoteLogic = new int[storage.Length]; int[] remoteOrder = new int[storage.Length]; @@ -46,6 +47,7 @@ public override void ProcessPacket(StationUIInitialSyncRequest packet, NebulaCon itemId[i] = storage[i].itemId; itemCountMax[i] = storage[i].max; itemCount[i] = storage[i].count; + itemInc[i] = storage[i].inc; localLogic[i] = (int)storage[i].localLogic; remoteLogic[i] = (int)storage[i].remoteLogic; remoteOrder[i] = storage[i].remoteOrder; @@ -64,9 +66,11 @@ public override void ProcessPacket(StationUIInitialSyncRequest packet, NebulaCon stationComponent.includeOrbitCollector, stationComponent.energy, stationComponent.energyPerTick, + stationComponent.pilerCount, itemId, itemCountMax, itemCount, + itemInc, localLogic, remoteLogic, remoteOrder diff --git a/NebulaNetwork/PacketProcessors/Logistics/StationUIProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/StationUIProcessor.cs index 337e6f825..7a8ec1931 100644 --- a/NebulaNetwork/PacketProcessors/Logistics/StationUIProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Logistics/StationUIProcessor.cs @@ -3,7 +3,6 @@ using NebulaModel.Packets; using NebulaModel.Packets.Logistics; using NebulaWorld; -using System.Collections.Generic; namespace NebulaNetwork.PacketProcessors.Logistics { @@ -20,60 +19,23 @@ public override void ProcessPacket(StationUI packet, NebulaConnection conn) { if (IsHost) { - // if a user adds/removes a ship, drone or warper or changes max power input broadcast to everyone. - if (Multiplayer.Session.StationsUI.UpdateCooldown == 0 && - (packet.SettingIndex == StationUI.EUISettings.MaxChargePower - || packet.SettingIndex == StationUI.EUISettings.SetDroneCount - || packet.SettingIndex == StationUI.EUISettings.SetShipCount - || packet.SettingIndex == StationUI.EUISettings.SetWarperCount) - ) - { - // this is the SendPacketToAllPlayers() logic but we need to set the mimic flag here. - using (playerManager.GetConnectedPlayers(out Dictionary connectedPlayers)) - { - foreach (KeyValuePair kvp in connectedPlayers) - { - INebulaPlayer p = kvp.Value; - packet.ShouldMimic = ((NebulaConnection)p.Connection) == conn; - p.SendPacket(packet); - } - } - } - else if (packet.SettingIndex == StationUI.EUISettings.AddOrRemoveItemFromStorageResponse) - { - // if someone adds or removes items by hand broadcast to every player on that planet - INebulaPlayer player = playerManager.GetPlayer(conn); - if (player != null) - { - playerManager.SendPacketToPlanet(packet, player.Data.LocalPlanetId); - } - } - else if (Multiplayer.Session.StationsUI.UpdateCooldown == 0 || !packet.IsStorageUI) - { - List subscribers = Multiplayer.Session.StationsUI.GetSubscribers(packet.PlanetId, packet.StationId, packet.StationGId); + // always update values for host + packet.ShouldRefund = false; + Multiplayer.Session.StationsUI.UpdateStation(ref packet); - for (int i = 0; i < subscribers.Count; i++) - { - if (subscribers[i] != null) - { - /* - * as we block the normal method for the client he must run it once he receives this packet. - * but only the one issued the request should do it, we indicate this here - */ - packet.ShouldMimic = subscribers[i] == conn; - subscribers[i].SendPacket(packet); - } - } - } - // always update values for host, but he does not need to rely on the mimic flag (infact its bad for him) - packet.ShouldMimic = false; + // broadcast to every clients that may have the station loaded. + int starId = GameMain.galaxy.PlanetById(packet.PlanetId)?.star.id ?? -1; + playerManager.SendPacketToStarExcept(packet, starId, conn); - Multiplayer.Session.StationsUI.UpdateUI(packet); + // as we block the normal method for the client he must run it once he receives this packet. + // but only the one issued the request should get items refund + packet.ShouldRefund = true; + conn.SendPacket(packet); } if (IsClient) { - Multiplayer.Session.StationsUI.UpdateUI(packet); + Multiplayer.Session.StationsUI.UpdateStation(ref packet); } } } diff --git a/NebulaNetwork/PacketProcessors/Logistics/StorageUIProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/StorageUIProcessor.cs new file mode 100644 index 000000000..4b850bbec --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Logistics/StorageUIProcessor.cs @@ -0,0 +1,42 @@ +using NebulaAPI; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Logistics; +using NebulaWorld; + +namespace NebulaNetwork.PacketProcessors.Logistics +{ + [RegisterPacketProcessor] + internal class StorageUIProcessor : PacketProcessor + { + private readonly IPlayerManager playerManager; + public StorageUIProcessor() + { + playerManager = Multiplayer.Session.Network.PlayerManager; + } + + public override void ProcessPacket(StorageUI packet, NebulaConnection conn) + { + if (IsHost) + { + // always update values for host + packet.ShouldRefund = false; + Multiplayer.Session.StationsUI.UpdateStorage(packet); + + // broadcast to every clients that may have the station loaded. + int starId = GameMain.galaxy.PlanetById(packet.PlanetId)?.star.id ?? -1; + playerManager.SendPacketToStarExcept(packet, starId, conn); + + // as we block some methods for the client he must run it once he receives this packet. + // but only the one issued the request should get items refund + packet.ShouldRefund = true; + conn.SendPacket(packet); + } + + if (IsClient) + { + Multiplayer.Session.StationsUI.UpdateStorage(packet); + } + } + } +} diff --git a/NebulaPatcher/Patches/Dynamic/PlanetTransport_Patch.cs b/NebulaPatcher/Patches/Dynamic/PlanetTransport_Patch.cs index 264a6d8a3..54562b2e4 100644 --- a/NebulaPatcher/Patches/Dynamic/PlanetTransport_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/PlanetTransport_Patch.cs @@ -18,7 +18,7 @@ public static bool SetStationStorage_Postfix(PlanetTransport __instance, int sta if (stationComponent != null) { - StationUI packet = new StationUI(__instance.planet.id, stationComponent.id, stationComponent.gid, storageIdx, itemId, itemCountMax, localLogic, remoteLogic); + StorageUI packet = new StorageUI(__instance.planet.id, stationComponent.id, stationComponent.gid, storageIdx, itemId, itemCountMax, localLogic, remoteLogic); Multiplayer.Session.Network.SendPacket(packet); } diff --git a/NebulaPatcher/Patches/Dynamic/UIStationStorage_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIStationStorage_Patch.cs index 5fd239d37..b90afdd2c 100644 --- a/NebulaPatcher/Patches/Dynamic/UIStationStorage_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIStationStorage_Patch.cs @@ -1,53 +1,81 @@ using HarmonyLib; using NebulaModel.Packets.Logistics; using NebulaWorld; -using UnityEngine.EventSystems; +#pragma warning disable Harmony003 namespace NebulaPatcher.Patches.Dynamic { [HarmonyPatch(typeof(UIStationStorage))] internal class UIStationStorage_Patch { + private static bool eventLock; + + [HarmonyPrefix] + [HarmonyPatch(nameof(UIStationStorage.OnMaxSliderValueChange))] + public static bool OnMaxSliderValueChangePrefix(UIStationStorage __instance, float val) + { + if (Multiplayer.IsActive && !eventLock) + { + if (val != (float)(__instance.station.storage[__instance.index].max / 100)) + { + // If the silder value doesn't match with storage.max, mark it + Multiplayer.Session.StationsUI.StorageMaxChangeId = __instance.index; + } + } + return !Multiplayer.IsActive; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(UIStationStorage._OnUpdate))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] + public static void _OnUpdate_Prefix(UIStationStorage __instance, ref float __state) + { + // Set up eventLock so value changes in maxSlider.value don't trigger changed check + eventLock = true; + __state = __instance.maxSlider.value; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationStorage._OnUpdate))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] + public static void _OnUpdate_Postfix(UIStationStorage __instance, float __state) + { + // Restore the silder value so it is not modified by RefreshValues() + if (Multiplayer.IsActive && Multiplayer.Session.StationsUI.StorageMaxChangeId != -1) + { + __instance.maxSlider.value = __state; + __instance.maxValueText.text = ((int)(__instance.maxSlider.value * 100)).ToString(); + } + eventLock = false; + } + /* * host behaves normally and sends update to clients which then apply the changes * clients send a request to the server and only run the original method once they receive the response */ [HarmonyPrefix] [HarmonyPatch(nameof(UIStationStorage.OnItemIconMouseDown))] - public static bool OnItemIconMouseDown_Postfix(UIStationStorage __instance, BaseEventData evt) + [HarmonyPriority(Priority.First)] + public static void OnItemIconMouseDown_Prefix(UIStationStorage __instance, ref (int,int) __state) + { + __state = (__instance.station.storage[__instance.index].count, __instance.station.storage[__instance.index].inc); + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationStorage.OnItemIconMouseDown))] + [HarmonyPriority(Priority.Last)] + public static void OnItemIconMouseDown_Postfix(UIStationStorage __instance, (int, int) __state) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + if (!Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - Multiplayer.Session.StationsUI.LastMouseEvent = evt; - Multiplayer.Session.StationsUI.LastMouseEventWasDown = true; - StationUI packet; - if (Multiplayer.Session.LocalPlayer.IsHost) - { - PointerEventData pointEventData = evt as PointerEventData; - if (GameMain.mainPlayer.inhandItemId == __instance.station.storage[__instance.index].itemId && pointEventData.button == PointerEventData.InputButton.Left) - { - int diff = __instance.station.storage[__instance.index].max - __instance.station.storage[__instance.index].count; - int amount = (diff >= GameMain.mainPlayer.inhandItemCount) ? GameMain.mainPlayer.inhandItemCount : diff; - if (amount < 0) - { - amount = 0; - } - packet = new StationUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, StationUI.EUISettings.AddOrRemoveItemFromStorageResponse, __instance.station.storage[__instance.index].itemId, __instance.station.storage[__instance.index].count + amount); - Multiplayer.Session.Network.SendPacket(packet); - } - } - else - { - packet = new StationUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, StationUI.EUISettings.AddOrRemoveItemFromStorageRequest, __instance.station.storage[__instance.index].itemId, __instance.station.storage[__instance.index].count); - Multiplayer.Session.Network.SendPacket(packet); - } - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return; + } + StationStore stationStore = __instance.station.storage[__instance.index]; + if (__state.Item1 != stationStore.count || __state.Item2 != stationStore.inc) + { + StorageUI packet = new StorageUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, stationStore.count, stationStore.inc); + Multiplayer.Session.Network.SendPacket(packet); } - return true; } /* @@ -56,35 +84,30 @@ public static bool OnItemIconMouseDown_Postfix(UIStationStorage __instance, Base */ [HarmonyPrefix] [HarmonyPatch(nameof(UIStationStorage.OnItemIconMouseUp))] - public static bool OnItemIconMouseUp_Postfix(UIStationStorage __instance, BaseEventData evt) + [HarmonyPriority(Priority.First)] + public static void OnItemIconMouseUp_Prefix(UIStationStorage __instance, ref (int, int) __state) + { + __state = (__instance.station.storage[__instance.index].count, __instance.station.storage[__instance.index].inc); + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationStorage.OnItemIconMouseUp))] + [HarmonyPriority(Priority.Last)] + public static void OnItemIconMouseUp_Postfix(UIStationStorage __instance, (int, int) __state) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + if (!Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - Multiplayer.Session.StationsUI.LastMouseEvent = evt; - Multiplayer.Session.StationsUI.LastMouseEventWasDown = false; - StationUI packet; - if (Multiplayer.Session.LocalPlayer.IsHost) - { - if (__instance.insplit) - { - int splitVal = UIRoot.instance.uiGame.gridSplit.value; - int diff = (splitVal >= __instance.station.storage[__instance.index].count) ? __instance.station.storage[__instance.index].count : splitVal; - packet = new StationUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, StationUI.EUISettings.AddOrRemoveItemFromStorageResponse, __instance.station.storage[__instance.index].itemId, __instance.station.storage[__instance.index].count - diff); - Multiplayer.Session.Network.SendPacket(packet); - } - } - else - { - packet = new StationUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, StationUI.EUISettings.AddOrRemoveItemFromStorageRequest, __instance.station.storage[__instance.index].itemId, __instance.station.storage[__instance.index].count); - Multiplayer.Session.Network.SendPacket(packet); - } - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return; + } + StationStore stationStore = __instance.station.storage[__instance.index]; + + if (__state.Item1 != stationStore.count || __state.Item2 != stationStore.inc) + + { + StorageUI packet = new StorageUI(__instance.stationWindow.factory.planet.id, __instance.station.id, __instance.station.gid, __instance.index, stationStore.count, stationStore.inc); + Multiplayer.Session.Network.SendPacket(packet); } - return true; } } } +#pragma warning restore Harmony003 diff --git a/NebulaPatcher/Patches/Dynamic/UIStationWindow_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIStationWindow_Patch.cs index 7aed6d18a..e4e6b4e58 100644 --- a/NebulaPatcher/Patches/Dynamic/UIStationWindow_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIStationWindow_Patch.cs @@ -1,7 +1,7 @@ using HarmonyLib; -using NebulaModel.Logger; using NebulaModel.Packets.Logistics; using NebulaWorld; +using UnityEngine; using UnityEngine.EventSystems; namespace NebulaPatcher.Patches.Dynamic @@ -9,359 +9,331 @@ namespace NebulaPatcher.Patches.Dynamic [HarmonyPatch(typeof(UIStationWindow))] internal class UIStationWindow_Patch { + private static long lastUpdateGametick; + [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnMaxChargePowerSliderValueChange))] public static bool OnMaxChargePowerSliderValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.MaxChargePower, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MaxChargePower; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnMaxTripDroneSliderValueChange))] public static bool OnMaxTripDroneSliderValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.MaxTripDrones, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MaxTripDrones; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnMaxTripVesselSliderValueChange))] public static bool OnMaxTripVesselSliderValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.MaxTripVessel, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MaxTripVessel; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnMinDeliverDroneValueChange))] public static bool OnMinDeliverDroneValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.MinDeliverDrone, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MinDeliverDrone; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnMinDeliverVesselValueChange))] public static bool OnMinDeliverVesselValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.MinDeliverVessel, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MinDeliverVessel; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(UIStationWindow.OnMaxMiningSpeedChange))] + public static bool OnMaxMiningSpeedChangee_Prefix(UIStationWindow __instance, float value) + { + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) + { + return true; + } + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.MaxMiningSpeed; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnWarperDistanceValueChange))] public static bool OnWarperDistanceValueChange_Prefix(UIStationWindow __instance, float value) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.WarpDistance, value); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.WarpDistance; + Multiplayer.Session.StationsUI.SliderBarPacket.SettingValue = value; + return Multiplayer.Session.LocalPlayer.IsHost; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationWindow.OnMinPilerValueChange))] + [HarmonyPatch(nameof(UIStationWindow.OnTechPilerClick))] + public static void OnPilerCountChange(UIStationWindow __instance) + { + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) + { + return; + } + + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.PilerCount, stationComponent.pilerCount); + Multiplayer.Session.Network.SendPacket(packet); } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnWarperNecessaryClick))] public static bool OnWarperNecessaryClick_Prefix(UIStationWindow __instance) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.WarperNeeded, 0f); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.WarperNeeded, 0f); + Multiplayer.Session.Network.SendPacket(packet); + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnIncludeOrbitCollectorClick))] public static bool OnIncludeOrbitCollectorClick_Prefix(UIStationWindow __instance) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS && (Multiplayer.Session.StationsUI.UIIsSyncedStage == 2 || Multiplayer.Session.LocalPlayer.IsHost)) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.IncludeCollectors, 0f); - Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) - { - return true; - } - return false; + return true; } - return true; + StationUI packet = new StationUI(__instance.factory.planet.id, __instance.factory.transport.stationPool[__instance.stationId].id, __instance.factory.transport.stationPool[__instance.stationId].gid, StationUI.EUISettings.IncludeCollectors, 0f); + Multiplayer.Session.Network.SendPacket(packet); + return Multiplayer.Session.LocalPlayer.IsHost; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnDroneIconClick))] - public static bool OnDroneIconClick_Prefix(UIStationWindow __instance) + [HarmonyPriority(Priority.First)] + public static void OnDroneIconClick_Prefix(UIStationWindow __instance, ref int __state) + { + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + __state = stationComponent.idleDroneCount; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationWindow.OnDroneIconClick))] + [HarmonyPriority(Priority.Last)] + public static void OnDroneIconClick_Posfix(UIStationWindow __instance, int __state) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - Player player = GameMain.mainPlayer; - if (player.inhandItemCount > 0 && player.inhandItemId != 5001) - { - ItemProto itemProto = LDB.items.Select(5001); - UIRealtimeTip.Popup("只能放入".Translate() + itemProto.name, true, 0); - return false; - } - StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; - ItemProto stationItem = LDB.items.Select(__instance.factory.entityPool[stationComponent.entityId].protoId); - - int toAdd; - if (player.inhandItemCount > 0) - { - int droneAmount = stationComponent.idleDroneCount + stationComponent.workDroneCount; - int spaceLeft = stationItem.prefabDesc.stationMaxDroneCount - droneAmount; - if (spaceLeft < 0) - { - spaceLeft = 0; - } - toAdd = (__instance.player.inhandItemCount >= spaceLeft) ? spaceLeft : __instance.player.inhandItemCount; - } - else - { - toAdd = stationComponent.idleDroneCount * -1; - } - if (!Multiplayer.Session.LocalPlayer.IsHost) - { - Multiplayer.Session.StationsUI.UIRequestedShipDronWarpChange = true; - } + return; + } - StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetDroneCount, stationComponent.idleDroneCount + toAdd); + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + if (__state != stationComponent.idleDroneCount) + { + int droneCount = stationComponent.idleDroneCount + stationComponent.workDroneCount; + StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetDroneCount, droneCount); Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) + if (Multiplayer.Session.LocalPlayer.IsClient) { - return true; + // Revert drone count until host verify + stationComponent.idleDroneCount = __state; + __instance.droneIconButton.button.interactable = false; } - return false; } - return true; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnShipIconClick))] - public static bool OnShipIconClick_Prefix(UIStationWindow __instance) + [HarmonyPriority(Priority.First)] + public static void OnShipIconClick_Prefix(UIStationWindow __instance, ref int __state) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + __state = stationComponent.idleShipCount; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationWindow.OnShipIconClick))] + [HarmonyPriority(Priority.Last)] + public static void OnShipIconClick_Posfix(UIStationWindow __instance, int __state) + { + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - Player player = GameMain.mainPlayer; - if (player.inhandItemCount > 0 && player.inhandItemId != 5002) - { - ItemProto itemProto = LDB.items.Select(5002); - UIRealtimeTip.Popup("只能放入".Translate() + itemProto.name, true, 0); - return false; - } - StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; - ItemProto stationItem = LDB.items.Select(__instance.factory.entityPool[stationComponent.entityId].protoId); - - int toAdd; - if (player.inhandItemCount > 0) - { - int shipAmount = stationComponent.idleShipCount + stationComponent.workShipCount; - int spaceLeft = stationItem.prefabDesc.stationMaxShipCount - shipAmount; - if (spaceLeft < 0) - { - spaceLeft = 0; - } - toAdd = (__instance.player.inhandItemCount >= spaceLeft) ? spaceLeft : __instance.player.inhandItemCount; - } - else - { - toAdd = stationComponent.idleShipCount * -1; - } - if (!Multiplayer.Session.LocalPlayer.IsHost) - { - Multiplayer.Session.StationsUI.UIRequestedShipDronWarpChange = true; - } - StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetShipCount, stationComponent.idleShipCount + toAdd); - Multiplayer.Session.Network.SendPacket(packet); + return; + } - if (Multiplayer.Session.LocalPlayer.IsHost) + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + if (__state != stationComponent.idleShipCount) + { + int ShipCount = stationComponent.idleShipCount + stationComponent.workShipCount; + StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetShipCount, ShipCount); + Multiplayer.Session.Network.SendPacket(packet); + if (Multiplayer.Session.LocalPlayer.IsClient) { - return true; + // Revert ship count until host verify + stationComponent.idleShipCount = __state; + __instance.shipIconButton.button.interactable = false; } - return false; } - return true; } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow.OnWarperIconClick))] - public static bool OnWarperIconClick_Prefix(UIStationWindow __instance) + [HarmonyPriority(Priority.First)] + public static void OnWarperIconClick_Prefix(UIStationWindow __instance, ref int __state) + { + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + __state = stationComponent.warperCount; + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationWindow.OnWarperIconClick))] + [HarmonyPriority(Priority.Last)] + public static void OnWarperIconClick_Posfix(UIStationWindow __instance, int __state) { - if (Multiplayer.IsActive && !Multiplayer.Session.Ships.PatchLockILS) + if (__instance.event_lock || !Multiplayer.IsActive || Multiplayer.Session.Ships.PatchLockILS) { - Player player = GameMain.mainPlayer; - if (player.inhandItemCount > 0 && player.inhandItemId != 1210) - { - ItemProto itemProto = LDB.items.Select(1210); - UIRealtimeTip.Popup("只能放入".Translate() + itemProto.name, true, 0); - return false; - } - StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; - - int toAdd; - if (player.inhandItemCount > 0) - { - int spaceLeft = stationComponent.warperMaxCount - stationComponent.warperCount; - if (spaceLeft < 0) - { - spaceLeft = 0; - } - toAdd = (__instance.player.inhandItemCount >= spaceLeft) ? spaceLeft : __instance.player.inhandItemCount; - } - else - { - toAdd = stationComponent.warperCount * -1; - } - if (!Multiplayer.Session.LocalPlayer.IsHost) - { - Multiplayer.Session.StationsUI.UIRequestedShipDronWarpChange = true; - } + return; + } - StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetWarperCount, stationComponent.warperCount + toAdd); + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + if (__state != stationComponent.warperCount) + { + StationUI packet = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.SetWarperCount, stationComponent.warperCount); Multiplayer.Session.Network.SendPacket(packet); - if (Multiplayer.Session.LocalPlayer.IsHost) + if (Multiplayer.Session.LocalPlayer.IsClient) { - return true; + // Revert warper count until host verify + stationComponent.warperCount = __state; + __instance.warperIconButton.button.interactable = false; } - return false; } - return true; } - [HarmonyPrefix] - [HarmonyPatch(nameof(UIStationWindow.OnStationIdChange))] - public static bool OnStationIdChange_Prefix(UIStationWindow __instance) + [HarmonyPostfix] + [HarmonyPatch(nameof(UIStationWindow._OnOpen))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] + public static void _OnOpen_Postfix(UIStationWindow __instance) { - if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.StationsUI.UIIsSyncedStage > 0 || GameMain.localPlanet == null || !__instance.active) - { - return true; - } - __instance.titleText.text = "Loading..."; - Multiplayer.Session.StationsUI.LastSelectedGameObj = EventSystem.current.currentSelectedGameObject; - if (__instance.factory == null) - { - __instance.factory = GameMain.localPlanet.factory; - } - if (__instance.transport == null) - { - __instance.transport = __instance.factory.transport; - } - StationComponent stationComponent = null; - if (__instance.stationId == 0) + if (!Multiplayer.IsActive || __instance.transport == null) { - UIStationStorage[] stationStorage = __instance.storageUIs; - if (stationStorage != null && stationStorage[0] != null && stationStorage[0].station.id != 0) - { - stationComponent = __instance.transport.stationPool[stationStorage[0].station.id]; - } + return; } - else + + StationComponent stationComponent = __instance.transport.stationPool[__instance.stationId]; + Multiplayer.Session.StationsUI.SliderBarPacket = new StationUI(__instance.factory.planet.id, stationComponent.id, stationComponent.gid, StationUI.EUISettings.None, 0); + Multiplayer.Session.StationsUI.StorageMaxChangeId = -1; + if (Multiplayer.Session.LocalPlayer.IsHost) { - stationComponent = __instance.transport.stationPool[__instance.stationId]; + return; } - if (stationComponent != null && GameMain.localPlanet != null) + lastUpdateGametick = GameMain.gameTick; + + // Stage 0 : Hide UI elements until sync data arrive + __instance.titleText.text = "Loading..."; + for (int i = 0; i < __instance.storageUIs.Length; i++) { - int id = (stationComponent.isStellar == true) ? stationComponent.gid : stationComponent.id; - // for some reason PLS has planetId set to 0, so we use players localPlanet here (he should be on a planet anyways when opening the UI) - Multiplayer.Session.Network.SendPacket(new StationUIInitialSyncRequest(stationComponent.planetId, stationComponent.id, stationComponent.gid)); - Multiplayer.Session.StationsUI.UIIsSyncedStage++; + __instance.storageUIs[i]._Close(); + __instance.storageUIs[i].ClosePopMenu(); } - return false; + __instance.panelDown.SetActive(false); + + // for some reason advance miner has planetId set to 0, so we use UI's factory planetId + Multiplayer.Session.Network.SendPacket(new StationUIInitialSyncRequest(__instance.factory.planetId, stationComponent.id, stationComponent.gid)); } [HarmonyPrefix] [HarmonyPatch(nameof(UIStationWindow._OnUpdate))] [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] - public static bool _OnUpdate_Prefix() + public static bool _OnUpdate_Prefix(UIStationWindow __instance) { - if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.StationsUI.UIIsSyncedStage == 2) + if (!Multiplayer.IsActive) { return true; } - return false; + + // When releasing left mouse button + if (Input.GetMouseButtonUp(0)) + { + if (Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex != StationUI.EUISettings.None) + { + // Send SliderBarPacket when left mouse button is released + Multiplayer.Session.Network.SendPacket(Multiplayer.Session.StationsUI.SliderBarPacket); + Multiplayer.Session.StationsUI.SliderBarPacket.SettingIndex = StationUI.EUISettings.None; + } + if (Multiplayer.Session.StationsUI.StorageMaxChangeId >= 0) + { + // Do the job in UIStationStorage.OnMaxSliderValueChange() + int index = Multiplayer.Session.StationsUI.StorageMaxChangeId; + float val = __instance.storageUIs[index].maxSlider.value; + StationStore stationStore = __instance.transport.stationPool[__instance.stationId].storage[index]; + __instance.transport.SetStationStorage(__instance.stationId, index, stationStore.itemId, (int)(val * 100f + 0.5f), stationStore.localLogic, stationStore.remoteLogic, GameMain.mainPlayer); + + // In client side, preserve displaying slider value until host response + Multiplayer.Session.StationsUI.StorageMaxChangeId = Multiplayer.Session.LocalPlayer.IsHost ? -1 : -2; + } + } + + // Request for remoteOrder update every 180tick + if (Multiplayer.Session.LocalPlayer.IsClient && GameMain.gameTick - lastUpdateGametick > 180) + { + int gid = __instance.transport?.stationPool?[__instance.stationId].gid ?? 0; + if (gid > 0) + { + Multiplayer.Session.Network.SendPacket(new RemoteOrderUpdate(gid, System.Array.Empty())); + } + lastUpdateGametick = GameMain.gameTick; + } + + return true; } [HarmonyPostfix] - [HarmonyPatch(nameof(UIStationWindow._OnClose))] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] - public static void _OnClose_Postfix(UIStationWindow __instance) + [HarmonyPatch(nameof(UIStationWindow.OnStationIdChange))] + public static void OnStationIdChange_Postfix(UIStationWindow __instance) { if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) { return; } - if (__instance.factory == null) - { - __instance.factory = GameMain.localPlanet.factory; - } - if (__instance.transport == null) - { - __instance.transport = __instance.factory.transport; - } - if (__instance.stationId != 0 || Multiplayer.Session.StationsUI.UIStationId != 0) - { - // it is actually 0 before we manually set it to the right value in StationUIInitialSyncProcessor.cs and thus its a good check to skip sending the packet on the Free() call - Multiplayer.Session.Network.SendPacket(new StationSubscribeUIUpdates(false, __instance.transport.planet.id, __instance.transport.stationPool[Multiplayer.Session.StationsUI.UIStationId].id, __instance.transport.stationPool[Multiplayer.Session.StationsUI.UIStationId].gid)); - Multiplayer.Session.StationsUI.LastSelectedGameObj = null; - Multiplayer.Session.StationsUI.UIIsSyncedStage = 0; - Multiplayer.Session.StationsUI.UIStationId = 0; - } + + __instance.warperIconButton.button.interactable = true; + __instance.shipIconButton.button.interactable = true; + __instance.droneIconButton.button.interactable = true; } } } diff --git a/NebulaWorld/Logistics/StationUIManager.cs b/NebulaWorld/Logistics/StationUIManager.cs index 011880b40..8dd1510c9 100644 --- a/NebulaWorld/Logistics/StationUIManager.cs +++ b/NebulaWorld/Logistics/StationUIManager.cs @@ -1,129 +1,67 @@ using NebulaModel.Logger; -using NebulaModel.Networking; using NebulaModel.Packets.Logistics; using System; -using System.Collections.Generic; using UnityEngine; -using UnityEngine.EventSystems; namespace NebulaWorld.Logistics { - public class Subscribers - { - public int PlanetId { get; } - public int StationId { get; } - public int StationGId { get; } - public List Connections { get; set; } - public Subscribers(int planetId, int stationId, int stationGId) - { - PlanetId = planetId; - StationId = stationId; - StationGId = stationGId; - Connections = new List(); - } - - public override string ToString() - { - return $"{PlanetId}.{StationId}.{StationGId}"; - } - - public static string GetKey(int planetId, int stationId, int statgionGId) - { - return $"{planetId}.{stationId}.{statgionGId}"; - } - } - public class StationUIManager : IDisposable { - private Dictionary _stationUISubscribers; - - public int UpdateCooldown; // cooldown is used to slow down updates on storage slider - public BaseEventData LastMouseEvent; - public bool LastMouseEventWasDown; - public GameObject LastSelectedGameObj; - public int UIIsSyncedStage; // 0 == not synced, 1 == request sent, 2 == synced | this is only used client side - public int UIStationId; - public bool UIRequestedShipDronWarpChange; // when receiving a ship, drone or warp change only take/add items from the one issuing the request + public int UpdateCooldown; // cooldown is reserved for future use + public bool UIRequestedShipDronWarpChange { get; set; } // when receiving a ship, drone or warp change only take/add items from the one issuing the request + public StationUI SliderBarPacket { get; set; } // store the change of slider bar temporary, only send it when mouse button is released. + public int StorageMaxChangeId { get; set; } // index of the storage that its slider value changed by the user. -1: None, -2: Syncing public StationUIManager() { - _stationUISubscribers = new Dictionary(); } public void Dispose() { - _stationUISubscribers = null; } - // When a client opens a station's UI he requests a subscription for live updates, so add him to the list - public void AddSubscriber(int planetId, int stationId, int stationGId, NebulaConnection connection) - { - // Attempt to find existing subscribers to a specific station, if we couldn't find an existing one - // we must initialize a new Subscribers for this specific station. - if (!_stationUISubscribers.TryGetValue(Subscribers.GetKey(planetId, stationId, stationGId), out Subscribers subscribers)) - { - _stationUISubscribers.Add(Subscribers.GetKey(planetId, stationId, stationGId), new Subscribers(planetId, stationId, stationGId)); - } - - _stationUISubscribers.TryGetValue(Subscribers.GetKey(planetId, stationId, stationGId), out subscribers); - - subscribers?.Connections.Add(connection); - } - public void RemoveSubscriber(int planetId, int stationId, int stationGId, NebulaConnection connection) + public void DecreaseCooldown() { - if (_stationUISubscribers.TryGetValue(Subscribers.GetKey(planetId, stationId, stationGId), out Subscribers subscribers)) + // cooldown is reserved for future use + if (UpdateCooldown > 0) { - subscribers.Connections.Remove(connection); - - if (subscribers.Connections.Count == 0) - { - _stationUISubscribers.Remove(subscribers.ToString()); - } + UpdateCooldown--; } } - public List GetSubscribers(int planetId, int stationId, int stationGId) + public void UpdateStation(ref StationUI packet) { - if (!_stationUISubscribers.TryGetValue(Subscribers.GetKey(planetId, stationId, stationGId), out Subscribers subscribers)) + StationComponent stationComponent = GetStation(packet.PlanetId, packet.StationId, packet.StationGId); + if (stationComponent == null) { - return new List(); + Log.Warn($"StationUI: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}"); + return; } - - return subscribers.Connections; + UpdateSettingsUI(stationComponent, ref packet); + RefreshWindow(packet.PlanetId, packet.StationId); } - public void DecreaseCooldown() + public void UpdateStorage(StorageUI packet) { - // cooldown is for the storage sliders - if (UpdateCooldown > 0) + StationComponent stationComponent = GetStation(packet.PlanetId, packet.StationId, packet.StationGId); + if (stationComponent == null) { - UpdateCooldown--; + Log.Warn($"StorageUI: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}"); + return; } + UpdateStorageUI(stationComponent, packet); + RefreshWindow(packet.PlanetId, packet.StationId); } - public void UpdateUI(StationUI packet) + private void RefreshWindow(int planetId, int stationId) { - if ((UpdateCooldown == 0 || !packet.IsStorageUI) && Multiplayer.Session.LocalPlayer.IsHost) - { - UpdateCooldown = 10; - if (packet.IsStorageUI) - { - UpdateStorageUI(packet); - } - else - { - UpdateSettingsUI(packet); - } - } - else if (!Multiplayer.Session.LocalPlayer.IsHost) + // If station window is opened and veiwing the updating station, refresh the window. + UIStationWindow stationWindow = UIRoot.instance.uiGame.stationWindow; + if (stationWindow != null && stationWindow.active) { - if (packet.IsStorageUI) + if (stationWindow.factory?.planetId == planetId && stationWindow.stationId == stationId) { - UpdateStorageUI(packet); - } - else - { - UpdateSettingsUI(packet); + stationWindow.OnStationIdChange(); } } } @@ -131,33 +69,53 @@ public void UpdateUI(StationUI packet) /** * Updates to a given station that should happen in the background. */ - private void UpdateSettingsUIBackground(StationUI packet, PlanetData planet, StationComponent stationComponent) + private void UpdateSettingsUI(StationComponent stationComponent, ref StationUI packet) { - StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool; - - // update drones, ships, warpers and energy consumption for everyone - if ((packet.SettingIndex >= StationUI.EUISettings.SetDroneCount && packet.SettingIndex <= StationUI.EUISettings.SetWarperCount) || packet.SettingIndex == StationUI.EUISettings.MaxChargePower) + // SetDroneCount, SetShipCount may change packet.SettingValue + switch (packet.SettingIndex) { - if (packet.SettingIndex == (int)StationUI.EUISettings.MaxChargePower && planet.factory?.powerSystem != null) + case StationUI.EUISettings.MaxChargePower: { - PowerConsumerComponent[] consumerPool = planet.factory.powerSystem.consumerPool; - if (consumerPool.Length > stationComponent.pcId) + PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); + if (planet.factory?.powerSystem != null) { - consumerPool[stationComponent.pcId].workEnergyPerTick = (long)(50000.0 * packet.SettingValue + 0.5); + PowerConsumerComponent[] consumerPool = planet.factory.powerSystem.consumerPool; + if (consumerPool.Length > stationComponent.pcId) + { + consumerPool[stationComponent.pcId].workEnergyPerTick = (long)(50000.0 * packet.SettingValue + 0.5); + } } + break; } - - if (packet.SettingIndex == StationUI.EUISettings.SetDroneCount) + case StationUI.EUISettings.SetDroneCount: { - stationComponent.idleDroneCount = (int)packet.SettingValue; + // Check if new setting is acceptable + int totalCount = Math.Min((int)packet.SettingValue, stationComponent.workDroneDatas.Length); + stationComponent.idleDroneCount = Math.Max(totalCount - stationComponent.workDroneCount, 0); + if (totalCount < (int)packet.SettingValue && packet.ShouldRefund) + { + // The result is less than original setting, refund extra drones to author + int refund = (int)packet.SettingValue - totalCount; + GameMain.mainPlayer.TryAddItemToPackage(5001, refund, 0, true); + } + packet.SettingValue = totalCount; + break; } - - if (packet.SettingIndex == StationUI.EUISettings.SetShipCount) + case StationUI.EUISettings.SetShipCount: { - stationComponent.idleShipCount = (int)packet.SettingValue; + // Check if new setting is acceptable + int totalCount = Math.Min((int)packet.SettingValue, stationComponent.workShipDatas.Length); + stationComponent.idleShipCount = Math.Max(totalCount - stationComponent.workShipCount, 0); + if (totalCount < (int)packet.SettingValue && packet.ShouldRefund) + { + // The result is less than original setting, refund extra ships to author + int refund = (int)packet.SettingValue - totalCount; + GameMain.mainPlayer.TryAddItemToPackage(5002, refund, 0, true); + } + packet.SettingValue = totalCount; + break; } - - if (packet.SettingIndex == StationUI.EUISettings.SetWarperCount) + case StationUI.EUISettings.SetWarperCount: { stationComponent.warperCount = (int)packet.SettingValue; if (stationComponent.storage != null && packet.WarperShouldTakeFromStorage) @@ -171,93 +129,91 @@ private void UpdateSettingsUIBackground(StationUI packet, PlanetData planet, Sta } } } + break; } - } - - if (packet.SettingIndex == StationUI.EUISettings.MaxTripDrones) - { - stationComponent.tripRangeDrones = Math.Cos(packet.SettingValue / 180.0 * 3.141592653589793); - } - - if (packet.SettingIndex == StationUI.EUISettings.MaxTripVessel) - { - double value = packet.SettingValue; - if (value > 40.5) + case StationUI.EUISettings.MaxTripDrones: { - value = 10000.0; + stationComponent.tripRangeDrones = Math.Cos(packet.SettingValue / 180.0 * 3.141592653589793); + break; } - else if (value > 20.5) + case StationUI.EUISettings.MaxTripVessel: { - value = value * 2f - 20f; + double value = packet.SettingValue; + if (value > 40.5) + { + value = 10000.0; + } + else if (value > 20.5) + { + value = value * 2f - 20f; + } + stationComponent.tripRangeShips = 2400000.0 * value; + break; } - - stationComponent.tripRangeShips = 2400000.0 * value; - } - - if (packet.SettingIndex == StationUI.EUISettings.MinDeliverDrone) - { - int value = (int)(packet.SettingValue * 10f + 0.5f); - if (value < 1) + case StationUI.EUISettings.MinDeliverDrone: { - value = 1; + int value = (int)(packet.SettingValue * 10f + 0.5f); + stationComponent.deliveryDrones = value < 1 ? 1 : value; + break; } - - stationComponent.deliveryDrones = value; - } - - if (packet.SettingIndex == StationUI.EUISettings.MinDeliverVessel) - { - int value = (int)(packet.SettingValue * 10f + 0.5f); - if (value < 1) - { - value = 1; - } - - stationComponent.deliveryShips = value; - } - - if (packet.SettingIndex == StationUI.EUISettings.WarpDistance) - { - double value = packet.SettingValue; - if (value < 1.5) + case StationUI.EUISettings.MinDeliverVessel: { - value = 0.2; + int value = (int)(packet.SettingValue * 10f + 0.5f); + stationComponent.deliveryShips = value < 1 ? 1 : value; + break; } - else if (value < 7.5) + case StationUI.EUISettings.WarpDistance: { - value = value * 0.5 - 0.5; + double value = packet.SettingValue; + if (value < 1.5) + { + value = 0.2; + } + else if (value < 7.5) + { + value = value * 0.5 - 0.5; + } + else if (value < 16.5) + { + value -= 4f; + } + else if (value < 20.5) + { + value = value * 2f - 20f; + } + else + { + value = 60; + } + stationComponent.warpEnableDist = 40000.0 * value; + break; } - else if (value < 16.5) + case StationUI.EUISettings.WarperNeeded: { - value -= 4f; + stationComponent.warperNecessary = !stationComponent.warperNecessary; + break; } - else if (value < 20.5) + case StationUI.EUISettings.IncludeCollectors: { - value = value * 2f - 20f; + stationComponent.includeOrbitCollector = !stationComponent.includeOrbitCollector; + break; } - else + case StationUI.EUISettings.PilerCount: { - value = 60; + stationComponent.pilerCount = (int)packet.SettingValue; + break; } - - stationComponent.warpEnableDist = 40000.0 * value; - } - - if (packet.SettingIndex == StationUI.EUISettings.WarperNeeded) - { - stationComponent.warperNecessary = !stationComponent.warperNecessary; - } - - if (packet.SettingIndex == StationUI.EUISettings.IncludeCollectors) - { - stationComponent.includeOrbitCollector = !stationComponent.includeOrbitCollector; - } - - if (packet.SettingIndex == StationUI.EUISettings.AddOrRemoveItemFromStorageResponse) - { - if (stationComponent.storage != null) - { - stationComponent.storage[packet.StorageIdx].count = (int)packet.SettingValue; + case StationUI.EUISettings.MaxMiningSpeed: + { + PlanetFactory factory = GameMain.galaxy.PlanetById(packet.PlanetId).factory; + if (factory != null) + { + int speed = 10000 + (int)(packet.SettingValue + 0.5f) * 1000; + long workEnergyPrefab = LDB.items.Select(factory.entityPool[stationComponent.entityId].protoId).prefabDesc.workEnergyPerTick; + factory.factorySystem.minerPool[stationComponent.minerId].speed = speed; + factory.powerSystem.consumerPool[stationComponent.pcId].workEnergyPerTick = (long)(workEnergyPrefab * (speed / 10000f) * (speed / 10000f)); + } + break; } } } @@ -267,193 +223,40 @@ private void UpdateSettingsUIBackground(StationUI packet, PlanetData planet, Sta * * First determine if the local player has the station window opened and handle that accordingly. */ - private void UpdateSettingsUI(StationUI packet) + private StationComponent GetStation(int planetId, int stationId, int stationGid) { - UIStationWindow stationWindow = UIRoot.instance.uiGame.stationWindow; - - StationComponent stationComponent = null; - PlanetData planet = GameMain.galaxy?.PlanetById(packet.PlanetId); + PlanetData planet = GameMain.galaxy?.PlanetById(planetId); // If we can't find planet or the factory for said planet, we can just skip this if (planet?.factory?.transport == null) { - return; + return null; } StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool; StationComponent[] stationPool = planet?.factory?.transport?.stationPool; // Figure out if we're dealing with a PLS or a ILS station - stationComponent = packet.StationGId > 0 ? gStationPool[packet.StationGId] : stationPool?[packet.StationId]; - - if (stationComponent == null) - { - Log.Error($"UpdateStorageUI: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}"); - return; - } - - if (stationWindow == null) - { - return; - } - - int _stationId = stationWindow._stationId; - - // Client has no knowledge of the planet, closed the window or - // opened a different station, do all updates in the background. - if (planet?.factory?.transport == null || stationComponent.id != _stationId) - { - UpdateSettingsUIBackground(packet, planet, stationComponent); - return; - } + StationComponent stationComponent = stationGid > 0 ? gStationPool[stationGid] : stationPool?[stationId]; + return stationComponent; + } - // this locks the patches so we can call vanilla functions without triggering our patches to avoid endless loops + private void UpdateStorageUI(StationComponent stationComponent, StorageUI packet) + { using (Multiplayer.Session.Ships.PatchLockILS.On()) { - if (packet.SettingIndex == StationUI.EUISettings.MaxChargePower) - { - stationWindow.OnMaxChargePowerSliderValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.MaxTripDrones) - { - stationWindow.OnMaxTripDroneSliderValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.MaxTripVessel) - { - stationWindow.OnMaxTripVesselSliderValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.MinDeliverDrone) - { - stationWindow.OnMinDeliverDroneValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.MinDeliverVessel) - { - stationWindow.OnMinDeliverVesselValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.WarpDistance) - { - stationWindow.OnWarperDistanceValueChange(packet.SettingValue); - } - if (packet.SettingIndex == StationUI.EUISettings.WarperNeeded) - { - stationWindow.OnWarperNecessaryClick(0); - } - if (packet.SettingIndex == StationUI.EUISettings.IncludeCollectors) - { - stationWindow.OnIncludeOrbitCollectorClick(0); - } - if (packet.SettingIndex >= StationUI.EUISettings.SetDroneCount && packet.SettingIndex <= StationUI.EUISettings.SetWarperCount) - { - if (packet.SettingIndex == StationUI.EUISettings.SetDroneCount) - { - if (UIRequestedShipDronWarpChange) - { - stationWindow.OnDroneIconClick(0); - UIRequestedShipDronWarpChange = false; - } - stationComponent.idleDroneCount = (int)packet.SettingValue; - } - if (packet.SettingIndex == StationUI.EUISettings.SetShipCount) - { - if (UIRequestedShipDronWarpChange) - { - stationWindow.OnShipIconClick(0); - UIRequestedShipDronWarpChange = false; - } - stationComponent.idleShipCount = (int)packet.SettingValue; - } - if (packet.SettingIndex == StationUI.EUISettings.SetWarperCount) - { - if (UIRequestedShipDronWarpChange) - { - stationWindow.OnWarperIconClick(0); - UIRequestedShipDronWarpChange = false; - } - stationComponent.warperCount = (int)packet.SettingValue; - - if (stationComponent.storage != null && packet.WarperShouldTakeFromStorage) - { - for (int i = 0; i < stationComponent.storage.Length; i++) - { - if (stationComponent.storage[i].itemId == 1210 && stationComponent.storage[i].count > 0) - { - stationComponent.storage[i].count--; - break; - } - } - } - } - } - /* - * the idea is that clients request that they want to apply a change and do so once the server responded with an okay. - * the calls to OnItemIconMouseDown() and OnItemIconMouseUp() are blocked for clients and called only from here. - */ - if (packet.SettingIndex == StationUI.EUISettings.AddOrRemoveItemFromStorageRequest) + PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); + if (packet.ItemCount == -1) { - if (stationComponent.storage != null) - { - if (packet.ShouldMimic) - { - BaseEventData mouseEvent = LastMouseEvent; - UIStationStorage[] storageUIs = stationWindow.storageUIs; - - if (LastMouseEvent != null) - { - // TODO: change this such that only server sends the response, else clients with a desynced state could change servers storage to a faulty value - // issue #249 - if (LastMouseEventWasDown) - { - storageUIs[packet.StorageIdx].OnItemIconMouseDown(mouseEvent); - StationUI packet2 = new StationUI(packet.PlanetId, packet.StationId, packet.StationGId, packet.StorageIdx, StationUI.EUISettings.AddOrRemoveItemFromStorageResponse, packet.ItemId, stationComponent.storage[packet.StorageIdx].count); - Multiplayer.Session.Network.SendPacket(packet2); - } - else - { - storageUIs[packet.StorageIdx].OnItemIconMouseUp(mouseEvent); - StationUI packet2 = new StationUI(packet.PlanetId, packet.StationId, packet.StationGId, packet.StorageIdx, StationUI.EUISettings.AddOrRemoveItemFromStorageResponse, packet.ItemId, stationComponent.storage[packet.StorageIdx].count); - Multiplayer.Session.Network.SendPacket(packet2); - } - LastMouseEvent = null; - } - } - } + planet.factory.transport.SetStationStorage(stationComponent.id, packet.StorageIdx, packet.ItemId, packet.ItemCountMax, packet.LocalLogic, packet.RemoteLogic, (packet.ShouldRefund == true) ? GameMain.mainPlayer : null); + StorageMaxChangeId = -1; } - if (packet.SettingIndex == StationUI.EUISettings.AddOrRemoveItemFromStorageResponse) + else { - if (stationComponent.storage != null) - { - stationComponent.storage[packet.StorageIdx].count = (int)packet.SettingValue; - } + stationComponent.storage[packet.StorageIdx].count = packet.ItemCount; + stationComponent.storage[packet.StorageIdx].inc = packet.ItemInc; } } } - - private void UpdateStorageUI(StationUI packet) - { - StationComponent stationComponent = null; - PlanetData planet = GameMain.galaxy?.PlanetById(packet.PlanetId); - - // If we can't find planet or the factory for said planet, we can just skip this - if (planet?.factory?.transport == null) - { - return; - } - - StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool; - StationComponent[] stationPool = planet?.factory?.transport?.stationPool; - - stationComponent = packet.StationGId > 0 ? gStationPool[packet.StationGId] : stationPool?[packet.StationId]; - - if (stationComponent == null) - { - Log.Error($"UpdateStorageUI: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}"); - return; - } - - using (Multiplayer.Session.Ships.PatchLockILS.On()) - { - planet.factory.transport.SetStationStorage(stationComponent.id, packet.StorageIdx, packet.ItemId, packet.ItemCountMax, packet.LocalLogic, packet.RemoteLogic, (packet.ShouldMimic == true) ? GameMain.mainPlayer : null); - } - } } }