Decompiled source of PeakCinemaFix v1.0.2

PeakCinemaFix.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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: AssemblyVersion("0.0.0.0")]
[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 PeakCinemaNoHoldAndReset
{
	[BepInPlugin("yourname.peakcinema.noholdreset", "PeakCinema NoHold + F4 Reset (+Speed Tweaks)", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <SpeedTranspiler>d__14 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private IEnumerator<CodeInstruction> <>7__wrap1;

			CodeInstruction? IEnumerator<CodeInstruction>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object? IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <SpeedTranspiler>d__14(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || num == 1)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap1 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<>7__wrap1 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					}
					if (<>7__wrap1.MoveNext())
					{
						CodeInstruction current = <>7__wrap1.Current;
						if (current.opcode == OpCodes.Ldc_R4 && current.operand is float a)
						{
							if (Nearly(a, 0.05f))
							{
								current.operand = 0.15f;
							}
							else if (Nearly(a, 0.2f))
							{
								current.operand = 1f;
							}
							else if (Nearly(a, 5f))
							{
								current.operand = 15f;
							}
							else if (Nearly(a, 10f))
							{
								current.operand = 50f;
							}
						}
						<>2__current = current;
						<>1__state = 1;
						return true;
					}
					<>m__Finally1();
					<>7__wrap1 = null;
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap1 != null)
				{
					<>7__wrap1.Dispose();
				}
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<SpeedTranspiler>d__14 <SpeedTranspiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<SpeedTranspiler>d__ = this;
				}
				else
				{
					<SpeedTranspiler>d__ = new <SpeedTranspiler>d__14(0);
				}
				<SpeedTranspiler>d__.instructions = <>3__instructions;
				return <SpeedTranspiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		private static ManualLogSource LogS;

		private static Harmony _harmony;

		private static Type? PeakType;

		private static PropertyInfo? p_CamTransform;

		private static PropertyInfo? p_CameraWasSpawned;

		private static PropertyInfo? p_DeathLocation;

		private static PropertyInfo? p_Smoothing;

		private static FieldInfo? bf_InitHoldTimer;

		private static FieldInfo? bf_HoldTimer;

		private const float BIG_HOLD = 999f;

		private const float NORMAL_MULT = 3f;

		private const float FAST_MULT = 5f;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Expected O, but got Unknown
			LogS = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("yourname.peakcinemahotfix.noholdreset");
			PeakType = AccessTools.TypeByName("PeakCinema.Plugin");
			if (PeakType == null)
			{
				LogS.LogError((object)"PeakCinema.Plugin not found. Is PeakCinema installed?");
				return;
			}
			p_CamTransform = AccessTools.Property(PeakType, "CamTransform");
			p_CameraWasSpawned = AccessTools.Property(PeakType, "CameraWasSpawned");
			p_DeathLocation = AccessTools.Property(PeakType, "DeathLocation");
			p_Smoothing = AccessTools.Property(PeakType, "Smoothing");
			bf_InitHoldTimer = AccessTools.Field(PeakType, "<InitHoldTimer>k__BackingField");
			bf_HoldTimer = AccessTools.Field(PeakType, "<HoldTimer>k__BackingField");
			TrySetFloat(bf_InitHoldTimer, 999f);
			TrySetFloat(bf_HoldTimer, 999f);
			TrySetBoolProp(p_Smoothing, v: false);
			MethodInfo methodInfo = AccessTools.Method(PeakType, "CinemaCameraFix", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				LogS.LogError((object)"PeakCinema.Plugin.CinemaCameraFix not found for transpile.");
			}
			else
			{
				_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin).GetMethod("SpeedTranspiler", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null);
				LogS.LogInfo((object)"Applied speed transpiler: normal x3, fast x5 (smoothing on/off).");
			}
			LogS.LogInfo((object)"NoHold active (hold branch disabled). Smoothing default OFF. F4 = manual reset.");
		}

		private void Update()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_0068: 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_00cd: 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_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: 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_0083: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			TrySetFloat(bf_HoldTimer, 999f);
			TrySetBoolProp(p_CameraWasSpawned, v: true);
			if (!Input.GetKeyDown((KeyCode)285))
			{
				return;
			}
			try
			{
				Transform val = (Transform)(p_CamTransform?.GetValue(null, null));
				if ((Object)(object)val == (Object)null)
				{
					LogS.LogDebug((object)"F4: CamTransform is null.");
					return;
				}
				Vector3 position;
				if (TryGetAliveHeadPos(out var pos))
				{
					position = pos;
				}
				else
				{
					Vector3 val2 = (Vector3)(p_DeathLocation?.GetValue(null, null) ?? ((object)Vector3.zero));
					if (val2 == Vector3.zero)
					{
						LogS.LogDebug((object)"F4: no alive pos and no death location.");
						return;
					}
					position = val2 + new Vector3(0f, 1.5f, -2f);
				}
				val.position = position;
				TrySetBoolProp(p_CameraWasSpawned, v: true);
				TrySetFloat(bf_HoldTimer, 999f);
				LogS.LogDebug((object)"F4: camera reset.");
			}
			catch (Exception ex)
			{
				LogS.LogWarning((object)("F4 reset failed: " + ex.Message));
			}
		}

		[IteratorStateMachine(typeof(<SpeedTranspiler>d__14))]
		private static IEnumerable<CodeInstruction> SpeedTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <SpeedTranspiler>d__14(-2)
			{
				<>3__instructions = instructions
			};
		}

		private static bool Nearly(float a, float b)
		{
			return Mathf.Abs(a - b) <= 0.0001f;
		}

		private static void TrySetFloat(FieldInfo? fi, float v)
		{
			try
			{
				fi?.SetValue(null, v);
			}
			catch
			{
			}
		}

		private static void TrySetBoolProp(PropertyInfo? pi, bool v)
		{
			try
			{
				pi?.SetValue(null, v, null);
			}
			catch
			{
			}
		}

		private static bool TryGetAliveHeadPos(out Vector3 pos)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: 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_014e: 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)
			pos = Vector3.zero;
			try
			{
				Type type = AccessTools.TypeByName("Character");
				if (type == null)
				{
					return false;
				}
				object obj = AccessTools.Field(type, "localCharacter")?.GetValue(null);
				if (obj == null && AccessTools.Property(type, "AllCharacters")?.GetValue(null, null) is IEnumerable enumerable)
				{
					foreach (object item in enumerable)
					{
						PropertyInfo propertyInfo = AccessTools.Property(item.GetType(), "IsLocal");
						if (propertyInfo != null && (bool)propertyInfo.GetValue(item, null))
						{
							obj = item;
							break;
						}
					}
				}
				if (obj == null)
				{
					return false;
				}
				object obj2 = AccessTools.Field(obj.GetType(), "refs")?.GetValue(obj);
				if (obj2 == null)
				{
					return false;
				}
				object? obj3 = AccessTools.Field(obj2.GetType(), "animationPositionTransform")?.GetValue(obj2);
				Transform val = (Transform)((obj3 is Transform) ? obj3 : null);
				if ((Object)(object)val == (Object)null)
				{
					return false;
				}
				pos = val.position + new Vector3(0f, 1.5f, -2f);
				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}