From f203bc2bbc5e1bdb6c5215858ecc43150402cd7d Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:46:48 +0100 Subject: [PATCH] Update GenericPacket --- Fika.Core/Coop/Components/CoopExfilManager.cs | 11 +- Fika.Core/Coop/GameMode/CoopGame.cs | 3 +- Fika.Core/Networking/FikaClient.cs | 79 +-------- .../Networking/FikaSerializationExtensions.cs | 19 +++ Fika.Core/Networking/FikaServer.cs | 65 +------- .../FirearmController/FirearmSubPackets.cs | 1 - .../Packets/GameWorld/GenericPacket.cs | 55 +------ .../Packets/GameWorld/GenericSubPackets.cs | 155 ++++++++++++++++++ Fika.Core/Networking/Packets/SubPacket.cs | 9 + 9 files changed, 208 insertions(+), 189 deletions(-) create mode 100644 Fika.Core/Networking/Packets/GameWorld/GenericSubPackets.cs diff --git a/Fika.Core/Coop/Components/CoopExfilManager.cs b/Fika.Core/Coop/Components/CoopExfilManager.cs index 1593a781..83378414 100644 --- a/Fika.Core/Coop/Components/CoopExfilManager.cs +++ b/Fika.Core/Coop/Components/CoopExfilManager.cs @@ -9,6 +9,8 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; +using static Fika.Core.Networking.Packets.GameWorld.GenericSubPackets; +using static Fika.Core.Networking.Packets.SubPacket; namespace Fika.Core.Coop.Components { @@ -196,9 +198,12 @@ private void ExfiltrationPoint_OnStatusChanged(ExfiltrationPoint point, EExfiltr GenericPacket packet = new() { NetId = mainPlayer.NetId, - Type = EPackageType.ExfilCountdown, - ExfilName = point.Settings.Name, - ExfilStartTime = point.ExfiltrationStartTime + Type = EGenericSubPacketType.ExfilCountdown, + SubPacket = new ExfilCountdown() + { + ExfilName = point.Settings.Name, + ExfilStartTime = point.ExfiltrationStartTime + } }; if (FikaBackendUtils.IsServer) diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 7e418ac1..68f0cdc9 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -44,6 +44,7 @@ using System.Linq; using System.Threading.Tasks; using UnityEngine; +using static Fika.Core.Networking.Packets.SubPacket; namespace Fika.Core.Coop.GameMode { @@ -1962,7 +1963,7 @@ public void Extract(CoopPlayer player, ExfiltrationPoint exfiltrationPoint, Tran GenericPacket genericPacket = new() { NetId = player.NetId, - Type = EPackageType.ClientExtract + Type = EGenericSubPacketType.ClientExtract }; try // This is to allow clients to extract if they lose connection diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 1dfc594f..9e9842db 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -1016,89 +1016,14 @@ private void OnExfiltrationPacketReceived(ExfiltrationPacket packet) private void OnGenericPacketReceived(GenericPacket packet) { - switch (packet.Type) - { - case EPackageType.ClientExtract: - { - if (coopHandler.Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) - { - coopHandler.Players.Remove(packet.NetId); - coopHandler.HumanPlayers.Remove(playerToApply); - if (!coopHandler.ExtractedPlayers.Contains(packet.NetId)) - { - coopHandler.ExtractedPlayers.Add(packet.NetId); - CoopGame coopGame = coopHandler.LocalGameInstance; - coopGame.ExtractedPlayers.Add(packet.NetId); - coopGame.ClearHostAI(playerToApply); - - if (FikaPlugin.ShowNotifications.Value) - { - string nickname = !string.IsNullOrEmpty(playerToApply.Profile.Info.MainProfileNickname) ? playerToApply.Profile.Info.MainProfileNickname : playerToApply.Profile.Nickname; - NotificationManagerClass.DisplayMessageNotification(string.Format(LocaleUtils.GROUP_MEMBER_EXTRACTED.Localized(), - ColorizeText(EColor.GREEN, nickname)), - ENotificationDurationType.Default, ENotificationIconType.EntryPoint); - } - } - - playerToApply.Dispose(); - AssetPoolObject.ReturnToPool(playerToApply.gameObject, true); - } - } - break; - /*case EPackageType.TrainSync: - { - MovingPlatform.GClass3230 adapter = Singleton.Instance.PlatformAdapters[0]; - if (adapter != null) - { - GStruct132 data = new() - { - Id = packet.PlatformId, - Position = packet.PlatformPosition - }; - adapter.StoreNetPacket(data); - adapter.ApplyStoredPackets(); - } - } - break;*/ - case EPackageType.ExfilCountdown: - { - if (ExfiltrationControllerClass.Instance != null) - { - ExfiltrationControllerClass exfilController = ExfiltrationControllerClass.Instance; - - ExfiltrationPoint exfilPoint = exfilController.ExfiltrationPoints.FirstOrDefault(x => x.Settings.Name == packet.ExfilName); - if (exfilPoint != null) - { - CoopGame game = coopHandler.LocalGameInstance; - exfilPoint.ExfiltrationStartTime = game != null ? game.PastTime : packet.ExfilStartTime; - - if (exfilPoint.Status != EExfiltrationStatus.Countdown) - { - exfilPoint.Status = EExfiltrationStatus.Countdown; - } - } - } - } - break; - case EPackageType.ClearEffects: - { - if (coopHandler.Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) - { - if (playerToApply is ObservedCoopPlayer observedPlayer) - { - observedPlayer.HealthBar.ClearEffects(); - } - } - } - break; - } + packet.SubPacket.Execute(null); } private void OnHealthSyncPacketReceived(HealthSyncPacket packet) { if (coopHandler.Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) { - playerToApply.PacketReceiver.HealthSyncPackets?.Enqueue(packet); + playerToApply.PacketReceiver.HealthSyncPackets.Enqueue(packet); } } diff --git a/Fika.Core/Networking/FikaSerializationExtensions.cs b/Fika.Core/Networking/FikaSerializationExtensions.cs index d4e66561..58d0fdd2 100644 --- a/Fika.Core/Networking/FikaSerializationExtensions.cs +++ b/Fika.Core/Networking/FikaSerializationExtensions.cs @@ -5,6 +5,7 @@ using EFT.InventoryLogic; using EFT.SynchronizableObjects; using Fika.Core.Coop.Utils; +using Fika.Core.Networking.Packets.GameWorld; using LiteNetLib.Utils; using System; using System.Collections.Generic; @@ -14,6 +15,7 @@ using static BasePhysicalClass; using static Fika.Core.Networking.CommonSubPackets; using static Fika.Core.Networking.FirearmSubPackets; +using static Fika.Core.Networking.Packets.GameWorld.GenericSubPackets; using static Fika.Core.Networking.Packets.SubPacket; using static Fika.Core.Networking.Packets.SubPackets; @@ -985,5 +987,22 @@ public static ISubPacket GetCommonSubPacket(this NetDataReader reader, ECommonSu } return null; } + + public static ISubPacket GetGenericSubPacket(this NetDataReader reader, EGenericSubPacketType type, int netId) + { + switch (type) + { + case EGenericSubPacketType.ClientExtract: + return new ClientExtract(netId); + case EGenericSubPacketType.ExfilCountdown: + return new ExfilCountdown(reader); + case EGenericSubPacketType.ClearEffects: + return new ClearEffects(netId); + default: + FikaPlugin.Instance.FikaLogger.LogError("GetGenericSubPacket: type was outside of bounds!"); + break; + } + return null; + } } } \ No newline at end of file diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index ec8b8573..2953ce0e 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -37,6 +37,8 @@ using System.Threading; using System.Threading.Tasks; using UnityEngine; +using static Fika.Core.Networking.Packets.GameWorld.GenericSubPackets; +using static Fika.Core.Networking.Packets.SubPacket; using static Fika.Core.Networking.ReconnectPacket; using static Fika.Core.UI.FikaUIGlobals; @@ -512,7 +514,7 @@ private void OnReconnectPacketReceived(ReconnectPacket packet, NetPeer peer) GenericPacket clearEffectsPacket = new() { NetId = observedCoopPlayer.NetId, - Type = EPackageType.ClearEffects + Type = EGenericSubPacketType.ClearEffects }; SendDataToAll(ref clearEffectsPacket, DeliveryMethod.ReliableUnordered, peer); @@ -939,72 +941,17 @@ private void OnExfiltrationPacketReceived(ExfiltrationPacket packet, NetPeer pee private void OnGenericPacketReceived(GenericPacket packet, NetPeer peer) { - switch (packet.Type) - { - case EPackageType.ClientExtract: - { - if (coopHandler.Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) - { - coopHandler.Players.Remove(packet.NetId); - coopHandler.HumanPlayers.Remove(playerToApply); - if (!coopHandler.ExtractedPlayers.Contains(packet.NetId)) - { - coopHandler.ExtractedPlayers.Add(packet.NetId); - CoopGame coopGame = coopHandler.LocalGameInstance; - coopGame.ExtractedPlayers.Add(packet.NetId); - coopGame.ClearHostAI(playerToApply); - - if (FikaPlugin.ShowNotifications.Value) - { - string nickname = !string.IsNullOrEmpty(playerToApply.Profile.Info.MainProfileNickname) ? playerToApply.Profile.Info.MainProfileNickname : playerToApply.Profile.Nickname; - NotificationManagerClass.DisplayMessageNotification(string.Format(LocaleUtils.GROUP_MEMBER_EXTRACTED.Localized(), - ColorizeText(EColor.GREEN, nickname)), - EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.EntryPoint); - } - } - - playerToApply.Dispose(); - AssetPoolObject.ReturnToPool(playerToApply.gameObject, true); - } - } - break; - case EPackageType.ExfilCountdown: - { - if (ExfiltrationControllerClass.Instance != null) - { - ExfiltrationControllerClass exfilController = ExfiltrationControllerClass.Instance; - - ExfiltrationPoint exfilPoint = exfilController.ExfiltrationPoints.FirstOrDefault(x => x.Settings.Name == packet.ExfilName); - if (exfilPoint != null) - { - CoopGame game = coopHandler.LocalGameInstance; - exfilPoint.ExfiltrationStartTime = game != null ? game.PastTime : packet.ExfilStartTime; - - if (exfilPoint.Status != EExfiltrationStatus.Countdown) - { - exfilPoint.Status = EExfiltrationStatus.Countdown; - } - } - } - } - break; - case EPackageType.TraderServiceNotification: - case EPackageType.ClearEffects: - default: - break; - } - SendDataToAll(ref packet, DeliveryMethod.ReliableOrdered, peer); + packet.SubPacket.Execute(null); } private void OnHealthSyncPacketReceived(HealthSyncPacket packet, NetPeer peer) { + SendDataToAll(ref packet, DeliveryMethod.ReliableOrdered, peer); if (coopHandler.Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) { - playerToApply.PacketReceiver.HealthSyncPackets?.Enqueue(packet); + playerToApply.PacketReceiver.HealthSyncPackets.Enqueue(packet); } - - SendDataToAll(ref packet, DeliveryMethod.ReliableOrdered, peer); } private void OnInformationPacketReceived(InformationPacket packet, NetPeer peer) diff --git a/Fika.Core/Networking/Packets/FirearmController/FirearmSubPackets.cs b/Fika.Core/Networking/Packets/FirearmController/FirearmSubPackets.cs index 301b1341..6d8a1ecc 100644 --- a/Fika.Core/Networking/Packets/FirearmController/FirearmSubPackets.cs +++ b/Fika.Core/Networking/Packets/FirearmController/FirearmSubPackets.cs @@ -3,7 +3,6 @@ using EFT.InventoryLogic; using Fika.Core.Coop.ObservedClasses; using Fika.Core.Coop.Players; -using Fika.Core.Coop.Utils; using LiteNetLib.Utils; using System; using System.Collections.Generic; diff --git a/Fika.Core/Networking/Packets/GameWorld/GenericPacket.cs b/Fika.Core/Networking/Packets/GameWorld/GenericPacket.cs index 8b522517..037cbad5 100644 --- a/Fika.Core/Networking/Packets/GameWorld/GenericPacket.cs +++ b/Fika.Core/Networking/Packets/GameWorld/GenericPacket.cs @@ -2,6 +2,7 @@ using EFT; using LiteNetLib.Utils; +using static Fika.Core.Networking.Packets.SubPacket; namespace Fika.Core.Networking { @@ -12,63 +13,21 @@ namespace Fika.Core.Networking public class GenericPacket : INetSerializable { public int NetId; - public EPackageType Type; - - /*public byte PlatformId; - public float PlatformPosition;*/ - - public string ExfilName; - public float ExfilStartTime; - - public ETraderServiceType TraderServiceType; + public EGenericSubPacketType Type; + public ISubPacket SubPacket; public void Deserialize(NetDataReader reader) { NetId = reader.GetInt(); - Type = (EPackageType)reader.GetInt(); - switch (Type) - { - /*case EPackageType.TrainSync: - PlatformId = reader.GetByte(); - PlatformPosition = reader.GetFloat(); - break;*/ - case EPackageType.ExfilCountdown: - ExfilName = reader.GetString(); - ExfilStartTime = reader.GetFloat(); - break; - case EPackageType.TraderServiceNotification: - TraderServiceType = (ETraderServiceType)reader.GetInt(); - break; - } + Type = (EGenericSubPacketType)reader.GetInt(); + SubPacket = reader.GetGenericSubPacket(Type, NetId); } public void Serialize(NetDataWriter writer) { writer.Put(NetId); writer.Put((int)Type); - switch (Type) - { - /*case EPackageType.TrainSync: - writer.Put(PlatformId); - writer.Put(PlatformPosition); - break;*/ - case EPackageType.ExfilCountdown: - writer.Put(ExfilName); - writer.Put(ExfilStartTime); - break; - case EPackageType.TraderServiceNotification: - writer.Put((int)TraderServiceType); - break; - } + SubPacket.Serialize(writer); } - } - - public enum EPackageType - { - ClientExtract, - //TrainSync, - ExfilCountdown, - TraderServiceNotification, - ClearEffects - } + } } diff --git a/Fika.Core/Networking/Packets/GameWorld/GenericSubPackets.cs b/Fika.Core/Networking/Packets/GameWorld/GenericSubPackets.cs new file mode 100644 index 00000000..80d84b15 --- /dev/null +++ b/Fika.Core/Networking/Packets/GameWorld/GenericSubPackets.cs @@ -0,0 +1,155 @@ +using Comfort.Common; +using EFT.AssetsManager; +using EFT.Interactive; +using Fika.Core.Coop.Components; +using Fika.Core.Coop.GameMode; +using Fika.Core.Coop.Players; +using Fika.Core.Coop.Utils; +using Fika.Core.Utils; +using LiteNetLib.Utils; +using System.Linq; +using static Fika.Core.Networking.Packets.SubPacket; +using static Fika.Core.UI.FikaUIGlobals; + +namespace Fika.Core.Networking.Packets.GameWorld +{ + public class GenericSubPackets + { + public struct ClientExtract : ISubPacket + { + public int NetId; + + public ClientExtract(int netId) + { + NetId = netId; + } + + public void Execute(CoopPlayer player) + { + CoopHandler coopHandler = Singleton.Instance.CoopHandler; + if (coopHandler == null) + { + FikaPlugin.Instance.FikaLogger.LogError("ClientExtract: CoopHandler was null!"); + return; + } + + if (coopHandler.Players.TryGetValue(NetId, out CoopPlayer playerToApply)) + { + coopHandler.Players.Remove(NetId); + coopHandler.HumanPlayers.Remove(playerToApply); + if (!coopHandler.ExtractedPlayers.Contains(NetId)) + { + coopHandler.ExtractedPlayers.Add(NetId); + CoopGame coopGame = coopHandler.LocalGameInstance; + coopGame.ExtractedPlayers.Add(NetId); + coopGame.ClearHostAI(playerToApply); + + if (FikaPlugin.ShowNotifications.Value) + { + string nickname = !string.IsNullOrEmpty(playerToApply.Profile.Info.MainProfileNickname) ? playerToApply.Profile.Info.MainProfileNickname : playerToApply.Profile.Nickname; + NotificationManagerClass.DisplayMessageNotification(string.Format(LocaleUtils.GROUP_MEMBER_EXTRACTED.Localized(), + ColorizeText(EColor.GREEN, nickname)), + EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.EntryPoint); + } + } + + playerToApply.Dispose(); + AssetPoolObject.ReturnToPool(playerToApply.gameObject, true); + } + } + + public void Serialize(NetDataWriter writer) + { + + } + } + + public struct ExfilCountdown : ISubPacket + { + public string ExfilName; + public float ExfilStartTime; + + public ExfilCountdown(NetDataReader reader) + { + ExfilName = reader.GetString(); + ExfilStartTime = reader.GetFloat(); + } + + public void Execute(CoopPlayer player) + { + CoopHandler coopHandler = Singleton.Instance.CoopHandler; + if (coopHandler == null) + { + FikaPlugin.Instance.FikaLogger.LogError("ClientExtract: CoopHandler was null!"); + return; + } + + if (ExfiltrationControllerClass.Instance != null) + { + ExfiltrationControllerClass exfilController = ExfiltrationControllerClass.Instance; + + foreach (ExfiltrationPoint exfiltrationPoint in exfilController.ExfiltrationPoints) + { + if (exfiltrationPoint.Settings.Name == ExfilName) + { + CoopGame game = coopHandler.LocalGameInstance; + exfiltrationPoint.ExfiltrationStartTime = game != null ? game.PastTime : ExfilStartTime; + + if (exfiltrationPoint.Status != EExfiltrationStatus.Countdown) + { + exfiltrationPoint.Status = EExfiltrationStatus.Countdown; + } + return; + } + } + + FikaPlugin.Instance.FikaLogger.LogError("ExfilCountdown: Could not find ExfiltrationPoint: " + ExfilName); + } + } + + public void Serialize(NetDataWriter writer) + { + writer.Put(ExfilName); + writer.Put(ExfilStartTime); + } + } + + public struct ClearEffects : ISubPacket + { + public int NetId; + + public ClearEffects(int netId) + { + NetId = netId; + } + + public void Execute(CoopPlayer player) + { + if (FikaBackendUtils.IsServer) + { + return; + } + + CoopHandler coopHandler = Singleton.Instance.CoopHandler; + if (coopHandler == null) + { + FikaPlugin.Instance.FikaLogger.LogError("ClientExtract: CoopHandler was null!"); + return; + } + + if (coopHandler.Players.TryGetValue(NetId, out CoopPlayer playerToApply)) + { + if (playerToApply is ObservedCoopPlayer observedPlayer) + { + observedPlayer.HealthBar.ClearEffects(); + } + } + } + + public void Serialize(NetDataWriter writer) + { + + } + } + } +} diff --git a/Fika.Core/Networking/Packets/SubPacket.cs b/Fika.Core/Networking/Packets/SubPacket.cs index 29ae914b..9f7ad4f9 100644 --- a/Fika.Core/Networking/Packets/SubPacket.cs +++ b/Fika.Core/Networking/Packets/SubPacket.cs @@ -99,5 +99,14 @@ public enum ECommonSubPacketType Interaction, Mounting, } + + public enum EGenericSubPacketType + { + ClientExtract, + //TrainSync, + ExfilCountdown, + //TraderServiceNotification, + ClearEffects + } } }