Decompiled source of DeadBodyOptimization v1.3.0

Zose_DeadBodyOptimization.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Enemies;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppSystem.Collections;
using Microsoft.CodeAnalysis;
using SNetwork;
using StateMachines;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
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;
		}
	}
}
namespace Zose_deadBodyOptimization
{
	public static class AgentPatches
	{
		public static void OnSetupPrefix(EnemyAgent __instance)
		{
			if (!((Object)(object)__instance == (Object)null) && !((Il2CppObjectBase)__instance).WasCollected && !((Object)(object)((Component)__instance).gameObject == (Object)null))
			{
				CleanupStatus component = ((Component)__instance).gameObject.GetComponent<CleanupStatus>();
				if ((Object)(object)component != (Object)null)
				{
					component.IsProcessed = false;
					component.HasAppearanceTriggered = false;
					component.HasVisualsTriggered = false;
					component.HasApplyDecayTriggered = false;
					component.HasBeenBanished = false;
				}
				Decay componentInChildren = ((Component)__instance).GetComponentInChildren<Decay>(true);
				if ((Object)(object)componentInChildren != (Object)null)
				{
					((Behaviour)componentInChildren).enabled = true;
				}
				Animator componentInChildren2 = ((Component)__instance).GetComponentInChildren<Animator>(true);
				if ((Object)(object)componentInChildren2 != (Object)null)
				{
					((Behaviour)componentInChildren2).enabled = true;
				}
			}
		}

		public static bool ReceiveStateDataPrefix(Component __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected)
			{
				return true;
			}
			EnemyAgent componentInParent = __instance.GetComponentInParent<EnemyAgent>();
			if ((Object)(object)componentInParent == (Object)null || ((Il2CppObjectBase)componentInParent).WasCollected || (Object)(object)((Component)componentInParent).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus component = ((Component)componentInParent).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.HasApplyDecayTriggered)
			{
				return false;
			}
			return true;
		}

		public static bool ChangeStatePrefix(Component __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected)
			{
				return true;
			}
			EnemyAgent componentInParent = __instance.GetComponentInParent<EnemyAgent>();
			if ((Object)(object)componentInParent == (Object)null || ((Il2CppObjectBase)componentInParent).WasCollected || (Object)(object)((Component)componentInParent).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus component = ((Component)componentInParent).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.HasApplyDecayTriggered)
			{
				return false;
			}
			return true;
		}
	}
	public static class DamagePatches
	{
		public static bool NetworkPrefix(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected || (Object)(object)((Component)__instance.Owner).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus component = ((Component)__instance.Owner).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.HasApplyDecayTriggered)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Decay))]
	public static class DecayPatches
	{
		[HarmonyPatch("Play", new Type[] { typeof(float) })]
		[HarmonyPrefix]
		public static bool OnPlayPrefix(Decay __instance, ref float duration)
		{
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance != (Object)null && !((Il2CppObjectBase)__instance).WasCollected && (Object)(object)((Component)__instance).gameObject != (Object)null)
			{
				float bodyCleanupDelay = Settings.GetBodyCleanupDelay();
				float num = bodyCleanupDelay + 0.00314f;
				float num2 = 0.00414f;
				if (Math.Abs(duration - num) > 0.0001f && Math.Abs(duration - num2) > 0.0001f)
				{
					try
					{
						PropertyInfo propertyInfo = typeof(Decay).GetProperty("targetRenderer") ?? typeof(Decay).GetProperty("m_targetRenderer");
						if (propertyInfo != null)
						{
							object? value = propertyInfo.GetValue(__instance);
							Renderer val = (Renderer)((value is Renderer) ? value : null);
							if ((Object)(object)val != (Object)null)
							{
								EnemyAgent componentInParent = ((Component)val).GetComponentInParent<EnemyAgent>();
								if ((Object)(object)componentInParent == (Object)null)
								{
									GameObject gameObject = ((Component)((Component)val).transform.root).gameObject;
									if (((Object)gameObject).name.Contains("(Clone)"))
									{
										gameObject.transform.position = new Vector3(0f, -10000f, 0f);
									}
								}
							}
						}
					}
					catch
					{
					}
					return false;
				}
				if (Math.Abs(duration - num) <= 0.0001f)
				{
					duration = bodyCleanupDelay;
				}
				else if (Math.Abs(duration - num2) <= 0.0001f)
				{
					duration = 0.001f;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ES_Dead))]
	public static class ES_DeadPatches
	{
		[HarmonyPatch("CommonEnter")]
		[HarmonyPrefix]
		public static bool OnCommonEnterPrefix(ES_Dead __instance)
		{
			if (__instance == null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)((ES_Base)__instance).m_enemyAgent == (Object)null || ((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent).WasCollected || (Object)(object)((Component)((ES_Base)__instance).m_enemyAgent).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus cleanupStatus = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)cleanupStatus == (Object)null)
			{
				cleanupStatus = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.AddComponent<CleanupStatus>();
				CleanupStatus.AllStatuses.Add(cleanupStatus);
			}
			cleanupStatus.IsProcessed = true;
			cleanupStatus.ProcessTime = Time.time;
			return true;
		}

		[HarmonyPatch("CommonEnter")]
		[HarmonyPostfix]
		public static void OnCommonEnterPostfix(ES_Dead __instance)
		{
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Expected O, but got Unknown
			if (__instance == null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)((ES_Base)__instance).m_enemyAgent == (Object)null || ((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent).WasCollected || (Object)(object)((Component)((ES_Base)__instance).m_enemyAgent).gameObject == (Object)null)
			{
				return;
			}
			CleanupStatus component = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null)
			{
				component.IsProcessed = true;
				component.HasVisualsTriggered = false;
			}
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(ES_Dead), "DecayDelayed", (Type[])null, (Type[])null);
				if (!(methodInfo != null))
				{
					return;
				}
				object obj = methodInfo.Invoke(__instance, new object[2]
				{
					Settings.GetStartDelay(),
					Settings.GetBodyCleanupDelay()
				});
				if (obj != null && (Object)(object)((ES_Base)__instance).m_enemyAgent != (Object)null)
				{
					((MonoBehaviour)((ES_Base)__instance).m_enemyAgent).StartCoroutine(new IEnumerator(((Il2CppObjectBase)obj).Pointer));
					if ((Object)(object)component != (Object)null)
					{
						component.HasApplyDecayTriggered = true;
						component.DecayEndTime = Time.time + Settings.GetStartDelay() + Settings.GetBodyCleanupDelay() + 0.2f;
					}
				}
			}
			catch
			{
			}
		}

		[HarmonyPatch("DecayDelayed")]
		[HarmonyPrefix]
		public static bool OnDecayDelayed(ref float delay, ref float duration, ES_Dead __instance)
		{
			if (__instance == null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)((ES_Base)__instance).m_enemyAgent == (Object)null || ((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent).WasCollected || (Object)(object)((Component)((ES_Base)__instance).m_enemyAgent).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus component = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null)
			{
				if (component.HasVisualsTriggered)
				{
					delay = 0f;
					duration = 0.00414f;
					return true;
				}
				component.HasVisualsTriggered = true;
			}
			delay = Settings.GetStartDelay();
			duration = Settings.GetBodyCleanupDelay() + 0.00314f;
			return true;
		}

		[HarmonyPatch("CommonUpdate")]
		[HarmonyPrefix]
		public static bool OnCommonUpdate(ES_Dead __instance)
		{
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			if (__instance != null && !((Il2CppObjectBase)__instance).WasCollected && (Object)(object)((ES_Base)__instance).m_enemyAgent != (Object)null && !((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent).WasCollected && (Object)(object)((Component)((ES_Base)__instance).m_enemyAgent).gameObject != (Object)null)
			{
				if (((MachineState<ES_Base>)(object)__instance).StateChangeTime > -500f)
				{
					((MachineState<ES_Base>)(object)__instance).StateChangeTime = -1000f;
				}
				CleanupStatus component = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.GetComponent<CleanupStatus>();
				if ((Object)(object)component != (Object)null && component.HasApplyDecayTriggered && Time.time >= component.DecayEndTime && !component.HasBeenBanished)
				{
					component.HasBeenBanished = true;
					((Component)((ES_Base)__instance).m_enemyAgent).transform.position = new Vector3(0f, -10000f, 0f);
				}
			}
			return true;
		}
	}
	public static class AppearancePatches
	{
		public static bool Prefix(EnemyAppearance __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)((Component)__instance).gameObject == (Object)null)
			{
				return true;
			}
			CleanupStatus componentInParent = ((Component)__instance).gameObject.GetComponentInParent<CleanupStatus>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				if (componentInParent.HasAppearanceTriggered)
				{
					return false;
				}
				componentInParent.HasAppearanceTriggered = true;
			}
			return true;
		}

		public static bool ReceiveStateDataPrefix(EnemyAppearance __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected)
			{
				return true;
			}
			CleanupStatus componentInParent = ((Component)__instance).gameObject.GetComponentInParent<CleanupStatus>();
			if ((Object)(object)componentInParent != (Object)null && componentInParent.HasApplyDecayTriggered)
			{
				return false;
			}
			return true;
		}
	}
	public class CleanupStatus : MonoBehaviour
	{
		public static List<CleanupStatus> AllStatuses = new List<CleanupStatus>();

		public bool IsProcessed = false;

		public float ProcessTime = 0f;

		public bool HasAppearanceTriggered = false;

		public bool HasVisualsTriggered = false;

		public bool HasApplyDecayTriggered = false;

		public bool HasBeenBanished = false;

		public float DecayEndTime = 0f;

		public CleanupStatus(IntPtr ptr)
			: base(ptr)
		{
		}
	}
	[HarmonyPatch(typeof(Dam_EnemyDamageBase))]
	public static class DamageBypassPatch
	{
		[HarmonyPatch("ReceiveBulletDamage")]
		[HarmonyPrefix]
		public static bool Prefix_ReceiveBulletDamage(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch("ReceiveMeleeDamage")]
		[HarmonyPrefix]
		public static bool Prefix_ReceiveMeleeDamage(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch("ProcessReceivedDamage")]
		[HarmonyPrefix]
		public static bool Prefix_ProcessReceivedDamage(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch("ReceiveSetHealth")]
		[HarmonyPrefix]
		public static bool Prefix_ReceiveSetHealth(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		public static bool Prefix_SetHealth(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch("ReceiveExplosionDamage")]
		[HarmonyPrefix]
		public static bool Prefix_ReceiveExplosionDamage(Dam_EnemyDamageBase __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.Owner == (Object)null || ((Il2CppObjectBase)__instance.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}
	}
	public static class LimbDamageBypassPatch
	{
		public static bool Prefix_ReceiveBulletDamage(Dam_EnemyDamageLimb __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.m_base == (Object)null || ((Il2CppObjectBase)__instance.m_base).WasCollected)
			{
				return false;
			}
			if ((Object)(object)__instance.m_base.Owner == (Object)null || ((Il2CppObjectBase)__instance.m_base.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.m_base.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		public static bool Prefix_ReceiveMeleeDamage(Dam_EnemyDamageLimb __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.m_base == (Object)null || ((Il2CppObjectBase)__instance.m_base).WasCollected)
			{
				return false;
			}
			if ((Object)(object)__instance.m_base.Owner == (Object)null || ((Il2CppObjectBase)__instance.m_base.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.m_base.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}

		public static bool Prefix_ProcessReceivedDamage(Dam_EnemyDamageLimb __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)__instance.m_base == (Object)null || ((Il2CppObjectBase)__instance.m_base).WasCollected)
			{
				return false;
			}
			if ((Object)(object)__instance.m_base.Owner == (Object)null || ((Il2CppObjectBase)__instance.m_base.Owner).WasCollected)
			{
				return false;
			}
			CleanupStatus component = ((Component)__instance.m_base.Owner).GetComponent<CleanupStatus>();
			if ((Object)(object)component != (Object)null && component.IsProcessed)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ES_Hitreact))]
	public static class HitreactPatches
	{
		[HarmonyPatch("DoHitReact")]
		[HarmonyPrefix]
		public static bool Prefix_DoHitReact(ES_Hitreact __instance)
		{
			if (__instance != null && (Object)(object)((ES_Base)__instance).m_enemyAgent != (Object)null)
			{
				CleanupStatus component = ((Component)((ES_Base)__instance).m_enemyAgent).gameObject.GetComponent<CleanupStatus>();
				if ((Object)(object)component != (Object)null && component.HasApplyDecayTriggered)
				{
					return false;
				}
			}
			return true;
		}
	}
	public static class DamSyncCompat
	{
		private static Type GetTypeRobust(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				Type type = assembly.GetType(fullName, throwOnError: false, ignoreCase: true);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		private static void TryPatchDamSync(Harmony h, Type type, string methodName, string patchMethodName)
		{
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				int num = 0;
				foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(type))
				{
					if (declaredMethod.Name == methodName)
					{
						h.Patch((MethodBase)declaredMethod, new HarmonyMethod(typeof(DamSyncCompat), patchMethodName, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num++;
					}
				}
				if (num == 0)
				{
					ManualLogSource l = EntryPoint.L;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(40, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Zose-Opt] DamSync method ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(methodName);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found in ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(type.Name);
					}
					l.LogWarning(val);
					return;
				}
				ManualLogSource l2 = EntryPoint.L;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(58, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[Zose-Opt] Successfully patched ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" overload(s) for DamSync ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(type.Name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(methodName);
				}
				l2.LogInfo(val2);
			}
			catch (Exception ex)
			{
				ManualLogSource l3 = EntryPoint.L;
				BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(37, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[Zose-Opt] Failed to patch DamSync ");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(methodName);
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
				}
				l3.LogError(val3);
			}
		}

		public static void Init(Harmony h)
		{
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Expected O, but got Unknown
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			try
			{
				Type typeRobust = GetTypeRobust("DamSync.Network");
				if (typeRobust != null)
				{
					TryPatchDamSync(h, typeRobust, "SendLimbHealth", "Prefix_SendLimbHealth");
					MethodInfo methodInfo = AccessTools.Method(typeRobust, "RecieveBytes_Prefix", (Type[])null, (Type[])null);
					if (methodInfo != null)
					{
						h.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(DamSyncCompat), "Finalizer_SwallowError", (Type[])null), (HarmonyMethod)null);
						EntryPoint.L.LogInfo((object)"DamSync network wrapper successfully applied! Finalizer attached.");
					}
				}
				Type typeRobust2 = GetTypeRobust("DamSync.DamageSync");
				if (typeRobust2 != null)
				{
					TryPatchDamSync(h, typeRobust2, "ProcessReceivedDamage", "Prefix_ProcessReceivedDamage");
					TryPatchDamSync(h, typeRobust2, "ReceiveSetHealth", "Prefix_ReceiveSetHealth");
					TryPatchDamSync(h, typeRobust2, "Sync", "Prefix_ProcessReceivedDamage");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = EntryPoint.L;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(26, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("DamSyncCompat Init Error: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogError(val);
			}
		}

		public static bool Prefix_SendLimbHealth(EnemyAgent target)
		{
			if ((Object)(object)target == (Object)null || ((Il2CppObjectBase)target).WasCollected)
			{
				return false;
			}
			return true;
		}

		public static Exception Finalizer_SwallowError(Exception __exception, ref bool __result)
		{
			if (__exception != null)
			{
				__result = false;
				return null;
			}
			return null;
		}

		public static bool Prefix_ProcessReceivedDamage(Dam_EnemyDamageBase __0)
		{
			if ((Object)(object)__0 == (Object)null || ((Il2CppObjectBase)__0).WasCollected || (Object)(object)__0.Owner == (Object)null || ((Il2CppObjectBase)__0.Owner).WasCollected)
			{
				return false;
			}
			return true;
		}

		public static bool Prefix_ReceiveSetHealth(Dam_EnemyDamageBase __0)
		{
			if ((Object)(object)__0 == (Object)null || ((Il2CppObjectBase)__0).WasCollected || (Object)(object)__0.Owner == (Object)null || ((Il2CppObjectBase)__0.Owner).WasCollected)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ManualLogSource))]
	public static class LogSuppressorPatch
	{
		[HarmonyPatch("Log", new Type[]
		{
			typeof(LogLevel),
			typeof(object)
		})]
		[HarmonyPrefix]
		public static bool Prefix(LogLevel level, object data)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			if ((int)level == 2 && data != null)
			{
				string text = data.ToString();
				if (text.Contains("invoked an event DamageIndicator which isn't registered"))
				{
					return false;
				}
				if (text.Contains("DamSync.Network::RecieveBytes_Prefix"))
				{
					return false;
				}
			}
			return true;
		}
	}
	[BepInPlugin("com.zose.deadBodyOptimization", "Zose_DeadBodyOptimization", "1.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class EntryPoint : BasePlugin
	{
		public static ManualLogSource? L;

		public override void Load()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			L = ((BasePlugin)this).Log;
			Settings.Init(((BasePlugin)this).Config);
			bool flag = default(bool);
			try
			{
				ClassInjector.RegisterTypeInIl2Cpp<CleanupStatus>();
			}
			catch (Exception ex)
			{
				ManualLogSource l = L;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(13, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("inject fail: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				l.LogError(val);
			}
			Harmony val2 = new Harmony("com.zose.deadBodyOptimization");
			try
			{
				val2.PatchAll();
				ApplyManual(val2);
				DamSyncCompat.Init(val2);
				RenderPipePatch.Init(val2);
				L.LogInfo((object)"deadBodyOpt init.");
			}
			catch (Exception ex2)
			{
				ManualLogSource l2 = L;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(12, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("patch fail: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex2.Message);
				}
				l2.LogError(val);
			}
		}

		private void TryPatch(Harmony h, Type type, string methodName, Type patchType, string patchMethodName)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				MethodInfo methodInfo = AccessTools.Method(type, methodName, (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					h.Patch((MethodBase)methodInfo, new HarmonyMethod(patchType, patchMethodName, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					return;
				}
				ManualLogSource l = L;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(32, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Zose-Opt] Method ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(methodName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found in ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(type.Name);
				}
				l.LogWarning(val);
			}
			catch (Exception ex)
			{
				ManualLogSource l2 = L;
				BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(30, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[Zose-Opt] Failed to patch ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(type.Name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(methodName);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				l2.LogError(val2);
			}
		}

		private void TryPatchAllOverloads(Harmony h, Type type, string methodName, Type patchType, string patchMethodName)
		{
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Expected O, but got Unknown
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				int num = 0;
				Type type2 = type;
				while (type2 != null && type2 != typeof(object))
				{
					foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(type2))
					{
						if (declaredMethod.Name == methodName)
						{
							h.Patch((MethodBase)declaredMethod, new HarmonyMethod(patchType, patchMethodName, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
							num++;
						}
					}
					type2 = type2.BaseType;
				}
				if (num == 0)
				{
					ManualLogSource l = L;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(48, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Zose-Opt] No overloads found for ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(methodName);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" in ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(type.Name);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" hierarchy");
					}
					l.LogWarning(val);
					return;
				}
				ManualLogSource l2 = L;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(50, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[Zose-Opt] Successfully patched ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" overload(s) for ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(type.Name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(methodName);
				}
				l2.LogInfo(val2);
			}
			catch (Exception ex)
			{
				ManualLogSource l3 = L;
				BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(44, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[Zose-Opt] Failed to patch overloads for ");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(type.Name);
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(methodName);
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
				}
				l3.LogError(val3);
			}
		}

		private void ApplyManual(Harmony h)
		{
			Type typeFromHandle = typeof(EnemyAgent);
			TryPatchAllOverloads(h, typeFromHandle, "Setup", typeof(AgentPatches), "OnSetupPrefix");
			TryPatchAllOverloads(h, typeFromHandle, "ChangeState", typeof(AgentPatches), "ChangeStatePrefix");
			TryPatchAllOverloads(h, typeFromHandle, "ReceiveStateData", typeof(AgentPatches), "ReceiveStateDataPrefix");
			Type typeFromHandle2 = typeof(EnemyLocomotion);
			TryPatchAllOverloads(h, typeFromHandle2, "ChangeState", typeof(AgentPatches), "ChangeStatePrefix");
			TryPatchAllOverloads(h, typeFromHandle2, "ReceiveStateData", typeof(AgentPatches), "ReceiveStateDataPrefix");
			Type typeFromHandle3 = typeof(EnemyAppearance);
			TryPatchAllOverloads(h, typeFromHandle3, "Setup", typeof(AppearancePatches), "Prefix");
			Type typeFromHandle4 = typeof(Dam_EnemyDamageBase);
			TryPatchAllOverloads(h, typeFromHandle4, "OnHealthChanged", typeof(DamageBypassPatch), "Prefix_SetHealth");
			Type typeFromHandle5 = typeof(Dam_EnemyDamageLimb);
			TryPatchAllOverloads(h, typeFromHandle5, "BulletDamage", typeof(LimbDamageBypassPatch), "Prefix_ReceiveBulletDamage");
			TryPatchAllOverloads(h, typeFromHandle5, "MeleeDamage", typeof(LimbDamageBypassPatch), "Prefix_ReceiveMeleeDamage");
			TryPatchAllOverloads(h, typeFromHandle5, "ExplosionDamage", typeof(LimbDamageBypassPatch), "Prefix_ProcessReceivedDamage");
			TryPatchAllOverloads(h, typeFromHandle5, "UpdateDestruction", typeof(LimbDamageBypassPatch), "Prefix_ProcessReceivedDamage");
		}
	}
	[HarmonyPatch(typeof(SNet_Replication))]
	public static class NetworkQueuePatch
	{
		[HarmonyPatch("RecieveBytes")]
		[HarmonyFinalizer]
		public static Exception Finalizer_RecieveBytes(Exception __exception)
		{
			if (__exception != null)
			{
				return null;
			}
			return null;
		}
	}
	public static class RenderPipePatch
	{
		private static float lastLogTime;

		private static int errorCount;

		public static void Init(Harmony h)
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Expected O, but got Unknown
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			try
			{
				Type type = Type.GetType("RenderPipe, Assembly-CSharp") ?? GetTypeRobust("RenderPipe");
				if (type != null)
				{
					MethodInfo methodInfo = AccessTools.Method(type, "GatherCommands", (Type[])null, (Type[])null);
					if (methodInfo != null)
					{
						h.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(RenderPipePatch), "Finalizer_GatherCommands", (Type[])null), (HarmonyMethod)null);
						EntryPoint.L.LogInfo((object)"[Zose-Opt] Successfully applied Finalizer to RenderPipe.GatherCommands! Black screen protection is active.");
					}
					else
					{
						EntryPoint.L.LogWarning((object)"[Zose-Opt] RenderPipe.GatherCommands method not found.");
					}
				}
				else
				{
					EntryPoint.L.LogWarning((object)"[Zose-Opt] RenderPipe type not found.");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = EntryPoint.L;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(39, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Zose-Opt] Failed to patch RenderPipe: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				l.LogError(val);
			}
		}

		private static Type GetTypeRobust(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				Type type = assembly.GetType(fullName, throwOnError: false, ignoreCase: true);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		public static Exception Finalizer_GatherCommands(Exception __exception)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			if (__exception != null)
			{
				if (Time.time - lastLogTime > 1f)
				{
					lastLogTime = Time.time;
					errorCount++;
					ManualLogSource l = EntryPoint.L;
					bool flag = default(bool);
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(111, 3, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Zose-Opt] [Render Protection] Swallowed RenderPipe.GatherCommands exception! Count: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(errorCount);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(".\nException: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(__exception.Message);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("\nStackTrace:\n");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(__exception.StackTrace);
					}
					l.LogWarning(val);
				}
				return null;
			}
			return null;
		}
	}
	public static class Settings
	{
		private static FileSystemWatcher? _w;

		private static ConfigFile? _c;

		public static ConfigEntry<float>? BodyDelay { get; private set; }

		public static ConfigEntry<float>? StartDelay { get; private set; }

		public static void Init(ConfigFile c)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			_c = c;
			string text = "Optimization";
			StartDelay = c.Bind<float>(text, "Wait Time Before Dissolve", 0.1f, new ConfigDescription("怪物死亡后多长时间才开始播放消散动画 (0.1s - 1.5s)。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1.5f), Array.Empty<object>()));
			BodyDelay = c.Bind<float>(text, "Dissolve Animation Duration", 0.5f, new ConfigDescription("消散动画本身的播放速度 (0.1s - 1.5s)。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1.5f), Array.Empty<object>()));
			Watch(c.ConfigFilePath);
		}

		private static void Watch(string path)
		{
			try
			{
				string directoryName = Path.GetDirectoryName(path);
				if (directoryName == null)
				{
					return;
				}
				_w = new FileSystemWatcher(directoryName, Path.GetFileName(path))
				{
					NotifyFilter = NotifyFilters.LastWrite
				};
				_w.Changed += delegate
				{
					Thread.Sleep(500);
					ConfigFile? c = _c;
					if (c != null)
					{
						c.Reload();
					}
				};
				_w.EnableRaisingEvents = true;
			}
			catch
			{
			}
		}

		public static float GetStartDelay()
		{
			return Mathf.Clamp(StartDelay?.Value ?? 0.1f, 0.1f, 1.5f);
		}

		public static float GetBodyCleanupDelay()
		{
			return Mathf.Clamp(BodyDelay?.Value ?? 0.5f, 0.1f, 1.5f);
		}
	}
}