Decompiled source of SplitStack v1.0.0

SplitStack.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
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 HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SplitStack")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SplitStack")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("524431b9-63c4-470d-bb36-b81cf7406464")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace SplitStack;

[BepInPlugin("org.ssmvc.splitstack", "SplitStack", "1.0.0")]
[BepInProcess("valheim.exe")]
public class SplitStack : BaseUnityPlugin
{
	[HarmonyPatch(typeof(InventoryGui), "OnSelectedItem")]
	private static class OnSelectedItemPatch
	{
		public static bool Prefix(InventoryGui __instance, InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			if (!Enabled.Value)
			{
				return true;
			}
			if (((Character)Player.m_localPlayer).IsTeleporting())
			{
				return false;
			}
			if ((int)mod != 1 || item.m_stack <= 1)
			{
				return true;
			}
			_splitNStacks = ZInput.GetKey(SplitNStacksKey.Value, true);
			_splitStacksN = ZInput.GetKey(SplitStacksNKey.Value, true);
			if (!_splitNStacks && !_splitStacksN)
			{
				return true;
			}
			int emptySlots = grid.GetInventory().GetEmptySlots();
			if (emptySlots == 0)
			{
				__instance.SetupDragItem(item, grid.GetInventory(), item.m_stack);
				return false;
			}
			if (_splitNStacks)
			{
				__instance.m_splitSlider.maxValue = Math.Min(item.m_stack, emptySlots + 1);
				__instance.m_splitSlider.minValue = 1f;
			}
			if (_splitStacksN)
			{
				__instance.m_splitSlider.maxValue = item.m_stack;
				if (emptySlots >= item.m_stack - 1)
				{
					__instance.m_splitSlider.minValue = 1f;
				}
				else
				{
					__instance.m_splitSlider.minValue = item.m_stack / (emptySlots + 1);
				}
			}
			__instance.m_splitSlider.value = __instance.m_splitSlider.minValue;
			__instance.m_splitIcon.sprite = item.GetIcon();
			__instance.m_splitIconName.text = Localization.instance.Localize(item.m_shared.m_name);
			__instance.m_splitItem = item;
			__instance.m_splitInventory = grid.GetInventory();
			__instance.OnSplitSliderChanged(__instance.m_splitSlider.value);
			((Component)__instance.m_splitPanel).gameObject.SetActive(true);
			return false;
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "OnSplitOk")]
	private static class OnSplitOkPatch
	{
		public static bool Prefix(InventoryGui __instance)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			if (!_splitNStacks && !_splitStacksN)
			{
				return true;
			}
			int num = (int)__instance.m_splitSlider.value;
			if (_splitStacksN)
			{
				if (num == __instance.m_splitItem.m_stack)
				{
					Close(__instance);
					return false;
				}
				while (__instance.m_splitItem.m_stack > num && __instance.m_splitInventory.GetEmptySlots() > 0)
				{
					Vector2i val = __instance.m_splitInventory.FindEmptySlot(true);
					__instance.m_splitInventory.AddItem(__instance.m_splitItem.Clone(), num, val.x, val.y);
					ItemData splitItem = __instance.m_splitItem;
					splitItem.m_stack -= num;
				}
			}
			if (_splitNStacks)
			{
				if (num == 1)
				{
					Close(__instance);
					return false;
				}
				int num2 = __instance.m_splitItem.m_stack / num;
				int num3 = __instance.m_splitItem.m_stack - num2 * num;
				__instance.m_splitItem.m_stack = num2 + num3;
				for (int i = 0; i < num - 1; i++)
				{
					Vector2i val2 = __instance.m_splitInventory.FindEmptySlot(true);
					__instance.m_splitInventory.AddItem(__instance.m_splitItem.Clone(), num2, val2.x, val2.y);
				}
			}
			if (Object.op_Implicit((Object)(object)__instance.m_currentContainer))
			{
				__instance.m_currentContainer.Save();
			}
			Close(__instance);
			return false;
		}

		private static void Close(InventoryGui __instance)
		{
			_splitNStacks = false;
			_splitStacksN = false;
			__instance.m_splitItem = null;
			__instance.m_splitInventory = null;
			((Component)__instance.m_splitPanel).gameObject.SetActive(false);
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "OnSplitSliderChanged")]
	private static class OnSplitSliderChangedPatch
	{
		public static bool Prefix(InventoryGui __instance, float value)
		{
			if (!_splitNStacks && !_splitStacksN)
			{
				return true;
			}
			if (_splitStacksN)
			{
				__instance.m_splitAmount.text = $"Split into stacks of {value}.";
				if (__instance.m_splitSlider.minValue > 1f)
				{
					TMP_Text splitAmount = __instance.m_splitAmount;
					splitAmount.text += $" Min {__instance.m_splitSlider.minValue}";
				}
			}
			else if (_splitNStacks)
			{
				__instance.m_splitAmount.text = $"Split into {value}/{__instance.m_splitSlider.maxValue} stacks.";
			}
			return false;
		}
	}

	public const string PluginGUID = "org.ssmvc.splitstack";

	public const string PluginName = "SplitStack";

	public const string PluginVersion = "1.0.0";

	private static Harmony _harmony;

	private static bool _splitNStacks;

	private static bool _splitStacksN;

	public static ConfigEntry<bool> Enabled { get; set; }

	public static ConfigEntry<KeyCode> SplitNStacksKey { get; set; }

	public static ConfigEntry<KeyCode> SplitStacksNKey { get; set; }

	public void Awake()
	{
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "org.ssmvc.splitstack");
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
		SplitNStacksKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keys", "Split N Stacks Key", (KeyCode)306, "Key for split modifier count. n Stacks.");
		SplitStacksNKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keys", "Split Stacks N Key", (KeyCode)308, "Key for split modifier quantity. Stacks of n quantity.");
	}

	public void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}