Decompiled source of StickyBackpack v0.1.0

plugins/com.github.mitchellcairns.StaticBackpack.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.mitchellcairns.StaticBackpack")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("com.github.mitchellcairns.StaticBackpack")]
[assembly: AssemblyTitle("StaticBackpack")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace StaticBackpack
{
	[BepInPlugin("com.github.mitchellcairns.StaticBackpack", "StaticBackpack", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private const string HarmonyID = "com.mitchellcairns.backpackhook";

		public const string Id = "com.github.mitchellcairns.StaticBackpack";

		internal static ManualLogSource Log { get; private set; }

		public static string Name => "StaticBackpack";

		public static string Version => "0.1.0";

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("com.mitchellcairns.backpackhook");
			_harmony.UnpatchSelf();
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony.PatchAll();
			Log.LogInfo((object)("Plugin " + Name + " is loaded!"));
		}
	}
	public class BackpackCollisionMonitor : MonoBehaviour
	{
		[CompilerGenerated]
		private sealed class <DelayedFreezeIfIntersecting>d__6 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public BackpackCollisionMonitor <>4__this;

			public float checkRadius;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <DelayedFreezeIfIntersecting>d__6(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_005e: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
				//IL_0105: 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_0140: 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)
				int num = <>1__state;
				BackpackCollisionMonitor backpackCollisionMonitor = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = null;
					<>1__state = 1;
					return true;
				case 1:
				{
					<>1__state = -1;
					if ((Object)(object)backpackCollisionMonitor._rb == (Object)null || (Object)(object)backpackCollisionMonitor._backpack == (Object)null)
					{
						return false;
					}
					Collider[] array = Physics.OverlapSphere(((Component)backpackCollisionMonitor._backpack).transform.position, checkRadius);
					Collider[] array2 = array;
					foreach (Collider val in array2)
					{
						if (!((Object)(object)val == (Object)null) && !((Object)(object)((Component)val).gameObject == (Object)(object)((Component)backpackCollisionMonitor._backpack).gameObject))
						{
							int layer = ((Component)val).gameObject.layer;
							if (layer == 20 || layer == 21)
							{
								backpackCollisionMonitor._hasStopped = true;
								backpackCollisionMonitor._rb.velocity = Vector3.zero;
								backpackCollisionMonitor._rb.angularVelocity = Vector3.zero;
								backpackCollisionMonitor._rb.constraints = (RigidbodyConstraints)126;
								backpackCollisionMonitor._rb.position = backpackCollisionMonitor._rb.position;
								backpackCollisionMonitor._rb.rotation = backpackCollisionMonitor._rb.rotation;
								((Component)((MonoBehaviourPun)backpackCollisionMonitor._backpack).photonView).transform.position = ((Component)backpackCollisionMonitor._backpack).transform.position;
								((Component)((MonoBehaviourPun)backpackCollisionMonitor._backpack).photonView).transform.rotation = ((Component)backpackCollisionMonitor._backpack).transform.rotation;
								Plugin.Log.LogWarning((object)("[BackpackHook] Backpack '" + ((Object)backpackCollisionMonitor._backpack).name + "' intersected with " + ((Object)((Component)val).gameObject).name + " — frozen in place."));
								return false;
							}
						}
					}
					return false;
				}
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private Backpack _backpack;

		private Rigidbody _rb;

		private bool _hasStopped;

		public void Init(Backpack backpack)
		{
			_backpack = backpack;
			_rb = ((Component)backpack).GetComponent<Rigidbody>();
		}

		private void OnCollisionEnter(Collision collision)
		{
			//IL_0078: 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_00ab: 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_00e6: 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)
			if (_hasStopped || (Object)(object)((Item)_backpack).trueHolderCharacter != (Object)null)
			{
				return;
			}
			int layer = collision.gameObject.layer;
			Plugin.Log.LogInfo((object)$"[BackpackHook] Backpack collided with '{((Object)collision.gameObject).name}' (layer {layer})");
			if (layer == 20 || layer == 21)
			{
				_hasStopped = true;
				if ((Object)(object)_rb != (Object)null)
				{
					_rb.velocity = Vector3.zero;
					_rb.angularVelocity = Vector3.zero;
					_rb.constraints = (RigidbodyConstraints)126;
					_rb.position = _rb.position;
					_rb.rotation = _rb.rotation;
					((Component)((MonoBehaviourPun)_backpack).photonView).transform.position = ((Component)_backpack).transform.position;
					((Component)((MonoBehaviourPun)_backpack).photonView).transform.rotation = ((Component)_backpack).transform.rotation;
				}
				Plugin.Log.LogInfo((object)("[BackpackHook] Backpack '" + ((Object)_backpack).name + "' stopped on terrain."));
			}
		}

		public void TryFreezeIfIntersecting(float checkRadius = 0.3f)
		{
			((MonoBehaviour)this).StartCoroutine(DelayedFreezeIfIntersecting(checkRadius));
		}

		[IteratorStateMachine(typeof(<DelayedFreezeIfIntersecting>d__6))]
		private IEnumerator DelayedFreezeIfIntersecting(float checkRadius)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <DelayedFreezeIfIntersecting>d__6(0)
			{
				<>4__this = this,
				checkRadius = checkRadius
			};
		}
	}
	internal class StaticBackpackPatches
	{
		[HarmonyPatch(typeof(Backpack), "Update")]
		private class Patch_Backpack_Update
		{
			private static void Postfix(Backpack __instance)
			{
				//IL_0022: 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_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_003a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_005c: Unknown result type (might be due to invalid IL or missing references)
				if (!LastStates.TryGetValue(__instance, out var value))
				{
					LastStates[__instance] = ((Item)__instance).itemState;
				}
				else if (((Item)__instance).itemState != value)
				{
					Plugin.Log.LogInfo((object)$"[BackpackHook] Backpack '{((Object)__instance).name}' state changed: {value} → {((Item)__instance).itemState}");
					LastStates[__instance] = ((Item)__instance).itemState;
				}
			}
		}

		[HarmonyPatch(typeof(Backpack), "AddPhysics")]
		private class Patch_Backpack_AddPhysics
		{
			private static void Postfix(Backpack __instance)
			{
				if (!((MonoBehaviourPun)__instance).photonView.IsMine)
				{
					Plugin.Log.LogInfo((object)("[BackpackHook] Skipping non-owned backpack '" + ((Object)__instance).name + "'"));
					return;
				}
				BackpackCollisionMonitor backpackCollisionMonitor = ((Component)__instance).gameObject.GetComponent<BackpackCollisionMonitor>();
				if ((Object)(object)backpackCollisionMonitor == (Object)null)
				{
					backpackCollisionMonitor = ((Component)__instance).gameObject.AddComponent<BackpackCollisionMonitor>();
					backpackCollisionMonitor.Init(__instance);
				}
				if ((Object)(object)((Item)__instance).trueHolderCharacter == (Object)null)
				{
					backpackCollisionMonitor.TryFreezeIfIntersecting();
				}
			}
		}

		private static readonly Dictionary<Backpack, ItemState> LastStates = new Dictionary<Backpack, ItemState>();
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}