Decompiled source of SteelhammerSmartHotbar v1.0.0

BepInEx\plugins\SteelhammerSmartHotbar\SteelhammerSmartHotbar.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
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("SteelhammerSmartHotbar")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SteelhammerSmartHotbar")]
[assembly: AssemblyTitle("SteelhammerSmartHotbar")]
[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 SteelhammerSmartHotbar
{
	[BepInPlugin("at.steelhammer.smarthotbar", "Steelhammer Smart Hotbar", "1.0.0")]
	public class SteelhammerSmartHotbarPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player), "UseHotbarItem")]
		private static class PlayerUseHotbarItemPatch
		{
			private static void Prefix(Player __instance, int index)
			{
				RecordHotbarSlot(__instance, index);
			}
		}

		public const string PluginGuid = "at.steelhammer.smarthotbar";

		public const string PluginName = "Steelhammer Smart Hotbar";

		public const string PluginVersion = "1.0.0";

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<bool> _invertScroll;

		private ConfigEntry<bool> _wrapAround;

		private ConfigEntry<bool> _disableWhileBuilding;

		private ConfigEntry<KeyCode> _zoomModifier;

		private ConfigEntry<float> _scrollDelay;

		private static SteelhammerSmartHotbarPlugin _instance;

		private static readonly MethodInfo InPlaceModeMethod = AccessTools.Method(typeof(Player), "InPlaceMode", (Type[])null, (Type[])null);

		private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput", (Type[])null, (Type[])null);

		private static readonly FieldInfo CameraDistanceField = AccessTools.Field(typeof(GameCamera), "m_distance");

		private int _currentSlot = 1;

		private float _nextScrollTime;

		private bool _restoreCamera;

		private float _cameraDistanceBeforeScroll;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			_instance = this;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Aktiviert oder deaktiviert Steelhammer Smart Hotbar.");
			_invertScroll = ((BaseUnityPlugin)this).Config.Bind<bool>("Hotbar", "InvertScrollDirection", false, "Kehrt die Richtung des Mausrads um.");
			_wrapAround = ((BaseUnityPlugin)this).Config.Bind<bool>("Hotbar", "WrapAround", true, "Nach Slot 8 wird wieder Slot 1 gewählt und umgekehrt.");
			_disableWhileBuilding = ((BaseUnityPlugin)this).Config.Bind<bool>("Building", "DisableWhileBuilding", true, "Im Baumodus wird das Mausrad Valheim überlassen, damit Bauteile gedreht werden können.");
			_zoomModifier = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "ZoomModifier", (KeyCode)306, "Wenn diese Taste gehalten wird, bleibt das Mausrad für den normalen Valheim-Kamerazoom frei.");
			_scrollDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Hotbar", "ScrollDelay", 0.1f, "Mindestzeit zwischen zwei Hotbar-Wechseln.");
			_harmony = new Harmony("at.steelhammer.smarthotbar");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Steelhammer Smart Hotbar 1.0.0 geladen.");
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
			_instance = null;
		}

		private void Update()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			if (!_enabled.Value)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || !CanPlayerTakeInput(localPlayer) || (_disableWhileBuilding.Value && IsInBuildMode(localPlayer)) || Input.GetKey(_zoomModifier.Value))
			{
				return;
			}
			float axis = Input.GetAxis("Mouse ScrollWheel");
			if (Mathf.Approximately(axis, 0f) || Time.unscaledTime < _nextScrollTime)
			{
				return;
			}
			int num = ((axis > 0f) ? 1 : (-1));
			if (_invertScroll.Value)
			{
				num *= -1;
			}
			int num2 = _currentSlot + num;
			if (_wrapAround.Value)
			{
				if (num2 > 8)
				{
					num2 = 1;
				}
				if (num2 < 1)
				{
					num2 = 8;
				}
			}
			else
			{
				num2 = Mathf.Clamp(num2, 1, 8);
			}
			if (num2 != _currentSlot)
			{
				SaveCameraDistance();
				_currentSlot = num2;
				localPlayer.UseHotbarItem(_currentSlot);
				_nextScrollTime = Time.unscaledTime + Mathf.Max(0.02f, _scrollDelay.Value);
			}
		}

		private void LateUpdate()
		{
			if (!_restoreCamera)
			{
				return;
			}
			_restoreCamera = false;
			if ((Object)(object)GameCamera.instance == (Object)null || CameraDistanceField == null)
			{
				return;
			}
			try
			{
				CameraDistanceField.SetValue(GameCamera.instance, _cameraDistanceBeforeScroll);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Kameradistanz konnte nicht wiederhergestellt werden: " + ex.Message));
			}
		}

		private void SaveCameraDistance()
		{
			if ((Object)(object)GameCamera.instance == (Object)null || CameraDistanceField == null)
			{
				return;
			}
			try
			{
				if (CameraDistanceField.GetValue(GameCamera.instance) is float cameraDistanceBeforeScroll)
				{
					_cameraDistanceBeforeScroll = cameraDistanceBeforeScroll;
					_restoreCamera = true;
				}
			}
			catch
			{
				_restoreCamera = false;
			}
		}

		private static bool IsInBuildMode(Player player)
		{
			if (InPlaceModeMethod == null)
			{
				return false;
			}
			try
			{
				object obj = InPlaceModeMethod.Invoke(player, null);
				bool flag = default(bool);
				int num;
				if (obj is bool)
				{
					flag = (bool)obj;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			}
			catch
			{
				return false;
			}
		}

		private static bool CanPlayerTakeInput(Player player)
		{
			if (TakeInputMethod == null)
			{
				return true;
			}
			try
			{
				object obj = TakeInputMethod.Invoke(player, null);
				bool flag = default(bool);
				int num;
				if (obj is bool)
				{
					flag = (bool)obj;
					num = ((1 == 0) ? 1 : 0);
				}
				else
				{
					num = 1;
				}
				return (byte)((uint)num | (flag ? 1u : 0u)) != 0;
			}
			catch
			{
				return true;
			}
		}

		internal static void RecordHotbarSlot(Player player, int index)
		{
			if (!((Object)(object)_instance == (Object)null) && !((Object)(object)player == (Object)null) && !((Object)(object)player != (Object)(object)Player.m_localPlayer) && index >= 1 && index <= 8)
			{
				_instance._currentSlot = index;
			}
		}
	}
}