Decompiled source of CoilHeadStare v1.0.9

CoilHeadStare.dll

Decompiled 9 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TDP.CoilHeadStare.Patch;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CoilHeadStare")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CoilHeadStare")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9d96f0c2-6393-4d02-99d7-34538a0dad5c")]
[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 TDP.CoilHeadStare
{
	[BepInPlugin("TDP.CoilHeadStare", "CoilHeadStare", "1.0.9")]
	public class ModBase : BaseUnityPlugin
	{
		private const string modGUID = "TDP.CoilHeadStare";

		private const string modName = "CoilHeadStare";

		private const string modVersion = "1.0.9";

		private Harmony harmony;

		internal static ModBase instance;

		internal ManualLogSource mls;

		public static ConfigEntry<float> config_timeUntilStare;

		public static ConfigEntry<float> config_maxStareDistance;

		public static ConfigEntry<float> config_turnHeadSpeed;

		public static ConfigEntry<bool> config_shovelReact;

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			else
			{
				Object.Destroy((Object)(object)this);
			}
			ConfigFile();
			harmony = new Harmony("TDP.CoilHeadStare");
			harmony.PatchAll(typeof(CoilHeadPatch));
			mls = Logger.CreateLogSource("TDP.CoilHeadStare");
			mls.LogInfo((object)"CoilHeadStare 1.0.9 loaded.");
		}

		private void ConfigFile()
		{
			config_timeUntilStare = ((BaseUnityPlugin)this).Config.Bind<float>("CoilHeadStare", "Time Until Stare", 8f, "Time between moving and looking at closest player (in seconds)");
			config_maxStareDistance = ((BaseUnityPlugin)this).Config.Bind<float>("CoilHeadStare", "Max Stare Distance", 6f, "Coilhead will only stare at players closer than this (for reference: coilhead's height is ca. 4)");
			config_turnHeadSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("CoilHeadStare", "Head Turn Speed", 0.3f, "The speed at which the head turns towards the player");
			config_shovelReact = ((BaseUnityPlugin)this).Config.Bind<bool>("CoilHeadStare", "Head Bounce on Shovel Hit", true, "Should the coilhead spring wobble when hit with a shovel (or with anything else)");
			Stare.timeUntilStare = config_timeUntilStare.Value;
			Stare.maxStareDistance = config_maxStareDistance.Value;
			Stare.turnHeadSpeed = config_turnHeadSpeed.Value;
			CoilHeadPatch.shovelReact = config_shovelReact.Value;
		}
	}
}
namespace TDP.CoilHeadStare.Patch
{
	internal class CoilHeadPatch
	{
		public static bool shovelReact;

		[HarmonyPatch(typeof(EnemyAI), "Start")]
		[HarmonyPostfix]
		[HarmonyPriority(200)]
		private static void StartPostFix(EnemyAI __instance)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			if (__instance is SpringManAI)
			{
				Debug.Log((object)"CoilHead found. Adding stare script.");
				Stare stare = ((Component)__instance).gameObject.AddComponent<Stare>();
				stare.Initialize((SpringManAI)__instance);
			}
		}

		[HarmonyPatch(typeof(EnemyAI), "HitEnemy")]
		[HarmonyPrefix]
		public static void HitEnemyPreFix(EnemyAI __instance)
		{
			if (__instance is SpringManAI)
			{
				if ((Object)(object)__instance == (Object)null)
				{
					ModBase.instance.mls.LogError((object)"On player hit Coil Head: Coil Head instance is missing.");
				}
				else if ((Object)(object)__instance.creatureAnimator == (Object)null)
				{
					ModBase.instance.mls.LogError((object)"On player hit Coil Head: Coil Head animator is missing.");
				}
				else if (__instance.creatureAnimator.parameters.All((AnimatorControllerParameter x) => x.name == "springBoing"))
				{
					ModBase.instance.mls.LogError((object)"On player hit Coil Head: Coil Head animatorcontroller is missing the parameter \"springBoing\".");
				}
				else if (shovelReact)
				{
					__instance.creatureAnimator.SetTrigger("springBoing");
				}
			}
		}
	}
	public class Stare : MonoBehaviour
	{
		private SpringManAI coilHeadInstance;

		public static float timeUntilStare;

		public static float maxStareDistance;

		public static float turnHeadSpeed;

		private Transform head;

		private float timeSinceMoving;

		private bool isTurningHead;

		private PlayerControllerB stareTarget;

		public void Initialize(SpringManAI instance)
		{
			Debug.Log((object)"Initializing Stare on Coil Head.");
			coilHeadInstance = instance;
			if ((Object)(object)coilHeadInstance == (Object)null)
			{
				ModBase.instance.mls.LogError((object)"Coil Head instance missing. Harmony Patch failed?");
			}
			List<Transform> list = FindChildren(((Component)coilHeadInstance).transform, "springBone.002");
			if (list == null)
			{
				ModBase.instance.mls.LogError((object)"Search for head parent returned null. This should not be possible, something went wrong.");
			}
			else if (list.Count > 0)
			{
				if (list.Last().childCount > 0)
				{
					head = list.Last().GetChild(list.Last().childCount - 1);
				}
				else
				{
					ModBase.instance.mls.LogError((object)"Found head parent (springBone.002), but it has no child transforms.");
				}
			}
			else
			{
				ModBase.instance.mls.LogError((object)"Could not find head parent bone. Possibly missing or renamed? Head must be parented to \"springBone.002\".");
			}
			if ((Object)(object)head == (Object)null)
			{
				ModBase.instance.mls.LogError((object)"Could not find head transform. Destroying script. (Coil Head should be unaffected by this)");
				Object.Destroy((Object)(object)this);
			}
			else
			{
				ModBase.instance.mls.LogInfo((object)("Found head transform: " + ((Object)head).name));
			}
		}

		private List<Transform> FindChildren(Transform parent, string name)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			List<Transform> list = new List<Transform>();
			foreach (Transform item in parent)
			{
				Transform val = item;
				if (((Object)val).name == name)
				{
					list.Add(val);
				}
				else
				{
					list.AddRange(FindChildren(val, name));
				}
			}
			return list;
		}

		private void Start()
		{
			if ((Object)(object)head != (Object)null)
			{
				ModBase.instance.mls.LogInfo((object)"Stare initialization successful. \nNote: If a reskin is being used and the head does not turn it is most likely incompatible.");
			}
			else
			{
				ModBase.instance.mls.LogInfo((object)"Stare initialization unsuccessful. An error has probably occurred.");
			}
		}

		private void Update()
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_00c6: 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_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)coilHeadInstance == (Object)null)
			{
				return;
			}
			if ((Object)(object)head == (Object)null)
			{
				ModBase.instance.mls.LogError((object)"Head transform missing. Destroying script.");
				Object.Destroy((Object)(object)this);
				return;
			}
			if (!isTurningHead)
			{
				stareTarget = ((EnemyAI)coilHeadInstance).GetClosestPlayer(false, false, false);
			}
			if (!((Object)(object)stareTarget == (Object)null))
			{
				Vector3 val = ((Component)stareTarget.gameplayCamera).transform.position - head.position;
				float num = Vector3.Distance(head.position, ((Component)((EnemyAI)coilHeadInstance).GetClosestPlayer(false, false, false)).transform.position);
				if ((double)((EnemyAI)coilHeadInstance).creatureAnimator.GetFloat("walkSpeed") > 0.01 || num > maxStareDistance || Vector3.Angle(head.forward, val) < 5f)
				{
					timeSinceMoving = 0f;
				}
				else
				{
					timeSinceMoving += Time.deltaTime;
				}
				if (timeSinceMoving > timeUntilStare)
				{
					isTurningHead = true;
				}
				else
				{
					isTurningHead = false;
				}
				if (isTurningHead)
				{
					head.forward = Vector3.RotateTowards(head.forward, val, turnHeadSpeed * Time.deltaTime, 0f);
				}
			}
		}
	}
}