Decompiled source of ToggleBowDrawMod v1.0.0

ToggleBowDrawMod.dll

Decompiled 4 hours ago
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ToggleBowDrawMod")]
[assembly: AssemblyDescription("A mod that allows toggling the bow draw animation.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Denizhan Öter")]
[assembly: AssemblyProduct("ToggleBowDrawMod")]
[assembly: AssemblyCopyright("Copyright © Denizhan Öter 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f1b958a9-3b6f-4a6a-b27f-71f24ecff0c8")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ToggleBowDrawMod;

[BepInPlugin("com.denizhanoter.ToggleBowDrawMod", "Valheim - Toggle Bow Draw Mod", "1.0.0")]
[BepInProcess("valheim.exe")]
public class ToggleBowDrawMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "UpdateAttackBowDraw")]
	public class UpdateAttackBowDraw_Patch
	{
		private static bool Prefix(Player __instance, ItemData weapon, float dt)
		{
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Expected O, but got Unknown
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Expected O, but got Unknown
			//IL_048f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0494: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
			if (!EnableToggle.Value)
			{
				return true;
			}
			bool flag = ((Humanoid)__instance).GetInventory().GetEquippedItems().Any((ItemData item) => (int)item.m_shared.m_itemType == 4);
			if (!isToggleEnabled || weapon == null || !flag)
			{
				return true;
			}
			FieldInfo fieldInfo = AccessTools.Field(typeof(Humanoid), "m_attackDrawTime");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(Character), "m_attackHold");
			FieldInfo fieldInfo3 = AccessTools.Field(typeof(Character), "m_seman");
			FieldInfo fieldInfo4 = AccessTools.Field(typeof(Character), "m_zanim");
			FieldInfo fieldInfo5 = AccessTools.Field(typeof(Character), "m_blocking");
			float num = (float)fieldInfo.GetValue(__instance);
			bool flag2 = (bool)fieldInfo2.GetValue(__instance);
			SEMan val = (SEMan)fieldInfo3.GetValue(__instance);
			ZSyncAnimation val2 = (ZSyncAnimation)fieldInfo4.GetValue(__instance);
			if ((bool)fieldInfo5.GetValue(__instance) || ((Character)__instance).InMinorAction() || ((Character)__instance).IsAttached())
			{
				num = -1f;
				if (!string.IsNullOrEmpty(weapon.m_shared.m_attack.m_drawAnimationState))
				{
					val2.SetBool(weapon.m_shared.m_attack.m_drawAnimationState, false);
				}
				fieldInfo.SetValue(__instance, num);
				return false;
			}
			float num2 = weapon.GetDrawStaminaDrain();
			float drawEitrDrain = weapon.GetDrawEitrDrain();
			if ((double)((Humanoid)__instance).GetAttackDrawPercentage() >= 1.0)
			{
				num2 *= 0.5f;
			}
			num2 += num2 * ((Character)__instance).GetEquipmentAttackStaminaModifier();
			val.ModifyAttackStaminaUsage(num2, ref num2, true);
			bool flag3 = num2 <= 0f || ((Character)__instance).HaveStamina(0f);
			bool flag4 = drawEitrDrain <= 0f || ((Character)__instance).HaveEitr(0f);
			if (isToggleEnabled)
			{
				if (num < 0f)
				{
					if (!isDrawing)
					{
						num = 0f;
					}
				}
				else if (isDrawing && flag3 && num >= 0f)
				{
					if (num == 0f)
					{
						if (!weapon.m_shared.m_attack.StartDraw((Humanoid)(object)__instance, weapon))
						{
							num = -1f;
							fieldInfo.SetValue(__instance, num);
							return false;
						}
						weapon.m_shared.m_holdStartEffect.Create(((Component)__instance).transform.position, Quaternion.identity, ((Component)__instance).transform, 1f, -1);
					}
					num += Time.fixedDeltaTime;
					if (!string.IsNullOrEmpty(weapon.m_shared.m_attack.m_drawAnimationState))
					{
						val2.SetBool(weapon.m_shared.m_attack.m_drawAnimationState, true);
						val2.SetFloat("drawpercent", ((Humanoid)__instance).GetAttackDrawPercentage());
					}
					((Character)__instance).UseStamina(num2 * dt, false);
					((Character)__instance).UseEitr(drawEitrDrain * dt);
				}
				else if (num > 0f)
				{
					if (flag3 && flag4)
					{
						((Character)__instance).StartAttack((Character)null, false);
					}
					if (!string.IsNullOrEmpty(weapon.m_shared.m_attack.m_drawAnimationState))
					{
						val2.SetBool(weapon.m_shared.m_attack.m_drawAnimationState, false);
					}
					num = 0f;
				}
				if (ZInput.GetButtonDown("Attack"))
				{
					isDrawing = !isDrawing;
				}
			}
			else if (num < 0f)
			{
				if (!flag2)
				{
					num = 0f;
				}
			}
			else if (flag2 && flag3 && num >= 0f)
			{
				if (num == 0f)
				{
					if (!weapon.m_shared.m_attack.StartDraw((Humanoid)(object)__instance, weapon))
					{
						num = -1f;
						fieldInfo.SetValue(__instance, num);
						return false;
					}
					weapon.m_shared.m_holdStartEffect.Create(((Component)__instance).transform.position, Quaternion.identity, ((Component)__instance).transform, 1f, -1);
				}
				num += Time.fixedDeltaTime;
				if (!string.IsNullOrEmpty(weapon.m_shared.m_attack.m_drawAnimationState))
				{
					val2.SetBool(weapon.m_shared.m_attack.m_drawAnimationState, true);
					val2.SetFloat("drawpercent", ((Humanoid)__instance).GetAttackDrawPercentage());
				}
				((Character)__instance).UseStamina(num2 * dt, false);
				((Character)__instance).UseEitr(drawEitrDrain * dt);
			}
			else if (num > 0f)
			{
				if (flag3 && flag4)
				{
					((Character)__instance).StartAttack((Character)null, false);
				}
				if (!string.IsNullOrEmpty(weapon.m_shared.m_attack.m_drawAnimationState))
				{
					val2.SetBool(weapon.m_shared.m_attack.m_drawAnimationState, false);
				}
				num = 0f;
			}
			fieldInfo.SetValue(__instance, num);
			return false;
		}
	}

	private const string pluginGUID = "com.denizhanoter.ToggleBowDrawMod";

	private const string pluginName = "Valheim - Toggle Bow Draw Mod";

	private const string pluginVersion = "1.0.0";

	public static ConfigEntry<bool> EnableToggle;

	private static ConfigEntry<KeyCode> toggleKey;

	private static bool isToggleEnabled;

	private static bool isDrawing;

	private void Awake()
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		EnableToggle = ((BaseUnityPlugin)this).Config.Bind<bool>("Mod Config", "Enable", true, "Enable or Disable this mod");
		toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ToggleKey", (KeyCode)110, "The key to toggle bow draw mode.");
		Harmony val = new Harmony("com.denizhanoter.ToggleBowDrawMod");
		val.PatchAll();
	}

	private void Update()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		if (EnableToggle.Value && ZInput.instance != null && ZInput.GetKeyDown(toggleKey.Value, true))
		{
			isToggleEnabled = !isToggleEnabled;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Toggle Bow Draw is now " + (isToggleEnabled ? "enabled" : "disabled")));
		}
	}
}