Decompiled source of SplitToWeight v1.2.1

SplitToWeight.dll

Decompiled 4 hours ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SplitToWeight")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SplitToWeight")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c9dd66e0-520d-467a-aa43-e321fd4d0460")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SplitToWeight;

[BepInPlugin("madtongpu.splittoweight", "Split To Weight", "1.2.0")]
public class SplitToWeightPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> AutoApply;

	internal static ConfigEntry<KeyCode> Hotkey;

	internal static ConfigEntry<string> ButtonLabel;

	internal static ConfigEntry<float> ButtonX;

	internal static ConfigEntry<float> ButtonY;

	internal static ConfigEntry<float> ButtonWidth;

	internal static ConfigEntry<float> ButtonHeight;

	internal static ConfigEntry<bool> DumpHierarchy;

	private void Awake()
	{
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		AutoApply = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoApply", false, "If true, the split slider starts at the max that fits your carry weight.");
		Hotkey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "Hotkey", (KeyCode)0, "Optional key that sets the slider to the max that fits. None = disabled.");
		ButtonLabel = ((BaseUnityPlugin)this).Config.Bind<string>("Button", "Label", "Max", "Text on the button.");
		ButtonX = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "OffsetX", 120f, "Horizontal offset from the slider's center.");
		ButtonY = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "OffsetY", 30f, "Vertical offset from the slider's center (positive = up).");
		ButtonWidth = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "Width", 70f, "Button width.");
		ButtonHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Button", "Height", 30f, "Button height.");
		DumpHierarchy = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DumpHierarchy", false, "Logs split-dialog diagnostics once, to help fix problems.");
		new Harmony("madtongpu.splittoweight").PatchAll();
	}

	internal static void ApplyMaxWeight(Slider slider, ItemData item, Inventory source)
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || item == null || (Object)(object)slider == (Object)null)
		{
			return;
		}
		Inventory inventory = ((Humanoid)localPlayer).GetInventory();
		float weight = item.m_shared.m_weight;
		float num = localPlayer.GetMaxCarryWeight() - inventory.GetTotalWeight();
		if (source == inventory)
		{
			if (!(num >= 0f) && !(weight <= 0f))
			{
				int num2 = Mathf.CeilToInt((0f - num) / weight);
				slider.value = Mathf.Clamp((float)num2, slider.minValue, slider.maxValue);
			}
		}
		else if (weight <= 0f)
		{
			slider.value = slider.maxValue;
		}
		else
		{
			int num3 = Mathf.FloorToInt(num / weight);
			slider.value = Mathf.Clamp((float)num3, slider.minValue, slider.maxValue);
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "ShowSplitDialog")]
internal static class ShowSplitDialogPatch
{
	private static Button weightButton;

	private static bool diagnosticsDone;

	internal static ItemData curItem;

	internal static Inventory curInv;

	internal static Slider curSlider;

	private static void Postfix(InventoryGui __instance, object[] __args)
	{
		curItem = (ItemData)((__args.Length != 0) ? /*isinst with value type is only supported in some contexts*/: null);
		curInv = (Inventory)((__args.Length > 1) ? /*isinst with value type is only supported in some contexts*/: null);
		if (SplitToWeightPlugin.DumpHierarchy.Value && !diagnosticsDone)
		{
			diagnosticsDone = true;
			LogSplitFields();
		}
		curSlider = FindSlider(__instance);
		if ((Object)(object)curSlider == (Object)null)
		{
			SplitToWeightPlugin.Log.LogWarning((object)"Could not find the split dialog's slider. See log above.");
			return;
		}
		if ((Object)(object)weightButton == (Object)null)
		{
			CreateButton(curSlider);
		}
		if (SplitToWeightPlugin.AutoApply.Value)
		{
			SplitToWeightPlugin.ApplyMaxWeight(curSlider, curItem, curInv);
		}
	}

	private static void OnWeightButtonClicked()
	{
		SplitToWeightPlugin.ApplyMaxWeight(curSlider, curItem, curInv);
	}

	private static void LogSplitFields()
	{
		FieldInfo[] fields = typeof(InventoryGui).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (FieldInfo fieldInfo in fields)
		{
			string text = fieldInfo.Name.ToLowerInvariant();
			string text2 = fieldInfo.FieldType.Name.ToLowerInvariant();
			if (text.Contains("split") || text2.Contains("split"))
			{
				SplitToWeightPlugin.Log.LogInfo((object)("InventoryGui field: " + fieldInfo.Name + " (" + fieldInfo.FieldType.FullName + ")"));
			}
		}
	}

	private static Slider FindSlider(InventoryGui gui)
	{
		FieldInfo[] fields = typeof(InventoryGui).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (FieldInfo fieldInfo in fields)
		{
			if (fieldInfo.FieldType == typeof(Slider) && fieldInfo.Name.ToLowerInvariant().Contains("split"))
			{
				object? value = fieldInfo.GetValue(gui);
				Slider val = (Slider)((value is Slider) ? value : null);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
		}
		List<Slider> list = new List<Slider>();
		Slider[] componentsInChildren = ((Component)gui).GetComponentsInChildren<Slider>(false);
		foreach (Slider val2 in componentsInChildren)
		{
			list.Add(val2);
			if (GetPath(((Component)val2).transform).ToLowerInvariant().Contains("split"))
			{
				return val2;
			}
		}
		if (list.Count == 1)
		{
			return list[0];
		}
		foreach (Slider item in list)
		{
			SplitToWeightPlugin.Log.LogInfo((object)("Active slider candidate: " + GetPath(((Component)item).transform)));
		}
		return null;
	}

	private static string GetPath(Transform t)
	{
		string text = ((Object)t).name;
		while ((Object)(object)t.parent != (Object)null)
		{
			t = t.parent;
			text = ((Object)t).name + "/" + text;
		}
		return text;
	}

	private static void CreateButton(Slider slider)
	{
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Expected O, but got Unknown
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Expected O, but got Unknown
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		Transform parent = ((Component)slider).transform.parent;
		for (int i = 0; i < 3; i++)
		{
			if (!((Object)(object)parent != (Object)null))
			{
				break;
			}
			if (((Component)parent).GetComponentsInChildren<Button>(true).Length != 0)
			{
				break;
			}
			parent = parent.parent;
		}
		if ((Object)(object)parent == (Object)null)
		{
			SplitToWeightPlugin.Log.LogWarning((object)"No container with buttons found near the slider.");
			return;
		}
		if (SplitToWeightPlugin.DumpHierarchy.Value)
		{
			Dump(parent, 0);
		}
		Button[] componentsInChildren = ((Component)parent).GetComponentsInChildren<Button>(true);
		if (componentsInChildren.Length == 0)
		{
			return;
		}
		Button val = componentsInChildren[0];
		Button[] array = componentsInChildren;
		foreach (Button val2 in array)
		{
			if (((Component)val2).transform.position.x > ((Component)val).transform.position.x)
			{
				val = val2;
			}
		}
		weightButton = Object.Instantiate<Button>(val, ((Component)slider).transform.parent, false);
		((Object)weightButton).name = "SplitToWeightButton";
		MonoBehaviour[] componentsInChildren2 = ((Component)weightButton).GetComponentsInChildren<MonoBehaviour>(true);
		foreach (MonoBehaviour val3 in componentsInChildren2)
		{
			if ((Object)(object)val3 != (Object)null && ((object)val3).GetType().Name == "UIGamePad")
			{
				Object.Destroy((Object)(object)val3);
			}
		}
		weightButton.onClick = new ButtonClickedEvent();
		((UnityEvent)weightButton.onClick).AddListener(new UnityAction(OnWeightButtonClicked));
		SetLabel(((Component)weightButton).gameObject, SplitToWeightPlugin.ButtonLabel.Value);
		RectTransform component = ((Component)weightButton).GetComponent<RectTransform>();
		RectTransform component2 = ((Component)slider).GetComponent<RectTransform>();
		Vector2 anchorMax = (component.anchorMin = (component2.anchorMin + component2.anchorMax) * 0.5f);
		component.anchorMax = anchorMax;
		component.pivot = new Vector2(0.5f, 0.5f);
		component.sizeDelta = new Vector2(SplitToWeightPlugin.ButtonWidth.Value, SplitToWeightPlugin.ButtonHeight.Value);
		((Transform)component).localPosition = ((Transform)component2).localPosition + new Vector3(SplitToWeightPlugin.ButtonX.Value, SplitToWeightPlugin.ButtonY.Value, 0f);
	}

	private static void SetLabel(GameObject go, string text)
	{
		Component[] componentsInChildren = go.GetComponentsInChildren<Component>(true);
		foreach (Component val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null))
			{
				PropertyInfo property = ((object)val).GetType().GetProperty("text", BindingFlags.Instance | BindingFlags.Public);
				if (property != null && property.PropertyType == typeof(string) && property.CanWrite)
				{
					property.SetValue(val, text, null);
					break;
				}
			}
		}
	}

	private static void Dump(Transform t, int depth)
	{
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Expected O, but got Unknown
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append(' ', depth * 2).Append(((Object)t).name);
		Component[] components = ((Component)t).GetComponents<Component>();
		foreach (Component val in components)
		{
			if ((Object)(object)val != (Object)null)
			{
				stringBuilder.Append(" [").Append(((object)val).GetType().Name).Append("]");
			}
		}
		RectTransform val2 = (RectTransform)(object)((t is RectTransform) ? t : null);
		if ((Object)(object)val2 != (Object)null)
		{
			StringBuilder stringBuilder2 = stringBuilder.Append(" pos=").Append(((Transform)val2).localPosition).Append(" size=");
			Rect rect = val2.rect;
			stringBuilder2.Append(((Rect)(ref rect)).size);
		}
		SplitToWeightPlugin.Log.LogInfo((object)stringBuilder.ToString());
		foreach (Transform item in t)
		{
			Dump(item, depth + 1);
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "Update")]
internal static class UpdatePatch
{
	private static void Postfix()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		if ((int)SplitToWeightPlugin.Hotkey.Value != 0)
		{
			Slider curSlider = ShowSplitDialogPatch.curSlider;
			if (!((Object)(object)curSlider == (Object)null) && ((Component)curSlider).gameObject.activeInHierarchy && Input.GetKeyDown(SplitToWeightPlugin.Hotkey.Value))
			{
				SplitToWeightPlugin.ApplyMaxWeight(curSlider, ShowSplitDialogPatch.curItem, ShowSplitDialogPatch.curInv);
			}
		}
	}
}