Decompiled source of ItemMultiplier v1.0.4

plugins/ItemMultiplier.dll

Decompiled 5 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using EntityStates.Missions.BrotherEncounter;
using Microsoft.CodeAnalysis;
using On.EntityStates.Missions.BrotherEncounter;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ItemMultiplier
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("HIFU.ItemMultiplier", "ItemMultiplier", "1.0.3")]
	public class Main : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <FixMithrixItems>d__25 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public Main <>4__this;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <FixMithrixItems>d__25(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: Expected O, but got Unknown
				//IL_00a2: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
				int num = <>1__state;
				Main main = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = (object)new WaitForSeconds(0.5f);
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					if (multiplier.Value <= 1)
					{
						return false;
					}
					foreach (CharacterBody readOnlyInstances in CharacterBody.readOnlyInstancesList)
					{
						if ((Object)(object)readOnlyInstances == (Object)null)
						{
							continue;
						}
						Inventory inventory = readOnlyInstances.inventory;
						if ((Object)(object)inventory == (Object)null || !readOnlyInstances.isPlayerControlled)
						{
							continue;
						}
						foreach (ItemIndex item in inventory.itemAcquisitionOrder)
						{
							int itemCountPermanent = inventory.GetItemCountPermanent(item);
							if (itemCountPermanent <= 0)
							{
								continue;
							}
							ItemDef itemDef = ItemCatalog.GetItemDef(item);
							if (!((Object)(object)itemDef == (Object)null) && main.ShouldMultiply(itemDef))
							{
								int num2 = itemCountPermanent - Mathf.CeilToInt((float)itemCountPermanent / (float)multiplier.Value);
								if (num2 > 0)
								{
									inventory.RemoveItemPermanent(item, num2);
								}
							}
						}
					}
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[CompilerGenerated]
		private sealed class <GiveGradually>d__17 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public Inventory inventory;

			public int total;

			public Main <>4__this;

			public ItemIndex itemIndex;

			private int <batchSize>5__2;

			private int <given>5__3;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <GiveGradually>d__17(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0069: Unknown result type (might be due to invalid IL or missing references)
				int num = <>1__state;
				Main main = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<batchSize>5__2 = 50;
					<given>5__3 = 0;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (<given>5__3 < total)
				{
					if ((Object)(object)inventory == (Object)null)
					{
						return false;
					}
					int num2 = Mathf.Min(<batchSize>5__2, total - <given>5__3);
					main._givingBonus = true;
					inventory.GiveItemPermanent(itemIndex, num2);
					main._givingBonus = false;
					<given>5__3 += num2;
					<>2__current = null;
					<>1__state = 1;
					return true;
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		public const string PluginGUID = "HIFU.ItemMultiplier";

		public const string PluginAuthor = "HIFU";

		public const string PluginName = "ItemMultiplier";

		public const string PluginVersion = "1.0.3";

		internal static ManualLogSource? Log;

		public static ConfigEntry<int>? multiplier;

		public static ConfigEntry<bool>? fixScrappers;

		public static ConfigEntry<bool>? fixCauldrons;

		public static ConfigEntry<bool>? fixMithrix;

		public static ConfigEntry<bool>? multiplyRemoval;

		private bool _givingBonus;

		private bool _removingBonus;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			BindConfig();
			if (Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions"))
			{
				RegisterRiskOfOptions();
			}
			HookEvents();
			Log.LogInfo((object)"ItemMultiplier v1.0.3 loaded!");
		}

		private void BindConfig()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			multiplier = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Multiplier", 10, new ConfigDescription("How many copies of each item you receive on pickup.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>()));
			multiplyRemoval = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Multiply Removal", true, "When true, item removals (scrapper, printer) are also multiplied.");
			fixScrappers = ((BaseUnityPlugin)this).Config.Bind<bool>("Fixes", "Fix Scrappers", true, "Prevents the scrapper from consuming more than one item at a time.");
			fixCauldrons = ((BaseUnityPlugin)this).Config.Bind<bool>("Fixes", "Fix Cauldrons", true, "Corrects the cost display on cauldrons/printers to show x1 instead of the multiplied amount.");
			fixMithrix = ((BaseUnityPlugin)this).Config.Bind<bool>("Fixes", "Fix Mithrix", true, "Prevents Mithrix Phase 4 from giving back multiplied stolen items.");
		}

		private void RegisterRiskOfOptions()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Expected O, but got Unknown
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Expected O, but got Unknown
			ModSettingsManager.SetModDescription("Multiplies all item gains. Configure the multiplier and various fixes.", "HIFU.ItemMultiplier", "ItemMultiplier");
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(multiplier, new IntSliderConfig
			{
				description = "How many copies of each item you receive per pickup.",
				min = 1,
				max = 1000
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(multiplyRemoval, new CheckBoxConfig
			{
				description = "Also multiply item removals (scrapper, printer input)."
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(fixScrappers, new CheckBoxConfig
			{
				description = "Fix scrappers so they only consume one item."
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(fixCauldrons, new CheckBoxConfig
			{
				description = "Fix cauldron/printer cost display."
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(fixMithrix, new CheckBoxConfig
			{
				description = "Prevent Mithrix Phase 4 from returning multiplied items."
			}));
		}

		private void HookEvents()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			Inventory.GiveItem_ItemIndex_int += new hook_GiveItem_ItemIndex_int(OnGiveItem);
			Inventory.RemoveItem_ItemIndex_int += new hook_RemoveItem_ItemIndex_int(OnRemoveItem);
			PurchaseInteraction.CanBeAffordedByInteractor += new hook_CanBeAffordedByInteractor(OnCanBeAfforded);
			CostHologramContent.FixedUpdate += new hook_FixedUpdate(OnCostHologramFixedUpdate);
			PurchaseInteraction.GetContextString += new hook_GetContextString(OnGetContextString);
			BossDeath.OnEnter += new hook_OnEnter(OnBossDeathEnter);
			Run.Start += new hook_Start(OnRunStart);
			Run.BeginStage += new hook_BeginStage(OnBeginStage);
		}

		private void OnGiveItem(orig_GiveItem_ItemIndex_int orig, Inventory self, ItemIndex itemIndex, int count)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self, itemIndex, count);
			if (_givingBonus || !NetworkServer.active)
			{
				return;
			}
			ItemDef itemDef = ItemCatalog.GetItemDef(itemIndex);
			if ((Object)(object)itemDef == (Object)null || !ShouldMultiply(itemDef))
			{
				return;
			}
			int num = (multiplier.Value - 1) * count;
			if (num <= 0)
			{
				return;
			}
			_givingBonus = true;
			try
			{
				((MonoBehaviour)this).StartCoroutine(GiveGradually(self, itemIndex, num));
			}
			finally
			{
				_givingBonus = false;
			}
		}

		[IteratorStateMachine(typeof(<GiveGradually>d__17))]
		private IEnumerator GiveGradually(Inventory inventory, ItemIndex itemIndex, int total)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <GiveGradually>d__17(0)
			{
				<>4__this = this,
				inventory = inventory,
				itemIndex = itemIndex,
				total = total
			};
		}

		private bool ShouldMultiply(ItemDef item)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)item.tier == 5)
			{
				return false;
			}
			if (item.hidden)
			{
				return false;
			}
			return true;
		}

		private void OnRemoveItem(orig_RemoveItem_ItemIndex_int orig, Inventory self, ItemIndex itemIndex, int count)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (_removingBonus)
			{
				orig.Invoke(self, itemIndex, count);
				return;
			}
			if (!NetworkServer.active)
			{
				orig.Invoke(self, itemIndex, count);
				return;
			}
			if (fixScrappers.Value && count > 1)
			{
				_removingBonus = true;
				try
				{
					orig.Invoke(self, itemIndex, 1);
					return;
				}
				finally
				{
					_removingBonus = false;
				}
			}
			orig.Invoke(self, itemIndex, count);
		}

		private bool OnCanBeAfforded(orig_CanBeAffordedByInteractor orig, PurchaseInteraction self, Interactor activator)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (!fixCauldrons.Value)
			{
				return orig.Invoke(self, activator);
			}
			int cost = self.cost;
			if (IsItemCost(self.costType) && multiplier.Value > 1)
			{
				self.cost = Mathf.Max(1, cost / multiplier.Value);
			}
			bool result = orig.Invoke(self, activator);
			self.cost = cost;
			return result;
		}

		private void OnCostHologramFixedUpdate(orig_FixedUpdate orig, CostHologramContent self)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			if (!fixCauldrons.Value)
			{
				orig.Invoke(self);
				return;
			}
			PurchaseInteraction componentInParent = ((Component)self).GetComponentInParent<PurchaseInteraction>();
			if ((Object)(object)componentInParent != (Object)null && IsItemCost(componentInParent.costType) && multiplier.Value > 1)
			{
				int cost = componentInParent.cost;
				componentInParent.cost = Mathf.Max(1, cost / multiplier.Value);
				orig.Invoke(self);
				componentInParent.cost = cost;
			}
			else
			{
				orig.Invoke(self);
			}
		}

		private string OnGetContextString(orig_GetContextString orig, PurchaseInteraction self, Interactor activator)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (!fixCauldrons.Value)
			{
				return orig.Invoke(self, activator);
			}
			int cost = self.cost;
			if (IsItemCost(self.costType) && multiplier.Value > 1)
			{
				self.cost = Mathf.Max(1, cost / multiplier.Value);
			}
			string result = orig.Invoke(self, activator);
			self.cost = cost;
			return result;
		}

		private static bool IsItemCost(CostTypeIndex costType)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Invalid comparison between Unknown and I4
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Invalid comparison between Unknown and I4
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Invalid comparison between Unknown and I4
			if ((int)costType != 0 && (int)costType != 1 && (int)costType != 2 && (int)costType != 3)
			{
				return (int)costType != 14;
			}
			return false;
		}

		private void OnBossDeathEnter(orig_OnEnter orig, BossDeath self)
		{
			orig.Invoke(self);
			if (fixMithrix.Value && NetworkServer.active)
			{
				((MonoBehaviour)this).StartCoroutine(FixMithrixItems());
			}
		}

		[IteratorStateMachine(typeof(<FixMithrixItems>d__25))]
		private IEnumerator FixMithrixItems()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <FixMithrixItems>d__25(0)
			{
				<>4__this = this
			};
		}

		private void OnRunStart(orig_Start orig, Run self)
		{
			orig.Invoke(self);
		}

		private void OnBeginStage(orig_BeginStage orig, Run self)
		{
			orig.Invoke(self);
		}
	}
}