Decompiled source of GearVisibility v1.0.0

BepInEx/plugins/GearVisibility/GearVisibility.dll

Decompiled 8 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("GearVisibility")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+11fde89c7cc8ad9a9b4a9d9be327658002dfaae9")]
[assembly: AssemblyProduct("GearVisibility")]
[assembly: AssemblyTitle("GearVisibility")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GearVisibility;

[BepInPlugin("local.valheim.gearvisibility", "Gear Visibility", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Humanoid), "SetupVisEquipment")]
	private static class SetupVisEquipmentPatch
	{
		private static void Prefix(Humanoid __instance, out ItemData[] __state)
		{
			__state = null;
			if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				__state = Concealment.Hide(hideHelmet.Value, hideCape.Value, ref HelmetItem.Invoke(__instance), ref CapeItem.Invoke(__instance));
			}
		}

		private static void Finalizer(Humanoid __instance, ItemData[] __state)
		{
			Concealment.Restore(__state, ref HelmetItem.Invoke(__instance), ref CapeItem.Invoke(__instance));
		}
	}

	[HarmonyPatch(typeof(Player), "OnSpawned")]
	private static class OnSpawnedPatch
	{
		private static void Postfix(Player __instance)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				Refresh();
			}
		}
	}

	private static ManualLogSource log;

	private static ConfigEntry<bool> hideHelmet;

	private static ConfigEntry<bool> hideCape;

	private static ConfigEntry<KeyboardShortcut> helmetKey;

	private static ConfigEntry<KeyboardShortcut> capeKey;

	private static readonly FieldRef<Humanoid, ItemData> HelmetItem = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_helmetItem");

	private static readonly FieldRef<Humanoid, ItemData> CapeItem = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_shoulderItem");

	private static MethodInfo setupEquipment;

	private void Awake()
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: 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)
		log = ((BaseUnityPlugin)this).Logger;
		hideHelmet = ((BaseUnityPlugin)this).Config.Bind<bool>("Visibility", "HideHelmet", false, "Hide your helmet on your character. It stays equipped and keeps its stats.");
		hideCape = ((BaseUnityPlugin)this).Config.Bind<bool>("Visibility", "HideCape", false, "Hide your cape on your character. It stays equipped and keeps its stats.");
		helmetKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "ToggleHelmet", KeyboardShortcut.Empty, "Key that shows or hides your helmet. Empty means no key.");
		capeKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "ToggleCape", KeyboardShortcut.Empty, "Key that shows or hides your cape. Empty means no key.");
		hideHelmet.SettingChanged += delegate
		{
			Refresh();
		};
		hideCape.SettingChanged += delegate
		{
			Refresh();
		};
		setupEquipment = AccessTools.Method(typeof(Humanoid), "SetupEquipment", (Type[])null, (Type[])null);
		new Harmony("local.valheim.gearvisibility").PatchAll(typeof(Plugin).Assembly);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Gear Visibility loaded.");
	}

	private void Update()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)Player.m_localPlayer == (Object)null) && !IsTyping())
		{
			KeyboardShortcut value = helmetKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				Flip(hideHelmet, "Helmet");
			}
			value = capeKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				Flip(hideCape, "Cape");
			}
		}
	}

	private static bool IsTyping()
	{
		if ((!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !Console.IsVisible() && !TextInput.IsVisible())
		{
			return Menu.IsVisible();
		}
		return true;
	}

	private static void Flip(ConfigEntry<bool> entry, string name)
	{
		entry.Value = !entry.Value;
		Player localPlayer = Player.m_localPlayer;
		if (localPlayer != null)
		{
			((Character)localPlayer).Message((MessageType)1, name + (entry.Value ? " hidden" : " shown"), 0, (Sprite)null, false);
		}
	}

	private static void Refresh()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || setupEquipment == null)
		{
			return;
		}
		try
		{
			setupEquipment.Invoke(localPlayer, null);
		}
		catch (Exception ex)
		{
			log.LogWarning((object)("Could not refresh equipment visuals: " + (ex.InnerException ?? ex).Message));
		}
	}
}
public static class Concealment
{
	public static T[] Hide<T>(bool hideHelmet, bool hideCape, ref T helmet, ref T cape) where T : class
	{
		if ((!hideHelmet || helmet == null) && (!hideCape || cape == null))
		{
			return null;
		}
		T[] result = new T[2] { helmet, cape };
		if (hideHelmet)
		{
			helmet = null;
		}
		if (hideCape)
		{
			cape = null;
		}
		return result;
	}

	public static void Restore<T>(T[] saved, ref T helmet, ref T cape) where T : class
	{
		if (saved != null)
		{
			helmet = saved[0];
			cape = saved[1];
		}
	}
}