Decompiled source of ThirdPerson v1.0.0

kaesem_3rdPersonView.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Lightbug.CharacterControllerPro.Demo;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BG_ThirdPerson")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BG_ThirdPerson")]
[assembly: AssemblyTitle("BG_ThirdPerson")]
[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 BG_ThirdPerson
{
	[BepInPlugin("com.kaesem.bg.thirdperson", "BG Third Person Toggle", "1.0.0")]
	public class ThirdPersonPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Camera3D), "get_ShouldBeThirdPerson")]
		private static class ShouldBeThirdPersonPatch
		{
			private static void Postfix(ref bool __result)
			{
				if (_forceThirdPerson)
				{
					__result = true;
				}
			}
		}

		[HarmonyPatch(typeof(Camera3D), "get_ThirdPersonRenderMode")]
		private static class ThirdPersonRenderModePatch
		{
			private static void Postfix(ref bool __result)
			{
				if (_forceThirdPerson)
				{
					__result = true;
				}
			}
		}

		[HarmonyPatch(typeof(Camera3D), "Update")]
		private static class CameraUpdatePatch
		{
			private static void Postfix(Camera3D __instance)
			{
				_ = _forceThirdPerson;
			}
		}

		private const string HarmonyId = "com.kaesem.bg.thirdperson";

		private Camera3D _camera;

		private bool _thirdPerson;

		private bool _loggedMissing;

		private ManualLogSource _log;

		private MethodInfo _changeMode;

		private static bool _forceThirdPerson;

		private static FieldInfo _fieldDebugThirdPerson;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			_log = ((BaseUnityPlugin)this).Logger;
			new Harmony("com.kaesem.bg.thirdperson").PatchAll();
			_log.LogInfo((object)"BG Third Person ready. Press F7 to toggle.");
		}

		private void Update()
		{
			if (Input.GetKeyDown((KeyCode)288))
			{
				ToggleThirdPerson();
			}
		}

		private void ToggleThirdPerson()
		{
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			if (_forceThirdPerson)
			{
				num = 1;
			}
			num = (num + 1) % 2;
			_forceThirdPerson = num != 0;
			_thirdPerson = _forceThirdPerson;
			Camera3D[] array = Object.FindObjectsOfType<Camera3D>(true);
			if (array == null || array.Length == 0)
			{
				if (!_loggedMissing)
				{
					_loggedMissing = true;
					_log.LogWarning((object)"Camera3D not found in scene; third-person toggle unavailable.");
				}
				return;
			}
			_loggedMissing = false;
			if (_changeMode == null)
			{
				_changeMode = AccessTools.Method(typeof(Camera3D), "ChangeCameraMode", new Type[1] { typeof(CameraMode) }, (Type[])null);
				if (_changeMode == null)
				{
					_log.LogWarning((object)"ChangeCameraMode not found on Camera3D; cannot toggle view.");
				}
			}
			if (_fieldDebugThirdPerson == null)
			{
				_fieldDebugThirdPerson = AccessTools.Field(typeof(Camera3D), "debugThirdPerson");
			}
			CameraMode val = (CameraMode)(_forceThirdPerson ? 1 : 0);
			Camera3D[] array2 = array;
			foreach (Camera3D obj in array2)
			{
				if (_changeMode != null)
				{
					_changeMode.Invoke(obj, new object[1] { val });
				}
				if (_fieldDebugThirdPerson != null)
				{
					_fieldDebugThirdPerson.SetValue(obj, _forceThirdPerson);
				}
			}
			string arg = ((num == 0) ? "First-person" : "Third-person (back)");
			_log.LogInfo((object)$"{arg} (applied to {array.Length} camera(s)).");
		}

		private bool EnsureCamera()
		{
			if ((Object)(object)_camera != (Object)null)
			{
				return true;
			}
			_camera = Object.FindObjectOfType<Camera3D>(true);
			if ((Object)(object)_camera == (Object)null)
			{
				if (!_loggedMissing)
				{
					_loggedMissing = true;
					_log.LogWarning((object)"Camera3D not found in scene; third-person toggle unavailable.");
				}
				return false;
			}
			_loggedMissing = false;
			return true;
		}
	}
}