using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Tab Target by Distance")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Tab Target by Distance")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5421251b-1058-4d19-b9f4-a17e0d7730a6")]
[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 ErenshorTabTargetFix;
[BepInPlugin("com.staticextasy.tabtargetfix", "Tab Target Fix", "1.0.1")]
public class TabTargetFixPlugin : BaseUnityPlugin
{
public static ConfigEntry<bool> EnableTabTargetFix;
private static Harmony harmony;
private static bool isPatched;
private static ManualLogSource log;
private static Character lastSelectedTarget;
public static ConfigEntry<float> MaxTabTargetDistance;
private void Awake()
{
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
//IL_0063: Expected O, but got Unknown
//IL_0083: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Expected O, but got Unknown
log = ((BaseUnityPlugin)this).Logger;
EnableTabTargetFix = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableTabTargetFix", true, "Enable sorting tab targeting to nearest target first.");
MaxTabTargetDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxTabTargetDistance", 20f, new ConfigDescription("Maximum distance (in meters) to tab-target enemies. Targets further than this will not be selected.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 100f), Array.Empty<object>()));
EnableTabTargetFix.SettingChanged += OnSettingChanged;
harmony = new Harmony("com.staticextasy.tabtargetfix");
if (EnableTabTargetFix.Value)
{
ApplyPatches();
}
}
private void OnSettingChanged(object sender, EventArgs e)
{
if (EnableTabTargetFix.Value && !isPatched)
{
ApplyPatches();
}
else if (!EnableTabTargetFix.Value && isPatched)
{
RemovePatches();
}
}
private void ApplyPatches()
{
harmony.PatchAll();
isPatched = true;
log.LogInfo((object)"[TabTargetFix] Patch applied.");
}
private void RemovePatches()
{
harmony.UnpatchSelf();
isPatched = false;
log.LogInfo((object)"[TabTargetFix] Patch removed.");
}
}
[HarmonyPatch(typeof(TargetTracker), "TabTarget")]
public static class TargetTracker_TabTarget_Patch
{
private static readonly FieldRef<TargetTracker, List<Character>> SortedTargetsRef = AccessTools.FieldRefAccess<TargetTracker, List<Character>>("SortedTargets");
private static readonly FieldRef<TargetTracker, int> CurIndexRef = AccessTools.FieldRefAccess<TargetTracker, int>("curIndex");
private static Character lastSelectedTarget = null;
private static bool Prefix(TargetTracker __instance, ref Character __result, Character _cur)
{
//IL_0099: Unknown result type (might be due to invalid IL or missing references)
//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
if (!TabTargetFixPlugin.EnableTabTargetFix.Value)
{
return true;
}
List<Character> nearbyTargets = __instance.NearbyTargets;
List<Character> list = SortedTargetsRef.Invoke(__instance);
_ = ref CurIndexRef.Invoke(__instance);
if (nearbyTargets.Count == 0)
{
__result = null;
lastSelectedTarget = null;
return false;
}
list.Clear();
float value = TabTargetFixPlugin.MaxTabTargetDistance.Value;
foreach (Character item in nearbyTargets)
{
if (!item.MyNPC.SimPlayer && !item.MyStats.Charmed && GameData.PlayerControl.CheckLOS(item) && Vector3.Distance(((Component)GameData.PlayerControl).transform.position, ((Component)item).transform.position) <= value)
{
list.Add(item);
}
}
if (list.Count <= 0)
{
__result = null;
lastSelectedTarget = null;
return false;
}
list.Sort(delegate(Character a, Character b)
{
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
float num2 = Vector3.Distance(((Component)GameData.PlayerControl).transform.position, ((Component)a).transform.position);
float value2 = Vector3.Distance(((Component)GameData.PlayerControl).transform.position, ((Component)b).transform.position);
return num2.CompareTo(value2);
});
int num = 0;
if ((Object)(object)lastSelectedTarget != (Object)null)
{
num = list.IndexOf(lastSelectedTarget);
if (num == -1)
{
num = 0;
}
}
if (Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305))
{
num--;
if (num < 0)
{
num = list.Count - 1;
}
}
else
{
num++;
if (num >= list.Count)
{
num = 0;
}
}
__result = list[num];
lastSelectedTarget = __result;
CurIndexRef.Invoke(__instance) = num;
return false;
}
}
[HarmonyPatch(typeof(TargetTracker), "Update")]
public static class TargetTracker_Update_Patch
{
private static HashSet<int> adjustedObjects = new HashSet<int>();
private static void Postfix(TargetTracker __instance)
{
if ((Object)(object)__instance == (Object)null)
{
return;
}
int instanceID = ((Object)__instance).GetInstanceID();
if (adjustedObjects.Contains(instanceID))
{
return;
}
SphereCollider component = ((Component)__instance).GetComponent<SphereCollider>();
if ((Object)(object)component != (Object)null)
{
if (component.radius < 80f)
{
component.radius = 80f;
((Collider)component).isTrigger = true;
Logger.CreateLogSource("TabTargetFix").LogInfo((object)$"[TabTargetFix] Increased scan radius to {component.radius} for {((Object)__instance).name}");
}
adjustedObjects.Add(instanceID);
}
}
}