Decompiled source of SelfMovingCartExtras v1.1.0

plugins/SelfMovingCartHostToggle.dll

Decompiled 31 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SelfMovingCartHostToggle;

[BepInPlugin("local.selfmovingcart.hosttoggle", "SelfMovingCartExtras", "1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	private class Cargo
	{
		public PhysGrabObject pgo;

		public Rigidbody rb;

		public Vector3 localPos;

		public Quaternion localRot;

		public LayerMask oldMask;
	}

	private class Flight
	{
		public MonoBehaviour manager;

		public Rigidbody rb;

		public PhysGrabObject pgo;

		public LayerMask oldMask;

		public List<Vector3> points;

		public int index;

		public Vector3 target;

		public int extractionInd;

		public bool isExtracting;

		public float deadline;

		public List<Cargo> cargo;
	}

	private class StuckState
	{
		public float nextCheck;

		public float bestDistance;
	}

	private const float LandDistance = 2.5f;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<string> ToggleKey;

	internal static ConfigEntry<bool> FlightEnabled;

	internal static ConfigEntry<float> FlightMinDistance;

	internal static ConfigEntry<bool> FlyWhenStuck;

	internal static ConfigEntry<float> StuckSeconds;

	internal static ConfigEntry<float> FlightSpeed;

	internal static ConfigEntry<float> FlightHeight;

	internal static ConfigEntry<bool> MapArrow;

	internal static ManualLogSource Log;

	private static bool patched;

	private static string message;

	private static float messageTimer;

	private float markerScanTimer;

	private static Type managerType;

	private static FieldInfo fRb;

	private static FieldInfo fPathCorners;

	private static FieldInfo fFinal;

	private static FieldInfo fFollowing;

	private static FieldInfo fPulled;

	private static FieldInfo fExtInd;

	private static FieldInfo fIsExt;

	private static FieldInfo fSyncCart;

	private static MethodInfo mSetTarget;

	private static MethodInfo mStop;

	private static FieldInfo fItemsInCart;

	private static readonly Dictionary<int, Flight> flights = new Dictionary<int, Flight>();

	private static readonly Dictionary<int, StuckState> stuck = new Dictionary<int, StuckState>();

	private static bool suppressFlight;

	private static int forceFlightId;

	private void Awake()
	{
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Expected O, but got Unknown
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Expected O, but got Unknown
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Expected O, but got Unknown
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Expected O, but got Unknown
		//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Host", "CartOrdersEnabled", true, "Self Moving Cart be/ki. Csak a host beállítása számít: kikapcsolva a kocsik semmilyen parancsra nem mozdulnak.");
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<string>("Host", "ToggleKey", "f9", "Gyorsbillentyű a Self Moving Cart ki-be kapcsolásához játék közben (csak host). Üresen hagyva nincs gyorsbillentyű.");
		FlightEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Flight", "FlightEnabled", true, "A kocsi repül, ha messzire küldik/hívják, vagy elakad (csak a host beállítása számít).");
		FlightMinDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Flight", "FlightMinDistance", 15f, new ConfigDescription("Ennél hosszabb útvonalon (méter) a kocsi repül. 0 = mindig repül.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), new object[0]));
		FlyWhenStuck = ((BaseUnityPlugin)this).Config.Bind<bool>("Flight", "FlyWhenStuck", true, "Ha a kocsi útközben elakad (lépcső, akadály), felszáll és repülve folytatja.");
		StuckSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Flight", "StuckSeconds", 2.5f, new ConfigDescription("Ennyi másodperc haladás nélkül számít elakadásnak.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), new object[0]));
		FlightSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Flight", "FlightSpeed", 9f, new ConfigDescription("Repülési sebesség (méter/másodperc).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2f, 30f), new object[0]));
		FlightHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Flight", "FlightHeight", 1.3f, new ConfigDescription("Milyen magasan lebeg a kocsi a padló fölött (méter).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 3f), new object[0]));
		MapArrow = ((BaseUnityPlugin)this).Config.Bind<bool>("Map", "MapArrow", true, "A térkép szélén narancs jelölő mutatja, merre van a kocsi, ha kívül esik a térképen.");
		Enabled.SettingChanged += OnEnabledChanged;
		managerType = AccessTools.TypeByName("SelfMovingCart.Patches.CartSelfMovementManager");
		Type type = AccessTools.TypeByName("SelfMovingCart.Patches.CartTargetSync");
		fItemsInCart = AccessTools.Field(typeof(PhysGrabCart), "itemsInCart");
		if (type == null || managerType == null)
		{
			Log.LogWarning((object)"Syntaxe-SelfMovingCart not found, only the map marker is active.");
			return;
		}
		fRb = AccessTools.Field(managerType, "rb");
		fPathCorners = AccessTools.Field(managerType, "pathCorners");
		fFinal = AccessTools.Field(managerType, "finalDestination");
		fFollowing = AccessTools.Field(managerType, "isCartFollowingPath");
		fPulled = AccessTools.Field(managerType, "isCartBeingPulled");
		fExtInd = AccessTools.Field(managerType, "extractionPointInd");
		fIsExt = AccessTools.Field(managerType, "isExtracting");
		fSyncCart = AccessTools.Field(type, "cart");
		mSetTarget = AccessTools.Method(managerType, "SetPathfindingTarget", (Type[])null, (Type[])null);
		mStop = AccessTools.Method(managerType, "StopPathfinding", (Type[])null, (Type[])null);
		Harmony val = new Harmony("local.selfmovingcart.hosttoggle");
		val.Patch((MethodBase)AccessTools.Method(type, "GoToTargetRPC", (Type[])null, (Type[])null), Hm("BlockWhenDisabled"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(type, "MoveCartRPC", (Type[])null, (Type[])null), Hm("MoveCartPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(managerType, "FixedUpdate", (Type[])null, (Type[])null), Hm("ManagerFixedUpdatePrefix"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)mSetTarget, (HarmonyMethod)null, Hm("SetTargetPostfix"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		patched = true;
		Log.LogInfo((object)("Self Moving Cart extras ready. Enabled = " + Enabled.Value + ", flight = " + FlightEnabled.Value + ", key = " + ToggleKey.Value));
	}

	private static HarmonyMethod Hm(string name)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Expected O, but got Unknown
		return new HarmonyMethod(typeof(Plugin).GetMethod(name, BindingFlags.Static | BindingFlags.NonPublic));
	}

	private static bool IsHost()
	{
		try
		{
			return SemiFunc.IsMasterClientOrSingleplayer();
		}
		catch (Exception)
		{
			return true;
		}
	}

	private static bool BlockWhenDisabled()
	{
		return Enabled.Value;
	}

	private static bool MoveCartPrefix(MonoBehaviour __instance, bool moveForward, bool moveBack, bool turnRight, bool turnLeft)
	{
		if (!Enabled.Value)
		{
			return false;
		}
		if (moveForward || moveBack || turnRight || turnLeft)
		{
			object? value = fSyncCart.GetValue(__instance);
			MonoBehaviour val = (MonoBehaviour)((value is MonoBehaviour) ? value : null);
			if ((Object)(object)val != (Object)null && flights.TryGetValue(((Object)val).GetInstanceID(), out var value2))
			{
				EndFlight(value2, 2);
			}
		}
		return true;
	}

	private static void OnEnabledChanged(object sender, EventArgs e)
	{
		if (!Enabled.Value)
		{
			EndAllFlights();
			StopAllCarts();
		}
		string text = (Enabled.Value ? "BEKAPCSOLVA" : "KIKAPCSOLVA");
		Show(IsHost() ? ("Önjáró kocsi: " + text) : ("Önjáró kocsi: " + text + " (csak hostként számít)"));
		Log.LogInfo((object)("Self Moving Cart " + (Enabled.Value ? "enabled" : "disabled")));
	}

	private static void StopAllCarts()
	{
		try
		{
			Type type = AccessTools.TypeByName("SelfMovingCart.Patches.PhysGrabCartPatch");
			FieldInfo fieldInfo = ((type == null) ? null : AccessTools.Field(type, "carts"));
			IEnumerable enumerable = ((fieldInfo == null) ? null : (fieldInfo.GetValue(null) as IEnumerable));
			if (enumerable == null)
			{
				return;
			}
			foreach (object item in enumerable)
			{
				Object val = (Object)((item is Object) ? item : null);
				if (!(val == (Object)null))
				{
					Traverse val2 = Traverse.Create(item);
					val2.Method("StopPathfinding", new object[0]).GetValue();
					val2.Field("remoteControlForward").SetValue((object)false);
					val2.Field("remoteControlBack").SetValue((object)false);
					val2.Field("remoteControlRight").SetValue((object)false);
					val2.Field("remoteControlLeft").SetValue((object)false);
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Could not stop carts: " + ex.Message));
		}
	}

	private static bool ManagerFixedUpdatePrefix(MonoBehaviour __instance)
	{
		int instanceID = ((Object)__instance).GetInstanceID();
		Flight value;
		bool flag = flights.TryGetValue(instanceID, out value);
		if (!Enabled.Value)
		{
			if (flag)
			{
				EndFlight(value, 1);
			}
			return false;
		}
		if (flag)
		{
			try
			{
				StepFlight(value);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Flight step failed: " + ex.Message));
				EndFlight(value, 1);
			}
			return false;
		}
		if (FlightEnabled.Value && FlyWhenStuck.Value && IsHost())
		{
			try
			{
				CheckStuck(__instance, instanceID);
			}
			catch (Exception)
			{
			}
		}
		return true;
	}

	private static void SetTargetPostfix(MonoBehaviour __instance, Vector3 target, int _extractionPointInd, bool _isExtracting)
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		int instanceID = ((Object)__instance).GetInstanceID();
		if (!suppressFlight && flights.TryGetValue(instanceID, out var value))
		{
			EndFlight(value, 2);
		}
		stuck.Remove(instanceID);
		if (suppressFlight || !FlightEnabled.Value || !IsHost())
		{
			return;
		}
		bool flag = forceFlightId == instanceID;
		try
		{
			object? value2 = fRb.GetValue(__instance);
			Rigidbody val = (Rigidbody)((value2 is Rigidbody) ? value2 : null);
			if (!((Object)(object)val == (Object)null))
			{
				List<Vector3> points = BuildPoints(__instance, val, target);
				float num = PathLength(val.position, points, 0);
				if ((flag || !(num < FlightMinDistance.Value)) && !(num <= 3f))
				{
					StartFlight(__instance, val, points, num, target, _extractionPointInd, _isExtracting);
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Could not start flight: " + ex.Message));
		}
	}

	private static List<Vector3> BuildPoints(MonoBehaviour manager, Rigidbody rb, Vector3 target)
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: 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)
		//IL_009d: 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_003d: 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_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: 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_0063: Unknown result type (might be due to invalid IL or missing references)
		List<Vector3> list = fPathCorners.GetValue(manager) as List<Vector3>;
		List<Vector3> list2 = new List<Vector3>();
		if (list != null && list.Count > 0)
		{
			list2.Add(list[0]);
			for (int i = 1; i < list.Count; i++)
			{
				Vector3 val = list2[list2.Count - 1];
				Vector3 val2 = list[i] - val;
				if (((Vector3)(ref val2)).sqrMagnitude > 0.25f)
				{
					list2.Add(list[i]);
				}
			}
		}
		else
		{
			list2.Add(rb.position - Vector3.up * 0.5f);
			list2.Add(target);
		}
		return list2;
	}

	private static float PathLength(Vector3 from, List<Vector3> points, int startIndex)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		float num = 0f;
		Vector3 val = from;
		for (int i = startIndex; i < points.Count; i++)
		{
			num += Vector3.Distance(val, points[i]);
			val = points[i];
		}
		return num;
	}

	private static void StartFlight(MonoBehaviour manager, Rigidbody rb, List<Vector3> points, float length, Vector3 target, int extractionInd, bool isExtracting)
	{
		//IL_0022: 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_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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_0207: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
		Flight flight = new Flight();
		flight.manager = manager;
		flight.rb = rb;
		flight.pgo = ((Component)manager).GetComponent<PhysGrabObject>();
		flight.oldMask = rb.excludeLayers;
		flight.points = points;
		flight.index = 0;
		flight.target = target;
		flight.extractionInd = extractionInd;
		flight.isExtracting = isExtracting;
		flight.deadline = Time.time + length / Mathf.Max(1f, FlightSpeed.Value) * 2f + 6f;
		flight.cargo = new List<Cargo>();
		PhysGrabCart component = ((Component)manager).GetComponent<PhysGrabCart>();
		IList list = (((Object)(object)component != (Object)null && fItemsInCart != null) ? (fItemsInCart.GetValue(component) as IList) : null);
		Quaternion val = Quaternion.Inverse(rb.rotation);
		if (list != null)
		{
			foreach (object item in list)
			{
				PhysGrabObject val2 = (PhysGrabObject)((item is PhysGrabObject) ? item : null);
				if ((Object)(object)val2 == (Object)null)
				{
					Component val3 = (Component)((item is Component) ? item : null);
					if ((Object)(object)val3 != (Object)null)
					{
						val2 = val3.GetComponent<PhysGrabObject>();
					}
				}
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.rb == (Object)null) && !val2.grabbed && !((Object)(object)((Component)val2).gameObject == (Object)(object)((Component)manager).gameObject))
				{
					Cargo cargo = new Cargo();
					cargo.pgo = val2;
					cargo.rb = val2.rb;
					cargo.localPos = val * (val2.rb.position - rb.position);
					cargo.localRot = val * val2.rb.rotation;
					cargo.oldMask = val2.rb.excludeLayers;
					val2.rb.excludeLayers = LayerMask.op_Implicit(-1);
					flight.cargo.Add(cargo);
				}
			}
		}
		rb.excludeLayers = LayerMask.op_Implicit(-1);
		flights[((Object)manager).GetInstanceID()] = flight;
		Log.LogInfo((object)("Cart takes off: " + length.ToString("F0") + " m, cargo " + flight.cargo.Count));
	}

	private static void StepFlight(Flight f)
	{
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: 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)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_016e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0173: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0212: Unknown result type (might be due to invalid IL or missing references)
		//IL_0217: Unknown result type (might be due to invalid IL or missing references)
		//IL_0220: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: Unknown result type (might be due to invalid IL or missing references)
		//IL_022d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_030e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0312: Unknown result type (might be due to invalid IL or missing references)
		//IL_0317: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)f.rb == (Object)null || (Object)(object)f.manager == (Object)null)
		{
			flights.Remove((!((Object)(object)f.manager == (Object)null)) ? ((Object)f.manager).GetInstanceID() : 0);
			return;
		}
		if ((bool)fPulled.GetValue(f.manager) || ((Object)(object)f.pgo != (Object)null && f.pgo.grabbed))
		{
			EndFlight(f, 1);
			return;
		}
		if (Time.time > f.deadline)
		{
			EndFlight(f, 0);
			return;
		}
		float fixedDeltaTime = Time.fixedDeltaTime;
		Vector3 position = f.rb.position;
		int num = f.points.Count - 1;
		if (PathLength(position, f.points, f.index) <= 2.5f)
		{
			EndFlight(f, 0);
			return;
		}
		float num2 = ((f.index >= num) ? 0.6f : FlightHeight.Value);
		Vector3 val = f.points[f.index] + Vector3.up * num2;
		Vector3 val2 = val - position;
		float magnitude = ((Vector3)(ref val2)).magnitude;
		float num3 = FlightSpeed.Value * fixedDeltaTime;
		Vector3 val3;
		if (magnitude <= num3)
		{
			val3 = val;
			if (f.index >= num)
			{
				EndFlight(f, 0);
				return;
			}
			f.index++;
		}
		else
		{
			val3 = position + val2 / magnitude * num3;
		}
		Vector3 linearVelocity = (val3 - position) / fixedDeltaTime;
		f.rb.linearVelocity = linearVelocity;
		f.rb.angularVelocity = Vector3.zero;
		Vector3 val4 = default(Vector3);
		((Vector3)(ref val4))..ctor(linearVelocity.x, 0f, linearVelocity.z);
		Quaternion rotation = f.rb.rotation;
		Quaternion val5 = Quaternion.Euler(0f, ((Quaternion)(ref rotation)).eulerAngles.y, 0f);
		if (((Vector3)(ref val4)).sqrMagnitude > 0.04f)
		{
			val5 = Quaternion.LookRotation(((Vector3)(ref val4)).normalized, Vector3.up);
		}
		Quaternion val6 = Quaternion.RotateTowards(f.rb.rotation, val5, 240f * fixedDeltaTime);
		f.rb.MoveRotation(val6);
		if ((Object)(object)f.pgo != (Object)null)
		{
			f.pgo.OverrideZeroGravity(0.2f);
		}
		for (int num4 = f.cargo.Count - 1; num4 >= 0; num4--)
		{
			Cargo cargo = f.cargo[num4];
			if ((Object)(object)cargo.rb == (Object)null || (Object)(object)cargo.pgo == (Object)null || cargo.pgo.grabbed)
			{
				RestoreCargo(cargo);
				f.cargo.RemoveAt(num4);
			}
			else
			{
				Vector3 val7 = val3 + val6 * cargo.localPos;
				cargo.rb.linearVelocity = (val7 - cargo.rb.position) / fixedDeltaTime;
				cargo.rb.angularVelocity = Vector3.zero;
				cargo.rb.MoveRotation(val6 * cargo.localRot);
				cargo.pgo.OverrideZeroGravity(0.2f);
				cargo.pgo.OverrideIndestructible(0.5f);
			}
		}
	}

	private static void RestoreCargo(Cargo c)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)c.rb != (Object)null)
		{
			c.rb.excludeLayers = c.oldMask;
		}
	}

	private static void EndFlight(Flight f, int mode)
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		int key = (((Object)(object)f.manager != (Object)null) ? ((Object)f.manager).GetInstanceID() : 0);
		flights.Remove(key);
		stuck.Remove(key);
		if ((Object)(object)f.rb != (Object)null)
		{
			f.rb.excludeLayers = f.oldMask;
			f.rb.linearVelocity = Vector3.zero;
			f.rb.angularVelocity = Vector3.zero;
		}
		foreach (Cargo item in f.cargo)
		{
			RestoreCargo(item);
			if ((Object)(object)item.rb != (Object)null)
			{
				item.rb.linearVelocity = Vector3.zero;
				item.rb.angularVelocity = Vector3.zero;
			}
			if ((Object)(object)item.pgo != (Object)null)
			{
				item.pgo.OverrideIndestructible(2f);
			}
		}
		if ((Object)(object)f.manager == (Object)null)
		{
			return;
		}
		try
		{
			switch (mode)
			{
			case 0:
				suppressFlight = true;
				mSetTarget.Invoke(f.manager, new object[3] { f.target, f.extractionInd, f.isExtracting });
				break;
			case 1:
				mStop.Invoke(f.manager, null);
				break;
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Landing hand-over failed: " + ex.Message));
		}
		finally
		{
			suppressFlight = false;
		}
	}

	private static void EndAllFlights()
	{
		List<Flight> list = new List<Flight>(flights.Values);
		foreach (Flight item in list)
		{
			EndFlight(item, 1);
		}
	}

	private static void CheckStuck(MonoBehaviour manager, int id)
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: 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_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		bool flag = (bool)fFollowing.GetValue(manager);
		bool flag2 = (bool)fPulled.GetValue(manager);
		if (!flag || flag2)
		{
			stuck.Remove(id);
			return;
		}
		object? value = fRb.GetValue(manager);
		Rigidbody val = (Rigidbody)((value is Rigidbody) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Vector3 val2 = (Vector3)fFinal.GetValue(manager);
		float num = Vector3.Distance(val.position, val2);
		if (!stuck.TryGetValue(id, out var value2))
		{
			value2 = new StuckState();
			value2.nextCheck = Time.time + StuckSeconds.Value;
			value2.bestDistance = num;
			stuck[id] = value2;
		}
		else
		{
			if (Time.time < value2.nextCheck)
			{
				return;
			}
			if (!(value2.bestDistance - num >= 1f) && !(num < 4f))
			{
				stuck.Remove(id);
				int num2 = (int)fExtInd.GetValue(manager);
				bool flag3 = (bool)fIsExt.GetValue(manager);
				forceFlightId = id;
				try
				{
					mSetTarget.Invoke(manager, new object[3] { val2, num2, flag3 });
					return;
				}
				finally
				{
					forceFlightId = 0;
				}
			}
			value2.bestDistance = Mathf.Min(value2.bestDistance, num);
			value2.nextCheck = Time.time + StuckSeconds.Value;
		}
	}

	private static void Show(string text)
	{
		message = text;
		messageTimer = 2.5f;
	}

	private static bool ChatOpen()
	{
		try
		{
			if ((Object)(object)ChatManager.instance == (Object)null)
			{
				return false;
			}
			return Traverse.Create((object)ChatManager.instance).Field("chatActive").GetValue<bool>();
		}
		catch (Exception)
		{
			return false;
		}
	}

	private void Update()
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		if (patched)
		{
			HandleKey();
		}
		if (messageTimer > 0f)
		{
			messageTimer -= Time.deltaTime;
			try
			{
				SemiFunc.UIFocusText(message, Color.white, new Color(1f, 0.55f, 0f), 0.2f);
			}
			catch (Exception)
			{
				messageTimer = 0f;
			}
		}
		markerScanTimer -= Time.deltaTime;
		if (!(markerScanTimer <= 0f))
		{
			return;
		}
		markerScanTimer = 1f;
		if (MapArrow.Value)
		{
			try
			{
				CartMapMarker.AttachToCarts();
			}
			catch (Exception ex2)
			{
				Log.LogDebug((object)("Map marker scan: " + ex2.Message));
			}
		}
	}

	private static void HandleKey()
	{
		string value = ToggleKey.Value;
		if (string.IsNullOrEmpty(value))
		{
			return;
		}
		Keyboard current = Keyboard.current;
		if (current == null)
		{
			return;
		}
		ButtonControl val;
		try
		{
			InputControl obj = ((InputControl)current)[value.Trim()];
			val = (ButtonControl)(object)((obj is ButtonControl) ? obj : null);
		}
		catch (Exception)
		{
			return;
		}
		if (val != null && val.wasPressedThisFrame && !ChatOpen())
		{
			if (!IsHost())
			{
				Show("Önjáró kocsi: csak a host kapcsolhatja");
			}
			else
			{
				Enabled.Value = !Enabled.Value;
			}
		}
	}
}
public class CartMapMarker : MonoBehaviour
{
	private const string AnchorName = "SMCX_MapAnchor";

	private static FieldInfo fEntity = AccessTools.Field(typeof(MapCustom), "mapCustomEntity");

	private static Camera mapCamera;

	private static Sprite generatedSprite;

	public MapCustom mapCustom;

	private Transform cart;

	public static void AttachToCarts()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Map.Instance == (Object)null)
		{
			return;
		}
		PhysGrabCart[] array = Object.FindObjectsByType<PhysGrabCart>((FindObjectsSortMode)0);
		PhysGrabCart[] array2 = array;
		foreach (PhysGrabCart val in array2)
		{
			if (!((Object)(object)val == (Object)null) && !val.isSmallCart && !((Object)(object)((Component)val).transform.Find("SMCX_MapAnchor") != (Object)null))
			{
				GameObject val2 = new GameObject("SMCX_MapAnchor");
				val2.transform.SetParent(((Component)val).transform, false);
				MapCustom val3 = val2.AddComponent<MapCustom>();
				val3.autoAdd = true;
				val3.sprite = PickSprite(val);
				val3.color = new Color(1f, 0.55f, 0f, 0f);
				CartMapMarker cartMapMarker = val2.AddComponent<CartMapMarker>();
				cartMapMarker.mapCustom = val3;
				cartMapMarker.cart = ((Component)val).transform;
			}
		}
	}

	private static Sprite PickSprite(PhysGrabCart cart)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Expected O, but got Unknown
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		MapCustom[] componentsInChildren = ((Component)cart).GetComponentsInChildren<MapCustom>(true);
		foreach (MapCustom val in componentsInChildren)
		{
			if ((Object)(object)val != (Object)null && (Object)(object)val.sprite != (Object)null && ((Object)((Component)val).gameObject).name != "SMCX_MapAnchor")
			{
				return val.sprite;
			}
		}
		if ((Object)(object)generatedSprite == (Object)null)
		{
			Texture2D val2 = new Texture2D(32, 32, (TextureFormat)4, false);
			((Texture)val2).filterMode = (FilterMode)1;
			Color32[] array = (Color32[])(object)new Color32[1024];
			float num = 15.5f;
			for (int j = 0; j < 32; j++)
			{
				for (int k = 0; k < 32; k++)
				{
					float num2 = Mathf.Sqrt(((float)k - num) * ((float)k - num) + ((float)j - num) * ((float)j - num));
					byte b = (byte)((num2 <= 14.5f) ? 255u : 0u);
					byte b2 = (byte)((num2 <= 10.5f) ? 255u : 90u);
					ref Color32 reference = ref array[j * 32 + k];
					reference = new Color32(b2, b2, b2, b);
				}
			}
			val2.SetPixels32(array);
			val2.Apply();
			generatedSprite = Sprite.Create(val2, new Rect(0f, 0f, 32f, 32f), new Vector2(0.5f, 0.5f), 160f);
		}
		return generatedSprite;
	}

	private static Camera FindMapCamera(int layer)
	{
		if ((Object)(object)mapCamera != (Object)null && ((Behaviour)mapCamera).isActiveAndEnabled)
		{
			return mapCamera;
		}
		Camera[] allCameras = Camera.allCameras;
		foreach (Camera val in allCameras)
		{
			if (!((Object)(object)val == (Object)null) && val.orthographic && !((Object)(object)val == (Object)(object)Camera.main) && (val.cullingMask & (1 << layer)) != 0)
			{
				mapCamera = val;
				return val;
			}
		}
		return mapCamera;
	}

	private void LateUpdate()
	{
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_016e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)mapCustom == (Object)null || fEntity == null)
		{
			return;
		}
		object? value = fEntity.GetValue(mapCustom);
		MapCustomEntity val = (MapCustomEntity)((value is MapCustomEntity) ? value : null);
		if ((Object)(object)val == (Object)null || (Object)(object)val.spriteRenderer == (Object)null)
		{
			return;
		}
		Map instance = Map.Instance;
		if (!Plugin.MapArrow.Value || (Object)(object)instance == (Object)null || (Object)(object)instance.playerTransformSource == (Object)null || (Object)(object)instance.OverLayerParent == (Object)null)
		{
			Hide(val);
			return;
		}
		Camera val2 = FindMapCamera(((Component)val).gameObject.layer);
		if ((Object)(object)val2 == (Object)null)
		{
			Hide(val);
			return;
		}
		float num = val2.orthographicSize / Mathf.Max(0.001f, instance.Scale) * 0.85f;
		Vector3 position = instance.playerTransformSource.position;
		Vector3 val3 = (((Object)(object)cart != (Object)null) ? cart.position : ((Component)this).transform.position);
		float num2 = val3.x - position.x;
		float num3 = val3.z - position.z;
		float num4 = Mathf.Sqrt(num2 * num2 + num3 * num3);
		if (num4 <= num)
		{
			Hide(val);
			return;
		}
		Vector3 val4 = default(Vector3);
		((Vector3)(ref val4))..ctor(position.x + num2 * num / num4, val3.y, position.z + num3 * num / num4);
		((Component)val).transform.position = val4 * instance.Scale + instance.OverLayerParent.position;
		Vector3 localPosition = ((Component)val).transform.localPosition;
		((Component)val).transform.localPosition = new Vector3(localPosition.x, 0f, localPosition.z);
		float num5 = 0.55f + 0.45f * Mathf.Abs(Mathf.Sin(Time.time * 3f));
		val.spriteRenderer.color = new Color(1f, 0.55f, 0f, num5);
	}

	private static void Hide(MapCustomEntity entity)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: 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)
		Color color = entity.spriteRenderer.color;
		if (color.a != 0f)
		{
			entity.spriteRenderer.color = new Color(color.r, color.g, color.b, 0f);
		}
	}
}