Decompiled source of StackSizeMultiplier v1.0.5

StackSizeMultiplier.dll

Decompiled a day ago
using System;
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 BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using SSSGame;
using SandSailorStudio.Inventory;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("blacks7ar")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StackSizeMultiplier")]
[assembly: AssemblyTitle("StackSizeMultiplier")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.0")]
[module: UnverifiableCode]
namespace StackSizeMultiplier;

[BepInPlugin("blacks7ar.StackSizeMultiplier", "StackSizeMultiplier", "1.0.5")]
public class Plugin : BasePlugin
{
	[HarmonyPatch]
	private static class Patches
	{
		private static readonly Dictionary<IntPtr, int> LastAppliedCount = new Dictionary<IntPtr, int>();

		private static readonly Dictionary<IntPtr, int> OriginalQuantity = new Dictionary<IntPtr, int>();

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ItemContainer), "GetStackSize", new Type[] { typeof(ItemInfo) })]
		private static void ItemContainer_GetStackSize_Postfix(ItemContainer __instance, ItemInfo itemInfo, ref int __result)
		{
			if (((__instance != null) ? __instance.Inventory : null) != null && (__instance.Inventory._name.Contains("CharacterAska") || __instance.Inventory._name.Contains("CharacterRagnar")))
			{
				__result = Helper.ApplyMultiplier(itemInfo, __result);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(LootSpawner), "SpawnLootStack")]
		private static void SpawnLootStack_Postfix(LootSpawner __instance, LootData loot, int stackIndex)
		{
			if (_IsExpandedBuildingModInstalled || loot == null)
			{
				return;
			}
			loot.spawnChance = 1f;
			HarvestInteraction harvestInteraction = __instance.harvestInteraction;
			if ((Object)(object)harvestInteraction == (Object)null || (Object)(object)harvestInteraction._itemComponent == (Object)null || harvestInteraction._itemComponent.ItemInstance == null)
			{
				return;
			}
			int count = harvestInteraction._itemComponent.ItemInstance.count;
			IntPtr pointer = ((Il2CppObjectBase)loot).Pointer;
			Il2CppStructArray<int> stacks = loot.stacks;
			if (stacks == null || ((Il2CppArrayBase<int>)(object)stacks).Count == 0)
			{
				return;
			}
			Il2CppStructArray<int> val = new Il2CppStructArray<int>((long)((Il2CppArrayBase<int>)(object)stacks).Length);
			for (int i = 0; i < ((Il2CppArrayBase<int>)(object)stacks).Length; i++)
			{
				if (!OriginalQuantity.TryGetValue(pointer, out var value))
				{
					value = ((Il2CppArrayBase<int>)(object)stacks)[i];
					OriginalQuantity[pointer] = value;
				}
				if (LastAppliedCount.TryGetValue(pointer, out var value2) && value2 == count)
				{
					return;
				}
				int num = ((count != 1) ? (value * count) : value);
				((Il2CppArrayBase<int>)(object)val)[i] = num;
			}
			LastAppliedCount[pointer] = count;
			loot.stacks = val;
		}
	}

	private static class Helper
	{
		public static int ApplyMultiplier(ItemInfo itemInfo, int baseStack)
		{
			if ((Object)(object)itemInfo == (Object)null || (Object)(object)itemInfo.storageClass == (Object)null)
			{
				return baseStack;
			}
			string name = ((Object)itemInfo.storageClass).name;
			int result = baseStack;
			if (itemInfo.localizedName.ToLower().Contains("arrow"))
			{
				return baseStack * _ammoMultiplier.Value;
			}
			if (name.Contains("Small"))
			{
				if (itemInfo.stackSize == 1)
				{
					return baseStack * 10 * _smallMultiplier.Value;
				}
				return baseStack * _smallMultiplier.Value;
			}
			if (name.Contains("Medium"))
			{
				return baseStack * _mediumMultiplier.Value;
			}
			return result;
		}

		public static bool CheckExpandedBuildingStorage()
		{
			if (!((BaseChainloader<BasePlugin>)(object)IL2CPPChainloader.Instance).Plugins.ContainsKey("blacks7ar.ExpandedBuildingStorage"))
			{
				return false;
			}
			SLogger.LogInfo((object)"ExpandedBuildingStorage mod detected, skipping spawn loot patch..");
			return true;
		}
	}

	private const string modGUID = "blacks7ar.StackSizeMultiplier";

	public const string modName = "StackSizeMultiplier";

	public const string modAuthor = "blacks7ar";

	public const string modVersion = "1.0.5";

	private static ConfigEntry<int> _smallMultiplier;

	private static ConfigEntry<int> _mediumMultiplier;

	private static ConfigEntry<int> _ammoMultiplier;

	private static ManualLogSource SLogger;

	private static bool _IsExpandedBuildingModInstalled;

	public override void Load()
	{
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		SLogger = ((BasePlugin)this).Log;
		_smallMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Small Items", 5, "Stack multiplier for small items.");
		_mediumMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Medium Items", 50, "Stack multiplier for medium items.");
		_ammoMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Ammos", 5, "Stack multiplier for ammo items (ie. arrows).");
		SLogger.LogInfo((object)"Mod loaded!");
		_IsExpandedBuildingModInstalled = Helper.CheckExpandedBuildingStorage();
		new Harmony("blacks7ar.StackSizeMultiplier").PatchAll();
	}
}