Decompiled source of ImprovedBuildHud v1.0.8

plugins/ImprovedBuildHud.dll

Decompiled 2 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 BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ImprovedBuildHud")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Randy Knapp Mods")]
[assembly: AssemblyProduct("ImprovedBuildHud")]
[assembly: AssemblyCopyright("Copyright © Randy Knapp 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5681a194-79cd-45fc-b47b-f5f27e96fc6d")]
[assembly: AssemblyFileVersion("1.0.8.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.8.0")]
namespace ImprovedBuildHud;

[HarmonyPatch(typeof(Hud), "SetupPieceInfo", new Type[] { typeof(Piece) })]
public static class Hud_Patch
{
	private static void Postfix(Piece piece, TMP_Text ___m_buildSelection)
	{
		if (!((Object)(object)piece != (Object)null) || string.IsNullOrEmpty(ImprovedBuildHudConfig.CanBuildAmountFormat.Value))
		{
			return;
		}
		string text = Localization.instance.Localize(piece.m_name);
		if (piece.m_resources.Length == 0)
		{
			return;
		}
		int num = int.MaxValue;
		Requirement[] resources = piece.m_resources;
		foreach (Requirement val in resources)
		{
			int num2 = ImprovedBuildHud.GetAvailableItems(val.m_resItem.m_itemData.m_shared.m_name) / val.m_amount;
			if (num2 < num)
			{
				num = num2;
			}
		}
		string text2 = string.Format(ImprovedBuildHudConfig.CanBuildAmountFormat.Value, num);
		if (!string.IsNullOrEmpty(ImprovedBuildHudConfig.CanBuildAmountColor.Value))
		{
			text2 = "<color=" + ImprovedBuildHudConfig.CanBuildAmountColor.Value + ">" + text2 + "</color>";
		}
		___m_buildSelection.text = text + " " + text2;
	}
}
[HarmonyPatch(typeof(InventoryGui), "SetupRequirement", new Type[]
{
	typeof(Transform),
	typeof(Requirement),
	typeof(Player),
	typeof(bool),
	typeof(int),
	typeof(int)
})]
public static class InventoryGui_SetupRequirement_Patch
{
	private static bool Prefix(ref bool __result, Transform elementRoot, Requirement req, Player player, bool craft, int quality, int craftMultiplier)
	{
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		Image component = ((Component)((Component)elementRoot).transform.Find("res_icon")).GetComponent<Image>();
		TMP_Text component2 = ((Component)((Component)elementRoot).transform.Find("res_name")).GetComponent<TMP_Text>();
		TMP_Text component3 = ((Component)((Component)elementRoot).transform.Find("res_amount")).GetComponent<TMP_Text>();
		UITooltip component4 = ((Component)elementRoot).GetComponent<UITooltip>();
		if ((Object)(object)req.m_resItem != (Object)null)
		{
			((Component)component).gameObject.SetActive(true);
			((Component)component2).gameObject.SetActive(true);
			((Component)component3).gameObject.SetActive(true);
			component.sprite = req.m_resItem.m_itemData.GetIcon();
			((Graphic)component).color = Color.white;
			component4.m_text = Localization.instance.Localize(req.m_resItem.m_itemData.m_shared.m_name);
			component2.text = Localization.instance.Localize(req.m_resItem.m_itemData.m_shared.m_name);
			int availableItems = ImprovedBuildHud.GetAvailableItems(req.m_resItem.m_itemData.m_shared.m_name);
			int num = req.GetAmount(quality) * craftMultiplier;
			if (num <= 0)
			{
				InventoryGui.HideRequirement(elementRoot);
				__result = false;
				return false;
			}
			component3.richText = true;
			component3.overflowMode = (TextOverflowModes)0;
			string text = string.Format(ImprovedBuildHudConfig.InventoryAmountFormat.Value, availableItems);
			if (!string.IsNullOrEmpty(ImprovedBuildHudConfig.InventoryAmountColor.Value))
			{
				text = "<color=" + ImprovedBuildHudConfig.InventoryAmountColor.Value + ">" + text + "</color>";
			}
			component3.text = $"{num} {text}";
			if (availableItems < num)
			{
				((Graphic)component3).color = (((double)Mathf.Sin(Time.time * 10f) > 0.0) ? Color.red : Color.white);
			}
			else
			{
				((Graphic)component3).color = Color.white;
			}
		}
		__result = true;
		return false;
	}
}
public static class ImprovedBuildHudConfig
{
	public static ConfigEntry<string> InventoryAmountFormat;

	public static ConfigEntry<string> InventoryAmountColor;

	public static ConfigEntry<string> CanBuildAmountFormat;

	public static ConfigEntry<string> CanBuildAmountColor;
}
[BepInPlugin("randyknapp.mods.improvedbuildhud", "Improved Build HUD", "1.0.8")]
[BepInProcess("valheim.exe")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class ImprovedBuildHud : BaseUnityPlugin
{
	public const string PluginId = "randyknapp.mods.improvedbuildhud";

	private Harmony _harmony;

	private static List<Container> _cachedContainers;

	public static bool CraftFromContainersInstalledAndActive;

	private void Awake()
	{
		ImprovedBuildHudConfig.InventoryAmountFormat = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Inventory Amount Format", "({0})", "Format for the amount of items in the player inventory to show after the required amount. Uses standard C# format rules. Leave empty to hide altogether.");
		ImprovedBuildHudConfig.InventoryAmountColor = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Inventory Amount Color", "#add8e6ff", "Color to set the inventory amount after the requirement amount. Leave empty to set no color. You can use the #XXXXXX hex color format.");
		ImprovedBuildHudConfig.CanBuildAmountFormat = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Build Amount Format", "({0})", "Format for the amount of times you can build the currently selected item with your current inventory. Uses standard C# format rules. Leave empty to hide altogether.");
		ImprovedBuildHudConfig.CanBuildAmountColor = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Build Amount Color", "white", "Color to set the can-build amount. Leave empty to set no color. You can use the #XXXXXX hex color format.");
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "randyknapp.mods.improvedbuildhud");
	}

	private void OnDestroy()
	{
		CraftFromContainersInstalledAndActive = false;
	}

	private void LateUpdate()
	{
		if (CraftFromContainersInstalledAndActive && _cachedContainers != null)
		{
			_cachedContainers.Clear();
			_cachedContainers = null;
		}
	}

	public static int GetAvailableItems(string itemName)
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return 0;
		}
		int num = ((Humanoid)localPlayer).GetInventory().CountItems(itemName, -1, true);
		int num2 = 0;
		return num + num2;
	}
}