Decompiled source of RestyleItems v1.0.0

RestyleItems.dll

Decompiled 2 days 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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("RestyleItems")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RestyleItems")]
[assembly: AssemblyTitle("RestyleItems")]
[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 RestyleItems
{
	[BepInPlugin("pieter.valheim.restyleitems", "Restyle Items", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "pieter.valheim.restyleitems";

		public const string Name = "Restyle Items";

		public const string Version = "1.0.0";

		private static ConfigEntry<KeyCode> _key;

		private static MethodInfo _hovered;

		private static readonly FieldRef<InventoryGui, InventoryGrid> ContainerGrid = AccessTools.FieldRefAccess<InventoryGui, InventoryGrid>("m_containerGrid");

		private static ItemData _pending;

		private static Inventory _pendingInventory;

		private static readonly MethodInfo InventoryChanged = AccessTools.Method(typeof(Inventory), "Changed", (Type[])null, (Type[])null);

		private void Awake()
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			_key = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "Restyle key", (KeyCode)114, "At a station that can craft the item: hover it in your inventory and press this key to pick a new style.");
			_hovered = AccessTools.Method(typeof(InventoryGrid), "GetHoveredElement", (Type[])null, (Type[])null);
			if (_hovered == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Disabled, InventoryGrid.GetHoveredElement was not found.");
				return;
			}
			new Harmony("pieter.valheim.restyleitems").PatchAll(typeof(Plugin));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Hover an item at its crafting station and press " + ((object)_key.Value/*cast due to .constrained prefix*/).ToString() + " to restyle it."));
		}

		[HarmonyPatch(typeof(InventoryGui), "Update")]
		[HarmonyPostfix]
		private static void UpdatePostfix(InventoryGui __instance)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			if (_pending != null && !((Component)__instance.m_variantDialog).gameObject.activeSelf)
			{
				_pending = null;
			}
			if (!InventoryGui.IsVisible() || ((Component)__instance.m_variantDialog).gameObject.activeSelf || !ZInput.GetKeyDown(_key.Value, true) || Console.IsVisible() || TextInput.IsVisible())
			{
				return;
			}
			Chat instance = Chat.instance;
			if (instance != null && instance.HasFocus())
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			Inventory inventory = __instance.m_playerGrid.GetInventory();
			ItemData val = Hovered(__instance.m_playerGrid);
			if (val == null && (Object)(object)ContainerGrid.Invoke(__instance) != (Object)null)
			{
				inventory = ContainerGrid.Invoke(__instance).GetInventory();
				val = Hovered(ContainerGrid.Invoke(__instance));
			}
			if ((Object)(object)localPlayer == (Object)null || val?.m_shared == null)
			{
				return;
			}
			if (val.m_shared.m_variants <= 1)
			{
				((Character)localPlayer).Message((MessageType)2, "This item has no other styles", 0, (Sprite)null, false);
				return;
			}
			CraftingStation currentCraftingStation = localPlayer.GetCurrentCraftingStation();
			Recipe val2 = (Object.op_Implicit((Object)(object)ObjectDB.instance) ? ObjectDB.instance.GetRecipe(val) : null);
			CraftingStation val3 = val2?.m_craftingStation;
			if ((Object)(object)currentCraftingStation == (Object)null || (Object)(object)val2 == (Object)null || ((Object)(object)val3 != (Object)null && val3.m_name != currentCraftingStation.m_name))
			{
				((Character)localPlayer).Message((MessageType)2, ((Object)(object)val3 != (Object)null) ? ("Restyle at a " + val3.m_name) : "Restyle at a crafting station", 0, (Sprite)null, false);
				return;
			}
			_pending = val;
			_pendingInventory = inventory;
			__instance.m_variantDialog.Setup(val);
		}

		private static ItemData Hovered(InventoryGrid grid)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)grid == (Object)null)
			{
				return null;
			}
			object? obj = _hovered.Invoke(grid, null);
			InventoryElement val = (InventoryElement)((obj is InventoryElement) ? obj : null);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			Inventory inventory = grid.GetInventory();
			if (inventory == null)
			{
				return null;
			}
			return inventory.GetItemAt(val.Position.x, val.Position.y);
		}

		[HarmonyPatch(typeof(InventoryGui), "OnVariantSelected")]
		[HarmonyPrefix]
		private static bool OnVariantSelectedPrefix(int index)
		{
			ItemData pending = _pending;
			Inventory pendingInventory = _pendingInventory;
			_pending = null;
			_pendingInventory = null;
			if (pending == null)
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || index < 0 || index >= pending.m_shared.m_variants)
			{
				return false;
			}
			if (pending.m_variant == index)
			{
				return false;
			}
			bool num = ((Humanoid)localPlayer).IsItemEquiped(pending);
			if (num)
			{
				((Humanoid)localPlayer).UnequipItem(pending, false);
			}
			pending.m_variant = index;
			if (num)
			{
				((Humanoid)localPlayer).EquipItem(pending, false);
			}
			if (pendingInventory != null)
			{
				InventoryChanged?.Invoke(pendingInventory, null);
			}
			((Character)localPlayer).Message((MessageType)2, "Style changed", 0, (Sprite)null, false);
			return false;
		}
	}
}