Decompiled source of NoSeveredScrap v1.0.3

BepInEx/plugins/NoSeveredScrap.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("NoSeveredScrap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NoSeveredScrap")]
[assembly: AssemblyTitle("NoSeveredScrap")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NoSeveredScrap
{
	[BepInPlugin("NoSeveredScrap", "NoSeveredScrap", "1.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private static readonly string[] Words = new string[18]
		{
			"severed", "hand", "foot", "feet", "heart", "ear", "tongue", "thigh", "bone", "rib",
			"skull", "finger", "toe", "eye", "organ", "kidney", "liver", "brain"
		};

		private static readonly string[] Allowed = new string[17]
		{
			"handgun", "shotgun", "hairdryer", "hair dryer", "earbud", "gear", "cog", "pearl", "bell", "horn",
			"teeth", "candy", "heartbeat", "steering", "toenail", "stop sign", "tea kettle"
		};

		private const int MinScrapTypes = 8;

		private static readonly HashSet<string> NotRandomScrap = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Apparatus", "Hive", "Body", "Kitchen knife", "Shotgun", "Gift", "Zed Dog", "Golden bar", "Clipboard", "Sticky note" };

		internal static bool IsBodyPart(Item item)
		{
			if ((Object)(object)item == (Object)null)
			{
				return false;
			}
			string[] array = new string[3]
			{
				item.itemName,
				((Object)item).name,
				((Object)(object)item.spawnPrefab != (Object)null) ? ((Object)item.spawnPrefab).name : null
			};
			foreach (string text in array)
			{
				if (string.IsNullOrEmpty(text))
				{
					continue;
				}
				string n = text.ToLowerInvariant();
				if (!Allowed.Any((string a) => n.Contains(a)))
				{
					if (n.Contains("severed"))
					{
						return true;
					}
					if ((from t in Regex.Split(text, "[^A-Za-z]+|(?<=[a-z])(?=[A-Z])")
						select t.ToLowerInvariant()).Any((string t) => Words.Contains(t) || Words.Contains(t.TrimEnd('s'))))
					{
						return true;
					}
				}
			}
			return false;
		}

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("NoSeveredScrap").PatchAll();
			Log.LogInfo((object)"NoSeveredScrap carregado.");
		}

		internal static bool IsBlocked(SpawnableItemWithRarity s)
		{
			if ((Object)(object)s?.spawnableItem != (Object)null)
			{
				return IsBodyPart(s.spawnableItem);
			}
			return false;
		}

		internal static void Filter(SelectableLevel level)
		{
			if (level?.spawnableScrap == null || !level.spawnableScrap.Any(IsBlocked))
			{
				return;
			}
			List<SpawnableItemWithRarity> list = level.spawnableScrap.Where((SpawnableItemWithRarity s) => !IsBlocked(s)).ToList();
			if (list.Count < 8)
			{
				HashSet<Item> have = new HashSet<Item>(list.Select((SpawnableItemWithRarity s) => s.spawnableItem));
				List<SpawnableItemWithRarity> list2 = (from s in FallbackScrap()
					where !have.Contains(s.spawnableItem)
					select s).ToList();
				list.AddRange(list2);
				Log.LogInfo((object)($"{level.PlanetName}: adicionados {list2.Count} itens normais: " + string.Join(", ", list2.Select((SpawnableItemWithRarity s) => s.spawnableItem.itemName))));
			}
			if (list.Count != 0)
			{
				int val = level.spawnableScrap.Count - list.Count;
				Log.LogInfo((object)(level.PlanetName + ": removendo " + string.Join(", ", from s in level.spawnableScrap.Where(IsBlocked)
					select s.spawnableItem.itemName + " [" + ((Object)s.spawnableItem).name + "]")));
				level.spawnableScrap = list;
				Log.LogInfo((object)$"{level.PlanetName}: itens de corpo removidos ({Math.Max(val, 0)}).");
			}
		}

		private static List<SpawnableItemWithRarity> FallbackScrap()
		{
			List<Item> list = StartOfRound.Instance?.allItemsList?.itemsList;
			if (list == null)
			{
				return new List<SpawnableItemWithRarity>();
			}
			return (from i in list
				where (Object)(object)i != (Object)null && i.isScrap && (Object)(object)i.spawnPrefab != (Object)null && !IsBodyPart(i) && !NotRandomScrap.Contains(i.itemName.Trim())
				group i by i.itemName into g
				select g.First()).Select((Func<Item, SpawnableItemWithRarity>)((Item i) => new SpawnableItemWithRarity(i, 10))).ToList();
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "Start")]
	internal static class StartOfRoundPatch
	{
		private static void Postfix(StartOfRound __instance)
		{
			if (__instance.levels != null)
			{
				SelectableLevel[] levels = __instance.levels;
				for (int i = 0; i < levels.Length; i++)
				{
					Plugin.Filter(levels[i]);
				}
			}
		}
	}
	[HarmonyPatch(typeof(RoundManager), "SpawnScrapInLevel")]
	internal static class SpawnScrapPatch
	{
		private static void Prefix(RoundManager __instance)
		{
			SelectableLevel currentLevel = __instance.currentLevel;
			if (currentLevel?.spawnableScrap != null && currentLevel.spawnableScrap.Any(Plugin.IsBlocked))
			{
				int increasedScrapSpawnRateIndex = __instance.increasedScrapSpawnRateIndex;
				SpawnableItemWithRarity val = ((increasedScrapSpawnRateIndex >= 0 && increasedScrapSpawnRateIndex < currentLevel.spawnableScrap.Count) ? currentLevel.spawnableScrap[increasedScrapSpawnRateIndex] : null);
				Plugin.Filter(currentLevel);
				__instance.increasedScrapSpawnRateIndex = ((val == null) ? (-1) : currentLevel.spawnableScrap.IndexOf(val));
			}
		}
	}
}