0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -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 BTRPatch().Enable();
new BTRTransferItemsPatch().Enable(); new BTRTransferItemsPatch().Enable();
new BTREndRaidItemDeliveryPatch().Enable(); new BTREndRaidItemDeliveryPatch().Enable();
new BTRBaseLocalGameStopPatch().Enable();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -57,7 +57,7 @@ namespace Aki.Custom.BTR
_updateTaxiPriceMethod = AccessTools.GetDeclaredMethods(btrControllerType).Single(IsUpdateTaxiPriceMethod); _updateTaxiPriceMethod = AccessTools.GetDeclaredMethods(btrControllerType).Single(IsUpdateTaxiPriceMethod);
} }
public void Init() private void Awake()
{ {
try try
{ {
@ -68,19 +68,19 @@ namespace Aki.Custom.BTR
return; 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(); InitBtr();
} }
catch catch
{ {
ConsoleScreen.LogError("[AKI-BTR] Unable to spawn BTR. Check logs."); ConsoleScreen.LogError("[AKI-BTR] Unable to spawn BTR. Check logs.");
DestroyGameObjects(); Destroy(this);
throw; throw;
} }
} }
@ -462,23 +462,18 @@ namespace Aki.Custom.BTR
private void OnDestroy() private void OnDestroy()
{ {
DestroyGameObjects(); if (gameWorld == null)
}
private void DestroyGameObjects()
{
if (btrController != null)
{ {
if (btrServerSide != null) return;
{ }
Destroy(btrServerSide.gameObject);
}
if (btrClientSide != null)
{
Destroy(btrClientSide.gameObject);
}
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) if (gameWorld.MainPlayer != null)
@ -486,14 +481,17 @@ namespace Aki.Custom.BTR
gameWorld.MainPlayer.OnBtrStateChanged -= HandleBtrDoorState; 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); if (btrServerSide != null)
StaticManager.KillCoroutine(ref _coverFireTimerCoroutine); {
Destroy(this); 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; var gameWorld = Singleton<GameWorld>.Instance;
if (gameWorld == null) if (gameWorld == null)
{ {
Logger.LogError("[AKI-BTR] BTRBotAttachPatch - GameWorld is null");
return false; return false;
} }

View File

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

View File

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