Decompiled source of RemoveJester v1.0.1

RemoveJester.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("RemoveJester")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RemoveJester")]
[assembly: AssemblyTitle("RemoveJester")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
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 RemoveJester
{
	[BepInPlugin("com.steadytao.removejester", "Remove Jester", "1.0.0")]
	public sealed class RemoveJesterPlugin : BaseUnityPlugin
	{
		internal const string PluginGuid = "com.steadytao.removejester";

		internal const string PluginName = "Remove Jester";

		internal const string PluginVersion = "1.0.0";

		private const string DungeonNpcSpawnerTypeName = "YAPYAP.DungeonNpcSpawner";

		private const string JesterStateMachineTypeName = "YAPYAP.Npc.Jester.JesterStateMachine";

		private Harmony? _harmony;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.steadytao.removejester");
			PatchDungeonNpcSpawner();
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void PatchDungeonNpcSpawner()
		{
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Expected O, but got Unknown
			Type type = AccessTools.TypeByName("YAPYAP.DungeonNpcSpawner");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not find YAPYAP.DungeonNpcSpawner. The game may have updated.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Initialize", (Type[])null, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(type, "SpawnNpc", (Type[])null, (Type[])null);
			MethodInfo methodInfo3 = AccessTools.Method(typeof(RemoveJesterPlugin), "FilterJesterPrefabs", (Type[])null, (Type[])null);
			MethodInfo methodInfo4 = AccessTools.Method(typeof(RemoveJesterPlugin), "BlockJesterSpawn", (Type[])null, (Type[])null);
			if (methodInfo == null || methodInfo2 == null || methodInfo3 == null || methodInfo4 == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not resolve the expected DungeonNpcSpawner methods for patching.");
				return;
			}
			_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo3), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(methodInfo4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Patched DungeonNpcSpawner to remove Jester from the dungeon spawn pool.");
		}

		private static void FilterJesterPrefabs(object __instance)
		{
			FieldInfo fieldInfo = AccessTools.Field(__instance.GetType(), "npcs");
			if (!(fieldInfo?.GetValue(__instance) is Array array) || array.Length == 0)
			{
				return;
			}
			Type elementType = array.GetType().GetElementType();
			if (elementType == null)
			{
				return;
			}
			List<object> list = new List<object>(array.Length);
			List<string> list2 = new List<string>();
			foreach (object item in array)
			{
				if (item != null)
				{
					if (IsJesterNpc(item))
					{
						list2.Add(GetNpcDisplayName(item));
					}
					else
					{
						list.Add(item);
					}
				}
			}
			if (list2.Count != 0)
			{
				Array array2 = Array.CreateInstance(elementType, list.Count);
				for (int i = 0; i < list.Count; i++)
				{
					array2.SetValue(list[i], i);
				}
				fieldInfo.SetValue(__instance, array2);
				Log.LogInfo((object)string.Format("Removed {0} Jester prefab(s) from DungeonNpcSpawner: {1}", list2.Count, string.Join(", ", list2.Distinct())));
			}
		}

		private static bool BlockJesterSpawn(object spawnInfo)
		{
			object npc = AccessTools.Field(spawnInfo.GetType(), "npc")?.GetValue(spawnInfo);
			if (!IsJesterNpc(npc))
			{
				return true;
			}
			Log.LogWarning((object)("Blocked fallback Jester spawn for " + GetNpcDisplayName(npc) + "."));
			return false;
		}

		private static bool IsJesterNpc(object? npc)
		{
			Component val = (Component)((npc is Component) ? npc : null);
			if (val == null)
			{
				return false;
			}
			Component[] components = val.GetComponents<Component>();
			foreach (Component val2 in components)
			{
				if (!((Object)(object)val2 == (Object)null) && string.Equals(((object)val2).GetType().FullName, "YAPYAP.Npc.Jester.JesterStateMachine", StringComparison.Ordinal))
				{
					return true;
				}
			}
			return ((Object)val).name.Contains("Jester", StringComparison.OrdinalIgnoreCase) || ((Object)val.gameObject).name.Contains("Jester", StringComparison.OrdinalIgnoreCase);
		}

		private static string GetNpcDisplayName(object? npc)
		{
			Component val = (Component)((npc is Component) ? npc : null);
			if (val != null)
			{
				return ((Object)val.gameObject).name;
			}
			return npc?.ToString() ?? "<null>";
		}
	}
}