Decompiled source of Wardrobe v1.5.1

Wardrobe.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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.5.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.5.1.0")]
[module: UnverifiableCode]
[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 Wardrobe
{
	public static class PluginConfig
	{
		public static ConfigEntry<KeyboardShortcut> QuickSwapKey;

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

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

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

		public static void BindConfig(ConfigFile config)
		{
			//IL_0036: 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.");
			SwapUtilityItem = config.Bind<bool>("Swap Options", "SwapUtilityItem", true, "Enable or disable swapping your Utility Item");
			SwapCloakItem = config.Bind<bool>("Swap Options", "SwapCloakItem", true, "Enable or disable swapping your Cloak/Cape Item");
		}
	}
	[HarmonyPatch(typeof(ArmorStand))]
	internal static class ArmorStandSwapPatch
	{
		public static readonly int ArmorStandHashCode = StringExtensionMethods.GetStableHashCode("ArmorStand");

		[HarmonyPrefix]
		[HarmonyPatch("UseItem")]
		private static bool ArmorStandSwapGear(ArmorStand __instance, Humanoid user)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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 are not on the ward this armor stand.");
				return false;
			}
			if (Object.op_Implicit((Object)(object)__instance.m_nview) && __instance.m_nview.m_zdo.m_prefab == ArmorStandHashCode)
			{
				List<ItemData> 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 static List<ItemData> ArmorStandEquip(ArmorStand stand)
		{
			List<ItemData> list = new List<ItemData>();
			ZDO zDO = stand.m_nview.GetZDO();
			for (int i = 0; i < stand.m_slots.Count; i++)
			{
				if (stand.HaveAttachment(i))
				{
					int attachedItem = stand.GetAttachedItem(i);
					GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(attachedItem);
					ItemData val = itemPrefab.GetComponent<ItemDrop>().m_itemData.Clone();
					val.m_dropPrefab = itemPrefab;
					ItemDrop.LoadFromZDO(val, zDO, i);
					list.Add(val);
					val.m_equipped = false;
					if (val.m_cheated)
					{
						Wardrobe.LogError(((Object)itemPrefab).name + " is cheated!");
					}
					stand.DestroyAttachment(i);
					zDO.Set(i + "_item", 0);
					stand.m_nview.InvokeRPC(ZNetView.Everybody, "RPC_DestroyAttachment", new object[1] { i });
					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);
			if (PluginConfig.SwapCloakItem.Value)
			{
				list.Add(player.m_shoulderItem);
			}
			if (PluginConfig.SwapUtilityItem.Value)
			{
				list.Add(player.m_utilityItem);
			}
			((MonoBehaviour)stand).CancelInvoke("UpdateAttach");
			foreach (ItemData item in list)
			{
				if (item != null)
				{
					if (item.m_cheated)
					{
						Wardrobe.LogError(item.m_shared.m_name + " is cheated!");
					}
					int num = FindFreeSlot(stand, item);
					if (num < 0)
					{
						throw new IndexOutOfRangeException("Error!");
					}
					stand.m_queuedItem = item;
					Wardrobe.LogInfo($"Sending {item.m_shared.m_name} to the Armor Stand.  Level: {item.m_quality} Crafted By: {item.m_crafterName}");
					stand.m_queuedSlot = num;
					stand.UpdateAttach();
				}
			}
			((MonoBehaviour)stand).InvokeRepeating("UpdateAttach", 0f, 0.1f);
		}

		public static void ArmorStandEquipmentToPlayer(List<ItemData> itemList, Humanoid human)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)human == (Object)(object)localPlayer))
			{
				return;
			}
			foreach (ItemData item in itemList)
			{
				if (!((Humanoid)localPlayer).m_inventory.AddItem(item))
				{
					ItemDrop.DropItem(item, 1, ((Component)localPlayer).transform.position, ((Component)localPlayer).transform.rotation);
				}
				else
				{
					localPlayer.QueueEquipAction(item);
				}
			}
		}

		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;
		}
	}
	[BepInPlugin("EardwulfDoesMods.valheim.Wardrobe", "Wardrobe", "1.5.1")]
	public class Wardrobe : BaseUnityPlugin
	{
		public const string PluginGuid = "EardwulfDoesMods.valheim.Wardrobe";

		public const string PluginName = "Wardrobe";

		public const string PluginVersion = "1.5.1";

		private static ManualLogSource _logger;

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

		public static void LogInfo(object obj)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}

		public static void LogError(object obj)
		{
			_logger.LogError((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}
	}
}