Decompiled source of AutoPickupSelector fixed v0.5.1

Ragnarsrokare_AutoPickupSelector.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Valheim_Ragnarsrokare")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Valheim_Ragnarsrokare")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1798dab1-7c3a-4719-ab8e-e82e7b3c26c9")]
[assembly: AssemblyFileVersion("0.5.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.5.1.0")]
namespace RagnarsRokare_AutoPickupSelector;

[BepInPlugin("RagnarsRokare.AutoPickupSelector", "RagnarsRökare AutoPickupSelector Mod", "0.5.1")]
public class RagnarsRokare : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	private class ObjectDB_Awake_Patch
	{
		private static void Postfix()
		{
			if (!((Object)(object)ObjectDB.instance != (Object)null))
			{
				return;
			}
			string[] source = AutoPickupBlockList.Value.Split(new char[1] { ';' });
			foreach (ItemDrop item in GetFilteredItemList())
			{
				item.m_autoPickup = !source.Any((string v) => v == ((Object)item).name);
			}
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "OnCloseTrophies")]
	private class OnCloseTrophies_Patch
	{
		private static void Postfix()
		{
			AutoPickupBlockList.Value = GeneralExtensions.Join<string>(from i in GetFilteredItemList()
				where !i.m_autoPickup
				select ((Object)i).name, (Func<string, string>)null, ";");
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "OnOpenTrophies")]
	private class OnOpenTrophies_Patch
	{
		private static void Postfix(ref float ___m_trophieListSpace, ref float ___m_trophieListBaseSize, ref RectTransform ___m_trophieListRoot, ref List<GameObject> ___m_trophyList, ref GameObject ___m_trophieElementPrefab, ref Scrollbar ___m_trophyListScroll)
		{
			foreach (GameObject ___m_trophy in ___m_trophyList)
			{
				Object.Destroy((Object)(object)___m_trophy);
			}
			___m_trophyList.Clear();
			___m_trophyList.AddRange(CreateItemTiles(___m_trophieElementPrefab, ___m_trophieListRoot, ___m_trophieListSpace, ___m_trophieListBaseSize));
			___m_trophyListScroll.value = 1f;
		}

		private static IEnumerable<GameObject> CreateItemTiles(GameObject elementPrefab, RectTransform itemListRoot, float tileWidth, float tileBaseSize)
		{
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			List<GameObject> list = new List<GameObject>();
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return list;
			}
			float num = 0f;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			int num5 = -10;
			RectTransform rectTransform = default(RectTransform);
			foreach (ItemDrop component in from i in GetFilteredItemList()
				orderby i.m_itemData.m_shared.m_itemType
				select i)
			{
				GameObject val = Object.Instantiate<GameObject>(elementPrefab, (Transform)(object)itemListRoot);
				val.SetActive(true);
				ref RectTransform reference = ref rectTransform;
				Transform transform = val.transform;
				reference = (RectTransform)(object)((transform is RectTransform) ? transform : null);
				rectTransform.anchoredPosition = new Vector2((num2 == 0) ? ((float)num4) : ((float)num2 * tileWidth), (num3 == 0) ? ((float)num5) : ((float)num3 * (0f - tileWidth)));
				num = Mathf.Min(num, rectTransform.anchoredPosition.y - tileWidth);
				string text = Localization.instance.Localize(component.m_itemData.m_shared.m_name);
				if (text.EndsWith(" trophy"))
				{
					text = text.Remove(text.Length - 7);
				}
				((Component)((Transform)rectTransform).Find("icon_bkg/icon")).GetComponent<Image>().sprite = component.m_itemData.GetIcon();
				((Component)((Transform)rectTransform).Find("name")).GetComponent<TMP_Text>().text = text;
				((Component)((Transform)rectTransform).Find("description")).GetComponent<TMP_Text>().text = GetLootStateString(component);
				val.AddComponent<UIInputHandler>();
				UIInputHandler component2 = val.GetComponent<UIInputHandler>();
				component2.m_onLeftDown = (Action<UIInputHandler>)Delegate.Combine(component2.m_onLeftDown, (Action<UIInputHandler>)delegate
				{
					component.m_autoPickup = !component.m_autoPickup;
					((Component)((Transform)rectTransform).Find("description")).GetComponent<TMP_Text>().text = GetLootStateString(component);
				});
				list.Add(val);
				num2++;
				float num6 = (float)(num2 + 1) * tileWidth;
				Rect rect = itemListRoot.rect;
				if (num6 > ((Rect)(ref rect)).width)
				{
					num2 = 0;
					num3++;
				}
			}
			float num7 = Mathf.Max(tileBaseSize, 0f - num);
			itemListRoot.SetSizeWithCurrentAnchors((Axis)1, num7);
			return list;
		}

		private static string GetLootStateString(ItemDrop component)
		{
			string text = Localization.instance.Localize("yes");
			string text2 = Localization.instance.Localize("no");
			string text3 = (component.m_autoPickup ? text : text2);
			return "Auto pickup: " + text3;
		}
	}

	private readonly Harmony harmony = new Harmony("RagnarsRokare.AutoPickupSelector");

	public static ConfigEntry<string> AutoPickupBlockList;

	public static ConfigEntry<string> IncludedCategories;

	public static ConfigEntry<int> NexusID;

	private static IEnumerable<ItemType> categories;

	private void Awake()
	{
		AutoPickupBlockList = ((BaseUnityPlugin)this).Config.Bind<string>("General", "AutoPickupBlockList", string.Empty, (ConfigDescription)null);
		IncludedCategories = ((BaseUnityPlugin)this).Config.Bind<string>("General", "IncludedCategories", "Material;Trophie;Consumable;Torch;Tool", "Semicolon separated list of item types to include. Possible types are:None;Material;Consumable;OneHandedWeapon;Bow;Shield;Helmet;Chest;Ammo;Customization;Legs;Hands;Trophie;TwoHandedWeapon;Torch;Misc;Shoulder;Utility;Tool;Attach_Atgeir");
		categories = BuildCategoryList();
		NexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 2657, "Nexus mod ID for updates");
		harmony.PatchAll();
	}

	private static IEnumerable<ItemType> BuildCategoryList()
	{
		string[] fromConfig = IncludedCategories.Value.Trim().Replace(" ", "").Split(new char[1] { ';' });
		string[] array = fromConfig;
		foreach (string c in array)
		{
			if (Enum.TryParse<ItemType>(c, out ItemType result))
			{
				yield return result;
			}
		}
	}

	internal static IEnumerable<ItemDrop> GetFilteredItemList()
	{
		return from i in ObjectDB.instance.m_items
			select i.GetComponent<ItemDrop>() into i
			where categories.Contains(i.m_itemData.m_shared.m_itemType)
			where i.m_itemData.m_shared.m_icons.Length != 0
			where IsKnownItem(i)
			select i;
	}

	private static bool IsKnownItem(ItemDrop i)
	{
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			return true;
		}
		if ((Traverse.Create((object)Player.m_localPlayer).Field("m_knownMaterial").GetValue() as HashSet<string>).Contains(i.m_itemData.m_shared.m_name))
		{
			return true;
		}
		if ((Traverse.Create((object)Player.m_localPlayer).Field("m_trophies").GetValue() as HashSet<string>).Contains(i.m_itemData.m_shared.m_name))
		{
			return true;
		}
		return false;
	}
}