Skip to content

Commit

Permalink
Update GenericPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Nov 22, 2024
1 parent 1f15069 commit f203bc2
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 189 deletions.
11 changes: 8 additions & 3 deletions Fika.Core/Coop/Components/CoopExfilManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
79 changes: 2 additions & 77 deletions Fika.Core/Networking/FikaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameWorld>.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);
}
}

Expand Down
19 changes: 19 additions & 0 deletions Fika.Core/Networking/FikaSerializationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
65 changes: 6 additions & 59 deletions Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 7 additions & 48 deletions Fika.Core/Networking/Packets/GameWorld/GenericPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using EFT;
using LiteNetLib.Utils;
using static Fika.Core.Networking.Packets.SubPacket;

namespace Fika.Core.Networking
{
Expand All @@ -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
}
}
}
Loading

0 comments on commit f203bc2

Please sign in to comment.