Decompiled source of NoWeapons by Schleimi v1.0.5

BepInEx/plugins/NoWeapons_by_Schleimi/NoWeapons_by_Schleimi.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace NoWeaponsBySchleimi;

[BepInPlugin("schleimi.noweapons", "No Weapons by Schleimi", "1.0.5")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ShopManager), "GetAllItemsFromStatsManager")]
	private static class ShopFilterPatch
	{
		private static void Postfix(ShopManager __instance)
		{
			if (ActiveOnAuthority() && !((Object)(object)__instance == (Object)null))
			{
				FilterShopList(__instance, "potentialItems");
				FilterShopList(__instance, "potentialItemConsumables");
				FilterShopList(__instance, "potentialItemUpgrades");
				FilterShopList(__instance, "potentialItemHealthPacks");
				FilterSecretShopLists(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(PunManager), "SpawnShopItem")]
	private static class ShopSpawnFilterPatch
	{
		private static void Prefix(List<Item> itemList)
		{
			FilterBeforeShopSpawn(itemList);
		}
	}

	public const string PluginGuid = "schleimi.noweapons";

	public const string PluginName = "No Weapons by Schleimi";

	public const string PluginVersion = "1.0.5";

	internal static Plugin Instance;

	private ConfigEntry<bool> modEnabled;

	private static bool spawnHookLogged;

	private void Awake()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Expected O, but got Unknown
		Instance = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Remove firearms, grenades, mines and melee weapons from the shop only. Staffs/wands remain allowed.");
		Harmony val = new Harmony("schleimi.noweapons");
		val.PatchAll();
		MethodInfo methodInfo = AccessTools.Method(typeof(PunManager), "SpawnShopItem", (Type[])null, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(Plugin), "FilterBeforeShopSpawn", (Type[])null, (Type[])null);
		val.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"No Weapons by Schleimi 1.0.5 loaded.");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Explicit shop-spawn filter installed.");
	}

	private static bool ActiveOnAuthority()
	{
		return (Object)(object)Instance != (Object)null && Instance.modEnabled.Value && (!PhotonNetwork.InRoom || PhotonNetwork.IsMasterClient);
	}

	private static object ReadField(object target, string fieldName)
	{
		if (target == null)
		{
			return null;
		}
		FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		return (field == null) ? null : field.GetValue(target);
	}

	private static bool IsBannedName(string itemName)
	{
		if (string.IsNullOrEmpty(itemName))
		{
			return false;
		}
		string text = itemName.ToLowerInvariant();
		if (text.Contains("staff") || text.Contains("wand"))
		{
			return false;
		}
		return text.Contains("gun") || text.Contains("shotgun") || text.Contains("revolver") || text.Contains("tranq") || text.Contains("grenade") || text.Contains("mine") || text.Contains("sword") || text.Contains("melee") || text.Contains("hammer") || text.Contains("stun_baton") || text.Contains("stun baton") || text.Contains("leafblower") || text.Contains("leaf blower") || text.Contains("cart_cannon") || text.Contains("cart cannon") || text.Contains("cart_laser") || text.Contains("cart laser") || text.Contains("crossbow");
	}

	private static bool IsBannedDefinition(Item item)
	{
		return (Object)(object)item != (Object)null && IsBannedName(((Object)item).name);
	}

	private static bool IsWeaponObject(ItemAttributes attributes)
	{
		if ((Object)(object)attributes == (Object)null)
		{
			return false;
		}
		if (IsBannedName(ReadField(attributes, "itemName") as string) || IsBannedName(((Object)attributes).name))
		{
			return true;
		}
		Component[] componentsInChildren = ((Component)attributes).GetComponentsInChildren<Component>(true);
		foreach (Component val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null))
			{
				string name = ((object)val).GetType().Name;
				if (name.StartsWith("ItemGun", StringComparison.Ordinal) || name.StartsWith("ItemGrenade", StringComparison.Ordinal) || name.StartsWith("ItemMine", StringComparison.Ordinal) || name.StartsWith("ItemMelee", StringComparison.Ordinal) || name == "ItemStunBaton" || name.StartsWith("ItemCrossbow", StringComparison.Ordinal))
				{
					return true;
				}
			}
		}
		return false;
	}

	internal static void RemoveWeaponShopItem(ItemAttributes attributes)
	{
		if (!((Object)(object)attributes == (Object)null) && IsWeaponObject(attributes) && ReadField(attributes, "shopItem") is int num && num != 0)
		{
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Removed shop weapon after spawn: " + ((Object)attributes).name));
			if (PhotonNetwork.InRoom)
			{
				PhotonNetwork.Destroy(((Component)attributes).gameObject);
			}
			else
			{
				Object.Destroy((Object)(object)((Component)attributes).gameObject);
			}
		}
	}

	private static void FilterShopList(object shopManager, string fieldName)
	{
		if (ReadField(shopManager, fieldName) is List<Item> list)
		{
			int num = list.RemoveAll(IsBannedDefinition);
			if (num > 0)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Removed " + num + " weapon entries from the shop pool."));
			}
		}
	}

	private static void FilterSecretShopLists(object shopManager)
	{
		if (!(ReadField(shopManager, "potentialSecretItems") is IDictionary dictionary))
		{
			return;
		}
		foreach (DictionaryEntry item in dictionary)
		{
			if (item.Value is List<Item> list)
			{
				int num = list.RemoveAll(IsBannedDefinition);
				if (num > 0)
				{
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Removed " + num + " weapon entries from a secret shop pool."));
				}
			}
		}
	}

	internal static void FilterBeforeShopSpawn(List<Item> itemList)
	{
		if (ActiveOnAuthority() && itemList != null)
		{
			int count = itemList.Count;
			int num = itemList.RemoveAll(IsBannedDefinition);
			if (!spawnHookLogged)
			{
				spawnHookLogged = true;
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Shop-spawn filter invoked: " + count + " candidates, " + num + " weapons removed."));
			}
			else if (num > 0)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Removed " + num + " weapon entries immediately before shop spawn."));
			}
		}
	}
}
[HarmonyPatch(typeof(ItemAttributes), "Start")]
internal static class ShopWeaponItemPatch
{
	private static void Postfix(ItemAttributes __instance)
	{
		Plugin.RemoveWeaponShopItem(__instance);
	}
}