Decompiled source of ValheimWardBarrier v1.0.3

ValheimWardBarrier1.0.3.dll

Decompiled 2 months ago
using System.Collections;
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 HarmonyLib;
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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace ValheimHostileBarrier;

[BepInPlugin("com.yourname.valheim.wardbarrier", "ValheimWardBarrier", "1.5.2")]
[BepInProcess("valheim.exe")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class HostileBarrierMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Projectile), "FixedUpdate")]
	public static class Patch_Projectile_FixedUpdate
	{
		public static void Postfix(Projectile __instance)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			FieldInfo field = typeof(PrivateArea).GetField("m_allAreas", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null || !(field.GetValue(null) is IEnumerable enumerable))
			{
				return;
			}
			FieldInfo field2 = typeof(PrivateArea).GetField("m_enabled", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			Vector3 position = ((Component)__instance).transform.position;
			foreach (object item in enumerable)
			{
				PrivateArea val = (PrivateArea)((item is PrivateArea) ? item : null);
				if ((Object)(object)val == (Object)null || (field2 != null && !(bool)field2.GetValue(val)))
				{
					continue;
				}
				float num = (useWardRadius.Value ? val.m_radius : customBarrierRadius.Value);
				if (Vector3.Distance(position, ((Component)val).transform.position) < num)
				{
					TriggerShieldFlashSafely(val);
					ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
					if ((Object)(object)component != (Object)null && component.IsValid())
					{
						component.Destroy();
					}
					else
					{
						Object.Destroy((Object)(object)((Component)__instance).gameObject);
					}
					break;
				}
			}
		}
	}

	[HarmonyPatch(typeof(Character), "CustomFixedUpdate")]
	public static class Patch_Character_FixedUpdate
	{
		public static void Postfix(Character __instance, float dt)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: 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_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: 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)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || __instance.IsPlayer() || __instance.IsTamed() || (int)__instance.m_faction == 0)
			{
				return;
			}
			FieldInfo field = typeof(PrivateArea).GetField("m_allAreas", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null || !(field.GetValue(null) is IEnumerable enumerable))
			{
				return;
			}
			FieldInfo field2 = typeof(PrivateArea).GetField("m_enabled", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			Vector3 position = ((Component)__instance).transform.position;
			foreach (object item in enumerable)
			{
				PrivateArea val = (PrivateArea)((item is PrivateArea) ? item : null);
				if ((Object)(object)val == (Object)null || (field2 != null && !(bool)field2.GetValue(val)))
				{
					continue;
				}
				float num = (useWardRadius.Value ? val.m_radius : customBarrierRadius.Value);
				if (!(Vector3.Distance(position, ((Component)val).transform.position) < num))
				{
					continue;
				}
				Vector3 val2 = position - ((Component)val).transform.position;
				Vector3 normalized = ((Vector3)(ref val2)).normalized;
				normalized.y = 0f;
				TriggerShieldFlashSafely(val);
				Rigidbody component = ((Component)__instance).GetComponent<Rigidbody>();
				if ((Object)(object)component != (Object)null)
				{
					component.linearVelocity = normalized * pushbackForce.Value;
				}
				else
				{
					Transform transform = ((Component)__instance).transform;
					transform.position += normalized * pushbackForce.Value * dt;
				}
				ZDOID zDOID = __instance.GetZDOID();
				float time = Time.time;
				if (!soundCooldownTimers.TryGetValue(zDOID, out var value) || time - value > audioCooldown.Value)
				{
					TriggerNativeStaggerEffect(position, ((Component)__instance).transform);
					soundCooldownTimers[zDOID] = time;
				}
				BaseAI component2 = ((Component)__instance).GetComponent<BaseAI>();
				if ((Object)(object)component2 != (Object)null)
				{
					MethodInfo method = typeof(BaseAI).GetMethod("ResetTarget", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (method != null)
					{
						method.Invoke(component2, null);
					}
				}
				break;
			}
		}
	}

	private const string PluginGUID = "com.yourname.valheim.wardbarrier";

	private const string PluginName = "ValheimWardBarrier";

	private const string PluginVersion = "1.5.2";

	public static ConfigEntry<float> pushbackForce;

	public static ConfigEntry<bool> useWardRadius;

	public static ConfigEntry<float> customBarrierRadius;

	public static ConfigEntry<float> audioCooldown;

	public static ConfigEntry<float> shieldFlashCooldown;

	public static Dictionary<ZDOID, float> soundCooldownTimers = new Dictionary<ZDOID, float>();

	private static Dictionary<PrivateArea, float> shieldFlashTimers = new Dictionary<PrivateArea, float>();

	private static Harmony harmonyInstance;

	private void Awake()
	{
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Expected O, but got Unknown
		pushbackForce = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PushbackForce", 25f, "Repulsion velocity applied to hostiles attempting to cross the perimeter.");
		useWardRadius = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UseWardRadius", true, "If true, matches the default in-game Ward radius size.");
		customBarrierRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CustomBarrierRadius", 32f, "Override radius applied if UseWardRadius is disabled.");
		audioCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "AudioCooldown", 1.5f, "Throttling threshold to suppress stagger audio effect spam.");
		shieldFlashCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Visual", "ShieldFlashCooldown", 4f, "How many seconds to wait before a ward can flash its blue shield visual again.");
		harmonyInstance = new Harmony("com.yourname.valheim.wardbarrier");
		harmonyInstance.PatchAll(Assembly.GetExecutingAssembly());
		((BaseUnityPlugin)this).Logger.LogInfo((object)"ValheimWardBarrier version 1.5.2 established with audio/visual adjustments.");
	}

	public static void TriggerShieldFlashSafely(PrivateArea ward)
	{
		if ((Object)(object)ward == (Object)null)
		{
			return;
		}
		float time = Time.time;
		if (!shieldFlashTimers.TryGetValue(ward, out var value) || time - value > shieldFlashCooldown.Value)
		{
			MethodInfo method = typeof(PrivateArea).GetMethod("FlashShield", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method != null)
			{
				method.Invoke(ward, new object[1] { false });
			}
			shieldFlashTimers[ward] = time;
		}
	}

	public static void TriggerNativeStaggerEffect(Vector3 position, Transform parent)
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)ZNetScene.instance == (Object)null))
		{
			GameObject prefab = ZNetScene.instance.GetPrefab("vfx_stagger");
			if ((Object)(object)prefab != (Object)null)
			{
				Object.Instantiate<GameObject>(prefab, position, Quaternion.identity, parent);
			}
		}
	}
}