Decompiled source of SpectatePreviousPlayer v1.0.2

plugins\SpectatePreviousPlayer.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SpectatePreviousPlayer.patch;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SpectatePreviousPlayer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SpectatePreviousPlayer")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5e21fa35-9f97-42b8-b795-306db0c8bee9")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SpectatePreviousPlayer
{
	[BepInPlugin("kerotein.SpectatePreviousPlayer", "SpectatePreviousPlayer", "1.0.2")]
	public class SpectatePreviousPlayer : BaseUnityPlugin
	{
		private const string ModName = "SpectatePreviousPlayer";

		private const string ModAuthor = "kerotein";

		private const string ModGUID = "kerotein.SpectatePreviousPlayer";

		private const string ModVersion = "1.0.2";

		private static readonly Harmony harmony = new Harmony("SpectatePreviousPlayer");

		internal void Awake()
		{
			harmony.PatchAll(typeof(Patch));
		}
	}
}
namespace SpectatePreviousPlayer.patch
{
	[HarmonyPatch(typeof(HUDManager))]
	internal static class Patch
	{
		private static readonly Type TypeHUDManager = typeof(HUDManager);

		private static readonly FieldInfo FieldInfoHoldButtonToEndGameEarlyHoldTime = TypeHUDManager.GetField("holdButtonToEndGameEarlyHoldTime", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly Type TypePlayerControllerB = typeof(PlayerControllerB);

		private static readonly MethodInfo MethodInfoRaycastSpectateCameraAroundPivot = TypePlayerControllerB.GetMethod("RaycastSpectateCameraAroundPivot", BindingFlags.Instance | BindingFlags.NonPublic);

		private static bool PingScanIsPressed;

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		internal static void HUDManager_Update_Patch(HUDManager __instance)
		{
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)null || (Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)null || !GameNetworkManager.Instance.localPlayerController.isPlayerDead)
			{
				return;
			}
			if (!StartOfRound.Instance.shipIsLeaving && StartOfRound.Instance.currentLevel.planetHasTime && !TimeOfDay.Instance.shipLeavingAlertCalled && !TimeOfDay.Instance.votedShipToLeaveEarlyThisRound)
			{
				if (StartOfRound.Instance.localPlayerUsingController)
				{
					((TMP_Text)__instance.holdButtonToEndGameEarlyText).text = "Tell autopilot ship to leave early : [R-trigger] (Hold)\nSpectate previous player : [R-trigger]";
				}
				else
				{
					((TMP_Text)__instance.holdButtonToEndGameEarlyText).text = "Tell autopilot ship to leave early : [RMB] (Hold)\nSpectate previous player : [RMB]";
				}
			}
			MovementActions movement = __instance.playerActions.Movement;
			if (((MovementActions)(ref movement)).PingScan.IsPressed())
			{
				if ((float)FieldInfoHoldButtonToEndGameEarlyHoldTime.GetValue(__instance) <= 0.25f)
				{
					((Component)__instance.holdButtonToEndGameEarlyMeter).gameObject.SetActive(false);
				}
				if (!PingScanIsPressed)
				{
					PingScanIsPressed = true;
					SpectatePreviousPlayer();
				}
			}
			else
			{
				PingScanIsPressed = false;
			}
		}

		private static void SpectatePreviousPlayer()
		{
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			if ((Object)(object)StartOfRound.Instance.localPlayerController.spectatedPlayerScript != (Object)null)
			{
				num = (int)StartOfRound.Instance.localPlayerController.spectatedPlayerScript.playerClientId;
			}
			for (int i = 0; i < StartOfRound.Instance.allPlayerScripts.Length; i++)
			{
				num = ((num - 1) % StartOfRound.Instance.allPlayerScripts.Length + StartOfRound.Instance.allPlayerScripts.Length) % StartOfRound.Instance.allPlayerScripts.Length;
				if (!StartOfRound.Instance.localPlayerController.playersManager.allPlayerScripts[num].isPlayerDead && StartOfRound.Instance.localPlayerController.playersManager.allPlayerScripts[num].isPlayerControlled && (Object)(object)StartOfRound.Instance.localPlayerController.playersManager.allPlayerScripts[num] != (Object)(object)StartOfRound.Instance.localPlayerController)
				{
					StartOfRound.Instance.localPlayerController.spectatedPlayerScript = StartOfRound.Instance.localPlayerController.playersManager.allPlayerScripts[num];
					StartOfRound.Instance.SetPlayerSafeInShip();
					StartOfRound.Instance.localPlayerController.SetSpectatedPlayerEffects(false);
					return;
				}
			}
			DeadBodyInfo deadBody = StartOfRound.Instance.localPlayerController.deadBody;
			if (deadBody != null && ((Component)deadBody).gameObject.activeSelf)
			{
				StartOfRound.Instance.localPlayerController.spectateCameraPivot.position = StartOfRound.Instance.localPlayerController.deadBody.bodyParts[0].position;
				MethodInfoRaycastSpectateCameraAroundPivot.Invoke(StartOfRound.Instance.localPlayerController, null);
			}
			StartOfRound.Instance.SetPlayerSafeInShip();
		}
	}
}