Decompiled source of RealisticWinch v1.1.0

RealisticWinch.dll

Decompiled 18 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using Il2Cpp;
using Il2CppFusion;
using Il2CppFusion.Addons.Physics;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppObi;
using Il2CppSystem;
using Il2CppSystem.Reflection;
using JoofOTHFramework;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using RealisticWinch;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Mod), "RealisticWinch", "1.1.0", "Joof", null)]
[assembly: MelonGame("Funselektor", "over the hill")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("RealisticWinch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c946dfb108af8052e4eb36320ab82a769f2e3b68")]
[assembly: AssemblyProduct("RealisticWinch")]
[assembly: AssemblyTitle("RealisticWinch")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 RealisticWinch
{
	public class Mod : MelonMod
	{
		internal const string Channel = "RealisticWinch";

		internal static Instance Log;

		internal static MelonPreferences_Entry<bool> Wrap;

		internal static MelonPreferences_Entry<float> Friction;

		internal static MelonPreferences_Entry<bool> Visual;

		private static readonly Dictionary<IntPtr, RopeWrap.StockState> stock = new Dictionary<IntPtr, RopeWrap.StockState>();

		private GameObject host;

		public override void OnInitializeMelon()
		{
			Log = ((MelonBase)this).LoggerInstance;
			MelonPreferences_Category obj = MelonPreferences.CreateCategory("RealisticWinch");
			Wrap = obj.CreateEntry<bool>("Wrap", true, (string)null, "The winch rope bends around rocks, trees and terrain instead of passing through them, like a rope over a pulley. Lets two winched trucks hang off each other over an arch. In multiplayer the host needs RealisticWinch too.", false, false, (ValueValidator)null, (string)null);
			Friction = obj.CreateEntry<float>("Friction", 0.3f, (string)null, "Friction where the rope bends over something (0 = frictionless pulley). Higher = the far side feels less of the pull.", false, false, (ValueValidator)null, (string)null);
			Visual = obj.CreateEntry<bool>("Visual", true, (string)null, "Draw the winch rope following its bends and lying on the ground when slack, instead of the game's rope.", false, false, (ValueValidator)null, (string)null);
			HillNet.Register<object>("RealisticWinch", (Action<NetworkRunner, PlayerRef, object>)delegate
			{
			});
			WinchHooks.BeforeTick += delegate(VehicleWinchSystem w)
			{
				RopeWrap.StockState stockState = RopeWrap.BeforeStock(w);
				if (stockState != null)
				{
					stock[((Il2CppObjectBase)w).Pointer] = stockState;
				}
			};
			WinchHooks.AfterTick += delegate(VehicleWinchSystem w)
			{
				if (stock.Remove(((Il2CppObjectBase)w).Pointer, out var value))
				{
					RopeWrap.AfterStock(w, value);
				}
			};
		}

		public override void OnUpdate()
		{
			//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_0033: Expected O, but got Unknown
			HillNet.Tick();
			if (!((Object)(object)host != (Object)null) && !Core.Quitting)
			{
				host = new GameObject("RealisticWinch")
				{
					hideFlags = (HideFlags)61
				};
				Object.DontDestroyOnLoad((Object)(object)host);
				host.AddComponent<RopeWrap>();
			}
		}
	}
	public struct Hit
	{
		public Vector3 Point;

		public Vector3 Normal;

		public Hit(Vector3 point, Vector3 normal)
		{
			Point = point;
			Normal = normal;
		}
	}
	public interface IObstacles
	{
		bool Raycast(Vector3 origin, Vector3 direction, float maxDistance, out Hit hit);

		bool Overlaps(Vector3 point, float radius);

		float? GroundBelow(Vector3 from, float depth);
	}
	public class RopePath
	{
		public readonly List<Vector3> Points = new List<Vector3>();

		public readonly List<Vector3> Into = new List<Vector3>();
	}
	public readonly struct Tension
	{
		public readonly float Impulse;

		public readonly float ShareA;

		public readonly float ShareB;

		public static readonly Tension None = new Tension(0f, 0f, 0f);

		public Tension(float impulse, float shareA, float shareB)
		{
			Impulse = impulse;
			ShareA = shareA;
			ShareB = shareB;
		}
	}
	public static class RopeMath
	{
		public const float Radius = 0.1f;

		public const float EndMargin = 0.3f;

		public const float WrapMargin = 0.2f;

		public const int MaxWraps = 32;

		public static float PathLength(Vector3 a, IReadOnlyList<Vector3> points, Vector3 b)
		{
			float num = 0f;
			Vector3 value = a;
			foreach (Vector3 point in points)
			{
				num += Vector3.Distance(value, point);
				value = point;
			}
			return num + Vector3.Distance(value, b);
		}

		public static float BendAngle(Vector3 a, IReadOnlyList<Vector3> points, Vector3 b)
		{
			float num = 0f;
			Vector3 vector = a;
			for (int i = 0; i < points.Count; i++)
			{
				Vector3 vector2 = ((i + 1 < points.Count) ? points[i + 1] : b);
				num += Angle(points[i] - vector, vector2 - points[i]);
				vector = points[i];
			}
			return num;
		}

		public static float Angle(Vector3 u, Vector3 v)
		{
			float num = u.Length();
			float num2 = v.Length();
			if (num < 1E-06f || num2 < 1E-06f)
			{
				return 0f;
			}
			return MathF.Acos(Math.Clamp(Vector3.Dot(u, v) / (num * num2), -1f, 1f));
		}

		private static Vector3 Normalized(Vector3 v)
		{
			float num = v.Length();
			if (!(num > 1E-06f))
			{
				return Vector3.Zero;
			}
			return v / num;
		}

		public static bool Blocked(IObstacles world, Vector3 p, Vector3 q, bool trimStart, bool trimEnd, out Hit hit)
		{
			hit = default(Hit);
			Vector3 vector = q - p;
			float num = vector.Length();
			float num2 = (trimStart ? 0.3f : 0.2f);
			float num3 = num - (trimEnd ? 0.3f : 0.2f);
			if (num3 - num2 < 0.05f)
			{
				return false;
			}
			Vector3 vector2 = vector / num;
			return world.Raycast(p + vector2 * num2, vector2, num3 - num2, out hit);
		}

		public static Vector3 Bend(Vector3 prev, Vector3 point, Vector3 next)
		{
			return Normalized(prev - point) + Normalized(next - point);
		}

		public static bool PressesIn(Vector3 prev, Vector3 point, Vector3 next, Vector3 into)
		{
			return Vector3.Dot(Bend(prev, point, next), into) > 0f;
		}

		public static void UpdateWraps(IObstacles world, RopePath rope, Vector3 a, Vector3 b, Vector3 prevA, Vector3 prevB, bool hasPrev)
		{
			List<Vector3> points = rope.Points;
			List<Vector3> list = rope.Into;
			Hit hit;
			for (int num = points.Count - 1; num >= 0; num--)
			{
				Vector3 vector = ((num == 0) ? a : points[num - 1]);
				Vector3 vector2 = ((num == points.Count - 1) ? b : points[num + 1]);
				if (!PressesIn(vector, points[num], vector2, list[num]) && !Blocked(world, vector, vector2, num == 0, num == points.Count - 1, out hit))
				{
					points.RemoveAt(num);
					list.RemoveAt(num);
				}
			}
			int num2 = 0;
			for (int i = 0; i <= points.Count; i++)
			{
				if (num2 >= 4)
				{
					break;
				}
				if (points.Count >= 32)
				{
					break;
				}
				bool flag = i == 0;
				bool flag2 = i == points.Count;
				Vector3 vector3 = (flag ? a : points[i - 1]);
				Vector3 vector4 = (flag2 ? b : points[i]);
				if (Blocked(world, vector3, vector4, flag, flag2, out hit))
				{
					Vector3 p = ((flag && hasPrev) ? prevA : vector3);
					Vector3 q = ((flag2 && hasPrev) ? prevB : vector4);
					if (FindWrap(world, p, q, vector3, vector4, flag, flag2, out var point, out var normal) && !(Vector3.DistanceSquared(point, vector3) < 0.040000003f) && !(Vector3.DistanceSquared(point, vector4) < 0.040000003f))
					{
						Vector3 v = Bend(vector3, point, vector4);
						points.Insert(i, point);
						list.Insert(i, (v.LengthSquared() > 1E-08f) ? Normalized(v) : (-normal));
						num2++;
						i--;
					}
				}
			}
		}

		public static bool FindWrap(IObstacles world, Vector3 p0, Vector3 q0, Vector3 p1, Vector3 q1, bool fromCar, bool toEnd, out Vector3 point, out Vector3 normal)
		{
			point = (normal = default(Vector3));
			Hit hit2;
			if (!Blocked(world, p0, q0, fromCar, toEnd, out var hit))
			{
				float num = 0f;
				float num2 = 1f;
				for (int i = 0; i < 10; i++)
				{
					float num3 = (num + num2) * 0.5f;
					if (Blocked(world, Vector3.Lerp(p0, p1, num3), Vector3.Lerp(q0, q1, num3), fromCar, toEnd, out hit))
					{
						num2 = num3;
					}
					else
					{
						num = num3;
					}
				}
				if (!Blocked(world, Vector3.Lerp(p0, p1, num2), Vector3.Lerp(q0, q1, num2), fromCar, toEnd, out hit2))
				{
					return false;
				}
				normal = hit2.Normal;
				point = PushOut(world, hit2.Point + normal * 0.1f, normal);
				return true;
			}
			if (!Blocked(world, p1, q1, fromCar, toEnd, out hit2) || !Blocked(world, q1, p1, toEnd, fromCar, out var hit3))
			{
				return false;
			}
			Vector3 mid = (hit2.Point + hit3.Point) * 0.5f;
			float num4 = -1f;
			for (int j = 1; j <= 100; j++)
			{
				if (Clear(0.5f * (float)j))
				{
					num4 = 0.5f * (float)j;
					break;
				}
			}
			if (num4 < 0f)
			{
				return false;
			}
			float num5 = num4;
			float num6 = 0f;
			for (int k = 0; k < 8; k++)
			{
				float num7 = (num5 + num6) * 0.5f;
				if (Clear(num7))
				{
					num5 = num7;
				}
				else
				{
					num6 = num7;
				}
			}
			normal = Vector3.UnitY;
			point = mid + Vector3.UnitY * (num5 + 0.1f);
			return true;
			bool Clear(float h)
			{
				Vector3 vector = mid + Vector3.UnitY * h;
				if (!Blocked(world, p1, vector, fromCar, trimEnd: false, out var hit4))
				{
					return !Blocked(world, vector, q1, trimStart: false, toEnd, out hit4);
				}
				return false;
			}
		}

		private static Vector3 PushOut(IObstacles world, Vector3 p, Vector3 normal)
		{
			for (int i = 0; i < 10; i++)
			{
				if (!world.Overlaps(p, 0.05f))
				{
					break;
				}
				p += normal * 0.1f;
			}
			return p;
		}

		public static Tension Solve(float pathLength, float ropeLength, float sA, float sB, float bendAngle, float friction, float invA, float invB, float frequency, float dampingRatio, float maxTension, float dt)
		{
			float num = pathLength - ropeLength;
			float num2 = sA + sB;
			float num3 = sA - sB;
			float num4 = MathF.Exp((0f - MathF.Max(0f, friction)) * bendAngle * Math.Clamp(MathF.Abs(num3) / 0.1f, 0f, 1f));
			float num5 = ((num3 >= 0f) ? 1f : num4);
			float num6 = ((num3 >= 0f) ? num4 : 1f);
			float num7 = num5 * invA + num6 * invB;
			if (num7 < 1E-08f || dt <= 0f)
			{
				return Tension.None;
			}
			float value;
			if (num > 0f)
			{
				float num8 = (float)Math.PI * 2f * ((frequency > 0f) ? frequency : 3f);
				float num9 = ((dampingRatio > 0f) ? dampingRatio : 1f);
				float num10 = 2f * num9 + dt * num8;
				float num11 = dt * num8 * num10;
				value = num11 / (1f + num11) * (num2 + num8 / num10 * num) / num7;
			}
			else
			{
				if (!(num + num2 * dt > 0f))
				{
					return Tension.None;
				}
				value = (num2 + num / dt) / num7;
			}
			value = Math.Clamp(value, 0f, MathF.Max(0f, maxTension) * dt);
			if (!(value > 0f))
			{
				return Tension.None;
			}
			return new Tension(value, num5, num6);
		}

		public static float SlackLength(float ropeLength, float straightDistance)
		{
			return MathF.Max(ropeLength, straightDistance + MathF.Max(1f, straightDistance * 0.1f));
		}

		public static float MaxForSlack(float ropeLength, float maxLength, float slackLength)
		{
			return MathF.Max(maxLength, slackLength + MathF.Max(0f, maxLength - ropeLength));
		}

		public static float CarryOverReeling(float realLength, float slackLength, float lengthAfterTick, float minLength, float maxLength)
		{
			return Math.Clamp(realLength + (lengthAfterTick - slackLength), minLength, MathF.Max(maxLength, minLength));
		}

		public static List<Vector3> Sag(IReadOnlyList<Vector3> path, float ropeLength, IObstacles world)
		{
			float num = 0f;
			for (int i = 1; i < path.Count; i++)
			{
				num += Vector3.Distance(path[i - 1], path[i]);
			}
			float num2 = MathF.Max(0f, ropeLength - num);
			List<Vector3> list = new List<Vector3> { path[0] };
			for (int j = 1; j < path.Count; j++)
			{
				Vector3 value = path[j - 1];
				Vector3 value2 = path[j];
				float num3 = Vector3.Distance(value, value2);
				float num4 = ((num > 0f) ? (num2 * num3 / num) : 0f);
				float num5 = MathF.Sqrt(3f * num3 * num4 / 8f);
				int num6 = ((!(num2 > 0.01f)) ? 1 : Math.Clamp((int)MathF.Ceiling(num3 / 0.75f), 2, 40));
				for (int k = 1; k <= num6; k++)
				{
					float num7 = (float)k / (float)num6;
					Vector3 vector = Vector3.Lerp(value, value2, num7);
					if (k < num6 && num5 > 0.01f)
					{
						float num8 = num5 * 4f * num7 * (1f - num7);
						Vector3 vector2 = vector - Vector3.UnitY * num8;
						float? num9 = world.GroundBelow(vector + Vector3.UnitY * 1.5f, 1.5f + num8 + 0.5f);
						float num10 = 0.030000001f;
						if (num9.HasValue)
						{
							float valueOrDefault = num9.GetValueOrDefault();
							if (valueOrDefault + num10 > vector2.Y)
							{
								vector2.Y = valueOrDefault + num10;
							}
						}
						vector = vector2;
					}
					list.Add(vector);
				}
			}
			return list;
		}
	}
	[RegisterTypeInIl2Cpp]
	public class RopeWrap : MonoBehaviour
	{
		private class Rope
		{
			public readonly RopePath Path = new RopePath();

			public Vector3 PrevA;

			public Vector3 PrevB;

			public bool HasPrev;

			public WinchStates State;

			public int LoggedCount;

			public float NextLog;

			public bool LoggedAttach;

			public LineRenderer Line;

			public IntPtr HiddenObi;
		}

		private class UnityObstacles : IObstacles
		{
			public static readonly UnityObstacles Instance = new UnityObstacles();

			private int obstacleMask;

			private int groundMask;

			private readonly Il2CppStructArray<RaycastHit> hits = new Il2CppStructArray<RaycastHit>(32L);

			private void Masks()
			{
				if (obstacleMask == 0)
				{
					try
					{
						obstacleMask = Layers.TerrainLayerMask | Layers.RockLayerMask | Layers.TreeLayerMask | Layers.DefaultLayerMask | Layers.CampableRockLayerMask;
						groundMask = Layers.TerrainLayerMask;
					}
					catch (Exception ex)
					{
						Mod.Log.Warning("Rope: falling back to layer names (" + ex.Message + ")");
						obstacleMask = LayerMask.GetMask(new string[5] { "Default", "Terrain", "Rock", "Tree", "CampableRock" });
						groundMask = LayerMask.GetMask(new string[1] { "Terrain" });
					}
					if (groundMask == 0)
					{
						groundMask = obstacleMask;
					}
				}
			}

			public bool Raycast(Vector3 origin, Vector3 direction, float maxDistance, out Hit hit)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0015: 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)
				//IL_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_0088: Unknown result type (might be due to invalid IL or missing references)
				//IL_0094: Unknown result type (might be due to invalid IL or missing references)
				Masks();
				hit = default(Hit);
				int num = Physics.RaycastNonAlloc(U(origin), U(direction), hits, maxDistance, obstacleMask, (QueryTriggerInteraction)1);
				float num2 = float.MaxValue;
				bool result = false;
				for (int i = 0; i < num; i++)
				{
					RaycastHit val = ((Il2CppArrayBase<RaycastHit>)(object)hits)[i];
					if (!(((RaycastHit)(ref val)).distance < 0.001f) && !(((RaycastHit)(ref val)).distance >= num2))
					{
						Rigidbody rigidbody = ((RaycastHit)(ref val)).rigidbody;
						if (!((Object)(object)rigidbody != (Object)null) || rigidbody.isKinematic)
						{
							num2 = ((RaycastHit)(ref val)).distance;
							hit = new Hit(N(((RaycastHit)(ref val)).point), N(((RaycastHit)(ref val)).normal));
							result = true;
						}
					}
				}
				return result;
			}

			public bool Overlaps(Vector3 point, float radius)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				Masks();
				return Physics.CheckSphere(U(point), radius, obstacleMask, (QueryTriggerInteraction)1);
			}

			public float? GroundBelow(Vector3 from, float depth)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: Unknown result type (might be due to invalid IL or missing references)
				Masks();
				RaycastHit val = default(RaycastHit);
				if (!Physics.Raycast(U(from), Vector3.down, ref val, depth, groundMask, (QueryTriggerInteraction)1))
				{
					return null;
				}
				return ((RaycastHit)(ref val)).point.y;
			}
		}

		internal class StockState
		{
			public float RopeLength;

			public float MaxRopeLength;

			public float Slack;

			public float RaisedMax = -1f;
		}

		private readonly Dictionary<IntPtr, Rope> ropes = new Dictionary<IntPtr, Rope>();

		private readonly List<Vector3> drawPath = new List<Vector3>();

		private Material lineMaterial;

		public static RopeWrap Instance { get; private set; }

		private static bool Active
		{
			get
			{
				if (Mod.Wrap.Value)
				{
					if (!HillNet.IsAuthority)
					{
						return HillNet.HostHas("RealisticWinch");
					}
					return true;
				}
				return false;
			}
		}

		public RopeWrap(IntPtr ptr)
			: base(ptr)
		{
		}

		private void Awake()
		{
			Instance = this;
			Tracking.Winches.Removed += OnWinchRemoved;
			Tracking.PlayerMoved += OnPlayerMoved;
		}

		private void OnDestroy()
		{
			Tracking.Winches.Removed -= OnWinchRemoved;
			Tracking.PlayerMoved -= OnPlayerMoved;
		}

		private void OnWinchRemoved(VehicleWinchSystem w)
		{
			Drop(((Il2CppObjectBase)w).Pointer, null);
		}

		private void OnPlayerMoved(int playerId)
		{
			//IL_004e: 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_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Invalid comparison between Unknown and I4
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if (ropes.Count == 0)
			{
				return;
			}
			foreach (VehicleWinchSystem item in Tracking.Winches.Items)
			{
				if (ropes.ContainsKey(((Il2CppObjectBase)item).Pointer))
				{
					PlayerRef val;
					int num;
					if ((Object)(object)((SimulationBehaviour)item).Object != (Object)null)
					{
						val = ((SimulationBehaviour)item).Object.InputAuthority;
						num = ((((PlayerRef)(ref val)).PlayerId == playerId) ? 1 : 0);
					}
					else
					{
						num = 0;
					}
					int num2;
					if ((int)item.WinchState == 1)
					{
						val = item.WinchedCarPlayerRef;
						num2 = ((((PlayerRef)(ref val)).PlayerId == playerId) ? 1 : 0);
					}
					else
					{
						num2 = 0;
					}
					bool flag = (byte)num2 != 0;
					if (((uint)num | (flag ? 1u : 0u)) != 0)
					{
						Drop(((Il2CppObjectBase)item).Pointer, item);
					}
				}
			}
		}

		private static Vector3 N(Vector3 v)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(v.x, v.y, v.z);
		}

		private static Vector3 U(Vector3 v)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(v.X, v.Y, v.Z);
		}

		private void FixedUpdate()
		{
			try
			{
				Tick();
			}
			catch (Exception ex)
			{
				Mod.Log.Error(ex.ToString());
			}
		}

		private unsafe void Tick()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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_008c: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: 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_00d2: 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)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
			IReadOnlyList<VehicleWinchSystem> items = Tracking.Winches.Items;
			for (int i = 0; i < items.Count; i++)
			{
				VehicleWinchSystem val = items[i];
				WinchStates winchState = val.WinchState;
				if (!Active || (int)winchState == 0 || !TryGetEnds(val, out var a, out var _, out var b, out var _))
				{
					Drop(((Il2CppObjectBase)val).Pointer, val);
					continue;
				}
				if (!ropes.TryGetValue(((Il2CppObjectBase)val).Pointer, out var value) || value.State != winchState)
				{
					Drop(((Il2CppObjectBase)val).Pointer, val);
					Dictionary<IntPtr, Rope> dictionary = ropes;
					IntPtr pointer = ((Il2CppObjectBase)val).Pointer;
					Rope obj = new Rope
					{
						State = winchState
					};
					value = obj;
					dictionary[pointer] = obj;
				}
				if (!value.LoggedAttach)
				{
					value.LoggedAttach = true;
					Transform val2 = ObiEnd(val);
					Instance log = Mod.Log;
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(65, 8);
					defaultInterpolatedStringHandler.AppendLiteral("Rope: ");
					defaultInterpolatedStringHandler.AppendFormatted<WinchStates>(winchState);
					defaultInterpolatedStringHandler.AppendLiteral(" car ");
					defaultInterpolatedStringHandler.AppendFormatted<Vector3>(a);
					defaultInterpolatedStringHandler.AppendLiteral(" far end ");
					defaultInterpolatedStringHandler.AppendFormatted<Vector3>(b);
					defaultInterpolatedStringHandler.AppendLiteral(" (");
					Vector3 val3 = b - a;
					defaultInterpolatedStringHandler.AppendFormatted(((Vector3)(ref val3)).magnitude, "F1");
					defaultInterpolatedStringHandler.AppendLiteral(" m, rope ");
					defaultInterpolatedStringHandler.AppendFormatted(val.RopeLength, "F1");
					defaultInterpolatedStringHandler.AppendLiteral(" m), ");
					defaultInterpolatedStringHandler.AppendLiteral("spring ");
					defaultInterpolatedStringHandler.AppendFormatted(val.frequency);
					defaultInterpolatedStringHandler.AppendLiteral(" Hz damping ");
					defaultInterpolatedStringHandler.AppendFormatted(val.dampingRatio);
					defaultInterpolatedStringHandler.AppendLiteral(", obi end ");
					object value2;
					if (!((Object)(object)val2 != (Object)null))
					{
						value2 = "none";
					}
					else
					{
						val3 = val2.position;
						value2 = ((object)(*(Vector3*)(&val3))/*cast due to .constrained prefix*/).ToString();
					}
					defaultInterpolatedStringHandler.AppendFormatted((string?)value2);
					log.Msg(defaultInterpolatedStringHandler.ToStringAndClear());
				}
				RopeMath.UpdateWraps(UnityObstacles.Instance, value.Path, N(a), N(b), value.PrevA, value.PrevB, value.HasPrev);
				value.PrevA = N(a);
				value.PrevB = N(b);
				value.HasPrev = true;
				int count = value.Path.Points.Count;
				if (count != value.LoggedCount && Time.time >= value.NextLog)
				{
					value.NextLog = Time.time + 0.5f;
					value.LoggedCount = count;
					Mod.Log.Msg($"Rope: {count} bends, path {RopeMath.PathLength(N(a), value.Path.Points, N(b)):F1} m, rope {val.RopeLength:F1} m");
				}
			}
		}

		private void Drop(IntPtr key, VehicleWinchSystem w)
		{
			if (ropes.TryGetValue(key, out var value))
			{
				if ((Object)(object)value.Line != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value.Line).gameObject);
				}
				if ((Object)(object)w != (Object)null && value.HiddenObi != IntPtr.Zero)
				{
					SetObiVisible(w, visible: true);
				}
				ropes.Remove(key);
			}
		}

		private static Transform ObiEnd(VehicleWinchSystem w)
		{
			ObiRopeReferences obiRopeReferences = w.obiRopeReferences;
			ObiParticleAttachment val = (((Object)(object)obiRopeReferences != (Object)null) ? obiRopeReferences.endPointAttachment : null);
			if (!((Object)(object)val != (Object)null))
			{
				return null;
			}
			return val.target;
		}

		private static bool TryGetEnds(VehicleWinchSystem w, out Vector3 a, out Rigidbody rbA, out Vector3 b, out Rigidbody rbB)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: 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_000c: 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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_0048: 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_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Invalid comparison between Unknown and I4
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: 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_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Invalid comparison between Unknown and I4
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: 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_00d4: Invalid comparison between Unknown and I4
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			a = (b = default(Vector3));
			rbB = null;
			rbA = w.carRigidbody;
			if ((Object)(object)rbA == (Object)null)
			{
				return false;
			}
			a = ((Component)rbA).transform.TransformPoint(Vector3Compressed.op_Implicit(w.WinchAttachPointInCar));
			WinchStates winchState = w.WinchState;
			if ((int)winchState == 2)
			{
				b = Vector3Compressed.op_Implicit(w.WinchAnchorPoint);
				return true;
			}
			Transform val = ObiEnd(w);
			if ((Object)(object)val != (Object)null)
			{
				b = val.position;
				rbB = ((Component)val).GetComponentInParent<Rigidbody>();
			}
			else if ((int)winchState == 3)
			{
				NetworkRigidbody3D winchedMovableObjectRigidbody = w.WinchedMovableObjectRigidbody;
				rbB = (((Object)(object)winchedMovableObjectRigidbody != (Object)null) ? ((Component)winchedMovableObjectRigidbody).GetComponent<Rigidbody>() : null);
				if ((Object)(object)rbB == (Object)null)
				{
					return false;
				}
				b = rbB.position;
			}
			else
			{
				if ((int)winchState != 1)
				{
					return false;
				}
				PlayerRef winchedCarPlayerRef = w.WinchedCarPlayerRef;
				VehicleWinchSystem val2 = CarOf(((PlayerRef)(ref winchedCarPlayerRef)).PlayerId);
				rbB = (((Object)(object)val2 != (Object)null) ? val2.carRigidbody : null);
				if ((Object)(object)rbB == (Object)null)
				{
					return false;
				}
				b = rbB.worldCenterOfMass;
			}
			if ((Object)(object)rbB != (Object)null && (rbB.isKinematic || ((Il2CppObjectBase)rbB).Pointer == ((Il2CppObjectBase)rbA).Pointer))
			{
				rbB = null;
			}
			return true;
		}

		private static VehicleWinchSystem CarOf(int playerId)
		{
			//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)
			foreach (VehicleWinchSystem item in Tracking.Winches.Items)
			{
				if ((Object)(object)((SimulationBehaviour)item).Object != (Object)null)
				{
					PlayerRef inputAuthority = ((SimulationBehaviour)item).Object.InputAuthority;
					if (((PlayerRef)(ref inputAuthority)).PlayerId == playerId)
					{
						return item;
					}
				}
			}
			return null;
		}

		internal static StockState BeforeStock(VehicleWinchSystem w)
		{
			//IL_009c: 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_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			RopeWrap instance = Instance;
			if ((Object)(object)instance == (Object)null || !Active || !instance.ropes.TryGetValue(((Il2CppObjectBase)w).Pointer, out var value) || value.Path.Points.Count == 0)
			{
				return null;
			}
			NetworkObject val = ((SimulationBehaviour)w).Object;
			if ((Object)(object)val == (Object)null || (!val.HasStateAuthority && !val.HasInputAuthority))
			{
				return null;
			}
			if (!TryGetEnds(w, out var a, out var rbA, out var b, out var rbB))
			{
				return null;
			}
			NetworkRunner runner = ((SimulationBehaviour)w).Runner;
			float dt = (((Object)(object)runner != (Object)null) ? runner.DeltaTime : Time.fixedDeltaTime);
			ApplyTension(w, value.Path, a, rbA, b, rbB, dt);
			StockState stockState = new StockState
			{
				RopeLength = w.RopeLength,
				MaxRopeLength = w.MaxRopeLength
			};
			stockState.Slack = RopeMath.SlackLength(stockState.RopeLength, Vector3.Distance(a, b));
			float num = RopeMath.MaxForSlack(stockState.RopeLength, stockState.MaxRopeLength, stockState.Slack);
			if (num > stockState.MaxRopeLength)
			{
				w.MaxRopeLength = (stockState.RaisedMax = num);
			}
			w.RopeLength = stockState.Slack;
			return stockState;
		}

		internal static void AfterStock(VehicleWinchSystem w, StockState s)
		{
			if (s != null)
			{
				if (s.RaisedMax >= 0f && w.MaxRopeLength == s.RaisedMax)
				{
					w.MaxRopeLength = s.MaxRopeLength;
				}
				w.RopeLength = RopeMath.CarryOverReeling(s.RopeLength, s.Slack, w.RopeLength, w.minRopeLength, w.MaxRopeLength);
			}
		}

		private static void ApplyTension(VehicleWinchSystem w, RopePath path, Vector3 a, Rigidbody rbA, Vector3 b, Rigidbody rbB, float dt)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: 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)
			//IL_003d: 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)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: 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)
			List<Vector3> points = path.Points;
			Vector3 val = U(points[0]) - a;
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			val = U(points[points.Count - 1]) - b;
			Vector3 normalized2 = ((Vector3)(ref val)).normalized;
			float sA = 0f - Vector3.Dot(rbA.GetPointVelocity(a), normalized);
			float sB = (((Object)(object)rbB != (Object)null) ? (0f - Vector3.Dot(rbB.GetPointVelocity(b), normalized2)) : 0f);
			Tension tension = RopeMath.Solve(RopeMath.PathLength(N(a), points, N(b)), w.RopeLength, sA, sB, RopeMath.BendAngle(N(a), points, N(b)), Mod.Friction.Value, InverseMassAlong(rbA, a, normalized), ((Object)(object)rbB != (Object)null) ? InverseMassAlong(rbB, b, normalized2) : 0f, w.frequency, w.dampingRatio, w.maxTension, dt);
			if (!(tension.Impulse <= 0f))
			{
				rbA.AddForceAtPosition(normalized * (tension.Impulse * tension.ShareA), a, (ForceMode)1);
				if ((Object)(object)rbB != (Object)null)
				{
					rbB.AddForceAtPosition(normalized2 * (tension.Impulse * tension.ShareB), b, (ForceMode)1);
				}
			}
		}

		private static float InverseMassAlong(Rigidbody rb, Vector3 point, Vector3 dir)
		{
			//IL_0024: 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_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: 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)
			//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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: 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_0053: 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_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: 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_0097: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)rb == (Object)null || rb.isKinematic || rb.mass <= 0f)
			{
				return 0f;
			}
			Vector3 val = Vector3.Cross(point - rb.worldCenterOfMass, dir);
			Vector3 val2 = Quaternion.Inverse(rb.rotation * rb.inertiaTensorRotation) * val;
			Vector3 inertiaTensor = rb.inertiaTensor;
			float num = ((inertiaTensor.x > 0f) ? (val2.x * val2.x / inertiaTensor.x) : 0f) + ((inertiaTensor.y > 0f) ? (val2.y * val2.y / inertiaTensor.y) : 0f) + ((inertiaTensor.z > 0f) ? (val2.z * val2.z / inertiaTensor.z) : 0f);
			return 1f / rb.mass + num;
		}

		private void LateUpdate()
		{
			try
			{
				Draw();
			}
			catch (Exception ex)
			{
				Mod.Log.Error(ex.ToString());
			}
		}

		private void Draw()
		{
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			if (ropes.Count == 0)
			{
				return;
			}
			IReadOnlyList<VehicleWinchSystem> items = Tracking.Winches.Items;
			for (int i = 0; i < items.Count; i++)
			{
				VehicleWinchSystem val = items[i];
				if (!ropes.TryGetValue(((Il2CppObjectBase)val).Pointer, out var value))
				{
					continue;
				}
				if (!Mod.Visual.Value || !TryGetEnds(val, out var a, out var _, out var b, out var _))
				{
					if ((Object)(object)value.Line != (Object)null)
					{
						((Renderer)value.Line).enabled = false;
					}
					if (value.HiddenObi != IntPtr.Zero)
					{
						SetObiVisible(val, visible: true);
					}
					value.HiddenObi = IntPtr.Zero;
					continue;
				}
				GameObject obiRopeGameObject = val.obiRopeGameObject;
				if ((Object)(object)obiRopeGameObject != (Object)null && value.HiddenObi != ((Il2CppObjectBase)obiRopeGameObject).Pointer)
				{
					if ((Object)(object)lineMaterial == (Object)null)
					{
						lineMaterial = FindRopeMaterial(obiRopeGameObject);
					}
					SetObiVisible(val, visible: false);
					value.HiddenObi = ((Il2CppObjectBase)obiRopeGameObject).Pointer;
				}
				if ((Object)(object)value.Line == (Object)null)
				{
					value.Line = MakeLine();
				}
				((Renderer)value.Line).enabled = true;
				drawPath.Clear();
				drawPath.Add(N(a));
				drawPath.AddRange(value.Path.Points);
				drawPath.Add(N(b));
				List<Vector3> list = RopeMath.Sag(drawPath, val.RopeLength, UnityObstacles.Instance);
				value.Line.positionCount = list.Count;
				for (int j = 0; j < list.Count; j++)
				{
					value.Line.SetPosition(j, U(list[j]));
				}
			}
		}

		private LineRenderer MakeLine()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: 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_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Expected O, but got Unknown
			LineRenderer obj = new GameObject("HillRope").AddComponent<LineRenderer>();
			obj.useWorldSpace = true;
			obj.widthMultiplier = 0.035f;
			obj.numCapVertices = 2;
			obj.numCornerVertices = 2;
			obj.textureMode = (LineTextureMode)1;
			if ((Object)(object)lineMaterial == (Object)null)
			{
				Shader val = Shader.Find("Universal Render Pipeline/Unlit") ?? Shader.Find("Sprites/Default");
				lineMaterial = new Material(val)
				{
					color = new Color(0.12f, 0.1f, 0.08f)
				};
			}
			((Renderer)obj).sharedMaterial = lineMaterial;
			return obj;
		}

		private static Material FindRopeMaterial(GameObject obi)
		{
			foreach (Renderer componentsInChild in obi.GetComponentsInChildren<Renderer>(true))
			{
				if ((Object)(object)componentsInChild != (Object)null && (Object)(object)componentsInChild.sharedMaterial != (Object)null)
				{
					return componentsInChild.sharedMaterial;
				}
			}
			return null;
		}

		private static void SetObiVisible(VehicleWinchSystem w, bool visible)
		{
			GameObject obiRopeGameObject = w.obiRopeGameObject;
			if ((Object)(object)obiRopeGameObject == (Object)null)
			{
				return;
			}
			foreach (Renderer componentsInChild in obiRopeGameObject.GetComponentsInChildren<Renderer>(true))
			{
				if ((Object)(object)componentsInChild != (Object)null)
				{
					componentsInChild.enabled = visible;
				}
			}
			foreach (MonoBehaviour componentsInChild2 in obiRopeGameObject.GetComponentsInChildren<MonoBehaviour>(true))
			{
				if (!((Object)(object)componentsInChild2 == (Object)null))
				{
					string name = ((MemberInfo)((Object)componentsInChild2).GetIl2CppType()).Name;
					if (name.StartsWith("ObiRope") && name.EndsWith("Renderer"))
					{
						((Behaviour)componentsInChild2).enabled = visible;
					}
				}
			}
		}
	}
}