Decompiled source of StoreKnightV v1.1.0

StoreKnightV.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Il2CppSystem.Reflection;
using Sons.Inventory;
using Sons.Items.Core;
using TheForest.Items.Inventory;
using TheForest.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("StoreKnightV")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StoreKnightV")]
[assembly: AssemblyTitle("StoreKnightV")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace StoreKnightV;

[BepInPlugin("SmokyAce.StoreKnightV", "StoreKnightV", "1.1.0")]
public class StoreKnightVPlugin : BasePlugin
{
	private class ModelPart
	{
		public string Name;

		public Mesh Mesh;

		public Il2CppReferenceArray<Material> Materials;

		public Renderer Source;

		public Vector3 Position;

		public Quaternion Rotation;

		public Vector3 Scale;
	}

	public const string Guid = "SmokyAce.StoreKnightV";

	public const string Name = "StoreKnightV";

	public const string Version = "1.1.0";

	internal static ManualLogSource ModLog;

	internal static ConfigEntry<KeyCode> StoreKey;

	internal static ConfigEntry<int> ModelItemId;

	internal static ConfigEntry<string> NextToItem;

	internal static ConfigEntry<float> OffsetX;

	internal static ConfigEntry<float> OffsetY;

	internal static ConfigEntry<bool> UseKnightModel;

	internal static ConfigEntry<float> ModelScale;

	internal static ConfigEntry<bool> FlipModel;

	private const string ModelName = "KnightVModel";

	private static readonly Regex LowerLod = new Regex("LOD_?[1-9]", RegexOptions.IgnoreCase);

	private static List<ModelPart> knightParts;

	private static Bounds knightBounds;

	private static bool knightPartsLoaded;

	private static readonly HashSet<string> loggedProblems = new HashSet<string>();

	private static readonly Dictionary<int, Transform> slotSources = new Dictionary<int, Transform>();

	private static readonly Dictionary<int, InventoryLayoutItemGroup> slotAnchors = new Dictionary<int, InventoryLayoutItemGroup>();

	private static readonly HashSet<int> placedSlots = new HashSet<int>();

	private static bool dumpedInventory;

	private static bool storageApplied;

	private static bool reportedGroups;

	private static bool storing;

	private static int lastStoreFrame = -1;

	private static readonly List<GameObject> createdSlots = new List<GameObject>();

	internal static bool IsStoring => storing;

	internal static int SlotCount
	{
		get
		{
			createdSlots.RemoveAll((GameObject slot) => (Object)(object)slot == (Object)null);
			return createdSlots.Count;
		}
	}

	public override void Load()
	{
		//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_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: 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_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: 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_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Expected O, but got Unknown
		ModLog = ((BasePlugin)this).Log;
		ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "StoreKnightV.cfg"), true);
		StoreKey = val.Bind<KeyCode>("General", "StoreKey", (KeyCode)103, "Stores the Knight V in the inventory, the same way G stores the glider");
		ModelItemId = val.Bind<int>("Inventory", "ModelItemId", 529, "Item whose inventory model is used for the stored Knight V. 529 is the GPS locator. The log lists every inventory item id when you load a game");
		NextToItem = val.Bind<string>("Inventory", "NextToItem", "PlasmaLighter", "Name of the inventory item the Knight V is placed next to. Leave empty to place it above the model item");
		OffsetX = val.Bind<float>("Inventory", "OffsetX", -1f, "Horizontal position next to that item, in item widths. -1 is one slot to one side, 1 the other side");
		OffsetY = val.Bind<float>("Inventory", "OffsetY", 1f, "Vertical position next to that item, in item heights");
		UseKnightModel = val.Bind<bool>("Inventory", "ShowKnightVModel", false, "Experimental: replace the copied model with the Knight V vehicle model");
		ModelScale = val.Bind<float>("Inventory", "ModelScale", 0.9f, "Size of the Knight V vehicle model relative to the model it replaces");
		FlipModel = val.Bind<bool>("Inventory", "FlipModel", false, "Turn the Knight V model 180 degrees on the inventory mat");
		Harmony.CreateAndPatchAll(typeof(DropRightHandPatch), (string)null);
		Harmony.CreateAndPatchAll(typeof(InventorySavePatch), (string)null);
		Harmony.CreateAndPatchAll(typeof(InventoryLoadPatch), (string)null);
		ClassInjector.RegisterTypeInIl2Cpp<StoreKnightVBehaviour>();
		GameObject val2 = new GameObject("StoreKnightV")
		{
			hideFlags = (HideFlags)61
		};
		Object.DontDestroyOnLoad((Object)val2);
		val2.AddComponent<StoreKnightVBehaviour>();
		((BasePlugin)this).Log.LogInfo((object)"Loaded StoreKnightV 1.1.0");
	}

	internal static void MakeStorable()
	{
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		if (storageApplied)
		{
			return;
		}
		try
		{
			ItemData val = ItemDatabaseManager.ItemById(ItemDatabase.KnightVId);
			ItemData val2 = ItemDatabaseManager.ItemById(ItemDatabase.HangGliderId);
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
			{
				val._canNotBeStored = false;
				val._alwaysDropOnUnequip = false;
				val._preventEquipFromInventoryView = false;
				val._canBeHotKeyed = val2._canBeHotKeyed;
				val._hotkeyCanToggleItem = val2._hotkeyCanToggleItem;
				val._allowQuickSelect = val2._allowQuickSelect;
				val._quickSelectSlot = val2._quickSelectSlot;
				val._alwaysLoadStashedFromSaves = true;
				if (val._maxAmount < 1)
				{
					val._maxAmount = ((val2._maxAmount <= 0) ? 1 : val2._maxAmount);
				}
				storageApplied = true;
				ModLog.LogInfo((object)"Knight V can now be stored in the inventory");
			}
		}
		catch (Exception ex)
		{
			ModLog.LogWarning((object)("Could not update Knight V item data: " + ex.Message));
		}
	}

	internal static void TryStoreFromKey()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Invalid comparison between Unknown and I4
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Invalid comparison between Unknown and I4
		if (!Input.GetKeyDown(StoreKey.Value))
		{
			return;
		}
		PlayerInventory inventory = LocalPlayer.Inventory;
		if ((Object)(object)inventory == (Object)null)
		{
			return;
		}
		if ((int)inventory.CurrentView == 2)
		{
			if (LocalPlayer.IsInWorld)
			{
				TryStoreHeldKnight(inventory);
			}
		}
		else if ((int)inventory.CurrentView == 3)
		{
			TryTakeOut(inventory);
		}
	}

	private static void TryTakeOut(PlayerInventory inventory)
	{
		if (storing || (Object)(object)inventory == (Object)null || IsHoldingKnight(inventory))
		{
			return;
		}
		int knightVId = ItemDatabase.KnightVId;
		try
		{
			if (inventory.Owns(knightVId))
			{
				inventory.TryEquip(knightVId, false, true, false);
			}
		}
		catch (Exception ex)
		{
			ModLog.LogWarning((object)("Could not equip the Knight V: " + ex.Message));
		}
	}

	internal static bool TryStoreHeldKnight(PlayerInventory inventory)
	{
		if (storing || (Object)(object)inventory == (Object)null || Time.frameCount == lastStoreFrame)
		{
			return false;
		}
		if (!IsHoldingKnight(inventory))
		{
			return false;
		}
		storing = true;
		lastStoreFrame = Time.frameCount;
		bool result;
		try
		{
			MakeStorable();
			int knightVId = ItemDatabase.KnightVId;
			if (!inventory.Owns(knightVId))
			{
				inventory.AddItem(knightVId, 1, true, false, (ItemInstance)null);
			}
			if (inventory.IsRightHandHolding(knightVId))
			{
				inventory.StashRightHandItem(true, false, false);
			}
			else if (inventory.IsLeftHandHolding(knightVId))
			{
				inventory.StashLeftHandItem(true, false);
			}
			else
			{
				inventory.StashHeldItems(true, false);
			}
			ManualLogSource modLog = ModLog;
			result = inventory.Owns(knightVId);
			modLog.LogInfo((object)("Stored the Knight V. Owns=" + result));
			result = true;
		}
		catch (Exception ex)
		{
			ModLog.LogWarning((object)("Could not store the Knight V: " + ex.Message));
			result = false;
		}
		finally
		{
			storing = false;
		}
		return result;
	}

	internal static bool IsHoldingKnight(PlayerInventory inventory)
	{
		if ((Object)(object)inventory == (Object)null)
		{
			return false;
		}
		int knightVId = ItemDatabase.KnightVId;
		if (!inventory.IsRightHandHolding(knightVId) && !inventory.IsLeftHandHolding(knightVId) && !inventory.HasInSlot((EquipmentSlot)4, knightVId) && !inventory.HasInSlot((EquipmentSlot)3, knightVId))
		{
			return inventory.HasInSlot((EquipmentSlot)0, knightVId);
		}
		return true;
	}

	internal static void TryCreateSlots()
	{
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		if (!LocalPlayer.IsInWorld)
		{
			return;
		}
		createdSlots.RemoveAll((GameObject slot) => (Object)(object)slot == (Object)null);
		InventoryLayoutItemGroup[] array;
		try
		{
			array = Il2CppArrayBase<InventoryLayoutItemGroup>.op_Implicit(Object.FindObjectsOfType<InventoryLayoutItemGroup>(true));
		}
		catch (Exception)
		{
			return;
		}
		if (array == null)
		{
			return;
		}
		int num = ((ModelItemId.Value > 0) ? ModelItemId.Value : ItemDatabase.GpsLocatorId);
		List<InventoryLayoutItemGroup> list = new List<InventoryLayoutItemGroup>();
		List<string> list2 = new List<string>();
		bool flag = false;
		InventoryLayoutItemGroup[] array2 = array;
		foreach (InventoryLayoutItemGroup val in array2)
		{
			if (!((Object)(object)val == (Object)null) && ((LayoutItemGroup<InventoryLayoutItem>)(object)val).ItemId != ItemDatabase.KnightVId)
			{
				string text = ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)val).ItemId);
				list2.Add(((LayoutItemGroup<InventoryLayoutItem>)(object)val).ItemId + " " + text);
				if (((LayoutItemGroup<InventoryLayoutItem>)(object)val).ItemId == num)
				{
					flag = true;
				}
				if (!string.IsNullOrEmpty(NextToItem.Value) && string.Equals(text, NextToItem.Value.Trim(), StringComparison.OrdinalIgnoreCase))
				{
					list.Add(val);
				}
			}
		}
		if (!reportedGroups && list2.Count > 0)
		{
			reportedGroups = true;
			list2.Sort();
			ModLog.LogInfo((object)("Inventory items (id name): " + string.Join(", ", list2)));
		}
		if (!flag)
		{
			ModelProblem("No inventory slot found for model item " + num + " " + ItemName(num));
			return;
		}
		array2 = array;
		foreach (InventoryLayoutItemGroup val2 in array2)
		{
			if ((Object)(object)val2 == (Object)null || ((LayoutItemGroup<InventoryLayoutItem>)(object)val2).ItemId != num || HasKnightSlot(((Component)val2).transform.parent))
			{
				continue;
			}
			try
			{
				GameObject val3 = CreateSlot(val2, Nearest(list, ((Component)val2).transform.position));
				if ((Object)(object)val3 != (Object)null)
				{
					createdSlots.Add(val3);
				}
			}
			catch (Exception ex2)
			{
				ModLog.LogWarning((object)("Could not create the inventory marker: " + ex2.Message));
			}
		}
	}

	private static bool HasKnightSlot(Transform parent)
	{
		if ((Object)(object)parent == (Object)null)
		{
			return false;
		}
		for (int i = 0; i < parent.childCount; i++)
		{
			if (((Object)parent.GetChild(i)).name.StartsWith("KnightVInventory"))
			{
				return true;
			}
		}
		return false;
	}

	private static string ItemName(int id)
	{
		try
		{
			ItemData val = ItemDatabaseManager.ItemById(id);
			return ((Object)(object)val != (Object)null) ? val.Name : "?";
		}
		catch (Exception)
		{
			return "?";
		}
	}

	private static InventoryLayoutItemGroup Nearest(List<InventoryLayoutItemGroup> groups, Vector3 position)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: 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_002a: Unknown result type (might be due to invalid IL or missing references)
		InventoryLayoutItemGroup result = null;
		float num = float.MaxValue;
		foreach (InventoryLayoutItemGroup group in groups)
		{
			Vector3 val = ((Component)group).transform.position - position;
			float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
			if (sqrMagnitude < num)
			{
				result = group;
				num = sqrMagnitude;
			}
		}
		return result;
	}

	private static string PlaceSlot(GameObject clone, InventoryLayoutItemGroup template, InventoryLayoutItemGroup anchor)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: 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_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		clone.transform.localPosition = ((Component)template).transform.localPosition;
		clone.transform.localRotation = ((Component)template).transform.localRotation;
		clone.transform.localScale = ((Component)template).transform.localScale;
		if ((Object)(object)anchor != (Object)null)
		{
			slotAnchors[((Object)clone).GetInstanceID()] = anchor;
			if (TryPlaceNextTo(clone, ((Component)template).transform, anchor, out var _))
			{
				placedSlots.Add(((Object)clone).GetInstanceID());
				return "next to the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)anchor).ItemId);
			}
		}
		Vector3 localPosition = ((Component)template).transform.localPosition;
		localPosition.y += SlotSpan(((Component)template).transform, 1);
		clone.transform.localPosition = localPosition;
		if (!((Object)(object)anchor != (Object)null))
		{
			return "above the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)template).ItemId);
		}
		return "above the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)template).ItemId) + " until the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)anchor).ItemId) + " model is loaded";
	}

	private static bool TryPlaceNextTo(GameObject clone, Transform template, InventoryLayoutItemGroup anchor, out string missing)
	{
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: 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_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		missing = null;
		Transform parent = ((Component)anchor).transform.parent;
		if ((Object)(object)parent == (Object)null)
		{
			missing = "the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)anchor).ItemId) + " has no parent";
			return false;
		}
		if (!TryMeshBounds(GroupRenderers(((Component)anchor).transform), parent, out var bounds, out var template2))
		{
			missing = "no mesh on the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)anchor).ItemId);
			return false;
		}
		if (!TryMeshBounds(GroupRenderers(template), parent, out var bounds2, out template2))
		{
			missing = "no mesh on the " + ((Object)template).name;
			return false;
		}
		clone.transform.localPosition = template.localPosition;
		Vector3 val = ((Bounds)(ref bounds)).center + new Vector3(OffsetX.Value * (((Bounds)(ref bounds)).size.x + ((Bounds)(ref bounds2)).size.x) * 0.55f, OffsetY.Value * (((Bounds)(ref bounds)).size.y + ((Bounds)(ref bounds2)).size.y) * 0.55f, 0f);
		Transform transform = clone.transform;
		transform.position += parent.TransformVector(val - ((Bounds)(ref bounds2)).center);
		return true;
	}

	private static GameObject CreateSlot(InventoryLayoutItemGroup template, InventoryLayoutItemGroup anchor)
	{
		GameObject val = CloneSlot(template);
		if ((Object)(object)val == (Object)null)
		{
			return null;
		}
		slotSources[((Object)val).GetInstanceID()] = ((Component)template).transform;
		string text = PlaceSlot(val, template, anchor);
		bool flag = false;
		try
		{
			flag = ApplyKnightModel(val);
		}
		catch (Exception ex)
		{
			ModLog.LogWarning((object)("Could not build the Knight V model: " + ex.Message));
		}
		val.SetActive(true);
		if (createdSlots.Count == 0)
		{
			DumpSlot(val, "slot created");
		}
		ModLog.LogInfo((object)((flag ? "Placed the Knight V model " : ("Placed a " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)template).ItemId) + " model for the Knight V ")) + text));
		return val;
	}

	internal static void MaintainSlots()
	{
		createdSlots.RemoveAll((GameObject slot) => (Object)(object)slot == (Object)null);
		DumpOnInventoryOpen();
		foreach (GameObject createdSlot in createdSlots)
		{
			int instanceID = ((Object)createdSlot).GetInstanceID();
			slotSources.TryGetValue(instanceID, out var value);
			if (!placedSlots.Contains(instanceID) && (Object)(object)value != (Object)null && slotAnchors.TryGetValue(instanceID, out var value2) && (Object)(object)value2 != (Object)null)
			{
				try
				{
					if (TryPlaceNextTo(createdSlot, value, value2, out var missing))
					{
						placedSlots.Add(instanceID);
						ModLog.LogInfo((object)("Moved the Knight V next to the " + ItemName(((LayoutItemGroup<InventoryLayoutItem>)(object)value2).ItemId)));
					}
					else
					{
						ModelProblem("Waiting to place the Knight V: " + missing);
					}
				}
				catch (Exception ex)
				{
					ModelProblem("Could not place the Knight V: " + ex.Message);
				}
			}
			try
			{
				ApplyKnightModel(createdSlot);
			}
			catch (Exception ex2)
			{
				ModelProblem("Could not build the Knight V model: " + ex2.Message);
			}
		}
	}

	private static void DumpOnInventoryOpen()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Invalid comparison between Unknown and I4
		if (!dumpedInventory && createdSlots.Count != 0)
		{
			PlayerInventory inventory = LocalPlayer.Inventory;
			if (!((Object)(object)inventory == (Object)null) && (int)inventory.CurrentView == 3)
			{
				dumpedInventory = true;
				DumpSlot(createdSlots[0], "inventory open");
			}
		}
	}

	private static void DumpSlot(GameObject slot, string when)
	{
		try
		{
			ModLog.LogInfo((object)("Inventory slot report (" + when + "):"));
			int instanceID = ((Object)slot).GetInstanceID();
			slotSources.TryGetValue(instanceID, out var value);
			slotAnchors.TryGetValue(instanceID, out var value2);
			DumpGroup("Model item", value);
			DumpGroup("Next to item", ((Object)(object)value2 != (Object)null) ? ((Component)value2).transform : null);
			DumpGroup("Knight V slot", slot.transform);
		}
		catch (Exception ex)
		{
			ModLog.LogWarning((object)("Could not describe the inventory slots: " + ex.Message));
		}
	}

	private static void ModelProblem(string message)
	{
		if (loggedProblems.Add(message))
		{
			ModLog.LogWarning((object)message);
		}
	}

	private static bool ApplyKnightModel(GameObject slot)
	{
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		if (!UseKnightModel.Value || !LoadKnightParts())
		{
			return false;
		}
		if ((Object)(object)FindNamed(slot.transform, "KnightVModel") == (Object)null)
		{
			Il2CppArrayBase<InventoryLayoutItem> componentsInChildren = slot.GetComponentsInChildren<InventoryLayoutItem>(true);
			Transform val = ((componentsInChildren != null && componentsInChildren.Length > 0 && (Object)(object)componentsInChildren[0] != (Object)null) ? ((Component)componentsInChildren[0]).transform : slot.transform);
			if (!TryMeshBounds(ChildRenderers(slot.transform), val, out var bounds, out var template))
			{
				val = slot.transform;
				if (!slotSources.TryGetValue(((Object)slot).GetInstanceID(), out var value) || (Object)(object)value == (Object)null || !TryMeshBounds(GroupRenderers(value), value, out bounds, out template))
				{
					ModelProblem("No mesh found to size the Knight V model. Slot renderers: " + slot.GetComponentsInChildren<Renderer>(true).Length + ", layout items: " + (componentsInChildren?.Length ?? 0));
					return false;
				}
			}
			BuildModel(val, bounds, template);
		}
		foreach (Renderer componentsInChild in slot.GetComponentsInChildren<Renderer>(true))
		{
			if ((Object)(object)componentsInChild != (Object)null && !componentsInChild.forceRenderingOff && !IsModelPart(((Component)componentsInChild).transform))
			{
				componentsInChild.forceRenderingOff = true;
			}
		}
		return true;
	}

	private static bool LoadKnightParts()
	{
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		if (knightPartsLoaded)
		{
			return knightParts.Count > 0;
		}
		ItemData val = ItemDatabaseManager.ItemById(ItemDatabase.KnightVId);
		if ((Object)(object)val == (Object)null)
		{
			ModelProblem("Knight V item data not found yet");
			return false;
		}
		knightPartsLoaded = true;
		knightParts = new List<ModelPart>();
		(string, Transform)[] array = new(string, Transform)[3]
		{
			("pickup", val.PickupPrefab),
			("prop", val.PropPrefab),
			("held", val.HeldPrefab)
		};
		for (int i = 0; i < array.Length; i++)
		{
			var (text, val2) = array[i];
			if ((Object)(object)val2 == (Object)null)
			{
				ModLog.LogInfo((object)("Knight V " + text + " prefab is not loaded"));
				continue;
			}
			if (ReadParts(val2, ChildRenderers(val2), visibleOnly: true, knightParts, out knightBounds))
			{
				ModLog.LogInfo((object)("Using the Knight V " + text + " prefab: " + knightParts.Count + " meshes, size " + ((object)((Bounds)(ref knightBounds)).size/*cast due to .constrained prefix*/).ToString()));
				return true;
			}
			ModLog.LogInfo((object)("Knight V " + text + " prefab has no usable mesh"));
			knightParts.Clear();
		}
		ModLog.LogWarning((object)"Found no Knight V mesh. The slot keeps the copied model.");
		return false;
	}

	private static List<Renderer> ChildRenderers(Transform root)
	{
		List<Renderer> list = new List<Renderer>();
		foreach (Renderer componentsInChild in ((Component)root).GetComponentsInChildren<Renderer>(true))
		{
			list.Add(componentsInChild);
		}
		return list;
	}

	private static List<Renderer> GroupRenderers(Transform group)
	{
		List<Renderer> result = new List<Renderer>();
		HashSet<int> seen = new HashSet<int>();
		Il2CppArrayBase<InventoryLayoutItem> componentsInChildren = ((Component)group).GetComponentsInChildren<InventoryLayoutItem>(true);
		if (componentsInChildren != null)
		{
			foreach (InventoryLayoutItem item in componentsInChildren)
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				List<Renderer> renderers = ((LayoutItem)item)._renderers;
				if (renderers != null)
				{
					for (int i = 0; i < renderers.Count; i++)
					{
						Add(renderers[i]);
					}
				}
				foreach (Renderer componentsInChild in ((Component)item).GetComponentsInChildren<Renderer>(true))
				{
					Add(componentsInChild);
				}
				if (result.Count > 0)
				{
					break;
				}
			}
		}
		if (result.Count == 0)
		{
			foreach (Renderer componentsInChild2 in ((Component)group).GetComponentsInChildren<Renderer>(true))
			{
				Add(componentsInChild2);
			}
		}
		return result;
		void Add(Renderer renderer)
		{
			if ((Object)(object)renderer != (Object)null && !IsModelPart(((Component)renderer).transform) && seen.Add(((Object)renderer).GetInstanceID()))
			{
				result.Add(renderer);
			}
		}
	}

	private static Mesh MeshOf(Renderer renderer)
	{
		SkinnedMeshRenderer val = ((Il2CppObjectBase)renderer).TryCast<SkinnedMeshRenderer>();
		if ((Object)(object)val != (Object)null)
		{
			return val.sharedMesh;
		}
		if ((Object)(object)((Il2CppObjectBase)renderer).TryCast<MeshRenderer>() == (Object)null)
		{
			return null;
		}
		MeshFilter component = ((Component)renderer).GetComponent<MeshFilter>();
		if (!((Object)(object)component != (Object)null))
		{
			return null;
		}
		return component.sharedMesh;
	}

	private static bool ReadParts(Transform root, List<Renderer> renderers, bool visibleOnly, List<ModelPart> parts, out Bounds bounds)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Invalid comparison between Unknown and I4
		//IL_0175: 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)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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_00e3: 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)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: 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_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: 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_012c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		bounds = default(Bounds);
		bool started = false;
		foreach (Renderer renderer in renderers)
		{
			if ((Object)(object)renderer == (Object)null || (int)renderer.shadowCastingMode == 3 || LowerLod.IsMatch(((Object)renderer).name) || (visibleOnly && (!renderer.enabled || !ActiveUnder(((Component)renderer).transform, root))))
			{
				continue;
			}
			Mesh val = MeshOf(renderer);
			if (!((Object)(object)val == (Object)null))
			{
				Transform transform = ((Component)renderer).transform;
				Vector3 lossyScale = root.lossyScale;
				Vector3 lossyScale2 = transform.lossyScale;
				parts.Add(new ModelPart
				{
					Name = ((Object)renderer).name,
					Mesh = val,
					Materials = renderer.sharedMaterials,
					Source = renderer,
					Position = root.InverseTransformPoint(transform.position),
					Rotation = Quaternion.Inverse(root.rotation) * transform.rotation,
					Scale = new Vector3(SafeDivide(lossyScale2.x, lossyScale.x), SafeDivide(lossyScale2.y, lossyScale.y), SafeDivide(lossyScale2.z, lossyScale.z))
				});
				EncapsulateMesh(root, transform, val, ref bounds, ref started);
				if (parts.Count >= 64)
				{
					break;
				}
			}
		}
		if (started)
		{
			Vector3 size = ((Bounds)(ref bounds)).size;
			return ((Vector3)(ref size)).sqrMagnitude > 0f;
		}
		return false;
	}

	private static bool TryMeshBounds(List<Renderer> renderers, Transform space, out Bounds bounds, out Renderer template)
	{
		//IL_0001: 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_0082: Unknown result type (might be due to invalid IL or missing references)
		bounds = default(Bounds);
		template = null;
		bool started = false;
		foreach (Renderer renderer in renderers)
		{
			if ((Object)(object)renderer == (Object)null || IsModelPart(((Component)renderer).transform))
			{
				continue;
			}
			Mesh val = MeshOf(renderer);
			if (!((Object)(object)val == (Object)null))
			{
				if ((Object)(object)template == (Object)null)
				{
					template = renderer;
				}
				EncapsulateMesh(space, ((Component)renderer).transform, val, ref bounds, ref started);
			}
		}
		if (started)
		{
			Vector3 size = ((Bounds)(ref bounds)).size;
			return ((Vector3)(ref size)).sqrMagnitude > 0f;
		}
		return false;
	}

	private static void CreatePart(Transform parent, ModelPart part, Renderer settings)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: 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_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: 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_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
		settings = (((Object)(object)settings != (Object)null) ? settings : part.Source);
		GameObject val = new GameObject(part.Name)
		{
			layer = (((Object)(object)settings != (Object)null) ? ((Component)settings).gameObject.layer : ((Component)parent).gameObject.layer)
		};
		val.transform.SetParent(parent, false);
		val.transform.localPosition = part.Position;
		val.transform.localRotation = part.Rotation;
		val.transform.localScale = part.Scale;
		val.AddComponent<MeshFilter>().sharedMesh = part.Mesh;
		MeshRenderer val2 = val.AddComponent<MeshRenderer>();
		((Renderer)val2).sharedMaterials = part.Materials;
		if (!((Object)(object)settings == (Object)null))
		{
			((Renderer)val2).shadowCastingMode = settings.shadowCastingMode;
			((Renderer)val2).receiveShadows = settings.receiveShadows;
			((Renderer)val2).renderingLayerMask = settings.renderingLayerMask;
			((Renderer)val2).lightProbeUsage = settings.lightProbeUsage;
		}
	}

	private static void DumpGroup(string label, Transform group)
	{
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)group == (Object)null)
		{
			ModLog.LogInfo((object)(label + ": none"));
			return;
		}
		Il2CppArrayBase<InventoryLayoutItem> componentsInChildren = ((Component)group).GetComponentsInChildren<InventoryLayoutItem>(true);
		ModLog.LogInfo((object)(label + ": " + PathOf(group) + " active=" + ((Component)group).gameObject.activeInHierarchy + " childRenderers=" + ((Component)group).GetComponentsInChildren<Renderer>(true).Length + " layoutItems=" + (componentsInChildren?.Length ?? 0)));
		int num = 0;
		foreach (Renderer item in GroupRenderers(group))
		{
			if (num++ >= 6)
			{
				break;
			}
			Mesh val = MeshOf(item);
			ModLog.LogInfo((object)("  renderer " + PathOf(((Component)item).transform) + " type=" + ((MemberInfo)((Object)item).GetIl2CppType()).Name + " mesh=" + (((Object)(object)val != (Object)null) ? ((Object)val).name : "none") + " enabled=" + item.enabled + " active=" + ((Component)item).gameObject.activeInHierarchy + " layer=" + ((Component)item).gameObject.layer + " forcedOff=" + item.forceRenderingOff + " pos=" + ((object)((Component)item).transform.position/*cast due to .constrained prefix*/).ToString()));
		}
	}

	private static string PathOf(Transform t)
	{
		string text = ((Object)t).name;
		Transform parent = t.parent;
		while ((Object)(object)parent != (Object)null)
		{
			text = ((Object)parent).name + "/" + text;
			parent = parent.parent;
		}
		return text;
	}

	private static void BuildModel(Transform anchor, Bounds target, Renderer template)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: 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_003f: 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_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: 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_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Expected O, but got Unknown
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: 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_0078: 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_007f: Unknown result type (might be due to invalid IL or missing references)
		Vector3 size = ((Bounds)(ref target)).size;
		Vector3 size2 = ((Bounds)(ref knightBounds)).size;
		SortAxes(size, out var thin, out var mid, out var longest);
		SortAxes(size2, out var thin2, out var mid2, out var longest2);
		Quaternion val = Quaternion.LookRotation(Axis(longest), Axis(thin)) * Quaternion.Inverse(Quaternion.LookRotation(Axis(longest2), Axis(thin2)));
		if (FlipModel.Value)
		{
			val = Quaternion.AngleAxis(180f, Axis(thin)) * val;
		}
		float num = Mathf.Min(SafeDivide(Get(size, longest), Get(size2, longest2)), SafeDivide(Get(size, mid), Get(size2, mid2)));
		num *= Mathf.Clamp(ModelScale.Value, 0.05f, 3f);
		int layer = ((Component)template).gameObject.layer;
		GameObject val2 = new GameObject("KnightVModel");
		val2.layer = layer;
		val2.transform.SetParent(anchor, false);
		val2.transform.localRotation = val;
		val2.transform.localScale = Vector3.one * num;
		val2.transform.localPosition = ((Bounds)(ref target)).center - val * (((Bounds)(ref knightBounds)).center * num);
		foreach (ModelPart knightPart in knightParts)
		{
			CreatePart(val2.transform, knightPart, template);
		}
		ModLog.LogInfo((object)("Built the Knight V model, scale " + num));
	}

	private static Transform FindNamed(Transform slot, string name)
	{
		foreach (Transform componentsInChild in ((Component)slot).GetComponentsInChildren<Transform>(true))
		{
			if ((Object)(object)componentsInChild != (Object)null && ((Object)componentsInChild).name == name)
			{
				return componentsInChild;
			}
		}
		return null;
	}

	private static bool IsModelPart(Transform t)
	{
		Transform val = t;
		while ((Object)(object)val != (Object)null)
		{
			if (((Object)val).name == "KnightVModel")
			{
				return true;
			}
			val = val.parent;
		}
		return false;
	}

	private static bool ActiveUnder(Transform t, Transform root)
	{
		Transform val = t;
		while ((Object)(object)val != (Object)null)
		{
			if (!((Component)val).gameObject.activeSelf)
			{
				return false;
			}
			if ((Object)(object)val == (Object)(object)root)
			{
				break;
			}
			val = val.parent;
		}
		return true;
	}

	private static void EncapsulateMesh(Transform space, Transform t, Mesh mesh, ref Bounds bounds, ref bool started)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: 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)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: 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_005b: 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_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: 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_0095: 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_007f: 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)
		Bounds bounds2 = mesh.bounds;
		Vector3 center = ((Bounds)(ref bounds2)).center;
		Vector3 extents = ((Bounds)(ref bounds2)).extents;
		for (int i = 0; i < 8; i++)
		{
			Vector3 val = center + new Vector3(((i & 1) == 0) ? (0f - extents.x) : extents.x, ((i & 2) == 0) ? (0f - extents.y) : extents.y, ((i & 4) == 0) ? (0f - extents.z) : extents.z);
			Vector3 val2 = space.InverseTransformPoint(t.TransformPoint(val));
			if (!started)
			{
				bounds = new Bounds(val2, Vector3.zero);
				started = true;
			}
			else
			{
				((Bounds)(ref bounds)).Encapsulate(val2);
			}
		}
	}

	private static void SortAxes(Vector3 size, out int thin, out int mid, out int longest)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		int[] array = new int[3] { 0, 1, 2 };
		Array.Sort(array, (int a, int b) => Get(size, a).CompareTo(Get(size, b)));
		thin = array[0];
		mid = array[1];
		longest = array[2];
	}

	private static float Get(Vector3 v, int axis)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		return axis switch
		{
			1 => v.y, 
			0 => v.x, 
			_ => v.z, 
		};
	}

	private static Vector3 Axis(int axis)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		return (Vector3)(axis switch
		{
			1 => Vector3.up, 
			0 => Vector3.right, 
			_ => Vector3.forward, 
		});
	}

	private static float SafeDivide(float a, float b)
	{
		if (!(Mathf.Abs(b) < 0.0001f))
		{
			return a / b;
		}
		return 1f;
	}

	private static GameObject CloneSlot(InventoryLayoutItemGroup source)
	{
		Transform parent = ((Component)source).transform.parent;
		if ((Object)(object)parent == (Object)null)
		{
			return null;
		}
		bool activeSelf = ((Component)source).gameObject.activeSelf;
		((Component)source).gameObject.SetActive(false);
		GameObject val = Object.Instantiate<GameObject>(((Component)source).gameObject, parent);
		((Component)source).gameObject.SetActive(activeSelf);
		((Object)val).name = "KnightVInventory";
		val.SetActive(false);
		InventoryLayoutItemGroup component = val.GetComponent<InventoryLayoutItemGroup>();
		if ((Object)(object)component == (Object)null)
		{
			Object.Destroy((Object)(object)val);
			return null;
		}
		((LayoutItemGroup<InventoryLayoutItem>)(object)component)._itemId = ItemDatabase.KnightVId;
		((LayoutItemGroup<InventoryLayoutItem>)(object)component)._itemData = ItemDatabaseManager.ItemById(ItemDatabase.KnightVId);
		component._alwaysShowIfOwned = true;
		return val;
	}

	private static float SlotSpan(Transform item, int axis)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: 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_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: 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_0076: 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_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: 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_00a4: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: 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_00b9: 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_00c0: 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_00c7: 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)
		Bounds val = WorldBounds(item, Il2CppArrayBase<Renderer>.op_Implicit(((Component)item).GetComponentsInChildren<Renderer>(true)));
		if (((Bounds)(ref val)).size == Vector3.zero || (Object)(object)item.parent == (Object)null)
		{
			return 0.35f;
		}
		Transform parent = item.parent;
		bool flag = false;
		Bounds val2 = default(Bounds);
		Vector3 extents = ((Bounds)(ref val)).extents;
		Vector3 center = ((Bounds)(ref val)).center;
		for (int i = 0; i < 8; i++)
		{
			Vector3 val3 = center + new Vector3(((i & 1) == 0) ? (0f - extents.x) : extents.x, ((i & 2) == 0) ? (0f - extents.y) : extents.y, ((i & 4) == 0) ? (0f - extents.z) : extents.z);
			Vector3 val4 = parent.InverseTransformPoint(val3);
			if (!flag)
			{
				((Bounds)(ref val2))..ctor(val4, Vector3.zero);
				flag = true;
			}
			else
			{
				((Bounds)(ref val2)).Encapsulate(val4);
			}
		}
		return Mathf.Max((axis == 0) ? ((Bounds)(ref val2)).size.x : ((Bounds)(ref val2)).size.y, 0.2f);
	}

	private static Bounds WorldBounds(Transform root, Renderer[] renderers)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: 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_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: 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_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		Bounds result = default(Bounds);
		bool flag = false;
		if (renderers == null)
		{
			return result;
		}
		foreach (Renderer val in renderers)
		{
			if (!((Object)(object)val == (Object)null))
			{
				if (!flag)
				{
					result = val.bounds;
					flag = true;
				}
				else
				{
					((Bounds)(ref result)).Encapsulate(val.bounds);
				}
			}
		}
		if (!flag)
		{
			((Bounds)(ref result))..ctor(root.position, Vector3.one * 0.3f);
		}
		return result;
	}
}
public class StoreKnightVBehaviour : MonoBehaviour
{
	private int settledScans;

	public StoreKnightVBehaviour(IntPtr ptr)
		: base(ptr)
	{
	}

	private void Update()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Invalid comparison between Unknown and I4
		bool isInWorld = LocalPlayer.IsInWorld;
		PlayerInventory inventory = LocalPlayer.Inventory;
		bool flag = (Object)(object)inventory != (Object)null && (int)inventory.CurrentView == 3;
		if (!isInWorld && !flag)
		{
			return;
		}
		StoreKnightVPlugin.MakeStorable();
		StoreKnightVPlugin.TryStoreFromKey();
		if (StoreKnightVPlugin.SlotCount == 0)
		{
			settledScans = 0;
		}
		if (Time.frameCount % (flag ? 15 : 90) != 0)
		{
			return;
		}
		StoreKnightVPlugin.MaintainSlots();
		if (isInWorld && settledScans <= 8)
		{
			int slotCount = StoreKnightVPlugin.SlotCount;
			StoreKnightVPlugin.TryCreateSlots();
			if (StoreKnightVPlugin.SlotCount > 0 && StoreKnightVPlugin.SlotCount == slotCount)
			{
				settledScans++;
			}
			else
			{
				settledScans = 0;
			}
		}
	}
}
[HarmonyPatch(typeof(PlayerInventory), "Sons_Save_ISaveGameSerializer_TheForest_Items_Inventory_PlayerInventory_PlayerInventorySaveData__OnSerialize")]
internal static class InventorySavePatch
{
	private static void Prefix()
	{
		StoreKnightVPlugin.MakeStorable();
	}
}
[HarmonyPatch(typeof(PlayerInventory), "Sons_Save_ISaveGameSerializer_TheForest_Items_Inventory_PlayerInventory_PlayerInventorySaveData__OnDeserialize")]
internal static class InventoryLoadPatch
{
	private static void Prefix()
	{
		StoreKnightVPlugin.MakeStorable();
	}

	private static void Postfix(PlayerInventory __instance)
	{
		try
		{
			StoreKnightVPlugin.ModLog.LogInfo((object)("Inventory loaded from save. Knight V stored: " + __instance.Owns(ItemDatabase.KnightVId)));
		}
		catch (Exception)
		{
		}
	}
}
[HarmonyPatch(typeof(PlayerInventory), "DropRightHandItem")]
internal static class DropRightHandPatch
{
	private static bool Prefix(PlayerInventory __instance)
	{
		try
		{
			return !StoreKnightVPlugin.IsStoring || !StoreKnightVPlugin.IsHoldingKnight(__instance);
		}
		catch (Exception)
		{
			return true;
		}
	}
}