Decompiled source of Hauler Overhaul v1.0.0

HaulerUnlimited.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
[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 HaulerUnlimited
{
	[BepInPlugin("com.olyvion.hauler_unlimited", "HaulerUnlimited", "1.0.0")]
	public class HaulerUnlimitedPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private readonly Harmony _harmony = new Harmony("com.olyvion.hauler_unlimited");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony.PatchAll(typeof(HaulerUnlimitedPlugin).Assembly);
			Log.LogInfo((object)"HaulerUnlimited v1.0.0 loaded.");
		}
	}
	internal static class H
	{
		private static readonly BindingFlags F = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static readonly BindingFlags SF = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

		public static readonly FieldInfo? ValueLimit = typeof(ItemValuableBox).GetField("valueLimit", F);

		public static readonly FieldInfo? TooBig = typeof(ItemValuableBox).GetField("tooBigOffenders", F);

		public static readonly FieldInfo? TooFull = typeof(ItemValuableBox).GetField("tooFullOffenders", F);

		public static readonly FieldInfo? BoxChecker = typeof(ItemValuableBox).GetField("boxChecker", F);

		public static readonly FieldInfo? ValidObjects = typeof(ItemValuableBox).GetField("validObjectsInBox", F);

		public static readonly FieldInfo? InsidePoint = typeof(ItemValuableBox).GetField("insidePoint", F);

		public static readonly MethodInfo? StateSet = typeof(ItemValuableBox).GetMethod("StateSet", F);

		public static readonly FieldInfo? CheckEnabled = typeof(PhysGrabObjectComponentInsideBoxChecker).GetField("checkForValuableTypes", F);

		public static readonly FieldInfo? LookForTypes = typeof(PhysGrabObjectComponentInsideBoxChecker).GetField("lookForValuableTypes", F);

		public static readonly FieldInfo? CachedTypes = typeof(PhysGrabObjectComponentInsideBoxChecker).GetField("cachedTypes", F);

		public static readonly FieldInfo? FoundObjects = typeof(PhysGrabObjectComponentInsideBoxChecker).GetField("foundObjects", F);

		public static readonly FieldInfo? PGO_grabbed = typeof(PhysGrabObject).GetField("grabbed", F);

		public static readonly FieldInfo? PGO_isValuable = typeof(PhysGrabObject).GetField("isValuable", F);

		public static readonly FieldInfo? GrabberInst = typeof(PhysGrabber).GetField("instance", SF);

		public static readonly FieldInfo? GrabbedPGO = typeof(PhysGrabber).GetField("grabbedPhysGrabObject", F);

		public static bool IsHauler(MonoBehaviour mb)
		{
			Transform val = ((Component)mb).transform;
			while ((Object)(object)val != (Object)null)
			{
				if (((Object)val).name.Contains("Semiscooter") || ((Object)val).name.Contains("Hauler"))
				{
					return true;
				}
				val = val.parent;
			}
			return false;
		}

		public static void ClearOffenders(ItemValuableBox b)
		{
			if (TooBig?.GetValue(b) is IList list && list.Count > 0)
			{
				list.Clear();
			}
			if (TooFull?.GetValue(b) is IList list2 && list2.Count > 0)
			{
				list2.Clear();
			}
		}

		public static void DisableFilter(PhysGrabObjectComponentInsideBoxChecker c)
		{
			CheckEnabled?.SetValue(c, false);
			if (LookForTypes?.GetValue(c) is IList list)
			{
				list.Clear();
			}
			if (CachedTypes?.GetValue(c) is IList list2)
			{
				list2.Clear();
			}
		}

		public static List<PhysGrabObject> FindNearbyUngrabbedValuables(Vector3 center, float radius)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			List<PhysGrabObject> list = new List<PhysGrabObject>();
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			ValuableObject[] array2 = array;
			bool flag = default(bool);
			foreach (ValuableObject val in array2)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				float num = Vector3.Distance(center, ((Component)val).transform.position);
				if (num > radius)
				{
					continue;
				}
				PhysGrabObject component = ((Component)val).GetComponent<PhysGrabObject>();
				if (!((Object)(object)component == (Object)null))
				{
					object obj = PGO_grabbed?.GetValue(component);
					int num2;
					if (obj is bool)
					{
						flag = (bool)obj;
						num2 = 1;
					}
					else
					{
						num2 = 0;
					}
					if (((uint)num2 & (flag ? 1u : 0u)) == 0 && !list.Contains(component))
					{
						list.Add(component);
					}
				}
			}
			return list;
		}

		public static PhysGrabObject? GetLocalGrabbedValuable()
		{
			object obj = GrabberInst?.GetValue(null);
			if (obj == null)
			{
				return null;
			}
			object? obj2 = GrabbedPGO?.GetValue(obj);
			PhysGrabObject val = (PhysGrabObject)((obj2 is PhysGrabObject) ? obj2 : null);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			if ((Object)(object)((Component)val).GetComponent<ValuableObject>() == (Object)null)
			{
				return null;
			}
			return val;
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "Start")]
	internal static class P_Start
	{
		private static void Postfix(ItemValuableBox __instance)
		{
			if (H.IsHauler((MonoBehaviour)(object)__instance))
			{
				H.ValueLimit?.SetValue(__instance, int.MaxValue);
				object? obj = H.BoxChecker?.GetValue(__instance);
				PhysGrabObjectComponentInsideBoxChecker val = (PhysGrabObjectComponentInsideBoxChecker)((obj is PhysGrabObjectComponentInsideBoxChecker) ? obj : null);
				if (val != null)
				{
					H.DisableFilter(val);
				}
				HaulerUnlimitedPlugin.Log.LogInfo((object)"[HaulerUnlimited] Start: ready.");
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabObjectComponentInsideBoxChecker), "MatchesValuableTypeFilter")]
	internal static class P_MatchesFilter
	{
		private static bool Prefix(PhysGrabObjectComponentInsideBoxChecker __instance, ref bool __result)
		{
			if (!H.IsHauler((MonoBehaviour)(object)__instance))
			{
				return true;
			}
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(PhysGrabObjectComponentInsideBoxChecker), "Start")]
	internal static class P_CheckerStart
	{
		private static void Postfix(PhysGrabObjectComponentInsideBoxChecker __instance)
		{
			if (H.IsHauler((MonoBehaviour)(object)__instance))
			{
				H.DisableFilter(__instance);
				HaulerUnlimitedPlugin.Log.LogInfo((object)"[HaulerUnlimited] Checker filter cleared.");
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabObjectComponentInsideBoxChecker), "Update")]
	internal static class P_CheckerUpdate
	{
		private static void Prefix(PhysGrabObjectComponentInsideBoxChecker __instance)
		{
			if (H.IsHauler((MonoBehaviour)(object)__instance))
			{
				H.CheckEnabled?.SetValue(__instance, false);
			}
		}

		private static void Postfix(PhysGrabObjectComponentInsideBoxChecker __instance)
		{
			//IL_005d: 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)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			if (!H.IsHauler((MonoBehaviour)(object)__instance))
			{
				return;
			}
			ItemValuableBox componentInParent = ((Component)__instance).GetComponentInParent<ItemValuableBox>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return;
			}
			object? obj = H.InsidePoint?.GetValue(componentInParent);
			Transform val = (Transform)((obj is Transform) ? obj : null);
			Vector3 center = (((Object)(object)val != (Object)null) ? val.position : ((Component)componentInParent).transform.position);
			List<PhysGrabObject> list = H.FindNearbyUngrabbedValuables(center, 3f);
			PhysGrabObject localGrabbedValuable = H.GetLocalGrabbedValuable();
			if ((Object)(object)localGrabbedValuable != (Object)null)
			{
				float num = Vector3.Distance(((Component)componentInParent).transform.position, ((Component)localGrabbedValuable).transform.position);
				if (num <= 4f && !list.Contains(localGrabbedValuable))
				{
					list.Add(localGrabbedValuable);
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			object obj2 = H.FoundObjects?.GetValue(__instance);
			object obj3 = H.ValidObjects?.GetValue(componentInParent);
			foreach (PhysGrabObject item in list)
			{
				if (obj2 is IList list2)
				{
					bool flag = false;
					foreach (object item2 in list2)
					{
						if (item2 == item)
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						list2.Add(item);
					}
				}
				if (!(obj3 is IList list3))
				{
					continue;
				}
				bool flag2 = false;
				foreach (object item3 in list3)
				{
					if (item3 == item)
					{
						flag2 = true;
						break;
					}
				}
				if (!flag2)
				{
					list3.Add(item);
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "StateIdle")]
	internal static class P_StateIdle
	{
		private static void Postfix(ItemValuableBox __instance)
		{
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			if (!H.IsHauler((MonoBehaviour)(object)__instance))
			{
				return;
			}
			object obj = H.ValidObjects?.GetValue(__instance);
			if (obj is IList list && list.Count > 0)
			{
				return;
			}
			PhysGrabObject localGrabbedValuable = H.GetLocalGrabbedValuable();
			if (!((Object)(object)localGrabbedValuable == (Object)null))
			{
				float num = Vector3.Distance(((Component)__instance).transform.position, ((Component)localGrabbedValuable).transform.position);
				if (!(num > 4f))
				{
					H.StateSet?.Invoke(__instance, new object[1] { 2 });
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "StateOpen")]
	internal static class P_StateOpen
	{
		private static List<PhysGrabObject>? _spoofed;

		private static void Prefix(ItemValuableBox __instance)
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			if (!H.IsHauler((MonoBehaviour)(object)__instance))
			{
				return;
			}
			_spoofed = new List<PhysGrabObject>();
			object? obj = H.InsidePoint?.GetValue(__instance);
			Transform val = (Transform)((obj is Transform) ? obj : null);
			Vector3 val2 = (((Object)(object)val != (Object)null) ? val.position : ((Component)__instance).transform.position);
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			ValuableObject[] array2 = array;
			bool flag = default(bool);
			foreach (ValuableObject val3 in array2)
			{
				if ((Object)(object)val3 == (Object)null)
				{
					continue;
				}
				float num = Vector3.Distance(val2, ((Component)val3).transform.position);
				if (num > 3f)
				{
					continue;
				}
				PhysGrabObject component = ((Component)val3).GetComponent<PhysGrabObject>();
				if (!((Object)(object)component == (Object)null))
				{
					object obj2 = H.PGO_grabbed?.GetValue(component);
					int num2;
					if (obj2 is bool)
					{
						flag = (bool)obj2;
						num2 = 1;
					}
					else
					{
						num2 = 0;
					}
					if (((uint)num2 & (flag ? 1u : 0u)) != 0)
					{
						H.PGO_grabbed?.SetValue(component, false);
						_spoofed.Add(component);
					}
				}
			}
			PhysGrabObject localGrabbedValuable = H.GetLocalGrabbedValuable();
			if ((Object)(object)localGrabbedValuable != (Object)null)
			{
				float num3 = Vector3.Distance(((Component)__instance).transform.position, ((Component)localGrabbedValuable).transform.position);
				if (num3 <= 4f && !_spoofed.Contains(localGrabbedValuable))
				{
					H.PGO_grabbed?.SetValue(localGrabbedValuable, false);
					_spoofed.Add(localGrabbedValuable);
				}
			}
		}

		private static void Postfix(ItemValuableBox __instance)
		{
			if (!H.IsHauler((MonoBehaviour)(object)__instance) || _spoofed == null)
			{
				return;
			}
			foreach (PhysGrabObject item in _spoofed)
			{
				H.PGO_grabbed?.SetValue(item, true);
			}
			_spoofed = null;
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "Update")]
	internal static class P_Update
	{
		private static void Postfix(ItemValuableBox __instance)
		{
			if (H.IsHauler((MonoBehaviour)(object)__instance))
			{
				H.ValueLimit?.SetValue(__instance, int.MaxValue);
				H.ClearOffenders(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "UpdateStickyMessage")]
	internal static class P_StickyMsg
	{
		private static bool Prefix(ItemValuableBox __instance)
		{
			return !H.IsHauler((MonoBehaviour)(object)__instance);
		}
	}
	[HarmonyPatch(typeof(ItemValuableBox), "ScreenMessage")]
	internal static class P_ScreenMsg
	{
		private static bool Prefix(ItemValuableBox __instance)
		{
			return !H.IsHauler((MonoBehaviour)(object)__instance);
		}
	}
	[HarmonyPatch(typeof(ItemAttributes), "Start")]
	internal static class P_ItemPrice
	{
		private static readonly FieldInfo? F_value = typeof(ItemAttributes).GetField("value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo? F_itemAssetName = typeof(ItemAttributes).GetField("itemAssetName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo? F_item = typeof(ItemAttributes).GetField("item", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private const int TARGET_PRICE = 100;

		private static void Postfix(ItemAttributes __instance)
		{
			string text = (F_itemAssetName?.GetValue(__instance) as string) ?? "";
			object obj = F_item?.GetValue(__instance);
			string text2 = ((obj != null) ? ((typeof(Item).GetField("itemName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(obj) as string) ?? "") : "");
			if ((!text.Contains("Semiscooter") && !text2.Contains("Semiscooter") && !text.Contains("Hauler") && !text2.Contains("Hauler")) || text.Contains("Small") || text2.Contains("Small"))
			{
				return;
			}
			object obj2 = F_value?.GetValue(__instance);
			if (obj2 is int)
			{
				int num = (int)obj2;
				if (true)
				{
					F_value?.SetValue(__instance, 100);
					HaulerUnlimitedPlugin.Log.LogInfo((object)$"[HaulerUnlimited] Price set: {num} -> {100} on '{text2}/{text}'");
				}
			}
		}
	}
	internal static class PluginInfo
	{
		internal const string PLUGIN_GUID = "com.olyvion.hauler_unlimited";

		internal const string PLUGIN_NAME = "HaulerUnlimited";

		internal const string PLUGIN_VERSION = "1.0.0";
	}
}