Decompiled source of BossReset v1.0.0

BossReset.dll

Decompiled 2 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.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BossReset")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+15033c8d706afeed3389020d32e825ed990dafbd")]
[assembly: AssemblyProduct("BossReset")]
[assembly: AssemblyTitle("BossReset")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 BossReset
{
	internal class BossFight
	{
		public Character Boss;

		public Vector3 Arena;

		public OfferingBowl Altar;

		public List<Character> Minions = new List<Character>();

		public List<KeyValuePair<ItemDrop, int>> GiveBack = new List<KeyValuePair<ItemDrop, int>>();

		public static BossFight Find(Vector3 point)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			float range = BossResetPlugin.BossRange.Value;
			Character boss = (from c in Character.GetAllCharacters()
				where (Object)(object)c != (Object)null && c.IsBoss() && !c.IsDead() && Vector3.Distance(((Component)c).transform.position, point) < range
				orderby Vector3.Distance(((Component)c).transform.position, point)
				select c).FirstOrDefault();
			if ((Object)(object)boss == (Object)null)
			{
				return null;
			}
			ZNetView component = ((Component)boss).GetComponent<ZNetView>();
			ZDO val = ((component != null) ? component.GetZDO() : null);
			BossFight fight = new BossFight
			{
				Boss = boss,
				Arena = ((val != null) ? val.GetVec3(ZDOVars.s_spawnPoint, ((Component)boss).transform.position) : ((Component)boss).transform.position)
			};
			fight.Altar = FindAltar(fight.Arena);
			fight.GiveBack = SummonItems(fight.Altar);
			if (BossResetPlugin.RemoveMinions.Value)
			{
				Player player = Player.m_localPlayer;
				float minionRange = BossResetPlugin.MinionRange.Value;
				fight.Minions = (from c in Character.GetAllCharacters()
					where (Object)(object)c != (Object)null && (Object)(object)c != (Object)(object)boss && !c.IsPlayer() && !c.IsDead() && !c.IsTamed() && !c.IsBoss() && (Object)(object)c.GetBaseAI() != (Object)null && (Object)(object)player != (Object)null && BaseAI.IsEnemy(c, (Character)(object)player) && Vector3.Distance(((Component)c).transform.position, fight.Arena) < minionRange
					select c).ToList();
			}
			return fight;
		}

		private static OfferingBowl FindAltar(Vector3 point)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			return (from b in Object.FindObjectsByType<OfferingBowl>((FindObjectsSortMode)0)
				where (Object)(object)b != (Object)null && (Object)(object)b.m_bossPrefab != (Object)null && Utils.DistanceXZ(SpawnArea(b), point) < 100f
				orderby Utils.DistanceXZ(SpawnArea(b), point)
				select b).FirstOrDefault();
		}

		private static Vector3 SpawnArea(OfferingBowl bowl)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			if (bowl.m_spawnPoints.Count > 0 && (Object)(object)bowl.m_spawnPoints[0] != (Object)null)
			{
				return bowl.m_spawnPoints[0].transform.position;
			}
			return ((Component)bowl).transform.position + Vector4.op_Implicit(((Component)bowl).transform.localToWorldMatrix * Vector4.op_Implicit(bowl.m_spawnAreaOffset));
		}

		private static List<KeyValuePair<ItemDrop, int>> SummonItems(OfferingBowl bowl)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			List<KeyValuePair<ItemDrop, int>> list = new List<KeyValuePair<ItemDrop, int>>();
			if ((Object)(object)bowl == (Object)null)
			{
				return list;
			}
			if (bowl.m_useItemStands)
			{
				ItemStand[] array = Object.FindObjectsByType<ItemStand>((FindObjectsSortMode)0);
				foreach (ItemStand val in array)
				{
					if (!((Object)(object)val == (Object)null) && !(Vector3.Distance(((Component)bowl).transform.position, ((Component)val).transform.position) > bowl.m_itemstandMaxRange) && ((Object)((Component)val).gameObject).name.StartsWith(bowl.m_itemStandPrefix, StringComparison.Ordinal) && !val.HaveAttachment() && val.m_supportedItems.Count == 1 && !((Object)(object)val.m_supportedItems[0] == (Object)null))
					{
						list.Add(new KeyValuePair<ItemDrop, int>(val.m_supportedItems[0], 1));
					}
				}
				return (from keyValuePair in list
					group keyValuePair by keyValuePair.Key into g
					select new KeyValuePair<ItemDrop, int>(g.Key, g.Sum((KeyValuePair<ItemDrop, int> keyValuePair) => keyValuePair.Value))).ToList();
			}
			if ((Object)(object)bowl.m_bossItem != (Object)null && bowl.m_bossItems > 0)
			{
				list.Add(new KeyValuePair<ItemDrop, int>(bowl.m_bossItem, bowl.m_bossItems));
			}
			return list;
		}

		public static bool Remove(Character c)
		{
			try
			{
				ZNetView component = ((Component)c).GetComponent<ZNetView>();
				if ((Object)(object)component == (Object)null || !component.IsValid())
				{
					return false;
				}
				if (!component.IsOwner())
				{
					component.ClaimOwnership();
				}
				if (c.IsBoss() && component.GetZDO().GetBool(ZDOVars.s_bossCount, false))
				{
					float num = default(float);
					ZoneSystem.instance.GetGlobalKey((GlobalKeys)47, ref num);
					ZoneSystem.instance.SetGlobalKey((GlobalKeys)47, Mathf.Max(0f, num - 1f));
					component.GetZDO().Set(ZDOVars.s_bossCount, false);
				}
				ZNetScene.instance.Destroy(((Component)c).gameObject);
				return true;
			}
			catch (Exception ex)
			{
				BossResetPlugin.Log.LogWarning((object)("Couldn't remove " + Name(c) + ": " + ex.Message));
				return false;
			}
		}

		public static void Give(Player player, ItemDrop prefab, int amount)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			Inventory inventory = ((Humanoid)player).GetInventory();
			if (!inventory.CanAddItem(((Component)prefab).gameObject, amount) || !inventory.AddItem(((Component)prefab).gameObject, amount))
			{
				ItemDrop component = Object.Instantiate<GameObject>(((Component)prefab).gameObject, ((Component)player).transform.position + Vector3.up, Quaternion.identity).GetComponent<ItemDrop>();
				if ((Object)(object)component != (Object)null)
				{
					component.SetStack(amount);
				}
			}
		}

		public static string Name(Character c)
		{
			return Localize(c.m_name);
		}

		public static string Localize(string token)
		{
			try
			{
				return (Localization.instance != null) ? Localization.instance.Localize(token) : token;
			}
			catch
			{
				return token;
			}
		}
	}
	internal static class Commands
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static ConsoleEvent <0>__Run;

			public static Func<Character, bool> <1>__Remove;
		}

		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static ConsoleOptionsFetcher <>9__0_0;

			public static Func<string, bool> <>9__1_0;

			public static Func<KeyValuePair<ItemDrop, int>, string> <>9__1_1;

			internal List<string> <Register>b__0_0()
			{
				return new List<string> { "confirm" };
			}

			internal bool <Run>b__1_0(string a)
			{
				return a.Equals("confirm", StringComparison.OrdinalIgnoreCase);
			}

			internal string <Run>b__1_1(KeyValuePair<ItemDrop, int> i)
			{
				return $"{i.Value}x {BossFight.Localize(i.Key.m_itemData.m_shared.m_name)}";
			}
		}

		public static void Register()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			try
			{
				object obj = <>O.<0>__Run;
				if (obj == null)
				{
					ConsoleEvent val = Run;
					<>O.<0>__Run = val;
					obj = (object)val;
				}
				object obj2 = <>c.<>9__0_0;
				if (obj2 == null)
				{
					ConsoleOptionsFetcher val2 = () => new List<string> { "confirm" };
					<>c.<>9__0_0 = val2;
					obj2 = (object)val2;
				}
				new ConsoleCommand("bossreset", "BossReset: end the nearest boss fight and get the summoning items back - bossreset | bossreset confirm", (ConsoleEvent)obj, false, false, false, false, false, false, (ConsoleOptionsFetcher)obj2, false, false, false);
			}
			catch (Exception ex)
			{
				BossResetPlugin.Log.LogWarning((object)("Couldn't add console commands: " + ex.Message));
			}
		}

		private static void Run(ConsoleEventArgs args)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				Say(args, "BossReset: load into a world with a character first.");
				return;
			}
			BossFight bossFight = BossFight.Find(((Component)localPlayer).transform.position);
			if (bossFight == null)
			{
				Say(args, $"BossReset: no living boss within {BossResetPlugin.BossRange.Value:0} m.");
				return;
			}
			bool num = args.Args.Skip(1).Any((string a) => a.Equals("confirm", StringComparison.OrdinalIgnoreCase));
			string text = BossFight.Name(bossFight.Boss);
			string text2 = ((bossFight.GiveBack.Count > 0) ? string.Join(", ", bossFight.GiveBack.Select((KeyValuePair<ItemDrop, int> i) => $"{i.Value}x {BossFight.Localize(i.Key.m_itemData.m_shared.m_name)}")) : null);
			if (!num)
			{
				Say(args, "BossReset: this removes " + text + ((bossFight.Minions.Count > 0) ? $" and {bossFight.Minions.Count} hostile creature(s) around the arena." : "."));
				if (text2 != null)
				{
					Say(args, "  You get back: " + text2 + ".");
				}
				else if ((Object)(object)bossFight.Altar == (Object)null)
				{
					Say(args, "  No altar found near where the boss spawned, so nothing is given back.");
				}
				else
				{
					Say(args, "  Nothing to give back (the altar's items are still in place).");
				}
				Say(args, "  Type 'bossreset confirm' to do it.");
				return;
			}
			int num2 = bossFight.Minions.Count(BossFight.Remove);
			if (!BossFight.Remove(bossFight.Boss))
			{
				Say(args, "BossReset: couldn't remove " + text + ", nothing given back. See the BepInEx log.");
				return;
			}
			foreach (KeyValuePair<ItemDrop, int> item in bossFight.GiveBack)
			{
				BossFight.Give(localPlayer, item.Key, item.Value);
			}
			Say(args, "BossReset: removed " + text + ((num2 > 0) ? $" and {num2} creature(s)" : "") + ((text2 != null) ? (". Gave back: " + text2 + ".") : "."));
			BossResetPlugin.Log.LogInfo((object)($"Reset {Utils.GetPrefabName(((Component)bossFight.Boss).gameObject)}: {num2} creature(s) removed, " + "gave back " + (text2 ?? "nothing") + "."));
		}

		private static void Say(ConsoleEventArgs args, string text)
		{
			if ((Object)(object)args?.Context != (Object)null)
			{
				args.Context.AddString(text);
			}
			else
			{
				BossResetPlugin.Log.LogInfo((object)text);
			}
		}
	}
	[BepInPlugin("com.koendertjh.bossreset", "BossReset", "1.0.0")]
	public class BossResetPlugin : BaseUnityPlugin
	{
		public const string ModGuid = "com.koendertjh.bossreset";

		public const string ModName = "BossReset";

		public const string ModVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<float> BossRange;

		internal static ConfigEntry<float> MinionRange;

		internal static ConfigEntry<bool> RemoveMinions;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			BossRange = ((BaseUnityPlugin)this).Config.Bind<float>("Reset", "BossRange", 150f, "How close (meters) a living boss has to be for bossreset to find it.");
			RemoveMinions = ((BaseUnityPlugin)this).Config.Bind<bool>("Reset", "RemoveMinions", true, "Also remove the hostile creatures around the arena (the boss's minions).");
			MinionRange = ((BaseUnityPlugin)this).Config.Bind<float>("Reset", "MinionRange", 80f, "Hostile creatures within this many meters of the arena are removed with the boss.");
			Commands.Register();
		}
	}
	internal static class PluginInfo
	{
		public const string Version = "1.0.0";
	}
}