Decompiled source of BaldiEnemy v1.2.0

BaldiEnemy.dll

Decompiled 6 days ago
using System;
using System.Collections;
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 BaldiEnemy.Configuration;
using BaldiEnemy.Patches;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using Unity.Netcode.Components;
using UnityEngine;
using com.riskivr.BaldiEnemy.NetcodePatcher;

[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("com.riskivr.BaldiEnemy")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+20200776ad83176223a072ea8bdca2b238e497f7")]
[assembly: AssemblyProduct("BaldiEnemy")]
[assembly: AssemblyTitle("com.riskivr.BaldiEnemy")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
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 BaldiEnemy
{
	public class BaldiEnemy : EnemyAI
	{
		private enum States
		{
			Roam,
			Active
		}

		[SerializeField]
		private AudioClip slap;

		[SerializeField]
		private AudioClip buzz;

		[SerializeField]
		private NetworkAnimator networkAnimator;

		[SerializeField]
		private GameObject billboard;

		[SerializeField]
		private GameObject pivot;

		private float moveTimer;

		private float moveTime = 2.5f;

		public override void Start()
		{
			((EnemyAI)this).Start();
			base.agent.speed = 0f;
			moveTimer = 0f;
			BaldiHearingManager.RegisterSpawnedBaldi(this);
			WanderToRandomNode();
		}

		public override void Update()
		{
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: 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_010f: 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_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: 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_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			((EnemyAI)this).Update();
			if ((Object)(object)base.targetPlayer != (Object)null && base.targetPlayer.isPlayerDead)
			{
				base.targetPlayer = null;
			}
			moveTimer += Time.deltaTime;
			if (moveTimer >= moveTime)
			{
				((MonoBehaviour)this).StartCoroutine(DoMovement());
				base.creatureSFX.PlayOneShot(slap);
				base.creatureAnimator.SetTrigger("slap");
				moveTimer = 0f;
				float num = (float)RoundManager.Instance.valueOfFoundScrapItems / RoundManager.Instance.totalScrapValueInLevel;
				moveTime = Mathf.Lerp(2.5f, 0.75f, num);
				if (RoundManager.Instance.powerOffPermanently)
				{
					moveTime *= 0.75f;
				}
			}
			Vector3 position = ((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).transform.position;
			Vector3 val = ((Component)this).transform.position - position;
			val.y = 0f;
			Quaternion val2 = Quaternion.LookRotation(val);
			Transform transform = pivot.transform;
			Quaternion rotation = ((Component)this).transform.rotation;
			float x = ((Quaternion)(ref rotation)).eulerAngles.x;
			float y = ((Quaternion)(ref val2)).eulerAngles.y;
			rotation = ((Component)this).transform.rotation;
			transform.rotation = Quaternion.Euler(x, y, ((Quaternion)(ref rotation)).eulerAngles.z);
		}

		private IEnumerator DoMovement()
		{
			base.agent.speed = 100f;
			yield return (object)new WaitForSeconds(0.1f);
			base.agent.speed = 0f;
		}

		public override void DoAIInterval()
		{
			((EnemyAI)this).DoAIInterval();
			switch (base.currentBehaviourStateIndex)
			{
			case 0:
				Roam();
				break;
			case 1:
				Active();
				break;
			}
		}

		public void Roam()
		{
			if (UpdateTargetSelection())
			{
				((EnemyAI)this).SwitchToBehaviourClientRpc(1);
			}
			else if (base.agent.remainingDistance < 1f)
			{
				WanderToRandomNode();
			}
		}

		protected bool WanderToRandomNode()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			GameObject[] array = (base.isOutside ? RoundManager.Instance.outsideAINodes : RoundManager.Instance.insideAINodes);
			if (array == null)
			{
				Plugin.Logger.LogError((object)"Monster was unable to choose a node");
				return false;
			}
			int num = new Random().Next(0, array.Length);
			Plugin.Logger.LogMessage((object)$"Monster has chosen node {num} to wander to");
			((EnemyAI)this).SetDestinationToPosition(array[num].transform.position, false);
			return true;
		}

		protected bool UpdateTargetSelection()
		{
			//IL_0028: 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)
			PlayerControllerB closestPlayer = ((EnemyAI)this).GetClosestPlayer(false, false, false);
			if ((Object)(object)closestPlayer == (Object)null)
			{
				base.targetPlayer = null;
				return false;
			}
			if (Vector3.Distance(((Component)this).transform.position, ((Component)closestPlayer).transform.position) < 50f)
			{
				base.targetPlayer = closestPlayer;
				return true;
			}
			base.targetPlayer = null;
			return false;
		}

		public void HearDoorStateChange(Vector3 DoorPosition)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			if (base.currentBehaviourStateIndex == 0)
			{
				((EnemyAI)this).SetDestinationToPosition(DoorPosition, false);
			}
		}

		public void Active()
		{
			//IL_002f: 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)
			if (UpdateTargetSelection())
			{
				((EnemyAI)this).SetDestinationToPosition(((Component)base.targetPlayer).transform.position, false);
				return;
			}
			((EnemyAI)this).SetDestinationToPosition(((Component)this).transform.position, false);
			((EnemyAI)this).SwitchToBehaviourClientRpc(0);
		}

		public override void OnCollideWithPlayer(Collider other)
		{
			//IL_002d: 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)
			((EnemyAI)this).OnCollideWithPlayer(other);
			PlayerControllerB val = ((EnemyAI)this).MeetsStandardPlayerCollisionConditions(other, false, false);
			if (!((Object)(object)val == (Object)null))
			{
				val.DamagePlayer(999, true, true, (CauseOfDeath)0, 0, false, default(Vector3));
				base.creatureVoice.PlayOneShot(buzz);
			}
		}

		public override void EnableEnemyMesh(bool enable, bool overrideDoNotSet = false)
		{
			int layer = (enable ? 19 : 23);
			if (!billboard.CompareTag("DoNotSet") || overrideDoNotSet)
			{
				billboard.layer = layer;
			}
		}

		protected override void __initializeVariables()
		{
			((EnemyAI)this).__initializeVariables();
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		protected internal override string __getTypeName()
		{
			return "BaldiEnemy";
		}
	}
	internal static class BaldiHearingManager
	{
		private static BaldiEnemy? Baldi;

		internal static void AlertBaldiToDoorSound(Vector3 position)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			Baldi?.HearDoorStateChange(position);
		}

		internal static void RegisterSpawnedBaldi(BaldiEnemy IncomingBaldi)
		{
			if (Baldi == null)
			{
				Baldi = IncomingBaldi;
			}
		}
	}
	[BepInPlugin("com.riskivr.BaldiEnemy", "BaldiEnemy", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		public static AssetBundle? ModAssets;

		internal static PluginConfig BoundConfig { get; private set; }

		private void Awake()
		{
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			BoundConfig = new PluginConfig(((BaseUnityPlugin)this).Config);
			InitializeNetworkBehaviours();
			string path = "baldiassets";
			ModAssets = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), path));
			if ((Object)(object)ModAssets == (Object)null)
			{
				Logger.LogError((object)"Failed to load custom assets.");
				return;
			}
			EnemyType val = ModAssets.LoadAsset<EnemyType>("Baldi");
			TerminalNode val2 = ModAssets.LoadAsset<TerminalNode>("BaldiEnemyTN");
			TerminalKeyword val3 = ModAssets.LoadAsset<TerminalKeyword>("BaldiEnemyTK");
			NetworkPrefabs.RegisterNetworkPrefab(val.enemyPrefab);
			Enemies.RegisterEnemy(val, BoundConfig.SpawnWeight.Value, (LevelTypes)(-1), val2, val3);
			Harmony val4 = new Harmony("com.riskivr.BaldiEnemy");
			val4.PatchAll(typeof(DoorPatch));
			Logger.LogInfo((object)"Plugin BaldiEnemy is awake :3");
		}

		private static void InitializeNetworkBehaviours()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.riskivr.BaldiEnemy";

		public const string PLUGIN_NAME = "BaldiEnemy";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BaldiEnemy.Patches
{
	public class DoorPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(DoorLock), "OpenOrCloseDoor")]
		public static void DoorStatePatch(DoorLock __instance)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			BaldiHearingManager.AlertBaldiToDoorSound(((Component)__instance).transform.position);
		}
	}
}
namespace BaldiEnemy.Configuration
{
	public class PluginConfig
	{
		public ConfigEntry<int> SpawnWeight;

		public PluginConfig(ConfigFile cfg)
		{
			SpawnWeight = cfg.Bind<int>("General", "Spawn weight", 20, "The spawn chance weight for BaldiEnemy, relative to other existing enemies.\nGoes up from 0, lower is more rare, 100 and up is very common.");
			ClearUnusedEntries(cfg);
		}

		private void ClearUnusedEntries(ConfigFile cfg)
		{
			PropertyInfo property = ((object)cfg).GetType().GetProperty("OrphanedEntries", BindingFlags.Instance | BindingFlags.NonPublic);
			Dictionary<ConfigDefinition, string> dictionary = (Dictionary<ConfigDefinition, string>)property.GetValue(cfg, null);
			dictionary.Clear();
			cfg.Save();
		}
	}
}
namespace com.riskivr.BaldiEnemy.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}