Decompiled source of NotExponentiallItems v1.0.0

NotExponentiallItems.dll

Decompiled 14 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.Orbs;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("NotExponentiallItems")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NotExponentiallItems")]
[assembly: AssemblyTitle("NotExponentiallItems")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NotExponentiallItems
{
	public enum GrowthMode
	{
		DoubleStack,
		DoubleAmount
	}
	[BepInPlugin("NotExponentiallItems", "NotExponentiallItems", "1.0.0")]
	public class NotExponentiallItemsPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "NotExponentiallItems";

		public const string PluginName = "NotExponentiallItems";

		public const string PluginVersion = "1.0.0";

		public const int StackLimit = 1073741824;

		internal static ManualLogSource Log;

		internal static ConfigEntry<GrowthMode> Mode;

		internal static ConfigEntry<int> MaxStack;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Mode = ((BaseUnityPlugin)this).Config.Bind<GrowthMode>("General", "Mode", GrowthMode.DoubleStack, "DoubleStack: every pickup doubles your stack, so you have 1, 2, 4, 8, 16, ...\nDoubleAmount: every pickup gives twice as many as the last one, +1, +2, +4, +8, ... (you have 1, 3, 7, 15, ...)");
			MaxStack = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxStack", 1073741824, new ConfigDescription("The stack stops growing exponentially at this size. After that, every pickup gives 1 item like in the base game.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1073741824), Array.Empty<object>()));
			harmony = new Harmony("NotExponentiallItems");
			Type[] nestedTypes = typeof(PickupPatches).GetNestedTypes(BindingFlags.NonPublic);
			foreach (Type type in nestedTypes)
			{
				try
				{
					harmony.CreateClassProcessor(type).Patch();
				}
				catch (Exception arg)
				{
					Log.LogError((object)$"Could not apply {type.Name}, that pickup path stays vanilla: {arg}");
				}
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}

		internal unsafe static int GetPickupCount(Inventory inventory, ItemIndex itemIndex, int vanillaCount)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			int itemCountPermanent = inventory.GetItemCountPermanent(itemIndex);
			long val = ((Mode.Value == GrowthMode.DoubleStack) ? Math.Max(itemCountPermanent, 1) : ((long)itemCountPermanent + 1L));
			long val2 = (long)MaxStack.Value - (long)itemCountPermanent;
			int num = (int)Math.Max(vanillaCount, Math.Min(val, val2));
			ItemDef itemDef = ItemCatalog.GetItemDef(itemIndex);
			Log.LogInfo((object)$"{(Object.op_Implicit((Object)(object)itemDef) ? ((Object)itemDef).name : ((object)(*(ItemIndex*)(&itemIndex))/*cast due to .constrained prefix*/).ToString())}: {itemCountPermanent} + {num} = {(long)itemCountPermanent + (long)num}");
			return num;
		}
	}
	internal static class PickupPatches
	{
		[HarmonyPatch(typeof(ItemDef), "AttemptGrant")]
		private static class ItemDefAttemptGrant
		{
			private static void Prefix(ref GrantContext context)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				UniquePickup pickup = ((GrantContext)(ref context)).pickup;
				groundPickupInventory = ((!((UniquePickup)(ref pickup)).isTempItem && !pickup.isRental && Object.op_Implicit((Object)(object)context.body)) ? context.body.inventory : null);
			}

			private static void Finalizer()
			{
				groundPickupInventory = null;
			}
		}

		[HarmonyPatch(typeof(Inventory), "GiveItemPermanent", new Type[]
		{
			typeof(ItemIndex),
			typeof(int)
		})]
		private static class InventoryGiveItemPermanent
		{
			private static void Prefix(Inventory __instance, ItemIndex itemIndex, ref int countToAdd)
			{
				//IL_0022: Unknown result type (might be due to invalid IL or missing references)
				if (Object.op_Implicit((Object)(object)groundPickupInventory) && !((Object)(object)groundPickupInventory != (Object)(object)__instance))
				{
					groundPickupInventory = null;
					countToAdd = NotExponentiallItemsPlugin.GetPickupCount(__instance, itemIndex, countToAdd);
				}
			}
		}

		[HarmonyPatch(typeof(PickupPickerController), "AttemptGrantPickupDirectly")]
		private static class PickupPickerControllerAttemptGrantPickupDirectly
		{
			private static void Prefix()
			{
				directPickupDepth++;
			}

			private static void Finalizer()
			{
				directPickupDepth--;
			}
		}

		[HarmonyPatch(typeof(ChestBehavior), "BaseItemOrbSpawn")]
		private static class ChestBehaviorBaseItemOrbSpawn
		{
			private static void Prefix()
			{
				directPickupDepth++;
			}

			private static void Finalizer()
			{
				directPickupDepth--;
			}
		}

		[HarmonyPatch(typeof(ItemTransferOrb), "DispatchItemTransferOrb", new Type[] { typeof(DispatchArgs) })]
		private static class ItemTransferOrbDispatchItemTransferOrb
		{
			private static void Postfix(ItemTransferOrb __result)
			{
				if (directPickupDepth <= 0 || __result == null || __result.stack <= 0)
				{
					return;
				}
				Action<ItemTransferOrb> onArrival = __result.onArrival;
				__result.onArrival = delegate(ItemTransferOrb orb)
				{
					//IL_0015: Unknown result type (might be due to invalid IL or missing references)
					if (Object.op_Implicit((Object)(object)orb.inventoryToGrantTo))
					{
						orb.stack = NotExponentiallItemsPlugin.GetPickupCount(orb.inventoryToGrantTo, orb.itemIndex, orb.stack);
					}
					onArrival?.Invoke(orb);
				};
			}
		}

		private static Inventory groundPickupInventory;

		private static int directPickupDepth;
	}
}