Decompiled source of MiggyClearClutter v1.0.0

BepInEx/plugins/ClearClutterPlugin.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using ClearClutterPlugin.Cleanup;
using Microsoft.CodeAnalysis;
using StonewardsPickupCleaner.Config;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ClearClutterPlugin")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Clear Clutter Plugin")]
[assembly: AssemblyTitle("ClearClutterPlugin")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 StonewardsPickupCleaner
{
	[BepInPlugin("com.Miggy.stonewards.pickupcleaner", "Stonewards Pickup Cleaner", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.Miggy.stonewards.pickupcleaner";

		public const string PluginName = "Stonewards Pickup Cleaner";

		public const string PluginVersion = "0.1.0";

		private ConfigEntry<string> openMenuKey;

		private ConfigEntry<string> clearScrapKey;

		private ConfigEntry<string> clearMaterialsKey;

		private ConfigEntry<string> clearEquipmentKey;

		private ConfigEntry<string> clearClutterKey;

		private ConfigEntry<string> clearEverythingKey;

		private KeyBinding openMenuBinding;

		private KeyBinding clearScrapBinding;

		private KeyBinding clearMaterialsBinding;

		private KeyBinding clearEquipmentBinding;

		private KeyBinding clearClutterBinding;

		private KeyBinding clearEverythingBinding;

		private string lastOpenMenuKey;

		private string lastClearScrapKey;

		private string lastClearMaterialsKey;

		private string lastClearEquipmentKey;

		private string lastClearClutterKey;

		private string lastClearEverythingKey;

		private bool showMenu;

		private bool showClearEverythingConfirmation;

		private void Awake()
		{
			openMenuKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Open Menu", "F3", "Key combination used to open and close the cleanup menu.");
			clearScrapKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Clear Scrap", "Ctrl+F1", "Key combination used to clear scrap.");
			clearMaterialsKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Clear Materials", "Ctrl+F2", "Key combination used to clear materials.");
			clearEquipmentKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Clear Weapons & Equipment", "Ctrl+F3", "Key combination used to clear weapons and equipment.");
			clearClutterKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Clear Clutter", "Ctrl+F4", "Key combination used to clear scrap, materials, weapons and equipment.");
			clearEverythingKey = ((BaseUnityPlugin)this).Config.Bind<string>("Key Bindings", "Clear Everything", "Ctrl+Shift+F4", "Key combination used to open the Clear Everything confirmation.");
			RebuildBindings();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Stonewards Pickup Cleaner 0.1.0 loaded.");
		}

		private void Update()
		{
			RebuildBindingsIfChanged();
			if (openMenuBinding.WasPressed())
			{
				showMenu = !showMenu;
				if (!showMenu)
				{
					showClearEverythingConfirmation = false;
				}
			}
			if (clearScrapBinding.WasPressed())
			{
				PickupDestroyer.DestroyScrap();
			}
			if (clearMaterialsBinding.WasPressed())
			{
				PickupDestroyer.DestroyMaterials();
			}
			if (clearEquipmentBinding.WasPressed())
			{
				PickupDestroyer.DestroyEquipment();
			}
			if (clearClutterBinding.WasPressed())
			{
				PickupDestroyer.DestroyScrap();
				PickupDestroyer.DestroyMaterials();
				PickupDestroyer.DestroyEquipment();
			}
			if (clearEverythingBinding.WasPressed())
			{
				showMenu = true;
				showClearEverythingConfirmation = true;
			}
		}

		private void OnGUI()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: 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_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			if (!showMenu)
			{
				return;
			}
			GUI.Box(new Rect(20f, 20f, 320f, 340f), "CLEAR ENTITIES");
			if (GUI.Button(new Rect(40f, 60f, 280f, 40f), "Clear Scrap"))
			{
				PickupDestroyer.DestroyScrap();
			}
			if (GUI.Button(new Rect(40f, 110f, 280f, 40f), "Clear Materials"))
			{
				PickupDestroyer.DestroyMaterials();
			}
			if (GUI.Button(new Rect(40f, 160f, 280f, 40f), "Clear Weapons & Equipment"))
			{
				PickupDestroyer.DestroyEquipment();
			}
			if (GUI.Button(new Rect(40f, 210f, 280f, 40f), "Clear Clutter"))
			{
				PickupDestroyer.DestroyScrap();
				PickupDestroyer.DestroyMaterials();
				PickupDestroyer.DestroyEquipment();
			}
			if (GUI.Button(new Rect(40f, 265f, 280f, 40f), "CLEAR EVERYTHING"))
			{
				showClearEverythingConfirmation = true;
			}
			if (GUI.Button(new Rect(40f, 315f, 280f, 25f), "CANCEL"))
			{
				showMenu = false;
				showClearEverythingConfirmation = false;
			}
			if (showClearEverythingConfirmation)
			{
				GUI.color = new Color(0f, 0f, 0f, 1f);
				GUI.Box(new Rect(360f, 80f, 400f, 220f), "");
				GUI.color = Color.white;
				GUI.Label(new Rect(390f, 110f, 340f, 30f), "ARE YOU SURE?");
				GUI.Label(new Rect(390f, 150f, 340f, 30f), "You are removing all items.");
				GUI.Label(new Rect(390f, 175f, 340f, 30f), "Tomes, arrows, and bombs will also be removed.");
				if (GUI.Button(new Rect(390f, 220f, 155f, 40f), "CLEAR EVERYTHING"))
				{
					PickupDestroyer.DestroyEverything();
					showClearEverythingConfirmation = false;
				}
				if (GUI.Button(new Rect(565f, 220f, 115f, 40f), "CANCEL"))
				{
					showClearEverythingConfirmation = false;
				}
			}
		}

		private void RebuildBindings()
		{
			openMenuBinding = new KeyBinding(openMenuKey.Value);
			clearScrapBinding = new KeyBinding(clearScrapKey.Value);
			clearMaterialsBinding = new KeyBinding(clearMaterialsKey.Value);
			clearEquipmentBinding = new KeyBinding(clearEquipmentKey.Value);
			clearClutterBinding = new KeyBinding(clearClutterKey.Value);
			clearEverythingBinding = new KeyBinding(clearEverythingKey.Value);
			lastOpenMenuKey = openMenuKey.Value;
			lastClearScrapKey = clearScrapKey.Value;
			lastClearMaterialsKey = clearMaterialsKey.Value;
			lastClearEquipmentKey = clearEquipmentKey.Value;
			lastClearClutterKey = clearClutterKey.Value;
			lastClearEverythingKey = clearEverythingKey.Value;
		}

		private void RebuildBindingsIfChanged()
		{
			if (openMenuKey.Value != lastOpenMenuKey || clearScrapKey.Value != lastClearScrapKey || clearMaterialsKey.Value != lastClearMaterialsKey || clearEquipmentKey.Value != lastClearEquipmentKey || clearClutterKey.Value != lastClearClutterKey || clearEverythingKey.Value != lastClearEverythingKey)
			{
				RebuildBindings();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[ClearClutter] Key bindings reloaded.");
			}
		}
	}
}
namespace StonewardsPickupCleaner.Config
{
	public class KeyBinding
	{
		private readonly string binding;

		private KeyCode key;

		private bool ctrl;

		private bool shift;

		private bool alt;

		public KeyBinding(string binding)
		{
			this.binding = binding;
			Parse(binding);
		}

		public bool WasPressed()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if ((int)key == 0)
			{
				return false;
			}
			if (!Input.GetKeyDown(key))
			{
				return false;
			}
			if (ctrl != (Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305)))
			{
				return false;
			}
			if (shift != (Input.GetKey((KeyCode)304) || Input.GetKey((KeyCode)303)))
			{
				return false;
			}
			if (alt != (Input.GetKey((KeyCode)308) || Input.GetKey((KeyCode)307)))
			{
				return false;
			}
			return true;
		}

		private void Parse(string value)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			key = (KeyCode)0;
			ctrl = false;
			shift = false;
			alt = false;
			if (string.IsNullOrWhiteSpace(value))
			{
				return;
			}
			string[] array = value.Split(new char[1] { '+' });
			string[] array2 = array;
			foreach (string text in array2)
			{
				string text2 = text.Trim();
				KeyCode result;
				if (text2.Equals("Ctrl", StringComparison.OrdinalIgnoreCase) || text2.Equals("Control", StringComparison.OrdinalIgnoreCase))
				{
					ctrl = true;
				}
				else if (text2.Equals("Shift", StringComparison.OrdinalIgnoreCase))
				{
					shift = true;
				}
				else if (text2.Equals("Alt", StringComparison.OrdinalIgnoreCase))
				{
					alt = true;
				}
				else if (Enum.TryParse<KeyCode>(text2, ignoreCase: true, out result))
				{
					key = result;
				}
			}
		}
	}
}
namespace ClearClutterPlugin
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ClearClutterPlugin";

		public const string PLUGIN_NAME = "Clear Clutter Plugin";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace ClearClutterPlugin.Cleanup
{
	public static class PickupDestroyer
	{
		public static int DestroyScrap()
		{
			PickableItem[] array = Object.FindObjectsByType<PickableItem>((FindObjectsSortMode)0);
			int num = 0;
			PickableItem[] array2 = array;
			foreach (PickableItem val in array2)
			{
				if (!((Object)(object)val == (Object)null) && PickupScanner.Classify(val) == PickupCategory.Scrap && CanDestroy(val) && !((Object)(object)ItemManager.Instance == (Object)null))
				{
					ItemManager.Instance.CmdDestroyPickableItem(val);
					num++;
				}
			}
			Debug.Log((object)("[ClearClutter] Clear Scrap requested destruction of " + num + " pickups."));
			return num;
		}

		private static bool CanDestroy(PickableItem pickup)
		{
			if ((Object)(object)pickup == (Object)null)
			{
				return false;
			}
			if (pickup.NetworkSyncPlayerID != 0)
			{
				return false;
			}
			return true;
		}

		public static int DestroyMaterials()
		{
			PickableItem[] array = Object.FindObjectsByType<PickableItem>((FindObjectsSortMode)0);
			int num = 0;
			PickableItem[] array2 = array;
			foreach (PickableItem val in array2)
			{
				if (!((Object)(object)val == (Object)null) && PickupScanner.Classify(val) == PickupCategory.Material && CanDestroy(val) && !((Object)(object)ItemManager.Instance == (Object)null))
				{
					ItemManager.Instance.CmdDestroyPickableItem(val);
					num++;
				}
			}
			Debug.Log((object)("[ClearClutter] Clear Materials requested destruction of " + num + " pickups."));
			return num;
		}

		public static int DestroyEquipment()
		{
			PickableItem[] array = Object.FindObjectsByType<PickableItem>((FindObjectsSortMode)0);
			int num = 0;
			PickableItem[] array2 = array;
			foreach (PickableItem val in array2)
			{
				if (!((Object)(object)val == (Object)null) && PickupScanner.Classify(val) == PickupCategory.Equipment && CanDestroy(val) && !((Object)(object)ItemManager.Instance == (Object)null))
				{
					ItemManager.Instance.CmdDestroyPickableItem(val);
					num++;
				}
			}
			Debug.Log((object)("[ClearClutter] Clear Weapons & Equipment requested destruction of " + num + " pickups."));
			return num;
		}

		public static int DestroyEverything()
		{
			PickableItem[] array = Object.FindObjectsByType<PickableItem>((FindObjectsSortMode)0);
			int num = 0;
			PickableItem[] array2 = array;
			foreach (PickableItem val in array2)
			{
				if (!((Object)(object)val == (Object)null) && CanDestroy(val) && !((Object)(object)ItemManager.Instance == (Object)null))
				{
					ItemManager.Instance.CmdDestroyPickableItem(val);
					num++;
				}
			}
			Debug.Log((object)("[ClearClutter] Clear Everything requested destruction of " + num + " pickups."));
			return num;
		}
	}
	public enum PickupCategory
	{
		Keep,
		Scrap,
		Material,
		Equipment
	}
	public static class PickupScanner
	{
		public static List<PickableItem> FindAllPickups()
		{
			PickableItem[] collection = Object.FindObjectsByType<PickableItem>((FindObjectsSortMode)0);
			return new List<PickableItem>(collection);
		}

		public static PickupCategory Classify(PickableItem pickup)
		{
			if ((Object)(object)pickup == (Object)null)
			{
				return PickupCategory.Keep;
			}
			if (pickup.NetworkSyncPlayerID != 0)
			{
				return PickupCategory.Keep;
			}
			if (pickup is PickableUpgrade)
			{
				return PickupCategory.Keep;
			}
			if (pickup is PickableLamp)
			{
				return PickupCategory.Keep;
			}
			ItemDataSO val = pickup.ItemDataSO;
			if ((Object)(object)val == (Object)null)
			{
				if ((Object)(object)ItemManager.Instance == (Object)null)
				{
					return PickupCategory.Keep;
				}
				string networkSyncItemID = pickup.NetworkSyncItemID;
				if (string.IsNullOrEmpty(networkSyncItemID))
				{
					return PickupCategory.Keep;
				}
				val = ItemManager.Instance.GetItemDataSoByID(networkSyncItemID);
				if ((Object)(object)val == (Object)null)
				{
					return PickupCategory.Keep;
				}
			}
			if (pickup is ScrapPickableItem)
			{
				return PickupCategory.Scrap;
			}
			if (IsForgeMaterial(val.itemID))
			{
				return PickupCategory.Material;
			}
			if (val is WeaponDataSO)
			{
				return PickupCategory.Equipment;
			}
			if (val is ParchmentDataSO)
			{
				return PickupCategory.Equipment;
			}
			if (val is DrinkDataSO)
			{
				return PickupCategory.Equipment;
			}
			if (pickup is PickableBomb)
			{
				return PickupCategory.Keep;
			}
			return PickupCategory.Keep;
		}

		private static bool IsForgeMaterial(string itemID)
		{
			if (string.IsNullOrEmpty(itemID))
			{
				return false;
			}
			Forge val = Object.FindAnyObjectByType<Forge>();
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			return val.IsPlaceable(itemID);
		}

		public static void LogClassification()
		{
			List<PickableItem> list = FindAllPickups();
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			Debug.Log((object)"[ClearClutter] ===== Pickup Scan =====");
			foreach (PickableItem item in list)
			{
				if (!((Object)(object)item == (Object)null))
				{
					PickupCategory pickupCategory = Classify(item);
					string name = ((object)item).GetType().Name;
					string networkSyncItemID = item.NetworkSyncItemID;
					string text = (((Object)(object)item.ItemDataSO != (Object)null) ? ((object)item.ItemDataSO).GetType().Name : "NULL");
					string text2 = item.NetworkSyncPlayerID.ToString();
					Debug.Log((object)("[ClearClutter] Pickup | Component=" + name + " | ItemID=" + networkSyncItemID + " | ItemData=" + text + " | PlayerID=" + text2 + " | Category=" + pickupCategory));
					switch (pickupCategory)
					{
					case PickupCategory.Scrap:
						num++;
						break;
					case PickupCategory.Material:
						num2++;
						break;
					case PickupCategory.Equipment:
						num3++;
						break;
					default:
						num4++;
						break;
					}
				}
			}
			Debug.Log((object)("[ClearClutter] ===== Scan Result ===== | Total=" + list.Count + " | Scrap=" + num + " | Materials=" + num2 + " | Equipment=" + num3 + " | Protected/Unknown=" + num4));
		}
	}
}