Decompiled source of SweepersHeaven v1.2.0

SweepersHeaven.dll

Decompiled 2 hours 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.Configuration;
using BepInEx.Logging;
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("SweepersHeaven")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0+5bea0d8430bbdb4c2e4b7969c2391068d76a4912")]
[assembly: AssemblyProduct("SweepersHeaven")]
[assembly: AssemblyTitle("SweepersHeaven")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SweepersHeaven
{
	[BepInPlugin("SweepersHeaven", "SweepersHeaven", "1.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(NPC_Info), "OnStartClient")]
		public static class NPCInfo_OnStartClient_Patch
		{
			public static void Postfix(NPC_Info __instance)
			{
				if ((Object)(object)StolenProductPrefab == (Object)null && (Object)(object)__instance.stolenProductPrefab != (Object)null)
				{
					StolenProductPrefab = __instance.stolenProductPrefab;
					Logger.LogInfo((object)"Stolen product prefab set.");
				}
			}
		}

		internal static Plugin Instance;

		internal static ManualLogSource Logger;

		private Harmony harmony;

		private ConfigEntry<float> pickupRadius;

		private ConfigEntry<int> maxItemsToPick;

		private ConfigEntry<bool> throwItemsOnPickup;

		private ConfigEntry<KeyCode> pickupKey;

		private ConfigEntry<KeyCode> spawnKey;

		private ConfigEntry<bool> isDebugMode;

		private ConfigEntry<bool> miniTransporterAutoPickup;

		internal static GameObject StolenProductPrefab;

		public float PickupRadius => pickupRadius.Value;

		public int MaxItemsToPick => maxItemsToPick.Value;

		public bool ThrowItemsOnPickup => throwItemsOnPickup.Value;

		public KeyCode PickupKey => pickupKey.Value;

		public KeyCode SpawnKey => spawnKey.Value;

		public bool IsDebugMode => isDebugMode.Value;

		public bool MiniTransporterAutoPickup => miniTransporterAutoPickup.Value;

		private void Awake()
		{
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Expected O, but got Unknown
			Instance = this;
			pickupRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Pickup Settings", "Pickup Radius", 2.5f, "Radius within which items are collected.");
			maxItemsToPick = ((BaseUnityPlugin)this).Config.Bind<int>("Pickup Settings", "Max Items to Pick", 5, "Maximum number of items picked at once.");
			throwItemsOnPickup = ((BaseUnityPlugin)this).Config.Bind<bool>("Pickup Settings", "Throw Items on Pickup", true, "If true, items are thrown when picked up with the broom.");
			pickupKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "Pickup Key", (KeyCode)323, "Key to activate broom sweeping. Default is Left Mouse Button.");
			spawnKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Debug", "Spawn Key", (KeyCode)103, "Key to spawn items around player. Only works in Debug Mode.");
			isDebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Enable Debug Mode", false, "If true, enables debug actions. Use Spawn Key to spawn items.");
			miniTransporterAutoPickup = ((BaseUnityPlugin)this).Config.Bind<bool>("Mini Transporter", "Auto Pickup", true, "If true, enables auto pickup for Mini Transporter.");
			harmony = new Harmony("SweepersHeaven");
			harmony.PatchAll();
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin SweepersHeaven is loaded!");
		}

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

		private void DropRandomProductAtPlayer()
		{
			List<Transform> randomShelfWithProducts = GetRandomShelfWithProducts();
			if (randomShelfWithProducts.Count != 0)
			{
				Data_Container component = ((Component)randomShelfWithProducts[Random.Range(0, randomShelfWithProducts.Count)]).GetComponent<Data_Container>();
				int randomProductFromShelf = GetRandomProductFromShelf(component.productInfoArray);
				if (randomProductFromShelf != -1)
				{
					component.NPCGetsItemFromRow(randomProductFromShelf);
					SpawnProductNearPlayer(randomProductFromShelf);
				}
			}
		}

		private List<Transform> GetRandomShelfWithProducts()
		{
			List<Transform> list = new List<Transform>();
			GameObject val = GameObject.Find("Level_SupermarketProps/Shelves");
			if ((Object)(object)val != (Object)null)
			{
				for (int i = 0; i < val.transform.childCount; i++)
				{
					Transform child = val.transform.GetChild(i);
					Data_Container component = ((Component)child).gameObject.GetComponent<Data_Container>();
					if ((Object)(object)component != (Object)null && ProductExistsInShelf(component.productInfoArray))
					{
						list.Add(child);
					}
				}
			}
			return list;
		}

		private int GetRandomProductFromShelf(int[] productInfoArray)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < productInfoArray.Length / 2; i++)
			{
				int num = productInfoArray[i * 2];
				int num2 = productInfoArray[i * 2 + 1];
				if (num >= 0 && num2 > 0)
				{
					list.Add(num);
				}
			}
			if (list.Count <= 0)
			{
				return -1;
			}
			return list[Random.Range(0, list.Count)];
		}

		private bool ProductExistsInShelf(int[] productInfoArray)
		{
			for (int i = 0; i < productInfoArray.Length / 2; i++)
			{
				if (productInfoArray[i * 2 + 1] > 0)
				{
					return true;
				}
			}
			return false;
		}

		private void SpawnProductNearPlayer(int productID)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			GameObject stolenProductPrefab = StolenProductPrefab;
			if (!((Object)(object)stolenProductPrefab == (Object)null))
			{
				Vector3 val = PlayerPosition() + new Vector3(Random.Range(-1.5f, 1.5f), Random.Range(0.5f, 1.5f), Random.Range(-1.5f, 1.5f));
				GameObject obj = Object.Instantiate<GameObject>(stolenProductPrefab, val, Quaternion.identity);
				obj.AddComponent<StolenProductSpawn>();
				obj.GetComponent<StolenProductSpawn>().NetworkproductID = productID;
				obj.GetComponent<StolenProductSpawn>().NetworkproductCarryingPrice = 10f;
				NetworkServer.Spawn(obj, (NetworkConnection)null);
				Logger.LogInfo((object)$"Spawned product ID {productID} at {val}");
			}
		}

		private static Vector3 PlayerPosition()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			GameObject obj = GameObject.FindWithTag("Player") ?? GameObject.Find("LocalGamePlayer");
			if (obj == null)
			{
				return Vector3.zero;
			}
			return obj.transform.position;
		}

		private void Update()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if (isDebugMode.Value && Input.GetKeyDown(spawnKey.Value))
			{
				for (int i = 0; i < 100; i++)
				{
					DropRandomProductAtPlayer();
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SweepersHeaven";

		public const string PLUGIN_NAME = "SweepersHeaven";

		public const string PLUGIN_VERSION = "1.2.0";
	}
}
namespace SweepersHeaven.Patches
{
	[HarmonyPatch(typeof(MiniTransportBehaviour), "CheckCollision")]
	internal class MiniTransport_AutoPickupPatch
	{
		private static void Postfix(MiniTransportBehaviour __instance, GameObject otherOBJ)
		{
			if (Plugin.Instance.MiniTransporterAutoPickup && !(__instance.velocity < 1f) && __instance.hasDriver && __instance.hasAuthority && otherOBJ.CompareTag("Interactable") && (Object)(object)otherOBJ.GetComponent<StolenProductSpawn>() != (Object)null)
			{
				((MonoBehaviour)__instance).StartCoroutine(PickupItemsCoroutine(__instance, otherOBJ));
			}
		}

		private static void PickupItem(MiniTransportBehaviour instance, GameObject item)
		{
			StolenProductSpawn component = item.GetComponent<StolenProductSpawn>();
			if ((Object)(object)component != (Object)null)
			{
				component.CmdRecoverStolenProduct();
			}
		}

		private static IEnumerator PickupItemsCoroutine(MiniTransportBehaviour instance, GameObject item)
		{
			PlayerSyncCharacter_PickupPatch.ApplyForceToItem(item, ((Component)instance).transform, ((Component)instance).transform.forward);
			yield return (object)new WaitForSeconds(1f);
			PickupItem(instance, item);
		}
	}
	[HarmonyPatch(typeof(PlayerSyncCharacter), "LateUpdate")]
	internal class PlayerSyncCharacter_PickupPatch
	{
		private static bool canPickup = true;

		private static int broomItemIndex = 3;

		private static int broomHitAnimationIndex = 0;

		private static void Postfix(PlayerSyncCharacter __instance)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			if (!((NetworkBehaviour)__instance).isLocalPlayer || !canPickup || __instance.pNetwork.equippedItem != broomItemIndex)
			{
				return;
			}
			if (Input.GetKeyDown(Plugin.Instance.PickupKey))
			{
				List<GameObject> list = ItemsInRange(((Component)__instance).transform);
				if (list.Count == 0)
				{
					return;
				}
				__instance.pNetwork.CmdPlayAnimation(broomHitAnimationIndex);
				((MonoBehaviour)__instance).StartCoroutine(PickupItemsCoroutine(__instance, list));
			}
			if (Input.GetMouseButtonDown(1))
			{
				List<GameObject> list2 = ItemsInRange(((Component)__instance).transform);
				if (list2.Count != 0)
				{
					__instance.pNetwork.CmdPlayAnimation(broomHitAnimationIndex);
					ThrowItems(list2, __instance);
				}
			}
		}

		private static List<GameObject> ItemsInRange(Transform playerTransform)
		{
			return FindItemsNearby(playerTransform, Plugin.Instance.PickupRadius);
		}

		private static IEnumerator PickupItemsCoroutine(PlayerSyncCharacter instance, List<GameObject> itemsInRange)
		{
			int itemsPicked = 0;
			ThrowItems(itemsInRange, instance);
			foreach (GameObject item in itemsInRange)
			{
				if (itemsPicked >= Plugin.Instance.MaxItemsToPick)
				{
					break;
				}
				ApplyForceToItem(item, ((Component)instance).transform);
				yield return (object)new WaitForSeconds(1f / (float)Mathf.Min(Plugin.Instance.MaxItemsToPick, itemsInRange.Count));
				item.GetComponent<StolenProductSpawn>().CmdRecoverStolenProduct();
				itemsPicked++;
			}
			Debug.Log((object)$"Picked up {itemsPicked} items with the broom!");
		}

		private static void ThrowItems(List<GameObject> items, PlayerSyncCharacter instance)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			((MonoBehaviour)instance).StartCoroutine(PickupDelayCoroutine(instance));
			foreach (GameObject item in items)
			{
				if (!Plugin.Instance.ThrowItemsOnPickup)
				{
					break;
				}
				ApplyForceToItem(item, ((Component)instance).transform);
			}
		}

		public static void ApplyForceToItem(GameObject item, Transform playerTransform, Vector3 direction = default(Vector3))
		{
			//IL_0021: 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_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: 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)
			//IL_0061: 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_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			Rigidbody component = item.GetComponent<Rigidbody>();
			if (!((Object)(object)component == (Object)null))
			{
				float num = Random.Range(2f, 10f);
				Vector3 val = Vector3.up + direction;
				Vector3 val2 = ((Vector3)(ref val)).normalized * num;
				val = default(Vector3);
				if (direction == val)
				{
					val = Vector3.up + -playerTransform.right;
					val2 = ((Vector3)(ref val)).normalized * num;
				}
				component.AddForce(val2, (ForceMode)1);
			}
		}

		private static IEnumerator PickupDelayCoroutine(PlayerSyncCharacter instance)
		{
			canPickup = false;
			yield return (object)new WaitForSeconds(1f);
			canPickup = true;
		}

		private static List<GameObject> FindItemsNearby(Transform playerTransform, float radius)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			Collider[] array = Physics.OverlapSphere(playerTransform.position, radius);
			List<GameObject> list = new List<GameObject>();
			Collider[] array2 = array;
			foreach (Collider val in array2)
			{
				if (((Component)val).gameObject.CompareTag("Interactable") && (Object)(object)((Component)val).gameObject.GetComponent<StolenProductSpawn>() != (Object)null)
				{
					list.Add(((Component)val).gameObject);
				}
			}
			list.Sort(delegate(GameObject a, GameObject b)
			{
				//IL_0006: 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_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_0022: 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_0032: 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_003e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0043: Unknown result type (might be due to invalid IL or missing references)
				//IL_0050: Unknown result type (might be due to invalid IL or missing references)
				//IL_0055: Unknown result type (might be due to invalid IL or missing references)
				Vector3 val2 = a.transform.position - playerTransform.position;
				Vector3 val3 = b.transform.position - playerTransform.position;
				float num = Vector3.Angle(playerTransform.forward, val2);
				float value = Vector3.Angle(playerTransform.forward, val3);
				return num.CompareTo(value);
			});
			return list;
		}
	}
}