Decompiled source of Hearthveil v1.3.0

plugins/Hearthveil.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Hearthveil;

[BepInPlugin("com.spawnshield.valheim", "Hearthveil", "1.3.0")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static class NetworkPrefabPatch
	{
		private static void Postfix(ZNetScene __instance, Dictionary<int, GameObject> ___m_namedPrefabs)
		{
			instance.RegisterPrefab(__instance, ___m_namedPrefabs);
		}
	}

	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	private static class BuildMenuPatch
	{
		private static void Postfix(ObjectDB __instance)
		{
			if (Object.op_Implicit((Object)(object)ZNetScene.instance))
			{
				instance.RegisterRecipe(__instance);
			}
		}
	}

	public const string Id = "com.spawnshield.valheim";

	internal const string PrefabName = "SpawnShield_Ward";

	private static Plugin instance;

	private static ConfigEntry<float> radius;

	private static ConfigEntry<bool> showDome;

	private Harmony harmony;

	private GameObject prefabRoot;

	private GameObject wardPrefab;

	private WardAppearance appearance;

	private AudioClip powerOnSound;

	private AudioClip powerOffSound;

	internal static float Radius
	{
		get
		{
			float value = radius.Value;
			if (!float.IsNaN(value))
			{
				return Mathf.Clamp(value, 10f, 80f);
			}
			return 40f;
		}
	}

	internal static bool ShowDome => showDome.Value;

	internal static void ToggleDome()
	{
		showDome.Value = !showDome.Value;
	}

	internal static void SetModelPower(MeshRenderer renderer, bool powered)
	{
		instance.appearance?.SetPower(renderer, powered);
	}

	internal static AudioClip GetPowerSound(bool powered)
	{
		if (powered)
		{
			if (!Object.op_Implicit((Object)(object)instance.powerOnSound))
			{
				instance.powerOnSound = CreatePowerSound("Hearthveil power on", 392f, 659.25f);
			}
			return instance.powerOnSound;
		}
		if (!Object.op_Implicit((Object)(object)instance.powerOffSound))
		{
			instance.powerOffSound = CreatePowerSound("Hearthveil power off", 523.25f, 261.63f);
		}
		return instance.powerOffSound;
	}

	private static AudioClip CreatePowerSound(string clipName, float firstNote, float secondNote)
	{
		float[] array = new float[12000];
		for (int i = 0; i < array.Length; i++)
		{
			float num = (float)i / 24000f;
			array[i] = ChimeNote(num, firstNote, 12f) + ChimeNote(num - 0.115f, secondNote, 10f);
		}
		AudioClip obj = AudioClip.Create(clipName, array.Length, 1, 24000, false);
		obj.SetData(array, 0);
		return obj;
	}

	private static float ChimeNote(float time, float frequency, float decay)
	{
		if (time < 0f)
		{
			return 0f;
		}
		float num = (float)Math.PI * 2f * frequency * time;
		float num2 = Mathf.Clamp01(time / 0.008f);
		float num3 = Mathf.Clamp01((0.38f - time) / 0.065f);
		return 0.27f * num2 * num3 * Mathf.Exp((0f - decay) * time) * (Mathf.Sin(num) + 0.3f * Mathf.Sin(2f * num) + 0.12f * Mathf.Sin(3.01f * num));
	}

	private void Awake()
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Expected O, but got Unknown
		instance = this;
		radius = ((BaseUnityPlugin)this).Config.Bind<float>("Field", "Radius", 40f, new ConfigDescription("Horizontal ward protection radius in meters. Use the same value on every multiplayer peer.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 80f), Array.Empty<object>()));
		showDome = ((BaseUnityPlugin)this).Config.Bind<bool>("Field", "ShowDome", true, "Show the blue shield boundary and arches on this client.");
		harmony = new Harmony("com.spawnshield.valheim");
		try
		{
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Hearthveil loaded.");
		}
		catch (Exception)
		{
			harmony.UnpatchSelf();
			throw;
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
		if (Object.op_Implicit((Object)(object)prefabRoot))
		{
			Object.Destroy((Object)(object)prefabRoot);
		}
		appearance?.Dispose();
		if (Object.op_Implicit((Object)(object)powerOnSound))
		{
			Object.Destroy((Object)(object)powerOnSound);
		}
		if (Object.op_Implicit((Object)(object)powerOffSound))
		{
			Object.Destroy((Object)(object)powerOffSound);
		}
	}

	private void RegisterPrefab(ZNetScene scene, Dictionary<int, GameObject> prefabs)
	{
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Expected O, but got Unknown
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		int stableHashCode = StringExtensionMethods.GetStableHashCode("SpawnShield_Ward");
		if (prefabs.ContainsKey(stableHashCode))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"The Hearthveil prefab hash is already in use.");
			return;
		}
		if (!Object.op_Implicit((Object)(object)wardPrefab))
		{
			GameObject prefab = scene.GetPrefab("guard_stone");
			if (!Object.op_Implicit((Object)(object)prefab))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not find the vanilla ward prefab (guard_stone).");
				return;
			}
			prefabRoot = new GameObject("Hearthveil prefabs");
			prefabRoot.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)prefabRoot);
			wardPrefab = Object.Instantiate<GameObject>(prefab, prefabRoot.transform, false);
			((Object)wardPrefab).name = "SpawnShield_Ward";
			wardPrefab.SetActive(true);
			PrivateArea[] componentsInChildren = wardPrefab.GetComponentsInChildren<PrivateArea>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
			}
			EffectArea[] componentsInChildren2 = wardPrefab.GetComponentsInChildren<EffectArea>(true);
			foreach (EffectArea val in componentsInChildren2)
			{
				Collider[] components = ((Component)val).GetComponents<Collider>();
				foreach (Collider val2 in components)
				{
					if (val2.isTrigger)
					{
						val2.enabled = false;
					}
				}
				Object.DestroyImmediate((Object)(object)val);
			}
			Piece component = wardPrefab.GetComponent<Piece>();
			component.m_name = "Hearthveil Ward";
			component.m_description = "Blocks hostile spawns, calms hostile creatures, and protects nearby player-built pieces.";
			component.m_category = (PieceCategory)0;
			component.m_usage = (UsageTagFlags)32769;
			appearance = new WardAppearance();
			try
			{
				appearance.Apply(wardPrefab, component);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not load the custom ward artwork: " + ex.Message));
			}
			wardPrefab.AddComponent<ShieldField>();
		}
		scene.m_prefabs.Add(wardPrefab);
		prefabs.Add(stableHashCode, wardPrefab);
		RegisterRecipe(ObjectDB.instance);
	}

	private void RegisterRecipe(ObjectDB database)
	{
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Expected O, but got Unknown
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Expected O, but got Unknown
		//IL_00f7: 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_0108: 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_0117: Expected O, but got Unknown
		if (!Object.op_Implicit((Object)(object)database) || !Object.op_Implicit((Object)(object)wardPrefab))
		{
			return;
		}
		GameObject itemPrefab = database.GetItemPrefab("Hammer");
		GameObject itemPrefab2 = database.GetItemPrefab("Wood");
		GameObject itemPrefab3 = database.GetItemPrefab("Stone");
		GameObject itemPrefab4 = database.GetItemPrefab("GreydwarfEye");
		GameObject prefab = ZNetScene.instance.GetPrefab("piece_workbench");
		if (!Object.op_Implicit((Object)(object)itemPrefab) || !Object.op_Implicit((Object)(object)itemPrefab2) || !Object.op_Implicit((Object)(object)itemPrefab3) || !Object.op_Implicit((Object)(object)itemPrefab4) || !Object.op_Implicit((Object)(object)prefab))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Hearthveil recipe registration is waiting for vanilla build assets.");
			return;
		}
		Piece component = wardPrefab.GetComponent<Piece>();
		component.m_craftingStation = prefab.GetComponent<CraftingStation>();
		component.m_resources = (Requirement[])(object)new Requirement[3]
		{
			new Requirement
			{
				m_resItem = itemPrefab2.GetComponent<ItemDrop>(),
				m_amount = 10,
				m_recover = true
			},
			new Requirement
			{
				m_resItem = itemPrefab3.GetComponent<ItemDrop>(),
				m_amount = 10,
				m_recover = true
			},
			new Requirement
			{
				m_resItem = itemPrefab4.GetComponent<ItemDrop>(),
				m_amount = 5,
				m_recover = true
			}
		};
		PieceTable buildPieces = itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces;
		if (!buildPieces.m_pieces.Contains(wardPrefab))
		{
			buildPieces.m_pieces.Add(wardPrefab);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Registered Hearthveil Ward in the Hammer build menu.");
		}
	}
}
public sealed class ShieldField : MonoBehaviour, Hoverable, Interactable
{
	private const string PowerRpc = "SpawnShield_SetPower";

	private static readonly int PowerKey = StringExtensionMethods.GetStableHashCode("SpawnShield_Powered");

	private static readonly HashSet<ShieldField> PlacedWards = new HashSet<ShieldField>();

	private static readonly int WardHash = StringExtensionMethods.GetStableHashCode("SpawnShield_Ward");

	private ZNetView view;

	private MeshRenderer modelRenderer;

	private Light crystalLight;

	private AudioSource powerAudio;

	private bool? shownPower;

	private GameObject visual;

	private Material material;

	private float drawnRadius;

	private bool IsPowered
	{
		get
		{
			if (Object.op_Implicit((Object)(object)view) && view.IsValid())
			{
				return view.GetZDO().GetBool(PowerKey, true);
			}
			return false;
		}
	}

	private void Start()
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Invalid comparison between Unknown and I4
		view = ((Component)this).GetComponent<ZNetView>();
		if (!Object.op_Implicit((Object)(object)view) || !view.IsValid() || view.GetZDO().GetPrefab() != WardHash)
		{
			((Behaviour)this).enabled = false;
			return;
		}
		PlacedWards.Add(this);
		view.Register<bool>("SpawnShield_SetPower", (Action<long, bool>)OnPowerRequested);
		if ((int)SystemInfo.graphicsDeviceType == 4)
		{
			return;
		}
		Transform val = ((Component)this).transform.Find("Hearthveil model");
		if (Object.op_Implicit((Object)(object)val))
		{
			modelRenderer = ((Component)val).GetComponent<MeshRenderer>();
			Transform val2 = val.Find("Hearthveil glow");
			if (Object.op_Implicit((Object)(object)val2))
			{
				crystalLight = ((Component)val2).GetComponent<Light>();
			}
		}
		RefreshState();
		((MonoBehaviour)this).InvokeRepeating("RefreshState", 0.2f, 0.2f);
	}

	private void OnDestroy()
	{
		PlacedWards.Remove(this);
		if (Object.op_Implicit((Object)(object)material))
		{
			Object.Destroy((Object)(object)material);
		}
	}

	public string GetHoverName()
	{
		return "Hearthveil Ward";
	}

	public float GetHoverOffset()
	{
		return 0f;
	}

	public string GetHoverText()
	{
		if (!Object.op_Implicit((Object)(object)view) || !view.IsValid())
		{
			return "";
		}
		string text = (IsPowered ? "Powered" : "Off");
		string text2 = (IsPowered ? "Turn off" : "Turn on");
		string text3 = (Plugin.ShowDome ? "Hide shield radius" : "Show shield radius");
		return Localization.instance.Localize("Hearthveil Ward (" + text + ")\n[<color=yellow><b>$KEY_Use</b></color>] " + text2 + "\n[<color=yellow><b>$KEY_AltPlace + $KEY_Use</b></color>] " + text3);
	}

	public bool Interact(Humanoid user, bool hold, bool alt)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Invalid comparison between Unknown and I4
		if (hold || !Object.op_Implicit((Object)(object)user) || !Object.op_Implicit((Object)(object)view) || !view.IsValid())
		{
			return false;
		}
		if (alt)
		{
			Plugin.ToggleDome();
			if ((int)SystemInfo.graphicsDeviceType != 4)
			{
				foreach (ShieldField placedWard in PlacedWards)
				{
					if (Object.op_Implicit((Object)(object)placedWard) && ((Behaviour)placedWard).isActiveAndEnabled)
					{
						placedWard.UpdateVisual();
					}
				}
			}
			return true;
		}
		bool flag = !IsPowered;
		if (view.IsOwner())
		{
			OnPowerRequested(0L, flag);
		}
		else
		{
			view.InvokeRPC("SpawnShield_SetPower", new object[1] { flag });
		}
		return true;
	}

	public bool UseItem(Humanoid user, ItemData item)
	{
		return false;
	}

	private void OnPowerRequested(long sender, bool powered)
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Invalid comparison between Unknown and I4
		if (Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner() && IsPowered != powered)
		{
			view.GetZDO().Set(PowerKey, powered);
			if ((int)SystemInfo.graphicsDeviceType != 4)
			{
				RefreshState();
			}
		}
	}

	internal static bool Blocks(GameObject prefab, Vector3 point)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)prefab) && Covers(point))
		{
			return IsHostile(prefab.GetComponent<Character>());
		}
		return false;
	}

	internal static bool Covers(Vector3 point)
	{
		//IL_0041: 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)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: 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)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		if (PlacedWards.Count == 0)
		{
			return false;
		}
		float radius = Plugin.Radius;
		foreach (ShieldField placedWard in PlacedWards)
		{
			if (Object.op_Implicit((Object)(object)placedWard) && ((Behaviour)placedWard).isActiveAndEnabled && placedWard.IsPowered)
			{
				Vector3 val = point - ((Component)placedWard).transform.position;
				if (Mathf.Abs(val.y) <= radius && val.x * val.x + val.z * val.z <= radius * radius)
				{
					return true;
				}
			}
		}
		return false;
	}

	internal static bool IsHostile(Character creature)
	{
		//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)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Invalid comparison between Unknown and I4
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Invalid comparison between Unknown and I4
		if (!Object.op_Implicit((Object)(object)creature) || Object.op_Implicit((Object)(object)((Component)creature).GetComponent<AnimalAI>()))
		{
			return false;
		}
		Faction faction = creature.m_faction;
		if ((int)faction <= 1 || faction - 10 <= 2)
		{
			return false;
		}
		return true;
	}

	private void RefreshState()
	{
		bool isPowered = IsPowered;
		if (shownPower != isPowered)
		{
			bool hasValue = shownPower.HasValue;
			shownPower = isPowered;
			if (Object.op_Implicit((Object)(object)modelRenderer))
			{
				Plugin.SetModelPower(modelRenderer, isPowered);
			}
			if (Object.op_Implicit((Object)(object)crystalLight))
			{
				((Behaviour)crystalLight).enabled = isPowered;
			}
			if (hasValue)
			{
				PlayPowerSound(isPowered);
			}
		}
		UpdateVisual();
	}

	private void PlayPowerSound(bool powered)
	{
		if (!Object.op_Implicit((Object)(object)powerAudio))
		{
			powerAudio = ((Component)this).gameObject.AddComponent<AudioSource>();
			powerAudio.playOnAwake = false;
			powerAudio.spatialBlend = 1f;
			powerAudio.dopplerLevel = 0f;
			powerAudio.rolloffMode = (AudioRolloffMode)1;
			powerAudio.minDistance = 2f;
			powerAudio.maxDistance = 20f;
		}
		powerAudio.clip = Plugin.GetPowerSound(powered);
		powerAudio.volume = 0.8f * (Object.op_Implicit((Object)(object)AudioMan.instance) ? AudioMan.GetSFXVolume() : 1f);
		powerAudio.Play();
	}

	private void UpdateVisual()
	{
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		if (!IsPowered || !Plugin.ShowDome)
		{
			if (Object.op_Implicit((Object)(object)visual))
			{
				visual.SetActive(false);
			}
			return;
		}
		float radius = Plugin.Radius;
		if (Object.op_Implicit((Object)(object)visual) && drawnRadius == radius)
		{
			visual.SetActive(true);
			return;
		}
		if (!Object.op_Implicit((Object)(object)material))
		{
			Shader val = Shader.Find("Sprites/Default");
			if (!Object.op_Implicit((Object)(object)val))
			{
				val = Shader.Find("Legacy Shaders/Particles/Alpha Blended");
			}
			if (!Object.op_Implicit((Object)(object)val))
			{
				return;
			}
			material = new Material(val);
		}
		if (Object.op_Implicit((Object)(object)visual))
		{
			Object.Destroy((Object)(object)visual);
		}
		visual = new GameObject("Hearthveil dome");
		visual.transform.SetParent(((Component)this).transform, false);
		drawnRadius = radius;
		Vector3[] array = (Vector3[])(object)new Vector3[96];
		float num = 0.35f;
		for (int i = 0; i < array.Length; i++)
		{
			float num2 = (float)i * 2f * (float)Math.PI / (float)array.Length;
			array[i] = new Vector3(radius * Mathf.Cos(num2), num, radius * Mathf.Sin(num2));
		}
		AddLine("Boundary", array, loop: true, 0.18f, 0.8f);
		for (int j = 0; j < 4; j++)
		{
			float num3 = (float)j * (float)Math.PI / 4f;
			for (int k = 0; k < array.Length; k++)
			{
				float num4 = (float)k * (float)Math.PI / (float)(array.Length - 1);
				float num5 = radius * Mathf.Cos(num4);
				array[k] = new Vector3(num5 * Mathf.Cos(num3), num + radius * Mathf.Sin(num4), num5 * Mathf.Sin(num3));
			}
			AddLine("Arch", array, loop: false, 0.14f, 0.44f);
		}
	}

	private void AddLine(string lineName, Vector3[] points, bool loop, float width, float alpha)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(lineName);
		val.transform.SetParent(visual.transform, false);
		LineRenderer obj = val.AddComponent<LineRenderer>();
		obj.useWorldSpace = false;
		obj.loop = loop;
		((Renderer)obj).sharedMaterial = material;
		((Renderer)obj).shadowCastingMode = (ShadowCastingMode)0;
		((Renderer)obj).receiveShadows = false;
		float startWidth = (obj.endWidth = width);
		obj.startWidth = startWidth;
		Color val2 = default(Color);
		((Color)(ref val2))..ctor(0.25f, 0.8f, 1f, alpha);
		obj.endColor = val2;
		obj.startColor = val2;
		obj.positionCount = points.Length;
		obj.SetPositions(points);
	}
}
internal static class CreaturePassivity
{
	[HarmonyPatch(typeof(MonsterAI), "UpdateTarget")]
	private static class Targeting
	{
		private static readonly Action<BaseAI, bool> SetAlerted = AccessTools.MethodDelegate<Action<BaseAI, bool>>(AccessTools.Method(typeof(BaseAI), "SetAlerted", (Type[])null, (Type[])null), (object)null, true);

		private static readonly Action<BaseAI, ZDOID> SetTargetInfo = AccessTools.MethodDelegate<Action<BaseAI, ZDOID>>(AccessTools.Method(typeof(BaseAI), "SetTargetInfo", (Type[])null, (Type[])null), (object)null, true);

		private static bool Prefix(MonsterAI __instance, Character ___m_character, ZNetView ___m_nview, ref bool __2, ref bool __3, ref Character ___m_targetCreature, ref StaticTarget ___m_targetStatic, ref float ___m_updateTargetTimer, ref float ___m_timeSinceSensedTargetCreature)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!IsCalmed(___m_character, ___m_nview))
			{
				return true;
			}
			___m_targetCreature = null;
			___m_targetStatic = null;
			___m_updateTargetTimer = 0f;
			___m_timeSinceSensedTargetCreature = 0f;
			__2 = false;
			__3 = false;
			SetAlerted((BaseAI)(object)__instance, arg2: false);
			SetTargetInfo((BaseAI)(object)__instance, ZDOID.None);
			return false;
		}
	}

	[HarmonyPatch(typeof(BaseAI), "SetAlerted")]
	private static class Alerting
	{
		private static bool Prefix(BaseAI __instance, bool __0, Character ___m_character, ZNetView ___m_nview)
		{
			if (__0 && __instance is MonsterAI)
			{
				return !IsCalmed(___m_character, ___m_nview);
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Humanoid), "StartAttack")]
	private static class AttackStart
	{
		private static bool Prefix(Humanoid __instance, ZNetView ___m_nview, ref bool __result)
		{
			if (!IsCalmed((Character)(object)__instance, ___m_nview))
			{
				return true;
			}
			__result = false;
			return false;
		}
	}

	[HarmonyPatch]
	private static class AttackEffects
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return AccessTools.Method(typeof(Attack), "OnAttackTrigger", (Type[])null, (Type[])null);
			yield return AccessTools.Method(typeof(Attack), "FireProjectileBurst", (Type[])null, (Type[])null);
		}

		private static bool Prefix(Humanoid ___m_character)
		{
			if (Object.op_Implicit((Object)(object)___m_character) && !((Character)___m_character).IsPlayer())
			{
				return !IsCalmed((Character)(object)___m_character, ((Component)___m_character).GetComponent<ZNetView>());
			}
			return true;
		}
	}

	private static bool IsCalmed(Character creature, ZNetView view)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)creature) && !creature.IsPlayer() && ShieldField.Covers(((Component)creature).transform.position) && Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner() && ShieldField.IsHostile(creature))
		{
			return !creature.IsTamed();
		}
		return false;
	}
}
[HarmonyPatch(typeof(WearNTear), "RPC_Damage")]
internal static class BuildingDamage
{
	private static bool Prefix(WearNTear __instance, HitData __1, ZNetView ___m_nview)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)___m_nview) || !___m_nview.IsValid() || !___m_nview.IsOwner() || !ShieldField.Covers(((Component)__instance).transform.position))
		{
			return true;
		}
		Piece component = ((Component)__instance).GetComponent<Piece>();
		if (!Object.op_Implicit((Object)(object)component) || !component.IsPlacedByPlayer())
		{
			return true;
		}
		Character val = ((__1 != null) ? __1.GetAttacker() : null);
		if (ShieldField.IsHostile(val))
		{
			return val.IsTamed();
		}
		return true;
	}
}
[HarmonyPatch(typeof(SpawnSystem), "IsSpawnPointGood")]
internal static class WorldSpawns
{
	private static void Postfix(SpawnData __0, ref Vector3 __1, ref bool __result)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		if (__result && ShieldField.Blocks(__0.m_prefab, __1))
		{
			__result = false;
		}
	}
}
[HarmonyPatch(typeof(CreatureSpawner), "Spawn")]
internal static class CreatureSpawns
{
	private static bool Prefix(CreatureSpawner __instance, ref ZNetView __result)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		if (!ShieldField.Blocks(__instance.m_creaturePrefab, ((Component)__instance).transform.position))
		{
			return true;
		}
		__result = null;
		return false;
	}
}
[HarmonyPatch(typeof(SpawnArea), "FindSpawnPoint")]
internal static class AreaSpawns
{
	private static void Postfix(GameObject __0, ref Vector3 __1, ref bool __result)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		if (__result && ShieldField.Blocks(__0, __1))
		{
			__result = false;
		}
	}
}
internal sealed class WardAppearance : IDisposable
{
	internal const string ModelName = "Hearthveil model";

	internal const string GlowName = "Hearthveil glow";

	private Mesh mesh;

	private Material[] materials;

	private Material[] offMaterials;

	private Texture2D texture;

	private Sprite icon;

	internal void Apply(GameObject prefab, Piece piece)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Invalid comparison between Unknown and I4
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Expected O, but got Unknown
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: 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)
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		bool flag = (int)SystemInfo.graphicsDeviceType == 4;
		if (!flag)
		{
			Shader val = Shader.Find("Standard");
			if (!Object.op_Implicit((Object)(object)val) || !val.isSupported)
			{
				val = Shader.Find("Diffuse");
			}
			if (!Object.op_Implicit((Object)(object)val) || !val.isSupported)
			{
				throw new InvalidOperationException("The ward needs a supported opaque shader.");
			}
			LoadMesh(val);
			LoadIcon();
		}
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer obj in componentsInChildren)
		{
			obj.enabled = false;
			obj.forceRenderingOff = true;
		}
		Light[] componentsInChildren2 = prefab.GetComponentsInChildren<Light>(true);
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			((Behaviour)componentsInChildren2[i]).enabled = false;
		}
		Collider[] componentsInChildren3 = prefab.GetComponentsInChildren<Collider>(true);
		for (int i = 0; i < componentsInChildren3.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren3[i]);
		}
		GameObject val2 = new GameObject("Hearthveil model");
		val2.transform.SetParent(prefab.transform, false);
		int num = LayerMask.NameToLayer("piece");
		val2.layer = ((num >= 0) ? num : prefab.layer);
		AddCollider(val2, new Vector3(0f, 0.33f, 0f), new Vector3(1.23f, 0.66f, 1.23f));
		AddCollider(val2, new Vector3(0f, 1.05f, 0f), new Vector3(0.78f, 0.78f, 0.78f));
		if (!flag)
		{
			val2.AddComponent<MeshFilter>().sharedMesh = mesh;
			((Renderer)val2.AddComponent<MeshRenderer>()).sharedMaterials = materials;
			GameObject val3 = new GameObject("Hearthveil glow");
			val3.transform.SetParent(val2.transform, false);
			val3.transform.localPosition = Vector3.up * 1.37f;
			Light obj2 = val3.AddComponent<Light>();
			obj2.type = (LightType)2;
			obj2.color = new Color(0.17f, 0.7f, 1f);
			obj2.range = 3f;
			obj2.intensity = 0.85f;
			obj2.shadows = (LightShadows)0;
			piece.m_icon = icon;
		}
	}

	internal void SetPower(MeshRenderer renderer, bool powered)
	{
		((Renderer)renderer).sharedMaterials = (powered ? materials : offMaterials);
	}

	private static void AddCollider(GameObject model, Vector3 center, Vector3 size)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		BoxCollider obj = model.AddComponent<BoxCollider>();
		obj.center = center;
		obj.size = size;
	}

	private void LoadMesh(Shader shader)
	{
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Expected O, but got Unknown
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Expected O, but got Unknown
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Expected O, but got Unknown
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_023f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0244: Unknown result type (might be due to invalid IL or missing references)
		//IL_025d: Expected O, but got Unknown
		//IL_025f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0266: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: Unknown result type (might be due to invalid IL or missing references)
		//IL_027a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0287: Unknown result type (might be due to invalid IL or missing references)
		//IL_028e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
		Shader val = Shader.Find("Unlit/Color");
		if (!Object.op_Implicit((Object)(object)val) || !val.isSupported)
		{
			val = Shader.Find("Sprites/Default");
		}
		using BinaryReader binaryReader = new BinaryReader(OpenResource("Hearthveil.WardModel"));
		if (binaryReader.ReadUInt32() != 827152211)
		{
			throw new InvalidDataException("Unrecognized ward mesh format.");
		}
		int num = binaryReader.ReadInt32();
		int num2 = binaryReader.ReadInt32();
		Vector3[] array = (Vector3[])(object)new Vector3[num];
		Vector3[] array2 = (Vector3[])(object)new Vector3[num];
		for (int i = 0; i < num; i++)
		{
			array[i] = ReadVector(binaryReader);
			array2[i] = ReadVector(binaryReader);
		}
		mesh = new Mesh
		{
			name = "Hearthveil Ward",
			vertices = array,
			normals = array2,
			subMeshCount = num2
		};
		materials = (Material[])(object)new Material[num2];
		offMaterials = (Material[])(object)new Material[num2];
		for (int j = 0; j < num2; j++)
		{
			Material val2 = new Material(shader)
			{
				name = "Hearthveil material " + j
			};
			materials[j] = val2;
			val2.color = ReadColor(binaryReader);
			float num3 = binaryReader.ReadSingle();
			float num4 = binaryReader.ReadSingle();
			Color val3 = ReadColor(binaryReader);
			if (val2.HasProperty("_Metallic"))
			{
				val2.SetFloat("_Metallic", num3);
			}
			if (val2.HasProperty("_Glossiness"))
			{
				val2.SetFloat("_Glossiness", 1f - num4);
			}
			offMaterials[j] = val2;
			if (((Color)(ref val3)).maxColorComponent > 0f)
			{
				Color color = val2.color;
				Material val4 = new Material(val2)
				{
					name = ((Object)val2).name + " (off)"
				};
				val4.color = new Color(color.r * 0.27f, color.g * 0.27f, color.b * 0.27f, color.a);
				offMaterials[j] = val4;
				if (val2.HasProperty("_EmissionColor"))
				{
					val2.SetColor("_EmissionColor", val3);
					val2.EnableKeyword("_EMISSION");
				}
				else if (Object.op_Implicit((Object)(object)val) && val.isSupported)
				{
					Material val5 = new Material(val)
					{
						name = ((Object)val2).name + " (glow)"
					};
					val5.color = new Color(Mathf.Clamp01(color.r + val3.r), Mathf.Clamp01(color.g + val3.g), Mathf.Clamp01(color.b + val3.b), 1f);
					materials[j] = val5;
					Object.Destroy((Object)(object)val2);
				}
			}
			int[] array3 = new int[binaryReader.ReadInt32()];
			for (int k = 0; k < array3.Length; k++)
			{
				array3[k] = binaryReader.ReadUInt16();
			}
			mesh.SetTriangles(array3, j, false);
		}
		mesh.RecalculateBounds();
	}

	private void LoadIcon()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		using Stream stream = OpenResource("Hearthveil.WardIcon");
		using MemoryStream memoryStream = new MemoryStream();
		stream.CopyTo(memoryStream);
		texture = new Texture2D(2, 2, (TextureFormat)4, false)
		{
			name = "Hearthveil Hammer icon",
			wrapMode = (TextureWrapMode)1
		};
		if (!ImageConversion.LoadImage(texture, memoryStream.ToArray(), true))
		{
			throw new InvalidDataException("The ward icon could not be decoded.");
		}
		icon = Sprite.Create(texture, new Rect(0f, 0f, (float)((Texture)texture).width, (float)((Texture)texture).height), new Vector2(0.5f, 0.5f));
	}

	private static Stream OpenResource(string name)
	{
		return typeof(WardAppearance).Assembly.GetManifestResourceStream(name) ?? throw new InvalidDataException("Missing embedded asset " + name);
	}

	private static Vector3 ReadVector(BinaryReader reader)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
	}

	private static Color ReadColor(BinaryReader reader)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		return new Color(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 1f);
	}

	public void Dispose()
	{
		Object.Destroy((Object)(object)icon);
		Object.Destroy((Object)(object)texture);
		Object.Destroy((Object)(object)mesh);
		if (materials == null)
		{
			return;
		}
		for (int i = 0; i < materials.Length; i++)
		{
			if ((Object)(object)offMaterials[i] != (Object)(object)materials[i])
			{
				Object.Destroy((Object)(object)offMaterials[i]);
			}
			Object.Destroy((Object)(object)materials[i]);
		}
	}
}