Decompiled source of AliveSpectator v1.2.0

plugins/LeerAliveSpectator.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PEAK")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PEAK")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("94d12321-9932-43d2-ba03-502c40b6fcee")]
[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")]
[BepInPlugin("com.leer.alivespectator", "Alive Spectator", "1.0.1")]
public class AliveSpectator : BaseUnityPlugin
{
	[HarmonyPatch(typeof(MainCameraMovement), "LateUpdate")]
	private class Patch_LateUpdate
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			FieldInfo fieldInfo = AccessTools.Field(typeof(Character), "localCharacter");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(Character), "data");
			FieldInfo fieldInfo3 = AccessTools.Field(typeof(CharacterData), "fullyPassedOut");
			MethodInfo methodInfo = AccessTools.Method(typeof(AliveSpectator), "IsManualSpectating", (Type[])null, (Type[])null);
			for (int i = 0; i < list.Count - 4; i++)
			{
				if (CodeInstructionExtensions.LoadsField(list[i], fieldInfo, false) && CodeInstructionExtensions.LoadsField(list[i + 1], fieldInfo2, false) && CodeInstructionExtensions.LoadsField(list[i + 2], fieldInfo3, false))
				{
					list.InsertRange(i + 3, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[2]
					{
						new CodeInstruction(OpCodes.Call, (object)methodInfo),
						new CodeInstruction(OpCodes.Or, (object)null)
					});
					break;
				}
			}
			return list;
		}
	}

	[HarmonyPatch(typeof(CharacterInput), "Sample")]
	private class Patch_CharacterInput_Sample
	{
		private static void Prefix(ref bool playerMovementActive)
		{
			if (IsManualSpectating())
			{
				playerMovementActive = false;
			}
		}

		private static void Postfix(CharacterInput __instance)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			if (IsManualSpectating())
			{
				resetInputMethod?.Invoke(__instance, null);
				if (CharacterInput.action_look != null)
				{
					__instance.lookInput = CharacterInput.action_look.ReadValue<Vector2>();
				}
				else
				{
					__instance.lookInput = Vector2.zero;
				}
				InputAction action_spectateLeft = CharacterInput.action_spectateLeft;
				__instance.spectateLeftWasPressed = action_spectateLeft != null && action_spectateLeft.WasPressedThisFrame();
				InputAction action_spectateRight = CharacterInput.action_spectateRight;
				__instance.spectateRightWasPressed = action_spectateRight != null && action_spectateRight.WasPressedThisFrame();
				float num = Input.mouseScrollDelta.y;
				if (Input.GetKey((KeyCode)334))
				{
					num -= 0.1f;
				}
				if (Input.GetKey((KeyCode)335))
				{
					num += 0.1f;
				}
				__instance.scrollInput = num;
			}
		}
	}

	private static bool manualSpectateActive;

	private MainCameraMovement cameraMovement;

	private ConfigEntry<KeyCode> toggleKey;

	private ConfigEntry<KeyCode> gamepadKey1;

	private ConfigEntry<KeyCode> gamepadKey2;

	private static MethodInfo resetInputMethod;

	private void Awake()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Binds", "KeyboardToggle", (KeyCode)288, "Keyboard key for toggling spectator mode on/off");
		gamepadKey1 = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Binds", "GamepadToggle1", (KeyCode)334, "First gamepad button for toggle activation (e.g., LB)");
		gamepadKey2 = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Binds", "GamepadToggle2", (KeyCode)335, "Second gamepad button for toggle activation (e.g., RB)");
		new Harmony("com.leer.alivespectator").PatchAll(Assembly.GetExecutingAssembly());
		resetInputMethod = typeof(CharacterInput).GetMethod("ResetInput", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	private void Update()
	{
		if (IsTogglePressed() && !((Object)(object)Character.localCharacter == (Object)null) && !((Object)(object)Character.localCharacter.data == (Object)null) && !Character.localCharacter.data.fullyPassedOut)
		{
			if ((Object)(object)cameraMovement == (Object)null)
			{
				cameraMovement = Singleton<MainCameraMovement>.Instance;
			}
			if (!((Object)(object)cameraMovement == (Object)null))
			{
				manualSpectateActive = !manualSpectateActive;
			}
		}
	}

	private bool IsTogglePressed()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		bool keyDown = Input.GetKeyDown(toggleKey.Value);
		bool flag = (Input.GetKeyDown(gamepadKey1.Value) && Input.GetKey(gamepadKey2.Value)) || (Input.GetKeyDown(gamepadKey2.Value) && Input.GetKey(gamepadKey1.Value));
		return keyDown || flag;
	}

	public static bool IsManualSpectating()
	{
		return manualSpectateActive;
	}
}