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.2.0")]
[BepInProcess("valheim.exe")]
public class FoodSorting : BaseUnityPlugin
{
[HarmonyPatch(typeof(InventoryGui), "UpdateRecipeList")]
private class UpdateRecipeListPatch
{
private static void Postfix(ref InventoryGui __instance, ref List<RecipeDataPair> ___m_availableRecipes)
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected O, but got Unknown
//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
ManualLogSource val = new ManualLogSource("FoodSorting");
Logger.Sources.Add((ILogSource)(object)val);
if (!Object.op_Implicit((Object)(object)Player.m_localPlayer))
{
val.LogInfo((object)"Patch skipped, not local player.");
return;
}
Dictionary<RecipeDataPair, int> originalOrder = ___m_availableRecipes.Select((RecipeDataPair recipe, int index) => new { recipe, index }).ToDictionary(x => x.recipe, x => x.index);
___m_availableRecipes.Sort(delegate(RecipeDataPair a, RecipeDataPair b)
{
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001d: Invalid comparison between Unknown and I4
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Invalid comparison between Unknown and I4
bool flag = (int)a.Recipe.m_item.m_itemData.m_shared.m_itemType == 2;
bool flag2 = (int)b.Recipe.m_item.m_itemData.m_shared.m_itemType == 2;
if (flag != flag2)
{
return flag2.CompareTo(flag);
}
if (flag && flag2)
{
float value = a.Recipe.m_item.m_itemData.m_shared.m_food + a.Recipe.m_item.m_itemData.m_shared.m_foodStamina + a.Recipe.m_item.m_itemData.m_shared.m_foodEitr;
int num = (b.Recipe.m_item.m_itemData.m_shared.m_food + b.Recipe.m_item.m_itemData.m_shared.m_foodStamina + b.Recipe.m_item.m_itemData.m_shared.m_foodEitr).CompareTo(value);
if (num != 0)
{
return num;
}
}
return originalOrder[a].CompareTo(originalOrder[b]);
});
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));
}
val.LogInfo((object)"Succesfully patched recipes list!");
}
}
private readonly Harmony harmony = new Harmony("shalopay.FoodSorting");
private void Awake()
{
harmony.PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"FoodSorting plugin has started!");
}
}
public 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;
}
}