Decompiled source of StoreKnightV v1.0.0

StoreKnightV.dll

Decompiled 4 hours 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.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
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.0.0")]
public class StoreKnightVPlugin : BasePlugin
{
	private class ModelPart
	{
		public string Name;

		public Mesh Mesh;

		public Il2CppReferenceArray<Material> Materials;

		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.0.0";

	internal static ConfigEntry<KeyCode> StoreKey;

	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 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_0010: 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_0031: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Expected O, but got Unknown
		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");
		UseKnightModel = val.Bind<bool>("Inventory", "UseKnightVModel", true, "Show the Knight V model in the inventory. When false, the slot looks like a second glider");
		ModelScale = val.Bind<float>("Inventory", "ModelScale", 0.9f, "Size of the Knight V model relative to the glider 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);
		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.0.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;
			}
		}
		catch (Exception ex)
		{
			Debug.LogWarning(Object.op_Implicit("StoreKnightV 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_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Invalid comparison between Unknown and I4
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Invalid comparison between Unknown and I4
		if (!Input.GetKeyDown(StoreKey.Value))
		{
			return;
		}
		PlayerInventory inventory = LocalPlayer.Inventory;
		if (!((Object)(object)inventory == (Object)null) && LocalPlayer.IsInWorld)
		{
			if ((int)inventory.CurrentView == 2)
			{
				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)
		{
			Debug.LogWarning(Object.op_Implicit("StoreKnightV 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);
			}
			result = inventory.Owns(knightVId);
			Debug.Log(Object.op_Implicit("StoreKnightV stored the Knight V. Owns=" + result));
			result = true;
		}
		catch (Exception ex)
		{
			Debug.LogWarning(Object.op_Implicit("StoreKnightV 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()
	{
		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;
		}
		InventoryLayoutItemGroup[] array2;
		if (!reportedGroups)
		{
			reportedGroups = true;
			int num = 0;
			array2 = array;
			foreach (InventoryLayoutItemGroup val in array2)
			{
				if ((Object)(object)val != (Object)null && ((LayoutItemGroup<InventoryLayoutItem>)(object)val).ItemId == ItemDatabase.HangGliderId)
				{
					num++;
				}
			}
			Debug.Log(Object.op_Implicit("StoreKnightV found " + array.Length + " inventory groups and " + num + " glider slots"));
		}
		array2 = array;
		foreach (InventoryLayoutItemGroup val2 in array2)
		{
			if ((Object)(object)val2 == (Object)null || ((LayoutItemGroup<InventoryLayoutItem>)(object)val2).ItemId != ItemDatabase.HangGliderId || HasKnightSlot(((Component)val2).transform.parent))
			{
				continue;
			}
			try
			{
				GameObject val3 = CreateSlot(val2);
				if ((Object)(object)val3 != (Object)null)
				{
					createdSlots.Add(val3);
				}
			}
			catch (Exception ex2)
			{
				Debug.LogWarning(Object.op_Implicit("StoreKnightV 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 GameObject CreateSlot(InventoryLayoutItemGroup glider)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: 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_004d: 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)
		GameObject val = CloneSlot(glider);
		if ((Object)(object)val == (Object)null)
		{
			return null;
		}
		Vector3 localPosition = ((Component)glider).transform.localPosition;
		localPosition.y += SlotSpan(((Component)glider).transform, 1);
		val.transform.localPosition = localPosition;
		val.transform.localRotation = ((Component)glider).transform.localRotation;
		val.transform.localScale = ((Component)glider).transform.localScale;
		bool flag = false;
		try
		{
			flag = ApplyKnightModel(val);
		}
		catch (Exception ex)
		{
			Debug.LogWarning(Object.op_Implicit("StoreKnightV could not build the Knight V model: " + ex.Message));
		}
		val.SetActive(true);
		Debug.Log(Object.op_Implicit(flag ? "StoreKnightV placed the Knight V model above the glider." : "StoreKnightV placed a glider copy above the glider."));
		return val;
	}

	internal static void MaintainSlots()
	{
		createdSlots.RemoveAll((GameObject slot) => (Object)(object)slot == (Object)null);
		foreach (GameObject createdSlot in createdSlots)
		{
			try
			{
				ApplyKnightModel(createdSlot);
			}
			catch (Exception)
			{
			}
		}
	}

	private static bool ApplyKnightModel(GameObject slot)
	{
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		if (!UseKnightModel.Value || !LoadKnightParts())
		{
			return false;
		}
		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 ((Object)(object)val.Find("KnightVModel") == (Object)null)
		{
			if (!TryGliderBounds(slot.transform, val, out var bounds, out var template))
			{
				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_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)
		if (knightPartsLoaded)
		{
			return knightParts.Count > 0;
		}
		ItemData val = ItemDatabaseManager.ItemById(ItemDatabase.KnightVId);
		if ((Object)(object)val == (Object)null)
		{
			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))
			{
				if (ReadParts(val2, knightParts, out knightBounds))
				{
					Debug.Log(Object.op_Implicit("StoreKnightV uses the Knight V " + text + " prefab: " + knightParts.Count + " meshes, size " + ((object)((Bounds)(ref knightBounds)).size/*cast due to .constrained prefix*/).ToString()));
					return true;
				}
				knightParts.Clear();
			}
		}
		Debug.LogWarning(Object.op_Implicit("StoreKnightV found no Knight V mesh. The slot keeps the glider model."));
		return false;
	}

	private static bool ReadParts(Transform root, List<ModelPart> parts, out Bounds bounds)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Invalid comparison between Unknown and I4
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: 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_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: 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_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: 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)
		//IL_0132: 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_013f: 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_0152: 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)
		//IL_0165: Unknown result type (might be due to invalid IL or missing references)
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		bounds = default(Bounds);
		bool started = false;
		Il2CppArrayBase<Renderer> componentsInChildren = ((Component)root).GetComponentsInChildren<Renderer>(true);
		if (componentsInChildren == null)
		{
			return false;
		}
		foreach (Renderer item in componentsInChildren)
		{
			if ((Object)(object)item == (Object)null || !item.enabled || (int)item.shadowCastingMode == 3 || LowerLod.IsMatch(((Object)item).name) || !ActiveUnder(((Component)item).transform, root))
			{
				continue;
			}
			Mesh val = null;
			SkinnedMeshRenderer val2 = ((Il2CppObjectBase)item).TryCast<SkinnedMeshRenderer>();
			if ((Object)(object)val2 != (Object)null)
			{
				val = val2.sharedMesh;
			}
			else if ((Object)(object)((Il2CppObjectBase)item).TryCast<MeshRenderer>() != (Object)null)
			{
				MeshFilter component = ((Component)item).GetComponent<MeshFilter>();
				if ((Object)(object)component != (Object)null)
				{
					val = component.sharedMesh;
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				Transform transform = ((Component)item).transform;
				Vector3 lossyScale = root.lossyScale;
				Vector3 lossyScale2 = transform.lossyScale;
				parts.Add(new ModelPart
				{
					Name = ((Object)item).name,
					Mesh = val,
					Materials = item.sharedMaterials,
					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 TryGliderBounds(Transform slot, Transform space, out Bounds bounds, out Renderer template)
	{
		//IL_0001: 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)
		bounds = default(Bounds);
		template = null;
		bool started = false;
		foreach (Renderer componentsInChild in ((Component)slot).GetComponentsInChildren<Renderer>(true))
		{
			if ((Object)(object)componentsInChild == (Object)null || IsModelPart(((Component)componentsInChild).transform))
			{
				continue;
			}
			Mesh val = null;
			SkinnedMeshRenderer val2 = ((Il2CppObjectBase)componentsInChild).TryCast<SkinnedMeshRenderer>();
			if ((Object)(object)val2 != (Object)null)
			{
				val = val2.sharedMesh;
			}
			else
			{
				MeshFilter component = ((Component)componentsInChild).GetComponent<MeshFilter>();
				if ((Object)(object)component != (Object)null)
				{
					val = component.sharedMesh;
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				if ((Object)(object)template == (Object)null)
				{
					template = componentsInChild;
				}
				EncapsulateMesh(space, ((Component)componentsInChild).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 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)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: 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)
		{
			GameObject val3 = new GameObject(knightPart.Name)
			{
				layer = layer
			};
			val3.transform.SetParent(val2.transform, false);
			val3.transform.localPosition = knightPart.Position;
			val3.transform.localRotation = knightPart.Rotation;
			val3.transform.localScale = knightPart.Scale;
			val3.AddComponent<MeshFilter>().sharedMesh = knightPart.Mesh;
			MeshRenderer obj = val3.AddComponent<MeshRenderer>();
			((Renderer)obj).sharedMaterials = knightPart.Materials;
			((Renderer)obj).shadowCastingMode = template.shadowCastingMode;
			((Renderer)obj).receiveShadows = template.receiveShadows;
			((Renderer)obj).renderingLayerMask = template.renderingLayerMask;
			((Renderer)obj).lightProbeUsage = template.lightProbeUsage;
		}
		Debug.Log(Object.op_Implicit("StoreKnightV built the Knight V model, scale " + num));
	}

	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()
	{
		if (!LocalPlayer.IsInWorld)
		{
			return;
		}
		StoreKnightVPlugin.MakeStorable();
		StoreKnightVPlugin.TryStoreFromKey();
		if (StoreKnightVPlugin.SlotCount == 0)
		{
			settledScans = 0;
		}
		if (Time.frameCount % 90 != 0)
		{
			return;
		}
		StoreKnightVPlugin.MaintainSlots();
		if (settledScans <= 8)
		{
			int slotCount = StoreKnightVPlugin.SlotCount;
			StoreKnightVPlugin.TryCreateSlots();
			if (StoreKnightVPlugin.SlotCount > 0 && StoreKnightVPlugin.SlotCount == slotCount)
			{
				settledScans++;
			}
			else
			{
				settledScans = 0;
			}
		}
	}
}
[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;
		}
	}
}