Decompiled source of Spawnable Store Items v0.1.2

NicholaScott.LethalCompany.SpawnableItems.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using NicholaScott.BepInEx.Utils.Configuration;
using NicholaScott.BepInEx.Utils.Instancing;
using NicholaScott.BepInEx.Utils.Patching;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("NicholaScott.LethalCompany.SpawnableItems")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NicholaScott.LethalCompany.SpawnableItems")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6A25689C-CA4B-4FB5-98D4-300354668DC9")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NicholaScott.LethalCompany.SpawnableItems;

[BepInDependency("NicholaScott.BepInEx.Utils", "1.2.1")]
[BepInPlugin("NicholaScott.LethalCompany.SpawnableStoreItems", "Spawnable Store Items", "0.1.2")]
public class SpawnableStoreItems : BaseUnityPlugin
{
	public struct Configuration
	{
		[ConfigEntryDefinition(Category = "General", Description = "Maximum number of items that can spawn inside the factory.")]
		public int MaxItemsSpawningInside;

		[ConfigEntryDefinition(Category = "WIP/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Binoculars;

		[ConfigEntryDefinition(Category = "WIP/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Mapper;

		[ConfigEntryDefinition(Name = "ShotgunShell", Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Ammo;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int WalkieTalkie;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Flashlight;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Shovel;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int LockPicker;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int ProFlashlight;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int StunGrenade;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Boombox;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int ExtensionLadder;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int RadarBooster;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int SprayPaint;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int TZPInhalant;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int ZapGun;

		[ConfigEntryDefinition(Category = "General/RelativeRarities", Description = "The rarity relative to all other entries. Set to 0 to disable this item")]
		public int Jetpack;
	}

	private const string GenRelCategory = "General/RelativeRarities";

	private const string WipRelCategory = "WIP/RelativeRarities";

	private const string GenDescription = "The rarity relative to all other entries. Set to 0 to disable this item";

	private void Awake()
	{
		Singleton<SpawnableStoreItems>.Instance = this;
		Singleton<SpawnableStoreItems, Configuration>.Configuration = Extensions.BindStruct<Configuration>(((BaseUnityPlugin)this).Config, new Configuration
		{
			MaxItemsSpawningInside = 30,
			Binoculars = 0,
			Mapper = 0,
			Ammo = 5,
			WalkieTalkie = 0,
			Flashlight = 15,
			Shovel = 0,
			LockPicker = 0,
			ProFlashlight = 0,
			StunGrenade = 0,
			Boombox = 1,
			ExtensionLadder = 5,
			RadarBooster = 0,
			SprayPaint = 20,
			TZPInhalant = 1,
			ZapGun = 0,
			Jetpack = 0
		});
		Extensions.PatchAttribute<Production>(Assembly.GetExecutingAssembly(), ((BaseUnityPlugin)this).Info.Metadata.GUID, (Action<object>)((BaseUnityPlugin)this).Logger.LogInfo);
	}
}
[Production]
public static class SpawnPatcher
{
	private static Dictionary<string, CachedItem<int>> cachedConfigRarities = new Dictionary<string, CachedItem<int>>();

	private static SpawnableItemWithRarity ItemQualifies(Item item)
	{
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: 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_00b3: Expected O, but got Unknown
		Type cfgType = typeof(SpawnableStoreItems.Configuration);
		SpawnableStoreItems.Configuration cfg = Singleton<SpawnableStoreItems, SpawnableStoreItems.Configuration>.Configuration;
		string cleanItemName = item.itemName.Replace("-", "").Replace(" ", "").ToLower();
		if (!cachedConfigRarities.ContainsKey(cleanItemName))
		{
			cachedConfigRarities.Add(cleanItemName, new CachedItem<int>((InstanceLocator<int>)delegate
			{
				FieldInfo[] source = (from it in cfgType.GetFields()
					where it.Name.ToLower() == cleanItemName
					select it).ToArray();
				return source.Any() ? ((int)source.First().GetValue(cfg)) : 0;
			}, (ItemNotNull<int>)null));
		}
		SpawnableItemWithRarity val = new SpawnableItemWithRarity
		{
			rarity = CachedItem<int>.op_Implicit(cachedConfigRarities[cleanItemName]),
			spawnableItem = item
		};
		Singleton<SpawnableStoreItems>.Logger.LogInfo((object)$"[{val.rarity}] {item.itemName}");
		return val;
	}

	private static int GetRandomWeightedIndex(Random random, int[] weights)
	{
		float num = weights.Where((int t) => t >= 0).Sum();
		if (num <= 0f)
		{
			return random.Next(0, weights.Length);
		}
		float num2 = (float)random.NextDouble();
		float num3 = 0f;
		for (int i = 0; i < weights.Length; i++)
		{
			if (weights[i] > 0)
			{
				num3 += (float)weights[i] / num;
				if (num3 >= num2)
				{
					return i;
				}
			}
		}
		return random.Next(0, weights.Length);
	}

	[HarmonyPatch(typeof(RoundManager), "SpawnScrapInLevel")]
	[HarmonyPostfix]
	public static void SpawnStoreItemsInsideFactory(RoundManager __instance)
	{
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		SpawnableItemWithRarity[] array = StartOfRound.Instance.allItemsList.itemsList.Where((Item item) => !item.isScrap && Object.op_Implicit((Object)(object)item.spawnPrefab)).Select(ItemQualifies).ToArray();
		int[] weights = array.Select((SpawnableItemWithRarity f) => f.rarity).ToArray();
		List<RandomScrapSpawn> list = (from s in Object.FindObjectsOfType<RandomScrapSpawn>()
			where !s.spawnUsed
			select s).ToList();
		Random random = new Random(StartOfRound.Instance.randomMapSeed - 7);
		int num = random.Next(Singleton<SpawnableStoreItems, SpawnableStoreItems.Configuration>.Configuration.MaxItemsSpawningInside / 2, Singleton<SpawnableStoreItems, SpawnableStoreItems.Configuration>.Configuration.MaxItemsSpawningInside);
		for (int i = 0; i < num; i++)
		{
			if (list.Count <= 0)
			{
				break;
			}
			int index = random.Next(0, list.Count);
			RandomScrapSpawn val = list[index];
			Vector3 val2 = ((Component)val).transform.position;
			if (val.spawnedItemsCopyPosition)
			{
				list.RemoveAt(index);
			}
			else
			{
				val2 = RoundManager.Instance.GetRandomNavMeshPositionInRadiusSpherical(((Component)val).transform.position, val.itemSpawnRange, RoundManager.Instance.navHit);
			}
			int randomWeightedIndex = GetRandomWeightedIndex(random, weights);
			GameObject val3 = Object.Instantiate<GameObject>(array[randomWeightedIndex].spawnableItem.spawnPrefab, val2 + Vector3.up * 0.5f, Quaternion.identity, StartOfRound.Instance.propsContainer);
			val3.GetComponent<GrabbableObject>().fallTime = 0f;
			val3.GetComponent<NetworkObject>().Spawn(false);
		}
	}
}