Decompiled source of block selling ur weapons to npcs v1.0.0

plugins/WeaponSaleLock/WeaponSaleLock.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[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("Estonia")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Prevents weapons from being sold in How to Fish.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WeaponSaleLock")]
[assembly: AssemblyTitle("WeaponSaleLock")]
[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 WeaponSaleLock
{
	[BepInPlugin("estonia.howtofish.weaponsalelock", "Weapon Sale Lock", "1.0.0")]
	[BepInProcess("How to Fish.exe")]
	public sealed class WeaponSaleLockPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(NPC), "CanEat")]
		private static class NpcCanEatPatch
		{
			private static bool Prefix(NPCQuest quest, Item item, ref bool __result)
			{
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				//IL_000f: Invalid comparison between Unknown and I4
				if (Object.op_Implicit((Object)(object)quest) && (int)quest.Type == 1 && ShouldBlock(item))
				{
					__result = false;
					return false;
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(MoneyManager), "SellItem")]
		private static class MoneyManagerSellItemPatch
		{
			private static bool Prefix(Item item)
			{
				return !ShouldBlock(item);
			}
		}

		private const string PluginGuid = "estonia.howtofish.weaponsalelock";

		private const string PluginName = "Weapon Sale Lock";

		private const string PluginVersion = "1.0.0";

		private const string Author = "Estonia";

		private static readonly BindingFlags PrivateStatic = BindingFlags.Static | BindingFlags.NonPublic;

		private static readonly BindingFlags PrivateInstance = BindingFlags.Instance | BindingFlags.NonPublic;

		private static WeaponSaleLockPlugin _instance;

		private ConfigEntry<bool> _blockWeaponSales;

		private Harmony _harmony;

		private PauseManager _pauseManager;

		private ButtonManager _buttonManager;

		private GameObject _optionsScreen;

		private GameObject _settingsRow;

		private Toggle _settingsToggle;

		private TextMeshProUGUI _settingsText;

		private bool BlockWeaponSales
		{
			get
			{
				return _blockWeaponSales.Value;
			}
			set
			{
				_blockWeaponSales.Value = value;
				RefreshSettingsUi();
			}
		}

		private void Awake()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			_instance = this;
			_blockWeaponSales = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BlockWeaponSales", true, "Yes blocks weapons from being sold. No allows weapon selling.");
			_harmony = new Harmony("estonia.howtofish.weaponsalelock");
			_harmony.PatchAll(typeof(WeaponSaleLockPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Weapon Sale Lock 1.0.0 loaded. Author: Estonia.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			if (Object.op_Implicit((Object)(object)_settingsRow))
			{
				Object.Destroy((Object)(object)_settingsRow);
			}
		}

		private void Update()
		{
			EnsureSettingsUi();
			RefreshSettingsUi();
		}

		private void EnsureSettingsUi()
		{
			PauseManager pauseManager = GetPauseManager();
			ButtonManager buttonManager = GetButtonManager();
			if (!Object.op_Implicit((Object)(object)pauseManager) || !Object.op_Implicit((Object)(object)buttonManager))
			{
				return;
			}
			GameObject privateField = GetPrivateField<GameObject>(pauseManager, "_optionsScreen");
			if (Object.op_Implicit((Object)(object)privateField) && (!((Object)(object)pauseManager == (Object)(object)_pauseManager) || !((Object)(object)buttonManager == (Object)(object)_buttonManager) || !((Object)(object)privateField == (Object)(object)_optionsScreen) || !Object.op_Implicit((Object)(object)_settingsRow)))
			{
				_pauseManager = pauseManager;
				_buttonManager = buttonManager;
				_optionsScreen = privateField;
				if (Object.op_Implicit((Object)(object)_settingsRow))
				{
					Object.Destroy((Object)(object)_settingsRow);
				}
				BuildSettingsToggle();
			}
		}

		private void BuildSettingsToggle()
		{
			Toggle val = GetPrivateField<Toggle>(_buttonManager, "_damageNumbersToggle") ?? GetPrivateField<Toggle>(_buttonManager, "_itemDotsToggle") ?? _optionsScreen.GetComponentInChildren<Toggle>(true);
			if (Object.op_Implicit((Object)(object)val))
			{
				BuildClonedToggle(val);
			}
			else
			{
				BuildFallbackButton();
			}
		}

		private void BuildClonedToggle(Toggle templateToggle)
		{
			Transform val = (Object.op_Implicit((Object)(object)((Component)templateToggle).transform.parent) ? ((Component)templateToggle).transform.parent : ((Component)templateToggle).transform);
			Transform val2 = (Object.op_Implicit((Object)(object)val.parent) ? val.parent : _optionsScreen.transform);
			_settingsRow = Object.Instantiate<GameObject>(((Component)val).gameObject, val2);
			((Object)_settingsRow).name = "WeaponSaleLock_SettingsToggle";
			_settingsRow.SetActive(true);
			_settingsRow.transform.SetAsLastSibling();
			RemoveRelabelComponents(_settingsRow);
			_settingsToggle = _settingsRow.GetComponentInChildren<Toggle>(true);
			if (Object.op_Implicit((Object)(object)_settingsToggle))
			{
				((UnityEventBase)_settingsToggle.onValueChanged).RemoveAllListeners();
				_settingsToggle.isOn = BlockWeaponSales;
				((UnityEvent<bool>)(object)_settingsToggle.onValueChanged).AddListener((UnityAction<bool>)SetFromToggle);
			}
			_settingsText = FindBestText(_settingsRow);
			RefreshSettingsUi();
		}

		private void BuildFallbackButton()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Expected O, but got Unknown
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			_settingsRow = new GameObject("WeaponSaleLock_SettingsButton", new Type[4]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Button),
				typeof(LayoutElement)
			});
			_settingsRow.transform.SetParent(_optionsScreen.transform, false);
			_settingsRow.transform.SetAsLastSibling();
			_settingsRow.GetComponent<RectTransform>().sizeDelta = new Vector2(360f, 56f);
			Image component = _settingsRow.GetComponent<Image>();
			((Graphic)component).color = new Color(0.12f, 0.06f, 0.12f, 0.72f);
			AddBorder(_settingsRow.transform, 2f, Color.white);
			Button component2 = _settingsRow.GetComponent<Button>();
			((Selectable)component2).targetGraphic = (Graphic)(object)component;
			((Selectable)component2).transition = (Transition)1;
			((UnityEvent)component2.onClick).AddListener((UnityAction)delegate
			{
				BlockWeaponSales = !BlockWeaponSales;
			});
			_settingsText = CreateText(_settingsRow.transform, "", 20f);
			RectTransform component3 = ((Component)_settingsText).GetComponent<RectTransform>();
			component3.anchorMin = Vector2.zero;
			component3.anchorMax = Vector2.one;
			component3.offsetMin = new Vector2(10f, 0f);
			component3.offsetMax = new Vector2(-10f, 0f);
			RefreshSettingsUi();
		}

		private void SetFromToggle(bool value)
		{
			BlockWeaponSales = value;
		}

		private void RefreshSettingsUi()
		{
			if (Object.op_Implicit((Object)(object)_settingsToggle) && _settingsToggle.isOn != BlockWeaponSales)
			{
				_settingsToggle.SetIsOnWithoutNotify(BlockWeaponSales);
			}
			string text = (BlockWeaponSales ? "Weapon Sales: Blocked" : "Weapon Sales: Allowed");
			if (Object.op_Implicit((Object)(object)_settingsText))
			{
				((TMP_Text)_settingsText).text = text;
				((TMP_Text)_settingsText).alignment = (TextAlignmentOptions)514;
				((TMP_Text)_settingsText).enableAutoSizing = true;
				((TMP_Text)_settingsText).fontSizeMin = 10f;
				((TMP_Text)_settingsText).fontSizeMax = Mathf.Max(18f, ((TMP_Text)_settingsText).fontSizeMax);
			}
		}

		private static bool ShouldBlock(Item item)
		{
			if (Object.op_Implicit((Object)(object)_instance) && _instance._blockWeaponSales.Value)
			{
				return IsWeapon(item);
			}
			return false;
		}

		private static bool IsWeapon(Item item)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between Unknown and I4
			if (Object.op_Implicit((Object)(object)item))
			{
				if (!Object.op_Implicit((Object)(object)item.Weapon))
				{
					return (int)item.Type == 2;
				}
				return true;
			}
			return false;
		}

		private static TextMeshProUGUI FindBestText(GameObject root)
		{
			TextMeshProUGUI[] componentsInChildren = root.GetComponentsInChildren<TextMeshProUGUI>(true);
			TextMeshProUGUI[] array = componentsInChildren;
			foreach (TextMeshProUGUI val in array)
			{
				if (Object.op_Implicit((Object)(object)val) && ((TMP_Text)val).text.Length > 0)
				{
					return val;
				}
			}
			if (componentsInChildren.Length == 0)
			{
				return CreateText(root.transform, "", 18f);
			}
			return componentsInChildren[0];
		}

		private static TextMeshProUGUI CreateText(Transform parent, string text, float size)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("WeaponSaleLock_Text", new Type[2]
			{
				typeof(RectTransform),
				typeof(TextMeshProUGUI)
			});
			val.transform.SetParent(parent, false);
			TextMeshProUGUI component = val.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component).text = text;
			((TMP_Text)component).fontSize = size;
			((TMP_Text)component).fontSizeMax = size;
			((TMP_Text)component).fontSizeMin = 10f;
			((TMP_Text)component).enableAutoSizing = true;
			((Graphic)component).color = Color.white;
			((TMP_Text)component).alignment = (TextAlignmentOptions)514;
			((Graphic)component).raycastTarget = false;
			return component;
		}

		private static void AddBorder(Transform parent, float thickness, Color color)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: 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_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			AddBorderPiece(parent, "BorderTop", new Vector2(0f, 1f), new Vector2(1f, 1f), new Vector2(0f, 0f - thickness), color);
			AddBorderPiece(parent, "BorderBottom", new Vector2(0f, 0f), new Vector2(1f, 0f), new Vector2(0f, thickness), color);
			AddBorderPiece(parent, "BorderLeft", new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(thickness, 0f), color);
			AddBorderPiece(parent, "BorderRight", new Vector2(1f, 0f), new Vector2(1f, 1f), new Vector2(0f - thickness, 0f), color);
		}

		private static void AddBorderPiece(Transform parent, string name, Vector2 anchorMin, Vector2 anchorMax, Vector2 sizeDelta, Color color)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: 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)
			GameObject val = new GameObject(name, new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			val.transform.SetParent(parent, false);
			Image component = val.GetComponent<Image>();
			((Graphic)component).color = color;
			((Graphic)component).raycastTarget = false;
			RectTransform component2 = val.GetComponent<RectTransform>();
			component2.anchorMin = anchorMin;
			component2.anchorMax = anchorMax;
			component2.pivot = new Vector2(0.5f, 0.5f);
			component2.anchoredPosition = Vector2.zero;
			component2.sizeDelta = sizeDelta;
		}

		private static void RemoveRelabelComponents(GameObject root)
		{
			Component[] componentsInChildren = root.GetComponentsInChildren<Component>(true);
			foreach (Component val in componentsInChildren)
			{
				if (Object.op_Implicit((Object)(object)val))
				{
					Type type = ((object)val).GetType();
					string text = type.FullName ?? type.Name;
					if (text.IndexOf("Localiz", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("Localized", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("Localize", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						Object.DestroyImmediate((Object)(object)val);
					}
				}
			}
		}

		private static PauseManager GetPauseManager()
		{
			object? obj = typeof(PauseManager).GetField("_instance", PrivateStatic)?.GetValue(null);
			return (PauseManager)((obj is PauseManager) ? obj : null);
		}

		private static ButtonManager GetButtonManager()
		{
			object? obj = typeof(ButtonManager).GetField("_instance", PrivateStatic)?.GetValue(null);
			return (ButtonManager)((obj is ButtonManager) ? obj : null);
		}

		private static T GetPrivateField<T>(object instance, string fieldName) where T : class
		{
			if (instance == null)
			{
				return null;
			}
			return instance.GetType().GetField(fieldName, PrivateInstance)?.GetValue(instance) as T;
		}
	}
}