Decompiled source of DisableHalfItemsOnDisableAllSkills v1.0.1

DisableHalfItems.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace DisableHalfItems;

[BepInPlugin("binma.disablehalfitems", "Disable Half Items On Disable All Skills", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	internal static ConfigEntry<float> Fraction;

	internal static ConfigEntry<bool> PlayersOnly;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Fraction = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Fraction of items disabled", 0.5f, "Share of your item types that get disabled while the debuff lasts (0.5 = half, rounded up). Chosen at random each time.");
		PlayersOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Players only", true, "Only affect player characters. Turn off to also disable items on enemies or allies hit by the debuff.");
		new Harmony("binma.disablehalfitems").PatchAll(typeof(Plugin).Assembly);
	}

	internal static bool IsDebuff(BuffDef def)
	{
		return Object.op_Implicit((Object)(object)def) && (Object)(object)def == (Object)(object)Buffs.DisableAllSkills;
	}
}
[HarmonyPatch(typeof(CharacterBody), "OnBuffFirstStackGained")]
internal static class GainPatch
{
	private static void Postfix(CharacterBody __instance, BuffDef buffDef)
	{
		if (NetworkServer.active && Plugin.IsDebuff(buffDef) && (!Plugin.PlayersOnly.Value || __instance.isPlayerControlled))
		{
			Inventory inventory = __instance.inventory;
			if (Object.op_Implicit((Object)(object)inventory))
			{
				HalfItemDisabler halfItemDisabler = ((Component)__instance).GetComponent<HalfItemDisabler>() ?? ((Component)__instance).gameObject.AddComponent<HalfItemDisabler>();
				halfItemDisabler.Disable(inventory);
			}
		}
	}
}
[HarmonyPatch(typeof(CharacterBody), "OnBuffFinalStackLost")]
internal static class LossPatch
{
	private static void Postfix(CharacterBody __instance, BuffDef buffDef)
	{
		if (NetworkServer.active && Plugin.IsDebuff(buffDef))
		{
			HalfItemDisabler component = ((Component)__instance).GetComponent<HalfItemDisabler>();
			if (Object.op_Implicit((Object)(object)component))
			{
				component.Restore();
			}
		}
	}
}
internal class HalfItemDisabler : MonoBehaviour
{
	private Inventory inventory;

	private readonly List<ItemIndex> disabled = new List<ItemIndex>();

	internal void Disable(Inventory inv)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Invalid comparison between Unknown and I4
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		Restore();
		inventory = inv;
		List<ItemIndex> list = new List<ItemIndex>();
		foreach (ItemIndex item in inv.itemAcquisitionOrder)
		{
			ItemDef itemDef = ItemCatalog.GetItemDef(item);
			if (Object.op_Implicit((Object)(object)itemDef) && !itemDef.hidden && (int)itemDef.tier != 5 && inv.GetItemCountTotal(item) > 0 && !inv.IsItemDisabled(item))
			{
				list.Add(item);
			}
		}
		int num = Mathf.CeilToInt((float)list.Count * Mathf.Clamp01(Plugin.Fraction.Value) - 0.0001f);
		for (int i = 0; i < num; i++)
		{
			if (list.Count <= 0)
			{
				break;
			}
			int index = Random.Range(0, list.Count);
			ItemIndex val = list[index];
			list.RemoveAt(index);
			inv.AddItemDisabler(val);
			disabled.Add(val);
		}
	}

	internal void Restore()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)inventory) && NetworkServer.active)
		{
			foreach (ItemIndex item in disabled)
			{
				inventory.RemoveItemDisabler(item);
			}
		}
		disabled.Clear();
	}

	private void OnDestroy()
	{
		try
		{
			Restore();
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)ex);
		}
	}
}