Decompiled source of QuickItem v1.0.1

Mods/QuickItem.dll

Decompiled 11 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using BoneLib.BoneMenu;
using BoneLib.Notifications;
using Il2CppSLZ.Marrow.Data;
using Il2CppSLZ.Marrow.Pool;
using Il2CppSLZ.Marrow.Warehouse;
using LabFusion.Entities;
using LabFusion.Marrow.Pool;
using LabFusion.Network;
using LabFusion.RPC;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using QuickItem;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(QuickItemMod), "QuickItem", "1.0.0", "nontendo", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("QuickItem")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("QuickItem")]
[assembly: AssemblyTitle("QuickItem")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace QuickItem
{
	public static class ItemReader
	{
		public static string? GetRightHandBarcode()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			if (!Player.HandsExist)
			{
				return null;
			}
			GameObject currentAttachedGO = Player.RightHand.m_CurrentAttachedGO;
			if ((Object)(object)currentAttachedGO == (Object)null)
			{
				return null;
			}
			Poolee componentInParent = currentAttachedGO.GetComponentInParent<Poolee>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return null;
			}
			SpawnableCrate spawnableCrate = componentInParent.SpawnableCrate;
			if ((Object)(object)spawnableCrate == (Object)null)
			{
				return null;
			}
			return ((Scannable)spawnableCrate).Barcode.ID;
		}
	}
	public class QuickItemMod : MelonMod
	{
		private static FunctionElement? _savedItemLabel;

		public static QuickItemMod Instance { get; private set; }

		public static bool FusionLoaded { get; private set; }

		public override void OnInitializeMelon()
		{
			Instance = this;
			SavedItem.Load();
			foreach (MelonMod registeredMelon in MelonTypeBase<MelonMod>.RegisteredMelons)
			{
				if (((MelonBase)registeredMelon).Info.Name == "LabFusion")
				{
					FusionLoaded = true;
					((MelonBase)this).LoggerInstance.Msg("LabFusion detected - networked spawning enabled.");
					break;
				}
			}
			Hooking.OnLevelLoaded += OnLevelLoaded;
			SetupMenu();
			((MelonBase)this).LoggerInstance.Msg("QuickItem loaded!");
		}

		private static void OnLevelLoaded(LevelInfo _)
		{
			SpawnTrigger.Reset();
		}

		private static void SetupMenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: 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_0080: Unknown result type (might be due to invalid IL or missing references)
			Page obj = Page.Root.CreatePage("QuickItem", Color.cyan, 0, true);
			_savedItemLabel = obj.CreateFunction("Saved: " + SavedItem.GetDisplayName(), Color.white, (Action)RefreshLabel);
			obj.CreateFunction("Save Held Item", Color.green, (Action)SaveHeldItem);
			obj.CreateFunction("Clear Saved Item", Color.red, (Action)ClearSavedItem);
			obj.CreateFloat("Hold Duration (s)", Color.yellow, SavedItem.HoldDuration, 0.5f, 0.5f, 10f, (Action<float>)delegate(float seconds)
			{
				SavedItem.SetHoldDuration(seconds);
			});
		}

		private static void RefreshLabel()
		{
			if (_savedItemLabel != null)
			{
				((Element)_savedItemLabel).ElementName = "Saved: " + SavedItem.GetDisplayName();
			}
		}

		private static void SaveHeldItem()
		{
			//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_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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_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_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			//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_0023: 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_0029: 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_003e: 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_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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			string rightHandBarcode = ItemReader.GetRightHandBarcode();
			if (rightHandBarcode == null)
			{
				Notifier.Send(new Notification
				{
					Title = new NotificationText
					{
						Text = "QuickItem"
					},
					Message = new NotificationText
					{
						Text = "Hold an item in your right hand first!"
					},
					Type = (NotificationType)1,
					ShowTitleOnPopup = true,
					PopupLength = 2f
				});
			}
			else
			{
				SavedItem.Set(rightHandBarcode);
				RefreshLabel();
				Notifier.Send(new Notification
				{
					Title = new NotificationText
					{
						Text = "QuickItem"
					},
					Message = new NotificationText
					{
						Text = "Saved: " + SavedItem.GetDisplayName()
					},
					Type = (NotificationType)3,
					ShowTitleOnPopup = true,
					PopupLength = 2.5f
				});
			}
		}

		private static void ClearSavedItem()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_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)
			//IL_002d: 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_0040: 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_0047: 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_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			SavedItem.Clear();
			RefreshLabel();
			Notifier.Send(new Notification
			{
				Title = new NotificationText
				{
					Text = "QuickItem"
				},
				Message = new NotificationText
				{
					Text = "Saved item cleared."
				},
				Type = (NotificationType)0,
				ShowTitleOnPopup = true,
				PopupLength = 2f
			});
		}

		public override void OnUpdate()
		{
			SpawnTrigger.OnUpdate();
		}
	}
	public static class SavedItem
	{
		private static readonly MelonPreferences_Category _prefs = MelonPreferences.CreateCategory("QuickItem");

		private static readonly MelonPreferences_Entry<string> _barcodeEntry = _prefs.CreateEntry<string>("SavedBarcode", string.Empty, "Saved Item Barcode", (string)null, false, false, (ValueValidator)null, (string)null);

		private static readonly MelonPreferences_Entry<float> _holdDurationEntry = _prefs.CreateEntry<float>("HoldDuration", 3f, "Hold Duration (seconds)", (string)null, false, false, (ValueValidator)null, (string)null);

		public static string? Barcode { get; private set; }

		public static float HoldDuration { get; private set; } = 3f;

		public static bool HasItem => Barcode != null;

		public static void Load()
		{
			string value = _barcodeEntry.Value;
			Barcode = (string.IsNullOrWhiteSpace(value) ? null : value);
			HoldDuration = _holdDurationEntry.Value;
		}

		public static void SetHoldDuration(float seconds)
		{
			HoldDuration = seconds;
			_holdDurationEntry.Value = seconds;
			_prefs.SaveToFile(false);
		}

		public static void Set(string barcode)
		{
			Barcode = barcode;
			_barcodeEntry.Value = barcode;
			_prefs.SaveToFile(false);
		}

		public static void Clear()
		{
			Barcode = null;
			_barcodeEntry.Value = string.Empty;
			_prefs.SaveToFile(false);
		}

		public static string GetDisplayName()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (Barcode == null)
			{
				return "None";
			}
			if (AssetWarehouse.ready)
			{
				Barcode val = new Barcode(Barcode);
				SpawnableCrate val2 = default(SpawnableCrate);
				if (AssetWarehouse.Instance.TryGetCrate<SpawnableCrate>(val, ref val2))
				{
					return ((Scannable)val2).Title;
				}
			}
			return Barcode;
		}
	}
	public static class SpawnTrigger
	{
		private const float TriggerDistance = 0.12f;

		private const float SpawnDistance = 0.5f;

		private const float SpawnCooldown = 2f;

		private static float _holdTimer;

		private static float _cooldownTimer;

		private static bool _wasTriggered;

		public static void Reset()
		{
			_holdTimer = 0f;
			_cooldownTimer = 0f;
			_wasTriggered = false;
		}

		public static void OnUpdate()
		{
			//IL_0037: 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)
			if (!Player.HandsExist || !SavedItem.HasItem)
			{
				return;
			}
			if (_cooldownTimer > 0f)
			{
				_cooldownTimer -= Time.deltaTime;
			}
			else if (Vector3.Distance(((Component)Player.LeftHand).transform.position, ((Component)Player.RightHand).transform.position) <= 0.12f)
			{
				_holdTimer += Time.deltaTime;
				if (!_wasTriggered && _holdTimer >= SavedItem.HoldDuration)
				{
					_wasTriggered = true;
					SpawnSavedItem();
				}
			}
			else
			{
				_holdTimer = 0f;
				_wasTriggered = false;
			}
		}

		private static void SpawnSavedItem()
		{
			//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_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_002b: 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)
			_cooldownTimer = 2f;
			string barcode = SavedItem.Barcode;
			Vector3 spawnPosition = GetSpawnPosition();
			Quaternion identity = Quaternion.identity;
			if (QuickItemMod.FusionLoaded && NetworkInfo.HasServer)
			{
				SpawnNetworked(barcode, spawnPosition, identity);
			}
			else
			{
				SpawnLocal(barcode, spawnPosition, identity);
			}
		}

		private static Vector3 GetSpawnPosition()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: 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)
			Vector3 position = ((Component)Player.LeftHand).transform.position;
			Vector3 forward = Player.Head.forward;
			forward.y = 0f;
			((Vector3)(ref forward)).Normalize();
			return position + forward * 0.5f;
		}

		private static void SpawnNetworked(string barcode, Vector3 position, Quaternion rotation)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_000e: 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_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			Spawnable spawnable = LocalAssetSpawner.CreateSpawnable(new SpawnableCrateReference(barcode));
			NetworkAssetSpawner.Spawn(new SpawnRequestInfo
			{
				Spawnable = spawnable,
				Position = position,
				Rotation = rotation,
				SpawnEffect = true,
				SpawnSource = (EntitySource)2
			});
		}

		private static void SpawnLocal(string barcode, Vector3 position, Quaternion rotation)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			LocalAssetSpawner.Spawn(LocalAssetSpawner.CreateSpawnable(new SpawnableCrateReference(barcode)), position, rotation, (Action<Poolee>)null);
		}
	}
}