Decompiled source of BossAntiSoftlock v1.0.4

lib/BossAntiSoftlock.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HG.Reflection;
using On.RoR2;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("BossAntiSoftlock")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BossAntiSoftlock")]
[assembly: AssemblyTitle("BossAntiSoftlock")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BossAntiSoftlock;

[BepInPlugin("com.justinderby.bossantisoftlock", "Boss Anti-Softlock", "1.0.4")]
public class BossAntiSoftlock : BaseUnityPlugin
{
	public const string GUID = "com.justinderby.bossantisoftlock";

	public const string ModName = "Boss Anti-Softlock";

	public const string Version = "1.0.4";

	public static Dictionary<CharacterBody, Vector3> SpawnPositions = new Dictionary<CharacterBody, Vector3>();

	public static ConfigFile Configuration;

	public static ConfigEntry<bool> ModHint;

	public static ConfigEntry<bool> ResetVoid;

	public static BossAntiSoftlock Instance;

	public void Awake()
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Expected O, but got Unknown
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Expected O, but got Unknown
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		Instance = SingletonHelper.Assign<BossAntiSoftlock>(Instance, this);
		Configuration = new ConfigFile(Path.Combine(Paths.ConfigPath, "com.justinderby.bossantisoftlock.cfg"), true);
		ModHint = Configuration.Bind<bool>("General", "Show Hints", true, "Whether to send a reminder every time the teleporter event has started.");
		ResetVoid = Configuration.Bind<bool>("General", "Reset Voidtouched Monsters", true, "Whether to also reset voidtouched monsters.");
		Stage.onStageStartGlobal += delegate
		{
			SpawnPositions.Clear();
		};
		Run.OnServerCharacterBodySpawned += new hook_OnServerCharacterBodySpawned(TrackNewMonster);
		GlobalEventManager.onCharacterDeathGlobal += RemoveMonster;
		TeleporterInteraction.onTeleporterBeginChargingGlobal += SendModHint;
		Console.RunCmd += new hook_RunCmd(HandleCommand);
	}

	public void Destroy()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		Instance = null;
		Stage.onStageStartGlobal -= delegate
		{
			SpawnPositions.Clear();
		};
		Run.OnServerCharacterBodySpawned -= new hook_OnServerCharacterBodySpawned(TrackNewMonster);
		GlobalEventManager.onCharacterDeathGlobal -= RemoveMonster;
		TeleporterInteraction.onTeleporterBeginChargingGlobal -= SendModHint;
		Console.RunCmd -= new hook_RunCmd(HandleCommand);
		SpawnPositions.Clear();
	}

	private void SendModChat(string message)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		Chat.SendBroadcastChat((ChatMessageBase)new SimpleChatMessage
		{
			baseToken = "<color=#93c47d>Boss Anti-Softlock:</color> " + message
		});
	}

	[ConCommand(/*Could not decode attribute arguments.*/)]
	private static void ForceResetPositions(ConCommandArgs _)
	{
		if (Object.op_Implicit((Object)(object)Instance))
		{
			Debug.Log((object)"Resetting all monster positions...");
			Instance.ResetCharactersPositions(GetBosses());
		}
	}

	private void HandleCommand(orig_RunCmd orig, Console self, CmdSender sender, string concommandName, List<string> userArgs)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		orig.Invoke(self, sender, concommandName, userArgs);
		if (!NetworkServer.active || (Object)(object)Run.instance == (Object)null || !concommandName.Equals("say", StringComparison.InvariantCultureIgnoreCase) || userArgs.Count == 0)
		{
			return;
		}
		switch (userArgs[0].ToLower())
		{
		case "/bossreset":
		case "/boss_reset":
		case "/resetboss":
		case "/resetbosses":
		case "/reset_boss":
		case "/reset_bosses":
		case "/br":
		case "/rb":
		{
			List<CharacterBody> bosses = GetBosses();
			SendModChat(string.Format("Resetting monster positions... ({0} monster{1})", bosses.Count, (bosses.Count == 1) ? "" : "s"));
			try
			{
				ResetCharactersPositions(bosses);
				break;
			}
			catch (Exception ex)
			{
				Debug.LogException(ex);
				SendModChat("Error resetting boss positions; check console for more info!");
				break;
			}
		}
		}
	}

	private void SendModHint(TeleporterInteraction obj)
	{
		if (ModHint.Value)
		{
			SendModChat("Type '/bossreset' to reset monster positions.");
		}
	}

	private void TrackNewMonster(orig_OnServerCharacterBodySpawned orig, Run self, CharacterBody body)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		orig.Invoke(self, body);
		if (!((Object)(object)((Component)body).gameObject == (Object)null) && ((Behaviour)body).isActiveAndEnabled && !body.isPlayerControlled)
		{
			SpawnPositions.Add(body, body.footPosition);
		}
	}

	private static List<CharacterBody> GetBosses()
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Invalid comparison between Unknown and I4
		List<CharacterBody> list = new List<CharacterBody>();
		foreach (CharacterBody key in SpawnPositions.Keys)
		{
			if (!((Object)(object)((key != null) ? ((Component)key).gameObject : null) == (Object)null) && ((Behaviour)key).isActiveAndEnabled && (key.isBoss || Run.instance is InfiniteTowerRun || (ResetVoid.Value && (int)key.teamComponent.teamIndex == 4)))
			{
				list.Add(key);
			}
		}
		return list;
	}

	private void RemoveMonster(DamageReport report)
	{
		if ((Object)(object)report?.victimBody != (Object)null)
		{
			SpawnPositions.Remove(report.victimBody);
		}
	}

	private void ResetCharactersPositions(List<CharacterBody> bodies)
	{
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		if (ResetVoid.Value)
		{
			foreach (TeamComponent teamMember in TeamComponent.GetTeamMembers((TeamIndex)4))
			{
				if ((Object)(object)teamMember.body != (Object)null && !bodies.Contains(teamMember.body))
				{
					bodies.Add(teamMember.body);
				}
			}
		}
		foreach (CharacterBody body in bodies)
		{
			if ((Object)(object)body == (Object)null || (Object)(object)((Component)body).gameObject == (Object)null || !((Behaviour)body).isActiveAndEnabled)
			{
				SpawnPositions.Remove(body);
				continue;
			}
			Debug.Log((object)string.Format("{0} - Teleporting {1} to {2}", "com.justinderby.bossantisoftlock", body, SpawnPositions[body]));
			TeleportHelper.TeleportGameObject(((Component)body).gameObject, SpawnPositions[body]);
			GameObject gameObject = ((Component)body).gameObject;
			if (Object.op_Implicit((Object)(object)gameObject))
			{
				EntityStateMachine[] components = gameObject.GetComponents<EntityStateMachine>();
				foreach (EntityStateMachine obj in components)
				{
					obj.initialStateType = obj.mainStateType;
				}
				GameObject teleportEffectPrefab = Run.instance.GetTeleportEffectPrefab(gameObject.gameObject);
				if (Object.op_Implicit((Object)(object)teleportEffectPrefab))
				{
					EffectManager.SimpleEffect(teleportEffectPrefab, SpawnPositions[body], Quaternion.identity, true);
				}
			}
		}
	}
}