Decompiled source of AutoLoot v1.0.0

BepInEx/plugins/AutoLoot/Plugin.dll

Decompiled 2 days 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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Fusion;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SecureVariables;
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("Plugin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Plugin")]
[assembly: AssemblyTitle("Plugin")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
[BepInPlugin("com.ic23.autoloot", "AutoLoot", "1.0.0")]
public class AutoLootPlugin : BaseUnityPlugin
{
	internal static class Logg
	{
		internal const string Prefix = "[AutoLoot] ";

		internal static readonly ManualLogSource Logger = Logger.CreateLogSource("Magnifier");

		internal static void Debug(object data)
		{
			Logger.Log((LogLevel)32, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}

		internal static void Info(object data)
		{
			Logger.Log((LogLevel)16, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}

		internal static void Message(object data)
		{
			Logger.Log((LogLevel)16, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}

		internal static void Warning(object data)
		{
			Logger.Log((LogLevel)4, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}

		internal static void Error(object data)
		{
			Logger.Log((LogLevel)2, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}

		internal static void Fatal(object data)
		{
			Logger.Log((LogLevel)1, (object)("[AutoLoot] " + (data?.ToString() ?? "null")));
		}
	}

	public static ConfigEntry<float> CollectRadiusConfig;

	public static float CollectRadius => CollectRadiusConfig.Value;

	private void Awake()
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		CollectRadiusConfig = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CollectRadius", 2000f, "Item collection radius in meters");
		new Harmony("com.ic23.autoloot").PatchAll();
		Logg.Info($"Plugin loading is completed. CollectRadius: {CollectRadius}");
	}
}
[HarmonyPatch(typeof(PlayerInventory), "SendLootToTruck")]
public static class PlayerInventory_SendLootToTruck_SkillBook_Patch
{
	private static void Prefix(PlayerInventory __instance)
	{
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: 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_00de: Unknown result type (might be due to invalid IL or missing references)
		FPP_Player player = __instance.player;
		if ((Object)(object)player == (Object)null || (Object)(object)((SimulationBehaviour)player).Runner == (Object)null)
		{
			return;
		}
		GameObject val = GameObject.FindGameObjectWithTag("SpawnLootTruck");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		for (int i = 0; i < __instance.itemSlots.Count; i++)
		{
			ItemSlot val2 = __instance.itemSlots[i];
			if ((Object)(object)val2.assignedItem == (Object)null || !val2.assignedItem.isSkillBook || val2.assignedItem.isLoot)
			{
				continue;
			}
			while ((Object)(object)val2.assignedItem != (Object)null && val2.amount > 0)
			{
				NetworkObject val3 = ((SimulationBehaviour)player).Runner.Spawn(val2.assignedItem.itemDropPrefab, (Vector3?)val.transform.position, (Quaternion?)Quaternion.identity, (PlayerRef?)null, (OnBeforeSpawned)null, (NetworkSpawnFlags)0);
				if ((Object)(object)val3 != (Object)null)
				{
					Transform transform = ((Component)val3).transform;
					transform.eulerAngles += val2.assignedItem.placementExtraRotation;
					__instance.SetDroppedValues(((Component)val3).GetComponent<Pickupable>(), val2.ReturnCurrentDamage(), val2.ReturnCurrentProperty());
				}
				val2.RemoveFromSlot();
			}
		}
	}
}
[HarmonyPatch(typeof(PlayerInventory), "SendLootToTruck")]
public static class PlayerInventory_SendLootToTruck_Patch
{
	private static void Postfix(PlayerInventory __instance)
	{
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: 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)
		FPP_Player player = __instance.player;
		if ((Object)(object)player == (Object)null || (Object)(object)((SimulationBehaviour)player).Runner == (Object)null)
		{
			return;
		}
		WeaponManager val = null;
		try
		{
			val = __instance.weaponManager;
		}
		catch
		{
			object? obj = AccessTools.Field(typeof(PlayerInventory), "weaponManager")?.GetValue(__instance);
			val = (WeaponManager)((obj is WeaponManager) ? obj : null);
		}
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Item carriedItem = val.carriedItem;
		if ((Object)(object)carriedItem == (Object)null || !carriedItem.isLoot)
		{
			return;
		}
		GameObject val2 = GameObject.FindGameObjectWithTag("SpawnLootTruck");
		if ((Object)(object)val2 == (Object)null)
		{
			return;
		}
		NetworkObject val3 = ((SimulationBehaviour)player).Runner.Spawn(carriedItem.itemDropPrefab, (Vector3?)val2.transform.position, (Quaternion?)Quaternion.identity, (PlayerRef?)null, (OnBeforeSpawned)null, (NetworkSpawnFlags)0);
		if ((Object)(object)val3 != (Object)null)
		{
			Transform transform = ((Component)val3).transform;
			transform.eulerAngles += carriedItem.placementExtraRotation;
			Pickupable component = ((Component)val3).GetComponent<Pickupable>();
			if ((Object)(object)component != (Object)null)
			{
				__instance.SetDroppedValues(component, val.carriedItemDamage, val.carriedItemCustomProperty);
				component.SpawnMarker();
			}
		}
		val.carriedItemDamage = 0f;
		val.carriedItemCustomProperty = "";
		val.carriedItem = null;
		val.isCarrying = false;
		val.currentLootItem = "";
		__instance.ActivateCurrentSlot();
	}
}
public static class AutoLootCollector
{
	[CompilerGenerated]
	private sealed class <CollectItemsCoroutine>d__3 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		public FPP_Player player;

		private NetworkObject <playerNetworkObject>5__2;

		private float <requiredValue>5__3;

		private int <collectedCount>5__4;

		private List<Pickupable>.Enumerator <>7__wrap4;

		private Pickupable <pickupable>5__6;

		private NetworkObject <pickupableNetObj>5__7;

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

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

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

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			int num = <>1__state;
			if ((uint)(num - -4) <= 1u || (uint)(num - 1) <= 2u)
			{
				try
				{
					if (num == -4 || (uint)(num - 1) <= 1u)
					{
						try
						{
						}
						finally
						{
							<>m__Finally2();
						}
					}
				}
				finally
				{
					<>m__Finally1();
				}
			}
			<playerNetworkObject>5__2 = null;
			<>7__wrap4 = default(List<Pickupable>.Enumerator);
			<pickupable>5__6 = null;
			<pickupableNetObj>5__7 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_040e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0418: Expected O, but got Unknown
			//IL_035a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0364: Expected O, but got Unknown
			//IL_03b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0399: Unknown result type (might be due to invalid IL or missing references)
			//IL_0464: Unknown result type (might be due to invalid IL or missing references)
			//IL_046e: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			bool result;
			try
			{
				switch (<>1__state)
				{
				default:
					result = false;
					break;
				case 0:
				{
					<>1__state = -1;
					isCollecting = true;
					<>1__state = -3;
					Vector3 position = ((Component)player).transform.position;
					<playerNetworkObject>5__2 = ((Component)player).GetComponent<NetworkObject>();
					if ((Object)(object)<playerNetworkObject>5__2 == (Object)null)
					{
						AutoLootPlugin.Logg.Error("NetworkObject not found on player");
						result = false;
						<>m__Finally1();
						break;
					}
					<requiredValue>5__3 = 0f;
					try
					{
						<requiredValue>5__3 = player.WM.quotaManager.requiredQuota - player.WM.quotaManager.currentQuota;
						AutoLootPlugin.Logg.Info($"Required quota: {<requiredValue>5__3}");
					}
					catch (Exception ex4)
					{
						AutoLootPlugin.Logg.Warning("Could not get requiredQuota: " + ex4.Message);
					}
					Pickupable[] array = Object.FindObjectsOfType<Pickupable>();
					List<Pickupable> list = new List<Pickupable>();
					Pickupable[] array2 = array;
					foreach (Pickupable val in array2)
					{
						if (!((Object)(object)val == (Object)null) && !(Vector3.Distance(position, ((Component)val).transform.position) > AutoLootPlugin.CollectRadius) && !((Object)(object)val.assignedItem == (Object)null) && (val.assignedItem.isLoot || val.assignedItem.isSkillBook) && !val.blockPickup && (!val.assignedItem.requireSkillToCarry || player.skillManager.CheckIfSkillLearned(val.assignedItem.skillIDToCarry)))
						{
							list.Add(val);
						}
					}
					AutoLootPlugin.Logg.Info($"Found {list.Count} items to collect");
					list.Sort((Pickupable a, Pickupable b) => ((SecureInt)(ref b.assignedItem.value)).CompareTo(a.assignedItem.value));
					List<Pickupable> collection = list.FindAll((Pickupable p) => p.assignedItem.isSkillBook);
					List<Pickupable> collection2 = list.FindAll((Pickupable p) => !p.assignedItem.isSkillBook);
					list.Clear();
					list.AddRange(collection);
					list.AddRange(collection2);
					<collectedCount>5__4 = 0;
					<>7__wrap4 = list.GetEnumerator();
					<>1__state = -4;
					goto IL_043c;
				}
				case 1:
					<>1__state = -4;
					try
					{
						if (player.CheckIfItemNearbyOtherPlayers(<pickupable>5__6))
						{
							<pickupable>5__6.RPC_RequestPickup(<playerNetworkObject>5__2.Id);
						}
						else
						{
							player.RPC_PickUp(<pickupableNetObj>5__7.Id);
						}
						<collectedCount>5__4++;
						AutoLootPlugin.Logg.Debug("Picked up: " + <pickupable>5__6.assignedItem.itemName);
					}
					catch (Exception ex3)
					{
						AutoLootPlugin.Logg.Error("Error RPC_PickUp: " + ex3.Message);
					}
					<>2__current = (object)new WaitForSeconds(0.15f);
					<>1__state = 2;
					result = true;
					break;
				case 2:
					<>1__state = -4;
					<pickupableNetObj>5__7 = null;
					<pickupable>5__6 = null;
					goto IL_043c;
				case 3:
					{
						<>1__state = -3;
						try
						{
							player.inventory.SendLootToTruck();
						}
						catch (Exception ex)
						{
							AutoLootPlugin.Logg.Error("Error final SendLootToTruck: " + ex.Message);
						}
						float num = Pinboard.Instance.collectedLootValue;
						AutoLootPlugin.Logg.Info($"Collected {<collectedCount>5__4} items. Total value: {num} / Required: {<requiredValue>5__3}");
						<playerNetworkObject>5__2 = null;
						<>m__Finally1();
						result = false;
						break;
					}
					IL_043c:
					while (true)
					{
						if (<>7__wrap4.MoveNext())
						{
							<pickupable>5__6 = <>7__wrap4.Current;
							if (<requiredValue>5__3 > 0f && !<pickupable>5__6.assignedItem.isSkillBook)
							{
								float num2 = Pinboard.Instance.collectedLootValue;
								if (num2 >= <requiredValue>5__3)
								{
									AutoLootPlugin.Logg.Info($"Quota reached! Collected: {num2} / Required: {<requiredValue>5__3}");
									goto IL_044c;
								}
							}
							if ((Object)(object)<pickupable>5__6 == (Object)null || <pickupable>5__6.blockPickup)
							{
								continue;
							}
							<pickupableNetObj>5__7 = ((Component)<pickupable>5__6).GetComponent<NetworkObject>();
							if (!((Object)(object)<pickupableNetObj>5__7 == (Object)null))
							{
								bool flag = false;
								try
								{
									player.inventory.SendLootToTruck();
									flag = true;
								}
								catch (Exception ex2)
								{
									AutoLootPlugin.Logg.Error("Error SendLootToTruck: " + ex2.Message);
								}
								if (flag)
								{
									<>2__current = (object)new WaitForSeconds(0.05f);
									<>1__state = 1;
									result = true;
									break;
								}
							}
							continue;
						}
						goto IL_044c;
						IL_044c:
						<>m__Finally2();
						<>7__wrap4 = default(List<Pickupable>.Enumerator);
						<>2__current = (object)new WaitForSeconds(0.1f);
						<>1__state = 3;
						result = true;
						break;
					}
					break;
				}
			}
			catch
			{
				//try-fault
				((IDisposable)this).Dispose();
				throw;
			}
			return result;
		}

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

		private void <>m__Finally1()
		{
			<>1__state = -1;
			isCollecting = false;
		}

		private void <>m__Finally2()
		{
			<>1__state = -3;
			((IDisposable)<>7__wrap4).Dispose();
		}

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

	private static bool isCollecting;

	private static FPP_Player lastPlayer;

	public static void CollectAllNearbyItems(FPP_Player player)
	{
		if ((Object)(object)lastPlayer == (Object)null || (Object)(object)lastPlayer != (Object)(object)player)
		{
			if (isCollecting)
			{
				AutoLootPlugin.Logg.Info("Previous collection was interrupted, resetting flag");
			}
			isCollecting = false;
		}
		lastPlayer = player;
		if (isCollecting)
		{
			AutoLootPlugin.Logg.Warning("Collection already in progress");
		}
		else if ((Object)(object)player == (Object)null || (Object)(object)((SimulationBehaviour)player).Runner == (Object)null)
		{
			AutoLootPlugin.Logg.Warning("Player or Runner is null");
		}
		else if ((Object)(object)GameObject.FindGameObjectWithTag("SpawnLootTruck") == (Object)null)
		{
			AutoLootPlugin.Logg.Warning("Truck not found!");
		}
		else
		{
			((MonoBehaviour)player).StartCoroutine(CollectItemsCoroutine(player));
		}
	}

	[IteratorStateMachine(typeof(<CollectItemsCoroutine>d__3))]
	private static IEnumerator CollectItemsCoroutine(FPP_Player player)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <CollectItemsCoroutine>d__3(0)
		{
			player = player
		};
	}
}
[HarmonyPatch(typeof(ThermalVision), "Update")]
internal class ThermalVision_Update_Patch
{
	private static void Postfix(ThermalVision __instance)
	{
		if (!((Object)(object)__instance.player == (Object)null) && Input.GetKeyDown((KeyCode)110))
		{
			AutoLootCollector.CollectAllNearbyItems(__instance.player);
		}
	}
}