Decompiled source of ShrekCracker v1.0.0

ShrekCracker.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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("ShrekCracker")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ShrekCracker")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("32b785a1-216e-4d7a-b17f-46a747a12fa7")]
[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 ShrekCracker
{
	[BepInPlugin("KadenBiel.ShrekCracker", "ShrekCracker", "1.0.0")]
	public class ShrekCrackerBase : BaseUnityPlugin
	{
		private const string modGUID = "KadenBiel.ShrekCracker";

		private const string modName = "ShrekCracker";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("KadenBiel.ShrekCracker");

		private static ShrekCrackerBase Instance;

		public static ManualLogSource mls;

		public static AssetBundle ShrekAssets;

		public static GameObject ShrekPrefab;

		public static AudioClip ChaseMusic;

		public static AudioClip Mad;

		public static AudioClip Search;

		public static string modDir;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			modDir = ((BaseUnityPlugin)this).Info.Location;
			mls = Logger.CreateLogSource("KadenBiel.ShrekCracker");
			LoadAssets();
			mls.LogInfo((object)"Shrek is awake");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}

		private static void LoadAssets()
		{
			try
			{
				ShrekAssets = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(modDir), "shrekcracker"));
			}
			catch (Exception ex)
			{
				mls.LogError((object)("AssetBundle failed to load -- " + ex.Message));
				return;
			}
			try
			{
				ShrekPrefab = ShrekAssets.LoadAsset<GameObject>("ShrekCracker.prefab");
				ChaseMusic = ShrekAssets.LoadAsset<AudioClip>("AllStarMad.ogg");
				Mad = ShrekAssets.LoadAsset<AudioClip>("WhatAreYouDoing.ogg");
				Search = ShrekAssets.LoadAsset<AudioClip>("AllStarPopUp.ogg");
				mls.LogInfo((object)"Shrek Assets Loaded");
			}
			catch (Exception ex2)
			{
				mls.LogError((object)("Failed to load assets -- " + ex2.Message));
			}
		}
	}
}
namespace ShrekCracker.Patchers
{
	[HarmonyPatch]
	internal class CrackerPatch
	{
		[HarmonyPatch(typeof(NutcrackerEnemyAI), "Update")]
		[HarmonyPostfix]
		private static void addController(NutcrackerEnemyAI __instance)
		{
			if ((Object)(object)((Component)__instance).gameObject.GetComponent<ShrekController>() == (Object)null)
			{
				ShrekCrackerBase.mls.LogInfo((object)"Instantiating Shrek");
				((Component)__instance).gameObject.AddComponent<ShrekController>();
			}
		}

		[HarmonyPatch(typeof(NutcrackerEnemyAI), "FireGun")]
		[HarmonyPostfix]
		private static void fire(NutcrackerEnemyAI __instance)
		{
			ShrekController component = ((Component)__instance).GetComponent<ShrekController>();
			component.Fire();
		}
	}
	internal class ShrekController : MonoBehaviour
	{
		private static GameObject Shrek;

		private static Animator Animator;

		private bool IsDead = false;

		private bool Search = false;

		private bool Chase = false;

		private NutcrackerEnemyAI CrackerAI { get; set; }

		private Vector3 prevPosition { get; set; }

		private void LoadAudio()
		{
			((EnemyAI)CrackerAI).creatureVoice.mute = true;
			ShrekCrackerBase.mls.LogInfo((object)"Audio Patched");
		}

		private void HideCrackerModel()
		{
			try
			{
				ShrekCrackerBase.mls.LogInfo((object)"Attempting to find nutcracker mesh");
				for (int i = 0; i < ((Component)CrackerAI).transform.childCount - 1; i++)
				{
					ShrekCrackerBase.mls.LogInfo((object)("Index: " + i + " Name: " + ((Object)((Component)CrackerAI).transform.GetChild(i)).name));
				}
				Renderer[] componentsInChildren = ((Component)((Component)CrackerAI).transform.Find("MeshContainer")).GetComponentsInChildren<Renderer>();
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					componentsInChildren[j].enabled = false;
				}
				ShrekCrackerBase.mls.LogInfo((object)"Nutcracker Hidden");
			}
			catch (Exception ex)
			{
				ShrekCrackerBase.mls.LogError((object)("Failed to find Nutcracker Model: " + ex.Message));
			}
		}

		private void EnableShrek()
		{
			try
			{
				MeshRenderer[] componentsInChildren = Shrek.GetComponentsInChildren<MeshRenderer>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					((Renderer)componentsInChildren[i]).enabled = true;
				}
				ShrekCrackerBase.mls.LogInfo((object)"Shrek Enabled");
			}
			catch (Exception ex)
			{
				ShrekCrackerBase.mls.LogError((object)("Failed to find Shrek Model: " + ex.Message));
			}
		}

		private void Start()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			ShrekCrackerBase.mls.LogInfo((object)"Shrek Started");
			CrackerAI = ((Component)this).GetComponent<NutcrackerEnemyAI>();
			Shrek = Object.Instantiate<GameObject>(ShrekCrackerBase.ShrekPrefab, ((Component)CrackerAI).transform.position, Quaternion.identity, ((Component)CrackerAI).transform);
			Animator = Shrek.GetComponent<Animator>();
			LoadAudio();
			HideCrackerModel();
			EnableShrek();
		}

		private void Update()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: 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)
			Animator.SetBool("isMoving", prevPosition != ((Component)this).transform.position);
			if (((EnemyAI)CrackerAI).isEnemyDead && !IsDead)
			{
				KillShrek();
			}
			prevPosition = ((Component)this).transform.position;
			if (((EnemyAI)CrackerAI).currentBehaviourStateIndex == 1 && (!CrackerAI.longRangeAudio.isPlaying || Chase))
			{
				Search = true;
				Chase = false;
				CrackerAI.longRangeAudio.Stop();
				CrackerAI.longRangeAudio.clip = ShrekCrackerBase.Search;
				CrackerAI.longRangeAudio.Play();
			}
			if (((EnemyAI)CrackerAI).currentBehaviourStateIndex == 2 && (!CrackerAI.longRangeAudio.isPlaying || Search))
			{
				Search = false;
				Chase = true;
				CrackerAI.longRangeAudio.Stop();
				CrackerAI.longRangeAudio.clip = ShrekCrackerBase.ChaseMusic;
				CrackerAI.longRangeAudio.Play();
			}
			if (((EnemyAI)CrackerAI).currentBehaviourStateIndex == 0 && CrackerAI.longRangeAudio.isPlaying)
			{
				Search = false;
				Chase = false;
				CrackerAI.longRangeAudio.Stop();
			}
		}

		private void KillShrek()
		{
			Animator.SetBool("IsDead", true);
			IsDead = true;
		}

		public void Fire()
		{
			Animator.ResetTrigger("isFiring");
			Animator.SetTrigger("isFiring");
		}
	}
}