Decompiled source of DefeatRespawner v0.1.0

plugins/DefeatRespawner.dll

Decompiled 2 weeks 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("SolarAaron")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DefeatRespawner")]
[assembly: AssemblyTitle("DefeatRespawner")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 DefeatRespawner
{
	[BepInPlugin("SolarAaron.DefeatRespawner", "DefeatRespawner", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class DefeatRespawner : BaseUnityPlugin
	{
		internal static DefeatRespawner Instance { get; private set; }

		public ConfigEntry<int>? ConfigMutations { get; private set; }

		public ConfigEntry<int>? ConfigChance { get; private set; }

		public ConfigEntry<bool> OrbTradeoff { get; private set; }

		internal static ManualLogSource Logger => Instance.BaseLogger;

		private ManualLogSource BaseLogger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Expected O, but got Unknown
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			ConfigMutations = ((BaseUnityPlugin)this).Config.Bind<int>("General", "numOfRespawns", 3, new ConfigDescription("Number of Respawns per enemy death (1 death -> # new enemies)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
			ConfigChance = ((BaseUnityPlugin)this).Config.Bind<int>("General", "respawnChance", 25, new ConfigDescription("Chance of Respawn happening per respawn", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			OrbTradeoff = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "orbTradeoff", false, new ConfigDescription("Small enemies drop orbs but are affected by respawn", (AcceptableValueBase)null, Array.Empty<object>()));
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0025: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
			Logger.LogInfo((object)"Defeat Respawner: Patched!!");
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
	[HarmonyPatch(typeof(EnemyHealth), "Death")]
	[HarmonyWrapSafe]
	public class EnemyHealthPatch
	{
		private static void Prefix(EnemyHealth __instance)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_0091: 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_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0345: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Unknown result type (might be due to invalid IL or missing references)
			//IL_035d: Unknown result type (might be due to invalid IL or missing references)
			//IL_038b: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || !Object.op_Implicit((Object)(object)__instance))
			{
				DefeatRespawner.Logger.LogDebug((object)"Instance is null, skipping check");
				return;
			}
			Enemy val = (Enemy)AccessTools.Field(typeof(EnemyHealth), "enemy").GetValue(__instance);
			if ((Object)(object)val == (Object)null || !Object.op_Implicit((Object)(object)val))
			{
				return;
			}
			int instanceID = ((Object)val).GetInstanceID();
			HashSet<LevelPoint> bannedPoints = new HashSet<LevelPoint>();
			EnemyParent componentInParent = ((Component)val).GetComponentInParent<EnemyParent>();
			if ((Object)(object)componentInParent == (Object)null || !Object.op_Implicit((Object)(object)componentInParent))
			{
				return;
			}
			List<Vector3> spawnOrigins = new List<Vector3> { ((Component)val).transform.position };
			FieldRef<PlayerAvatar, bool> val2 = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isDisabled");
			foreach (PlayerAvatar item in SemiFunc.PlayerGetList())
			{
				if (!((Object)(object)item == (Object)null) && Object.op_Implicit((Object)(object)item) && !val2.Invoke(item))
				{
					spawnOrigins.Add(((Component)item).transform.position);
				}
			}
			if (!__instance.spawnValuable && DefeatRespawner.Instance.OrbTradeoff.Value)
			{
				FieldRef<EnemyHealth, int> val3 = AccessTools.FieldRefAccess<EnemyHealth, int>("spawnValuableCurrent");
				__instance.spawnValuable = true;
				DefeatRespawner.Logger.LogDebug((object)$"DEBUG: spawn valuable current was {val3.Invoke(__instance)}");
				val3.Invoke(__instance) = 0;
			}
			string enemyName = componentInParent.enemyName;
			if (enemyName == null || !__instance.spawnValuable)
			{
				return;
			}
			EnemySetup val4 = Enemies.AllEnemies?.FirstOrDefault((Func<EnemySetup, bool>)((EnemySetup setup) => EnemySetupMatchesName(setup, enemyName)));
			if ((Object)(object)val4 == (Object)null)
			{
				return;
			}
			FieldRef<LevelPoint, bool> inStartRoomRef = AccessTools.FieldRefAccess<LevelPoint, bool>("inStartRoom");
			for (int num = 0; num < DefeatRespawner.Instance.ConfigMutations.Value; num++)
			{
				if (Random.Range(0, 100) >= DefeatRespawner.Instance.ConfigChance.Value)
				{
					continue;
				}
				var list = (from val5 in SemiFunc.LevelPointsGetAll()
					where Object.op_Implicit((Object)(object)val5) && !bannedPoints.Contains(val5) && !inStartRoomRef.Invoke(val5)
					select new
					{
						Point = val5,
						NearestOriginDistance = spawnOrigins.Min((Vector3 origin) => Vector3.Distance(((Component)val5).transform.position, origin))
					} into candidate
					where candidate.NearestOriginDistance > 10f
					select candidate).ToList();
				if (list.Count == 0)
				{
					DefeatRespawner.Logger.LogDebug((object)"No safe respawn point available; skipping this respawn.");
					continue;
				}
				float furthestDistance = list.Max(candidate => candidate.NearestOriginDistance);
				var list2 = list.Where(candidate => Mathf.Approximately(candidate.NearestOriginDistance, furthestDistance)).ToList();
				LevelPoint point = list2[Random.Range(0, list2.Count)].Point;
				foreach (LevelPoint item2 in SemiFunc.LevelPointGetWithinDistance(((Component)point).transform.position, 0f, 10f))
				{
					bannedPoints.Add(item2);
				}
				Enemies.SpawnEnemy(val4, ((Component)point).transform.position, Quaternion.identity, false);
				SemiFunc.EnemyGetNearest(((Component)point).transform.position, 2f, false);
				DefeatRespawner.Logger.LogInfo((object)$"Respawned: {enemyName} | ID: {instanceID} at {((Component)point).transform.position}");
			}
		}

		private static bool EnemySetupMatchesName(EnemySetup? enemySetup, string enemyName)
		{
			if ((Object)(object)enemySetup == (Object)null || enemySetup.spawnObjects == null)
			{
				return false;
			}
			if (NameMatches(((Object)enemySetup).name, enemyName))
			{
				return true;
			}
			foreach (PrefabRef spawnObject in enemySetup.spawnObjects)
			{
				if (spawnObject == null)
				{
					continue;
				}
				GameObject prefab = ((PrefabRef<GameObject>)(object)spawnObject).Prefab;
				if (!((Object)(object)prefab == (Object)null))
				{
					if (NameMatches(((Object)prefab).name, enemyName))
					{
						return true;
					}
					EnemyParent componentInChildren = prefab.GetComponentInChildren<EnemyParent>(true);
					if ((Object)(object)componentInChildren != (Object)null && string.Equals(componentInChildren.enemyName, enemyName, StringComparison.OrdinalIgnoreCase))
					{
						return true;
					}
				}
			}
			return false;
		}

		private static bool NameMatches(string candidate, string enemyName)
		{
			if (!string.Equals(candidate, enemyName, StringComparison.OrdinalIgnoreCase))
			{
				return string.Equals(candidate, "Enemy - " + enemyName, StringComparison.OrdinalIgnoreCase);
			}
			return true;
		}
	}
}