Decompiled source of ImmersiveTacticalHotkeys v1.0.0

CompanionTacticalHotkeys.dll

Decompiled 5 days ago
using System;
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 BepInEx.Logging;
using Companions;
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: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CompanionTacticalHotkeys")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CompanionTacticalHotkeys")]
[assembly: AssemblyTitle("CompanionTacticalHotkeys")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ImmersiveTacticalHotkeys;

[BepInPlugin("com.yourname.immersivetacticalhotkeys", "Immersive Tactical Hotkeys", "1.0.0")]
[BepInProcess("valheim.exe")]
public class TacticalHotkeysPlugin : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("com.yourname.immersivetacticalhotkeys");

	private ConfigEntry<KeyCode> passiveKey;

	private ConfigEntry<KeyCode> balancedKey;

	private ConfigEntry<KeyCode> archerKey;

	public static ManualLogSource Log;

	private static MethodInfo setCombatStanceMethod;

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		passiveKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "PassiveModeKey", (KeyCode)277, "Hotkey to switch companion to Passive mode.");
		balancedKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "BalancedModeKey", (KeyCode)278, "Hotkey to switch companion to Balanced mode.");
		archerKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "ArcherModeKey", (KeyCode)280, "Hotkey to switch companion to Archer mode.");
		setCombatStanceMethod = typeof(CompanionSetup).GetMethod("SetCombatStance", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (setCombatStanceMethod == null)
		{
			Log.LogError((object)"CRITICAL: Failed to locate internal 'SetCombatStance' method via reflection!");
		}
		harmony.PatchAll();
		Log.LogInfo((object)"Immersive Tactical Hotkeys Loaded Successfully!");
	}

	private void Update()
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: 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_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Player.m_localPlayer == (Object)null || Console.IsVisible() || Menu.IsVisible())
		{
			return;
		}
		Chat instance = Chat.instance;
		if (instance == null || !instance.HasFocus())
		{
			KeyCode val = (KeyCode)((passiveKey == null) ? 277 : ((int)passiveKey.Value));
			KeyCode val2 = (KeyCode)((balancedKey == null) ? 278 : ((int)balancedKey.Value));
			KeyCode val3 = (KeyCode)((archerKey == null) ? 280 : ((int)archerKey.Value));
			if (Input.GetKeyDown(val))
			{
				ExecuteNativeStanceShift(1, "Passive");
			}
			else if (Input.GetKeyDown(val2))
			{
				ExecuteNativeStanceShift(0, "Balanced");
			}
			else if (Input.GetKeyDown(val3))
			{
				ExecuteNativeStanceShift(2, "Archer");
			}
		}
	}

	private void ExecuteNativeStanceShift(int stanceId, string stanceName)
	{
		CompanionAI[] array = Object.FindObjectsByType<CompanionAI>((FindObjectsInactive)0, (FindObjectsSortMode)0);
		if (array == null || array.Length == 0)
		{
			return;
		}
		bool flag = false;
		CompanionAI[] array2 = array;
		foreach (CompanionAI val in array2)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			GameObject followTarget = val.GetFollowTarget();
			if ((Object)(object)followTarget == (Object)null || (Object)(object)followTarget != (Object)(object)((Component)Player.m_localPlayer).gameObject)
			{
				continue;
			}
			CompanionSetup component = ((Component)val).GetComponent<CompanionSetup>();
			if ((Object)(object)component == (Object)null || !(setCombatStanceMethod != null))
			{
				continue;
			}
			try
			{
				ZNetView component2 = ((Component)val).GetComponent<ZNetView>();
				if ((Object)(object)component2 != (Object)null && component2.GetZDO() != null && !component2.IsOwner())
				{
					component2.ClaimOwnership();
				}
				setCombatStanceMethod.Invoke(component, new object[1] { stanceId });
				flag = true;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Stance alignment interaction failed: " + ex.Message));
			}
		}
		if ((Object)(object)MessageHud.instance != (Object)null)
		{
			if (flag)
			{
				MessageHud.instance.ShowMessage((MessageType)1, "Companion stance updated to: " + stanceName.ToUpper(), 0, (Sprite)null, false);
			}
			else
			{
				MessageHud.instance.ShowMessage((MessageType)1, "Hotkey pressed, but no following companion found nearby.", 0, (Sprite)null, false);
			}
		}
	}
}