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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ComfyBatchDeposit;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ComfyBatchDeposit")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ComfyBatchDeposit")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("709e5af0-05c2-4322-8d30-80819c68d4f6")]
[assembly: AssemblyFileVersion("1.3.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.1.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace BatchDeposit
{
[HarmonyPatch(typeof(InventoryGrid))]
public class InventoryGridPatch
{
private static RectTransform _sortButton;
[HarmonyPrefix]
[HarmonyPatch("OnLeftClick")]
public static bool OnLeftClickPrefix(InventoryGrid __instance, UIInputHandler clickHandler)
{
//IL_0012: 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_004b: 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_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
if (PluginConfig.IsModEnabled.Value && Input.GetKey(PluginConfig.Modifier.Value))
{
GameObject gameObject = ((Component)clickHandler).gameObject;
if ((Object)(object)gameObject == (Object)null)
{
return true;
}
Vector2i buttonPos = __instance.GetButtonPos(gameObject);
bool flag = false;
ItemData itemAt = __instance.m_inventory.GetItemAt(buttonPos.x, buttonPos.y);
if (itemAt == null)
{
return true;
}
if (itemAt != null)
{
for (int i = 0; i < __instance.m_height; i++)
{
for (int num = __instance.m_width - 1; num >= 0; num--)
{
ItemData itemAt2 = __instance.m_inventory.GetItemAt(num, i);
if (itemAt2 != null && itemAt2.m_shared.m_name.Equals(itemAt.m_shared.m_name))
{
__instance.m_onSelected(__instance, itemAt2, new Vector2i(num, i), (Modifier)2);
}
}
}
}
return false;
}
return true;
}
}
}
namespace ComfyBatchDeposit
{
[BepInPlugin("com.bruce.valheim.comfybatchdeposit", "ComfyBatchDeposit", "1.3.1")]
public class ComfyBatchDeposit : BaseUnityPlugin
{
public const string PluginGuid = "com.bruce.valheim.comfybatchdeposit";
public const string PluginName = "ComfyBatchDeposit";
public const string PluginVersion = "1.3.1";
private static ManualLogSource _logger;
private Harmony _harmony;
public void Awake()
{
_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.bruce.valheim.comfybatchdeposit");
_logger = ((BaseUnityPlugin)this).Logger;
PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
}
public void OnDestroy()
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
public static void SortInventory(Inventory inventory, bool isPlayerInventory)
{
Player player = Player.m_localPlayer;
List<ItemData> list = new List<ItemData>(inventory.GetAllItems());
List<ItemData> list2 = new List<ItemData>();
List<string> list3 = new List<string>();
foreach (ItemData itemData in list)
{
if (list3.Contains(itemData.m_shared.m_name + itemData.m_quality))
{
continue;
}
list3.Add(itemData.m_shared.m_name + itemData.m_quality);
List<ItemData> list4 = list.FindAll((ItemData data) => data.m_shared.m_name == itemData.m_shared.m_name && data.m_quality == itemData.m_quality).FindAll((ItemData data) => !data.m_shared.m_questItem && !((Humanoid)player).IsItemEquiped(data) && (!isPlayerInventory || data.m_gridPos.y != 0));
foreach (ItemData item in list4)
{
inventory.RemoveItem(item);
}
if (itemData.m_shared.m_maxStackSize > 1)
{
int num = list4.ConvertAll((ItemData data) => data.m_stack).Sum();
int num2 = num / itemData.m_shared.m_maxStackSize;
num -= num2 * itemData.m_shared.m_maxStackSize;
if (num2 > 0)
{
for (int i = 0; i < num2; i++)
{
ItemData val = itemData.Clone();
val.m_stack = itemData.m_shared.m_maxStackSize;
list2.Add(val);
}
}
if (num > 0)
{
ItemData val2 = itemData.Clone();
val2.m_stack = num;
list2.Add(val2);
}
}
else
{
list2.AddRange(list4);
}
}
list2.Sort((ItemData firstData, ItemData secondData) => string.Compare(firstData.m_shared.m_name, secondData.m_shared.m_name, StringComparison.Ordinal));
foreach (ItemData item2 in list2)
{
inventory.AddItem(item2);
}
inventory.Changed();
}
public static void DumpItems(Inventory from, Inventory target, bool stackOnly)
{
Inventory from2 = from;
List<ItemData> allItems = target.GetAllItems();
List<string> targetItemCompare = allItems.ConvertAll((ItemData input) => input.m_shared.m_name + input.m_quality);
List<ItemData> list = from2.GetAllItems().FindAll((ItemData data) => !stackOnly || (!from2.TopFirst(data) && targetItemCompare.Contains(data.m_shared.m_name + data.m_quality)));
if (((Humanoid)Player.m_localPlayer).GetInventory() == from2)
{
list = list.FindAll((ItemData data) => !((Humanoid)Player.m_localPlayer).IsItemEquiped(data) && !Player.m_localPlayer.IsEquipActionQueued(data)).FindAll((ItemData data) => stackOnly || data.m_gridPos.y != 0);
}
foreach (Tuple<string, int> item2 in list.ConvertAll((ItemData input) => Tuple.Create(input.m_shared.m_name, input.m_quality)).Distinct())
{
string item = item2.Item1;
string compareString = item2.Item1 + item2.Item2;
List<ItemData> list2 = list.FindAll((ItemData data) => data.m_shared.m_name + data.m_quality == compareString);
ItemData val = list2.First().Clone();
int maxStackSize = val.m_shared.m_maxStackSize;
if (maxStackSize <= 1)
{
foreach (ItemData item3 in list2)
{
if (target.AddItem(item3))
{
from2.RemoveItem(item3);
continue;
}
break;
}
continue;
}
int num = list2.ConvertAll((ItemData input) => input.m_stack).Sum();
int num2 = allItems.FindAll((ItemData data) => data.m_shared.m_name + data.m_quality == compareString).ConvertAll((ItemData input) => input.m_stack).Sum() % maxStackSize;
int num3 = ((num2 != 0) ? (maxStackSize - num2) : 0);
num3 += target.GetEmptySlots() * maxStackSize;
int num4 = ((num3 >= num) ? num : num3);
if (num4 <= 0)
{
continue;
}
int num5 = num4 / maxStackSize;
int num6 = num4 - num5 * maxStackSize;
if (num5 > 0)
{
for (int i = 0; i < num5; i++)
{
ItemData val2 = val.Clone();
val2.m_stack = maxStackSize;
target.AddItem(val2);
}
}
if (num6 > 0)
{
ItemData val3 = val.Clone();
val3.m_stack = num6;
target.AddItem(val3);
}
from2.RemoveItem(item, num4, -1, true);
}
target.Changed();
from2.Changed();
}
}
public class PluginConfig
{
public static ConfigEntry<bool> IsModEnabled;
public static ConfigEntry<bool> SortOnDump;
public static ConfigEntry<KeyCode> Modifier;
public static void BindConfig(ConfigFile config)
{
IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
SortOnDump = config.Bind<bool>("Behavior", "sortOnDump", false, "Runs sort function immediately after dump function is completely when dump button is pressed.");
Modifier = config.Bind<KeyCode>("DepositAllModifier", "depositAllModifier", (KeyCode)308, "Hot key modifier for deposit all on left click.");
}
}
}
namespace ComfyBatchDeposit.Patches
{
[HarmonyPatch(typeof(InventoryGui))]
public class InventoryGuiPatch
{
private static RectTransform _sortButton;
private static RectTransform _dumpButton;
[HarmonyPostfix]
[HarmonyPatch("Show")]
public static void ShowPostfix(InventoryGui __instance, Container container)
{
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_0080: Expected O, but got Unknown
//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
//IL_00dc: Expected O, but got Unknown
InventoryGui __instance2 = __instance;
if (!__instance2.IsContainerOpen())
{
return;
}
if ((Object)(object)_sortButton == (Object)null)
{
_sortButton = PrepareButton(__instance2, "sort", "S");
RelocateButtons(_sortButton, 0.3f);
((UnityEvent)((Component)_sortButton).GetComponent<Button>().onClick).AddListener((UnityAction)delegate
{
if (!((Character)Player.m_localPlayer).IsTeleporting() && Object.op_Implicit((Object)(object)__instance2.m_containerGrid))
{
ComfyBatchDeposit.SortInventory(__instance2.m_containerGrid.GetInventory(), isPlayerInventory: false);
}
});
}
if (!((Object)(object)_dumpButton == (Object)null))
{
return;
}
_dumpButton = PrepareButton(__instance2, "dump", "D");
RelocateButtons(_dumpButton, 1.5f);
((UnityEvent)((Component)_dumpButton).GetComponent<Button>().onClick).AddListener((UnityAction)delegate
{
if (!((Character)Player.m_localPlayer).IsTeleporting() && Object.op_Implicit((Object)(object)__instance2.m_containerGrid))
{
ComfyBatchDeposit.DumpItems(__instance2.m_playerGrid.GetInventory(), __instance2.m_containerGrid.GetInventory(), !Input.GetKey((KeyCode)304));
if (PluginConfig.SortOnDump.Value)
{
ComfyBatchDeposit.SortInventory(__instance2.m_containerGrid.GetInventory(), isPlayerInventory: false);
}
}
});
}
private static RectTransform PrepareButton(InventoryGui instance, string name, string text)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0018: 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_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_0098: Expected O, but got Unknown
RectTransform val = (RectTransform)((Component)instance).transform.parent.Find(name);
if ((Object)(object)val != (Object)null)
{
return val;
}
Transform transform = ((Component)instance.m_takeAllButton).transform;
Transform val2 = Object.Instantiate<Transform>(transform, ((Component)transform).transform.parent);
((Object)val2).name = name;
Transform transform2 = ((Component)val2).transform;
val = (RectTransform)((Component)transform2).transform;
val.SetSizeWithCurrentAnchors((Axis)0, 45f);
val.SetSizeWithCurrentAnchors((Axis)1, 30f);
RectTransform val3 = (RectTransform)((Component)val).transform.Find("Text");
val3.SetSizeWithCurrentAnchors((Axis)0, 45f);
val3.SetSizeWithCurrentAnchors((Axis)1, 30f);
TMP_Text component = ((Component)val3).GetComponent<TMP_Text>();
component.text = text;
component.m_enableAutoSizing = true;
return val;
}
private static void RelocateButtons(RectTransform transform, float vertical)
{
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
if (Object.op_Implicit((Object)(object)transform))
{
transform.pivot = new Vector2(-10f, vertical);
}
}
}
}