Decompiled source of ShowContainerContents v0.4.0

ShowContainerContents.dll

Decompiled 8 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using ShowContainerContents;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class ItemData
{
	public ItemData idd;

	public SharedData m_shared;

	public int m_stack;

	public ItemData(ItemData idd)
	{
		m_shared = idd.m_shared;
		m_stack = idd.m_stack;
	}
}
public class SortUtils
{
	public static void SortByType(BepInExPlugin.SortType type, List<ItemData> items, bool asc)
	{
		SortByName(items, asc: true);
		for (int i = 0; i < items.Count; i++)
		{
			while (i < items.Count - 1 && items[i].m_stack < items[i].m_shared.m_maxStackSize && items[i + 1].m_shared.m_name == items[i].m_shared.m_name)
			{
				int num = Mathf.Min(items[i].m_shared.m_maxStackSize - items[i].m_stack, items[i + 1].m_stack);
				items[i].m_stack += num;
				if (num == items[i + 1].m_stack)
				{
					items.RemoveAt(i + 1);
				}
				else
				{
					items[i + 1].m_stack -= num;
				}
			}
		}
		switch (type)
		{
		case BepInExPlugin.SortType.Name:
			SortByName(items, asc);
			break;
		case BepInExPlugin.SortType.Weight:
			SortByWeight(items, asc);
			break;
		case BepInExPlugin.SortType.Value:
			SortByValue(items, asc);
			break;
		case BepInExPlugin.SortType.Amount:
			SortByAmount(items, asc);
			break;
		}
	}

	public static void SortByName(List<ItemData> items, bool asc)
	{
		items.Sort((ItemData a, ItemData b) => (a.m_shared.m_name == b.m_shared.m_name) ? CompareInts(a.m_stack, b.m_stack, asc: false) : CompareStrings(Localization.instance.Localize(a.m_shared.m_name), Localization.instance.Localize(b.m_shared.m_name), asc));
	}

	public static void SortByWeight(List<ItemData> items, bool asc)
	{
		items.Sort((ItemData a, ItemData b) => (a.m_shared.m_weight == b.m_shared.m_weight) ? ((a.m_shared.m_name == b.m_shared.m_name) ? CompareInts(a.m_stack, b.m_stack, asc: false) : CompareStrings(Localization.instance.Localize(a.m_shared.m_name), Localization.instance.Localize(b.m_shared.m_name), asc: true)) : CompareFloats(a.m_shared.m_weight, b.m_shared.m_weight, asc));
	}

	public static void SortByValue(List<ItemData> items, bool asc)
	{
		items.Sort((ItemData a, ItemData b) => (a.m_shared.m_value == b.m_shared.m_value) ? ((a.m_shared.m_name == b.m_shared.m_name) ? CompareInts(a.m_stack, b.m_stack, asc: false) : CompareStrings(Localization.instance.Localize(a.m_shared.m_name), Localization.instance.Localize(b.m_shared.m_name), asc: true)) : CompareInts(a.m_shared.m_value, b.m_shared.m_value, asc));
	}

	public static void SortByAmount(List<ItemData> items, bool asc)
	{
		items.Sort((ItemData a, ItemData b) => (a.m_stack == b.m_stack) ? CompareStrings(Localization.instance.Localize(a.m_shared.m_name), Localization.instance.Localize(b.m_shared.m_name), asc: true) : CompareInts(a.m_stack, b.m_stack, asc));
	}

	public static int CompareStrings(string a, string b, bool asc)
	{
		if (asc)
		{
			return a.CompareTo(b);
		}
		return b.CompareTo(a);
	}

	public static int CompareFloats(float a, float b, bool asc)
	{
		if (asc)
		{
			return a.CompareTo(b);
		}
		return b.CompareTo(a);
	}

	public static int CompareInts(float a, float b, bool asc)
	{
		if (asc)
		{
			return a.CompareTo(b);
		}
		return b.CompareTo(a);
	}
}
namespace ShowContainerContents;

[BepInPlugin("aedenthorn.ShowContainerContents", "Show Container Contents", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	public enum SortType
	{
		Name,
		Weight,
		Amount,
		Value
	}

	[HarmonyPatch(typeof(Container), "GetHoverText")]
	private static class GetHoverText_Patch
	{
		private static void Postfix(Container __instance, ref string __result)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || (__instance.m_checkGuardStone && !PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false)) || __instance.GetInventory().NrOfItems() == 0)
			{
				return;
			}
			List<ItemData> list = new List<ItemData>();
			foreach (ItemData allItem in __instance.GetInventory().GetAllItems())
			{
				list.Add(new ItemData(allItem));
			}
			SortUtils.SortByType(SortType.Value, list, sortAsc.Value);
			int num = 0;
			int num2 = 0;
			string text = "";
			if (capacityText.Value.Trim().Length > 0)
			{
				__result = __result.Replace("\n", string.Format(capacityText.Value, __instance.GetInventory().GetAllItems().Count, __instance.GetInventory().GetWidth() * __instance.GetInventory().GetHeight()) + "\n");
			}
			for (int i = 0; i < list.Count; i++)
			{
				if (maxEntries.Value >= 0 && num >= maxEntries.Value)
				{
					if (overFlowText.Value.Length > 0)
					{
						__result = __result + "\n" + overFlowText.Value;
					}
					break;
				}
				ItemData itemData = list[i];
				if (itemData.m_shared.m_name == text || text == "")
				{
					num2 += itemData.m_stack;
				}
				else
				{
					__result = __result + "\n" + string.Format(entryText.Value, num2, Localization.instance.Localize(text));
					num++;
					num2 = itemData.m_stack;
				}
				text = itemData.m_shared.m_name;
				if (i == list.Count - 1)
				{
					__result = __result + "\n" + string.Format(entryText.Value, num2, Localization.instance.Localize(text));
				}
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
				return false;
			}
			return true;
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<int> maxEntries;

	public static ConfigEntry<SortType> sortType;

	public static ConfigEntry<bool> sortAsc;

	public static ConfigEntry<string> entryText;

	public static ConfigEntry<string> overFlowText;

	public static ConfigEntry<string> capacityText;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 829, "Nexus mod ID for updates");
		maxEntries = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxEntries", -1, "Max number of entries to show");
		sortType = ((BaseUnityPlugin)this).Config.Bind<SortType>("General", "SortType", SortType.Value, "Type by which to sort entries.");
		sortAsc = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SortAsc", false, "Sort ascending?");
		entryText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "EntryText", "<color=#FFFFAAFF>{0}</color> <color=#AAFFAAFF>{1}</color>", "Entry text. {0} is replaced by the total amount, {1} is replaced by the item name.");
		overFlowText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "OverFlowText", "<color=#AAAAAAFF>...</color>", "Overflow text if more items than max entries.");
		capacityText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "CapacityText", "<color=#FFFFAAFF> {0}/{1}</color>", "Text to show capacity. {0} is replaced by number of full slots, {1} is replaced by total slots.");
		if (modEnabled.Value)
		{
			harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			harmony.PatchAll();
		}
	}

	private void OnDestroy()
	{
		Dbgl("Destroying plugin");
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchAll((string)null);
		}
	}
}