Decompiled source of LCThirdPerson v1.0.4

LCThirdPerson.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
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 GameNetcodeStuff;
using HarmonyLib;
using LCThirdPerson.Patches;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LCThirdPerson")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LCThirdPerson")]
[assembly: AssemblyTitle("LCThirdPerson")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 LCThirdPerson
{
	[BepInPlugin("LCThirdPerson", "LCThirdPerson", "1.0.0")]
	public class ThirdPersonPlugin : BaseUnityPlugin
	{
		private readonly Harmony harmony = new Harmony("LCThirdPerson");

		private bool tpEnabled;

		public static ThirdPersonPlugin Instance { get; private set; }

		public static Transform Camera { get; internal set; }

		public static Transform OriginalTransform { get; internal set; }

		public static Sprite CrosshairSprite { get; internal set; }

		public ConfigEntry<KeyboardShortcut> Enable { get; internal set; }

		public ConfigEntry<bool> ShowCursor { get; set; }

		public ConfigEntry<bool> StartEnabled { get; set; }

		public ConfigEntry<Vector3> Offset { get; set; }

		public bool Enabled
		{
			get
			{
				return tpEnabled;
			}
			set
			{
				SetEnabled(value);
			}
		}

		internal static ManualLogSource Log { get; set; }

		internal UnityEvent OnEnable { get; private set; }

		internal UnityEvent OnDisable { get; private set; }

		private void Awake()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LCThirdPerson is loaded!");
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Log = Logger.CreateLogSource("LCThirdPerson");
			OnEnable = new UnityEvent();
			OnDisable = new UnityEvent();
			SetConfig();
			SetCrosshairSprite();
			harmony.PatchAll(typeof(ThirdPersonPlugin));
			harmony.PatchAll(typeof(CameraPatch));
			harmony.PatchAll(typeof(CursorPatch));
			harmony.PatchAll(typeof(HUDPatch));
		}

		internal void SetConfig()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			Enable = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Keybinds", "Toggle", new KeyboardShortcut((KeyCode)118, Array.Empty<KeyCode>()), (ConfigDescription)null);
			ShowCursor = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "ShowCursor", true, (ConfigDescription)null);
			StartEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "StartEnabled", true, (ConfigDescription)null);
			Offset = ((BaseUnityPlugin)this).Config.Bind<Vector3>("Options", "CameraOffset", new Vector3(1f, 0f, -2f), (ConfigDescription)null);
			Enabled = StartEnabled.Value;
		}

		internal void SetCrosshairSprite()
		{
			CrosshairSprite = CursorPatch.CreateCrosshairSprite();
		}

		internal void CheckEnable()
		{
			//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)
			KeyboardShortcut value = Enable.Value;
			if (((KeyboardShortcut)(ref value)).IsUp())
			{
				Enabled = !Enabled;
			}
		}

		public void SetEnabled(bool value)
		{
			tpEnabled = value;
			if (tpEnabled)
			{
				OnEnable.Invoke();
			}
			else
			{
				OnDisable.Invoke();
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LCThirdPerson";

		public const string PLUGIN_NAME = "LCThirdPerson";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace LCThirdPerson.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class CameraPatch
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static UnityAction <0>__OnEnable;

			public static UnityAction <1>__OnDisable;
		}

		private static PlayerControllerB Instance;

		private static bool TriggerAwake;

		private static int OriginalCullingMask;

		private static ShadowCastingMode OriginalShadowCastingMode;

		private static readonly string[] IgnoreGameObjectPrefixes = new string[1] { "VolumeMain" };

		public static void OnEnable()
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			ThirdPersonPlugin.Log.LogInfo((object)"Enabled");
			Transform localVisor = Instance.localVisor;
			SkinnedMeshRenderer thisPlayerModel = Instance.thisPlayerModel;
			((Component)localVisor).gameObject.SetActive(false);
			((Renderer)thisPlayerModel).shadowCastingMode = (ShadowCastingMode)1;
			((Renderer)Instance.thisPlayerModelArms).enabled = false;
			Instance.grabDistance = Math.Max(Instance.grabDistance - ThirdPersonPlugin.Instance.Offset.Value.z, 5f);
			Instance.gameplayCamera.cullingMask = OriginalCullingMask | 0x800000;
		}

		public static void OnDisable()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			ThirdPersonPlugin.Log.LogInfo((object)"Disabled");
			Transform localVisor = Instance.localVisor;
			SkinnedMeshRenderer thisPlayerModel = Instance.thisPlayerModel;
			((Component)localVisor).gameObject.SetActive(true);
			((Renderer)thisPlayerModel).shadowCastingMode = OriginalShadowCastingMode;
			((Renderer)Instance.thisPlayerModelArms).enabled = true;
			Instance.grabDistance = 5f;
			Instance.gameplayCamera.cullingMask = OriginalCullingMask;
		}

		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void Awake()
		{
			TriggerAwake = true;
		}

		[HarmonyPrefix]
		[HarmonyPatch("Update")]
		private static void PatchUpdate(ref PlayerControllerB __instance, ref bool ___isCameraDisabled, ref bool ___isPlayerControlled)
		{
			//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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: 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)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Expected O, but got Unknown
			if (!___isPlayerControlled | ___isCameraDisabled)
			{
				return;
			}
			if (TriggerAwake)
			{
				Instance = __instance;
				OriginalCullingMask = Instance.gameplayCamera.cullingMask;
				OriginalShadowCastingMode = ((Renderer)Instance.thisPlayerModel).shadowCastingMode;
				UnityEvent onEnable = ThirdPersonPlugin.Instance.OnEnable;
				object obj = <>O.<0>__OnEnable;
				if (obj == null)
				{
					UnityAction val = OnEnable;
					<>O.<0>__OnEnable = val;
					obj = (object)val;
				}
				onEnable.AddListener((UnityAction)obj);
				UnityEvent onDisable = ThirdPersonPlugin.Instance.OnDisable;
				object obj2 = <>O.<1>__OnDisable;
				if (obj2 == null)
				{
					UnityAction val2 = OnDisable;
					<>O.<1>__OnDisable = val2;
					obj2 = (object)val2;
				}
				onDisable.AddListener((UnityAction)obj2);
				if (ThirdPersonPlugin.Instance.Enabled)
				{
					OnEnable();
				}
				TriggerAwake = false;
			}
			if (!((Object)(object)Instance == (Object)null))
			{
				if ((Object)(object)ThirdPersonPlugin.OriginalTransform == (Object)null)
				{
					ThirdPersonPlugin.OriginalTransform = CopyTransform(((Component)Instance.gameplayCamera).transform, "LCThirdPerson_Original Camera Position");
					ThirdPersonPlugin.Camera = CopyTransform(((Component)Instance.gameplayCamera).transform, "LCThirdPerson_Camera Position");
				}
				ThirdPersonPlugin.Camera.position = ((Component)Instance.gameplayCamera).transform.position;
				ThirdPersonPlugin.Camera.rotation = ((Component)Instance.gameplayCamera).transform.rotation;
				((Component)Instance.gameplayCamera).transform.rotation = ((Component)ThirdPersonPlugin.OriginalTransform).transform.rotation;
				((Component)Instance.gameplayCamera).transform.position = ((Component)ThirdPersonPlugin.OriginalTransform).transform.position;
				if (!Instance.inTerminalMenu && !Instance.isTypingChat)
				{
					ThirdPersonPlugin.Instance.CheckEnable();
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("LateUpdate")]
		private static void PatchCamera(ref bool ___isCameraDisabled, ref bool ___isPlayerControlled)
		{
			//IL_003e: 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: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: 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_00ce: 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)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: 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_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)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: 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_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: 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_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			Transform originalTransform = ThirdPersonPlugin.OriginalTransform;
			if ((!___isPlayerControlled | ___isCameraDisabled) || (Object)(object)originalTransform == (Object)null)
			{
				return;
			}
			Camera gameplayCamera = Instance.gameplayCamera;
			((Component)originalTransform).transform.rotation = ((Component)gameplayCamera).transform.rotation;
			if (ThirdPersonPlugin.Instance.Enabled && !Instance.inTerminalMenu)
			{
				Vector3 val = ((Component)originalTransform).transform.right * ThirdPersonPlugin.Instance.Offset.Value.x + ((Component)originalTransform).transform.up * ThirdPersonPlugin.Instance.Offset.Value.y;
				Vector3 position = ((Component)originalTransform).transform.position;
				Vector3 val2 = ((Component)originalTransform).transform.position + val + ((Component)originalTransform).transform.forward * ThirdPersonPlugin.Instance.Offset.Value.z;
				RaycastHit val3 = default(RaycastHit);
				val = ((!Physics.Linecast(position, val2, ref val3, StartOfRound.Instance.collidersAndRoomMask) || IgnoreCollision(((Object)((RaycastHit)(ref val3)).transform).name)) ? (val + ((Component)originalTransform).transform.forward * -2f) : (val + ((Component)originalTransform).transform.forward * (0f - Mathf.Max(((RaycastHit)(ref val3)).distance, 0f))));
				((Component)gameplayCamera).transform.position = ((Component)originalTransform).transform.position + val;
				if (!Instance.isClimbingLadder)
				{
					MethodInfo method = typeof(PlayerControllerB).GetMethod("SetHoverTipAndCurrentInteractTrigger", BindingFlags.Instance | BindingFlags.NonPublic);
					method.Invoke(Instance, new object[0]);
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("SetFaceUnderwaterFilters")]
		private static void UnderwaterPrepatch()
		{
			//IL_003c: 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)
			if (!((Object)(object)Instance == (Object)null) && !((Object)(object)ThirdPersonPlugin.Camera == (Object)null))
			{
				((Component)Instance.gameplayCamera).transform.position = ((Component)ThirdPersonPlugin.Camera).transform.position;
				((Component)Instance.gameplayCamera).transform.rotation = ((Component)ThirdPersonPlugin.Camera).transform.rotation;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetFaceUnderwaterFilters")]
		private static void UnderwaterPostpatch()
		{
			//IL_003c: 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)
			if (!((Object)(object)Instance == (Object)null) && !((Object)(object)ThirdPersonPlugin.OriginalTransform == (Object)null))
			{
				((Component)Instance.gameplayCamera).transform.position = ((Component)ThirdPersonPlugin.OriginalTransform).transform.position;
				((Component)Instance.gameplayCamera).transform.rotation = ((Component)ThirdPersonPlugin.OriginalTransform).transform.rotation;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("SpawnPlayerAnimation")]
		private static void SpawnPlayerPostpatch(ref bool ___isPlayerControlled)
		{
			if (!((Object)(object)Instance == (Object)null) && ___isPlayerControlled && ThirdPersonPlugin.Instance.Enabled)
			{
				OnEnable();
			}
		}

		private static Transform CopyTransform(Transform copyTransform, string gameObjectName)
		{
			//IL_0002: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = new GameObject(gameObjectName).transform;
			transform.position = copyTransform.position;
			transform.rotation = copyTransform.rotation;
			transform.parent = copyTransform.parent;
			return transform;
		}

		private static bool IgnoreCollision(string name)
		{
			return IgnoreGameObjectPrefixes.Any((string prefix) => name.StartsWith(prefix));
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class CursorPatch
	{
		internal static Sprite CreateCrosshairSprite()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			string name = "LCThirdPerson.crosshair.png";
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(name);
			Texture2D val = new Texture2D(2, 2);
			using (MemoryStream memoryStream = new MemoryStream())
			{
				manifestResourceStream.CopyTo(memoryStream);
				ImageConversion.LoadImage(val, memoryStream.ToArray());
			}
			((Texture)val).filterMode = (FilterMode)0;
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2((float)(((Texture)val).width / 2), (float)(((Texture)val).height / 2)));
		}

		[HarmonyPostfix]
		[HarmonyPatch("LateUpdate")]
		private static void PatchUpdate(ref PlayerControllerB __instance, ref bool ___isCameraDisabled)
		{
			if (!(!__instance.isPlayerControlled | ___isCameraDisabled) && ThirdPersonPlugin.Instance.Enabled && ThirdPersonPlugin.Instance.ShowCursor.Value && !__instance.inTerminalMenu && !((Behaviour)__instance.cursorIcon).enabled)
			{
				((Behaviour)__instance.cursorIcon).enabled = true;
				__instance.cursorIcon.sprite = ThirdPersonPlugin.CrosshairSprite;
			}
		}
	}
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("UpdateScanNodes")]
		private static void UnderwaterPrepatch(PlayerControllerB playerScript)
		{
			//IL_0028: 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)
			if (!((Object)(object)ThirdPersonPlugin.Camera == (Object)null))
			{
				((Component)playerScript.gameplayCamera).transform.position = ((Component)ThirdPersonPlugin.Camera).transform.position;
				((Component)playerScript.gameplayCamera).transform.rotation = ((Component)ThirdPersonPlugin.Camera).transform.rotation;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("UpdateScanNodes")]
		private static void UnderwaterPostpatch(PlayerControllerB playerScript)
		{
			//IL_0028: 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)
			if (!((Object)(object)ThirdPersonPlugin.OriginalTransform == (Object)null))
			{
				((Component)playerScript.gameplayCamera).transform.position = ((Component)ThirdPersonPlugin.OriginalTransform).transform.position;
				((Component)playerScript.gameplayCamera).transform.rotation = ((Component)ThirdPersonPlugin.OriginalTransform).transform.rotation;
			}
		}
	}
}