Decompiled source of FoodSorting v1.1.0

FoodSorting.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FoodSorting")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FoodSorting")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d42527f5-8915-4a40-905d-8d0290701c5c")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace FoodSorting;

[BepInPlugin("shalopay.FoodSorting", "FoodSorting", "1.1.0")]
[BepInProcess("valheim.exe")]
public class FoodSorting : BaseUnityPlugin
{
	[HarmonyPatch(typeof(InventoryGui), "Show")]
	private class ConsumableRecipesOrderPatch
	{
		private static void Postfix(ref InventoryGui __instance, ref List<RecipeDataPair> ___m_availableRecipes, ref RecipeDataPair ___m_selectedRecipe)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			ManualLogSource val = new ManualLogSource("FoodSorting");
			Logger.Sources.Add((ILogSource)(object)val);
			if (debugMode && !shouldPatch)
			{
				shouldPatch = true;
				val.LogInfo((object)"[DEBUG] Skipped patching.");
				return;
			}
			if (debugMode)
			{
				shouldPatch = false;
			}
			var list = ___m_availableRecipes.Select((RecipeDataPair recipe, int index) => new
			{
				RecipePair = recipe,
				Index = index
			}).ToList();
			list.Sort((a, b) =>
			{
				//IL_001e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0024: Invalid comparison between Unknown and I4
				//IL_0044: Unknown result type (might be due to invalid IL or missing references)
				//IL_004a: Invalid comparison between Unknown and I4
				bool flag = (int)a.RecipePair.Recipe.m_item.m_itemData.m_shared.m_itemType == 2;
				bool flag2 = (int)b.RecipePair.Recipe.m_item.m_itemData.m_shared.m_itemType == 2;
				if (flag != flag2)
				{
					return flag2.CompareTo(flag);
				}
				if (flag && flag2)
				{
					float value = a.RecipePair.Recipe.m_item.m_itemData.m_shared.m_food + a.RecipePair.Recipe.m_item.m_itemData.m_shared.m_foodStamina + a.RecipePair.Recipe.m_item.m_itemData.m_shared.m_foodEitr;
					int num = (b.RecipePair.Recipe.m_item.m_itemData.m_shared.m_food + b.RecipePair.Recipe.m_item.m_itemData.m_shared.m_foodStamina + b.RecipePair.Recipe.m_item.m_itemData.m_shared.m_foodEitr).CompareTo(value);
					if (num != 0)
					{
						return num;
					}
				}
				return a.Index.CompareTo(b.Index);
			});
			___m_availableRecipes = list.Select(x => x.RecipePair).ToList();
			for (int i = 0; i < ___m_availableRecipes.Count; i++)
			{
				Transform transform = ___m_availableRecipes[i].InterfaceElement.transform;
				((RectTransform)((transform is RectTransform) ? transform : null)).anchoredPosition = new Vector2(0f, (float)i * (0f - __instance.m_recipeListSpace));
			}
			try
			{
				if (___m_availableRecipes.Count > 0)
				{
					if ((Object)(object)___m_selectedRecipe.Recipe != (Object)null)
					{
						int index2 = SelectedRecipeIndexPatch.MyGetSelectedRecipeIndex(__instance, acceptOneLevelHigher: true);
						SetRecipePatch.MySetRecipe(__instance, index2, center: true);
					}
					else
					{
						SetRecipePatch.MySetRecipe(__instance, 0, center: true);
					}
				}
				else
				{
					SetRecipePatch.MySetRecipe(__instance, -1, center: true);
				}
			}
			catch
			{
				val.LogWarning((object)"Failed to execute set recipe patch!");
			}
			val.LogInfo((object)"Succesfully patched recipes order!");
		}
	}

	private readonly Harmony harmony = new Harmony("shalopay.FoodSorting");

	private static bool debugMode;

	private static bool shouldPatch;

	private void Awake()
	{
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"FoodSorting plugin has started!");
	}
}
[HarmonyPatch]
public class SetRecipePatch
{
	[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPatch(typeof(InventoryGui), "SetRecipe")]
	public static void MySetRecipe(object instance, int index, bool center)
	{
		throw new NotImplementedException("It's a stub");
	}
}
[HarmonyPatch]
public class SelectedRecipeIndexPatch
{
	[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPatch(typeof(InventoryGui), "GetSelectedRecipeIndex")]
	public static int MyGetSelectedRecipeIndex(object instance, bool acceptOneLevelHigher = false)
	{
		throw new NotImplementedException("It's a stub");
	}
}
internal struct RecipeDataPair
{
	public Recipe Recipe { get; private set; }

	public ItemData ItemData { get; private set; }

	public GameObject InterfaceElement { get; private set; }

	public bool CanCraft { get; private set; }

	public RecipeDataPair(Recipe recipe, ItemData data, GameObject element, bool canCraft)
	{
		Recipe = recipe;
		ItemData = data;
		InterfaceElement = element;
		CanCraft = canCraft;
	}
}