Decompiled source of WardEventBlocker v1.0.0

bbar.Mods.WardEventBlocker.dll

Decompiled 4 months 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 UnityEngine;
using WardEventBlocker.Support;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("bbar.Mods.WardEventBlocker")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Ward Event Blocker")]
[assembly: AssemblyTitle("bbar.Mods.WardEventBlocker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace WardEventBlocker
{
	[BepInPlugin("bbar.Mods.WardEventBlocker", "Ward Event Blocker", "1.0.0")]
	public class VWEB : BaseUnityPlugin
	{
		private Harmony _harmony;

		private ConfigEntry<bool> _flashWards;

		private ConfigEntry<float> _wardRadius;

		public static VWEB Instance { get; private set; }

		public bool Initialized { get; private set; }

		public bool FlashWards => _flashWards.Value;

		public float WardRadius => _wardRadius.Value;

		internal ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			_flashWards = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FlashWardsOnEventBlocked", false, "Whether or not wards should flash when they hide a player from the random event system.");
			_wardRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ProtectionRadius", 32f, new ConfigDescription("The radius for wards to hide a player from a random event. This defaults to the vanilla Valheim ward radius. Note that changing this has no effect on the range of the vanilla ward functionality.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 200f), Array.Empty<object>()));
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony = new Harmony("bbar.Mods.WardEventBlocker");
			_harmony.PatchAll(executingAssembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin bbar.Mods.WardEventBlocker is loaded!");
			Initialized = true;
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "bbar.Mods.WardEventBlocker";

		public const string PLUGIN_NAME = "Ward Event Blocker";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace WardEventBlocker.Support
{
	public static class StableHashes
	{
		public static readonly int WardPrefab = StringExtensionMethods.GetStableHashCode("guard_stone");
	}
	public static class WardHelper
	{
		public static IList<WardHolder> GetEnabledWardsAroundPlayers()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			List<Vector2i> list = (from zdo in ZNet.instance.GetAllCharacterZDOS()
				select zdo.GetSector()).Distinct().ToList();
			List<ZDO> list2 = new List<ZDO>();
			foreach (Vector2i item in list)
			{
				ZDOMan.instance.FindSectorObjects(item, 1, 0, list2, (List<ZDO>)null);
			}
			return (from zdo in list2
				where zdo.GetPrefab() == StableHashes.WardPrefab
				where zdo.GetBool(ZDOVars.s_enabled, false)
				select zdo).Select(delegate(ZDO zdo)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				//IL_0018: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				WardHolder result = default(WardHolder);
				result.Position = zdo.GetPosition();
				result.Uid = zdo.m_uid;
				return result;
			}).Distinct().ToList();
		}

		public static bool IsInside(Vector3 center, Vector3 target)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return Utils.DistanceXZ(center, target) < VWEB.Instance.WardRadius;
		}
	}
	public struct WardHolder : IEqualityComparer<WardHolder>
	{
		public Vector3 Position;

		public ZDOID Uid;

		public bool Equals(WardHolder x, WardHolder y)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			return ((ZDOID)(ref x.Uid)).Equals(y.Uid);
		}

		public int GetHashCode(WardHolder obj)
		{
			return ((object)(ZDOID)(ref obj.Uid)).GetHashCode();
		}
	}
}
namespace WardEventBlocker.Patches
{
	[HarmonyPatch(typeof(RandEventSystem))]
	public class RandEventSystemPatch
	{
		[HarmonyPatch("GetPossibleRandomEvents")]
		[HarmonyPostfix]
		protected static List<KeyValuePair<RandomEvent, Vector3>> FilterEventsWithinWards(List<KeyValuePair<RandomEvent, Vector3>> originalPoints)
		{
			//IL_00c4: 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)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			if (!ZNet.instance.IsServer())
			{
				return originalPoints;
			}
			IList<WardHolder> enabledWardsAroundPlayers = WardHelper.GetEnabledWardsAroundPlayers();
			VWEB.Instance.Log.LogDebug((object)$"Checking {originalPoints.Count} possible event locations against {enabledWardsAroundPlayers.Count} enabled wards.");
			if (enabledWardsAroundPlayers.Count < 1)
			{
				return originalPoints;
			}
			List<KeyValuePair<RandomEvent, Vector3>> list = new List<KeyValuePair<RandomEvent, Vector3>>();
			List<ZDOID> list2 = new List<ZDOID>();
			foreach (KeyValuePair<RandomEvent, Vector3> point in originalPoints)
			{
				List<WardHolder> list3 = enabledWardsAroundPlayers.Where((WardHolder pa) => WardHelper.IsInside(pa.Position, point.Value)).ToList();
				if (list3.Count <= 0)
				{
					list.Add(point);
					continue;
				}
				VWEB.Instance.Log.LogDebug((object)$"Removed {point.Value} from list of potential event locations.");
				list2.AddRange(list3.Select((WardHolder pa) => pa.Uid));
			}
			if (VWEB.Instance.FlashWards)
			{
				foreach (ZDOID item in list2.Distinct())
				{
					VWEB.Instance.Log.LogDebug((object)$"Flashing ward {item}");
					ZRoutedRpc.instance.InvokeRoutedRPC(ZNetView.Everybody, item, "FlashShield", Array.Empty<object>());
				}
			}
			VWEB.Instance.Log.LogInfo((object)"Successfully filtered random events.");
			return list;
		}
	}
}