Decompiled source of SpiderPositionFix v1.1.2

DLLs/fandovec03.SpiderPositionFix.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SpiderPositionFix.Patches;
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("fandovec03.SpiderPositionFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+5e840961d63670c118b8b451d5a567bd769a6a6b")]
[assembly: AssemblyProduct("SpiderPositionFix")]
[assembly: AssemblyTitle("fandovec03.SpiderPositionFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.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.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;
		}
	}
	[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 SpiderPositionFix
{
	[BepInPlugin("fandovec03.SpiderPositionFix", "SpiderPositionFix", "1.1.1")]
	public class InitialScript : BaseUnityPlugin
	{
		internal static bool isStarlancePresent = Chainloader.PluginInfos.ContainsKey("AudioKnight-StarlancerEnemyEscape");

		public static AssetBundle SpiderAssets;

		public static InitialScript Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		internal static ConfigClass configSettings { get; set; } = null;


		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			configSettings = new ConfigClass(((BaseUnityPlugin)this).Config);
			Patch();
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			SpiderAssets = AssetBundle.LoadFromFile(Path.Combine(directoryName, "spideranimationfixbundle"));
			if ((Object)(object)SpiderAssets == (Object)null)
			{
				Logger.LogError((object)"Failed to load Assets");
			}
			Logger.LogInfo((object)"fandovec03.SpiderPositionFix v1.1.1 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: 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_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("fandovec03.SpiderPositionFix");
			}
			if (isStarlancePresent)
			{
				Logger.LogInfo((object)"Found StarlanceEnemyEscape in Chainloader");
			}
			Logger.LogDebug((object)"Patching spider position fix...");
			Harmony.PatchAll(typeof(SpiderPositionPatch));
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching spider position fix...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	internal class ConfigClass
	{
		public readonly ConfigEntry<bool> applyMask;

		public readonly ConfigEntry<bool> debug;

		public ConfigClass(ConfigFile cfg)
		{
			cfg.SaveOnConfigSet = false;
			applyMask = cfg.Bind<bool>("Settings", "Apply changes to agent areaMask", true, "Apply the changes made to the spider agent areaMask. This will affect the pathfinding over offMeshLinks");
			debug = cfg.Bind<bool>("Debug", "Debug logs", false, "Enable debug logs");
			ClearOrphanedEntries(cfg);
			cfg.Save();
			cfg.SaveOnConfigSet = true;
		}

		public void ClearOrphanedEntries(ConfigFile cfg)
		{
			PropertyInfo propertyInfo = AccessTools.Property(typeof(ConfigFile), "OrphanedEntries");
			Dictionary<ConfigDefinition, string> dictionary = (Dictionary<ConfigDefinition, string>)propertyInfo.GetValue(cfg);
			dictionary.Clear();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "fandovec03.SpiderPositionFix";

		public const string PLUGIN_NAME = "SpiderPositionFix";

		public const string PLUGIN_VERSION = "1.1.1";
	}
}
namespace SpiderPositionFix.Patches
{
	internal class spiderPositionData
	{
		public int currentJumpMaskBit = 1;

		public float returningFromWallState = 0f;

		public bool startPatch = false;

		public bool applySpeedSlowdown = false;

		public float originalSpeed = 4.25f;

		public float offsetSpeed = 0f;
	}
	[HarmonyPatch(typeof(SandSpiderAI))]
	public class SpiderPositionPatch
	{
		private static bool debug = InitialScript.configSettings.debug.Value;

		private static Dictionary<SandSpiderAI, spiderPositionData> spiderData = new Dictionary<SandSpiderAI, spiderPositionData>();

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostfix(SandSpiderAI __instance)
		{
			if ((Object)(object)InitialScript.SpiderAssets != (Object)null)
			{
				try
				{
					AnimatorOverrideController runtimeAnimatorController = InitialScript.SpiderAssets.LoadAsset<AnimatorOverrideController>("Assets/LethalCompany/CustomAnims/SandSpider/Spider Anim Override.overrideController");
					((EnemyAI)__instance).creatureAnimator.runtimeAnimatorController = (RuntimeAnimatorController)(object)runtimeAnimatorController;
				}
				catch
				{
					InitialScript.Logger.LogError((object)"Failed to load OverrideController asset");
				}
			}
			if (!spiderData.ContainsKey(__instance))
			{
				spiderData.Add(__instance, new spiderPositionData());
			}
			spiderData[__instance].startPatch = true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostfix(SandSpiderAI __instance)
		{
			//IL_0105: 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_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0352: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_035f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0365: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = spiderData[__instance];
			if (InitialScript.configSettings.applyMask.Value)
			{
				if (((EnemyAI)__instance).isOutside && spiderPositionData2.currentJumpMaskBit != 1)
				{
					ChangeJumpMask(__instance, ref spiderPositionData2.currentJumpMaskBit);
					spiderPositionData2.currentJumpMaskBit = 1;
				}
				else if (!((EnemyAI)__instance).isOutside && spiderPositionData2.currentJumpMaskBit != 0)
				{
					ChangeJumpMask(__instance, ref spiderPositionData2.currentJumpMaskBit);
					spiderPositionData2.currentJumpMaskBit = 0;
				}
			}
			if (__instance.watchFromDistance)
			{
				if (debug)
				{
					InitialScript.Logger.LogDebug((object)"watchFromDistance true. Returning...");
				}
				return;
			}
			if (!spiderPositionData2.applySpeedSlowdown)
			{
				if (((EnemyAI)__instance).currentBehaviourStateIndex == 1)
				{
					spiderPositionData2.originalSpeed = __instance.spiderSpeed;
				}
				else
				{
					spiderPositionData2.originalSpeed = ((EnemyAI)__instance).agent.speed;
				}
			}
			if (!__instance.onWall)
			{
				if (Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position) > 0.4f && !((EnemyAI)__instance).agent.isOnOffMeshLink)
				{
					if (spiderPositionData2.applySpeedSlowdown)
					{
						spiderPositionData2.offsetSpeed = Mathf.Clamp(Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position), 0f, 2f) / 2f;
						((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed - Mathf.Min(1f, spiderPositionData2.offsetSpeed) - 0.15f;
					}
					else
					{
						spiderPositionData2.applySpeedSlowdown = true;
						if (((EnemyAI)__instance).currentBehaviourStateIndex == 1)
						{
							spiderPositionData2.originalSpeed = __instance.spiderSpeed;
						}
						else
						{
							((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed;
						}
					}
					if (debug && spiderPositionData2.originalSpeed != 0f)
					{
						InitialScript.Logger.LogDebug((object)("Applying slowdown. New speed: " + ((EnemyAI)__instance).agent.speed));
					}
				}
				else if (spiderPositionData2.applySpeedSlowdown && !((EnemyAI)__instance).agent.isOnOffMeshLink)
				{
					spiderPositionData2.applySpeedSlowdown = false;
					spiderPositionData2.offsetSpeed = 0f;
					((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed;
					if (debug)
					{
						InitialScript.Logger.LogDebug((object)"Returning original speed");
					}
				}
				if (((EnemyAI)__instance).agent.isOnOffMeshLink)
				{
					spiderPositionData2.applySpeedSlowdown = true;
					((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed / 1.15f;
					if (debug)
					{
						InitialScript.Logger.LogDebug((object)"On offMeshLink. Cutting speed");
					}
				}
				return;
			}
			if (spiderPositionData2.applySpeedSlowdown && __instance.onWall && !__instance.reachedWallPosition)
			{
				spiderPositionData2.applySpeedSlowdown = false;
				spiderPositionData2.offsetSpeed = 0f;
				((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed;
				if (debug)
				{
					InitialScript.Logger.LogDebug((object)"/2/ Returning original speed");
				}
			}
			if (__instance.onWall && (__instance.meshContainerTarget == __instance.floorPosition || __instance.meshContainerTarget == __instance.wallPosition))
			{
				((EnemyAI)__instance).agent.speed = 0f;
			}
		}

		[HarmonyPatch("LateUpdate")]
		[HarmonyPostfix]
		private static void MeshContainerPositionFix(SandSpiderAI __instance)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: 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_02f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: 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_00c4: 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_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: 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_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = spiderData[__instance];
			if (!spiderPositionData2.startPatch)
			{
				return;
			}
			if (!__instance.lookingForWallPosition && !__instance.gotWallPositionInLOS && !__instance.onWall)
			{
				if (Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position) > 0.8f || Mathf.Abs(__instance.meshContainerPosition.y - ((Component)__instance).transform.position.y) > 0.25f)
				{
					string text = "null";
					if (Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position) > 0.8f)
					{
						text = "Triggered by distance: " + Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position);
					}
					else if (Mathf.Abs(__instance.meshContainerPosition.y - ((Component)__instance).transform.position.y) > 0.25f)
					{
						text = "Triggered by height projection: " + Mathf.Abs(__instance.meshContainerPosition.y - ((Component)__instance).transform.position.y);
					}
					if (!__instance.onWall && !__instance.overrideSpiderLookRotation)
					{
						__instance.meshContainerTargetRotation = ((Component)((EnemyAI)__instance).agent).transform.rotation;
					}
					if (debug)
					{
						InitialScript.Logger.LogDebug((object)text);
					}
					__instance.meshContainerTarget = ((Component)((EnemyAI)__instance).agent).transform.position;
				}
				if (((EnemyAI)__instance).agent.isOnOffMeshLink)
				{
					__instance.meshContainer.position = Vector3.Lerp(__instance.meshContainerPosition, ((EnemyAI)__instance).agent.nextPosition, Distance(Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position), 0.5f));
					__instance.meshContainerPosition = __instance.meshContainer.position;
					Quaternion rotation = __instance.meshContainer.rotation;
					OffMeshLinkData currentOffMeshLinkData = ((EnemyAI)__instance).agent.currentOffMeshLinkData;
					__instance.meshContainerTargetRotation = Quaternion.Lerp(rotation, Quaternion.LookRotation(((OffMeshLinkData)(ref currentOffMeshLinkData)).endPos - __instance.meshContainer.position, Vector3.up), 0.75f);
				}
				else if (Mathf.Abs(__instance.meshContainerPosition.y - ((Component)__instance).transform.position.y) > 0.25f)
				{
					__instance.meshContainerTargetRotation = Quaternion.LookRotation(((Component)((EnemyAI)__instance).agent).transform.position - __instance.meshContainer.position, Vector3.up);
				}
			}
			if (!__instance.lookingForWallPosition && __instance.onWall && ((EnemyAI)__instance).movingTowardsTargetPlayer && ((__instance.onWall && Vector3.Distance(__instance.meshContainerPosition, ((Component)__instance).transform.position) < 1f) || spiderPositionData2.returningFromWallState > 6f))
			{
				spiderPositionData2.returningFromWallState = 0f;
			}
		}

		[HarmonyPatch("CalculateMeshMovement")]
		[HarmonyPrefix]
		private static bool MeshMovementPatch(SandSpiderAI __instance)
		{
			//IL_002a: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.lookingForWallPosition && __instance.gotWallPositionInLOS && !__instance.onWall)
			{
				((EnemyAI)__instance).SetDestinationToPosition(__instance.floorPosition, false);
				__instance.CalculateSpiderPathToPosition();
				__instance.navigateToPositionTarget = ((Component)__instance).transform.position + Vector3.Normalize(((EnemyAI)__instance).agent.desiredVelocity) * 2f;
				if (Vector3.Distance(((Component)__instance.meshContainer).transform.position, __instance.floorPosition) < 0.7f)
				{
					__instance.onWall = true;
					return true;
				}
				return false;
			}
			return true;
		}

		private static float Distance(float distance, float time)
		{
			return distance / time;
		}

		private static void ChangeJumpMask(SandSpiderAI __instance, ref int bit)
		{
			if ((Object)(object)__instance != (Object)null)
			{
				NavMeshAgent agent = ((EnemyAI)__instance).agent;
				agent.areaMask ^= 1 << NavMesh.GetAreaFromName("Jump");
				if (bit == 0)
				{
					bit = 1;
				}
				else if (bit == 1)
				{
					bit = 0;
				}
				if (debug)
				{
					InitialScript.Logger.LogDebug((object)("Spider: Toggled mask bit to " + bit));
				}
			}
		}
	}
}