Decompiled source of RagdollThirdPerson v1.2.0

RagdollThirdPerson.dll

Decompiled a month ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace RagdollThirdPerson;

public static class PluginInfo
{
	public const string PLUGIN_GUID = "com.ragdollthirdperson.camera";

	public const string PLUGIN_NAME = "RagdollThirdPerson";

	public const string PLUGIN_VERSION = "1.2.0";
}
[BepInPlugin("com.ragdollthirdperson.camera", "RagdollThirdPerson", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enable;

	internal static ConfigEntry<float> Distance;

	internal static ConfigEntry<float> Height;

	internal static ConfigEntry<float> ControlThreshold;

	internal static ConfigEntry<float> RecoverSeconds;

	internal static FieldInfo LocalCharacter;

	internal static FieldInfo CharacterData;

	internal static FieldInfo FullyPassedOut;

	internal static FieldInfo PassedOut;

	internal static FieldInfo LookDirection;

	internal static FieldInfo IsSpectating;

	internal static MethodInfo GetRagdollControl;

	internal static MethodInfo GetCenter;

	internal static bool Recovering;

	internal static float Blend;

	internal static Vector3 LastThirdPersonPos;

	internal static Quaternion LastThirdPersonRot;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Enable = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable", true, "Third person while ragdolled.");
		Distance = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "Distance", 4f, "Distance behind the body.");
		Height = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "Height", 1.4f, "Height above the body.");
		ControlThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "ControlThreshold", 0.4f, "Ragdoll control below this enables third person.");
		RecoverSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "RecoverSeconds", 0.55f, "How long the camera takes to fly into first person when you get up.");
		try
		{
			Type type = AccessTools.TypeByName("Character");
			Type type2 = AccessTools.TypeByName("CharacterData");
			Type type3 = AccessTools.TypeByName("MainCameraMovement");
			if (type != null)
			{
				LocalCharacter = AccessTools.Field(type, "localCharacter");
				CharacterData = AccessTools.Field(type, "data");
				GetCenter = AccessTools.Method(type, "get_Center", (Type[])null, (Type[])null);
			}
			if (type2 != null)
			{
				FullyPassedOut = AccessTools.Field(type2, "fullyPassedOut");
				PassedOut = AccessTools.Field(type2, "passedOut");
				LookDirection = AccessTools.Field(type2, "lookDirection");
				GetRagdollControl = AccessTools.Method(type2, "get_currentRagdollControll", (Type[])null, (Type[])null);
			}
			if (type3 != null)
			{
				IsSpectating = AccessTools.Field(type3, "isSpectating");
			}
			_harmony = new Harmony("com.ragdollthirdperson.camera");
			_harmony.PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)"RagdollThirdPerson v1.2.0 loaded.");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Failed to load: " + ex));
		}
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
[HarmonyPatch]
public static class MainCameraMovement_LateUpdate_Patch
{
	private static MethodBase TargetMethod()
	{
		Type type = AccessTools.TypeByName("MainCameraMovement");
		return (!(type == null)) ? AccessTools.Method(type, "LateUpdate", (Type[])null, (Type[])null) : null;
	}

	private static void Postfix(object __instance)
	{
		//IL_020e: Unknown result type (might be due to invalid IL or missing references)
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0221: 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_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_022c: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0256: Unknown result type (might be due to invalid IL or missing references)
		//IL_025b: Unknown result type (might be due to invalid IL or missing references)
		//IL_024d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0252: Unknown result type (might be due to invalid IL or missing references)
		//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ac: 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)
		//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02db: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_02df: 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_02ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0289: Unknown result type (might be due to invalid IL or missing references)
		//IL_028e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_035f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0361: Unknown result type (might be due to invalid IL or missing references)
		//IL_0365: Unknown result type (might be due to invalid IL or missing references)
		//IL_0371: Unknown result type (might be due to invalid IL or missing references)
		//IL_0373: Unknown result type (might be due to invalid IL or missing references)
		//IL_0377: Unknown result type (might be due to invalid IL or missing references)
		//IL_0383: Unknown result type (might be due to invalid IL or missing references)
		//IL_0388: Unknown result type (might be due to invalid IL or missing references)
		//IL_038f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0394: Unknown result type (might be due to invalid IL or missing references)
		//IL_042c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0431: Unknown result type (might be due to invalid IL or missing references)
		//IL_0435: Unknown result type (might be due to invalid IL or missing references)
		//IL_0441: Unknown result type (might be due to invalid IL or missing references)
		//IL_0446: Unknown result type (might be due to invalid IL or missing references)
		//IL_044a: Unknown result type (might be due to invalid IL or missing references)
		if (Plugin.Enable == null || !Plugin.Enable.Value || Plugin.LocalCharacter == null)
		{
			return;
		}
		if (!Plugin.Recovering && Plugin.Blend <= 0f)
		{
			object value = Plugin.LocalCharacter.GetValue(null);
			if (value == null)
			{
				return;
			}
			object obj = ((!(Plugin.CharacterData != null)) ? null : Plugin.CharacterData.GetValue(value));
			if (obj == null)
			{
				return;
			}
			bool flag = false;
			if (Plugin.GetRagdollControl != null)
			{
				object obj2 = Plugin.GetRagdollControl.Invoke(obj, null);
				if (obj2 is float && (float)obj2 < Plugin.ControlThreshold.Value)
				{
					flag = true;
				}
			}
			if (!flag)
			{
				return;
			}
		}
		try
		{
			if (Plugin.IsSpectating != null)
			{
				object value2 = Plugin.IsSpectating.GetValue(__instance);
				if (value2 is bool && (bool)value2)
				{
					return;
				}
			}
			object value3 = Plugin.LocalCharacter.GetValue(null);
			if (value3 == null)
			{
				return;
			}
			object obj3 = ((!(Plugin.CharacterData != null)) ? null : Plugin.CharacterData.GetValue(value3));
			if (obj3 == null)
			{
				return;
			}
			if (Plugin.FullyPassedOut != null)
			{
				object value4 = Plugin.FullyPassedOut.GetValue(obj3);
				if (value4 is bool && (bool)value4)
				{
					return;
				}
			}
			bool flag2 = false;
			if (Plugin.GetRagdollControl != null)
			{
				object obj4 = Plugin.GetRagdollControl.Invoke(obj3, null);
				if (obj4 is float && (float)obj4 < Plugin.ControlThreshold.Value)
				{
					flag2 = true;
				}
			}
			if (!flag2 && Plugin.PassedOut != null)
			{
				object value5 = Plugin.PassedOut.GetValue(obj3);
				if (value5 is bool && (bool)value5)
				{
					flag2 = true;
				}
			}
			Transform transform = ((Component)__instance).transform;
			Vector3 position = transform.position;
			Quaternion rotation = transform.rotation;
			Vector3 val = position;
			if (Plugin.GetCenter != null)
			{
				val = (Vector3)Plugin.GetCenter.Invoke(value3, null);
			}
			Vector3 val2 = transform.forward;
			if (Plugin.LookDirection != null && Plugin.LookDirection.GetValue(obj3) is Vector3 val3 && ((Vector3)(ref val3)).sqrMagnitude > 0.0001f)
			{
				val2 = ((Vector3)(ref val3)).normalized;
			}
			Vector3 val4 = val - val2 * Plugin.Distance.Value + Vector3.up * Plugin.Height.Value;
			Vector3 val5 = val - val4;
			Quaternion val6 = Quaternion.LookRotation(((Vector3)(ref val5)).normalized, Vector3.up);
			float num = Time.unscaledDeltaTime;
			if (num < 0f)
			{
				num = 0f;
			}
			if (num > 0.05f)
			{
				num = 0.05f;
			}
			if (flag2)
			{
				Plugin.Recovering = true;
				Plugin.Blend = Mathf.MoveTowards(Plugin.Blend, 1f, num / 0.18f);
				float num2 = Smooth(Plugin.Blend);
				transform.position = Vector3.Lerp(position, val4, num2);
				transform.rotation = Quaternion.Slerp(rotation, val6, num2);
				Plugin.LastThirdPersonPos = transform.position;
				Plugin.LastThirdPersonRot = transform.rotation;
			}
			else if (Plugin.Recovering || !(Plugin.Blend <= 0f))
			{
				float num3 = Plugin.RecoverSeconds.Value;
				if (num3 < 0.05f)
				{
					num3 = 0.05f;
				}
				Plugin.Blend = Mathf.MoveTowards(Plugin.Blend, 0f, num / num3);
				if (Plugin.Blend <= 0.001f)
				{
					Plugin.Blend = 0f;
					Plugin.Recovering = false;
				}
				else
				{
					float num4 = Smooth(1f - Plugin.Blend);
					transform.position = Vector3.Lerp(Plugin.LastThirdPersonPos, position, num4);
					transform.rotation = Quaternion.Slerp(Plugin.LastThirdPersonRot, rotation, num4);
				}
			}
		}
		catch
		{
		}
	}

	private static float Smooth(float x)
	{
		if (x < 0f)
		{
			x = 0f;
		}
		if (x > 1f)
		{
			x = 1f;
		}
		return x * x * (3f - 2f * x);
	}
}