using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using Configgy;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("V2poopy")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("V2poopy")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("97f37404-8a67-421d-bdce-1d67fb55da2d")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace V2Companion;
public static class V2Config
{
[Configgable("V2 Companion", null, 0, null)]
public static bool FriendlyV2Enabled = true;
[Configgable("V2 Companion", null, 0, null)]
public static bool TeleportWhenFar = false;
[Configgable("V2 Companion", null, 0, null)]
public static float SpeedMultiplier = 1f;
[Configgable("V2 Companion", null, 0, null)]
public static float HealthMultiplier = 1f;
}
[BepInPlugin("you.ultrakill.v2companion", "V2 Companion", "0.3.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
private GameObject allyV2;
private GameObject v2Prefab;
private NewMovement cachedPlayer;
private NavMeshAgent allyAgent;
private Animator allyAnim;
private float checkTimer;
private float spawnCooldown;
private const float teleportDistance = 45f;
private const float stuckSeconds = 4f;
private Vector3 lastCheckPos;
private float stuckTimer;
private float animDebugTimer;
private string runState;
private string idleState;
private float baseHealth = -1f;
private float lastDmg = -1f;
private float lastSpd = -1f;
private float lastHp = -1f;
private const float stopDistance = 6f;
private const float runDistance = 20f;
private const float baseWalkSpeed = 14f;
private const float baseRunSpeed = 28f;
private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private void Awake()
{
SceneManager.sceneLoaded += OnSceneLoaded;
RegisterConfig();
((BaseUnityPlugin)this).Logger.LogInfo((object)"V2 Companion loaded");
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void RegisterConfig()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected O, but got Unknown
try
{
ConfigBuilder val = new ConfigBuilder("you.ultrakill.v2companion", "V2 Companion");
val.BuildAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"Configgy settings registered");
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Configgy not available, using default settings: " + ex.Message));
}
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
allyV2 = null;
allyAgent = null;
allyAnim = null;
cachedPlayer = null;
baseHealth = -1f;
lastDmg = (lastSpd = (lastHp = -1f));
spawnCooldown = 2f;
}
private void Update()
{
if (!V2Config.FriendlyV2Enabled)
{
if ((Object)(object)allyV2 != (Object)null)
{
Object.Destroy((Object)(object)allyV2);
allyV2 = null;
}
return;
}
if (spawnCooldown > 0f)
{
spawnCooldown -= Time.deltaTime;
return;
}
checkTimer += Time.deltaTime;
if (checkTimer < 0.25f)
{
return;
}
checkTimer = 0f;
if ((Object)(object)cachedPlayer == (Object)null)
{
cachedPlayer = Object.FindObjectOfType<NewMovement>();
}
NewMovement val = cachedPlayer;
if (!((Object)(object)val == (Object)null) && ((Component)val).gameObject.activeInHierarchy)
{
if ((Object)(object)allyV2 == (Object)null)
{
SpawnAlly(((Component)val).transform);
return;
}
ApplyStatsIfChanged();
FollowPlayer(((Component)val).transform);
}
}
private void SpawnAlly(Transform player)
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0047: Unknown result type (might be due to invalid IL or missing references)
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_006a: Unknown result type (might be due to invalid IL or missing references)
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
//IL_0093: Unknown result type (might be due to invalid IL or missing references)
//IL_0094: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)v2Prefab == (Object)null)
{
v2Prefab = FindV2Prefab();
}
if ((Object)(object)v2Prefab == (Object)null)
{
spawnCooldown = 5f;
return;
}
Vector3 val = player.position + player.right * 3f + Vector3.up * 0.5f;
NavMeshHit val2 = default(NavMeshHit);
if (NavMesh.SamplePosition(val, ref val2, 5f, -1))
{
val = ((NavMeshHit)(ref val2)).position;
}
allyV2 = Object.Instantiate<GameObject>(v2Prefab, val, Quaternion.identity);
((Object)allyV2).name = "V2_Ally";
allyV2.SetActive(true);
MakeFriendly(allyV2);
allyAgent = allyV2.GetComponent<NavMeshAgent>();
allyAnim = allyV2.GetComponentInChildren<Animator>();
FindStates();
if ((Object)(object)allyAgent != (Object)null)
{
allyAgent.areaMask = -1;
allyAgent.autoTraverseOffMeshLink = true;
allyAgent.autoRepath = true;
allyAgent.acceleration = 60f;
allyAgent.angularSpeed = 720f;
allyAgent.updatePosition = true;
allyAgent.updateRotation = true;
}
EnemyIdentifier component = allyV2.GetComponent<EnemyIdentifier>();
if ((Object)(object)component != (Object)null)
{
baseHealth = GetFloat(component, "health", -1f);
}
lastDmg = (lastSpd = (lastHp = -1f));
ApplyStatsIfChanged();
LogDiagnostics(allyV2);
((BaseUnityPlugin)this).Logger.LogInfo((object)"Spawned friendly V2");
}
private void MakeFriendly(GameObject v2)
{
EnemyIdentifier component = v2.GetComponent<EnemyIdentifier>();
if ((Object)(object)component == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"V2 has no EnemyIdentifier");
return;
}
SetMember(component, "ignorePlayer", true);
SetMember(component, "attackEnemies", true);
SetMember(component, "dontCountAsKills", true);
}
private void ApplyStatsIfChanged()
{
if ((Object)(object)allyV2 == (Object)null)
{
return;
}
float num = Mathf.Max(0.01f, V2Config.HealthMultiplier);
if (Mathf.Approximately(num, lastHp))
{
return;
}
if (baseHealth > 0f)
{
float num2 = baseHealth * num;
EnemyIdentifier component = allyV2.GetComponent<EnemyIdentifier>();
if ((Object)(object)component != (Object)null)
{
SetMember(component, "health", num2);
}
Component[] components = allyV2.GetComponents<Component>();
foreach (Component val in components)
{
if ((Object)(object)val != (Object)null && ((object)val).GetType().Name == "Machine")
{
SetMember(val, "health", num2);
}
}
}
lastHp = num;
}
private void FollowPlayer(Transform player)
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_0069: Unknown result type (might be due to invalid IL or missing references)
//IL_006e: Unknown result type (might be due to invalid IL or missing references)
//IL_0132: Unknown result type (might be due to invalid IL or missing references)
//IL_0137: Unknown result type (might be due to invalid IL or missing references)
//IL_013a: Unknown result type (might be due to invalid IL or missing references)
//IL_0154: Unknown result type (might be due to invalid IL or missing references)
//IL_0159: Unknown result type (might be due to invalid IL or missing references)
//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
float num = Vector3.Distance(allyV2.transform.position, player.position);
if (V2Config.TeleportWhenFar && num > 45f)
{
TeleportToPlayer(player);
return;
}
float num2 = Vector3.Distance(allyV2.transform.position, lastCheckPos);
lastCheckPos = allyV2.transform.position;
if (num > 8f && num2 < 0.5f)
{
stuckTimer += 0.25f;
}
else
{
stuckTimer = 0f;
}
if (V2Config.TeleportWhenFar && stuckTimer >= 4f)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"V2 stuck, teleporting to player");
TeleportToPlayer(player);
}
else
{
if ((Object)(object)allyAgent == (Object)null || !((Behaviour)allyAgent).enabled || !allyAgent.isOnNavMesh)
{
return;
}
if (num > 6f)
{
Vector3 position = player.position;
NavMeshHit val = default(NavMeshHit);
if (NavMesh.SamplePosition(player.position, ref val, 30f, -1))
{
position = ((NavMeshHit)(ref val)).position;
float num3 = Mathf.Max(0.1f, V2Config.SpeedMultiplier);
allyAgent.isStopped = false;
allyAgent.speed = ((num > 20f) ? 28f : 14f) * num3;
allyAgent.stoppingDistance = 6f;
allyAgent.SetDestination(position);
}
}
else
{
allyAgent.isStopped = true;
}
}
}
private void TeleportToPlayer(Transform player)
{
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Unknown result type (might be due to invalid IL or missing references)
//IL_0135: Unknown result type (might be due to invalid IL or missing references)
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
Vector3 val = player.position + player.right * 3f;
NavMeshHit val2 = default(NavMeshHit);
if (NavMesh.SamplePosition(val, ref val2, 30f, -1))
{
val = ((NavMeshHit)(ref val2)).position;
}
bool flag = false;
if ((Object)(object)allyAgent != (Object)null && ((Behaviour)allyAgent).enabled)
{
flag = allyAgent.Warp(val);
}
if (!flag)
{
bool enabled = (Object)(object)allyAgent != (Object)null && ((Behaviour)allyAgent).enabled;
if ((Object)(object)allyAgent != (Object)null)
{
((Behaviour)allyAgent).enabled = false;
}
allyV2.transform.position = val;
Rigidbody component = allyV2.GetComponent<Rigidbody>();
if ((Object)(object)component != (Object)null)
{
component.position = val;
component.velocity = Vector3.zero;
}
if ((Object)(object)allyAgent != (Object)null)
{
((Behaviour)allyAgent).enabled = enabled;
}
}
stuckTimer = 0f;
lastCheckPos = allyV2.transform.position;
}
private void LateUpdate()
{
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ac: Invalid comparison between Unknown and I4
//IL_014f: Unknown result type (might be due to invalid IL or missing references)
//IL_0155: Invalid comparison between Unknown and I4
//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
//IL_0191: Unknown result type (might be due to invalid IL or missing references)
//IL_0196: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)allyV2 == (Object)null || (Object)(object)allyAgent == (Object)null || (Object)(object)allyAnim == (Object)null)
{
return;
}
Vector3 velocity;
int num;
if (((Behaviour)allyAgent).enabled && allyAgent.isOnNavMesh && !allyAgent.isStopped)
{
velocity = allyAgent.velocity;
num = ((((Vector3)(ref velocity)).magnitude > 0.5f) ? 1 : 0);
}
else
{
num = 0;
}
bool flag = (byte)num != 0;
AnimatorControllerParameter[] parameters = allyAnim.parameters;
foreach (AnimatorControllerParameter val in parameters)
{
string text = val.name.ToLowerInvariant();
if ((int)val.type == 4)
{
if (text.Contains("run") || text.Contains("walk") || text.Contains("move"))
{
allyAnim.SetBool(val.name, flag);
}
else if (flag && (text.Contains("slid") || text.Contains("fall") || text.Contains("inair")))
{
allyAnim.SetBool(val.name, false);
}
}
else if ((int)val.type == 1 && (text.Contains("speed") || text.Contains("velocity")))
{
Animator obj = allyAnim;
string name = val.name;
velocity = allyAgent.velocity;
obj.SetFloat(name, ((Vector3)(ref velocity)).magnitude);
}
}
if (runState != null)
{
AnimatorStateInfo currentAnimatorStateInfo = allyAnim.GetCurrentAnimatorStateInfo(0);
if (flag)
{
if (!((AnimatorStateInfo)(ref currentAnimatorStateInfo)).IsName(runState) && !allyAnim.IsInTransition(0))
{
allyAnim.CrossFade(runState, 0.1f);
}
}
else if (idleState != null && ((AnimatorStateInfo)(ref currentAnimatorStateInfo)).IsName(runState) && !allyAnim.IsInTransition(0))
{
allyAnim.CrossFade(idleState, 0.15f);
}
}
animDebugTimer += Time.deltaTime;
if (flag && animDebugTimer >= 3f)
{
animDebugTimer = 0f;
LogAnimatorState();
}
}
private void FindStates()
{
runState = null;
idleState = null;
if ((Object)(object)allyAnim == (Object)null || (Object)(object)allyAnim.runtimeAnimatorController == (Object)null)
{
return;
}
string text = null;
AnimationClip[] animationClips = allyAnim.runtimeAnimatorController.animationClips;
foreach (AnimationClip val in animationClips)
{
string text2 = ((Object)val).name.ToLowerInvariant();
if (allyAnim.HasState(0, Animator.StringToHash(((Object)val).name)))
{
if (runState == null && (text2.Contains("run") || text2.Contains("sprint") || text2.Contains("jog")))
{
runState = ((Object)val).name;
}
else if (text == null && text2.Contains("walk"))
{
text = ((Object)val).name;
}
else if (idleState == null && text2.Contains("idle"))
{
idleState = ((Object)val).name;
}
}
}
if (runState == null)
{
runState = text;
}
((BaseUnityPlugin)this).Logger.LogInfo((object)("Animation states chosen: run=" + runState + " idle=" + idleState));
}
private void LogAnimatorState()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0047: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a5: Invalid comparison between Unknown and I4
//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
//IL_0102: Invalid comparison between Unknown and I4
//IL_015a: Unknown result type (might be due to invalid IL or missing references)
//IL_0160: Invalid comparison between Unknown and I4
Vector3 velocity = allyAgent.velocity;
string text = "[AnimState] speed=" + ((Vector3)(ref velocity)).magnitude.ToString("F1") + " clips=";
AnimatorClipInfo[] currentAnimatorClipInfo = allyAnim.GetCurrentAnimatorClipInfo(0);
for (int i = 0; i < currentAnimatorClipInfo.Length; i++)
{
AnimatorClipInfo val = currentAnimatorClipInfo[i];
text = text + ((Object)((AnimatorClipInfo)(ref val)).clip).name + ",";
}
text += " params=";
AnimatorControllerParameter[] parameters = allyAnim.parameters;
foreach (AnimatorControllerParameter val2 in parameters)
{
if ((int)val2.type == 4)
{
text = text + val2.name + "=" + allyAnim.GetBool(val2.name) + ",";
}
else if ((int)val2.type == 1)
{
text = text + val2.name + "=" + allyAnim.GetFloat(val2.name).ToString("F2") + ",";
}
else if ((int)val2.type == 3)
{
text = text + val2.name + "=" + allyAnim.GetInteger(val2.name) + ",";
}
}
((BaseUnityPlugin)this).Logger.LogInfo((object)text);
}
private void LogDiagnostics(GameObject v2)
{
//IL_0282: Unknown result type (might be due to invalid IL or missing references)
//IL_0287: Unknown result type (might be due to invalid IL or missing references)
((BaseUnityPlugin)this).Logger.LogInfo((object)"=== V2 diagnostics ===");
Component[] componentsInChildren = v2.GetComponentsInChildren<Component>(true);
foreach (Component val in componentsInChildren)
{
if ((Object)(object)val != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Component: " + ((object)val).GetType().Name + " on " + ((Object)val.gameObject).name));
}
}
if ((Object)(object)allyAgent != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Agent: enabled=" + ((Behaviour)allyAgent).enabled + " onNavMesh=" + allyAgent.isOnNavMesh + " updatePosition=" + allyAgent.updatePosition + " speed=" + allyAgent.speed + " height=" + allyAgent.height + " radius=" + allyAgent.radius));
}
else
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"V2 has NO NavMeshAgent");
}
if ((Object)(object)allyAnim != (Object)null && (Object)(object)allyAnim.runtimeAnimatorController != (Object)null)
{
string text = "";
AnimationClip[] animationClips = allyAnim.runtimeAnimatorController.animationClips;
foreach (AnimationClip val2 in animationClips)
{
text = text + ((Object)val2).name + ", ";
}
((BaseUnityPlugin)this).Logger.LogInfo((object)("All animation clips: " + text));
}
Rigidbody component = v2.GetComponent<Rigidbody>();
if ((Object)(object)component != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Rigidbody: kinematic=" + component.isKinematic));
}
if ((Object)(object)allyAnim != (Object)null)
{
AnimatorControllerParameter[] parameters = allyAnim.parameters;
foreach (AnimatorControllerParameter val3 in parameters)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Animator param: " + val3.name + " (" + ((object)val3.type/*cast due to .constrained prefix*/).ToString() + ")"));
}
}
else
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"V2 has NO Animator");
}
}
private void SetMember(object target, string name, object value)
{
Type type = target.GetType();
FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (field != null)
{
try
{
field.SetValue(target, Convert.ChangeType(value, field.FieldType));
return;
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not set " + name + ": " + ex.Message));
return;
}
}
PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property != null && property.CanWrite)
{
try
{
property.SetValue(target, Convert.ChangeType(value, property.PropertyType), null);
return;
}
catch (Exception ex2)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not set " + name + ": " + ex2.Message));
return;
}
}
((BaseUnityPlugin)this).Logger.LogWarning((object)("Member not found on " + type.Name + ": " + name));
}
private float GetFloat(object target, string name, float fallback)
{
Type type = target.GetType();
FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (field != null)
{
try
{
return Convert.ToSingle(field.GetValue(target));
}
catch
{
}
}
PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property != null && property.CanRead)
{
try
{
return Convert.ToSingle(property.GetValue(target, null));
}
catch
{
}
}
return fallback;
}
private void CallMethod(object target, string name)
{
MethodInfo method = target.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
if (method == null)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Method not found on " + target.GetType().Name + ": " + name));
return;
}
try
{
method.Invoke(target, null);
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not call " + name + ": " + ex.Message));
}
}
private GameObject FindV2Prefab()
{
GameObject[] array = Resources.FindObjectsOfTypeAll<GameObject>();
foreach (GameObject val in array)
{
if (!((Object)(object)val == (Object)null) && !(((Object)val).name != "V2") && !((Object)(object)val.GetComponent<EnemyIdentifier>() == (Object)null))
{
return val;
}
}
return null;
}
}