Decompiled source of ExpandedBuildingStorage v1.0.5

ExpandedBuildingStorage.dll

Decompiled 2 weeks 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.Configuration;
using BepInEx.Core.Logging.Interpolation;
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("ExpandedBuildingStorage")]
[assembly: AssemblyTitle("ExpandedBuildingStorage")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.0")]
[module: UnverifiableCode]
namespace ExpandedBuildingStorage;

[BepInPlugin("blacks7ar.ExpandedBuildingStorage", "ExpandedBuildingStorage", "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, ref int __result)
		{
			if (((__instance != null) ? __instance.Inventory : null) != null && !((Object)(object)__instance.itemConfig == (Object)null))
			{
				__result = Helper.ApplyMultiplier(__instance, __result);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HarvestInteraction), "add_OnHarvestDamageTaken")]
		private static void OnHarvestDamageTaken_Postfix(HarvestInteraction __instance)
		{
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			ItemComponent componentInChildren = ((Component)__instance).GetComponentInChildren<ItemComponent>(true);
			if ((Object)(object)__instance._itemComponent == (Object)null || __instance._itemComponent.ItemInstance == null || !((Object)(object)componentInChildren != (Object)null))
			{
				return;
			}
			int count = __instance._itemComponent.ItemInstance.count;
			IntPtr pointer = ((Il2CppObjectBase)__instance).Pointer;
			if (!OriginalQuantity.TryGetValue(pointer, out var value))
			{
				value = componentInChildren.data.quantity;
				OriginalQuantity[pointer] = value;
			}
			if (!LastAppliedCount.TryGetValue(pointer, out var value2) || value2 != count)
			{
				int quantity = ((count != 1) ? (value * count) : value);
				componentInChildren.data.quantity = quantity;
				LastAppliedCount[pointer] = count;
				ManualLogSource eLogger = ELogger;
				bool flag = default(bool);
				BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(19, 4, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(((Object)__instance._itemComponent.ItemInstance.info).name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" count: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(count);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" / ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(((Object)componentInChildren.data.itemInfo).name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" count: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(componentInChildren.data.quantity);
				}
				eLogger.LogDebug(val);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(LootSpawner), "SpawnLootStack")]
		private static void SpawnLootStack_Postfix(LootSpawner __instance, LootData loot, int stackIndex)
		{
			if (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(ItemContainer container, int baseStack)
		{
			if (((container != null) ? container.Inventory : null) == null)
			{
				return baseStack;
			}
			if (IsSmallItemsSingle(container))
			{
				return baseStack + 10 * _smallMultiplier.Value;
			}
			if (IsSmallItems(container))
			{
				return baseStack * _smallMultiplier.Value;
			}
			if (IsMediumItems(container))
			{
				return baseStack * _mediumMultiplier.Value;
			}
			if (!IsLargeItems(container))
			{
				return baseStack;
			}
			return baseStack * _largeMultiplier.Value;
		}

		private static bool IsMediumItems(ItemContainer container)
		{
			string name = ((Object)container.itemConfig).name;
			int num;
			switch (name)
			{
			default:
				num = ((!(name == "Storage_WorkshopLinen")) ? 1 : 0);
				break;
			case "Storage_Wood_Bark":
			case "Storage_Wood_Sticks":
			case "Storage_Wood_Firewood":
			case "Storage_Wood_Thatch":
			case "Storage_Wood_LongSticks":
			case "Fishing_WoodRodFishes":
			case "Storage_FurnitureFireWood":
			case "Storage_Horns":
			case "Storage_Leather_HidePelt_T2":
			case "Storage_Food_SmolkrPenHorns":
			case "Storage_Arrows":
			case "Storage_ClothingMaterials":
			case "Storage_WorkshopMediumItems_L1":
			case "Storage_Leather_HidePelt_T1":
			case "Storage_Leather_CuredHidePeltT1":
			case "Storage_Leather_CuredHidePeltT2":
				num = 0;
				break;
			}
			return num == 0;
		}

		private static bool IsSmallItems(ItemContainer container)
		{
			string name = ((Object)container.itemConfig).name;
			int num;
			switch (name)
			{
			default:
				num = ((!(name == "Storage_Crawler_Resources")) ? 1 : 0);
				break;
			case "Storage_Seeds":
			case "Storage_FoodFarm":
			case "Storage_Wood_Fibers":
			case "Fishing_Baits":
			case "Storage_FoodAll":
			case "Storage_AllNonFood":
			case "Storage_WorkshopSmallItems":
			case "Storage_Vegetables":
			case "Storage_Fruits":
			case "Storage_GathererMaterials":
			case "Storage_Leather_Scraps":
			case "Storage_Food_RawRedMeat_Bone":
			case "Storage_WarehouseResin":
			case "Storage_Stone_Small":
			case "Storage_Food_WarehouseCheese":
			case "Storage_CoalMakerPile_Output":
			case "Storage_Food_Cooked":
			case "Storage_WarehouseCrawlerResources":
			case "Storage_WarehouseDye":
			case "Storage_WarehouseSmallCraftingMaterials":
			case "Storage_WarehouseFoodSilo":
			case "Storage_Wood_FibersResinConeReedsSeeds":
			case "Storage_WorkshopSmallItems_L1":
			case "Storage_WorkshopLinenThread":
				num = 0;
				break;
			}
			return num == 0;
		}

		private static bool IsSmallItemsSingle(ItemContainer container)
		{
			string name = ((Object)container.itemConfig).name;
			int num;
			switch (name)
			{
			default:
				num = ((!(name == "Storage_Bloomery_Ore_L1")) ? 1 : 0);
				break;
			case "Storage_WarehouseJotunBlood":
			case "Warehouse_Storage_OreBloom":
			case "Storage_Carpenter_SmallOutput":
				num = 0;
				break;
			}
			return num == 0;
		}

		private static bool IsLargeItems(ItemContainer container)
		{
			string name = ((Object)container.itemConfig).name;
			int num;
			switch (name)
			{
			default:
				num = ((!(name == "Storage_Wood_HardWoodLongSticks")) ? 1 : 0);
				break;
			case "Storage_Food_DeerLeg":
			case "Storage_Stone_Raw":
			case "Storage_Carpenter_LargeOutput":
			case "Storage_Carpenter_LongOutput":
			case "Storage_ExplodingSack":
			case "Storage_Wood_Timber":
			case "Storage_Wood_LongSticks":
			case "Storage_Wood_LongSticksL0":
			case "Storage_Wood_HardWoodLog":
				num = 0;
				break;
			}
			return num == 0;
		}
	}

	private const string modGUID = "blacks7ar.ExpandedBuildingStorage";

	public const string modName = "ExpandedBuildingStorage";

	public const string modAuthor = "blacks7ar";

	public const string modVersion = "1.0.5";

	private static ManualLogSource ELogger;

	private static ConfigEntry<int> _smallMultiplier;

	private static ConfigEntry<int> _mediumMultiplier;

	private static ConfigEntry<int> _largeMultiplier;

	public override void Load()
	{
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		ELogger = ((BasePlugin)this).Log;
		ELogger.LogInfo((object)"Mod loaded!");
		_smallMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Small Items", 3, "Stack multiplier for small items.");
		_mediumMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Medium Items", 10, "Stack multiplier for Medium items.");
		_largeMultiplier = ((BasePlugin)this).Config.Bind<int>("General", "Large Items", 5, "Stack multiplier for large items.");
		new Harmony("blacks7ar.ExpandedBuildingStorage").PatchAll();
	}
}