Decompiled source of FreddyEnemy Refix v1.0.1

FreddyEnemy_Patch.dll

Decompiled 18 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using FreddyEnemy.AI;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FreddyEnemy_Patch")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("FreddyEnemy_Patch")]
[assembly: AssemblyTitle("FreddyEnemy_Patch")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 FreddyEnemy_Patch
{
	[BepInPlugin("erv.FreddyEnemy_Patch", "FreddyEnemy Patch", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private const float MaxAttackRange = 8f;

		private static readonly FieldRef<EnemyFreddy, bool> StateImpulse = AccessTools.FieldRefAccess<EnemyFreddy, bool>("_stateImpulse");

		private static readonly FieldRef<EnemyFreddy, float> StateTimer = AccessTools.FieldRefAccess<EnemyFreddy, float>("_stateTimer");

		private static readonly FieldRef<EnemyFreddy, Enemy> EnemyRef = AccessTools.FieldRefAccess<EnemyFreddy, Enemy>("_enemy");

		private static readonly FieldRef<EnemyFreddy, PhotonView> ViewRef = AccessTools.FieldRefAccess<EnemyFreddy, PhotonView>("_photonView");

		private static readonly FieldRef<EnemyFreddy, PlayerAvatar> TargetRef = AccessTools.FieldRefAccess<EnemyFreddy, PlayerAvatar>("_target");

		private static readonly FieldRef<EnemyFreddyAnim, bool> AnimStunned = AccessTools.FieldRefAccess<EnemyFreddyAnim, bool>("stunned");

		private static readonly FieldRef<EnemyFreddyAnim, bool> AnimHaywiring = AccessTools.FieldRefAccess<EnemyFreddyAnim, bool>("haywiring");

		private static readonly FieldRef<EnemyFreddyAnim, bool> AnimCloaked = AccessTools.FieldRefAccess<EnemyFreddyAnim, bool>("cloaked");

		private static readonly MethodInfo UpdateStateMI = AccessTools.Method(typeof(EnemyFreddy), "UpdateState", (Type[])null, (Type[])null);

		private static readonly MethodInfo FreddyZoomMI = AccessTools.Method(typeof(EnemyFreddy), "FreddyZoomRPC", (Type[])null, (Type[])null);

		private static readonly MethodInfo DisableMouthLightMI = AccessTools.Method(typeof(EnemyFreddy), "DisableMouthLightRPC", (Type[])null, (Type[])null);

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("erv.FreddyEnemy_Patch");
			val.PatchAll(typeof(Plugin));
			Log.LogInfo((object)"FreddyEnemy Patch 1.0.1 applied.");
		}

		[HarmonyPatch(typeof(EnemyFreddy), "Update")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> AttackUpdateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			MethodInfo methodInfo = AccessTools.Method(typeof(EnemyFreddy), "HandleAttack", (Type[])null, (Type[])null);
			MethodInfo operand = AccessTools.Method(typeof(Plugin), "CustomHandleAttack", (Type[])null, (Type[])null);
			int num = 0;
			for (int i = 0; i < list.Count; i++)
			{
				if ((list[i].opcode == OpCodes.Call || list[i].opcode == OpCodes.Callvirt) && list[i].operand is MethodInfo methodInfo2 && methodInfo2 == methodInfo)
				{
					list[i].opcode = OpCodes.Call;
					list[i].operand = operand;
					num++;
				}
			}
			if (num == 0)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogWarning((object)"AttackUpdateTranspiler: no se encontró la llamada a HandleAttack; el parche de ataque NO está activo.");
				}
			}
			return list;
		}

		public static void CustomHandleAttack(EnemyFreddy instance)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			Enemy val = EnemyRef.Invoke(instance);
			PhotonView val2 = ViewRef.Invoke(instance);
			PlayerAvatar val3 = TargetRef.Invoke(instance);
			if (StateImpulse.Invoke(instance))
			{
				StateImpulse.Invoke(instance) = false;
				StateTimer.Invoke(instance) = 2f;
				if (!((Object)(object)val3 != (Object)null) || !((Object)(object)val3.playerHealth != (Object)null) || val3.isDisabled || !(Vector3.Distance(val.CenterTransform.position, ((Component)val3).transform.position) <= 8f))
				{
					UpdateStateMI.Invoke(instance, new object[1] { (object)(State)7 });
					return;
				}
				instance.attackImpulse = true;
				val.StuckCount = 0;
				if (GameManager.Multiplayer() && (Object)(object)val2 != (Object)null)
				{
					val2.RPC("AttackImpulseRPC", (RpcTarget)1, Array.Empty<object>());
				}
				Vector3 position = ((Component)val3).transform.position;
				val3.playerHealth.HurtOther(50, position, false, SemiFunc.EnemyGetIndex(val), false);
				if (SemiFunc.IsMultiplayer() && (Object)(object)val2 != (Object)null)
				{
					val2.RPC("ActivateHurtColliderRPC", (RpcTarget)0, new object[1] { position });
					val2.RPC("FreddyZoomRPC", (RpcTarget)0, Array.Empty<object>());
				}
				else
				{
					instance.ActivateHurtColliderRPC(position);
					FreddyZoomMI.Invoke(instance, null);
				}
				val.NavMeshAgent.ResetPath();
				val.NavMeshAgent.Warp(((Component)val.Rigidbody).transform.position, false);
				return;
			}
			val.NavMeshAgent.Stop(0.2f);
			StateTimer.Invoke(instance) -= Time.deltaTime;
			if (StateTimer.Invoke(instance) <= 0f)
			{
				if (SemiFunc.IsMultiplayer() && (Object)(object)val2 != (Object)null)
				{
					val2.RPC("DisableMouthLightRPC", (RpcTarget)0, Array.Empty<object>());
				}
				else
				{
					DisableMouthLightMI.Invoke(instance, null);
				}
				UpdateStateMI.Invoke(instance, new object[1] { (object)(State)7 });
			}
		}

		[HarmonyPatch(typeof(EnemyFreddyAnim), "Update")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> AnimUpdateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			Dictionary<MethodBase, MethodInfo> dictionary = new Dictionary<MethodBase, MethodInfo>();
			dictionary.Add(AccessTools.Method(typeof(EnemyFreddyAnim), "SfxStunnedLoop", (Type[])null, (Type[])null), AccessTools.Method(typeof(Plugin), "CustomSfxStunnedLoop", (Type[])null, (Type[])null));
			dictionary.Add(AccessTools.Method(typeof(EnemyFreddyAnim), "SfxHaywireLoop", (Type[])null, (Type[])null), AccessTools.Method(typeof(Plugin), "CustomSfxHaywireLoop", (Type[])null, (Type[])null));
			dictionary.Add(AccessTools.Method(typeof(EnemyFreddyAnim), "SfxCloakedLoop", (Type[])null, (Type[])null), AccessTools.Method(typeof(Plugin), "CustomSfxCloakedLoop", (Type[])null, (Type[])null));
			Dictionary<MethodBase, MethodInfo> dictionary2 = dictionary;
			int num = 0;
			for (int i = 0; i < list.Count; i++)
			{
				if ((list[i].opcode == OpCodes.Call || list[i].opcode == OpCodes.Callvirt) && list[i].operand is MethodBase key && dictionary2.TryGetValue(key, out var value))
				{
					list[i].opcode = OpCodes.Call;
					list[i].operand = value;
					num++;
				}
			}
			if (num != 3)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogWarning((object)$"AnimUpdateTranspiler: se reemplazaron {num}/3 llamadas de loops de sonido.");
				}
			}
			return list;
		}

		public static void CustomSfxStunnedLoop(EnemyFreddyAnim instance)
		{
			Sound sfxStunnedLoop = instance.sfxStunnedLoop;
			if (sfxStunnedLoop != null)
			{
				sfxStunnedLoop.PlayLoop(AnimStunned.Invoke(instance), 5f, 5f, 1f, 1f);
			}
		}

		public static void CustomSfxHaywireLoop(EnemyFreddyAnim instance)
		{
			Sound sfxHaywireLoop = instance.sfxHaywireLoop;
			if (sfxHaywireLoop != null)
			{
				sfxHaywireLoop.PlayLoop(AnimHaywiring.Invoke(instance), 5f, 5f, 1f, 1f);
			}
		}

		public static void CustomSfxCloakedLoop(EnemyFreddyAnim instance)
		{
			Sound sfxCloakedLoop = instance.sfxCloakedLoop;
			if (sfxCloakedLoop != null)
			{
				sfxCloakedLoop.PlayLoop(AnimCloaked.Invoke(instance), 5f, 5f, 1f, 1f);
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}