using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
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("StackableChests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StackableChests")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("116b9b55-e5c8-485a-af93-6e0e8042abf4")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.travv.aleandtale.stackablechests", "StackableChests", "1.0.4")]
public class StackableChests : BaseUnityPlugin
{
private void Awake()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
new Harmony("com.travv.aleandtale.stackablechests").PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"StackableChests loaded (hold X for stacking mode)");
}
}
internal static class StackState
{
public class Holder
{
public Transform Root;
}
public static readonly ConditionalWeakTable<SurfaceSnapFurnitureTool, Holder> Target = new ConditionalWeakTable<SurfaceSnapFurnitureTool, Holder>();
}
internal static class ChestUtil
{
public static bool IsChest(ItemData d)
{
return (Object)(object)d != (Object)null && !string.IsNullOrEmpty(d.name) && d.name.ToLowerInvariant().Contains("chest");
}
public static bool TryGetItemData(ushort dataId, out ItemData data)
{
data = null;
return (Object)(object)ItemManager.Instance != (Object)null && ItemManager.Instance.GetItemData((uint)dataId, ref data) && (Object)(object)data != (Object)null;
}
public static bool StackModeHeld()
{
return Input.GetKey((KeyCode)113);
}
}
[HarmonyPatch(typeof(SurfaceSnapFurnitureTool), "FixedUpdate")]
public static class Patch_SurfaceSnapFurnitureTool_FixedUpdate_ChestStackMode
{
private static readonly FieldRef<SurfaceSnapFurnitureTool, bool> CanPlace = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, bool>("_canPlace");
private static readonly FieldRef<SurfaceSnapFurnitureTool, GameObject> PrvObject = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, GameObject>("_prvObject");
private static readonly FieldRef<SurfaceSnapFurnitureTool, Vector3> TargetPos = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, Vector3>("_targetPos");
private static readonly FieldRef<SurfaceSnapFurnitureTool, Vector3> PrvNormal = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, Vector3>("_prvNormal");
private static readonly FieldRef<SurfaceSnapFurnitureTool, Material> PrvMat = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, Material>("_prvMat");
private static readonly FieldRef<SurfaceSnapFurnitureTool, Item> ToolItem = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, Item>("item");
private static readonly FieldRef<SurfaceSnapFurnitureTool, float> RayDist = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, float>("rayDist");
private static void Postfix(SurfaceSnapFurnitureTool __instance)
{
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
//IL_0068: Unknown result type (might be due to invalid IL or missing references)
//IL_0072: Unknown result type (might be due to invalid IL or missing references)
//IL_0077: Unknown result type (might be due to invalid IL or missing references)
//IL_007c: Unknown result type (might be due to invalid IL or missing references)
//IL_007d: Unknown result type (might be due to invalid IL or missing references)
//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00c0: 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_00de: Unknown result type (might be due to invalid IL or missing references)
//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
//IL_010a: Unknown result type (might be due to invalid IL or missing references)
//IL_010f: 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_017a: Unknown result type (might be due to invalid IL or missing references)
StackState.Holder orCreateValue = StackState.Target.GetOrCreateValue(__instance);
orCreateValue.Root = null;
if (!ChestUtil.StackModeHeld())
{
return;
}
Item val = ToolItem.Invoke(__instance);
if (!ChestUtil.TryGetItemData(val.dataId, out var data) || !ChestUtil.IsChest(data))
{
return;
}
Ray val2 = PlayerMovement.Instance.mainCamera.ViewportPointToRay(Vector3.one * 0.5f);
RaycastHit val3 = default(RaycastHit);
if (Physics.Raycast(val2, ref val3, RayDist.Invoke(__instance)) && !((Object)(object)((RaycastHit)(ref val3)).collider == (Object)null))
{
Bounds bounds = ((RaycastHit)(ref val3)).collider.bounds;
Vector3 val4 = default(Vector3);
((Vector3)(ref val4))..ctor(((Bounds)(ref bounds)).center.x, ((Bounds)(ref bounds)).max.y + 0.03f, ((Bounds)(ref bounds)).center.z);
TargetPos.Invoke(__instance) = val4;
PrvNormal.Invoke(__instance) = Vector3.up;
GameObject val5 = PrvObject.Invoke(__instance);
if ((Object)(object)val5 != (Object)null)
{
val5.transform.position = val4;
val5.SetActive(true);
}
CanPlace.Invoke(__instance) = true;
Material val6 = PrvMat.Invoke(__instance);
if ((Object)(object)val6 != (Object)null)
{
val6.color = FurnitureManager.Instance.colorGood;
}
orCreateValue.Root = ((Component)((RaycastHit)(ref val3)).collider).transform.root;
}
}
}
[HarmonyPatch(typeof(SurfaceSnapFurnitureTool), "OnChildTriggerStay")]
public static class Patch_SurfaceSnapFurnitureTool_OnChildTriggerStay_StackModeIgnoreTarget
{
private static readonly FieldRef<SurfaceSnapFurnitureTool, bool> CanPlace = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, bool>("_canPlace");
private static readonly FieldRef<SurfaceSnapFurnitureTool, Material> PrvMat = AccessTools.FieldRefAccess<SurfaceSnapFurnitureTool, Material>("_prvMat");
private static bool Prefix(SurfaceSnapFurnitureTool __instance, Collider child, Collider other)
{
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
if (!ChestUtil.StackModeHeld())
{
return true;
}
if ((Object)(object)other == (Object)null || other.isTrigger)
{
return false;
}
if (StackState.Target.TryGetValue(__instance, out var value) && (Object)(object)value.Root != (Object)null && (Object)(object)((Component)other).transform.root == (Object)(object)value.Root)
{
return false;
}
CanPlace.Invoke(__instance) = false;
Material val = PrvMat.Invoke(__instance);
if ((Object)(object)val != (Object)null)
{
val.color = FurnitureManager.Instance.colorBad;
}
return false;
}
}