Decompiled source of BeeBlocker v1.0.0

plugins/BeeBlocker.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Assets.Scripts.Actors.Enemies;
using Assets.Scripts.Managers;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
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(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BeeBlocker")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Replaces Bees with Slimes.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+80b63a9148b0da49d56e2009478d1bb2460cf88d")]
[assembly: AssemblyProduct("BeeBlocker")]
[assembly: AssemblyTitle("BeeBlocker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MEGABONK_BeeBlocker
{
	[BepInPlugin("RougeShadow_BeeBlocker", "BeeBlocker", "1.0.2")]
	public class Plugin : BasePlugin
	{
		[HarmonyPatch(typeof(EnemyManager))]
		public static class Patch_EnemyManager_SpawnEnemy
		{
			[CompilerGenerated]
			private sealed class <TargetMethods>d__6 : IEnumerable<MethodBase>, IEnumerable, IEnumerator<MethodBase>, IEnumerator, IDisposable
			{
				private int <>1__state;

				private MethodBase <>2__current;

				private int <>l__initialThreadId;

				private Type <t>5__1;

				MethodBase IEnumerator<MethodBase>.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				object IEnumerator.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				[DebuggerHidden]
				public <TargetMethods>d__6(int <>1__state)
				{
					this.<>1__state = <>1__state;
					<>l__initialThreadId = Environment.CurrentManagedThreadId;
				}

				[DebuggerHidden]
				void IDisposable.Dispose()
				{
					<t>5__1 = null;
					<>1__state = -2;
				}

				private bool MoveNext()
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<t>5__1 = typeof(EnemyManager);
						<>2__current = AccessTools.Method(<t>5__1, "SpawnEnemy", new Type[5]
						{
							typeof(EnemyData),
							typeof(int),
							typeof(bool),
							typeof(EEnemyFlag),
							typeof(bool)
						}, (Type[])null);
						<>1__state = 1;
						return true;
					case 1:
						<>1__state = -1;
						<>2__current = AccessTools.Method(<t>5__1, "SpawnEnemy", new Type[6]
						{
							typeof(EnemyData),
							typeof(Vector3),
							typeof(int),
							typeof(bool),
							typeof(EEnemyFlag),
							typeof(bool)
						}, (Type[])null);
						<>1__state = 2;
						return true;
					case 2:
						<>1__state = -1;
						return false;
					}
				}

				bool IEnumerator.MoveNext()
				{
					//ILSpy generated this explicit interface implementation from .override directive in MoveNext
					return this.MoveNext();
				}

				[DebuggerHidden]
				void IEnumerator.Reset()
				{
					throw new NotSupportedException();
				}

				[DebuggerHidden]
				IEnumerator<MethodBase> IEnumerable<MethodBase>.GetEnumerator()
				{
					if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
					{
						<>1__state = 0;
						return this;
					}
					return new <TargetMethods>d__6(0);
				}

				[DebuggerHidden]
				IEnumerator IEnumerable.GetEnumerator()
				{
					return ((IEnumerable<MethodBase>)this).GetEnumerator();
				}
			}

			private const string PreferredReplacementLabel = "Slime";

			private static EnemyData _cachedPreferred;

			private static EnemyData _cachedAnyNonBee;

			private static bool _schemaInspected;

			private static List<MemberInfo> _candidateNameMembers;

			private static List<MemberInfo> _candidateEnumMembers;

			[IteratorStateMachine(typeof(<TargetMethods>d__6))]
			private static IEnumerable<MethodBase> TargetMethods()
			{
				//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
				return new <TargetMethods>d__6(-2);
			}

			private static bool Prefix(ref EnemyData enemyData)
			{
				if ((Object)(object)enemyData == (Object)null)
				{
					return true;
				}
				EnsureSchema();
				if (!IsBee(enemyData))
				{
					if ((Object)(object)_cachedAnyNonBee == (Object)null)
					{
						_cachedAnyNonBee = enemyData;
					}
					string bestLabel = GetBestLabel(enemyData);
					if (!string.IsNullOrEmpty(bestLabel) && string.Equals(bestLabel, "Slime", StringComparison.OrdinalIgnoreCase))
					{
						_cachedPreferred = enemyData;
					}
					return true;
				}
				if ((Object)(object)_cachedPreferred != (Object)null)
				{
					enemyData = _cachedPreferred;
					return true;
				}
				if ((Object)(object)_cachedAnyNonBee != (Object)null)
				{
					enemyData = _cachedAnyNonBee;
					return true;
				}
				ManualLogSource log = Plugin.log;
				if (log != null)
				{
					log.LogWarning((object)"[BeeBlocker] Bee blocked but no replacement cached yet. Will replace once we've seen any non-Bee spawn.");
				}
				return false;
			}

			private static void EnsureSchema()
			{
				if (_schemaInspected)
				{
					return;
				}
				_schemaInspected = true;
				_candidateNameMembers = new List<MemberInfo>();
				_candidateEnumMembers = new List<MemberInfo>();
				Type typeFromHandle = typeof(EnemyData);
				BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
				MemberInfo[] members = typeFromHandle.GetMembers(bindingAttr);
				foreach (MemberInfo memberInfo in members)
				{
					if (memberInfo.MemberType != MemberTypes.Field && memberInfo.MemberType != MemberTypes.Property)
					{
						continue;
					}
					Type memberType = GetMemberType(memberInfo);
					if (!(memberType == null))
					{
						string text = memberInfo.Name.ToLowerInvariant();
						if ((memberType.IsEnum || memberType == typeof(int) || memberType == typeof(short) || memberType == typeof(byte)) && (text.Contains("enemy") || text.Contains("type") || text.Contains("kind") || text == "id"))
						{
							_candidateEnumMembers.Add(memberInfo);
						}
						if (memberType == typeof(string) && (text.Contains("name") || text.Contains("label") || text.Contains("id")))
						{
							_candidateNameMembers.Add(memberInfo);
						}
					}
				}
			}

			private static bool IsBee(EnemyData ed)
			{
				foreach (MemberInfo candidateEnumMember in _candidateEnumMembers)
				{
					object obj = SafeGetValue(candidateEnumMember, ed);
					if (obj == null || !obj.GetType().IsEnum || !string.Equals(obj.ToString(), "Bee", StringComparison.OrdinalIgnoreCase))
					{
						continue;
					}
					return true;
				}
				foreach (MemberInfo candidateNameMember in _candidateNameMembers)
				{
					string text = SafeGetValue(candidateNameMember, ed) as string;
					if (!string.IsNullOrEmpty(text) && text.IndexOf("Bee", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						return true;
					}
				}
				string text2 = SafeUnityName((Object)(object)ed);
				return !string.IsNullOrEmpty(text2) && text2.IndexOf("Bee", StringComparison.OrdinalIgnoreCase) >= 0;
			}

			private static string GetBestLabel(EnemyData ed)
			{
				foreach (MemberInfo candidateEnumMember in _candidateEnumMembers)
				{
					object obj = SafeGetValue(candidateEnumMember, ed);
					if (obj == null || !obj.GetType().IsEnum)
					{
						continue;
					}
					return obj.ToString();
				}
				foreach (MemberInfo candidateNameMember in _candidateNameMembers)
				{
					string text = SafeGetValue(candidateNameMember, ed) as string;
					if (!string.IsNullOrEmpty(text))
					{
						return text;
					}
				}
				return SafeUnityName((Object)(object)ed) ?? "<unknown>";
			}

			private static string SafeUnityName(Object o)
			{
				try
				{
					return Object.op_Implicit(o) ? o.name : null;
				}
				catch
				{
					return null;
				}
			}

			private static Type GetMemberType(MemberInfo m)
			{
				if (1 == 0)
				{
				}
				Type result = ((m is FieldInfo fieldInfo) ? fieldInfo.FieldType : ((!(m is PropertyInfo propertyInfo)) ? null : propertyInfo.PropertyType));
				if (1 == 0)
				{
				}
				return result;
			}

			private static object SafeGetValue(MemberInfo m, object instance)
			{
				try
				{
					if (m is FieldInfo fieldInfo)
					{
						return fieldInfo.GetValue(instance);
					}
					if (m is PropertyInfo propertyInfo && propertyInfo.CanRead)
					{
						return propertyInfo.GetValue(instance, null);
					}
				}
				catch
				{
				}
				return null;
			}
		}

		public const string MODNAME = "BeeBlocker";

		public const string AUTHOR = "RougeShadow";

		public const string GUID = "RougeShadow_BeeBlocker";

		public const string VERSION = "1.0.2";

		internal static ManualLogSource log;

		public Plugin()
		{
			log = ((BasePlugin)this).Log;
		}

		public override void Load()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			ManualLogSource val = log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(14, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Loading ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("BeeBlocker");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.2");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" by ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("RougeShadow");
			}
			val.LogInfo(val2);
			new Harmony("RougeShadow_BeeBlocker").PatchAll();
		}
	}
}