Decompiled source of LostEnemyFix v1.0.0

BepInEx/plugins/LostEnemyFix/LostEnemyFix.dll

Decompiled a day ago
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 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.0.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.0.0";

		private readonly Harmony harmony = new Harmony("JacobG5.LostEnemyFix");

		internal ManualLogSource mls;

		public static LostEnemyFix Instance;

		public static ConfigEntry<KillLostEnemies> killLostEnemiesMode;

		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 16 NavMesh errors.\nOff: Vanilla behavior");
			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_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			bool flag = enemy.agent.SetDestination(enemy.destination);
			if (!flag)
			{
				bool flag2 = false;
				switch (LostEnemyFix.killLostEnemiesMode.Value)
				{
				case LostEnemyFix.KillLostEnemies.Reasonable:
					if (EnemyErrors.ContainsKey(enemy))
					{
						EnemyErrors[enemy]++;
						if (EnemyErrors[enemy] > 16)
						{
							flag2 = true;
						}
					}
					else
					{
						EnemyErrors.Add(enemy, 1);
					}
					break;
				case LostEnemyFix.KillLostEnemies.Aggressive:
					flag2 = true;
					break;
				}
				LostEnemyFix.Instance.mls.LogWarning((object)((((Object)(object)enemy.enemyType == (Object)null) ? ((Object)enemy).name : enemy.enemyType.enemyName) + " Failed to locate NavMesh. " + (flag2 ? "Killing enemy to prevent further errors.\n" : "\n") + $"Enemy Position: {((Component)enemy).transform.position} Target Destination: {enemy.destination} IsOutside: {enemy.isOutside} State: {enemy.currentBehaviourState.name}"));
				if (flag2)
				{
					enemy.KillEnemyServerRpc(true);
				}
			}
			return flag;
		}

		[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");
		}
	}
}