Decompiled source of NestFix v1.1.0

NestFix.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("NestFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Fixes Nest Positions")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NestFix")]
[assembly: AssemblyTitle("NestFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NestFix
{
	[BepInPlugin("PureFPSZac.NestFix", "NestFix", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "PureFPSZac.NestFix";

		public const string NAME = "NestFix";

		public const string VERSION = "1.0.0";

		private static ManualLogSource NestLogger;

		private static readonly NavMeshPath TempPath = new NavMeshPath();

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			NestLogger = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("PureFPSZac.NestFix");
			val.PatchAll(typeof(Plugin));
		}

		private static Vector4 Vec4Position(Vector3 position)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0020: Unknown result type (might be due to invalid IL or missing references)
			return new Vector4(position.x, position.y, position.z, 1f);
		}

		private static bool NestSpawnPositionIsValid(EnemyType enemyType, Vector3 position, Quaternion rotation)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: 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_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Invalid comparison between Unknown and I4
			if (((Object)enemyType).name == "BaboonHawk")
			{
				Transform transform = enemyType.nestSpawnPrefab.transform;
				Transform val = transform.Find("WoodPikes");
				Matrix4x4 val2 = Matrix4x4.TRS(position, rotation, transform.localScale) * transform.worldToLocalMatrix;
				if ((Object)(object)val != (Object)null)
				{
					BoxCollider[] componentsInChildren = ((Component)val).GetComponentsInChildren<BoxCollider>();
					BoxCollider[] array = componentsInChildren;
					RaycastHit val7 = default(RaycastHit);
					NavMeshHit val8 = default(NavMeshHit);
					foreach (BoxCollider val3 in array)
					{
						Transform transform2 = ((Component)val3).transform;
						Vector4 val4 = Vec4Position(val3.center);
						Vector4 val5 = val2 * transform2.localToWorldMatrix * (val4 - new Vector4(0f, 0f, val3.size.z / 2f));
						Vector4 val6 = val2 * transform2.localToWorldMatrix * (val4 + new Vector4(0f, 0f, val3.size.z / 2f));
						if (!Physics.Linecast(Vector4.op_Implicit(val6), Vector4.op_Implicit(val5), ref val7, 256))
						{
							return false;
						}
						if (!NavMesh.SamplePosition(((RaycastHit)(ref val7)).point, ref val8, 0.5f, -1))
						{
							return false;
						}
						if (!NavMesh.CalculatePath(position, ((NavMeshHit)(ref val8)).position, -1, TempPath) || (int)TempPath.status > 0)
						{
							return false;
						}
					}
				}
			}
			return true;
		}

		private static void SpawnNest()
		{
			foreach (SpawnableEnemyWithRarity outsideEnemy in RoundManager.Instance.currentLevel.OutsideEnemies)
			{
				EnemyType enemyType = outsideEnemy.enemyType;
				if (((Object)enemyType).name == "BaboonHawk" && (Object)(object)enemyType.nestSpawnPrefab != (Object)null)
				{
					Random random = new Random();
					for (int i = 0; i < 100; i++)
					{
						RoundManager.Instance.SpawnNestObjectForOutsideEnemy(enemyType, random);
					}
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(RoundManager), "SpawnNestObjectForOutsideEnemy")]
		private static bool SpawnNestObjectForOutsideEnemyPrefix(RoundManager __instance, EnemyType enemyType, Random randomSeed)
		{
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: 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_0098: 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)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: 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)
			if (((Object)enemyType).name != "BaboonHawk")
			{
				return true;
			}
			GameObject[] array = GameObject.FindGameObjectsWithTag("OutsideAINode");
			int num = 32;
			Vector3? val = null;
			Quaternion? val2 = null;
			while (!val.HasValue && num > 0)
			{
				List<GameObject> list = new List<GameObject>(array);
				NestLogger.LogInfo((object)$"SpawnNestObjectForOutsideEnemyPrefix was called with {array.Length} nodes, {num} tries left");
				while (list.Count > 0)
				{
					int index = randomSeed.Next(0, list.Count);
					Vector3 val3 = list[index].transform.position;
					list.RemoveAt(index);
					val3 = __instance.GetRandomNavMeshPositionInBoxPredictable(val3, 15f, default(NavMeshHit), randomSeed, __instance.GetLayermaskForEnemySizeLimit(enemyType));
					val3 = __instance.PositionWithDenialPointsChecked(val3, array, enemyType);
					val3 = __instance.PositionEdgeCheck(val3, enemyType.nestSpawnPrefabWidth);
					Quaternion val4 = Quaternion.Euler(0f, (float)randomSeed.Next(-180, 180), 0f);
					if (!((Vector3)(ref val3)).Equals(Vector3.zero) && NestSpawnPositionIsValid(enemyType, val3, val4))
					{
						val = val3;
						val2 = val4;
						break;
					}
				}
				num--;
			}
			if (!val.HasValue)
			{
				NestLogger.LogWarning((object)("Failed to find a spawn position for the " + ((Object)enemyType).name + " nest."));
				return false;
			}
			GameObject val5 = Object.Instantiate<GameObject>(enemyType.nestSpawnPrefab, val.Value, Quaternion.Euler(Vector3.zero));
			val5.transform.localRotation = val2.Value * val5.transform.localRotation;
			if (!Object.op_Implicit((Object)(object)val5.gameObject.GetComponentInChildren<NetworkObject>()))
			{
				Debug.LogError((object)("Error: No NetworkObject found in enemy nest spawn prefab that was just spawned on the host: '" + ((Object)val5).name + "'"));
			}
			else
			{
				val5.gameObject.GetComponentInChildren<NetworkObject>().Spawn(true);
			}
			if (!Object.op_Implicit((Object)(object)val5.GetComponent<EnemyAINestSpawnObject>()))
			{
				Debug.LogError((object)("Error: No EnemyAINestSpawnObject component in nest object prefab that was just spawned on the host: '" + ((Object)val5).name + "'"));
			}
			else
			{
				__instance.enemyNestSpawnObjects.Add(val5.GetComponent<EnemyAINestSpawnObject>());
			}
			enemyType.nestsSpawned++;
			return false;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "NestFix";

		public const string PLUGIN_NAME = "NestFix";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}