using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LostEnemyFix.Patches;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LostEnemyFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LostEnemyFix")]
[assembly: AssemblyTitle("LostEnemyFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace LostEnemyFix
{
[BepInPlugin("JacobG5.LostEnemyFix", "LostEnemyFix", "1.1.0")]
public class LostEnemyFix : BaseUnityPlugin
{
public enum KillLostEnemies
{
Aggressive,
Reasonable,
Off
}
private const string modGUID = "JacobG5.LostEnemyFix";
private const string modName = "LostEnemyFix";
private const string modVersion = "1.1.0";
private readonly Harmony harmony = new Harmony("JacobG5.LostEnemyFix");
internal ManualLogSource mls;
public static LostEnemyFix Instance;
public static ConfigEntry<KillLostEnemies> killLostEnemiesMode;
public static ConfigEntry<bool> attemptRelocation;
private void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Instance = this;
}
killLostEnemiesMode = ((BaseUnityPlugin)this).Config.Bind<KillLostEnemies>("Main", "Kill Lost Enemies Mode", KillLostEnemies.Reasonable, "Aggressive: Kills enemies when they throw a NavMesh error.\nReasonable: Kills enemies after they have thrown 8 NavMesh errors.\nOff: Vanilla behavior");
attemptRelocation = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "Attempt Relocation", true, "When instructed to kill an enemy based on the selected mode when enabled will attempt to search for a nearby navmesh first and teleport to the enemy to that. If no navmesh is found will kill the enemy.");
mls = Logger.CreateLogSource("JacobG5.LostEnemyFix");
harmony.PatchAll(typeof(EnemyAIPatch));
harmony.PatchAll(typeof(RoundManagerPatch));
}
}
}
namespace LostEnemyFix.Patches
{
[HarmonyPatch(typeof(EnemyAI))]
internal class EnemyAIPatch
{
public static readonly Dictionary<EnemyAI, int> EnemyErrors = new Dictionary<EnemyAI, int>();
public static bool SetAgentDestination(EnemyAI enemy)
{
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
//IL_010b: Unknown result type (might be due to invalid IL or missing references)
//IL_0165: Unknown result type (might be due to invalid IL or missing references)
//IL_019d: Unknown result type (might be due to invalid IL or missing references)
//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
if (enemy.agent.isOnNavMesh || enemy.agent.isOnOffMeshLink)
{
return enemy.agent.SetDestination(enemy.destination);
}
bool flag = false;
switch (LostEnemyFix.killLostEnemiesMode.Value)
{
case LostEnemyFix.KillLostEnemies.Reasonable:
if (EnemyErrors.ContainsKey(enemy))
{
EnemyErrors[enemy]++;
if (EnemyErrors[enemy] > 8)
{
flag = true;
}
}
else
{
EnemyErrors.Add(enemy, 1);
}
break;
case LostEnemyFix.KillLostEnemies.Aggressive:
flag = true;
break;
}
string text = (((Object)(object)enemy.enemyType == (Object)null) ? ((Object)enemy).name : enemy.enemyType.enemyName);
LostEnemyFix.Instance.mls.LogWarning((object)$"{text} Failed to locate NavMesh.\nEnemy Position: {((Component)enemy).transform.position} Target Destination: {enemy.destination} IsOutside: {enemy.isOutside} State: {enemy.currentBehaviourState.name}");
if (flag && ((NetworkBehaviour)enemy).IsOwner)
{
NavMeshHit val = default(NavMeshHit);
if (LostEnemyFix.attemptRelocation.Value && NavMesh.SamplePosition(((Component)enemy).transform.position, ref val, 50f, enemy.agent.areaMask))
{
LostEnemyFix.Instance.mls.LogInfo((object)$"Relocating {text} to {((NavMeshHit)(ref val)).position} to prevent further errors.");
enemy.agent.Warp(((NavMeshHit)(ref val)).position);
}
else
{
LostEnemyFix.Instance.mls.LogInfo((object)("Killing " + text + " to prevent further errors."));
enemy.KillEnemyServerRpc(true);
}
}
return false;
}
[HarmonyPatch("DoAIInterval")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo SetDestinationCall = AccessTools.Method(typeof(NavMeshAgent), "SetDestination", new Type[1] { typeof(Vector3) }, (Type[])null);
MethodInfo ReplacementMethod = AccessTools.Method(typeof(EnemyAIPatch), "SetAgentDestination", new Type[1] { typeof(EnemyAI) }, (Type[])null);
bool found = false;
List<CodeInstruction> newInstructions = new List<CodeInstruction>();
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt && CodeInstructionExtensions.Calls(instruction, SetDestinationCall))
{
found = true;
newInstructions.RemoveAt(newInstructions.Count - 1);
newInstructions.RemoveAt(newInstructions.Count - 1);
newInstructions.RemoveAt(newInstructions.Count - 1);
newInstructions.Add(new CodeInstruction(OpCodes.Call, (object)ReplacementMethod));
}
else
{
newInstructions.Add(instruction);
}
}
foreach (CodeInstruction item in newInstructions)
{
yield return item;
}
if (!found)
{
LostEnemyFix.Instance.mls.LogInfo((object)"Failed to patch EnemyAI!");
}
}
}
[HarmonyPatch(typeof(RoundManager))]
internal class RoundManagerPatch
{
[HarmonyPatch("ResetEnemyVariables")]
[HarmonyPrefix]
public static void patchResetEnemyVariables()
{
EnemyAIPatch.EnemyErrors.Clear();
LostEnemyFix.Instance.mls.LogInfo((object)"Clearing Enemy Error List");
}
}
}