2024-01-03 16:06:50 +00:00
using Aki.Reflection.Patching ;
using Comfort.Common ;
using EFT ;
using EFT.NextObservedPlayer ;
using EFT.UI ;
using EFT.Vehicle ;
using HarmonyLib ;
using System ;
using System.Reflection ;
2024-01-23 08:47:09 +00:00
namespace Aki.Custom.BTR.Patches
2024-01-03 16:06:50 +00:00
{
// Fixes the BTR Bot initialization in AttachBot() of BTRTurretView
//
// Context:
// ClientGameWorld in LiveEFT will register the server-side BTR Bot as type ObservedPlayerView and is stored in GameWorld's allObservedPlayersByID dictionary.
// In SPT, GameWorld.allObservedPlayersByID is empty which results in the game never finishing the initialization of the BTR Bot which includes disabling its gun, voice and mesh renderers.
// For now, we do dirty patches to work around the lack of ObservedPlayerView, using Player instead.
2024-01-23 08:47:09 +00:00
public class BTRBotAttachPatch : ModulePatch
2024-01-03 16:06:50 +00:00
{
protected override MethodBase GetTargetMethod ( )
{
return AccessTools . Method ( typeof ( BTRTurretView ) , nameof ( BTRTurretView . AttachBot ) ) ;
}
[PatchPrefix]
2024-01-20 09:20:32 +00:00
private static bool PatchPrefix ( BTRTurretView __instance , int btrBotId )
2024-01-03 16:06:50 +00:00
{
var gameWorld = Singleton < GameWorld > . Instance ;
2024-01-25 08:52:33 +00:00
if ( gameWorld = = null )
{
return false ;
}
2024-01-03 16:06:50 +00:00
2024-01-25 08:52:33 +00:00
var btrTurretViewTupleField = ( ValueTuple < ObservedPlayerView , bool > ) AccessTools . Field ( __instance . GetType ( ) , "valueTuple_0" ) . GetValue ( __instance ) ;
if ( ! btrTurretViewTupleField . Item2 )
2024-01-03 16:06:50 +00:00
{
2024-01-25 08:52:33 +00:00
__instance . method_1 ( btrBotId ) ;
return false ;
2024-01-03 16:06:50 +00:00
}
2024-01-25 08:52:33 +00:00
var btrController = gameWorld . BtrController ;
if ( btrController = = null )
2024-01-03 16:06:50 +00:00
{
2024-01-25 08:52:33 +00:00
Logger . LogError ( "[AKI-BTR] BTRBotAttachPatch - BtrController is null" ) ;
2024-01-03 16:06:50 +00:00
return false ;
}
2024-01-25 08:52:33 +00:00
var btrBotShooter = btrController . BotShooterBtr ;
if ( btrBotShooter = = null )
2024-01-03 16:06:50 +00:00
{
2024-01-25 08:52:33 +00:00
Logger . LogError ( "[AKI-BTR] BTRBotAttachPatch - BtrBotShooter is null" ) ;
2024-01-03 16:06:50 +00:00
return false ;
}
2024-01-25 08:52:33 +00:00
2024-01-03 16:06:50 +00:00
try
{
2024-01-25 08:52:33 +00:00
var btrBot = btrBotShooter . GetPlayer ;
2024-01-20 09:20:32 +00:00
var botRootTransform = __instance . BotRoot ;
2024-01-03 16:06:50 +00:00
btrBot . Transform . position = botRootTransform . position ;
2024-01-20 09:20:32 +00:00
var firearmController = btrBot . gameObject . GetComponent < Player . FirearmController > ( ) ;
var currentWeaponPrefab = ( WeaponPrefab ) AccessTools . Field ( firearmController . GetType ( ) , "weaponPrefab_0" ) . GetValue ( firearmController ) ;
2024-01-03 16:06:50 +00:00
currentWeaponPrefab . transform . position = botRootTransform . position ;
btrBot . PlayerBones . Weapon_Root_Anim . SetPositionAndRotation ( botRootTransform . position , botRootTransform . rotation ) ;
2024-01-04 08:51:06 +00:00
2024-01-03 16:06:50 +00:00
return false ;
}
catch
{
2024-01-25 08:52:33 +00:00
ConsoleScreen . LogError ( "[AKI-BTR] Could not finish BtrBot initialization. Check logs." ) ;
2024-01-03 16:06:50 +00:00
throw ;
}
}
}
}