Skip to content

Commit

Permalink
Rework item position sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Oct 19, 2024
1 parent c2881f5 commit 7092556
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 109 deletions.
14 changes: 0 additions & 14 deletions Fika.Core/Coop/ClientClasses/CoopClientGameWorld.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Comfort.Common;
using EFT;
using EFT.Interactive;
using EFT.InventoryLogic;
using EFT.SynchronizableObjects;
using Fika.Core.Coop.Utils;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;

namespace Fika.Core.Coop.ClientClasses
Expand All @@ -30,18 +28,6 @@ public static CoopClientGameWorld Create(GameObject gameObject, PoolManager obje
return gameWorld;
}

public override LootItem CreateLootWithRigidbody(GameObject lootObject, Item item, string objectName, bool randomRotation, [CanBeNull] string[] validProfiles, out BoxCollider objectCollider, bool syncable, bool performPickUpValidation = true, float makeVisibleAfterDelay = 0)
{
if (syncable)
{
ObservedLootItem observedLootItem = ObservedLootItem.CreateLootWithRigidbody(lootObject, item, objectName, this, randomRotation, validProfiles, out objectCollider, performPickUpValidation, makeVisibleAfterDelay);
Traverse.Create(observedLootItem).Field<bool>("bool_3").Value = true;
return observedLootItem;
}

return base.CreateLootWithRigidbody(lootObject, item, objectName, randomRotation, validProfiles, out objectCollider, true, performPickUpValidation, makeVisibleAfterDelay);
}

public override GClass722 CreateGrenadeFactory()
{
return new GClass723();
Expand Down
101 changes: 101 additions & 0 deletions Fika.Core/Coop/Components/ItemPositionSyncer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Comfort.Common;
using EFT.Interactive;
using Fika.Core.Networking;
using LiteNetLib;
using UnityEngine;

namespace Fika.Core.Coop.Components
{
public class ItemPositionSyncer : MonoBehaviour
{
private FikaServer server;
private FikaClient client;
private bool isServer;
private ObservedLootItem lootItem;
private GStruct128 data;

private Rigidbody Rigidbody
{
get
{
return lootItem.RigidBody;
}
}

public static void Create(GameObject gameObject, bool isServer, ObservedLootItem lootItem)
{
ItemPositionSyncer posSync = gameObject.AddComponent<ItemPositionSyncer>();
posSync.isServer = isServer;
if (isServer)
{
posSync.server = Singleton<FikaServer>.Instance;
}
else
{
posSync.client = Singleton<FikaClient>.Instance;
}
posSync.lootItem = lootItem;
posSync.data = new()
{
Id = lootItem.GetNetId()
};
}

public void Start()
{
if (lootItem == null)
{
FikaPlugin.Instance.FikaLogger.LogError("HostItemPositionSync::Start: LootItem was null!");
Destroy(this);
}

if (Rigidbody == null)
{
FikaPlugin.Instance.FikaLogger.LogError("HostItemPositionSync::Start: Rigidbody was null!");
Destroy(this);
}
}

public void FixedUpdate()
{
if (Rigidbody == null)
{
data.Position = lootItem.transform.position;
data.Rotation = lootItem.transform.rotation;
data.Velocity = Vector3.zero;
data.AngularVelocity = Vector3.zero;
data.Done = true;
LootSyncPacket endPacket = new()
{
Data = data
};
if (isServer)
{
server.SendDataToAll(ref endPacket, DeliveryMethod.ReliableOrdered);
Destroy(this);
return;
}

client.SendData(ref endPacket, DeliveryMethod.ReliableOrdered);
Destroy(this);
return;
}

data.Position = lootItem.transform.position;
data.Rotation = lootItem.transform.rotation;
data.Velocity = Rigidbody.velocity;
data.AngularVelocity = Rigidbody.angularVelocity;
LootSyncPacket packet = new()
{
Data = data
};
if (isServer)
{
server.SendDataToAll(ref packet, DeliveryMethod.Unreliable);
return;
}

client.SendData(ref packet, DeliveryMethod.Unreliable);
}
}
}
16 changes: 1 addition & 15 deletions Fika.Core/Coop/HostClasses/CoopHostGameWorld.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Comfort.Common;
using EFT;
using EFT.Interactive;
using EFT.InventoryLogic;
using EFT.SynchronizableObjects;
using Fika.Core.Coop.HostClasses;
using Fika.Core.Coop.Utils;
using Fika.Core.Networking;
using HarmonyLib;
using JetBrains.Annotations;
using LiteNetLib;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -41,22 +39,10 @@ public static CoopHostGameWorld Create(GameObject gameObject, PoolManager object
gameWorld.CurrentProfileId = currentProfileId;
gameWorld.UnityTickListener = GameWorldUnityTickListener.Create(gameObject, gameWorld);
gameWorld.AudioSourceCulling = gameObject.GetOrAddComponent<AudioSourceCulling>();
gameObject.AddComponent<FikaHostWorld>();
FikaHostWorld.Create(gameWorld);
return gameWorld;
}

public override LootItem CreateLootWithRigidbody(GameObject lootObject, Item item, string objectName, bool randomRotation, [CanBeNull] string[] validProfiles, out BoxCollider objectCollider, bool syncable, bool performPickUpValidation = true, float makeVisibleAfterDelay = 0)
{
if (syncable)
{
ObservedLootItem lootItem = ObservedLootItem.CreateLootWithRigidbody(lootObject, item, objectName, this, randomRotation, validProfiles, out objectCollider, performPickUpValidation, makeVisibleAfterDelay);
HostItemPositionSync.Create(lootObject, Server, lootItem);
return lootItem;
}

return base.CreateLootWithRigidbody(lootObject, item, objectName, randomRotation, validProfiles, out objectCollider, syncable, performPickUpValidation, makeVisibleAfterDelay);
}

public override GClass722 CreateGrenadeFactory()
{
return new HostGrenadeFactory();
Expand Down
35 changes: 32 additions & 3 deletions Fika.Core/Coop/HostClasses/FikaHostWorld.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Comfort.Common;
using EFT;
using EFT.Interactive;
using Fika.Core.Coop.ClientClasses;
using Fika.Core.Networking;
using LiteNetLib;
using System.Collections.Generic;

namespace Fika.Core.Coop.HostClasses
{
Expand All @@ -11,13 +13,24 @@ namespace Fika.Core.Coop.HostClasses
/// </summary>
public class FikaHostWorld : World
{
public List<GStruct128> LootSyncPackets;

private FikaServer server;
private GameWorld gameWorld;

protected void Start()
public static FikaHostWorld Create(CoopHostGameWorld gameWorld)
{
FikaHostWorld hostWorld = gameWorld.gameObject.AddComponent<FikaHostWorld>();
hostWorld.server = Singleton<FikaServer>.Instance;
hostWorld.server.FikaHostWorld = hostWorld;
hostWorld.gameWorld = gameWorld;
hostWorld.LootSyncPackets = new List<GStruct128>(8);
return hostWorld;
}

protected void Update()
{
server = Singleton<FikaServer>.Instance;
gameWorld = GetComponent<GameWorld>();
UpdateLootItems(gameWorld.LootItems);
}

protected void FixedUpdate()
Expand Down Expand Up @@ -60,6 +73,22 @@ protected void FixedUpdate()
gameWorld.ArtilleryProjectilesStates.Clear();
}

public void UpdateLootItems(GClass770<int, LootItem> lootItems)
{
for (int i = LootSyncPackets.Count - 1; i >= 0; i--)
{
GStruct128 gstruct = LootSyncPackets[i];
if (lootItems.TryGetByKey(gstruct.Id, out LootItem lootItem))
{
if (lootItem is ObservedLootItem observedLootItem)
{
observedLootItem.ApplyNetPacket(gstruct);
}
LootSyncPackets.RemoveAt(i);
}
}
}

/// <summary>
/// Sets up all the <see cref="BorderZone"/>s on the map
/// </summary>
Expand Down
77 changes: 0 additions & 77 deletions Fika.Core/Coop/HostClasses/HostItemPositionSync.cs

This file was deleted.

34 changes: 34 additions & 0 deletions Fika.Core/Coop/Patches/GameWorld/GameWorld_ThrowItem_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using EFT;
using EFT.Interactive;
using Fika.Core.Coop.Components;
using Fika.Core.Coop.Utils;
using HarmonyLib;
using SPT.Reflection.Patching;
using System.Linq;
using System.Reflection;

namespace Fika.Core.Coop.Patches
{
public class GameWorld_ThrowItem_Patch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(GameWorld).GetMethods().First(x => x.Name == nameof(GameWorld.ThrowItem) && x.GetParameters().Length == 3);
}

[PatchPostfix]
public static void Postfix(LootItem __result, IPlayer player)
{
if (__result is ObservedLootItem observedLootItem)
{
if (player.IsYourPlayer || player.IsAI)
{
ItemPositionSyncer.Create(observedLootItem.gameObject, FikaBackendUtils.IsServer, observedLootItem);
return;
}

Traverse.Create(observedLootItem).Field<bool>("bool_3").Value = true;
}
}
}
}
1 change: 1 addition & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private static void EnableFikaPatches()
new MatchmakerPlayerControllerClass_GetCoopBlockReason_Patch().Enable();
new CoopSettingsWindow_Show_Patch().Enable();
new MainMenuController_method_48_Patch().Enable();
new GameWorld_ThrowItem_Patch().Enable();

#if DEBUG
TasksExtensions_HandleFinishedTask_Patches.Enable();
Expand Down
13 changes: 13 additions & 0 deletions Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Fika.Core.Coop.Custom;
using Fika.Core.Coop.Factories;
using Fika.Core.Coop.GameMode;
using Fika.Core.Coop.HostClasses;
using Fika.Core.Coop.ObservedClasses;
using Fika.Core.Coop.ObservedClasses.Snapshotting;
using Fika.Core.Coop.Players;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class FikaServer : MonoBehaviour, INetEventListener, INetLogger, INatPunc
public DateTime TimeSinceLastPeerDisconnected = DateTime.Now.AddDays(1);
public bool HasHadPeer = false;
public bool RaidInitialized = false;
public FikaHostWorld FikaHostWorld { get; set; }
public bool Started
{
get
Expand Down Expand Up @@ -171,6 +173,7 @@ public async Task Init()
packetProcessor.SubscribeNetSerializable<TransitInteractPacket, NetPeer>(OnSubscribeNetSerializableReceived);
packetProcessor.SubscribeNetSerializable<BotStatePacket, NetPeer>(OnBotStatePacketReceived);
packetProcessor.SubscribeNetSerializable<PingPacket, NetPeer>(OnPingPacketReceived);
packetProcessor.SubscribeNetSerializable<LootSyncPacket, NetPeer>(OnLootSyncPacketReceived);

#if DEBUG
AddDebugPackets();
Expand Down Expand Up @@ -278,6 +281,16 @@ public async Task Init()
FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this));
}

private void OnLootSyncPacketReceived(LootSyncPacket packet, NetPeer peer)
{
SendDataToAll(ref packet, packet.Data.Done ? DeliveryMethod.ReliableOrdered : DeliveryMethod.Unreliable, peer);

if (FikaHostWorld != null)
{
FikaHostWorld.LootSyncPackets.Add(packet.Data);
}
}

private void OnPingPacketReceived(PingPacket packet, NetPeer peer)
{
SendDataToAll(ref packet, DeliveryMethod.ReliableOrdered, peer);
Expand Down

0 comments on commit 7092556

Please sign in to comment.