From 5c956dec3a7c046e5e2c7c239e3ce60df7f44ae0 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Tue, 21 May 2024 22:18:13 +0200 Subject: [PATCH] Fix startup errors and restructure init of players --- Fika.Core/Coop/Components/CoopHandler.cs | 12 +++--- Fika.Core/Coop/GameMode/CoopGame.cs | 2 +- .../Coop/PacketHandlers/ServerPacketSender.cs | 2 +- Fika.Core/Coop/Players/CoopBot.cs | 11 ++---- Fika.Core/Coop/Players/CoopPlayer.cs | 37 +++++++++++-------- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 15 ++++---- 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Fika.Core/Coop/Components/CoopHandler.cs b/Fika.Core/Coop/Components/CoopHandler.cs index 25fad416..b881b263 100644 --- a/Fika.Core/Coop/Components/CoopHandler.cs +++ b/Fika.Core/Coop/Components/CoopHandler.cs @@ -365,7 +365,7 @@ await Singleton.Instance.LoadBundlesAndCreatePools(PoolManager.Pool } }); - LocalPlayer otherPlayer = SpawnObservedPlayer(spawnObject.Profile, spawnObject.Position, playerId, spawnObject.IsAI, spawnObject.NetId); + ObservedCoopPlayer otherPlayer = SpawnObservedPlayer(spawnObject.Profile, spawnObject.Position, playerId, spawnObject.IsAI, spawnObject.NetId); if (!spawnObject.IsAlive) { @@ -453,9 +453,9 @@ public WorldInteractiveObject GetInteractiveObject(string objectId, out WorldInt return null; } - private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId) + private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId) { - LocalPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity, + ObservedCoopPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity, "Player", isAI == true ? "Bot_" : $"Player_{profile.Nickname}_", EPointOfView.ThirdPerson, profile, isAI, EUpdateQueue.Update, Player.EUpdateMode.Manual, Player.EUpdateMode.Auto, GClass549.Config.CharacterController.ObservedPlayerMode, @@ -468,7 +468,7 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p return null; } - ((CoopPlayer)otherPlayer).NetId = netId; + otherPlayer.NetId = netId; Logger.LogInfo($"SpawnObservedPlayer: {profile.Nickname} spawning with NetId {netId}"); if (!isAI) { @@ -477,7 +477,7 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p if (!Players.ContainsKey(netId)) { - Players.Add(netId, (CoopPlayer)otherPlayer); + Players.Add(netId, otherPlayer); } else { @@ -532,6 +532,8 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p } } + otherPlayer.InitObservedPlayer(); + Logger.LogDebug($"CreateLocalPlayer::{profile.Info.Nickname}::Spawned."); SetWeaponInHandsOfNewPlayer(otherPlayer, () => { }); diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 76e76a27..90902739 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -824,7 +824,7 @@ public override async Task vmethod_2(int playerId, Vector3 position } else { - Singleton.Instance?.SendData(new NetDataWriter(), ref packet, LiteNetLib.DeliveryMethod.ReliableUnordered); + Singleton.Instance.SendData(new NetDataWriter(), ref packet, LiteNetLib.DeliveryMethod.ReliableUnordered); } if (MatchmakerAcceptPatches.IsServer) diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 58efa421..042467f3 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -47,7 +47,7 @@ protected void Start() protected void FixedUpdate() { - if (player == null || Writer == null) + if (player == null || Writer == null || Server == null) { return; } diff --git a/Fika.Core/Coop/Players/CoopBot.cs b/Fika.Core/Coop/Players/CoopBot.cs index e5ad3f05..6b9dc07b 100644 --- a/Fika.Core/Coop/Players/CoopBot.cs +++ b/Fika.Core/Coop/Players/CoopBot.cs @@ -51,6 +51,9 @@ public static async Task CreateBot(int playerId, Vector3 position, InventoryControllerClass inventoryController = new CoopBotInventoryController(player, profile, true); + player.PacketSender = player.gameObject.AddComponent(); + player.PacketReceiver = player.gameObject.AddComponent(); + await player.Init(rotation, layerName, pointOfView, profile, inventoryController, new CoopBotHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl), new CoopObservedStatisticsManager(), null, null, filter, @@ -137,14 +140,6 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy protected override void Start() { - PacketSender = gameObject.AddComponent(); - PacketReceiver = gameObject.AddComponent(); - - /*if (FikaPlugin.DynamicAI.Value) - { - dynamicAi = gameObject.AddComponent(); - }*/ - if (FikaPlugin.DisableBotMetabolism.Value) { HealthController.DisableMetabolism(); diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index 4bef608b..5b1dd094 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -72,6 +72,17 @@ public static async Task Create(int playerId, Vector3 position, Qua achievementsController.Init(); achievementsController.Run(); + if (MatchmakerAcceptPatches.IsServer) + { + player.PacketSender = player.gameObject.AddComponent(); + } + else if (MatchmakerAcceptPatches.IsClient) + { + player.PacketSender = player.gameObject.AddComponent(); + } + + player.PacketReceiver = player.gameObject.AddComponent(); + await player.Init(rotation, layerName, pointOfView, profile, inventoryController, new CoopClientHealthController(profile.Health, player, inventoryController, profile.Skills, aiControl), statisticsManager, questController, achievementsController, filter, @@ -376,15 +387,18 @@ public override void SendHeadlightsPacket(bool isSilent) { GStruct163[] lightStates = _helmetLightControllers.Select(new Func(ClientPlayer.Class1430.class1430_0.method_0)).ToArray(); - PacketSender.CommonPlayerPackets.Enqueue(new() + if (PacketSender != null) { - HasHeadLightsPacket = true, - HeadLightsPacket = new() + PacketSender.CommonPlayerPackets.Enqueue(new() { - Amount = lightStates.Count(), - LightStates = lightStates - } - }); + HasHeadLightsPacket = true, + HeadLightsPacket = new() + { + Amount = lightStates.Count(), + LightStates = lightStates + } + }); + } } public override void OnItemAddedOrRemoved(Item item, ItemAddress location, bool added) @@ -827,15 +841,6 @@ public override void TryInteractionCallback(LootableContainer container) protected virtual void Start() { - if (MatchmakerAcceptPatches.IsServer) - { - PacketSender = gameObject.AddComponent(); - } - else if (MatchmakerAcceptPatches.IsClient) - { - PacketSender = gameObject.AddComponent(); - } - PacketReceiver = gameObject.AddComponent(); Profile.Info.GroupId = "Fika"; if (Side != EPlayerSide.Savage) diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 64ed3d5d..1b24c571 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -115,7 +115,7 @@ public override float SqrCameraDistance } #endregion - public static async Task CreateObservedPlayer(int playerId, Vector3 position, Quaternion rotation, + public static async Task CreateObservedPlayer(int playerId, Vector3 position, Quaternion rotation, string layerName, string prefix, EPointOfView pointOfView, Profile profile, bool aiControl, EUpdateQueue updateQueue, EUpdateMode armsUpdateMode, EUpdateMode bodyUpdateMode, CharacterControllerSpawner.Mode characterControllerMode, Func getSensitivity, @@ -890,7 +890,7 @@ private IEnumerator DestroyNetworkedComponents() } } - protected override async void Start() + public void InitObservedPlayer() { if (gameObject.name.StartsWith("Bot_")) { @@ -954,18 +954,17 @@ protected override async void Start() EFT.Communications.ENotificationDurationType.Default, EFT.Communications.ENotificationIconType.Friend); } - // Spawn these later to prevent errors - while (coopGame.Status != GameStatus.Started) - { - await Task.Delay(TimeSpan.FromSeconds(1)); - } - healthBar = gameObject.AddComponent(); RaycastCameraTransform = Traverse.Create(this).Field("_playerLookRaycastTransform").GetValue(); } } + protected override void Start() + { + // Do nothing + } + public override void LateUpdate() { DistanceDirty = true;