Decompiled source of SpectatorSwitch v0.1.1

plugins/StonewardsSpectatorSwitch/StonewardsSpectatorSwitch.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.Rendering;
using UnityEngine.UIElements;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("StonewardsSpectatorSwitch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+11fb70f4fc0b517dd36576079d2d94a322900e77")]
[assembly: AssemblyProduct("StonewardsSpectatorSwitch")]
[assembly: AssemblyTitle("StonewardsSpectatorSwitch")]
[assembly: AssemblyVersion("0.1.1.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 StonewardsSpectatorSwitch
{
	internal sealed class SpectatorHud
	{
		private static readonly Color PanelColor = new Color(0.075f, 0.065f, 0.055f, 0.93f);

		private static readonly Color BorderColor = new Color(0.53f, 0.4f, 0.23f, 1f);

		private static readonly Color ButtonColor = new Color(0.22f, 0.18f, 0.13f, 0.98f);

		private static readonly Color GoldColor = new Color(0.91f, 0.75f, 0.42f, 1f);

		private static readonly Color IvoryColor = new Color(0.95f, 0.91f, 0.82f, 1f);

		private static readonly Color MutedColor = new Color(0.69f, 0.67f, 0.62f, 1f);

		private readonly Action<int> _cycle;

		private VisualElement _root;

		private Label _name;

		private Label _hint;

		private Button _previous;

		private Button _next;

		private Hud _attachedHud;

		public SpectatorHud(Action<int> cycle)
		{
			_cycle = cycle;
		}

		public bool Show(string playerName, int index, int count)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if (!EnsureAttached())
			{
				return false;
			}
			_root.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)0);
			_root.BringToFront();
			bool flag = count > 0;
			((VisualElement)_previous).SetEnabled(flag);
			((VisualElement)_next).SetEnabled(flag);
			if (!flag)
			{
				SetText(_name, "NO LIVING DWARFS");
				SetText(_hint, "Waiting for a teammate");
			}
			else if (index < 0)
			{
				SetText(_name, "SELECT A DWARF");
				SetText(_hint, count + " alive  |  LMB / RMB  |  Hold ALT to click");
			}
			else
			{
				SetText(_name, ShortName(playerName) + "  (" + (index + 1) + "/" + count + ")");
				SetText(_hint, "LMB / RMB  |  Hold ALT to click");
			}
			return true;
		}

		public void Hide()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (_root != null)
			{
				_root.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
			}
		}

		public void Dispose()
		{
			if (_root != null)
			{
				_root.RemoveFromHierarchy();
			}
			_root = null;
			_attachedHud = null;
		}

		private bool EnsureAttached()
		{
			Hud instance = Hud.Instance;
			UIDocument val = (((Object)(object)instance != (Object)null) ? instance.hudDocument : null);
			VisualElement val2 = (((Object)(object)val != (Object)null) ? val.rootVisualElement : null);
			if (val2 == null)
			{
				Hide();
				return false;
			}
			if (_root == null)
			{
				Build();
			}
			if ((Object)(object)_attachedHud != (Object)(object)instance || _root.panel == null)
			{
				_root.RemoveFromHierarchy();
				val2.Add(_root);
				_attachedHud = instance;
			}
			return true;
		}

		private void Build()
		{
			//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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Expected O, but got Unknown
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Expected O, but got Unknown
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_030c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0311: Unknown result type (might be due to invalid IL or missing references)
			//IL_0319: Expected O, but got Unknown
			//IL_0324: Unknown result type (might be due to invalid IL or missing references)
			//IL_0335: Unknown result type (might be due to invalid IL or missing references)
			//IL_0345: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Unknown result type (might be due to invalid IL or missing references)
			//IL_035b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0370: Unknown result type (might be due to invalid IL or missing references)
			//IL_0387: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0398: Expected O, but got Unknown
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03be: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0408: Unknown result type (might be due to invalid IL or missing references)
			//IL_0424: Unknown result type (might be due to invalid IL or missing references)
			//IL_0429: Unknown result type (might be due to invalid IL or missing references)
			//IL_0435: Expected O, but got Unknown
			//IL_0445: Unknown result type (might be due to invalid IL or missing references)
			//IL_045a: Unknown result type (might be due to invalid IL or missing references)
			//IL_045f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0475: Unknown result type (might be due to invalid IL or missing references)
			_root = new VisualElement
			{
				name = "SpectatorSwitchRoot",
				pickingMode = (PickingMode)1
			};
			_root.style.position = StyleEnum<Position>.op_Implicit((Position)1);
			_root.style.left = StyleLength.op_Implicit(0f);
			_root.style.right = StyleLength.op_Implicit(0f);
			_root.style.bottom = StyleLength.op_Implicit(96f);
			_root.style.height = StyleLength.op_Implicit(94f);
			_root.style.alignItems = StyleEnum<Align>.op_Implicit((Align)2);
			_root.style.justifyContent = StyleEnum<Justify>.op_Implicit((Justify)1);
			_root.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
			VisualElement val = new VisualElement
			{
				name = "SpectatorSwitchPanel",
				pickingMode = (PickingMode)1
			};
			val.style.width = StyleLength.op_Implicit(468f);
			val.style.height = StyleLength.op_Implicit(80f);
			val.style.flexDirection = StyleEnum<FlexDirection>.op_Implicit((FlexDirection)2);
			val.style.alignItems = StyleEnum<Align>.op_Implicit((Align)2);
			val.style.justifyContent = StyleEnum<Justify>.op_Implicit((Justify)4);
			val.style.backgroundColor = StyleColor.op_Implicit(PanelColor);
			val.style.borderTopWidth = StyleFloat.op_Implicit(2f);
			val.style.borderRightWidth = StyleFloat.op_Implicit(2f);
			val.style.borderBottomWidth = StyleFloat.op_Implicit(2f);
			val.style.borderLeftWidth = StyleFloat.op_Implicit(2f);
			val.style.borderTopColor = StyleColor.op_Implicit(BorderColor);
			val.style.borderRightColor = StyleColor.op_Implicit(BorderColor);
			val.style.borderBottomColor = StyleColor.op_Implicit(BorderColor);
			val.style.borderLeftColor = StyleColor.op_Implicit(BorderColor);
			val.style.borderTopLeftRadius = StyleLength.op_Implicit(5f);
			val.style.borderTopRightRadius = StyleLength.op_Implicit(5f);
			val.style.borderBottomLeftRadius = StyleLength.op_Implicit(5f);
			val.style.borderBottomRightRadius = StyleLength.op_Implicit(5f);
			_root.Add(val);
			_previous = CreateArrow("<", "Previous living dwarf");
			_previous.clicked += delegate
			{
				_cycle(-1);
			};
			val.Add((VisualElement)(object)_previous);
			VisualElement val2 = new VisualElement
			{
				pickingMode = (PickingMode)1
			};
			val2.style.width = StyleLength.op_Implicit(330f);
			val2.style.flexShrink = StyleFloat.op_Implicit(1f);
			val2.style.alignItems = StyleEnum<Align>.op_Implicit((Align)2);
			val2.style.justifyContent = StyleEnum<Justify>.op_Implicit((Justify)1);
			val.Add(val2);
			Label val3 = new Label("SPECTATING")
			{
				pickingMode = (PickingMode)1
			};
			((VisualElement)val3).style.fontSize = StyleLength.op_Implicit(10f);
			((VisualElement)val3).style.unityFontStyleAndWeight = StyleEnum<FontStyle>.op_Implicit((FontStyle)1);
			((VisualElement)val3).style.color = StyleColor.op_Implicit(GoldColor);
			((VisualElement)val3).style.unityTextAlign = StyleEnum<TextAnchor>.op_Implicit((TextAnchor)4);
			((VisualElement)val3).style.marginBottom = StyleLength.op_Implicit(1f);
			val2.Add((VisualElement)(object)val3);
			_name = new Label("SELECT A DWARF")
			{
				pickingMode = (PickingMode)1
			};
			((VisualElement)_name).style.fontSize = StyleLength.op_Implicit(17f);
			((VisualElement)_name).style.unityFontStyleAndWeight = StyleEnum<FontStyle>.op_Implicit((FontStyle)1);
			((VisualElement)_name).style.color = StyleColor.op_Implicit(IvoryColor);
			((VisualElement)_name).style.unityTextAlign = StyleEnum<TextAnchor>.op_Implicit((TextAnchor)4);
			((VisualElement)_name).style.marginBottom = StyleLength.op_Implicit(2f);
			val2.Add((VisualElement)(object)_name);
			_hint = new Label("LMB / RMB  |  Hold ALT to click")
			{
				pickingMode = (PickingMode)1
			};
			((VisualElement)_hint).style.fontSize = StyleLength.op_Implicit(11f);
			((VisualElement)_hint).style.color = StyleColor.op_Implicit(MutedColor);
			((VisualElement)_hint).style.unityTextAlign = StyleEnum<TextAnchor>.op_Implicit((TextAnchor)4);
			val2.Add((VisualElement)(object)_hint);
			_next = CreateArrow(">", "Next living dwarf");
			_next.clicked += delegate
			{
				_cycle(1);
			};
			val.Add((VisualElement)(object)_next);
		}

		private static Button CreateArrow(string symbol, string tooltip)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: 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_0097: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			Button button = new Button
			{
				text = symbol,
				tooltip = tooltip
			};
			((VisualElement)button).style.width = StyleLength.op_Implicit(47f);
			((VisualElement)button).style.height = StyleLength.op_Implicit(47f);
			((VisualElement)button).style.flexShrink = StyleFloat.op_Implicit(0f);
			((VisualElement)button).style.backgroundColor = StyleColor.op_Implicit(ButtonColor);
			((VisualElement)button).style.color = StyleColor.op_Implicit(GoldColor);
			((VisualElement)button).style.fontSize = StyleLength.op_Implicit(27f);
			((VisualElement)button).style.unityFontStyleAndWeight = StyleEnum<FontStyle>.op_Implicit((FontStyle)1);
			((VisualElement)button).style.unityTextAlign = StyleEnum<TextAnchor>.op_Implicit((TextAnchor)4);
			((VisualElement)button).style.borderTopWidth = StyleFloat.op_Implicit(1f);
			((VisualElement)button).style.borderRightWidth = StyleFloat.op_Implicit(1f);
			((VisualElement)button).style.borderBottomWidth = StyleFloat.op_Implicit(1f);
			((VisualElement)button).style.borderLeftWidth = StyleFloat.op_Implicit(1f);
			((VisualElement)button).style.borderTopColor = StyleColor.op_Implicit(BorderColor);
			((VisualElement)button).style.borderRightColor = StyleColor.op_Implicit(BorderColor);
			((VisualElement)button).style.borderBottomColor = StyleColor.op_Implicit(BorderColor);
			((VisualElement)button).style.borderLeftColor = StyleColor.op_Implicit(BorderColor);
			((CallbackEventHandler)button).RegisterCallback<PointerEnterEvent>((EventCallback<PointerEnterEvent>)delegate
			{
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Unknown result type (might be due to invalid IL or missing references)
				if (((VisualElement)button).enabledInHierarchy)
				{
					((VisualElement)button).style.backgroundColor = StyleColor.op_Implicit(new Color(0.35f, 0.27f, 0.17f, 0.98f));
				}
			}, (TrickleDown)0);
			((CallbackEventHandler)button).RegisterCallback<PointerLeaveEvent>((EventCallback<PointerLeaveEvent>)delegate
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				((VisualElement)button).style.backgroundColor = StyleColor.op_Implicit(ButtonColor);
			}, (TrickleDown)0);
			return button;
		}

		private static void SetText(Label label, string text)
		{
			if (((TextElement)label).text != text)
			{
				((TextElement)label).text = text;
			}
		}

		private static string ShortName(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return "DWARF";
			}
			if (name.Length <= 26)
			{
				return name;
			}
			return name.Substring(0, 25) + "…";
		}
	}
	[BepInPlugin("com.thereisnouser.stonewards.spectatorswitch", "Stonewards Spectator Switch", "0.1.1")]
	public sealed class SpectatorSwitchPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.thereisnouser.stonewards.spectatorswitch";

		public const string PluginName = "Stonewards Spectator Switch";

		public const string PluginVersion = "0.1.1";

		private readonly List<FirstPersonController> _alivePlayers = new List<FirstPersonController>(4);

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _orbitDistance;

		private ConfigEntry<float> _eyeHeight;

		private ConfigEntry<bool> _autoAdvance;

		private ConfigEntry<bool> _showButtons;

		private SpectatorHud _hud;

		private bool _cursorBorrowed;

		private CursorLockMode _savedCursorLock;

		private bool _savedCursorVisible;

		private FirstPersonController _target;

		private FirstPersonController _lastLocalPlayer;

		private Camera _renderCamera;

		private Camera _overriddenCamera;

		private Vector3 _originalPosition;

		private bool _dead;

		private bool _wasDead;

		private void Awake()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Allow left/right mouse clicks to cycle through living teammates while dead.");
			_autoAdvance = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoAdvanceWhenTargetDies", true, "Automatically select another living teammate if the current spectator target dies or leaves.");
			_orbitDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "OrbitDistance", 0f, new ConfigDescription("Optional pullback along your current view direction (0 = player eye position).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 8f), Array.Empty<object>()));
			_eyeHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Camera", "EyeHeight", 1.55f, new ConfigDescription("Camera eye height relative to the selected player's transform.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 3f), Array.Empty<object>()));
			_showButtons = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "ShowButtons", true, "Show clickable spectator buttons in the HUD while dead.");
			_hud = new SpectatorHud(CycleFromButton);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Spectator Switch v0.1.1 initialized (client-side). LMB: next, RMB: previous; hold ALT to click spectator buttons.");
		}

		private void OnEnable()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			Camera.onPreCull = (CameraCallback)Delegate.Combine((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(OnCameraPreCull));
			Camera.onPostRender = (CameraCallback)Delegate.Combine((Delegate?)(object)Camera.onPostRender, (Delegate?)new CameraCallback(OnCameraPostRender));
			RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
			RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
		}

		private void Update()
		{
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Invalid comparison between Unknown and I4
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Invalid comparison between Unknown and I4
			if (_enabled == null || !_enabled.Value)
			{
				ResetSpectator();
				return;
			}
			GameManager instance = GameManager.Instance;
			FirstPersonController val = (((Object)(object)instance != (Object)null) ? instance.LocalPlayer : null);
			_dead = (Object)(object)val != (Object)null && (Object)(object)val.PlayerStats != (Object)null && val.PlayerStats.isDead;
			if ((Object)(object)val != (Object)(object)_lastLocalPlayer)
			{
				ResetSpectator();
				_lastLocalPlayer = val;
				_dead = (Object)(object)val != (Object)null && (Object)(object)val.PlayerStats != (Object)null && val.PlayerStats.isDead;
			}
			if (!_dead)
			{
				if (_wasDead || (Object)(object)_target != (Object)null)
				{
					ResetSpectator();
				}
				_wasDead = false;
				return;
			}
			_wasDead = true;
			RefreshPlayers();
			if ((Object)(object)_target != (Object)null && !_alivePlayers.Contains(_target))
			{
				_target = null;
				if (_autoAdvance.Value)
				{
					Cycle(1);
				}
			}
			bool flag = (Object)(object)InputManager.Instance != (Object)null && (int)InputManager.Instance.CurrentInputState == 4;
			bool flag2 = false;
			if (flag && _showButtons.Value && _hud != null)
			{
				flag2 = _hud.Show(((Object)(object)_target != (Object)null) ? _target.playerName : null, _alivePlayers.IndexOf(_target), _alivePlayers.Count);
			}
			else
			{
				_hud?.Hide();
			}
			UpdateCursorForButtons(flag && flag2);
			if (flag && Mouse.current != null && (!Cursor.visible || (int)Cursor.lockState == 1))
			{
				if (Mouse.current.leftButton.wasPressedThisFrame)
				{
					Cycle(1);
				}
				else if (Mouse.current.rightButton.wasPressedThisFrame)
				{
					Cycle(-1);
				}
			}
		}

		private void CycleFromButton(int direction)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Invalid comparison between Unknown and I4
			if (_enabled != null && _enabled.Value && _dead && !((Object)(object)InputManager.Instance == (Object)null) && (int)InputManager.Instance.CurrentInputState == 4)
			{
				Cycle(direction);
			}
		}

		private void UpdateCursorForButtons(bool canClick)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			Keyboard current = Keyboard.current;
			bool flag = current != null && (((ButtonControl)current[(Key)53]).isPressed || ((ButtonControl)current[(Key)54]).isPressed);
			if (!canClick || !flag)
			{
				ReleaseCursorOverride();
				return;
			}
			if (!_cursorBorrowed)
			{
				_savedCursorLock = Cursor.lockState;
				_savedCursorVisible = Cursor.visible;
				_cursorBorrowed = true;
			}
			Cursor.lockState = (CursorLockMode)0;
			Cursor.visible = true;
		}

		private void ReleaseCursorOverride()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between Unknown and I4
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			if (_cursorBorrowed)
			{
				_cursorBorrowed = false;
				InputManager instance = InputManager.Instance;
				if (!((Object)(object)instance == (Object)null) && ((int)instance.CurrentInputState == 4 || (int)instance.CurrentInputState == 2) && (int)Cursor.lockState == 0 && Cursor.visible)
				{
					Cursor.lockState = _savedCursorLock;
					Cursor.visible = _savedCursorVisible;
				}
			}
		}

		private void RefreshPlayers()
		{
			_alivePlayers.Clear();
			List<FirstPersonController> localPlayers = FirstPersonController.LocalPlayers;
			if (localPlayers == null)
			{
				return;
			}
			for (int i = 0; i < localPlayers.Count; i++)
			{
				FirstPersonController val = localPlayers[i];
				if ((Object)(object)val != (Object)null && !((NetworkBehaviour)val).isLocalPlayer && (Object)(object)val.PlayerStats != (Object)null && !val.PlayerStats.isDead && ((Component)val).gameObject.activeInHierarchy)
				{
					_alivePlayers.Add(val);
				}
			}
			_alivePlayers.Sort((FirstPersonController a, FirstPersonController b) => ((NetworkBehaviour)a).netId.CompareTo(((NetworkBehaviour)b).netId));
		}

		private void Cycle(int direction)
		{
			RefreshPlayers();
			if (_alivePlayers.Count == 0)
			{
				_target = null;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Spectator: no living teammates to watch.");
				return;
			}
			int num = _alivePlayers.IndexOf(_target);
			int num2 = ((num >= 0) ? ((num + direction + _alivePlayers.Count) % _alivePlayers.Count) : ((direction <= 0) ? (_alivePlayers.Count - 1) : 0));
			_target = _alivePlayers[num2];
			_renderCamera = null;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Spectating " + _target.playerName + " (" + (num2 + 1) + "/" + _alivePlayers.Count + ")."));
		}

		private void OnCameraPreCull(Camera camera)
		{
			TryOverrideCamera(camera);
		}

		private void OnCameraPostRender(Camera camera)
		{
			RestoreCamera(camera);
		}

		private void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
		{
			TryOverrideCamera(camera);
		}

		private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera)
		{
			RestoreCamera(camera);
		}

		private void TryOverrideCamera(Camera camera)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Invalid comparison between Unknown and I4
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			if (!_dead || (Object)(object)_target == (Object)null || (Object)(object)camera == (Object)null || !((Behaviour)camera).enabled || _enabled == null || !_enabled.Value || (Object)(object)_overriddenCamera != (Object)null || (int)camera.cameraType != 1 || (Object)(object)camera.targetTexture != (Object)null)
			{
				return;
			}
			if ((Object)(object)_renderCamera == (Object)null || !((Behaviour)_renderCamera).isActiveAndEnabled)
			{
				_renderCamera = FindMainGameCamera();
			}
			if ((Object)(object)camera != (Object)(object)_renderCamera)
			{
				return;
			}
			FirstPersonController target = _target;
			if (!((Object)(object)target == (Object)null) && !((Object)(object)target.PlayerStats == (Object)null) && !target.PlayerStats.isDead)
			{
				Transform transform = ((Component)target).transform;
				Vector3 val = transform.position + Vector3.up * _eyeHeight.Value;
				Vector3 val2 = val - ((Component)camera).transform.forward * _orbitDistance.Value;
				RaycastHit val3 = default(RaycastHit);
				if (_orbitDistance.Value > 0.05f && Physics.Linecast(val, val2, ref val3, -1, (QueryTriggerInteraction)1) && (Object)(object)((RaycastHit)(ref val3)).collider != (Object)null && !((Component)((RaycastHit)(ref val3)).collider).transform.IsChildOf(transform))
				{
					Vector3 val4 = val - val2;
					Vector3 normalized = ((Vector3)(ref val4)).normalized;
					val2 = ((RaycastHit)(ref val3)).point + normalized * 0.2f;
				}
				_originalPosition = ((Component)camera).transform.position;
				_overriddenCamera = camera;
				((Component)camera).transform.position = val2;
			}
		}

		private static Camera FindMainGameCamera()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Invalid comparison between Unknown and I4
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Invalid comparison between Unknown and I4
			Camera main = Camera.main;
			if ((Object)(object)main != (Object)null && ((Behaviour)main).isActiveAndEnabled && (Object)(object)main.targetTexture == (Object)null)
			{
				return main;
			}
			Camera[] allCameras = Camera.allCameras;
			foreach (Camera val in allCameras)
			{
				if ((Object)(object)val != (Object)null && ((Behaviour)val).isActiveAndEnabled && (int)val.cameraType == 1 && (Object)(object)val.targetTexture == (Object)null && ((Object)val).name.IndexOf("main", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return val;
				}
			}
			foreach (Camera val2 in allCameras)
			{
				if ((Object)(object)val2 != (Object)null && ((Behaviour)val2).isActiveAndEnabled && (int)val2.cameraType == 1 && (Object)(object)val2.targetTexture == (Object)null)
				{
					return val2;
				}
			}
			return null;
		}

		private void RestoreCamera(Camera camera)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)camera == (Object)null) && !((Object)(object)camera != (Object)(object)_overriddenCamera))
			{
				((Component)camera).transform.position = _originalPosition;
				_overriddenCamera = null;
			}
		}

		private void ResetSpectator()
		{
			ReleaseCursorOverride();
			_hud?.Hide();
			if ((Object)(object)_overriddenCamera != (Object)null)
			{
				RestoreCamera(_overriddenCamera);
			}
			_target = null;
			_renderCamera = null;
			_dead = false;
			_wasDead = false;
			_alivePlayers.Clear();
		}

		private void OnDisable()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			Camera.onPreCull = (CameraCallback)Delegate.Remove((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(OnCameraPreCull));
			Camera.onPostRender = (CameraCallback)Delegate.Remove((Delegate?)(object)Camera.onPostRender, (Delegate?)new CameraCallback(OnCameraPostRender));
			RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
			RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
			ResetSpectator();
		}

		private void OnDestroy()
		{
			ResetSpectator();
			_hud?.Dispose();
		}
	}
}