Decompiled source of PeakCinema v0.3.2

plugins/com.github.megalon.peakcinema.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.megalon.peakcinema")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.3.2.0")]
[assembly: AssemblyInformationalVersion("0.3.2+457d01ee296d440060b705e16f2198faf0130a4b")]
[assembly: AssemblyProduct("com.github.megalon.peakcinema")]
[assembly: AssemblyTitle("PeakCinema")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.3.2.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.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]
	[Microsoft.CodeAnalysis.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]
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace PeakCinema
{
	[BepInPlugin("com.github.megalon.peakcinema", "PeakCinema", "0.3.2")]
	public class Plugin : BaseUnityPlugin
	{
		public class PluginModConfig
		{
			public readonly ConfigEntry<KeyCode> toggleCinemaCamControlKey;

			public readonly ConfigEntry<KeyCode> exitCinemaCamKey;

			public readonly ConfigEntry<KeyCode> keyMoveForward;

			public readonly ConfigEntry<KeyCode> keyMoveBackward;

			public readonly ConfigEntry<KeyCode> keyMoveLeft;

			public readonly ConfigEntry<KeyCode> keyMoveRight;

			public readonly ConfigEntry<KeyCode> keyMoveUp;

			public readonly ConfigEntry<KeyCode> keyMoveDown;

			public readonly ConfigEntry<KeyCode> keyMoveFaster;

			public readonly ConfigEntry<KeyCode> keySmoothToggle;

			public PluginModConfig(ConfigFile config)
			{
				toggleCinemaCamControlKey = config.Bind<KeyCode>("Keybinds", "Toggle Cinema Cam Control", (KeyCode)284, "");
				exitCinemaCamKey = config.Bind<KeyCode>("Keybinds", "Exit Cinema Cam", (KeyCode)27, "");
				keyMoveForward = config.Bind<KeyCode>("Keybinds", "Move Forward", (KeyCode)119, "");
				keyMoveBackward = config.Bind<KeyCode>("Keybinds", "Move Backward", (KeyCode)115, "");
				keyMoveLeft = config.Bind<KeyCode>("Keybinds", "Move Left", (KeyCode)97, "");
				keyMoveRight = config.Bind<KeyCode>("Keybinds", "Move Right", (KeyCode)100, "");
				keyMoveUp = config.Bind<KeyCode>("Keybinds", "Move Up", (KeyCode)32, "");
				keyMoveDown = config.Bind<KeyCode>("Keybinds", "Move Down", (KeyCode)306, "");
				keyMoveFaster = config.Bind<KeyCode>("Keybinds", "Move Faster", (KeyCode)304, "");
				keySmoothToggle = config.Bind<KeyCode>("Keybinds", "Toggle Camera Smoothing", (KeyCode)301, "");
			}
		}

		internal static GameObject HUD = null;

		internal static List<VoiceObscuranceFilter> VoiceFilters = new List<VoiceObscuranceFilter>();

		public const string Id = "com.github.megalon.peakcinema";

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


		internal static PluginModConfig ModConfig { get; private set; } = null;


		internal static bool CinemaCamActive { get; private set; }

		internal static Transform? CamTransform { get; private set; }

		internal static bool CameraWasSpawned { get; private set; }

		internal static bool Smoothing { get; private set; } = true;


		internal static float HoldTimer { get; private set; }

		internal static float InitHoldTimer { get; private set; } = 3f;


		internal static Vector3 DeathLocation { get; private set; }

		public static string Name => "PeakCinema";

		public static string Version => "0.3.2";

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)("Plugin " + Name + " is loaded!"));
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
			ModConfig = new PluginModConfig(((BaseUnityPlugin)this).Config);
			HoldTimer = InitHoldTimer;
		}

		[HarmonyPatch(typeof(VoiceObscuranceFilter), "Start")]
		[HarmonyPostfix]
		private static void VoiceObscuranceFilter_Start(VoiceObscuranceFilter __instance)
		{
			if (CinemaCamActive)
			{
				SetVoiceFilterToCinemaCam(__instance);
			}
			VoiceFilters.Add(__instance);
		}

		[HarmonyPatch(typeof(CinemaCamera), "Start")]
		[HarmonyPostfix]
		private static void CinemaCamera_Start(CinemaCamera __instance)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			CamTransform = __instance.cam;
			CameraWasSpawned = false;
			HoldTimer = InitHoldTimer;
			DeathLocation = Vector3.zero;
		}

		[HarmonyPatch(typeof(CinemaCamera), "Update")]
		[HarmonyPrefix]
		private static bool CinemaCameraFix(CinemaCamera __instance)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: 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_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_031d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_039e: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_040f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0447: Unknown result type (might be due to invalid IL or missing references)
			//IL_0480: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_04f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0502: Unknown result type (might be due to invalid IL or missing references)
			//IL_051e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0534: Unknown result type (might be due to invalid IL or missing references)
			//IL_0549: Unknown result type (might be due to invalid IL or missing references)
			//IL_0559: Unknown result type (might be due to invalid IL or missing references)
			//IL_056f: Unknown result type (might be due to invalid IL or missing references)
			//IL_057f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0595: Unknown result type (might be due to invalid IL or missing references)
			//IL_05a5: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)HUD == (Object)null)
			{
				HUD = GameObject.Find("Canvas_HUD");
			}
			if (Input.GetKeyDown(ModConfig.exitCinemaCamKey.Value))
			{
				CinemaCamActive = false;
				__instance.on = false;
				GameObject hUD = HUD;
				if (hUD != null)
				{
					hUD.SetActive(true);
				}
				((Component)__instance.cam).gameObject.SetActive(false);
				if ((Object)(object)__instance.fog != (Object)null)
				{
					((Component)__instance.fog).gameObject.SetActive(true);
				}
				if ((Object)(object)__instance.oldCam != (Object)null)
				{
					((Component)__instance.oldCam).gameObject.SetActive(true);
				}
				foreach (VoiceObscuranceFilter voiceFilter in VoiceFilters)
				{
					if (!((Object)(object)voiceFilter == (Object)null))
					{
						voiceFilter.head = ((Component)MainCamera.instance).transform;
					}
				}
			}
			else if (Input.GetKey(ModConfig.toggleCinemaCamControlKey.Value))
			{
				HoldTimer -= Time.deltaTime;
				if (HoldTimer <= 0f)
				{
					MoveCameraToPlayerPosition(__instance);
					if (!__instance.on)
					{
						__instance.on = true;
					}
					HoldTimer = -1f;
				}
			}
			else if (Input.GetKeyUp(ModConfig.toggleCinemaCamControlKey.Value))
			{
				if (!CinemaCamActive)
				{
					CinemaCamActive = true;
					HandleVoiceFilters();
				}
				if (HoldTimer > -1f)
				{
					__instance.on = !__instance.on;
				}
				HoldTimer = InitHoldTimer;
			}
			if (__instance.on)
			{
				InputSystem.actions.Disable();
				GameObject hUD2 = HUD;
				if (hUD2 != null)
				{
					hUD2.SetActive(false);
				}
				__instance.ambience.parent = ((Component)__instance).transform;
				if (Object.op_Implicit((Object)(object)__instance.fog))
				{
					((Component)__instance.fog).gameObject.SetActive(false);
				}
				if (Object.op_Implicit((Object)(object)__instance.oldCam))
				{
					((Component)__instance.oldCam).gameObject.SetActive(false);
				}
				((Component)__instance).transform.parent = null;
				__instance.cam.parent = null;
				if (!CameraWasSpawned)
				{
					MoveCameraToPlayerPosition(__instance);
				}
				((Component)__instance.cam).gameObject.SetActive(true);
				if (Input.GetKeyDown(ModConfig.keySmoothToggle.Value))
				{
					Smoothing = !Smoothing;
				}
				float num = 0f;
				if (Smoothing)
				{
					__instance.vel = Vector3.Lerp(__instance.vel, Vector3.zero, 1f * Time.deltaTime);
					__instance.rot = Vector3.Lerp(__instance.rot, Vector3.zero, 2.5f * Time.deltaTime);
					num = (Input.GetKey(ModConfig.keyMoveFaster.Value) ? 0.2f : 0.05f);
					__instance.rot.y += Input.GetAxis("Mouse X") * num * 0.05f;
					__instance.rot.x += Input.GetAxis("Mouse Y") * num * 0.05f;
				}
				else
				{
					__instance.vel = Vector3.zero;
					__instance.rot = Vector3.zero;
					num = (Input.GetKey(ModConfig.keyMoveFaster.Value) ? 10f : 5f);
					__instance.rot.y += Input.GetAxis("Mouse X") * num * 0.1f;
					__instance.rot.x += Input.GetAxis("Mouse Y") * num * 0.1f;
				}
				float num2 = num * Time.deltaTime;
				if (Input.GetKey(ModConfig.keyMoveRight.Value))
				{
					__instance.vel.x = (Smoothing ? (__instance.vel.x + num2) : num2);
				}
				if (Input.GetKey(ModConfig.keyMoveLeft.Value))
				{
					__instance.vel.x = (Smoothing ? (__instance.vel.x - num2) : (0f - num2));
				}
				if (Input.GetKey(ModConfig.keyMoveForward.Value))
				{
					__instance.vel.z = (Smoothing ? (__instance.vel.z + num2) : num2);
				}
				if (Input.GetKey(ModConfig.keyMoveBackward.Value))
				{
					__instance.vel.z = (Smoothing ? (__instance.vel.z - num2) : (0f - num2));
				}
				if (Input.GetKey(ModConfig.keyMoveUp.Value))
				{
					__instance.vel.y = (Smoothing ? (__instance.vel.y + num2) : num2);
				}
				if (Input.GetKey(ModConfig.keyMoveDown.Value))
				{
					__instance.vel.y = (Smoothing ? (__instance.vel.y - num2) : (0f - num2));
				}
				((Component)__instance.cam).transform.Rotate(Vector3.up * __instance.rot.y, (Space)0);
				((Component)__instance.cam).transform.Rotate(((Component)__instance).transform.right * (0f - __instance.rot.x));
				((Component)__instance.cam).transform.Translate(Vector3.right * __instance.vel.x, (Space)1);
				((Component)__instance.cam).transform.Translate(Vector3.forward * __instance.vel.z, (Space)1);
				((Component)__instance.cam).transform.Translate(Vector3.up * __instance.vel.y, (Space)0);
				__instance.t = true;
				CameraWasSpawned = true;
			}
			else
			{
				InputSystem.actions.Enable();
			}
			return false;
		}

		private static void MoveCameraToPlayerPosition(CinemaCamera __instance)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: 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_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			Character val = Character.AllCharacters.First((Character c) => c.IsLocal);
			if ((Object)(object)val != (Object)null)
			{
				if (val.data.dead)
				{
					((Component)__instance.cam).transform.position = DeathLocation;
				}
				else
				{
					((Component)__instance.cam).transform.position = val.refs.animationPositionTransform.position;
				}
				Transform transform = ((Component)__instance.cam).transform;
				transform.position += new Vector3(0f, 1.5f, -2f);
			}
		}

		private static void HandleVoiceFilters()
		{
			for (int num = VoiceFilters.Count - 1; num >= 0; num--)
			{
				VoiceObscuranceFilter val = VoiceFilters[num];
				if ((Object)(object)val == (Object)null)
				{
					VoiceFilters.RemoveAt(num);
				}
			}
			foreach (VoiceObscuranceFilter voiceFilter in VoiceFilters)
			{
				if (!((Object)(object)voiceFilter == (Object)null))
				{
					SetVoiceFilterToCinemaCam(voiceFilter);
				}
			}
			Log.LogInfo((object)$"Adjusted {VoiceFilters.Count} voice filters!");
		}

		private static void SetVoiceFilterToCinemaCam(VoiceObscuranceFilter filter)
		{
			if (!((Object)(object)CamTransform == (Object)null) && !((Object)(object)filter == (Object)null))
			{
				filter.head = CamTransform;
			}
		}

		[HarmonyPatch(typeof(AmbienceAudio), "FixedUpdate")]
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Expected O, but got Unknown
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Expected O, but got Unknown
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			MethodInfo methodInfo = AccessTools.Method(typeof(Component), "GetComponent", (Type[])null, (Type[])null).MakeGenericMethod(typeof(CharacterData));
			for (int i = 0; i < list.Count - 4; i++)
			{
				if (list[i].opcode == OpCodes.Ldarg_0 && CodeInstructionExtensions.Calls(list[i + 1], AccessTools.PropertyGetter(typeof(Component), "transform")) && CodeInstructionExtensions.Calls(list[i + 2], AccessTools.PropertyGetter(typeof(Transform), "root")) && CodeInstructionExtensions.Calls(list[i + 3], methodInfo) && list[i + 4].opcode == OpCodes.Stloc_0)
				{
					list.RemoveRange(i, 5);
					list.InsertRange(i, new <>z__ReadOnlyArray<CodeInstruction>((CodeInstruction[])(object)new CodeInstruction[3]
					{
						new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Character), "localCharacter")),
						new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Character), "data")),
						new CodeInstruction(OpCodes.Stloc_0, (object)null)
					}));
					break;
				}
			}
			return list;
		}

		[HarmonyPatch(typeof(Character), "RPCA_Die")]
		[HarmonyPrefix]
		private static bool Character_RPCA_Die(Character __instance)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			if (!__instance.IsLocal)
			{
				return true;
			}
			DeathLocation = __instance.refs.animationPositionTransform.position;
			return true;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
[CompilerGenerated]
internal sealed class <>z__ReadOnlyArray<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
	int ICollection.Count => _items.Length;

	bool ICollection.IsSynchronized => false;

	object ICollection.SyncRoot => this;

	object IList.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	bool IList.IsFixedSize => true;

	bool IList.IsReadOnly => true;

	int IReadOnlyCollection<T>.Count => _items.Length;

	T IReadOnlyList<T>.this[int index] => _items[index];

	int ICollection<T>.Count => _items.Length;

	bool ICollection<T>.IsReadOnly => true;

	T IList<T>.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	public <>z__ReadOnlyArray(T[] items)
	{
		_items = items;
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return ((IEnumerable)_items).GetEnumerator();
	}

	void ICollection.CopyTo(Array array, int index)
	{
		((ICollection)_items).CopyTo(array, index);
	}

	int IList.Add(object value)
	{
		throw new NotSupportedException();
	}

	void IList.Clear()
	{
		throw new NotSupportedException();
	}

	bool IList.Contains(object value)
	{
		return ((IList)_items).Contains(value);
	}

	int IList.IndexOf(object value)
	{
		return ((IList)_items).IndexOf(value);
	}

	void IList.Insert(int index, object value)
	{
		throw new NotSupportedException();
	}

	void IList.Remove(object value)
	{
		throw new NotSupportedException();
	}

	void IList.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}

	IEnumerator<T> IEnumerable<T>.GetEnumerator()
	{
		return ((IEnumerable<T>)_items).GetEnumerator();
	}

	void ICollection<T>.Add(T item)
	{
		throw new NotSupportedException();
	}

	void ICollection<T>.Clear()
	{
		throw new NotSupportedException();
	}

	bool ICollection<T>.Contains(T item)
	{
		return ((ICollection<T>)_items).Contains(item);
	}

	void ICollection<T>.CopyTo(T[] array, int arrayIndex)
	{
		((ICollection<T>)_items).CopyTo(array, arrayIndex);
	}

	bool ICollection<T>.Remove(T item)
	{
		throw new NotSupportedException();
	}

	int IList<T>.IndexOf(T item)
	{
		return ((IList<T>)_items).IndexOf(item);
	}

	void IList<T>.Insert(int index, T item)
	{
		throw new NotSupportedException();
	}

	void IList<T>.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}
}