Decompiled source of Abacus v1.0.0

Abacus.dll

Decompiled a year 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 Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
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("Abacus")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Abacus")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("158ef1a3-a2bf-42ec-8cbd-100d38ee792a")]
[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 Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace Abacus
{
	[BepInPlugin("bruce.valheim.brucecomfymods.abacus", "Abacus", "1.0.0")]
	public class Abacus : BaseUnityPlugin
	{
		public const string PluginGuid = "bruce.valheim.brucecomfymods.abacus";

		public const string PluginName = "Abacus";

		public const string PluginVersion = "1.0.0";

		private Harmony _harmony;

		public void Awake()
		{
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "bruce.valheim.brucecomfymods.abacus");
		}

		public void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<int> SmallIncrement10 { get; private set; }

		public static ConfigEntry<int> LargeIncrement10 { get; private set; }

		public static ConfigEntry<int> SmallIncrement20 { get; private set; }

		public static ConfigEntry<int> LargeIncrement20 { get; private set; }

		public static ConfigEntry<int> SmallIncrement30 { get; private set; }

		public static ConfigEntry<int> LargeIncrement30 { get; private set; }

		public static ConfigEntry<int> SmallIncrement50 { get; private set; }

		public static ConfigEntry<int> LargeIncrement50 { get; private set; }

		public static ConfigEntry<int> SmallIncrement100 { get; private set; }

		public static ConfigEntry<int> LargeIncrement100 { get; private set; }

		public static ConfigEntry<int> SmallIncrement999 { get; private set; }

		public static ConfigEntry<int> LargeIncrement999 { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Expected O, but got Unknown
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Expected O, but got Unknown
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Expected O, but got Unknown
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Expected O, but got Unknown
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Expected O, but got Unknown
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Expected O, but got Unknown
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Expected O, but got Unknown
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Expected O, but got Unknown
			IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
			SmallIncrement10 = config.Bind<int>("Increment10", "smallIncrement", 1, new ConfigDescription("Small increment for max stack size of 10.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 2), Array.Empty<object>()));
			LargeIncrement10 = config.Bind<int>("Increment10", "largeIncrement", 2, new ConfigDescription("Large increment for max stack size of 10.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 9), Array.Empty<object>()));
			SmallIncrement20 = config.Bind<int>("Increment20", "smallIncrement", 5, new ConfigDescription("Small increment for max stack size of 20.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			LargeIncrement20 = config.Bind<int>("Increment20", "largeIncrement", 10, new ConfigDescription("Large increment for max stack size of 20.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(5, 19), Array.Empty<object>()));
			SmallIncrement30 = config.Bind<int>("Increment30", "smallIncrement", 5, new ConfigDescription("Small increment for max stack size of 30.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			LargeIncrement30 = config.Bind<int>("Increment30", "largeIncrement", 10, new ConfigDescription("Large increment for max stack size of 30.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(5, 29), Array.Empty<object>()));
			SmallIncrement50 = config.Bind<int>("Increment50", "smallIncrement", 5, new ConfigDescription("Small increment for max stack size of 50.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), Array.Empty<object>()));
			LargeIncrement50 = config.Bind<int>("Increment50", "largeIncrement", 10, new ConfigDescription("Large increment for max stack size of 50.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(10, 50), Array.Empty<object>()));
			SmallIncrement100 = config.Bind<int>("Increment100", "smallIncrement", 10, new ConfigDescription("Small increment for max stack size of 100.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 25), Array.Empty<object>()));
			LargeIncrement100 = config.Bind<int>("Increment100", "largeIncrement", 25, new ConfigDescription("Large increment for max stack size of 100.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(25, 99), Array.Empty<object>()));
			SmallIncrement999 = config.Bind<int>("Increment999", "smallIncrement", 25, new ConfigDescription("Small increment for gold splitting.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(5, 100), Array.Empty<object>()));
			LargeIncrement999 = config.Bind<int>("Increment999", "largeIncrement", 100, new ConfigDescription("Large increment for gold spliting.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(100, 999), Array.Empty<object>()));
		}
	}
}
namespace Abacus.Patches
{
	[HarmonyPatch(typeof(InventoryGui))]
	public class InventoryGuiPatch
	{
		private static bool _isInitialized = false;

		private static RectTransform _smallValueSelect;

		private static RectTransform _largeValueSelect;

		private static TMP_Text _smallText;

		private static TMP_Text _largeText;

		private static RectTransform _smallValueDecrement;

		private static RectTransform _smallValueIncrement;

		private static RectTransform _largeValueDecrement;

		private static RectTransform _largeValueIncrement;

		public static int _currentSmallIncrement = 0;

		public static int _currentLargeIncrement = 0;

		private static readonly string _winBkgName = "win_bkg";

		private static readonly string _okButtonName = "Button_ok";

		private static readonly string _coinsItemName = "$item_coins";

		[HarmonyPrefix]
		[HarmonyPatch("ShowSplitDialog")]
		public static void ShowSplitDialogPrefix(InventoryGui __instance, ItemData item, Inventory fromIventory)
		{
			if (PluginConfig.IsModEnabled.Value && Object.op_Implicit((Object)(object)__instance) && Object.op_Implicit((Object)(object)__instance.m_splitPanel))
			{
				if (!_isInitialized)
				{
					InitializeAbacusUI(__instance);
					_isInitialized = true;
				}
				if (_isInitialized)
				{
					UpdateAbacusUI(item);
				}
			}
		}

		public static void InitializeAbacusUI(InventoryGui inventoryGui)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: 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_01b0: Expected O, but got Unknown
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Expected O, but got Unknown
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Expected O, but got Unknown
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Expected O, but got Unknown
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Expected O, but got Unknown
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Expected O, but got Unknown
			InventoryGui inventoryGui2 = inventoryGui;
			_smallValueDecrement = CreateButton(inventoryGui2, "smallDecrement", "-", new Vector2(50f, 40f), new Vector3(70f, 60f, 0f));
			_smallValueSelect = CreateButton(inventoryGui2, "smallSelect", _currentSmallIncrement.ToString(), new Vector2(50f, 40f), new Vector3(120f, 60f, 0f));
			_smallValueIncrement = CreateButton(inventoryGui2, "smallIncrement", "+", new Vector2(50f, 40f), new Vector3(170f, 60f, 0f));
			_largeValueDecrement = CreateButton(inventoryGui2, "largeDecrement", "-", new Vector2(50f, 40f), new Vector3(70f, 20f, 0f));
			_largeValueSelect = CreateButton(inventoryGui2, "largeSelect", _currentLargeIncrement.ToString(), new Vector2(50f, 40f), new Vector3(120f, 20f, 0f));
			_largeValueIncrement = CreateButton(inventoryGui2, "largeIncrement", "+", new Vector2(50f, 40f), new Vector3(170f, 20f, 0f));
			Button val = default(Button);
			if (((Component)_smallValueDecrement).TryGetComponent<Button>(ref val))
			{
				((UnityEvent)val.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)((int)inventoryGui2.m_splitSlider.value - _currentSmallIncrement), inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
				});
			}
			Button val2 = default(Button);
			if (((Component)_smallValueSelect).TryGetComponent<Button>(ref val2))
			{
				((UnityEvent)val2.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)_currentSmallIncrement, inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
					inventoryGui2.OnSplitOk();
				});
			}
			Button val3 = default(Button);
			if (((Component)_smallValueIncrement).TryGetComponent<Button>(ref val3))
			{
				((UnityEvent)val3.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)((int)inventoryGui2.m_splitSlider.value + _currentSmallIncrement), inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
				});
			}
			Button val4 = default(Button);
			if (((Component)_largeValueDecrement).TryGetComponent<Button>(ref val4))
			{
				((UnityEvent)val4.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)((int)inventoryGui2.m_splitSlider.value - _currentLargeIncrement), inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
				});
			}
			Button val5 = default(Button);
			if (((Component)_largeValueSelect).TryGetComponent<Button>(ref val5))
			{
				((UnityEvent)val5.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)_currentLargeIncrement, inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
					inventoryGui2.OnSplitOk();
				});
			}
			Button val6 = default(Button);
			if (((Component)_largeValueIncrement).TryGetComponent<Button>(ref val6))
			{
				((UnityEvent)val6.onClick).AddListener((UnityAction)delegate
				{
					inventoryGui2.m_splitSlider.value = Mathf.Clamp((float)((int)inventoryGui2.m_splitSlider.value + _currentLargeIncrement), inventoryGui2.m_splitSlider.minValue, inventoryGui2.m_splitSlider.maxValue);
					inventoryGui2.OnSplitSliderChanged(inventoryGui2.m_splitSlider.value);
				});
			}
		}

		public static void UpdateSmallIncrementText()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			if (!Object.op_Implicit((Object)(object)_smallValueSelect))
			{
				return;
			}
			if (!Object.op_Implicit((Object)(object)_smallText))
			{
				RectTransform val = (RectTransform)((Component)_smallValueSelect).transform.Find("Text");
				TMP_Text smallText = default(TMP_Text);
				if ((Object)(object)val == (Object)null || !((Component)val).TryGetComponent<TMP_Text>(ref smallText))
				{
					return;
				}
				_smallText = smallText;
			}
			_smallText.text = _currentSmallIncrement.ToString();
		}

		public static void UpdateLargeIncrementText()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			if (!Object.op_Implicit((Object)(object)_largeValueSelect))
			{
				return;
			}
			if (!Object.op_Implicit((Object)(object)_largeText))
			{
				RectTransform val = (RectTransform)((Component)_largeValueSelect).transform.Find("Text");
				TMP_Text largeText = default(TMP_Text);
				if ((Object)(object)val == (Object)null || !((Component)val).TryGetComponent<TMP_Text>(ref largeText))
				{
					return;
				}
				_largeText = largeText;
			}
			_largeText.text = _currentLargeIncrement.ToString();
		}

		private static RectTransform CreateButton(InventoryGui inventoryGui, string name, string text, Vector2 size, Vector3 position)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			RectTransform val = FindOkButton(inventoryGui.m_splitPanel);
			if ((Object)(object)val == (Object)null)
			{
				ZLog.LogWarning((object)"Ok button not found.");
				return null;
			}
			RectTransform val2 = Object.Instantiate<RectTransform>(val, ((Component)val).transform.parent);
			((Object)val2).name = name;
			((Transform)val2).localPosition = position;
			val2.sizeDelta = size;
			RectTransform val3 = (RectTransform)((Component)val2).transform.Find("Text");
			if ((Object)(object)val3 == (Object)null)
			{
				ZLog.LogWarning((object)"Text transform not found. Text not set.");
				return val2;
			}
			TMP_Text val4 = default(TMP_Text);
			if (!((Component)val3).TryGetComponent<TMP_Text>(ref val4))
			{
				ZLog.LogWarning((object)"TMP Text component not found on Text");
				return val2;
			}
			TMP_Text component = ((Component)val3).GetComponent<TMP_Text>();
			component.text = text;
			component.m_enableAutoSizing = true;
			return val2;
		}

		private static RectTransform FindOkButton(Transform splitPanel)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			RectTransform val = (RectTransform)((Component)splitPanel).transform.Find(_winBkgName);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			return (RectTransform)((Component)val).transform.Find(_okButtonName);
		}

		public static void UpdateAbacusUI(ItemData item)
		{
			if (item.m_shared.m_maxStackSize == 10)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement10.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement10.Value;
			}
			if (item.m_shared.m_maxStackSize == 20)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement20.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement20.Value;
			}
			if (item.m_shared.m_maxStackSize == 30)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement30.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement30.Value;
			}
			if (item.m_shared.m_maxStackSize == 50)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement50.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement50.Value;
			}
			if (item.m_shared.m_maxStackSize == 100)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement100.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement100.Value;
			}
			if (item.m_shared.m_maxStackSize == 999)
			{
				_currentSmallIncrement = PluginConfig.SmallIncrement999.Value;
				_currentLargeIncrement = PluginConfig.LargeIncrement999.Value;
			}
			UpdateSmallIncrementText();
			UpdateLargeIncrementText();
		}
	}
}