From 1a5c148024312e7641d5f90c54f9c44803bbf602 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Mon, 15 Jul 2024 20:15:39 +0200 Subject: [PATCH] Fixes and filtering --- Fika.Core/Coop/Components/CoopHandler.cs | 19 +- Fika.Core/Coop/Custom/FikaHealthBar.cs | 2 +- .../PacketHandlers/DedicatedPacketSender.cs | 211 ++++++++++++++++++ ...arkovApplication_LocalGameCreator_Patch.cs | 6 + Fika.Core/Coop/Players/CoopPlayer.cs | 10 +- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 4 +- Fika.Core/Coop/Utils/FikaBackendUtils.cs | 3 +- Fika.Core/Networking/FikaPingingClient.cs | 2 +- Fika.Core/Networking/FikaServer.cs | 4 +- 9 files changed, 242 insertions(+), 19 deletions(-) create mode 100644 Fika.Core/Coop/PacketHandlers/DedicatedPacketSender.cs diff --git a/Fika.Core/Coop/Components/CoopHandler.cs b/Fika.Core/Coop/Components/CoopHandler.cs index 71fcbfdf..91359d7b 100644 --- a/Fika.Core/Coop/Components/CoopHandler.cs +++ b/Fika.Core/Coop/Components/CoopHandler.cs @@ -433,6 +433,8 @@ public WorldInteractiveObject GetInteractiveObject(string objectId, out WorldInt private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId) { + bool isDedicatedProfile = profile.Nickname.Contains("dedicated_"); + ObservedCoopPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity, "Player", isAI == true ? "Bot_" : $"Player_{profile.Nickname}_", EPointOfView.ThirdPerson, profile, isAI, EUpdateQueue.Update, Player.EUpdateMode.Manual, @@ -453,13 +455,16 @@ private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position HumanPlayers++; } - if (!Players.ContainsKey(netId)) - { - Players.Add(netId, otherPlayer); - } - else + if (!isDedicatedProfile) { - Logger.LogError($"Trying to add {otherPlayer.Profile.Nickname} to list of players but it was already there!"); + if (!Players.ContainsKey(netId)) + { + Players.Add(netId, otherPlayer); + } + else + { + Logger.LogError($"Trying to add {otherPlayer.Profile.Nickname} to list of players but it was already there!"); + } } if (!Singleton.Instance.RegisteredPlayers.Any(x => x.Profile.ProfileId == profile.ProfileId)) @@ -512,7 +517,7 @@ private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position } } - otherPlayer.InitObservedPlayer(); + otherPlayer.InitObservedPlayer(isDedicatedProfile); Logger.LogDebug($"CreateLocalPlayer::{profile.Info.Nickname}::Spawned."); diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index f2fb34be..bc81a71b 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -174,7 +174,7 @@ private void CreateHealthBar() if (currentPlayer.ProfileId == FikaBackendUtils.GetGroupId()) { - if (FikaBackendUtils.IsDedicated) + if (FikaBackendUtils.IsDedicatedGame) { // Do not show dedicated client name plate Destroy(this); diff --git a/Fika.Core/Coop/PacketHandlers/DedicatedPacketSender.cs b/Fika.Core/Coop/PacketHandlers/DedicatedPacketSender.cs new file mode 100644 index 00000000..9344571d --- /dev/null +++ b/Fika.Core/Coop/PacketHandlers/DedicatedPacketSender.cs @@ -0,0 +1,211 @@ +// © 2024 Lacyway All Rights Reserved + +using BepInEx.Logging; +using Comfort.Common; +using EFT; +using EFT.MovingPlatforms; +using Fika.Core.Coop.GameMode; +using Fika.Core.Coop.Players; +using Fika.Core.Networking; +using Fika.Core.Networking.Packets; +using HarmonyLib; +using LiteNetLib; +using LiteNetLib.Utils; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace Fika.Core.Coop.PacketHandlers +{ + public class DedicatedPacketSender : MonoBehaviour, IPacketSender + { + private CoopPlayer player; + + public bool Enabled { get; set; } = true; + public FikaServer Server { get; set; } = Singleton.Instance; + public FikaClient Client { get; set; } + public NetDataWriter Writer { get; set; } = new(); + public Queue FirearmPackets { get; set; } = new(50); + public Queue DamagePackets { get; set; } = new(50); + public Queue ArmorDamagePackets { get; set; } = new(50); + public Queue InventoryPackets { get; set; } = new(50); + public Queue CommonPlayerPackets { get; set; } = new(50); + public Queue HealthSyncPackets { get; set; } = new(50); + + private ManualLogSource logger; + + protected void Awake() + { + logger = BepInEx.Logging.Logger.CreateLogSource("ServerPacketSender"); + player = GetComponent(); + enabled = false; + } + + public void Init() + { + enabled = true; + StartCoroutine(SendTrainTime()); + } + + public void SendQuestPacket(ref QuestConditionPacket packet) + { + Writer.Reset(); + Server.SendDataToAll(Writer, ref packet, DeliveryMethod.ReliableUnordered); + } + + public void SendQuestItemPacket(ref QuestItemPacket packet) + { + Writer.Reset(); + Server.SendDataToAll(Writer, ref packet, DeliveryMethod.ReliableUnordered); + } + + protected void Update() + { + int firearmPackets = FirearmPackets.Count; + if (firearmPackets > 0) + { + for (int i = 0; i < firearmPackets; i++) + { + WeaponPacket firearmPacket = FirearmPackets.Dequeue(); + firearmPacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); + } + } + int damagePackets = DamagePackets.Count; + if (damagePackets > 0) + { + for (int i = 0; i < damagePackets; i++) + { + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + } + int armorDamagePackets = ArmorDamagePackets.Count; + if (armorDamagePackets > 0) + { + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); + } + } + int inventoryPackets = InventoryPackets.Count; + if (inventoryPackets > 0) + { + for (int i = 0; i < inventoryPackets; i++) + { + InventoryPacket inventoryPacket = InventoryPackets.Dequeue(); + inventoryPacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref inventoryPacket, DeliveryMethod.ReliableOrdered); + } + } + int commonPlayerPackets = CommonPlayerPackets.Count; + if (commonPlayerPackets > 0) + { + for (int i = 0; i < commonPlayerPackets; i++) + { + CommonPlayerPacket commonPlayerPacket = CommonPlayerPackets.Dequeue(); + commonPlayerPacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref commonPlayerPacket, DeliveryMethod.ReliableOrdered); + } + } + int healthSyncPackets = HealthSyncPackets.Count; + if (healthSyncPackets > 0) + { + for (int i = 0; i < healthSyncPackets; i++) + { + HealthSyncPacket healthSyncPacket = HealthSyncPackets.Dequeue(); + healthSyncPacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref healthSyncPacket, DeliveryMethod.ReliableOrdered); + } + } + } + + private IEnumerator SendTrainTime() + { + while (!Singleton.Instantiated) + { + yield return null; + } + + while (string.IsNullOrEmpty(Singleton.Instance.MainPlayer.Location)) + { + yield return null; + } + + string location = Singleton.Instance.MainPlayer.Location; + + if (location.Contains("RezervBase") || location.Contains("Lighthouse")) + { + CoopGame coopGame = (CoopGame)Singleton.Instance; + + while (coopGame.Status != GameStatus.Started) + { + yield return null; + } + + // Trains take around 20 minutes to come in by default so we can safely wait 20 seconds to make sure everyone is loaded in + yield return new WaitForSeconds(20); + + Locomotive locomotive = FindObjectOfType(); + if (locomotive != null) + { + long time = Traverse.Create(locomotive).Field("_depart").Value.Ticks; + + GenericPacket packet = new() + { + NetId = player.NetId, + PacketType = EPackageType.TrainSync, + DepartureTime = time + }; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref packet, DeliveryMethod.ReliableOrdered); + } + else + { + logger.LogError("SendTrainTime: Could not find locomotive!"); + } + } + else + { + yield break; + } + } + + public void DestroyThis() + { + Writer = null; + FirearmPackets.Clear(); + DamagePackets.Clear(); + InventoryPackets.Clear(); + CommonPlayerPackets.Clear(); + HealthSyncPackets.Clear(); + if (Server != null) + { + Server = null; + } + if (Client != null) + { + Client = null; + } + Destroy(this); + } + } +} diff --git a/Fika.Core/Coop/Patches/LocalGame/TarkovApplication_LocalGameCreator_Patch.cs b/Fika.Core/Coop/Patches/LocalGame/TarkovApplication_LocalGameCreator_Patch.cs index 54b844a1..4faeedb8 100644 --- a/Fika.Core/Coop/Patches/LocalGame/TarkovApplication_LocalGameCreator_Patch.cs +++ b/Fika.Core/Coop/Patches/LocalGame/TarkovApplication_LocalGameCreator_Patch.cs @@ -85,6 +85,12 @@ public static async Task Postfix(Task __result, TarkovApplication __instance, Ti Profile profile = session.GetProfileBySide(____raidSettings.Side); + bool isDedicatedHost = session.Profile.Nickname.StartsWith("dedicated_"); + if (isDedicatedHost) + { + FikaBackendUtils.IsDedicated = true; + } + profile.Inventory.Stash = null; profile.Inventory.QuestStashItems = null; profile.Inventory.DiscardLimits = Singleton.Instance.GetDiscardLimits(); diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index d6d4fe23..16c25c53 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -92,6 +92,11 @@ public static async Task Create(int playerId, Vector3 position, Qua { player.PacketSender = player.gameObject.AddComponent(); } + else if (FikaBackendUtils.IsDedicated) + { + FikaPlugin.Instance.FikaLogger.LogError("I AM DEDICATED"); + player.PacketSender = player.gameObject.AddComponent(); + } player.PacketReceiver = player.gameObject.AddComponent(); @@ -119,11 +124,6 @@ await player.Init(rotation, layerName, pointOfView, profile, inventoryController return player; } - /*public override BasePhysicalClass CreatePhysical() - { - return FikaPlugin.Instance.UseInertia ? new PlayerPhysicalClass() : new NoInertiaPhysical(); - }*/ - public override void CreateMovementContext() { LayerMask movement_MASK = EFTHardSettings.Instance.MOVEMENT_MASK; diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 55b8b152..ace35952 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -800,7 +800,7 @@ private IEnumerator DestroyNetworkedComponents() } } - public void InitObservedPlayer() + public void InitObservedPlayer(bool isDedicatedHost) { if (gameObject.name.StartsWith("Bot_")) { @@ -860,7 +860,7 @@ public void InitObservedPlayer() InitVaultingAudioControllers(ObservedVaultingParameters); - if (FikaPlugin.ShowNotifications.Value) + if (FikaPlugin.ShowNotifications.Value && !isDedicatedHost) { NotificationManagerClass.DisplayMessageNotification($"Group member '{(Side == EPlayerSide.Savage ? Profile.Info.MainProfileNickname : Profile.Nickname)}' has spawned", EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.Friend); diff --git a/Fika.Core/Coop/Utils/FikaBackendUtils.cs b/Fika.Core/Coop/Utils/FikaBackendUtils.cs index 42d181dc..9333d034 100644 --- a/Fika.Core/Coop/Utils/FikaBackendUtils.cs +++ b/Fika.Core/Coop/Utils/FikaBackendUtils.cs @@ -24,6 +24,7 @@ public static class FikaBackendUtils public static EMatchmakerType MatchingType = EMatchmakerType.Single; public static bool IsServer => MatchingType == EMatchmakerType.GroupLeader; public static bool IsClient => MatchingType == EMatchmakerType.GroupPlayer; + public static bool IsDedicated = false; public static bool IsSinglePlayer { get @@ -32,7 +33,7 @@ public static bool IsSinglePlayer && Singleton.Instance.NetServer.ConnectedPeersCount == 0; } } - public static bool IsDedicated = false; + public static bool IsDedicatedGame = false; public static PlayersRaidReadyPanel PlayersRaidReadyPanel; public static MatchMakerGroupPreview MatchMakerGroupPreview; public static int HostExpectedNumberOfPlayers = 1; diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs index 8b8fbea7..72c21e10 100644 --- a/Fika.Core/Networking/FikaPingingClient.cs +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -33,7 +33,7 @@ public bool Init(string serverId) GetHostResponse result = FikaRequestHandler.GetHost(body); FikaBackendUtils.IsHostNatPunch = result.NatPunch; - FikaBackendUtils.IsDedicated = result.IsDedicated; + FikaBackendUtils.IsDedicatedGame = result.IsDedicated; NetClient.Start(); diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index 4f520be4..18d71085 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -206,7 +206,7 @@ public async Task Init() iconType: EFT.Communications.ENotificationIconType.Alert); } - SetHostRequest body = new(Ips, Port, FikaPlugin.UseNatPunching.Value, FikaBackendUtils.IsDedicated); + SetHostRequest body = new(Ips, Port, FikaPlugin.UseNatPunching.Value, FikaBackendUtils.IsDedicatedGame); FikaRequestHandler.UpdateSetHost(body); FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this)); @@ -870,7 +870,7 @@ public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) timeSinceLastPeerDisconnected = DateTime.Now; } - if (FikaBackendUtils.IsDedicated) + if (FikaBackendUtils.IsDedicatedGame) { if (_netServer.ConnectedPeersCount == 0) {