Decompiled source of CartFix v1.0.5

CartFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CartFix.Services;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("Vippy")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyInformationalVersion("1.0.5+01f769260f116c8db3cdb7e6777589520909746a")]
[assembly: AssemblyProduct("CartFix")]
[assembly: AssemblyTitle("CartFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 CartFix
{
	internal static class BuildInfo
	{
		public const string Version = "1.0.5";
	}
	[BepInPlugin("Vippy.CartFix", "CartFix", "1.0.5")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<float> LoadMassFactor = null;

		internal static bool Enabled { get; set; } = true;

		internal static ManualLogSource Log { get; private set; } = null;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			LoadMassFactor = ((BaseUnityPlugin)this).Config.Bind<float>("CartFix", "Load mass factor", 2f, new ConfigDescription("Extra cart mass per unit of payload mass while steering. At 2 the cart is always at least twice as heavy as its cargo, enough for momentum to survive contacts with the payload. 0 is vanilla.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 5f), Array.Empty<object>()));
			new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID).PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} loaded.");
		}
	}
}
namespace CartFix.Services
{
	internal static class CartPhysics
	{
		public const float SteerMass = 4f;

		public const float LockedSmallCartMass = 8f;

		public const float VanillaLerpSpeed = 1f;

		private const float LerpCoefficient = 15f;

		private const float SettledRelativeSpeed = 1.5f;

		public static bool IsVanillaFlatMass(float mass)
		{
			if (mass != 4f)
			{
				return mass == 8f;
			}
			return true;
		}

		public static float OverrideMass(float vanillaMass, float payloadMass, float factor)
		{
			if (!IsVanillaFlatMass(vanillaMass))
			{
				return vanillaMass;
			}
			return vanillaMass + payloadMass * factor;
		}

		public static Vector3 CartVelocityAt(Vector3 cartVelocity, Vector3 cartAngularVelocity, Vector3 offsetFromCartCenter)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			return cartVelocity + Vector3.Cross(cartAngularVelocity, offsetFromCartCenter);
		}

		public static bool StillInFlight(Vector3 itemVelocity, Vector3 cartVelocityAtItem)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: 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)
			Vector3 val = itemVelocity - cartVelocityAtItem;
			return ((Vector3)(ref val)).magnitude > 1.5f;
		}

		public static Vector3 PullTowardCart(Vector3 itemVelocity, Vector3 cartVelocityAtItem, float fixedDeltaTime)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//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_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = Vector3.Lerp(itemVelocity, cartVelocityAtItem, 15f * fixedDeltaTime);
			if (val.y > itemVelocity.y)
			{
				val.y = itemVelocity.y;
			}
			return val;
		}
	}
}
namespace CartFix.Patches
{
	[HarmonyPatch(typeof(PhysGrabObjectImpactDetector), "FixedUpdate")]
	internal static class CartLerpBoostPatch
	{
		private static void Prefix(PhysGrabObjectImpactDetector __instance, out Vector3 __state)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			__state = ((__instance.inCart && (Object)(object)__instance.rb != (Object)null) ? __instance.rb.velocity : Vector3.zero);
		}

		private static void Postfix(PhysGrabObjectImpactDetector __instance, Vector3 __state)
		{
			//IL_007b: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: 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_00c8: 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_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.Enabled || !__instance.inCart || (GameManager.instance.gameMode == 1 && !PhotonNetwork.IsMasterClient) || __instance.isEnemy || __instance.physGrabObject.playerGrabbing.Count != 0)
			{
				return;
			}
			PhysGrabCart currentCart = __instance.currentCart;
			if ((Object)(object)currentCart == (Object)null || (Object)(object)currentCart.rb == (Object)null)
			{
				return;
			}
			Rigidbody rb = __instance.rb;
			if (!((Object)(object)rb == (Object)null) && !rb.isKinematic && !(rb.velocity != __state) && !(((Vector3)(ref currentCart.actualVelocity)).magnitude > 1f) && !((Object)(object)((Component)__instance).GetComponent<PlayerTumble>() != (Object)null))
			{
				Vector3 cartVelocityAtItem = CartPhysics.CartVelocityAt(currentCart.actualVelocity, currentCart.rb.angularVelocity, rb.worldCenterOfMass - currentCart.rb.worldCenterOfMass);
				if (!CartPhysics.StillInFlight(rb.velocity, cartVelocityAtItem))
				{
					rb.velocity = CartPhysics.PullTowardCart(rb.velocity, cartVelocityAtItem, Time.fixedDeltaTime);
				}
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabCart), "CartMassOverride")]
	internal static class CartMassOverrideScalePatch
	{
		private static bool warnedUnknownMass;

		private static void Prefix(PhysGrabCart __instance, ref float mass)
		{
			if (!Plugin.Enabled)
			{
				return;
			}
			if (!CartPhysics.IsVanillaFlatMass(mass))
			{
				if (!warnedUnknownMass)
				{
					warnedUnknownMass = true;
					Plugin.Log.LogWarning((object)$"[CartMass] got {mass:F2}, expected 4 or 8, standing down");
				}
			}
			else
			{
				mass = CartPhysics.OverrideMass(mass, PayloadMass(__instance.itemsInCart), Plugin.LoadMassFactor.Value);
			}
		}

		private static float PayloadMass(List<PhysGrabObject> items)
		{
			float num = 0f;
			for (int i = 0; i < items.Count; i++)
			{
				PhysGrabObject val = items[i];
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val.rb == (Object)null))
				{
					num += ((val.massOriginal > 0f) ? val.massOriginal : val.rb.mass);
				}
			}
			return num;
		}
	}
}