Decompiled source of BossGroupAffix v1.0.1

Cjayride.BossGroupAffix.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.BossGroupAffix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+92db485eed101d2201a1ea018c36e71c15f7a9e1")]
[assembly: AssemblyProduct("Cjayride.BossGroupAffix")]
[assembly: AssemblyTitle("Cjayride.BossGroupAffix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[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 Cjayride.BossGroupAffix
{
	[HarmonyPatch(typeof(Character), "Awake")]
	internal static class Character_Awake_Patch
	{
		private static void Postfix(Character __instance)
		{
			if (Object.op_Implicit((Object)(object)__instance) && __instance.IsBoss() && !Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<AffixWatcher>()))
			{
				((Component)__instance).gameObject.AddComponent<AffixWatcher>();
			}
		}
	}
	internal sealed class AffixWatcher : MonoBehaviour
	{
		private const string ZdoLocked = "cjayride.BossGroupAffix.Locked";

		private const float Interval = 2f;

		private static readonly List<Player> Nearby = new List<Player>();

		private Character _boss;

		private float _timer;

		private void Awake()
		{
			_boss = ((Component)this).GetComponent<Character>();
		}

		private void Update()
		{
			_timer += Time.deltaTime;
			if (!(_timer < 2f))
			{
				_timer = 0f;
				Check();
			}
		}

		private void Check()
		{
			if (!Plugin.Enabled.Value || !Object.op_Implicit((Object)(object)_boss) || _boss.IsDead() || !Object.op_Implicit((Object)(object)_boss.m_nview) || !_boss.m_nview.IsValid() || !_boss.m_nview.IsOwner())
			{
				return;
			}
			ZDO zDO = _boss.m_nview.GetZDO();
			if (zDO.GetBool("cjayride.BossGroupAffix.Locked", false))
			{
				((Behaviour)this).enabled = false;
				return;
			}
			if (_boss.GetHealth() < _boss.GetMaxHealth())
			{
				zDO.Set("cjayride.BossGroupAffix.Locked", true);
				((Behaviour)this).enabled = false;
				return;
			}
			string text = Cllc.Get(_boss);
			if (!(text == "Twin") && !(text == "Godlike"))
			{
				string text2 = ((CountPlayers() <= 1) ? Plugin.SoloAffix.Value : Plugin.GroupAffix.Value);
				if (Cllc.IsValid(text2) && !(text2 == text))
				{
					Cllc.Set(_boss, text2);
					Plugin.Log.LogInfo((object)("BossGroupAffix: " + _boss.m_name + " affix " + text + " -> " + text2 + "."));
				}
			}
		}

		private int CountPlayers()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			Nearby.Clear();
			Player.GetPlayersInRange(((Component)_boss).transform.position, Plugin.PlayerRange.Value, Nearby);
			int num = 0;
			foreach (Player item in Nearby)
			{
				if (Object.op_Implicit((Object)(object)item) && !((Character)item).IsDead())
				{
					num++;
				}
			}
			Nearby.Clear();
			return num;
		}
	}
	[BepInPlugin("cjayride.bossgroupaffix", "BossGroupAffix", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "cjayride.bossgroupaffix";

		public const string PluginName = "BossGroupAffix";

		public const string PluginVersion = "1.0.1";

		internal const string CllcGuid = "org.bepinex.plugins.creaturelevelcontrol";

		internal static Plugin Instance;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<string> SoloAffix;

		internal static ConfigEntry<string> GroupAffix;

		internal static ConfigEntry<float> PlayerRange;

		internal static ManualLogSource Log => ((BaseUnityPlugin)Instance).Logger;

		private void Awake()
		{
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Pick the boss affix from how many players are near the boss before the fight starts.");
			SoloAffix = ((BaseUnityPlugin)this).Config.Bind<string>("General", "SoloAffix", "Mending", "Creature Level & Loot Control affix for a boss with one player nearby. One of: None, Reflective, Shielded, Mending, Summoner, Elementalist, Enraged, Sturdy.");
			GroupAffix = ((BaseUnityPlugin)this).Config.Bind<string>("General", "GroupAffix", "Summoner", "Affix for a boss with two or more players nearby. Same choices as SoloAffix.");
			PlayerRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PlayerRange", 400f, "Meters from the boss. Players this close count toward solo or group.");
			if (!Cllc.Init())
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"BossGroupAffix: Creature Level & Loot Control API not found; boss affixes are left alone.");
			}
			else
			{
				new Harmony("cjayride.bossgroupaffix").PatchAll(Assembly.GetExecutingAssembly());
			}
		}
	}
	internal static class Cllc
	{
		private static Type _affixType;

		private static MethodInfo _get;

		private static MethodInfo _set;

		internal static bool Init()
		{
			Type type = AccessTools.TypeByName("CreatureLevelControl.API");
			_affixType = AccessTools.TypeByName("CreatureLevelControl.BossAffix");
			if (type == null || _affixType == null)
			{
				return false;
			}
			_get = AccessTools.Method(type, "GetAffixBoss", new Type[1] { typeof(Character) }, (Type[])null);
			_set = AccessTools.Method(type, "SetAffixBoss", new Type[2]
			{
				typeof(Character),
				_affixType
			}, (Type[])null);
			if (_get != null)
			{
				return _set != null;
			}
			return false;
		}

		internal static string Get(Character boss)
		{
			return _get.Invoke(null, new object[1] { boss })?.ToString() ?? "None";
		}

		internal static void Set(Character boss, string affix)
		{
			_set.Invoke(null, new object[2]
			{
				boss,
				Enum.Parse(_affixType, affix)
			});
		}

		internal static bool IsValid(string affix)
		{
			if (string.IsNullOrEmpty(affix) || affix == "Twin" || affix == "Godlike")
			{
				return false;
			}
			try
			{
				Enum.Parse(_affixType, affix);
				return true;
			}
			catch (ArgumentException)
			{
				return false;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}