Decompiled source of DeadBodyOptimization v1.1.0

Zose_DeadBodyOptimization.dll

Decompiled 8 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using Agents;
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;
using Il2CppSystem.Collections;
using Microsoft.CodeAnalysis;
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 OnSetup(EnemyAgent __instance)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				CleanupStatus component = ((Component)__instance).gameObject.GetComponent<CleanupStatus>();
				if ((Object)(object)component != (Object)null)
				{
					component.IsProcessed = false;
				}
			}
		}
	}
	public static class DecayPatches
	{
		public static bool OnPlay(Decay __instance, ref float duration)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			duration = Settings.GetBodyCleanupDelay();
			return true;
		}
	}
	[HarmonyPatch(typeof(ES_Dead))]
	public static class ES_DeadPatches
	{
		[HarmonyPatch("CommonEnter")]
		[HarmonyPrefix]
		public static bool OnCommonEnterPrefix(ES_Dead __instance)
		{
			if ((Object)(object)((__instance != null) ? ((ES_Base)__instance).m_enemyAgent : null) == (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>();
			}
			if (!((Agent)((ES_Base)__instance).m_enemyAgent).IsLocallyOwned && cleanupStatus.IsProcessed)
			{
				return false;
			}
			cleanupStatus.IsProcessed = true;
			return true;
		}

		[HarmonyPatch("CommonEnter")]
		[HarmonyPostfix]
		public static void OnCommonEnterPostfix(ES_Dead __instance)
		{
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Expected O, but got Unknown
			if (__instance == null)
			{
				return;
			}
			((MachineState<ES_Base>)(object)__instance).StateChangeTime = -1000f;
			try
			{
				EnemyAgent enemyAgent = ((ES_Base)__instance).m_enemyAgent;
				if (enemyAgent != null)
				{
					EnemyAppearance appearance = enemyAgent.Appearance;
					if (appearance != null)
					{
						appearance.OnDead();
					}
				}
			}
			catch
			{
			}
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(ES_Dead), "DecayDelayed", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					object obj2 = methodInfo.Invoke(__instance, new object[2]
					{
						Settings.GetStartDelay(),
						Settings.GetBodyCleanupDelay()
					});
					if (obj2 != null && (Object)(object)((ES_Base)__instance).m_enemyAgent != (Object)null)
					{
						((MonoBehaviour)((ES_Base)__instance).m_enemyAgent).StartCoroutine(new IEnumerator(((Il2CppObjectBase)obj2).Pointer));
					}
				}
			}
			catch
			{
			}
			try
			{
				EnemyAgent enemyAgent2 = ((ES_Base)__instance).m_enemyAgent;
				if (enemyAgent2 != null)
				{
					Nanoswarmable component = ((Component)enemyAgent2).GetComponent<Nanoswarmable>();
					if (component != null)
					{
						component.AddSwarm((NanoswarmType)0, 100f, Settings.GetStartDelay() + 0.001f);
					}
				}
			}
			catch
			{
			}
		}

		[HarmonyPatch("DecayDelayed")]
		[HarmonyPrefix]
		public static bool OnDecayDelayed(ref float delay, ref float duration)
		{
			delay = Settings.GetStartDelay();
			duration = Settings.GetBodyCleanupDelay();
			return true;
		}

		[HarmonyPatch("CommonUpdate")]
		[HarmonyPrefix]
		public static void OnCommonUpdate(ES_Dead __instance)
		{
			if (__instance != null && ((MachineState<ES_Base>)(object)__instance).StateChangeTime > -500f)
			{
				((MachineState<ES_Base>)(object)__instance).StateChangeTime = -1000f;
			}
		}
	}
	public class CleanupStatus : MonoBehaviour
	{
		public bool IsProcessed = false;

		public CleanupStatus(IntPtr ptr)
			: base(ptr)
		{
		}
	}
	public static class NanoswarmPatches
	{
		public static bool OnNano(Object __instance, ref float swarmValue, ref float duration)
		{
			swarmValue = Settings.GetStartDelay() + 0.001f;
			duration = Settings.GetBodyCleanupDelay();
			return true;
		}
	}
	[BepInPlugin("com.zose.deadBodyOptimization", "Zose_DeadBodyOptimization", "1.0.0")]
	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_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: 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);
				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 ApplyManual(Harmony h)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			Type typeFromHandle = typeof(EnemyAgent);
			h.Patch((MethodBase)AccessTools.Method(typeFromHandle, "Setup", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(AgentPatches), "OnSetup", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Type typeFromHandle2 = typeof(Decay);
			h.Patch((MethodBase)AccessTools.Method(typeFromHandle2, "Play", new Type[1] { typeof(float) }, (Type[])null), new HarmonyMethod(typeof(DecayPatches), "OnPlay", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MethodInfo methodInfo = AccessTools.Method(typeof(Nanoswarmable), "StartNanoSwarmDecayOnAgentAfterTime", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				h.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(NanoswarmPatches), "OnNano", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)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);
		}
	}
}