Decompiled source of SBG AutoSpectator v1.1.0

SBG-AutoSpectator.dll

Decompiled 13 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("GameAssembly")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SBG-AutoSpectator")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0")]
[assembly: AssemblyProduct("Auto Spectator")]
[assembly: AssemblyTitle("SBG-AutoSpectator")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SpectatorCamera
{
	[BepInPlugin("com.kingcox22.sbg.autospectator", "Auto Spectator", "1.0.1")]
	public class SpectatorCameraPlugin : BaseUnityPlugin
	{
		private static ConfigEntry<float> _configSpecInterval;

		private float _specTimer = 0f;

		private Transform _cachedHole;

		private OrbitCameraModule _cachedOrbit;

		private bool _isCustomSpectatorActive = false;

		private string _lastScene = "";

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			_configSpecInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Spectator", "Update Interval", 3f, "Seconds between camera updates.");
			PlayerSpectator.LocalPlayerIsSpectatingChanged += OnGameSpectateChanged;
			PlayerSpectator.LocalPlayerStoppedSpectating += OnGameSpectateStopped;
			Harmony val = new Harmony("com.kingcox22.sbg.autospectator");
			val.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Auto Spectator Loaded. Watching for Round Ends...");
		}

		private void Update()
		{
			//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)
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if (name != _lastScene)
			{
				if (name.Contains("DrivingRange") || name.Contains("Lobby"))
				{
					OnGameSpectateStopped();
				}
				_lastScene = name;
			}
			if (SingletonNetworkBehaviour<HoleOverviewCameraUi>.HasInstance && SingletonNetworkBehaviour<HoleOverviewCameraUi>.Instance.NetworkisVisible && _isCustomSpectatorActive)
			{
				OnGameSpectateStopped();
			}
			if (NetworkClient.active)
			{
				_specTimer += Time.deltaTime;
				if (_specTimer >= _configSpecInterval.Value)
				{
					_specTimer = 0f;
					CheckSpectatorStateManually();
					RefreshSpectatorLogic();
				}
			}
		}

		private void CheckSpectatorStateManually()
		{
			PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
			if ((Object)(object)localPlayerInfo != (Object)null && (Object)(object)localPlayerInfo.AsSpectator != (Object)null)
			{
				_isCustomSpectatorActive = localPlayerInfo.AsSpectator.IsSpectating;
			}
		}

		private void OnGameSpectateChanged()
		{
			PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
			if ((Object)(object)localPlayerInfo != (Object)null && (Object)(object)localPlayerInfo.AsSpectator != (Object)null)
			{
				_isCustomSpectatorActive = localPlayerInfo.AsSpectator.IsSpectating;
			}
		}

		private void OnGameSpectateStopped()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Round end or Manual Stop detected. Resetting camera to player.");
			_isCustomSpectatorActive = false;
			ResetCamera();
		}

		private void ResetCamera()
		{
			if ((Object)(object)_cachedOrbit == (Object)null)
			{
				_cachedOrbit = Object.FindFirstObjectByType<OrbitCameraModule>();
			}
			if ((Object)(object)_cachedOrbit != (Object)null)
			{
				PlayerGolfer val = ((IEnumerable<PlayerGolfer>)Object.FindObjectsByType<PlayerGolfer>((FindObjectsSortMode)0)).FirstOrDefault((Func<PlayerGolfer, bool>)((PlayerGolfer g) => ((NetworkBehaviour)g).isLocalPlayer));
				if ((Object)(object)val != (Object)null)
				{
					_cachedOrbit.subject = ((Component)val).transform;
				}
			}
			_cachedHole = null;
		}

		private void RefreshSpectatorLogic()
		{
			if (!_isCustomSpectatorActive)
			{
				return;
			}
			if ((Object)(object)_cachedHole == (Object)null)
			{
				GameObject val = GameObject.Find("Hole") ?? GameObject.Find("Main hole") ?? GameObject.FindWithTag("Hole");
				if ((Object)(object)val != (Object)null)
				{
					_cachedHole = val.transform;
				}
			}
			if ((Object)(object)_cachedOrbit == (Object)null)
			{
				_cachedOrbit = Object.FindFirstObjectByType<OrbitCameraModule>();
			}
			if ((Object)(object)_cachedHole == (Object)null || (Object)(object)_cachedOrbit == (Object)null)
			{
				return;
			}
			List<PlayerGolfer> list = (from g in Object.FindObjectsByType<PlayerGolfer>((FindObjectsSortMode)0)
				where (Object)(object)g != (Object)null && !((NetworkBehaviour)g).isLocalPlayer && ((Component)g).gameObject.activeInHierarchy
				select g).ToList();
			if (list.Count != 0)
			{
				PlayerGolfer val2 = list.OrderBy((PlayerGolfer g) => Vector3.Distance(((Component)g).transform.position, _cachedHole.position)).FirstOrDefault();
				if ((Object)(object)val2 != (Object)null && (Object)(object)_cachedOrbit.subject != (Object)(object)((Component)val2).transform)
				{
					_cachedOrbit.subject = ((Component)val2).transform;
				}
			}
		}

		private void LateUpdate()
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			if (_isCustomSpectatorActive && !((Object)(object)_cachedHole == (Object)null) && !((Object)(object)_cachedOrbit == (Object)null) && !((Object)(object)_cachedOrbit.subject == (Object)null))
			{
				Transform subject = _cachedOrbit.subject;
				Vector3 val = subject.position - _cachedHole.position;
				Vector3 normalized = ((Vector3)(ref val)).normalized;
				normalized.y = 0f;
				((Component)_cachedOrbit).transform.position = subject.position + normalized * 8.5f + Vector3.up * 4.5f;
				((Component)_cachedOrbit).transform.LookAt(_cachedHole.position + Vector3.up * 1.5f);
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SBG-AutoSpectator";

		public const string PLUGIN_NAME = "Auto Spectator";

		public const string PLUGIN_VERSION = "1.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}