Decompiled source of CrafteoPorTandas v1.0.12

BepInEx/plugins/CrafteoPorTandas/CrafteoPorTandas.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CrafteoPorTandas")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.12.0")]
[assembly: AssemblyInformationalVersion("1.0.12")]
[assembly: AssemblyProduct("CrafteoPorTandas")]
[assembly: AssemblyTitle("CrafteoPorTandas")]
[assembly: AssemblyVersion("1.0.12.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 CrafteoPorTandas
{
	internal static class Desmontar
	{
		private static bool _cambiandoNosotros;

		internal static bool Activo { get; private set; }

		internal static void Alternar(InventoryGui gui)
		{
			if (!((Object)(object)gui == (Object)null))
			{
				if (Activo)
				{
					Salir(gui);
				}
				else
				{
					Entrar(gui);
				}
			}
		}

		private static void Entrar(InventoryGui gui)
		{
			Activo = true;
			RefrescarLista(gui);
		}

		internal static void Salir(InventoryGui gui)
		{
			if (Activo)
			{
				Activo = false;
				RefrescarLista(gui);
			}
		}

		internal static void SalirPorCambioDePestana()
		{
			if (!_cambiandoNosotros)
			{
				Activo = false;
			}
		}

		private static void RefrescarLista(InventoryGui gui)
		{
			_cambiandoNosotros = true;
			try
			{
				gui.OnTabCraftPressed();
			}
			finally
			{
				_cambiandoNosotros = false;
			}
		}

		internal static void RecetasDeLoQueTengo(Player jugador, List<Recipe> destino)
		{
			destino.Clear();
			ObjectDB instance = ObjectDB.instance;
			Inventory val = (((Object)(object)jugador != (Object)null) ? ((Humanoid)jugador).GetInventory() : null);
			if ((Object)(object)instance == (Object)null || val == null)
			{
				return;
			}
			HashSet<Recipe> hashSet = new HashSet<Recipe>();
			foreach (ItemData allItem in val.GetAllItems())
			{
				if (allItem != null && allItem.m_shared != null)
				{
					Recipe recipe = instance.GetRecipe(allItem);
					if (!((Object)(object)recipe == (Object)null) && recipe.m_enabled && !recipe.m_requireOnlyOneIngredient && !hashSet.Contains(recipe) && HayBastante(val, recipe))
					{
						hashSet.Add(recipe);
						destino.Add(recipe);
					}
				}
			}
		}

		internal static bool Hacer(InventoryGui gui, out string motivo)
		{
			motivo = null;
			Player localPlayer = Player.m_localPlayer;
			Inventory val = (((Object)(object)localPlayer != (Object)null) ? ((Humanoid)localPlayer).GetInventory() : null);
			Recipe val2 = Seleccion.Receta(gui);
			if ((Object)(object)localPlayer == (Object)null || val == null || (Object)(object)val2 == (Object)null)
			{
				motivo = "Elige algo de la lista";
				return false;
			}
			if (val2.m_requireOnlyOneIngredient)
			{
				motivo = "Esta receta no se puede desmontar";
				return false;
			}
			int calidad;
			int num = Contar(val, val2, out calidad);
			int num2 = ((val2.m_amount <= 0) ? 1 : val2.m_amount);
			if (num < num2)
			{
				motivo = ((num2 == 1) ? "No te queda ninguno para desmontar" : ("Necesitas " + num2 + " para desmontar una tanda"));
				return false;
			}
			Dictionary<ItemDrop, int> dictionary = MaterialesADevolver(val2, calidad);
			if (dictionary.Count == 0)
			{
				motivo = "Esta receta no devuelve materiales";
				return false;
			}
			foreach (KeyValuePair<ItemDrop, int> item in dictionary)
			{
				if (!val.CanAddItem(((Component)item.Key).gameObject, item.Value))
				{
					motivo = "Haz espacio para recuperar los materiales";
					return false;
				}
			}
			if (!Quitar(val, val2, calidad, num2))
			{
				motivo = "No se pudo quitar lo que ibas a desmontar";
				return false;
			}
			foreach (KeyValuePair<ItemDrop, int> item2 in dictionary)
			{
				val.AddItem(((Component)item2.Key).gameObject, item2.Value);
			}
			RefrescarLista(gui);
			motivo = "Desmontado: materiales recuperados";
			return true;
		}

		private static Dictionary<ItemDrop, int> MaterialesADevolver(Recipe receta, int calidad)
		{
			Dictionary<ItemDrop, int> dictionary = new Dictionary<ItemDrop, int>();
			if (receta.m_resources == null)
			{
				return dictionary;
			}
			Requirement[] resources = receta.m_resources;
			foreach (Requirement val in resources)
			{
				if (val == null || (Object)(object)val.m_resItem == (Object)null)
				{
					continue;
				}
				int num = 0;
				for (int j = 1; j <= calidad; j++)
				{
					if (j != 1 || !val.m_upgraderResource)
					{
						num += val.GetAmount(j);
					}
				}
				if (num > 0)
				{
					dictionary[val.m_resItem] = (dictionary.TryGetValue(val.m_resItem, out var value) ? (value + num) : num);
				}
			}
			return dictionary;
		}

		private static bool HayBastante(Inventory inventario, Recipe receta)
		{
			int num = ((receta.m_amount <= 0) ? 1 : receta.m_amount);
			int calidad;
			return Contar(inventario, receta, out calidad) >= num;
		}

		private static int Contar(Inventory inventario, Recipe receta, out int calidad)
		{
			calidad = 1;
			ItemData val = (((Object)(object)receta.m_item != (Object)null) ? receta.m_item.m_itemData : null);
			if (val == null || val.m_shared == null)
			{
				return 0;
			}
			string name = val.m_shared.m_name;
			bool flag = false;
			foreach (ItemData allItem in inventario.GetAllItems())
			{
				if (Casa(allItem, name) && !allItem.m_equipped && (!flag || allItem.m_quality < calidad))
				{
					calidad = allItem.m_quality;
					flag = true;
				}
			}
			if (!flag)
			{
				return 0;
			}
			int num = 0;
			foreach (ItemData allItem2 in inventario.GetAllItems())
			{
				if (Casa(allItem2, name) && !allItem2.m_equipped && allItem2.m_quality == calidad)
				{
					num += allItem2.m_stack;
				}
			}
			return num;
		}

		private static bool Casa(ItemData item, string nombre)
		{
			if (item != null && item.m_shared != null)
			{
				return item.m_shared.m_name == nombre;
			}
			return false;
		}

		private static bool Quitar(Inventory inventario, Recipe receta, int calidad, int cantidad)
		{
			string name = receta.m_item.m_itemData.m_shared.m_name;
			int num = cantidad;
			foreach (ItemData item in new List<ItemData>(inventario.GetAllItems()))
			{
				if (num <= 0)
				{
					break;
				}
				if (Casa(item, name) && !item.m_equipped && item.m_quality == calidad)
				{
					int num2 = ((item.m_stack < num) ? item.m_stack : num);
					inventario.RemoveItem(item, num2);
					num -= num2;
				}
			}
			return num <= 0;
		}
	}
	[HarmonyPatch(typeof(Player), "GetAvailableRecipes")]
	internal static class ParcheListaDesmontar
	{
		private static void Postfix(Player __instance, ref List<Recipe> available)
		{
			if (Desmontar.Activo && available != null)
			{
				Desmontar.RecetasDeLoQueTengo(__instance, available);
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "AddRecipeToList")]
	internal static class ParcheEntradasDesmontar
	{
		private static void Prefix(ref bool canCraft)
		{
			if (Desmontar.Activo)
			{
				canCraft = true;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "SetupRequirement")]
	internal static class ParcheRequisitosDesmontar
	{
		private static void Postfix(Transform elementRoot, Requirement req, int quality, ref bool __result)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			if (Desmontar.Activo && !((Object)(object)elementRoot == (Object)null) && req != null && !((Object)(object)req.m_resItem == (Object)null))
			{
				__result = true;
				Transform val = elementRoot.Find("res_amount");
				TMP_Text val2 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<TMP_Text>() : null);
				if (!((Object)(object)val2 == (Object)null))
				{
					val2.text = req.GetAmount(quality).ToString();
					((Graphic)val2).color = Color.white;
				}
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "OnTabCraftPressed")]
	internal static class ParcheSalirPorElaborar
	{
		private static void Prefix()
		{
			Desmontar.SalirPorCambioDePestana();
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "OnTabUpgradePressed")]
	internal static class ParcheSalirPorMejorar
	{
		private static void Prefix()
		{
			Desmontar.SalirPorCambioDePestana();
		}
	}
	internal static class InterfazCrafteo
	{
		private sealed class EntradaCantidad : TextReceiver
		{
			public string GetText()
			{
				CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
				if (!((Object)(object)instancia != (Object)null))
				{
					return "1";
				}
				return instancia.Cantidad.ToString(CultureInfo.InvariantCulture);
			}

			public void SetText(string text)
			{
				CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
				if ((Object)(object)instancia == (Object)null)
				{
					return;
				}
				if (!int.TryParse((text ?? string.Empty).Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) || result < 1)
				{
					Mensaje("Escribe un número entre " + 1 + " y " + 999);
					return;
				}
				instancia.Cantidad = result;
				if (result > 999)
				{
					Mensaje("El máximo es " + 999 + ", se queda en " + instancia.Cantidad);
				}
			}
		}

		[CompilerGenerated]
		private static class <>O
		{
			public static UnityAction <0>__PulsarDeshacer;

			public static UnityAction <1>__PulsarTotal;
		}

		private const float Hueco = 4f;

		private const float AltoFila = 30f;

		private const int LimiteDigitos = 3;

		private const string Sufijo = " ×";

		private static InventoryGui _gui;

		private static RectTransform _fila;

		private static readonly List<Button> Rapidos = new List<Button>();

		private static Button _total;

		private static TMP_Text _textoTotal;

		private static Button _deshacer;

		private static TMP_Text _textoDeshacer;

		private static TMP_Text _textoCrear;

		private static int _mostrado = -1;

		private static Recipe _ultimaReceta;

		private static readonly EntradaCantidad Entrada = new EntradaCantidad();

		internal static void Refrescar(InventoryGui gui)
		{
			CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
			if (!((Object)(object)gui == (Object)null) && !((Object)(object)instancia == (Object)null) && ((!((Object)(object)_gui != (Object)(object)gui) && !((Object)(object)_fila == (Object)null) && !((Object)(object)_deshacer == (Object)null)) || Construir(gui)))
			{
				bool activo = Desmontar.Activo;
				Recipe val = Seleccion.Receta(gui);
				if (val != _ultimaReceta)
				{
					_ultimaReceta = val;
					instancia.ReiniciarCantidad();
				}
				bool flag = instancia.Activo && gui.InCraftTab() && !activo;
				if (((Component)_fila).gameObject.activeSelf != flag)
				{
					((Component)_fila).gameObject.SetActive(flag);
				}
				if (flag && _mostrado != instancia.Cantidad)
				{
					_mostrado = instancia.Cantidad;
					Escribir(_textoTotal, "Cantidad: " + instancia.Cantidad);
				}
				if (((Component)_deshacer).gameObject.activeSelf != instancia.PermitirDeshacer)
				{
					((Component)_deshacer).gameObject.SetActive(instancia.PermitirDeshacer);
				}
				if (instancia.PermitirDeshacer)
				{
					Escribir(_textoDeshacer, "DESHACER");
				}
			}
		}

		internal static void EtiquetarBoton(InventoryGui gui)
		{
			if ((Object)(object)gui == (Object)null || (Object)(object)gui.m_craftButton == (Object)null)
			{
				return;
			}
			TMP_Text val = (((Object)(object)_textoCrear != (Object)null) ? _textoCrear : ((Component)gui.m_craftButton).GetComponentInChildren<TMP_Text>(true));
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			if (Desmontar.Activo)
			{
				Escribir(val, "Desmontar");
				((Selectable)gui.m_craftButton).interactable = true;
				return;
			}
			CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
			if (!((Object)(object)instancia == (Object)null))
			{
				string text = val.text ?? string.Empty;
				int num = text.IndexOf(" ×", StringComparison.Ordinal);
				string text2 = ((num >= 0) ? text.Substring(0, num) : text);
				bool flag = instancia.Activo && instancia.Cantidad > 1 && gui.InCraftTab() && !Seleccion.EsMejora(gui);
				Escribir(val, flag ? (text2 + " ×" + instancia.Cantidad) : text2);
			}
		}

		private static bool Construir(InventoryGui gui)
		{
			try
			{
				Destruir();
				Button tabUpgrade = gui.m_tabUpgrade;
				RectTransform val = (((Object)(object)tabUpgrade != (Object)null) ? ((Component)tabUpgrade).GetComponent<RectTransform>() : null);
				Button craftButton = gui.m_craftButton;
				RectTransform val2 = (((Object)(object)craftButton != (Object)null) ? ((Component)craftButton).GetComponent<RectTransform>() : null);
				if ((Object)(object)val == (Object)null || (Object)(object)((Transform)val).parent == (Object)null || (Object)(object)val2 == (Object)null || (Object)(object)((Transform)val2).parent == (Object)null)
				{
					return false;
				}
				ConstruirPestanaDeshacer(tabUpgrade, val);
				ConstruirFila(tabUpgrade, val2);
				_textoCrear = ((Component)craftButton).GetComponentInChildren<TMP_Text>(true);
				_mostrado = -1;
				_gui = gui;
				return true;
			}
			catch (Exception ex)
			{
				Anotar("No se pudo crear la interfaz de crafteo: " + ex);
				Destruir();
				return false;
			}
		}

		private static void ConstruirPestanaDeshacer(Button modelo, RectTransform rtModelo)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: 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_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			_deshacer = Clonar(modelo, ((Transform)rtModelo).parent);
			RectTransform component = ((Component)_deshacer).GetComponent<RectTransform>();
			component.anchorMin = rtModelo.anchorMin;
			component.anchorMax = rtModelo.anchorMax;
			component.pivot = rtModelo.pivot;
			((Transform)component).localScale = ((Transform)rtModelo).localScale;
			component.sizeDelta = rtModelo.sizeDelta;
			Vector2 anchoredPosition = rtModelo.anchoredPosition;
			Rect rect = rtModelo.rect;
			component.anchoredPosition = anchoredPosition + new Vector2(((Rect)(ref rect)).width + 4f, 0f);
			ButtonClickedEvent onClick = _deshacer.onClick;
			object obj = <>O.<0>__PulsarDeshacer;
			if (obj == null)
			{
				UnityAction val = PulsarDeshacer;
				<>O.<0>__PulsarDeshacer = val;
				obj = (object)val;
			}
			((UnityEvent)onClick).AddListener((UnityAction)obj);
			_textoDeshacer = ((Component)_deshacer).GetComponentInChildren<TMP_Text>(true);
		}

		private static void ConstruirFila(Button modelo, RectTransform rtCrear)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: 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)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Expected O, but got Unknown
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Expected O, but got Unknown
			int[] pasosRapidos = CrafteoPorTandasPlugin.Instancia.PasosRapidos;
			_fila = new GameObject("CrafteoPorTandas_Fila", new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)_fila).SetParent(((Transform)rtCrear).parent, false);
			_fila.anchorMin = rtCrear.anchorMin;
			_fila.anchorMax = rtCrear.anchorMax;
			_fila.pivot = rtCrear.pivot;
			Rect rect = rtCrear.rect;
			float width = ((Rect)(ref rect)).width;
			_fila.sizeDelta = new Vector2(width, 30f);
			RectTransform fila = _fila;
			Vector2 anchoredPosition = rtCrear.anchoredPosition;
			rect = rtCrear.rect;
			fila.anchoredPosition = anchoredPosition - new Vector2(0f, ((Rect)(ref rect)).height * 0.5f + 15f + 4f);
			float num = (float)pasosRapidos.Length + 2.4f;
			float num2 = (width - 4f * (float)pasosRapidos.Length) / num;
			_total = Clonar(modelo, (Transform)(object)_fila);
			Colocar(_total, 0f, num2 * 2.4f, width);
			ButtonClickedEvent onClick = _total.onClick;
			object obj = <>O.<1>__PulsarTotal;
			if (obj == null)
			{
				UnityAction val = PulsarTotal;
				<>O.<1>__PulsarTotal = val;
				obj = (object)val;
			}
			((UnityEvent)onClick).AddListener((UnityAction)obj);
			_textoTotal = ((Component)_total).GetComponentInChildren<TMP_Text>(true);
			if ((Object)(object)_textoTotal != (Object)null)
			{
				_textoTotal.fontSizeMax = _textoTotal.fontSize;
				_textoTotal.fontSizeMin = 8f;
				_textoTotal.enableAutoSizing = true;
			}
			float num3 = num2 * 2.4f + 4f;
			int[] array = pasosRapidos;
			foreach (int num4 in array)
			{
				int suma = num4;
				Button val2 = Clonar(modelo, (Transform)(object)_fila);
				Colocar(val2, num3, num2, width);
				Escribir(((Component)val2).GetComponentInChildren<TMP_Text>(true), "+" + suma);
				((UnityEvent)val2.onClick).AddListener((UnityAction)delegate
				{
					Sumar(suma);
				});
				Rapidos.Add(val2);
				num3 += num2 + 4f;
			}
		}

		private static void Colocar(Button boton, float desplazamiento, float anchoCelda, float anchoFila)
		{
			//IL_0011: 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_003b: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			RectTransform component = ((Component)boton).GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.pivot = new Vector2(0.5f, 0.5f);
			((Transform)component).localScale = Vector3.one;
			component.sizeDelta = new Vector2(anchoCelda, 30f);
			component.anchoredPosition = new Vector2((0f - anchoFila) * 0.5f + anchoCelda * 0.5f + desplazamiento, 0f);
		}

		private static Button Clonar(Button modelo, Transform padre)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			Button obj = Object.Instantiate<Button>(modelo, padre);
			((Component)obj).gameObject.SetActive(true);
			((Selectable)obj).interactable = true;
			obj.onClick = new ButtonClickedEvent();
			return obj;
		}

		private static void Escribir(TMP_Text texto, string valor)
		{
			if ((Object)(object)texto != (Object)null && texto.text != valor)
			{
				texto.text = valor;
			}
		}

		private static void Sumar(int paso)
		{
			CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
			if ((Object)(object)instancia != (Object)null)
			{
				instancia.Cantidad += paso;
			}
		}

		private static void PulsarTotal()
		{
			TextInput instance = TextInput.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				instance.RequestText((TextReceiver)(object)Entrada, "Cuántos crear (1-" + 999 + ")", 3);
			}
		}

		private static void PulsarDeshacer()
		{
			Desmontar.Alternar(_gui);
		}

		internal static void Mensaje(string texto)
		{
			MessageHud instance = MessageHud.instance;
			if ((Object)(object)instance != (Object)null && !string.IsNullOrEmpty(texto))
			{
				instance.ShowMessage((MessageType)2, texto, 0, (Sprite)null, false, false);
			}
		}

		internal static void Anotar(string texto)
		{
			if (CrafteoPorTandasPlugin.Registro != null)
			{
				CrafteoPorTandasPlugin.Registro.LogInfo((object)texto);
			}
		}

		internal static void Rehacer()
		{
			Destruir();
		}

		internal static void OlvidarSeleccion()
		{
			_ultimaReceta = null;
			CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
			if ((Object)(object)instancia != (Object)null)
			{
				instancia.ReiniciarCantidad();
			}
		}

		private static void Destruir()
		{
			if ((Object)(object)_fila != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_fila).gameObject);
			}
			if ((Object)(object)_deshacer != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_deshacer).gameObject);
			}
			Rapidos.Clear();
			_fila = null;
			_total = null;
			_textoTotal = null;
			_deshacer = null;
			_textoDeshacer = null;
			_textoCrear = null;
			_mostrado = -1;
			_ultimaReceta = null;
			_gui = null;
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateCraftingPanel")]
	internal static class ParcheInterfazCrafteo
	{
		private static void Postfix(InventoryGui __instance)
		{
			InterfazCrafteo.Refrescar(__instance);
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateRecipe")]
	internal static class ParcheEtiquetaBoton
	{
		private static void Postfix(InventoryGui __instance)
		{
			InterfazCrafteo.EtiquetarBoton(__instance);
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Hide")]
	internal static class ParcheCerrarVentana
	{
		private static void Postfix()
		{
			InterfazCrafteo.OlvidarSeleccion();
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "OnCraftPressed")]
	internal static class ParcheCrafteoMultiple
	{
		private static readonly FieldInfo CampoMultiTactil = AccessTools.Field(typeof(InventoryGui), "m_touchMultiCrafting");

		private static bool Prefix(InventoryGui __instance, out bool __state)
		{
			__state = false;
			if (Desmontar.Activo)
			{
				Desmontar.Hacer(__instance, out var motivo);
				InterfazCrafteo.Mensaje(motivo);
				InterfazCrafteo.Anotar("Desmontar: " + motivo);
				return false;
			}
			CrafteoPorTandasPlugin instancia = CrafteoPorTandasPlugin.Instancia;
			if ((Object)(object)instancia == (Object)null || !instancia.Activo || CampoMultiTactil == null)
			{
				return true;
			}
			int num = CantidadPosible(__instance, instancia.Cantidad);
			if (num <= 1)
			{
				return true;
			}
			__state = (bool)CampoMultiTactil.GetValue(__instance);
			CampoMultiTactil.SetValue(__instance, true);
			__instance.m_multiCraftAmount = num;
			return true;
		}

		private static void Postfix(InventoryGui __instance, bool __state)
		{
			if (CampoMultiTactil != null)
			{
				CampoMultiTactil.SetValue(__instance, __state);
			}
		}

		private static int CantidadPosible(InventoryGui gui, int pedida)
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || Seleccion.EsMejora(gui))
			{
				return 1;
			}
			Recipe val = Seleccion.Receta(gui);
			if ((Object)(object)val == (Object)null)
			{
				return 1;
			}
			for (int num = pedida; num > 1; num--)
			{
				if (localPlayer.HaveRequirements(val, false, 1, num))
				{
					if (num < pedida)
					{
						InterfazCrafteo.Mensaje("Materiales para " + num + ", no para " + pedida);
					}
					return num;
				}
			}
			if (pedida > 1)
			{
				InterfazCrafteo.Mensaje("Materiales solo para 1, no para " + pedida);
			}
			return 1;
		}
	}
	[BepInPlugin("altairfernando.crafteoportandas", "CrafteoPorTandas", "1.0.12")]
	public class CrafteoPorTandasPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "altairfernando.crafteoportandas";

		public const string PluginName = "CrafteoPorTandas";

		public const string PluginVersion = "1.0.12";

		internal const int CantidadMinima = 1;

		internal const int CantidadMaxima = 999;

		private const int MaximoBotones = 6;

		internal static CrafteoPorTandasPlugin Instancia;

		internal static ManualLogSource Registro;

		private ConfigEntry<bool> _activo;

		private ConfigEntry<int> _cantidadInicial;

		private ConfigEntry<string> _pasos;

		private ConfigEntry<bool> _permitirDeshacer;

		private int _cantidadActual = 1;

		internal bool Activo
		{
			get
			{
				if (_activo != null)
				{
					return _activo.Value;
				}
				return false;
			}
		}

		internal bool PermitirDeshacer
		{
			get
			{
				if (_permitirDeshacer != null)
				{
					return _permitirDeshacer.Value;
				}
				return false;
			}
		}

		internal int[] PasosRapidos { get; private set; }

		internal int Cantidad
		{
			get
			{
				return _cantidadActual;
			}
			set
			{
				_cantidadActual = Limitar(value);
			}
		}

		internal void ReiniciarCantidad()
		{
			_cantidadActual = ((_cantidadInicial == null) ? 1 : Limitar(_cantidadInicial.Value));
		}

		private static int Limitar(int valor)
		{
			if (valor >= 1)
			{
				if (valor <= 999)
				{
					return valor;
				}
				return 999;
			}
			return 1;
		}

		private void Awake()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			Instancia = this;
			Registro = ((BaseUnityPlugin)this).Logger;
			PasosRapidos = new int[4] { 2, 3, 5, 20 };
			_activo = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Crafteo", "CraftearVarios", true, "Craftea o cocina varios de golpe en vez de uno por uno. Consume los materiales de todos.");
			_cantidadInicial = ((BaseUnityPlugin)this).Config.Bind<int>("1 - Crafteo", "CantidadInicial", 1, new ConfigDescription("Con cuantos empieza el contador cada vez que abres la ventana o eliges otra receta.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 999), Array.Empty<object>()));
			_pasos = ((BaseUnityPlugin)this).Config.Bind<string>("1 - Crafteo", "BotonesRapidos", "2,3,5,20", "Cuanto suma cada boton de la fila, separados por coma. Van sumando: con 4 puestos, +2 deja 6. Maximo " + 6 + " botones. Para un numero exacto se pulsa el total y se escribe.");
			_permitirDeshacer = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Deshacer", "PermitirDeshacer", true, "Muestra la pestaña Deshacer, que sirve para desmontar y recuperar los materiales.");
			ReiniciarCantidad();
			RecargarPasos();
			_pasos.SettingChanged += delegate
			{
				RecargarPasos();
				InterfazCrafteo.Rehacer();
			};
			new Harmony("altairfernando.crafteoportandas").PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CrafteoPorTandas 1.0.12 cargado.");
		}

		private void RecargarPasos()
		{
			List<int> list = new List<int>();
			string[] array = (_pasos.Value ?? string.Empty).Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				if (int.TryParse(array[i].Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) && result > 0 && !list.Contains(result))
				{
					list.Add(result);
				}
				if (list.Count == 6)
				{
					break;
				}
			}
			if (list.Count == 0)
			{
				list.Add(1);
			}
			PasosRapidos = list.ToArray();
		}
	}
	internal static class Seleccion
	{
		private static readonly FieldInfo Campo = AccessTools.Field(typeof(InventoryGui), "m_selectedRecipe");

		private static readonly MethodInfo LeerReceta = ((Campo != null) ? AccessTools.Method(Campo.FieldType, "get_Recipe", (Type[])null, (Type[])null) : null);

		private static readonly MethodInfo LeerItem = ((Campo != null) ? AccessTools.Method(Campo.FieldType, "get_ItemData", (Type[])null, (Type[])null) : null);

		internal static bool Disponible
		{
			get
			{
				if (Campo != null && LeerReceta != null)
				{
					return LeerItem != null;
				}
				return false;
			}
		}

		internal static Recipe Receta(InventoryGui gui)
		{
			object obj = Valor(gui);
			if (obj == null)
			{
				return null;
			}
			object? obj2 = LeerReceta.Invoke(obj, null);
			return (Recipe)((obj2 is Recipe) ? obj2 : null);
		}

		internal static bool EsMejora(InventoryGui gui)
		{
			object obj = Valor(gui);
			if (obj != null)
			{
				return LeerItem.Invoke(obj, null) is ItemData;
			}
			return false;
		}

		private static object Valor(InventoryGui gui)
		{
			if (!Disponible || !((Object)(object)gui != (Object)null))
			{
				return null;
			}
			return Campo.GetValue(gui);
		}
	}
}