Skip to content

Commit

Permalink
Fixes and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Jul 15, 2024
1 parent 8bfd26c commit 1a5c148
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 19 deletions.
19 changes: 12 additions & 7 deletions Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<GameWorld>.Instance.RegisteredPlayers.Any(x => x.Profile.ProfileId == profile.ProfileId))
Expand Down Expand Up @@ -512,7 +517,7 @@ private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position
}
}

otherPlayer.InitObservedPlayer();
otherPlayer.InitObservedPlayer(isDedicatedProfile);

Logger.LogDebug($"CreateLocalPlayer::{profile.Info.Nickname}::Spawned.");

Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/Custom/FikaHealthBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
211 changes: 211 additions & 0 deletions Fika.Core/Coop/PacketHandlers/DedicatedPacketSender.cs
Original file line number Diff line number Diff line change
@@ -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<FikaServer>.Instance;
public FikaClient Client { get; set; }
public NetDataWriter Writer { get; set; } = new();
public Queue<WeaponPacket> FirearmPackets { get; set; } = new(50);
public Queue<DamagePacket> DamagePackets { get; set; } = new(50);
public Queue<ArmorDamagePacket> ArmorDamagePackets { get; set; } = new(50);
public Queue<InventoryPacket> InventoryPackets { get; set; } = new(50);
public Queue<CommonPlayerPacket> CommonPlayerPackets { get; set; } = new(50);
public Queue<HealthSyncPacket> HealthSyncPackets { get; set; } = new(50);

private ManualLogSource logger;

protected void Awake()
{
logger = BepInEx.Logging.Logger.CreateLogSource("ServerPacketSender");
player = GetComponent<CoopPlayer>();
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<GameWorld>.Instantiated)
{
yield return null;
}

while (string.IsNullOrEmpty(Singleton<GameWorld>.Instance.MainPlayer.Location))
{
yield return null;
}

string location = Singleton<GameWorld>.Instance.MainPlayer.Location;

if (location.Contains("RezervBase") || location.Contains("Lighthouse"))
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.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<Locomotive>();
if (locomotive != null)
{
long time = Traverse.Create(locomotive).Field<DateTime>("_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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemFactory>.Instance.GetDiscardLimits();
Expand Down
10 changes: 5 additions & 5 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public static async Task<LocalPlayer> Create(int playerId, Vector3 position, Qua
{
player.PacketSender = player.gameObject.AddComponent<ClientPacketSender>();
}
else if (FikaBackendUtils.IsDedicated)
{
FikaPlugin.Instance.FikaLogger.LogError("I AM DEDICATED");
player.PacketSender = player.gameObject.AddComponent<DedicatedPacketSender>();
}

player.PacketReceiver = player.gameObject.AddComponent<PacketReceiver>();

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ private IEnumerator DestroyNetworkedComponents()
}
}

public void InitObservedPlayer()
public void InitObservedPlayer(bool isDedicatedHost)
{
if (gameObject.name.StartsWith("Bot_"))
{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Fika.Core/Coop/Utils/FikaBackendUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +33,7 @@ public static bool IsSinglePlayer
&& Singleton<FikaServer>.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;
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Networking/FikaPingingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -870,7 +870,7 @@ public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
timeSinceLastPeerDisconnected = DateTime.Now;
}

if (FikaBackendUtils.IsDedicated)
if (FikaBackendUtils.IsDedicatedGame)
{
if (_netServer.ConnectedPeersCount == 0)
{
Expand Down

0 comments on commit 1a5c148

Please sign in to comment.