Decompiled source of DropFullStack v0.1.1

plugins\StonewardsDropFullStack.dll

Decompiled 3 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("StonewardsDropFullStack")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+ef1731f53eb7aa7bb973fcc095e0efa67244cf81")]
[assembly: AssemblyProduct("StonewardsDropFullStack")]
[assembly: AssemblyTitle("StonewardsDropFullStack")]
[assembly: AssemblyVersion("0.1.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 StonewardsDropFullStack
{
	[BepInPlugin("thereisnouser.stonewards.dropfullstack", "Stonewards Drop Full Stack", "0.1.1")]
	public sealed class DropFullStackPlugin : BaseUnityPlugin
	{
		private sealed class PendingDrop
		{
			internal string ItemId;

			internal int Count;

			internal float ExpiresAt;
		}

		public const string PluginGuid = "thereisnouser.stonewards.dropfullstack";

		public const string PluginName = "Stonewards Drop Full Stack";

		public const string PluginVersion = "0.1.1";

		private const float PendingTimeoutSeconds = 5f;

		private static readonly Dictionary<long, PendingDrop> Pending = new Dictionary<long, PendingDrop>();

		private static ConfigEntry<bool> _enabled;

		private Harmony _harmony;

		private float _nextCleanup;

		private void Awake()
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Drop the entire selected inventory stack with the normal Release key.");
			MethodInfo methodInfo = AccessTools.Method(typeof(FirstPersonController), "CmdReleaseItem", (Type[])null, (Type[])null);
			if (!HasExpectedSignature(methodInfo))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"CmdReleaseItem not found or its signature has changed. Mod disabled.");
				return;
			}
			try
			{
				_harmony = new Harmony("thereisnouser.stonewards.dropfullstack");
				_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(AccessTools.Method(typeof(DropFullStackPlugin), "BeforeRelease", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Drop Full Stack 0.1.1 active (native Release action).");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to hook native release command: " + ex));
				if (_harmony != null)
				{
					_harmony.UnpatchSelf();
					_harmony = null;
				}
			}
		}

		private static bool HasExpectedSignature(MethodInfo method)
		{
			if (method == null)
			{
				return false;
			}
			ParameterInfo[] parameters = method.GetParameters();
			if (parameters.Length == 5 && parameters[0].ParameterType == typeof(FirstPersonController) && parameters[1].ParameterType == typeof(string) && parameters[2].ParameterType == typeof(int) && parameters[3].ParameterType == typeof(int))
			{
				return parameters[4].ParameterType == typeof(Vector3);
			}
			return false;
		}

		private static bool BeforeRelease(FirstPersonController __instance, FirstPersonController __0, string __1, int __2, ref int __3)
		{
			//IL_008a: 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_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (_enabled == null || !_enabled.Value || (Object)(object)__instance == (Object)null || (!((NetworkBehaviour)__instance).isLocalPlayer && ((Object)(object)__0 == (Object)null || !((NetworkBehaviour)__0).isLocalPlayer)) || __3 <= 0 || string.IsNullOrEmpty(__1))
				{
					return true;
				}
				FirstPersonController val = (((Object)(object)__0 != (Object)null) ? __0 : __instance);
				InventorySystem inventorySystem = val.InventorySystem;
				if ((Object)(object)inventorySystem == (Object)null || __2 < 0 || __2 >= inventorySystem.InventoryEntries.Count)
				{
					return true;
				}
				InventoryEntry val2 = inventorySystem.InventoryEntries[__2];
				if (!string.Equals(val2.itemID, __1, StringComparison.Ordinal))
				{
					return true;
				}
				int count = val2.count;
				if (count <= 0 || __3 >= count)
				{
					return true;
				}
				long key = (long)(((ulong)(uint)((Object)val).GetInstanceID() << 32) | (uint)__2);
				float unscaledTime = Time.unscaledTime;
				if (Pending.TryGetValue(key, out var value))
				{
					if (unscaledTime < value.ExpiresAt && value.Count == count && string.Equals(value.ItemId, __1, StringComparison.Ordinal))
					{
						return false;
					}
					Pending.Remove(key);
				}
				__3 = count;
				Pending[key] = new PendingDrop
				{
					ItemId = __1,
					Count = count,
					ExpiresAt = unscaledTime + 5f
				};
				return true;
			}
			catch (Exception)
			{
				return true;
			}
		}

		private void Update()
		{
			if (Time.unscaledTime < _nextCleanup)
			{
				return;
			}
			_nextCleanup = Time.unscaledTime + 10f;
			if (Pending.Count == 0)
			{
				return;
			}
			List<long> list = new List<long>();
			foreach (KeyValuePair<long, PendingDrop> item in Pending)
			{
				if (Time.unscaledTime >= item.Value.ExpiresAt)
				{
					list.Add(item.Key);
				}
			}
			foreach (long item2 in list)
			{
				Pending.Remove(item2);
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Pending.Clear();
		}
	}
}