Decompiled source of Shop Duplicate Unlock v1.0.0

Matsu.ShopDuplicates.dll

Decompiled 10 hours ago
using System;
using System.Collections.Generic;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("Matsu.ShopDuplicates")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Matsu.ShopDuplicates")]
[assembly: AssemblyTitle("Matsu.ShopDuplicates")]
[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.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]
	[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]
	[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 Matsu.ShopDuplicates
{
	[BepInPlugin("com.matsu.shopduplicates", "Shop Duplicate Unlock", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.matsu.shopduplicates";

		public const string Name = "Shop Duplicate Unlock";

		public const string Version = "1.0.0";

		public static ManualLogSource Log;

		public static ConfigEntry<int> MaxCopiesPerItem;

		public static ConfigEntry<bool> IncludeConsumables;

		public static ConfigEntry<bool> IncludeHealthPacks;

		private void Awake()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			MaxCopiesPerItem = ((BaseUnityPlugin)this).Config.Bind<int>("Shop", "MaxCopiesPerItem", 5, "How many times each weapon/item/drone can appear in a single shop (vanilla usually caps them at 1). Higher = more duplicates possible. The shop still only fills its fixed number of slots, so this just removes the 'one of each' restriction.");
			IncludeConsumables = ((BaseUnityPlugin)this).Config.Bind<bool>("Shop", "IncludeConsumables", true, "Also allow duplicate power crystals / consumables.");
			IncludeHealthPacks = ((BaseUnityPlugin)this).Config.Bind<bool>("Shop", "IncludeHealthPacks", true, "Also allow duplicate health packs.");
			new Harmony("com.matsu.shopduplicates").PatchAll();
			Log.LogInfo((object)string.Format("{0} {1} loaded (MaxCopiesPerItem={2}).", "Shop Duplicate Unlock", "1.0.0", MaxCopiesPerItem.Value));
		}
	}
	[HarmonyPatch(typeof(ShopManager), "GetAllItemsFromStatsManager")]
	internal static class ShopDuplicatesPatch
	{
		private static void Postfix(ShopManager __instance)
		{
			try
			{
				int value = Plugin.MaxCopiesPerItem.Value;
				if (value > 1)
				{
					TopUp(__instance.potentialItems, value);
					if (Plugin.IncludeConsumables.Value)
					{
						TopUp(__instance.potentialItemConsumables, value);
					}
					if (Plugin.IncludeHealthPacks.Value)
					{
						TopUp(__instance.potentialItemHealthPacks, value);
					}
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"[ShopDuplicates] error: {arg}");
			}
		}

		private static void TopUp(List<Item>? list, int max)
		{
			if (list == null || list.Count == 0)
			{
				return;
			}
			Dictionary<Item, int> dictionary = new Dictionary<Item, int>();
			foreach (Item item in list)
			{
				if ((Object)(object)item != (Object)null)
				{
					dictionary[item] = ((!dictionary.TryGetValue(item, out var value)) ? 1 : (value + 1));
				}
			}
			List<Item> list2 = new List<Item>();
			foreach (KeyValuePair<Item, int> item2 in dictionary)
			{
				for (int i = item2.Value; i < max; i++)
				{
					list2.Add(item2.Key);
				}
			}
			if (list2.Count != 0)
			{
				list.AddRange(list2);
				for (int num = list.Count - 1; num > 0; num--)
				{
					int num2 = Random.Range(0, num + 1);
					int index = num;
					int index2 = num2;
					Item value2 = list[num2];
					Item value3 = list[num];
					list[index] = value2;
					list[index2] = value3;
				}
			}
		}
	}
}