Decompiled source of SelfMovingCart CancelTheRide v1.0.0

SelfMovingCartCancelPatch.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("SelfMovingCartCancelPatch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SelfMovingCartCancelPatch")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3d65c8c8-fdee-4c7c-9793-4d7752161859")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SelfMovingCartCancel;

[BepInPlugin("Number_79.SelfMovingCartCancel", "SelfMovingCart Cancel Order", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public static class SetPathfindingTargetPatch
	{
		public static void Prefix(object __instance)
		{
			//IL_0021: 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)
			MonoBehaviour val = (MonoBehaviour)((__instance is MonoBehaviour) ? __instance : null);
			if (!((Object)(object)val == (Object)null))
			{
				orderOrigins[__instance] = ((Component)val).transform.position;
				hasActiveOrder[__instance] = true;
				mls.LogInfo((object)$"Order origin saved: {((Component)val).transform.position}");
			}
		}
	}

	[HarmonyPatch(typeof(PlayerController), "Update")]
	public static class PlayerControllerUpdatePatch
	{
		private static readonly List<string> mouseBtns = new List<string> { "leftButton", "rightButton", "middleButton", "forwardButton", "backButton" };

		private static bool WasPressed(string btn)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				InputControl val = (mouseBtns.Contains(btn) ? ((InputControl)Mouse.current)[btn] : ((InputControl)Keyboard.current)[btn]);
				return ((ButtonControl)val).wasPressedThisFrame;
			}
			catch
			{
				return false;
			}
		}

		private static bool IsChatActive()
		{
			try
			{
				if (chatStateField == null)
				{
					return false;
				}
				return (int)chatStateField.GetValue(null) == 1;
			}
			catch
			{
				return false;
			}
		}

		[HarmonyPostfix]
		private static void Postfix(PlayerController __instance)
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			if (IsChatActive() || !WasPressed(cancelOrderKey.Value) || getCartToOrderMethod == null)
			{
				return;
			}
			object obj = getCartToOrderMethod.Invoke(null, new object[1] { ((Component)__instance).transform.position });
			if (obj != null)
			{
				Vector3 value2;
				if (!hasActiveOrder.TryGetValue(obj, out var value) || !value)
				{
					mls.LogInfo((object)"N pressed but no active order to cancel.");
				}
				else if (orderOrigins.TryGetValue(obj, out value2))
				{
					mls.LogInfo((object)$"Cancelling order, returning to {value2}");
					setPathfindingTargetMethod.Invoke(obj, new object[3] { value2, -1, false });
					hasActiveOrder[obj] = false;
				}
			}
		}
	}

	private const string modGUID = "Number_79.SelfMovingCartCancel";

	private const string modName = "SelfMovingCart Cancel Order";

	private const string modVersion = "1.0.0";

	public static ManualLogSource mls;

	public static ConfigEntry<string> cancelOrderKey;

	public static Dictionary<object, Vector3> orderOrigins = new Dictionary<object, Vector3>();

	public static Dictionary<object, bool> hasActiveOrder = new Dictionary<object, bool>();

	public static MethodInfo getCartToOrderMethod;

	public static MethodInfo setPathfindingTargetMethod;

	public static FieldInfo chatStateField;

	private void Awake()
	{
		//IL_014a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Expected O, but got Unknown
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_016e: Expected O, but got Unknown
		mls = Logger.CreateLogSource("Number_79.SelfMovingCartCancel");
		cancelOrderKey = ((BaseUnityPlugin)this).Config.Bind<string>("Controls", "CancelOrderKey", "n", "Key to cancel the current cart order and send it back to where it started.");
		Assembly assembly = null;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly2 in assemblies)
		{
			if (assembly2.GetName().Name == "SelfMovingCart")
			{
				assembly = assembly2;
				break;
			}
		}
		if (assembly == null)
		{
			mls.LogError((object)"SelfMovingCart not found!");
			return;
		}
		Type type = assembly.GetType("SelfMovingCart.Patches.CartSelfMovementManager");
		Type type2 = assembly.GetType("SelfMovingCart.Patches.PhysGrabCartPatch");
		Type type3 = assembly.GetType("SelfMovingCart.Patches.ChatManagerPatch");
		setPathfindingTargetMethod = type?.GetMethod("SetPathfindingTarget", BindingFlags.Instance | BindingFlags.Public);
		getCartToOrderMethod = type2?.GetMethod("GetCartToOrder", BindingFlags.Static | BindingFlags.Public);
		chatStateField = type3?.GetField("chatState", BindingFlags.Static | BindingFlags.Public);
		if (setPathfindingTargetMethod == null || getCartToOrderMethod == null)
		{
			mls.LogError((object)"Could not find required methods. Check SelfMovingCart version.");
			return;
		}
		Harmony val = new Harmony("Number_79.SelfMovingCartCancel");
		HarmonyMethod val2 = new HarmonyMethod(typeof(SetPathfindingTargetPatch).GetMethod("Prefix", BindingFlags.Static | BindingFlags.Public));
		val.Patch((MethodBase)setPathfindingTargetMethod, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		mls.LogInfo((object)"Patched SetPathfindingTarget successfully.");
		val.PatchAll(typeof(PlayerControllerUpdatePatch));
		mls.LogInfo((object)("SelfMovingCart Cancel Order loaded! Cancel key: [" + cancelOrderKey.Value + "]"));
	}
}