Decompiled source of StaffOfWisps v2.0.1

StaffOfWisps.dll

Decompiled 3 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("StaffOfWisps")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2e2a01b06ca898b6cdc0498735115a11a5d543b3")]
[assembly: AssemblyProduct("StaffOfWisps")]
[assembly: AssemblyTitle("StaffOfWisps")]
[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 StaffOfWisps
{
	[BepInPlugin("mishka.valheim.staffofwisps", "StaffOfWisps", "2.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class StaffOfWispsPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Projectile), "Setup")]
		private static class ScaleWithQuality
		{
			private static void Postfix(Projectile __instance, ItemData item)
			{
				if (!((Object)(object)item?.m_dropPrefab == (Object)null) && !(((Object)item.m_dropPrefab).name != "StaffWisp"))
				{
					int num = Mathf.Clamp(item.m_quality - 1, 0, StayTtlByQuality.Length - 1);
					__instance.m_spawnOnHit = landedWispByQuality[num];
					ParticleSystemForceField componentInChildren = ((Component)__instance).GetComponentInChildren<ParticleSystemForceField>();
					if ((Object)(object)componentInChildren != (Object)null)
					{
						componentInChildren.endRange *= MistRadiusMultiplierByQuality[num];
					}
				}
			}
		}

		private class DestroyAfterSeconds : MonoBehaviour
		{
			public float delay = 25f;

			private void Start()
			{
				((MonoBehaviour)this).Invoke("DoDestroy", delay);
			}

			private void DoDestroy()
			{
				if ((Object)(object)ZNetScene.instance != (Object)null)
				{
					ZNetScene.instance.Destroy(((Component)this).gameObject);
				}
				else
				{
					Object.Destroy((Object)(object)((Component)this).gameObject);
				}
			}
		}

		public const string PluginGUID = "mishka.valheim.staffofwisps";

		public const string PluginName = "StaffOfWisps";

		public const string PluginVersion = "2.0.1";

		private static readonly Color WispColor = new Color(0.55f, 0.85f, 1f);

		private static readonly float[] StayTtlByQuality = new float[4] { 25f, 35f, 45f, 60f };

		private static readonly float[] MistRadiusMultiplierByQuality = new float[4] { 1f, 1.25f, 1.5f, 2f };

		private static GameObject[] landedWispByQuality;

		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("mishka.valheim.staffofwisps").PatchAll();
			PrefabManager.OnVanillaPrefabsAvailable += AddStaffOfWisps;
		}

		private void AddStaffOfWisps()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: 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)
			//IL_0099: Expected O, but got Unknown
			//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_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Expected O, but got Unknown
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			PrefabManager.OnVanillaPrefabsAvailable -= AddStaffOfWisps;
			landedWispByQuality = BuildLandedWispVariants();
			GameObject projectile = BuildWispProjectile();
			GameObject val = BuildWispStaff(projectile);
			ItemManager.Instance.AddItem(new CustomItem(val, true));
			ItemManager instance = ItemManager.Instance;
			RecipeConfig val2 = new RecipeConfig();
			val2.Name = "Recipe_StaffWisp";
			val2.Item = "StaffWisp";
			val2.Amount = 1;
			val2.CraftingStation = "piece_magetable";
			val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[3]
			{
				new RequirementConfig
				{
					Item = "YggdrasilWood",
					Amount = 20,
					AmountPerLevel = 10
				},
				new RequirementConfig
				{
					Item = "Wisp",
					Amount = 4,
					AmountPerLevel = 2
				},
				new RequirementConfig
				{
					Item = "Eitr",
					Amount = 16,
					AmountPerLevel = 8
				}
			};
			instance.AddRecipe(new CustomRecipe(val2));
		}

		private static GameObject[] BuildLandedWispVariants()
		{
			GameObject[] array = (GameObject[])(object)new GameObject[StayTtlByQuality.Length];
			for (int i = 0; i < array.Length; i++)
			{
				GameObject val = PrefabManager.Instance.CreateClonedPrefab("staff_wisp_landed_q" + (i + 1), "piece_groundtorch_mist");
				Transform val2 = val.transform.Find("TorchModel");
				if ((Object)(object)val2 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)((Component)val2).gameObject);
				}
				Transform val3 = val.transform.Find("PlayerBase");
				if ((Object)(object)val3 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)((Component)val3).gameObject);
				}
				Piece[] componentsInChildren = val.GetComponentsInChildren<Piece>(true);
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					Object.DestroyImmediate((Object)(object)componentsInChildren[j]);
				}
				WearNTear[] componentsInChildren2 = val.GetComponentsInChildren<WearNTear>(true);
				for (int j = 0; j < componentsInChildren2.Length; j++)
				{
					Object.DestroyImmediate((Object)(object)componentsInChildren2[j]);
				}
				ParticleSystemForceField componentInChildren = val.GetComponentInChildren<ParticleSystemForceField>(true);
				if ((Object)(object)componentInChildren != (Object)null)
				{
					componentInChildren.endRange *= MistRadiusMultiplierByQuality[i];
				}
				val.AddComponent<DestroyAfterSeconds>().delay = StayTtlByQuality[i];
				array[i] = val;
			}
			return array;
		}

		private GameObject BuildWispProjectile()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = PrefabManager.Instance.CreateClonedPrefab("staff_wisp_projectile", "staff_fireball_projectile");
			ReplaceFireLookWithWisp(val.transform);
			AttachDemisterOrb(val.transform, Vector3.zero, 0.5f, keepMistClearing: true);
			ZNetView component = val.GetComponent<ZNetView>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_distant = true;
			}
			Projectile component2 = val.GetComponent<Projectile>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.m_gravity = 3.5f;
				component2.m_ttl = 30f;
				component2.m_spawnOnHit = landedWispByQuality[0];
				if (component2.m_hitEffects.m_effectPrefabs != null)
				{
					for (int i = 0; i < component2.m_hitEffects.m_effectPrefabs.Length; i++)
					{
						component2.m_hitEffects.m_effectPrefabs[i].m_enabled = false;
					}
				}
			}
			return val;
		}

		private GameObject BuildWispStaff(GameObject projectile)
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: 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_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: 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_0147: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = PrefabManager.Instance.CreateClonedPrefab("StaffWisp", "StaffFireball");
			ItemDrop component = val.GetComponent<ItemDrop>();
			component.m_itemData.m_shared.m_name = "Staff of Wisps";
			component.m_itemData.m_shared.m_description = "Throw a bound wisp ahead of you to light the way through the mist.";
			component.m_itemData.m_shared.m_attack.m_attackProjectile = projectile;
			DamageTypes damages = component.m_itemData.m_shared.m_damages;
			damages.m_spirit = damages.m_fire;
			damages.m_fire = 0f;
			component.m_itemData.m_shared.m_damages = damages;
			DamageTypes damagesPerLevel = component.m_itemData.m_shared.m_damagesPerLevel;
			damagesPerLevel.m_spirit = damagesPerLevel.m_fire;
			damagesPerLevel.m_fire = 0f;
			component.m_itemData.m_shared.m_damagesPerLevel = damagesPerLevel;
			Vector3 val2 = ReplaceFireLookWithWisp(val.transform);
			Transform val3 = val.transform.Find("attach");
			if ((Object)(object)val3 != (Object)null)
			{
				val2 = val3.InverseTransformPoint(val.transform.TransformPoint(val2));
			}
			else
			{
				val3 = val.transform;
			}
			Vector3 localPosition = val2 + new Vector3(-0.03f, 0.03f, 0f);
			CreatePrimitive((PrimitiveType)0, val3, localPosition, Vector3.one * 0.25f, GetDimmedWispMaterial());
			LODGroup component2 = val.GetComponent<LODGroup>();
			if ((Object)(object)component2 != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component2);
			}
			Sprite val4 = RenderManager.Instance.Render(val);
			if ((Object)(object)val4 != (Object)null)
			{
				component.m_itemData.m_shared.m_icons = (Sprite[])(object)new Sprite[1] { val4 };
			}
			return val;
		}

		private static Vector3 ReplaceFireLookWithWisp(Transform root)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: 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)
			Vector3? val = null;
			Light componentInChildren = ((Component)root).GetComponentInChildren<Light>(true);
			if ((Object)(object)componentInChildren != (Object)null)
			{
				val = root.InverseTransformPoint(((Component)componentInChildren).transform.position);
			}
			ParticleSystem[] componentsInChildren = ((Component)root).GetComponentsInChildren<ParticleSystem>(true);
			foreach (ParticleSystem val2 in componentsInChildren)
			{
				if ((Object)(object)((Component)val2).transform != (Object)(object)root)
				{
					if (!val.HasValue)
					{
						val = root.InverseTransformPoint(((Component)val2).transform.position);
					}
					Object.DestroyImmediate((Object)(object)((Component)val2).gameObject);
				}
			}
			Light[] componentsInChildren2 = ((Component)root).GetComponentsInChildren<Light>(true);
			foreach (Light obj in componentsInChildren2)
			{
				obj.color = WispColor;
				obj.range = Mathf.Min(obj.range, 1.2f);
				obj.intensity = Mathf.Min(obj.intensity, 0.4f);
			}
			Material dimmedWispMaterial = GetDimmedWispMaterial();
			if ((Object)(object)dimmedWispMaterial != (Object)null)
			{
				MeshRenderer[] componentsInChildren3 = ((Component)root).GetComponentsInChildren<MeshRenderer>(true);
				foreach (MeshRenderer val3 in componentsInChildren3)
				{
					Material[] sharedMaterials = ((Renderer)val3).sharedMaterials;
					if (sharedMaterials.Length >= 2)
					{
						sharedMaterials[^1] = dimmedWispMaterial;
						((Renderer)val3).sharedMaterials = sharedMaterials;
					}
					else if (sharedMaterials.Length == 1)
					{
						((Renderer)val3).sharedMaterial = dimmedWispMaterial;
					}
				}
			}
			return (Vector3)(((??)val) ?? Vector3.zero);
		}

		private static GameObject CreatePrimitive(PrimitiveType type, Transform parent, Vector3 localPosition, Vector3 localScale, Material material)
		{
			//IL_0000: 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_0031: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = GameObject.CreatePrimitive(type);
			Object.DestroyImmediate((Object)(object)val.GetComponent<Collider>());
			val.transform.SetParent(parent, false);
			val.transform.localPosition = localPosition;
			val.transform.localScale = localScale;
			if ((Object)(object)material != (Object)null)
			{
				((Renderer)val.GetComponent<MeshRenderer>()).sharedMaterial = material;
			}
			return val;
		}

		private static void AttachDemisterOrb(Transform parent, Vector3 localPosition, float scale, bool keepMistClearing)
		{
			//IL_0033: 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_004f: 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_006f: 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)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			GameObject prefab = PrefabManager.Instance.GetPrefab("demister_ball");
			if ((Object)(object)prefab == (Object)null)
			{
				return;
			}
			GameObject val = Object.Instantiate<GameObject>(prefab, parent);
			((Object)val).name = "wisp_orb";
			val.transform.localPosition = localPosition;
			val.transform.localRotation = Quaternion.identity;
			val.transform.localScale = Vector3.one * scale;
			ParticleSystem[] componentsInChildren = val.GetComponentsInChildren<ParticleSystem>(true);
			foreach (ParticleSystem obj in componentsInChildren)
			{
				MainModule main = obj.main;
				((MainModule)(ref main)).loop = true;
				obj.Play();
			}
			ZSyncTransform[] componentsInChildren2 = val.GetComponentsInChildren<ZSyncTransform>(true);
			for (int i = 0; i < componentsInChildren2.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren2[i]);
			}
			ZNetView[] componentsInChildren3 = val.GetComponentsInChildren<ZNetView>(true);
			for (int i = 0; i < componentsInChildren3.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren3[i]);
			}
			if (keepMistClearing)
			{
				ParticleSystemForceField[] componentsInChildren4 = val.GetComponentsInChildren<ParticleSystemForceField>(true);
				foreach (ParticleSystemForceField obj2 in componentsInChildren4)
				{
					MinMaxCurve gravity = obj2.gravity;
					((MinMaxCurve)(ref gravity)).constant = ((MinMaxCurve)(ref gravity)).constant * 4f;
					((MinMaxCurve)(ref gravity)).constantMin = ((MinMaxCurve)(ref gravity)).constantMin * 4f;
					((MinMaxCurve)(ref gravity)).constantMax = ((MinMaxCurve)(ref gravity)).constantMax * 4f;
					obj2.gravity = gravity;
				}
			}
			else
			{
				Demister[] componentsInChildren5 = val.GetComponentsInChildren<Demister>(true);
				for (int i = 0; i < componentsInChildren5.Length; i++)
				{
					Object.DestroyImmediate((Object)(object)componentsInChildren5[i]);
				}
				ParticleSystemForceField[] componentsInChildren4 = val.GetComponentsInChildren<ParticleSystemForceField>(true);
				for (int i = 0; i < componentsInChildren4.Length; i++)
				{
					Object.DestroyImmediate((Object)(object)componentsInChildren4[i]);
				}
			}
		}

		private static Material GetDimmedWispMaterial()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: 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)
			Material wispMaterial = GetWispMaterial();
			if ((Object)(object)wispMaterial == (Object)null)
			{
				return null;
			}
			Material val = new Material(wispMaterial);
			if (val.HasProperty("_EmissionColor"))
			{
				val.SetColor("_EmissionColor", WispColor * 0.6f);
			}
			if (val.HasProperty("_Color"))
			{
				val.SetColor("_Color", WispColor);
			}
			return val;
		}

		private static Material GetWispMaterial()
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab("Wisp");
			MeshRenderer val = ((prefab != null) ? prefab.GetComponentInChildren<MeshRenderer>(true) : null);
			if (!((Object)(object)val != (Object)null))
			{
				return null;
			}
			return ((Renderer)val).sharedMaterial;
		}
	}
}