0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-12 22:30:44 -05:00

Fixed exception caused by BTR not being destroyed at correct time (!68)

Todo:

* Make BTR spawn at random time during raid instead of at start
* ~~Fix BTR trader services not working for subsequent raids in Streets~~ Drakia provided me with a fix and it's included in this PR

TL;DR - It's ready for testing

Co-authored-by: Nympfonic <arys.steam@gmail.com>
Reviewed-on: SPT-AKI/Modules#68
Co-authored-by: Arys <arys@noreply.dev.sp-tarkov.com>
Co-committed-by: Arys <arys@noreply.dev.sp-tarkov.com>
This commit is contained in:
Arys 2024-01-26 08:28:29 +00:00 committed by chomp
parent 3c9bf227e0
commit a124ace054
6 changed files with 65 additions and 28 deletions

View File

@ -63,6 +63,7 @@ namespace Aki.Custom
new BTRPatch().Enable();
new BTRTransferItemsPatch().Enable();
new BTREndRaidItemDeliveryPatch().Enable();
new BTRBaseLocalGameStopPatch().Enable();
}
catch (Exception ex)
{

View File

@ -57,7 +57,7 @@ namespace Aki.Custom.BTR
_updateTaxiPriceMethod = AccessTools.GetDeclaredMethods(btrControllerType).Single(IsUpdateTaxiPriceMethod);
}
public void Init()
private void Awake()
{
try
{
@ -68,19 +68,19 @@ namespace Aki.Custom.BTR
return;
}
if (gameWorld.BtrController == null && !Singleton<BTRControllerClass>.Instantiated)
if (gameWorld.BtrController == null)
{
Singleton<BTRControllerClass>.Create(new BTRControllerClass());
gameWorld.BtrController = new BTRControllerClass();
}
gameWorld.BtrController = btrController = Singleton<BTRControllerClass>.Instance;
btrController = gameWorld.BtrController;
InitBtr();
}
catch
{
ConsoleScreen.LogError("[AKI-BTR] Unable to spawn BTR. Check logs.");
DestroyGameObjects();
Destroy(this);
throw;
}
}
@ -462,23 +462,18 @@ namespace Aki.Custom.BTR
private void OnDestroy()
{
DestroyGameObjects();
}
private void DestroyGameObjects()
{
if (btrController != null)
if (gameWorld == null)
{
if (btrServerSide != null)
{
Destroy(btrServerSide.gameObject);
}
if (btrClientSide != null)
{
Destroy(btrClientSide.gameObject);
}
return;
}
btrController.Dispose();
StaticManager.KillCoroutine(ref _shootingTargetCoroutine);
StaticManager.KillCoroutine(ref _coverFireTimerCoroutine);
if (TraderServicesManager.Instance != null)
{
TraderServicesManager.Instance.OnTraderServicePurchased -= BtrTraderServicePurchased;
TraderServicesManager.Instance.Clear();
}
if (gameWorld.MainPlayer != null)
@ -486,14 +481,17 @@ namespace Aki.Custom.BTR
gameWorld.MainPlayer.OnBtrStateChanged -= HandleBtrDoorState;
}
if (TraderServicesManager.Instance != null)
if (btrClientSide != null)
{
TraderServicesManager.Instance.OnTraderServicePurchased -= BtrTraderServicePurchased;
Debug.LogWarning("[AKI-BTR] BTRManager - Destroying btrClientSide");
Destroy(btrClientSide.gameObject);
}
StaticManager.KillCoroutine(ref _shootingTargetCoroutine);
StaticManager.KillCoroutine(ref _coverFireTimerCoroutine);
Destroy(this);
if (btrServerSide != null)
{
Debug.LogWarning("[AKI-BTR] BTRManager - Destroying btrServerSide");
Destroy(btrServerSide.gameObject);
}
}
}
}

View File

@ -0,0 +1,34 @@
using Aki.Reflection.Patching;
using Comfort.Common;
using EFT;
using HarmonyLib;
using System.Reflection;
using Object = UnityEngine.Object;
namespace Aki.Custom.BTR.Patches
{
public class BTRBaseLocalGameStopPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(BaseLocalGame<GamePlayerOwner>), nameof(BaseLocalGame<GamePlayerOwner>.Stop));
}
[PatchPrefix]
private static void PatchPrefix()
{
var gameWorld = Singleton<GameWorld>.Instance;
if (gameWorld == null)
{
return;
}
var btrManager = gameWorld.GetComponent<BTRManager>();
if (btrManager != null)
{
Logger.LogWarning("[AKI-BTR] BTRBaseLocalGameStopPatch - Raid Ended: Destroying BTRManager");
Object.Destroy(btrManager);
}
}
}
}

View File

@ -29,7 +29,6 @@ namespace Aki.Custom.BTR.Patches
var gameWorld = Singleton<GameWorld>.Instance;
if (gameWorld == null)
{
Logger.LogError("[AKI-BTR] BTRBotAttachPatch - GameWorld is null");
return false;
}

View File

@ -35,8 +35,7 @@ namespace Aki.Custom.BTR.Patches
return;
}
var btrManager = gameWorld.gameObject.AddComponent<BTRManager>();
btrManager.Init();
gameWorld.gameObject.AddComponent<BTRManager>();
}
catch (System.Exception)
{

View File

@ -40,6 +40,12 @@ namespace Aki.SinglePlayer.Utils.TraderServices
_servicePurchased = new Dictionary<ETraderServiceType, Dictionary<string, bool>>();
}
public void Clear()
{
_servicePurchased.Clear();
_cachedTraders.Clear();
}
public void GetTraderServicesDataFromServer(string traderId)
{
Dictionary<ETraderServiceType, ServiceData> servicesData = Singleton<BackendConfigSettingsClass>.Instance.ServicesData;