Decompiled source of SortCraft v1.1.1

kg_SortCraft.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("kg_SortCraft")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("kg_SortCraft")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("0C7AA79F-B0E4-4978-B699-FFBAAFE30210")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 kg_SortedCraft
{
	[BepInPlugin("kg_SortedCraft", "kg_SortedCraft", "1.1.1")]
	public class kg_SortedCraft : BaseUnityPlugin
	{
		private enum SortBy
		{
			All,
			HP,
			Stamina,
			Eitr,
			Armor,
			Block,
			Physical,
			Elemental,
			Blunt,
			Slash,
			Pierce,
			Chop,
			Fire,
			Frost,
			Lightning,
			Poison,
			Spirit
		}

		[HarmonyPatch(typeof(CraftingStation), "Start")]
		private static class CraftingStation_Start_Patch
		{
			private static void Postfix(CraftingStation __instance)
			{
				__instance.m_craftRequireRoof = false;
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "UpdateRecipeList")]
		internal static class InventoryGuiUpdateRecipeListPatch
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Expected O, but got Unknown
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_001c: Expected O, but got Unknown
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_002a: Expected O, but got Unknown
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Expected O, but got Unknown
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				//IL_0046: Expected O, but got Unknown
				//IL_0061: Unknown result type (might be due to invalid IL or missing references)
				//IL_0067: Expected O, but got Unknown
				//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d5: Expected O, but got Unknown
				//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f6: Expected O, but got Unknown
				//IL_0113: Unknown result type (might be due to invalid IL or missing references)
				//IL_0119: Expected O, but got Unknown
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				CodeInstruction[] array = (CodeInstruction[])(object)new CodeInstruction[5]
				{
					new CodeInstruction(OpCodes.Ldc_I4_0, (object)null),
					new CodeInstruction(OpCodes.Stloc_S, (object)null),
					new CodeInstruction(OpCodes.Br, (object)null),
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(InventoryGui), "m_availableRecipes"))
				};
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[5]
				{
					CodeMatch.op_Implicit(array[0]),
					CodeMatch.op_Implicit(array[1]),
					CodeMatch.op_Implicit(array[2]),
					CodeMatch.op_Implicit(array[3]),
					CodeMatch.op_Implicit(array[4])
				});
				if (!val.IsValid)
				{
					MonoBehaviour.print((object)"Failed to find target in InventoryGui.UpdateRecipeList");
					return instructions;
				}
				CodeInstruction[] array2 = (CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldflda, (object)AccessTools.Field(typeof(InventoryGui), "m_availableRecipes")),
					new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(kg_SortedCraft), "SortRecipes", (Type[])null, (Type[])null))
				};
				array2[0].labels = new List<Label>(val.Instruction.labels);
				val.Instruction.labels.Clear();
				val.Insert(array2);
				return val.Instructions();
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "Awake")]
		private static class InventoryGuiAwakePatch
		{
			private static readonly Dictionary<SortBy, GameObject> buttons = new Dictionary<SortBy, GameObject>();

			private static void Postfix(InventoryGui __instance)
			{
				buttons.Clear();
				CreateUI(__instance);
			}

			public static void ResetColors()
			{
				//IL_0021: Unknown result type (might be due to invalid IL or missing references)
				foreach (KeyValuePair<SortBy, GameObject> button in buttons)
				{
					((Graphic)button.Value.GetComponent<Image>()).color = Color.white;
				}
			}

			private static void CreateUI(InventoryGui gui)
			{
				//IL_0038: Unknown result type (might be due to invalid IL or missing references)
				//IL_003d: Unknown result type (might be due to invalid IL or missing references)
				//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d6: Expected O, but got Unknown
				UI = Object.Instantiate<GameObject>(asset.LoadAsset<GameObject>("kg_SortedCraftingUI"), ((Component)gui.m_crafting).transform);
				((Transform)UI.GetComponent<RectTransform>()).localPosition = Vector2.op_Implicit(new Vector2(-622f, -207f));
				UI.SetActive(false);
				foreach (GameObject item in from x in UI.GetComponentsInChildren<Button>(true)
					select ((Component)x).gameObject)
				{
					if (Enum.TryParse<SortBy>(((Object)item).name, out var sort))
					{
						buttons[sort] = item;
						((UnityEvent)item.GetComponent<Button>().onClick).AddListener((UnityAction)delegate
						{
							ClickButton(sort);
						});
					}
				}
				Material material = ((Graphic)((Component)((Component)gui.m_crafting).transform.Find("Bkg")).GetComponent<Image>()).material;
				((Graphic)((Component)UI.transform.Find("Potions/bg")).GetComponent<Image>()).material = material;
				((Graphic)((Component)UI.transform.Find("Armory/bg")).GetComponent<Image>()).material = material;
			}

			public static void ClickButton(SortBy t)
			{
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				ResetColors();
				((Graphic)buttons[t].GetComponent<Image>()).color = Color.green;
				CurrentSortType = t;
				InventoryGui instance = InventoryGui.instance;
				if (instance != null)
				{
					instance.UpdateCraftingPanel(false);
				}
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "Show")]
		private static class InventoryGuiShowPatch
		{
			private static void Prefix()
			{
				if (!Object.op_Implicit((Object)(object)UI))
				{
					return;
				}
				CurrentSortType = SortBy.All;
				InventoryGuiAwakePatch.ResetColors();
				CraftingStation currentCraftingStation = Player.m_localPlayer.GetCurrentCraftingStation();
				if (!Object.op_Implicit((Object)(object)currentCraftingStation))
				{
					UI.SetActive(false);
					return;
				}
				InventoryGuiAwakePatch.ClickButton(SortBy.All);
				UI.SetActive(true);
				bool flag;
				switch (((Object)currentCraftingStation).name.Replace("(Clone)", ""))
				{
				case "piece_cauldron":
				case "rk_grill":
				case "rk_griddle":
				case "rk_prep":
				case "piece_preptable":
					flag = true;
					break;
				default:
					flag = false;
					break;
				}
				if (flag)
				{
					((Component)UI.transform.Find("Armory")).gameObject.SetActive(false);
					((Component)UI.transform.Find("Potions")).gameObject.SetActive(true);
				}
				else
				{
					((Component)UI.transform.Find("Armory")).gameObject.SetActive(true);
					((Component)UI.transform.Find("Potions")).gameObject.SetActive(false);
				}
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "Hide")]
		private static class InventoryGuiHidePatch
		{
			private static void Prefix()
			{
				if (Object.op_Implicit((Object)(object)UI))
				{
					CurrentSortType = SortBy.All;
					InventoryGuiAwakePatch.ResetColors();
					UI.SetActive(false);
				}
			}
		}

		private const string GUID = "kg_SortedCraft";

		private const string PluginVersion = "1.1.1";

		private static AssetBundle asset;

		private static GameObject UI;

		private static SortBy CurrentSortType;

		private void Awake()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			asset = GetAssetBundle("kg_sortedcraft");
			new Harmony("kg_SortedCraft").PatchAll();
		}

		public static void SortRecipes(ref List<RecipeDataPair> recipes)
		{
			if (Object.op_Implicit((Object)(object)UI) && CurrentSortType != 0)
			{
				recipes = CurrentSortType switch
				{
					SortBy.HP => recipes.OrderByDescending((RecipeDataPair x) => (int)((RecipeDataPair)(ref x)).Recipe.m_item.m_itemData.m_shared.m_itemType == 2).ThenByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_food).ToList(), 
					SortBy.Stamina => recipes.OrderByDescending((RecipeDataPair x) => (int)((RecipeDataPair)(ref x)).Recipe.m_item.m_itemData.m_shared.m_itemType == 2).ThenByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_foodStamina).ToList(), 
					SortBy.Eitr => recipes.OrderByDescending((RecipeDataPair x) => (int)((RecipeDataPair)(ref x)).Recipe.m_item.m_itemData.m_shared.m_itemType == 2).ThenByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_foodEitr).ToList(), 
					SortBy.Armor => recipes.OrderByDescending(delegate(RecipeDataPair x)
					{
						//IL_0016: Unknown result type (might be due to invalid IL or missing references)
						//IL_001b: Unknown result type (might be due to invalid IL or missing references)
						//IL_001c: Unknown result type (might be due to invalid IL or missing references)
						//IL_001e: Unknown result type (might be due to invalid IL or missing references)
						//IL_0020: Invalid comparison between Unknown and I4
						//IL_0022: Unknown result type (might be due to invalid IL or missing references)
						//IL_0025: Unknown result type (might be due to invalid IL or missing references)
						//IL_0027: Invalid comparison between Unknown and I4
						//IL_0029: 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_002e: Invalid comparison between Unknown and I4
						ItemType itemType = ((RecipeDataPair)(ref x)).Recipe.m_item.m_itemData.m_shared.m_itemType;
						return (itemType - 6 <= 1 || itemType - 11 <= 1 || itemType - 17 <= 1) ? true : false;
					}).ThenByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_armor).ToList(), 
					SortBy.Block => recipes.OrderByDescending((RecipeDataPair x) => (int)((RecipeDataPair)(ref x)).Recipe.m_item.m_itemData.m_shared.m_itemType == 5).ThenByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_blockPower).ToList(), 
					SortBy.Physical => recipes.OrderByDescending((RecipeDataPair recipe) => ((DamageTypes)(ref ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages)).GetTotalPhysicalDamage()).ToList(), 
					SortBy.Elemental => recipes.OrderByDescending((RecipeDataPair recipe) => ((DamageTypes)(ref ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages)).GetTotalElementalDamage()).ToList(), 
					SortBy.Blunt => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_blunt).ToList(), 
					SortBy.Slash => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_slash).ToList(), 
					SortBy.Pierce => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_pierce).ToList(), 
					SortBy.Chop => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_chop).ToList(), 
					SortBy.Fire => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_fire).ToList(), 
					SortBy.Frost => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_frost).ToList(), 
					SortBy.Lightning => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_lightning).ToList(), 
					SortBy.Poison => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_poison).ToList(), 
					SortBy.Spirit => recipes.OrderByDescending((RecipeDataPair recipe) => ((RecipeDataPair)(ref recipe)).Recipe.m_item.m_itemData.m_shared.m_damages.m_spirit).ToList(), 
					SortBy.All => recipes, 
					_ => recipes, 
				};
			}
		}

		private static AssetBundle GetAssetBundle(string filename)
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string name = executingAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(filename));
			using Stream stream = executingAssembly.GetManifestResourceStream(name);
			return AssetBundle.LoadFromStream(stream);
		}
	}
}