diff --git a/Source/Coop/Components/CoopGameComponents/SITGameComponent.cs b/Source/Coop/Components/CoopGameComponents/SITGameComponent.cs index 8b58e109..a1aced0d 100644 --- a/Source/Coop/Components/CoopGameComponents/SITGameComponent.cs +++ b/Source/Coop/Components/CoopGameComponents/SITGameComponent.cs @@ -348,16 +348,16 @@ private void GameWorld_AfterGameStarted() { GameWorldGameStarted = true; Logger.LogDebug(nameof(GameWorld_AfterGameStarted)); - //if (Singleton.Instance.RegisteredPlayers.Any()) - //{ - // // Send My Player to Aki, so that other clients know about me - // CoopSITGame.SendPlayerDataToServer((LocalPlayer)Singleton.Instance.RegisteredPlayers.First(x => x.IsYourPlayer)); - //} this.GetOrAddComponent(); - this.GetOrAddComponent(); this.GetOrAddComponent(); + // Add Server Authority components + if (SITMatchmaking.IsServer) + { + this.GetOrAddComponent(); + } + StartCoroutine(SendPlayerStatePacket()); // tell clients which exfils are disabled diff --git a/Source/Coop/Components/CoopGameComponents/SITGameTimeAndWeatherSyncComponent.cs b/Source/Coop/Components/CoopGameComponents/SITGameTimeAndWeatherSyncComponent.cs index 3928948f..25ff730c 100644 --- a/Source/Coop/Components/CoopGameComponents/SITGameTimeAndWeatherSyncComponent.cs +++ b/Source/Coop/Components/CoopGameComponents/SITGameTimeAndWeatherSyncComponent.cs @@ -32,9 +32,9 @@ void Update() { LastTimeSent = DateTime.Now; - TimeAndWeatherPacket packet = new TimeAndWeatherPacket(); + TimeAndWeatherPacket packet = new(); - var sitGame = Singleton.Instance as BaseLocalGame; + var sitGame = Singleton.Instance; if (sitGame.GameDateTime != null) packet.GameDateTime = sitGame.GameDateTime.Calculate().Ticks; @@ -58,6 +58,13 @@ void Update() Networking.GameClient.SendData(packet.Serialize()); } + + if (sitGame.GameTimer.StartDateTime.HasValue && sitGame.GameTimer.SessionTime.HasValue) + { + RaidTimerPacket raidTimerPacket = new(); + raidTimerPacket.SessionTime = (sitGame.GameTimer.SessionTime - sitGame.GameTimer.PastTime).Value.Ticks; + Networking.GameClient.SendData(raidTimerPacket.Serialize()); + } } } } diff --git a/Source/Coop/NetworkPacket/Raid/RaidTimerPacket.cs b/Source/Coop/NetworkPacket/Raid/RaidTimerPacket.cs index 35c43a5c..5c6c9bdd 100644 --- a/Source/Coop/NetworkPacket/Raid/RaidTimerPacket.cs +++ b/Source/Coop/NetworkPacket/Raid/RaidTimerPacket.cs @@ -1,20 +1,27 @@ -using EFT.UI.BattleTimer; +using BepInEx.Logging; +using Comfort.Common; +using EFT; +using EFT.UI.BattleTimer; +using HarmonyLib.Tools; using StayInTarkov.Coop.Components.CoopGameComponents; using StayInTarkov.Coop.Matchmaker; using StayInTarkov.Coop.SITGameModes; using System; -using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace StayInTarkov.Coop.NetworkPacket.Raid { internal sealed class RaidTimerPacket : BasePacket { + static RaidTimerPacket() + { + Logger = BepInEx.Logging.Logger.CreateLogSource(nameof(RaidTimerPacket)); + } + public long SessionTime { get; set; } + public static ManualLogSource Logger { get; } public RaidTimerPacket() : base(nameof(RaidTimerPacket)) { @@ -23,7 +30,7 @@ public RaidTimerPacket() : base(nameof(RaidTimerPacket)) public override byte[] Serialize() { var ms = new MemoryStream(); - using BinaryWriter writer = new BinaryWriter(ms); + using BinaryWriter writer = new(ms); WriteHeader(writer); writer.Write(SessionTime); return ms.ToArray(); @@ -31,7 +38,7 @@ public override byte[] Serialize() public override ISITPacket Deserialize(byte[] bytes) { - using BinaryReader reader = new BinaryReader(new MemoryStream(bytes)); + using BinaryReader reader = new(new MemoryStream(bytes)); ReadHeader(reader); SessionTime = reader.ReadInt64(); return this; @@ -48,9 +55,24 @@ public override void Process() var sessionTime = new TimeSpan(SessionTime); - if (coopGameComponent.LocalGameInstance is CoopSITGame coopGame) + if (!Singleton.Instantiated) + { + Logger.LogError($"{nameof(Process)} failed {nameof(ISITGame)} was not instantiated!"); + return; + } + + if (!Singleton.Instantiated) + { + Logger.LogError($"{nameof(Process)} failed {nameof(AbstractGame)} was not instantiated!"); + return; + } + + var sitGame = Singleton.Instance; + var abstractGame = Singleton.Instance; + + //if (coopGameComponent.LocalGameInstance is CoopSITGame coopGame) { - var gameTimer = coopGame.GameTimer; + var gameTimer = sitGame.GameTimer; if (gameTimer.StartDateTime.HasValue && gameTimer.SessionTime.HasValue) { if (gameTimer.PastTime.TotalSeconds < 3) @@ -64,7 +86,7 @@ public override void Process() StayInTarkovHelperConstants.Logger.LogInfo($"RaidTimer: New SessionTime {timeRemain.TraderFormat()}"); gameTimer.ChangeSessionTime(timeRemain); - MainTimerPanel mainTimerPanel = ReflectionHelpers.GetFieldOrPropertyFromInstance(coopGame.GameUi.TimerPanel, "_mainTimerPanel", false); + MainTimerPanel mainTimerPanel = ReflectionHelpers.GetFieldOrPropertyFromInstance(abstractGame.GameUi.TimerPanel, "_mainTimerPanel", false); if (mainTimerPanel != null) { FieldInfo extractionDateTimeField = ReflectionHelpers.GetFieldFromType(typeof(TimerPanel), "dateTime_0"); diff --git a/Source/Coop/SITGameModes/CoopSITGame.cs b/Source/Coop/SITGameModes/CoopSITGame.cs index 784fbd27..cba429fd 100644 --- a/Source/Coop/SITGameModes/CoopSITGame.cs +++ b/Source/Coop/SITGameModes/CoopSITGame.cs @@ -216,7 +216,7 @@ void OnDestroy() Logger.LogDebug("OnDestroy()"); Singleton.Instance.AfterGameStarted -= Instance_AfterGameStarted; - Comfort.Common.Singleton.TryRelease(this); + Comfort.Common.Singleton.TryRelease(this); } public void CreateCoopGameComponent() @@ -242,8 +242,6 @@ public void CreateCoopGameComponent() if (SITMatchmaking.IsServer) { - StartCoroutine(GameTimerSync()); - StartCoroutine(TimeAndWeatherSync()); StartCoroutine(ArmoredTrainTimeSync()); } @@ -288,79 +286,6 @@ private IEnumerator DebugObjects() } } - private IEnumerator GameTimerSync() - { - var waitSeconds = new WaitForSeconds(10f); - - while (true) - { - yield return waitSeconds; - - if (!SITGameComponent.TryGetCoopGameComponent(out var coopGameComponent)) - yield break; - - if (GameTimer.StartDateTime.HasValue && GameTimer.SessionTime.HasValue) - { - //Dictionary raidTimerDict = new() - //{ - // { "serverId", coopGameComponent.ServerId }, - // { "m", "RaidTimer" }, - // { "sessionTime", (GameTimer.SessionTime - GameTimer.PastTime).Value.Ticks }, - //}; - //Networking.GameClient.SendData(raidTimerDict.ToJson()); - RaidTimerPacket packet = new RaidTimerPacket(); - packet.SessionTime = (GameTimer.SessionTime - GameTimer.PastTime).Value.Ticks; - Networking.GameClient.SendData(packet.Serialize()); - } - } - } - - private IEnumerator TimeAndWeatherSync() - { - var waitSeconds = new WaitForSeconds(15f); - - while (true) - { - yield return waitSeconds; - - if (!SITGameComponent.TryGetCoopGameComponent(out var coopGameComponent)) - yield break; - - Dictionary timeAndWeatherDict = new() - { - { "serverId", coopGameComponent.ServerId }, - { "m", "TimeAndWeather" } - }; - - if (GameDateTime != null) - timeAndWeatherDict.Add("GameDateTime", GameDateTime.Calculate().Ticks); - - var weatherController = WeatherController.Instance; - if (weatherController != null) - { - if (weatherController.CloudsController != null) - timeAndWeatherDict.Add("CloudDensity", weatherController.CloudsController.Density); - - var weatherCurve = weatherController.WeatherCurve; - if (weatherCurve != null) - { - timeAndWeatherDict.Add("Fog", weatherCurve.Fog); - timeAndWeatherDict.Add("LightningThunderProbability", weatherCurve.LightningThunderProbability); - timeAndWeatherDict.Add("Rain", weatherCurve.Rain); - timeAndWeatherDict.Add("Temperature", weatherCurve.Temperature); - timeAndWeatherDict.Add("WindDirection.x", weatherCurve.Wind.x); - timeAndWeatherDict.Add("WindDirection.y", weatherCurve.Wind.y); - timeAndWeatherDict.Add("TopWindDirection.x", weatherCurve.TopWind.x); - timeAndWeatherDict.Add("TopWindDirection.y", weatherCurve.TopWind.y); - } - - string packet = timeAndWeatherDict.ToJson(); - Logger.LogDebug(packet); - Networking.GameClient.SendData(packet); - } - } - } - private IEnumerator ArmoredTrainTimeSync() { var waitSeconds = new WaitForSeconds(30f); @@ -619,7 +544,7 @@ public override async Task vmethod_2(int playerId, Vector3 position switch (SITMatchmaking.SITProtocol) { case ESITProtocol.RelayTcp: - JObject j = new JObject(); + JObject j = new(); j.Add("serverId", SITGameComponent.GetServerId()); j.Add("profileId", profile.ProfileId); j.Add("connect", true); @@ -771,7 +696,7 @@ private async Task WaitForPlayersToSpawn() } //}); - ReadyToStartGamePacket packet = new ReadyToStartGamePacket(SITMatchmaking.Profile.ProfileId); + ReadyToStartGamePacket packet = new(SITMatchmaking.Profile.ProfileId); GameClient.SendData(packet.Serialize()); } @@ -836,7 +761,7 @@ private async Task WaitForPlayersToBeReady() if (!SITMatchmaking.IsClient) { - HostStartingGamePacket packet = new HostStartingGamePacket(); + HostStartingGamePacket packet = new(); GameClient.SendData(packet.Serialize()); } } @@ -895,7 +820,7 @@ private async Task WaitForHostToStart() private void SendRequestSpawnPlayersPacket() { - RequestSpawnPlayersPacket requestSpawnPlayersPacket = new RequestSpawnPlayersPacket([Singleton.Instance.MainPlayer.ProfileId]); + RequestSpawnPlayersPacket requestSpawnPlayersPacket = new([Singleton.Instance.MainPlayer.ProfileId]); GameClient.SendData(requestSpawnPlayersPacket.Serialize()); } @@ -1069,7 +994,7 @@ public override IEnumerator vmethod_4(float startDelay, BotControllerSettings co try { bool isWinter = BackEndSession.IsWinter; - WinterEventController winterEventController = new WinterEventController(); + WinterEventController winterEventController = new(); ReflectionHelpers.GetFieldFromTypeByFieldType(typeof(GameWorld), typeof(WinterEventController)).SetValue(Singleton.Instance, winterEventController); winterEventController.Run(isWinter).ContinueWith(x => { if (x.IsFaulted) Logger.LogError(x.Exception); return Task.CompletedTask; }); } @@ -1398,7 +1323,7 @@ public override void CleanUp() value.Dispose(); - if(value.gameObject != null) + if (value.gameObject != null) AssetPoolObject.ReturnToPool(value.gameObject); } catch (Exception exception) @@ -1477,19 +1402,19 @@ public async Task Run(BotControllerSettings botsSettings, string backendUrl, Inv location = await BackEndSession.LoadLocationLoot(Location_0.Id, variantId); } } - + BackendConfigManagerConfig config = BackendConfigManager.Config; if (config.FixedFrameRate > 0f) { base.FixedDeltaTime = 1f / config.FixedFrameRate; } - - EFT.Player player = await CreatePlayerSpawn(); - dictionary_0.Add(player.ProfileId, player); - gparam_0 = func_1(player); - PlayerCameraController.Create(gparam_0.Player); - CameraClass.Instance.SetOcclusionCullingEnabled(Location_0.OcculsionCullingEnabled); - CameraClass.Instance.IsActive = false; + + EFT.Player player = await CreatePlayerSpawn(); + dictionary_0.Add(player.ProfileId, player); + gparam_0 = func_1(player); + PlayerCameraController.Create(gparam_0.Player); + CameraClass.Instance.SetOcclusionCullingEnabled(Location_0.OcculsionCullingEnabled); + CameraClass.Instance.IsActive = false; await SpawnLoot(location); await WaitForPlayersToSpawn(); diff --git a/Source/Coop/SITGameModes/ISITGame.cs b/Source/Coop/SITGameModes/ISITGame.cs index 94e0fc27..fad03770 100644 --- a/Source/Coop/SITGameModes/ISITGame.cs +++ b/Source/Coop/SITGameModes/ISITGame.cs @@ -47,5 +47,9 @@ public interface ISITGame //Task WaitForPlayersToSpawn(); //Task WaitForPlayersToBeReady(); + GameDateTime GameDateTime { get; set; } + + public GameTimerClass GameTimer { get; set; } + } }