Decompiled source of YolkYeeter v0.1.1

plugins/YolkYeeter/YolkYeeter.dll

Decompiled a week 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 Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Laoch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright (c) Laoch")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+3af9a94824ab3e9c74b832ba79508377aee4f0cf")]
[assembly: AssemblyProduct("YolkYeeter")]
[assembly: AssemblyTitle("YolkYeeter")]
[assembly: InternalsVisibleTo("YolkYeeter.Harness")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.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 YolkYeeter
{
	internal static class EggSetup
	{
		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		private static class ZNetSceneAwake
		{
			[HarmonyPriority(200)]
			private static void Postfix(ZNetScene __instance)
			{
				GameObject prefab = __instance.GetPrefab("Snowball");
				GameObject prefab2 = __instance.GetPrefab("ChickenEgg");
				if (EnsureProjectile(prefab, prefab2))
				{
					ApplySplat(__instance);
					int stableHashCode = StringExtensionMethods.GetStableHashCode("YolkYeeter_EggProjectile");
					if (!__instance.m_namedPrefabs.ContainsKey(stableHashCode))
					{
						__instance.m_prefabs.Add(Projectile);
						__instance.m_namedPrefabs[stableHashCode] = Projectile;
					}
				}
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		private static class ObjectDBAwake
		{
			[HarmonyPriority(200)]
			private static void Postfix(ObjectDB __instance)
			{
				Apply(__instance);
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
		private static class ObjectDBCopy
		{
			[HarmonyPriority(200)]
			private static void Postfix(ObjectDB __instance)
			{
				Apply(__instance);
			}
		}

		internal const string EggItem = "ChickenEgg";

		internal const string SnowballItem = "Snowball";

		internal const string ProjectileName = "YolkYeeter_EggProjectile";

		private static readonly string[] SplatEffects = new string[2] { "fx_egg_splash", "Egg_gib" };

		private static GameObject _holder;

		internal static GameObject Projectile { get; private set; }

		internal static bool EggReady { get; private set; }

		private static void Apply(ObjectDB db)
		{
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: 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_00b0: 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_019a: 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)
			if ((Object)(object)db == (Object)null || db.m_items == null || db.m_items.Count == 0)
			{
				return;
			}
			GameObject itemPrefab = db.GetItemPrefab("Snowball");
			GameObject itemPrefab2 = db.GetItemPrefab("ChickenEgg");
			if ((Object)(object)itemPrefab == (Object)null || (Object)(object)itemPrefab2 == (Object)null)
			{
				YolkYeeterPlugin.Log.LogWarning((object)"Snowball or ChickenEgg not found; eggs stay unthrowable.");
			}
			else if (EnsureProjectile(itemPrefab, itemPrefab2))
			{
				SharedData shared = itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared;
				SharedData shared2 = itemPrefab2.GetComponent<ItemDrop>().m_itemData.m_shared;
				shared2.m_itemType = shared.m_itemType;
				shared2.m_animationState = shared.m_animationState;
				shared2.m_skillType = shared.m_skillType;
				shared2.m_attachOverride = shared.m_attachOverride;
				shared2.m_equipDuration = shared.m_equipDuration;
				shared2.m_useDurability = false;
				shared2.m_attackForce = shared.m_attackForce;
				shared2.m_backstabBonus = shared.m_backstabBonus;
				shared2.m_blockable = shared.m_blockable;
				shared2.m_dodgeable = shared.m_dodgeable;
				shared2.m_startEffect = shared.m_startEffect;
				shared2.m_triggerEffect = shared.m_triggerEffect;
				shared2.m_trailStartEffect = shared.m_trailStartEffect;
				shared2.m_attack = shared.m_attack.Clone();
				shared2.m_attack.m_attackProjectile = Projectile;
				Attack secondaryAttack = shared.m_secondaryAttack;
				shared2.m_secondaryAttack = ((secondaryAttack != null) ? secondaryAttack.Clone() : null);
				if (shared2.m_secondaryAttack != null && (Object)(object)shared2.m_secondaryAttack.m_attackProjectile != (Object)null)
				{
					shared2.m_secondaryAttack.m_attackProjectile = Projectile;
				}
				shared2.m_damages = new DamageTypes
				{
					m_blunt = Settings.Damage.Value
				};
				AddHandModel(itemPrefab2, itemPrefab);
				EggReady = true;
			}
		}

		internal static void RefreshDamage()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = (((Object)(object)ObjectDB.instance != (Object)null) ? ObjectDB.instance.GetItemPrefab("ChickenEgg") : null);
			if (!((Object)(object)val == (Object)null) && EggReady)
			{
				val.GetComponent<ItemDrop>().m_itemData.m_shared.m_damages = new DamageTypes
				{
					m_blunt = Settings.Damage.Value
				};
			}
		}

		private static void AddHandModel(GameObject egg, GameObject snowball)
		{
			//IL_00ad: 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_00be: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)egg.transform.Find("attach") != (Object)null)
			{
				return;
			}
			Transform val = FindModel(egg.transform);
			if ((Object)(object)val == (Object)null)
			{
				YolkYeeterPlugin.Log.LogWarning((object)"No egg model found; the egg will be invisible in the hand.");
				return;
			}
			GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject, egg.transform, false);
			((Object)val2).name = "attach";
			val2.SetActive(false);
			StripToVisuals(val2);
			Transform val3 = snowball.transform.Find("attach");
			if ((Object)(object)val3 != (Object)null)
			{
				val2.transform.localPosition = val3.localPosition;
				val2.transform.localRotation = val3.localRotation;
			}
			else
			{
				val2.transform.localPosition = Vector3.zero;
			}
			val2.transform.localScale = val.localScale;
		}

		private static bool EnsureProjectile(GameObject snowball, GameObject egg)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Projectile != (Object)null)
			{
				return true;
			}
			if ((Object)(object)snowball == (Object)null || (Object)(object)egg == (Object)null)
			{
				return false;
			}
			GameObject val = snowball.GetComponent<ItemDrop>()?.m_itemData.m_shared.m_attack?.m_attackProjectile;
			if ((Object)(object)val == (Object)null)
			{
				YolkYeeterPlugin.Log.LogWarning((object)"Snowball has no projectile; eggs stay unthrowable.");
				return false;
			}
			_holder = new GameObject("YolkYeeter_Prefabs");
			_holder.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)_holder);
			GameObject val2 = Object.Instantiate<GameObject>(val, _holder.transform, false);
			((Object)val2).name = "YolkYeeter_EggProjectile";
			Projectile component = val2.GetComponent<Projectile>();
			LODGroup[] componentsInChildren = val2.GetComponentsInChildren<LODGroup>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren[i], true);
			}
			MeshRenderer[] componentsInChildren2 = val2.GetComponentsInChildren<MeshRenderer>(true);
			for (int i = 0; i < componentsInChildren2.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren2[i], true);
			}
			Transform val3 = FindModel(egg.transform);
			if ((Object)(object)val3 != (Object)null)
			{
				GameObject val4 = Object.Instantiate<GameObject>(((Component)val3).gameObject, val2.transform, false);
				((Object)val4).name = "egg_visual";
				val4.transform.localPosition = Vector3.zero;
				val4.transform.localRotation = Quaternion.identity;
				val4.transform.localScale = val3.localScale;
				val4.SetActive(true);
				StripToVisuals(val4);
				if ((Object)(object)component != (Object)null && (Object)(object)component.m_visual != (Object)null)
				{
					component.m_visual = val4;
				}
			}
			if ((Object)(object)component != (Object)null)
			{
				component.m_spawnOnHit = null;
			}
			Projectile = val2;
			if ((Object)(object)ZNetScene.instance != (Object)null)
			{
				ApplySplat(ZNetScene.instance);
			}
			return true;
		}

		private static void ApplySplat(ZNetScene scene)
		{
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Expected O, but got Unknown
			Projectile val = (((Object)(object)Projectile != (Object)null) ? Projectile.GetComponent<Projectile>() : null);
			if ((Object)(object)val == (Object)null || (val.m_hitEffects?.m_effectPrefabs != null && val.m_hitEffects.m_effectPrefabs.Any((EffectData e) => (Object)(object)e.m_prefab != (Object)null && ((Object)e.m_prefab).name == SplatEffects[0])))
			{
				return;
			}
			List<EffectData> list = (from n in SplatEffects
				select scene.GetPrefab(n) into p
				where (Object)(object)p != (Object)null
				select p).Select((Func<GameObject, EffectData>)((GameObject p) => new EffectData
			{
				m_prefab = p,
				m_enabled = true
			})).ToList();
			if (list.Count > 0)
			{
				IEnumerable<EffectData> second = val.m_hitEffects?.m_effectPrefabs?.Where((EffectData e) => (Object)(object)e.m_prefab != (Object)null && ((Object)e.m_prefab).name.StartsWith("sfx_")) ?? Enumerable.Empty<EffectData>();
				val.m_hitEffects = new EffectList
				{
					m_effectPrefabs = list.Concat(second).ToArray()
				};
			}
			else
			{
				YolkYeeterPlugin.Log.LogWarning((object)"Egg splat effects not found; eggs will puff like snowballs.");
			}
		}

		private static Transform FindModel(Transform root)
		{
			//IL_004a: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			MeshRenderer val = ((IEnumerable<MeshRenderer>)((Component)root).GetComponentsInChildren<MeshRenderer>(true)).FirstOrDefault((Func<MeshRenderer, bool>)((MeshRenderer r) => (Object)(object)((Component)r).GetComponent<MeshFilter>() != (Object)null && (Object)(object)((Component)r).GetComponent<MeshFilter>().sharedMesh != (Object)null));
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			if ((Object)(object)((Component)val).transform == (Object)(object)root)
			{
				GameObject val2 = new GameObject("model");
				val2.SetActive(false);
				val2.transform.SetParent(root, false);
				val2.AddComponent<MeshFilter>().sharedMesh = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				((Renderer)val2.AddComponent<MeshRenderer>()).sharedMaterials = ((Renderer)val).sharedMaterials;
				return val2.transform;
			}
			return ((Component)val).transform;
		}

		private static void StripToVisuals(GameObject go)
		{
			List<Component> list = new List<Component>();
			Component[] componentsInChildren = go.GetComponentsInChildren<Component>(true);
			foreach (Component val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !(val is Transform) && !(val is MeshFilter) && !(val is Renderer) && !(val is LODGroup))
				{
					list.Add(val);
				}
			}
			foreach (Component item in list.Where((Component c) => !(c is Rigidbody)))
			{
				Object.DestroyImmediate((Object)(object)item, true);
			}
			foreach (Component item2 in list.Where((Component c) => c is Rigidbody))
			{
				if ((Object)(object)item2 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)item2, true);
				}
			}
		}
	}
	internal static class Settings
	{
		internal static ConfigEntry<float> Damage { get; private set; }

		internal static void Bind(ConfigFile config)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			Damage = config.Bind<float>("1 - Eggs", "Damage", 1f, new ConfigDescription("Blunt damage a thrown egg does. Meant to sting, not to kill: 1 is a snowball-sized tap. 0 = splat only.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), Array.Empty<object>()));
			Damage.SettingChanged += delegate
			{
				EggSetup.RefreshDamage();
			};
		}
	}
	[BepInPlugin("com.laoch.yolkyeeter", "YolkYeeter", "0.1.1")]
	public class YolkYeeterPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.laoch.yolkyeeter";

		public const string PluginName = "YolkYeeter";

		public const string PluginVersion = "0.1.1";

		internal static YolkYeeterPlugin Instance { get; private set; }

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Settings.Bind(((BaseUnityPlugin)this).Config);
			new Harmony("com.laoch.yolkyeeter").PatchAll(typeof(YolkYeeterPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"YolkYeeter 0.1.1 loaded. Equip a raw egg and attack to throw it.");
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}