Decompiled source of CartAutoCollect v1.0.0

CartAutoCollect.dll

Decompiled 3 days ago
using System;
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.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("CartAutoCollect")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c963178d1fb51edcf5e5ea04c2d81ab578ccba77")]
[assembly: AssemblyProduct("CartAutoCollect")]
[assembly: AssemblyTitle("CartAutoCollect")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 CartAutoCollect
{
	[BepInPlugin("com.community.cartautocollect", "CartAutoCollect", "1.0.0")]
	public class CartAutoCollectPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.community.cartautocollect";

		public const string PluginName = "CartAutoCollect";

		public const string PluginVersion = "1.0.0";

		public static ConfigEntry<bool> ModEnabled;

		public static ConfigEntry<float> CollectionRadius;

		public static ConfigEntry<float> CollectionInterval;

		public static ConfigEntry<bool> OnlyMatchingItems;

		public static ConfigEntry<bool> CollectWhenPulling;

		private readonly Harmony harmony = new Harmony("com.community.cartautocollect");

		private void Awake()
		{
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ModEnabled", true, "Enable or disable CartAutoCollect.");
			CollectionRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "CollectionRadius", 8f, "Radius (in metres) around the cart to vacuum up items.");
			CollectionInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "CollectionInterval", 1f, "How often (in seconds) the cart checks for nearby items.");
			OnlyMatchingItems = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "OnlyMatchingItems", true, "If true, only collect item types already present in the cart. If false, collect everything.");
			CollectWhenPulling = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "CollectWhenPulling", true, "If true, only collect while the player is attached and pulling the cart.");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CartAutoCollect 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
		}
	}
	public class CartCollector : MonoBehaviour
	{
		private Vagon m_vagon;

		private Container m_container;

		private float m_timer;

		private void Awake()
		{
			m_vagon = ((Component)this).GetComponent<Vagon>();
			m_container = ((Component)this).GetComponentInChildren<Container>();
		}

		private void Update()
		{
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			if (!CartAutoCollectPlugin.ModEnabled.Value || (Object)(object)m_vagon == (Object)null || (Object)(object)m_container == (Object)null)
			{
				return;
			}
			m_timer += Time.deltaTime;
			if (m_timer < CartAutoCollectPlugin.CollectionInterval.Value)
			{
				return;
			}
			m_timer = 0f;
			if (CartAutoCollectPlugin.CollectWhenPulling.Value && !m_vagon.IsAttached((Character)(object)Player.m_localPlayer))
			{
				return;
			}
			Inventory inventory = m_container.GetInventory();
			if (inventory == null)
			{
				return;
			}
			HashSet<string> hashSet = new HashSet<string>();
			if (CartAutoCollectPlugin.OnlyMatchingItems.Value)
			{
				foreach (ItemData allItem in inventory.GetAllItems())
				{
					hashSet.Add(allItem.m_shared.m_name);
				}
				if (hashSet.Count == 0)
				{
					return;
				}
			}
			float value = CartAutoCollectPlugin.CollectionRadius.Value;
			Collider[] array = Physics.OverlapSphere(((Component)this).transform.position, value);
			for (int i = 0; i < array.Length; i++)
			{
				ItemDrop componentInParent = ((Component)array[i]).GetComponentInParent<ItemDrop>();
				if (!((Object)(object)componentInParent == (Object)null) && componentInParent.m_itemData != null && componentInParent.CanPickup(true))
				{
					string name = componentInParent.m_itemData.m_shared.m_name;
					if ((!CartAutoCollectPlugin.OnlyMatchingItems.Value || hashSet.Contains(name)) && inventory.CanAddItem(componentInParent.m_itemData, -1))
					{
						inventory.AddItem(componentInParent.m_itemData);
						ZNetScene.instance.Destroy(((Component)componentInParent).gameObject);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(Vagon), "Awake")]
	public static class VagonAwakePatch
	{
		private static void Postfix(Vagon __instance)
		{
			if ((Object)(object)((Component)__instance).GetComponent<CartCollector>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<CartCollector>();
			}
		}
	}
}