Decompiled source of Wardrobe v1.0.2

Wardrobe.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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("Wardrobe")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Wardrobe")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("466cf3d5-484d-48d0-8b99-e69bec9c3827")]
[assembly: AssemblyFileVersion("1.0.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
namespace Wardrobe;

public static class PluginConfig
{
	public static ConfigEntry<KeyboardShortcut> QuickSwapKey;

	public static ConfigEntry<bool> IsModEnabled { get; private set; }

	public static void BindConfig(ConfigFile config)
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		IsModEnabled = config.Bind<bool>("Global", "IsModEnabled", true, "Enable or Disable this mod");
		QuickSwapKey = config.Bind<KeyboardShortcut>("Quick Swap Keybind", "QuickSwapKey", new KeyboardShortcut((KeyCode)101, (KeyCode[])(object)new KeyCode[1] { (KeyCode)308 }), "Keyboard Shortcut to swap equipment.  You must `E` with a modifier key (LeftAlt, RightCtrl, etc.");
	}
}
[BepInPlugin("EardwulfDoesMods.valheim.Wardrobe", "Wardrobe", "1.0.2")]
public class Wardrobe : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ArmorStand))]
	private static class ArmorStandSwapPatch
	{
		public static readonly int ArmorStandHashCode = StringExtensionMethods.GetStableHashCode("ArmorStand");

		[HarmonyPrefix]
		[HarmonyPatch("UseItem")]
		private static bool ArmorStandSwapGear(ArmorStand __instance, Humanoid user)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = PluginConfig.QuickSwapKey.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown() || !PluginConfig.IsModEnabled.Value)
			{
				return true;
			}
			if (!PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, true, false))
			{
				ZLog.Log((object)"You do you have access to this armor stand.");
				return false;
			}
			if (Object.op_Implicit((Object)(object)__instance.m_nview) && __instance.m_nview.m_zdo.m_prefab == ArmorStandHashCode)
			{
				List<ItemDrop> itemlist = ArmorStandEquip(__instance);
				PlayerEquipmentToArmorStand(__instance, user);
				ArmorStandEquipmentToPlayer(itemlist, user);
				ZLog.Log((object)"Swapping Gear with Armor Stand!");
				((Terminal)Chat.m_instance).AddString("Swapping gear with ArmorStand");
				return false;
			}
			((Terminal)Chat.m_instance).AddString("<color=red>Failed to swap armor because this is not an armor stand.</color>");
			ZLog.Log((object)"ArmorStandSwapGear() failed beacuse the target is not an ArmorStand.");
			return false;
		}
	}

	public const string PluginGuid = "EardwulfDoesMods.valheim.Wardrobe";

	public const string PluginName = "Wardrobe";

	public const string PluginVersion = "1.0.2";

	private Harmony _harmony;

	private void Awake()
	{
		PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "EardwulfDoesMods.valheim.Wardrobe");
	}

	public static List<ItemDrop> ArmorStandEquip(ArmorStand stand)
	{
		List<ItemDrop> list = new List<ItemDrop>();
		for (int i = 0; i < stand.m_slots.Count; i++)
		{
			if (stand.HaveAttachment(i))
			{
				string @string = stand.m_nview.GetZDO().GetString(i + "_item", "");
				ItemDrop component = ObjectDB.instance.GetItemPrefab(@string).GetComponent<ItemDrop>();
				ItemDrop.LoadFromZDO(i, component.m_itemData, stand.m_nview.GetZDO());
				list.Add(component);
				ZLog.Log((object)$"Wardrobe says I removed item {((Object)component).name} From the ArmorStand. Level: {component.m_itemData.m_quality}");
				stand.DestroyAttachment(i);
				stand.m_nview.GetZDO().Set(i + "_item", "");
				stand.m_nview.InvokeRPC(ZNetView.Everybody, "RPC_SetVisualItem", new object[3] { i, "", 0 });
				stand.UpdateSupports();
				stand.m_cloths = ((Component)stand).GetComponentsInChildren<Cloth>();
			}
		}
		return list;
	}

	public static void PlayerEquipmentToArmorStand(ArmorStand stand, Humanoid player)
	{
		List<ItemData> list = new List<ItemData>();
		list.Add(player.m_rightItem);
		list.Add(player.m_leftItem);
		list.Add(player.m_chestItem);
		list.Add(player.m_legItem);
		list.Add(player.m_helmetItem);
		list.Add(player.m_shoulderItem);
		list.Add(player.m_utilityItem);
		((MonoBehaviour)stand).CancelInvoke("UpdateAttach");
		foreach (ItemData item in list)
		{
			if (item != null)
			{
				int num = FindFreeSlot(stand, item);
				if (num < 0)
				{
					throw new IndexOutOfRangeException("This should never happen! Call a grownup!");
				}
				stand.m_queuedItem = item;
				ZLog.Log((object)$"Sending {item.m_shared.m_name} to the Armor Stand.  Level: {item.m_quality}");
				stand.m_queuedSlot = num;
				stand.UpdateAttach();
			}
		}
		((MonoBehaviour)stand).InvokeRepeating("UpdateAttach", 0f, 0.1f);
	}

	public static void ArmorStandEquipmentToPlayer(List<ItemDrop> itemlist, Humanoid human)
	{
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)human == (Object)(object)localPlayer))
		{
			return;
		}
		foreach (ItemDrop item in itemlist)
		{
			((Humanoid)localPlayer).m_inventory.AddItem(item.m_itemData);
			localPlayer.QueueEquipAction(item.m_itemData);
		}
	}

	private static int FindFreeSlot(ArmorStand stand, ItemData item)
	{
		for (int i = 0; i < stand.m_slots.Count; i++)
		{
			if (stand.CanAttach(stand.m_slots[i], item))
			{
				return i;
			}
		}
		return -1;
	}

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