Decompiled source of Holdens Third Person Mod v1.0.0

HoldensThirdPersonMod.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ThirdPersonMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Holden")]
[assembly: AssemblyProduct("ThirdPersonMod")]
[assembly: AssemblyCopyright("Copyright © Holden 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("12aa83f7-baa2-4332-be61-e878ccf60e56")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ThirdPersonMod;

[ContentWarningPlugin("12aa83f7-baa2-4332-be61-e878ccf60e56", "1.0", true)]
[BepInPlugin("holden.plugin.test.ThirdPerson", "Holden's Third Person Mod!", "1.0")]
public class ThirdPerson : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Spectate), "Update")]
	public static class PatchSPecUpd
	{
		[HarmonyPrefix]
		public static bool UpdatePref()
		{
			if (!Object.op_Implicit((Object)(object)Player.localPlayer))
			{
				return true;
			}
			if (Player.localPlayer.data.dead)
			{
				return true;
			}
			if (!isThirdPerson)
			{
				return true;
			}
			if (Spectate.spectating)
			{
				spectate.Reflect<Spectate>().Invoke("DoSpectate", false);
			}
			if (!Player.localPlayer.data.dead)
			{
				spectate.Reflect<Spectate>().Invoke("StartSpectate", false);
				return false;
			}
			if (Player.localPlayer.data.dead)
			{
				spectate.Reflect<Spectate>().Invoke("StopSpectate", false);
				isThirdPerson = false;
				return false;
			}
			isThirdPerson = false;
			return false;
		}
	}

	[HarmonyPatch(typeof(Spectate), "StartSpectate")]
	public static class StartSpectatePatch
	{
		[HarmonyPrefix]
		public static bool StartSpectatePrefix()
		{
			if (Player.localPlayer.data.dead)
			{
				return true;
			}
			if (!isThirdPerson)
			{
				return true;
			}
			Player.observedPlayer = Player.localPlayer;
			Spectate.spectating = true;
			return false;
		}
	}

	[HarmonyPatch(typeof(Spectate), "DoSpectate")]
	public static class DoSpectatePatch
	{
		[HarmonyPrefix]
		public static bool DoSpectatePrefix()
		{
			//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_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: 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_0090: 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_00b3: 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_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_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: 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_0158: 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)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: 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)
			if (Player.localPlayer.data.dead)
			{
				return true;
			}
			if (!isThirdPerson)
			{
				return true;
			}
			if ((Object)(object)Player.observedPlayer == (Object)null)
			{
				Player.observedPlayer = Player.localPlayer;
				return false;
			}
			((Component)spectate).transform.rotation = Quaternion.LookRotation(Player.localPlayer.data.lookDirection);
			((Component)spectate).transform.position = (Vector3)Player.observedPlayer.Reflect<Player>().Invoke("TransformCenter", false) + Vector3.up * 0.75f;
			Vector3 val = ((Component)spectate).transform.position + ((Component)spectate).transform.forward * -3f;
			RaycastHit val2 = HelperFunctions.LineCheck(((Component)spectate).transform.position, val, (LayerType)1, 0f);
			if (Object.op_Implicit((Object)(object)((RaycastHit)(ref val2)).transform))
			{
				Transform transform = ((Component)spectate).transform;
				transform.position += -((Component)spectate).transform.forward * (((RaycastHit)(ref val2)).distance - 0.2f);
			}
			else
			{
				((Component)spectate).transform.position = val + ((Component)spectate).transform.forward * 0.2f;
			}
			spectate.Reflect<Spectate>().Invoke("Look", false);
			return false;
		}
	}

	[HarmonyPatch(typeof(HelmetUIToggler), "Update")]
	public static class HelmetUITogglerPatch
	{
		[HarmonyPrefix]
		public static bool StartSpectatePrefix()
		{
			if (Player.localPlayer.data.dead)
			{
				return true;
			}
			if (!isThirdPerson)
			{
				return true;
			}
			Player.observedPlayer = Player.localPlayer;
			Spectate.spectating = true;
			return false;
		}
	}

	private static Spectate spectate;

	private static Camera mainCamera;

	public static bool isThirdPerson;

	public static void ApplyPatches()
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)"Trying to apply patches.");
		new Harmony("com.holden.codewarning").PatchAll(Assembly.GetExecutingAssembly());
		Debug.Log((object)"Harmony patches applied.");
	}

	public static void ThirdPersonToggle()
	{
		if (!Input.anyKey || !Input.anyKeyDown || !Input.GetKeyDown((KeyCode)9))
		{
			return;
		}
		if ((Object)(object)spectate == (Object)null)
		{
			spectate = Object.FindObjectOfType<Spectate>();
		}
		if (!Spectate.spectating)
		{
			if ((Object)(object)mainCamera == (Object)null)
			{
				mainCamera = ((Component)spectate).GetComponent<Camera>();
			}
			StartSpectate();
		}
		else
		{
			if ((Object)(object)mainCamera == (Object)null)
			{
				mainCamera = ((Component)spectate).GetComponent<Camera>();
			}
			StopSpectate();
		}
	}

	private static void StartSpectate()
	{
		mainCamera.cullingMask = -1;
		spectate.Reflect<Spectate>().Invoke("StartSpectate", false);
		isThirdPerson = true;
	}

	private static void StopSpectate()
	{
		mainCamera.cullingMask = -1909354953;
		spectate.Reflect<Spectate>().Invoke("StopSpectate", false);
		isThirdPerson = false;
	}

	private void Start()
	{
		ApplyPatches();
	}

	private void Update()
	{
		ThirdPersonToggle();
	}
}
public class ReflectionUtil<R>
{
	private const BindingFlags privateInst = BindingFlags.Instance | BindingFlags.NonPublic;

	private const BindingFlags privateStatic = BindingFlags.Static | BindingFlags.NonPublic;

	private const BindingFlags privateField = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField;

	private const BindingFlags privateProp = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty;

	private const BindingFlags privateMethod = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod;

	private const BindingFlags staticField = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField;

	private const BindingFlags staticProp = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetProperty;

	private const BindingFlags staticMethod = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;

	private R @object { get; }

	private Type type { get; }

	internal ReflectionUtil(R obj)
	{
		@object = obj;
		type = typeof(R);
	}

	private object GetValue(string variableName, BindingFlags flags)
	{
		FieldInfo field = type.GetField(variableName, flags);
		if (!(field != null))
		{
			return null;
		}
		return field.GetValue(@object);
	}

	private object GetProperty(string propertyName, BindingFlags flags)
	{
		PropertyInfo property = type.GetProperty(propertyName, flags);
		if (!(property != null))
		{
			return null;
		}
		return property.GetValue(@object);
	}

	private void SetValue(string variableName, object value, BindingFlags flags)
	{
		FieldInfo field = type.GetField(variableName, flags);
		if (field != null)
		{
			field.SetValue(@object, value);
		}
	}

	private void SetProperty(string propertyName, object value, BindingFlags flags)
	{
		PropertyInfo property = type.GetProperty(propertyName, flags);
		if (property != null)
		{
			property.SetValue(@object, value);
		}
	}

	private object InvokeMethod(string methodName, BindingFlags flags, params object[] args)
	{
		MethodInfo[] methods = type.GetMethods(flags);
		foreach (MethodInfo methodInfo in methods)
		{
			if (methodInfo.Name == methodName && MatchMethodParameters(methodInfo, args))
			{
				return methodInfo.Invoke(@object, args);
			}
		}
		return null;
	}

	private bool MatchMethodParameters(MethodInfo method, object[] args)
	{
		ParameterInfo[] parameters = method.GetParameters();
		if (parameters.Length != args.Length)
		{
			return false;
		}
		for (int i = 0; i < parameters.Length; i++)
		{
			if (!parameters[i].ParameterType.IsInstanceOfType(args[i]) && args[i] != null)
			{
				return false;
			}
		}
		return true;
	}

	public object GetValue(string fieldName, bool isStatic = false, bool isProperty = false)
	{
		BindingFlags flags = ((!isProperty) ? (isStatic ? (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField) : (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField)) : (isStatic ? (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetProperty) : (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty)));
		if (!isProperty)
		{
			return GetValue(fieldName, flags);
		}
		return GetProperty(fieldName, flags);
	}

	public void SetValue(string fieldName, object value, bool isStatic = false, bool isProperty = false)
	{
		BindingFlags flags = ((!isProperty) ? (isStatic ? (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField) : (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField)) : (isStatic ? (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetProperty) : (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty)));
		if (isProperty)
		{
			SetProperty(fieldName, value, flags);
		}
		else
		{
			SetValue(fieldName, value, flags);
		}
	}

	public object Invoke(string methodName, bool isStatic = false, params object[] args)
	{
		return InvokeMethod(methodName, isStatic ? (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod) : (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod), args);
	}
}
public static class ReflectorExtensions
{
	public static ReflectionUtil<R> Reflect<R>(this R obj)
	{
		return new ReflectionUtil<R>(obj);
	}
}