Decompiled source of ShelfLayoutKeeper v1.1.2

BepInEx/plugins/OldMarket.ShelfLayoutKeeper.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;
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("OldMarket.ShelfReplace")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OldMarket.ShelfReplace")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("bdf5b2d1-095e-4269-ab6f-6d9f4f1ac25b")]
[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")]
namespace OldMarketMods.ShelfReplaceHelper;

[HarmonyPatch(typeof(PlayerInventory), "HandleDrop")]
internal static class PlayerInventory_HandleDrop_Patch
{
	private static void Prefix(PlayerInventory __instance)
	{
		//IL_0020: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: 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_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!((NetworkBehaviour)__instance).IsOwner)
			{
				return;
			}
			PlayerActions player = InputManager.Instance.inputMaster.Player;
			if (!((PlayerActions)(ref player)).Drop.WasPerformedThisFrame())
			{
				return;
			}
			ShelfReplaceManager instance = ShelfReplaceManager.Instance;
			if (instance == null || !instance.HasData)
			{
				return;
			}
			NetworkList<InventorySlot> slots = __instance.slots;
			if (slots == null || slots.Count == 0)
			{
				return;
			}
			int value = __instance.currentSlot.Value;
			if (value >= 0 && value < slots.Count)
			{
				InventorySlot val = slots[value];
				if (val.itemId == instance.ShelfBlockId)
				{
					float num = 0.25f;
					float num2 = 1f;
					Vector3 position = ((Component)__instance).transform.position;
					Vector3 forward = ((Component)__instance).transform.forward;
					forward.y = 0f;
					Vector3 dropPosition = position + forward * num + new Vector3(0f, num2, 0f);
					instance.DropAtPosition(dropPosition);
				}
			}
		}
		catch (Exception arg)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)$"[ShelfReplace] HandleDrop Prefix error: {arg}");
			}
		}
	}
}
[HarmonyPatch(typeof(PlayerInventory), "HandleNewBlockPlacement")]
internal static class PlayerInventory_HandleNewBlockPlacement_Patch
{
	private static readonly FieldInfo BlockBlueprintField = AccessTools.Field(typeof(PlayerInventory), "blockBlueprint");

	private static readonly FieldInfo MeshRenderersBlockBlueprintField = AccessTools.Field(typeof(PlayerInventory), "meshRenderersBlockBlueprint");

	private static void Postfix(PlayerInventory __instance)
	{
		try
		{
			if (!((NetworkBehaviour)__instance).IsOwner)
			{
				return;
			}
			ShelfReplaceManager instance = ShelfReplaceManager.Instance;
			if (instance == null || !instance.HasData || BlockBlueprintField == null || MeshRenderersBlockBlueprintField == null)
			{
				return;
			}
			object? value = BlockBlueprintField.GetValue(__instance);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if ((Object)(object)val == (Object)null || (Object)(object)val.GetComponent<ShelfBlueprintMarker>() != (Object)null)
			{
				return;
			}
			Block componentInChildren = val.GetComponentInChildren<Block>();
			if ((Object)(object)componentInChildren == (Object)null || (Object)(object)componentInChildren.blockSO == (Object)null || !ShelfReplaceUtil.IsShelfLikeBlock(componentInChildren))
			{
				return;
			}
			BlockSO blockSO = componentInChildren.blockSO;
			if ((Object)(object)blockSO == (Object)null || (instance.ShelfBlockId != 0L && instance.ShelfBlockId != ((ItemSO)blockSO).id))
			{
				return;
			}
			List<MeshRenderer> rendererList = MeshRenderersBlockBlueprintField.GetValue(__instance) as List<MeshRenderer>;
			if (rendererList == null)
			{
				return;
			}
			Transform root = ((Component)componentInChildren).transform;
			instance.ForEachItemForBlueprint(delegate(long itemId, Vector3 localPos, Quaternion localRot)
			{
				//IL_004d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0058: 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_005e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0063: 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_006f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0074: Unknown result type (might be due to invalid IL or missing references)
				//IL_0075: Unknown result type (might be due to invalid IL or missing references)
				//IL_007a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0081: Unknown result type (might be due to invalid IL or missing references)
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					if (!((Object)(object)GameManager.Instance == (Object)null))
					{
						ItemSO itemById = GameManager.Instance.GetItemById(itemId);
						if (!((Object)(object)itemById == (Object)null) && !((Object)(object)itemById.prefab == (Object)null))
						{
							Vector3 val2 = root.position + root.rotation * localPos;
							Quaternion val3 = root.rotation * localRot;
							GameObject val4 = Object.Instantiate<GameObject>(itemById.prefab, val2, val3);
							val4.transform.SetParent(root, true);
							Collider[] componentsInChildren = val4.GetComponentsInChildren<Collider>();
							foreach (Collider val5 in componentsInChildren)
							{
								val5.enabled = false;
							}
							NetworkObject component = val4.GetComponent<NetworkObject>();
							if ((Object)(object)component != (Object)null)
							{
								Object.Destroy((Object)(object)component);
							}
							MonoBehaviour[] componentsInChildren2 = val4.GetComponentsInChildren<MonoBehaviour>();
							foreach (MonoBehaviour val6 in componentsInChildren2)
							{
								((Behaviour)val6).enabled = false;
							}
							MeshRenderer[] componentsInChildren3 = val4.GetComponentsInChildren<MeshRenderer>();
							if (componentsInChildren3 != null && componentsInChildren3.Length != 0)
							{
								rendererList.AddRange(componentsInChildren3);
							}
						}
					}
				}
				catch (Exception ex2)
				{
					ManualLogSource log2 = ShelfReplacePlugin.Log;
					if (log2 != null)
					{
						log2.LogError((object)("[ShelfLayoutKeeper] Blueprint ghost spawn failed: " + ex2));
					}
				}
			});
			val.AddComponent<ShelfBlueprintMarker>();
		}
		catch (Exception ex)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)("[ShelfLayoutKeeper] HandleNewBlockPlacement Postfix error: " + ex));
			}
		}
	}
}
internal class ShelfBlueprintMarker : MonoBehaviour
{
	internal long BlockId;

	internal readonly List<Transform> GhostTransforms = new List<Transform>();

	internal readonly List<Vector3> BaseLocalPositions = new List<Vector3>();
}
[HarmonyPatch(typeof(GameManager), "RemoveBlockServerRpc")]
internal static class GameManager_RemoveBlockServerRpc_Patch
{
	private static void Prefix(NetworkObjectReference target, ServerRpcParams serverRpcParams)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			ShelfReplaceManager instance = ShelfReplaceManager.Instance;
			if (instance == null || !instance.HasData)
			{
				return;
			}
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null))
			{
				ulong senderClientId = serverRpcParams.Receive.SenderClientId;
				if (singleton.ConnectedClients.TryGetValue(senderClientId, out var value) && value != null && !((Object)(object)value.PlayerObject == (Object)null))
				{
					Vector3 position = ((Component)value.PlayerObject).transform.position;
					instance.DropAtPosition(position);
				}
			}
		}
		catch (Exception arg)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)$"[ShelfReplace] RemoveBlockServerRpc Prefix error: {arg}");
			}
		}
	}
}
[HarmonyPatch(typeof(GameManager), "LoadWorldServer")]
internal static class GameManager_LoadWorldServer_Patch
{
	private static void Prefix(GameManager __instance)
	{
		try
		{
			if (((NetworkBehaviour)__instance).IsServer)
			{
				ShelfReplaceManager.Instance?.Clear();
			}
		}
		catch (Exception arg)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)$"[ShelfReplace] LoadWorldServer Prefix error: {arg}");
			}
		}
	}
}
[HarmonyPatch(typeof(Block), "OnNetworkDespawn")]
internal static class Block_OnNetworkDespawn_Patch
{
	private static void Prefix(Block __instance)
	{
		try
		{
			if (!((NetworkBehaviour)__instance).IsServer || !ShelfReplaceUtil.IsShelfLikeBlock(__instance) || (Object)(object)GameManager.Instance == (Object)null || (Object)(object)__instance.blueprintTrigger == (Object)null)
			{
				return;
			}
			ShelfReplaceManager instance = ShelfReplaceManager.Instance;
			if (instance == null)
			{
				return;
			}
			BlockSO blockSO = __instance.blockSO;
			bool flag = (Object)(object)blockSO != (Object)null && blockSO.isStand;
			List<Item> itemsInTrigger = GameManager.Instance.GetItemsInTrigger(__instance.blueprintTrigger);
			if (itemsInTrigger == null || itemsInTrigger.Count == 0)
			{
				return;
			}
			List<Item> list = new List<Item>();
			foreach (Item item in itemsInTrigger)
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				Block supportingBlockForItem = ShelfReplaceUtil.GetSupportingBlockForItem(item);
				if (flag)
				{
					if ((Object)(object)supportingBlockForItem != (Object)null && (Object)(object)supportingBlockForItem != (Object)(object)__instance)
					{
						continue;
					}
				}
				else if ((Object)(object)supportingBlockForItem != (Object)(object)__instance)
				{
					continue;
				}
				if (instance.ShouldCapture(__instance, item))
				{
					list.Add(item);
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			instance.Capture(__instance, list);
			for (int num = list.Count - 1; num >= 0; num--)
			{
				Item val = list[num];
				if ((Object)(object)val != (Object)null && (Object)(object)((NetworkBehaviour)val).NetworkObject != (Object)null && ((NetworkBehaviour)val).NetworkObject.IsSpawned)
				{
					((NetworkBehaviour)val).NetworkObject.Despawn(true);
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)("[ShelfLayoutKeeper] OnNetworkDespawn Prefix error: " + ex));
			}
		}
	}
}
[HarmonyPatch(typeof(Block), "OnNetworkSpawn")]
internal static class Block_OnNetworkSpawn_Patch
{
	private static void Postfix(Block __instance)
	{
		try
		{
			if (((NetworkBehaviour)__instance).IsServer && ShelfReplaceUtil.IsShelfLikeBlock(__instance))
			{
				ShelfReplaceManager instance = ShelfReplaceManager.Instance;
				if (instance != null && instance.HasData)
				{
					instance.AttachTo(__instance);
				}
			}
		}
		catch (Exception arg)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)$"[ShelfReplace] OnNetworkSpawn Postfix error: {arg}");
			}
		}
	}
}
internal class ShelfReplaceManager
{
	private class ShelfItemData
	{
		internal long itemId;

		internal int amount;

		internal int cost;

		internal int dayCounter;

		internal Vector3 localPosition;

		internal Quaternion localRotation;
	}

	private static ShelfReplaceManager _instance;

	private readonly List<ShelfItemData> _items = new List<ShelfItemData>();

	private bool _hasData;

	private long _shelfBlockId;

	private static MethodInfo _serverSetupItemMethod;

	private static FieldInfo _onStandField;

	private static PropertyInfo _networkVarBoolValueProp;

	internal static ShelfReplaceManager Instance => _instance ?? (_instance = new ShelfReplaceManager());

	internal bool HasData => _hasData && _items.Count > 0;

	internal long ShelfBlockId => _shelfBlockId;

	internal void Clear()
	{
		_items.Clear();
		_hasData = false;
		_shelfBlockId = 0L;
	}

	internal bool ShouldCapture(Block shelf, Item item)
	{
		if ((Object)(object)shelf == (Object)null || (Object)(object)shelf.blockSO == (Object)null || (Object)(object)item == (Object)null || (Object)(object)item.itemSO == (Object)null)
		{
			return false;
		}
		BlockSO blockSO = shelf.blockSO;
		if ((Object)(object)blockSO == (Object)null)
		{
			return false;
		}
		if (blockSO.isStand)
		{
			return IsItemOnStand(item);
		}
		return true;
	}

	private static bool IsItemOnStand(Item item)
	{
		if ((Object)(object)item == (Object)null)
		{
			return false;
		}
		try
		{
			if (_onStandField == null)
			{
				_onStandField = typeof(Item).GetField("onStand", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (_onStandField == null)
				{
					FieldInfo[] fields = typeof(Item).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					foreach (FieldInfo fieldInfo in fields)
					{
						if (!fieldInfo.FieldType.IsGenericType)
						{
							continue;
						}
						Type genericTypeDefinition = fieldInfo.FieldType.GetGenericTypeDefinition();
						if (!(genericTypeDefinition != typeof(NetworkVariable<>)))
						{
							Type[] genericArguments = fieldInfo.FieldType.GetGenericArguments();
							if (genericArguments.Length == 1 && genericArguments[0] == typeof(bool) && fieldInfo.Name.IndexOf("stand", StringComparison.OrdinalIgnoreCase) >= 0)
							{
								_onStandField = fieldInfo;
								break;
							}
						}
					}
					if (_onStandField == null)
					{
						ManualLogSource log = ShelfReplacePlugin.Log;
						if (log != null)
						{
							log.LogWarning((object)"[ShelfReplace] Item.onStand field not found. Capture will not filter floor items.");
						}
						return true;
					}
				}
			}
			object value = _onStandField.GetValue(item);
			if (value == null)
			{
				return false;
			}
			if (_networkVarBoolValueProp == null)
			{
				_networkVarBoolValueProp = value.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (_networkVarBoolValueProp == null || _networkVarBoolValueProp.PropertyType != typeof(bool))
				{
					ManualLogSource log2 = ShelfReplacePlugin.Log;
					if (log2 != null)
					{
						log2.LogWarning((object)"[ShelfReplace] NetworkVariable<bool>.Value property not found. Capture will not filter floor items.");
					}
					return true;
				}
			}
			if (_networkVarBoolValueProp.GetValue(value, null) is bool result)
			{
				return result;
			}
			return false;
		}
		catch (Exception ex)
		{
			ManualLogSource log3 = ShelfReplacePlugin.Log;
			if (log3 != null)
			{
				log3.LogError((object)("[ShelfReplace] IsItemOnStand failed: " + ex));
			}
			return true;
		}
	}

	internal void Capture(Block shelf, List<Item> items)
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: 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_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: 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_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		Clear();
		if ((Object)(object)shelf == (Object)null || items == null || items.Count == 0 || (Object)(object)shelf.blockSO == (Object)null)
		{
			return;
		}
		_shelfBlockId = ((ItemSO)shelf.blockSO).id;
		Vector3 position = ((Component)shelf).transform.position;
		Quaternion rotation = ((Component)shelf).transform.rotation;
		Quaternion val = Quaternion.Inverse(rotation);
		foreach (Item item in items)
		{
			if ((Object)(object)item == (Object)null || (Object)(object)item.itemSO == (Object)null)
			{
				continue;
			}
			try
			{
				Vector3 position2 = ((Component)item).transform.position;
				Quaternion rotation2 = ((Component)item).transform.rotation;
				Vector3 localPosition = val * (position2 - position);
				Quaternion localRotation = val * rotation2;
				int amount = ((item.amount == null) ? 1 : item.amount.Value);
				int dayCounter = ((item.dayCounter != null) ? item.dayCounter.Value : 0);
				_items.Add(new ShelfItemData
				{
					itemId = item.itemSO.id,
					amount = amount,
					cost = item.cost,
					dayCounter = dayCounter,
					localPosition = localPosition,
					localRotation = localRotation
				});
			}
			catch (Exception arg)
			{
				ManualLogSource log = ShelfReplacePlugin.Log;
				if (log != null)
				{
					log.LogError((object)$"[ShelfReplace] Capture item failed: {arg}");
				}
			}
		}
		_hasData = _items.Count > 0;
	}

	internal void ForEachItemForBlueprint(Action<long, Vector3, Quaternion> handler)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		if (handler == null || !HasData)
		{
			return;
		}
		foreach (ShelfItemData item in _items)
		{
			handler(item.itemId, item.localPosition, item.localRotation);
		}
	}

	private static void EnsureServerSetupItemMethod()
	{
		if (_serverSetupItemMethod != null)
		{
			return;
		}
		_serverSetupItemMethod = typeof(Item).GetMethod("ServerSetupItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[7]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int),
			typeof(bool),
			typeof(float)
		}, null);
		if (_serverSetupItemMethod == null)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)"[ShelfReplace] Item.ServerSetupItem not found.");
			}
		}
	}

	private void SpawnStandItemOnShelf(ShelfItemData data, Block shelf)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: 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_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: 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_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)GameManager.Instance == (Object)null)
		{
			return;
		}
		EnsureServerSetupItemMethod();
		if (_serverSetupItemMethod == null)
		{
			return;
		}
		Vector3 position = ((Component)shelf).transform.position;
		Quaternion rotation = ((Component)shelf).transform.rotation;
		Vector3 val = position + rotation * data.localPosition;
		Quaternion val2 = rotation * data.localRotation;
		float y = ((Quaternion)(ref rotation)).eulerAngles.y;
		try
		{
			GameObject item = GameManager.Instance.item;
			if (!((Object)(object)item == (Object)null))
			{
				GameObject val3 = Object.Instantiate<GameObject>(item, val, val2);
				NetworkObject component = val3.GetComponent<NetworkObject>();
				if ((Object)(object)component != (Object)null)
				{
					component.Spawn(false);
				}
				Item component2 = val3.GetComponent<Item>();
				if (!((Object)(object)component2 == (Object)null))
				{
					object[] parameters = new object[7] { data.itemId, false, data.amount, data.cost, data.dayCounter, true, y };
					_serverSetupItemMethod.Invoke(component2, parameters);
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)("[ShelfReplace] SpawnStandItemOnShelf failed: " + ex));
			}
		}
	}

	internal void AttachTo(Block newShelf)
	{
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_014a: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		if (!HasData)
		{
			return;
		}
		if ((Object)(object)newShelf == (Object)null || (Object)(object)newShelf.blockSO == (Object)null)
		{
			Clear();
			return;
		}
		BlockSO blockSO = newShelf.blockSO;
		bool flag = (Object)(object)blockSO != (Object)null && blockSO.isStand;
		long num = (((Object)(object)blockSO != (Object)null) ? ((ItemSO)blockSO).id : 0);
		if (_shelfBlockId != 0L && num != _shelfBlockId)
		{
			return;
		}
		if ((Object)(object)GameManager.Instance == (Object)null)
		{
			Clear();
			return;
		}
		if (flag)
		{
			foreach (ShelfItemData item in _items)
			{
				SpawnStandItemOnShelf(item, newShelf);
			}
		}
		else
		{
			Vector3 position = ((Component)newShelf).transform.position;
			Quaternion rotation = ((Component)newShelf).transform.rotation;
			foreach (ShelfItemData item2 in _items)
			{
				try
				{
					Vector3 val = position + rotation * item2.localPosition;
					Quaternion val2 = rotation * item2.localRotation;
					GameManager.Instance.SpawnItemAtPositionServerRpc(item2.itemId, val, val2, true, Vector3.zero, item2.amount, item2.cost, item2.dayCounter);
				}
				catch (Exception ex)
				{
					ManualLogSource log = ShelfReplacePlugin.Log;
					if (log != null)
					{
						log.LogError((object)("[ShelfReplace] Attach spawn (storage) failed: " + ex));
					}
				}
			}
		}
		Clear();
	}

	internal void DropAtPosition(Vector3 dropPosition)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: 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_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		if (!HasData)
		{
			return;
		}
		if ((Object)(object)GameManager.Instance == (Object)null)
		{
			Clear();
			return;
		}
		Vector3 val = dropPosition + Vector3.up * 0.1f;
		Quaternion identity = Quaternion.identity;
		foreach (ShelfItemData item in _items)
		{
			try
			{
				GameManager.Instance.SpawnItemAtPositionServerRpc(item.itemId, val, identity, true, Vector3.zero, item.amount, item.cost, item.dayCounter);
			}
			catch (Exception ex)
			{
				ManualLogSource log = ShelfReplacePlugin.Log;
				if (log != null)
				{
					log.LogError((object)("[ShelfReplace] DropAtPosition failed: " + ex));
				}
			}
		}
		Clear();
	}
}
internal static class ShelfReplaceUtil
{
	internal static bool IsShelfLikeBlock(Block block)
	{
		if ((Object)(object)block == (Object)null)
		{
			return false;
		}
		BlockSO blockSO = block.blockSO;
		if ((Object)(object)blockSO == (Object)null)
		{
			return false;
		}
		if (blockSO.isStand)
		{
			return true;
		}
		if (!string.IsNullOrEmpty(((Object)blockSO).name) && ((Object)blockSO).name.Contains("CellarStand"))
		{
			return true;
		}
		if (!string.IsNullOrEmpty(((Object)block).name) && ((Object)block).name.Contains("CellarStand"))
		{
			return true;
		}
		return false;
	}

	internal static Block GetSupportingBlockForItem(Item item)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)item == (Object)null)
		{
			return null;
		}
		try
		{
			Vector3 val = ((Component)item).transform.position + Vector3.up * 0.05f;
			Vector3 down = Vector3.down;
			float num = 2f;
			RaycastHit[] array = Physics.RaycastAll(val, down, num, -1, (QueryTriggerInteraction)1);
			Block result = null;
			float num2 = float.MaxValue;
			for (int i = 0; i < array.Length; i++)
			{
				Collider collider = ((RaycastHit)(ref array[i])).collider;
				if (!((Object)(object)collider == (Object)null))
				{
					Block componentInParent = ((Component)collider).GetComponentInParent<Block>();
					if (!((Object)(object)componentInParent == (Object)null) && ((RaycastHit)(ref array[i])).distance < num2)
					{
						num2 = ((RaycastHit)(ref array[i])).distance;
						result = componentInParent;
					}
				}
			}
			return result;
		}
		catch (Exception ex)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)("[ShelfLayoutKeeper] GetSupportingBlockForItem failed: " + ex));
			}
			return null;
		}
	}
}
internal sealed class ShelfHotbarMarker : MonoBehaviour
{
	private PlayerInventory _inventory;

	private readonly List<Transform> _markedSlots = new List<Transform>();

	private float _timer;

	private const float Interval = 0.2f;

	internal void Initialize(PlayerInventory inventory)
	{
		_inventory = inventory;
	}

	private void Update()
	{
		if (!((Object)(object)_inventory == (Object)null) && ((NetworkBehaviour)_inventory).IsLocalPlayer)
		{
			_timer += Time.unscaledDeltaTime;
			if (!(_timer < 0.2f))
			{
				_timer = 0f;
				Refresh();
			}
		}
	}

	private void Refresh()
	{
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		ShelfReplaceManager instance = ShelfReplaceManager.Instance;
		if (instance == null || !instance.HasData)
		{
			ClearAllMarkers();
			return;
		}
		if ((Object)(object)GameManager.Instance == (Object)null)
		{
			ClearAllMarkers();
			return;
		}
		long shelfBlockId = instance.ShelfBlockId;
		if (shelfBlockId == 0)
		{
			ClearAllMarkers();
			return;
		}
		NetworkList<InventorySlot> slots = _inventory.slots;
		Transform transform = _inventory.itemSlots.transform;
		int num = Mathf.Min(slots.Count, transform.childCount);
		ClearAllMarkers();
		for (int i = 0; i < num; i++)
		{
			InventorySlot val = slots[i];
			Transform child = transform.GetChild(i);
			if (((Component)child).gameObject.activeInHierarchy && val.itemId == shelfBlockId)
			{
				ItemSO itemById = GameManager.Instance.GetItemById(val.itemId);
				BlockSO val2 = (BlockSO)(object)((itemById is BlockSO) ? itemById : null);
				if (!((Object)(object)val2 == (Object)null))
				{
					AddMarker(child);
				}
			}
		}
	}

	private void AddMarker(Transform slotTransform)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		//IL_0066: 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_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: 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 (!((Object)(object)slotTransform == (Object)null))
		{
			if ((Object)(object)slotTransform.Find("ShelfReplace_Marker") != (Object)null)
			{
				_markedSlots.Add(slotTransform);
				return;
			}
			GameObject val = new GameObject("ShelfReplace_Marker");
			val.transform.SetParent(slotTransform, false);
			RectTransform val2 = val.AddComponent<RectTransform>();
			val2.anchorMin = new Vector2(1f, 1f);
			val2.anchorMax = new Vector2(1f, 1f);
			val2.pivot = new Vector2(1f, 1f);
			val2.sizeDelta = new Vector2(12f, 12f);
			val2.anchoredPosition = new Vector2(-4f, -4f);
			Image val3 = val.AddComponent<Image>();
			((Graphic)val3).raycastTarget = false;
			((Graphic)val3).color = new Color(0f, 1f, 0f, 0.9f);
			_markedSlots.Add(slotTransform);
		}
	}

	private void ClearAllMarkers()
	{
		for (int i = 0; i < _markedSlots.Count; i++)
		{
			Transform val = _markedSlots[i];
			if (!((Object)(object)val == (Object)null))
			{
				Transform val2 = val.Find("ShelfReplace_Marker");
				if ((Object)(object)val2 != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)val2).gameObject);
				}
			}
		}
		_markedSlots.Clear();
	}

	private void OnDestroy()
	{
		ClearAllMarkers();
	}
}
[HarmonyPatch(typeof(PlayerInventory), "OnNetworkSpawn")]
internal static class PlayerInventory_OnNetworkSpawn_Patch
{
	private static void Postfix(PlayerInventory __instance)
	{
		try
		{
			if (((NetworkBehaviour)__instance).IsLocalPlayer && !((Object)(object)__instance.itemSlots == (Object)null) && !((Object)(object)__instance.itemSlots.GetComponent<ShelfHotbarMarker>() != (Object)null))
			{
				ShelfHotbarMarker shelfHotbarMarker = __instance.itemSlots.gameObject.AddComponent<ShelfHotbarMarker>();
				shelfHotbarMarker.Initialize(__instance);
			}
		}
		catch (Exception arg)
		{
			ManualLogSource log = ShelfReplacePlugin.Log;
			if (log != null)
			{
				log.LogError((object)$"[ShelfReplace] PlayerInventory_OnNetworkSpawn error: {arg}");
			}
		}
	}
}
[BepInPlugin("oldmarket.shelflayoutkeeper", "Old Market Shelf Layout Keeper", "1.1.2")]
[BepInProcess("Old Market Simulator.exe")]
public class ShelfReplacePlugin : BaseUnityPlugin
{
	public const string PluginGuid = "oldmarket.shelflayoutkeeper";

	public const string PluginName = "Old Market Shelf Layout Keeper";

	public const string PluginVersion = "1.1.2";

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			Harmony val = new Harmony("oldmarket.shelflayoutkeeper");
			val.PatchAll();
		}
		catch (Exception arg)
		{
			Log.LogError((object)$"[ShelfReplace] Harmony patch failed: {arg}");
		}
	}
}