Decompiled source of SBG PiPBallCam v1.1.3

SBG-PiPBallCam.dll

Decompiled 20 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 Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[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-PiPBallCam")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0")]
[assembly: AssemblyProduct("Picture-in-Picture Ball Camera")]
[assembly: AssemblyTitle("SBG-PiPBallCam")]
[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;
		}
	}
}
[BepInPlugin("com.kingcox.sbg.pipballcam", "SBG PiP Ball Cam", "1.1.1")]
public class BallCamMod : BaseUnityPlugin
{
	private Camera _ballCam;

	private GameObject _ballCamObj;

	private float _smoothSpeed = 10f;

	private float _movementThreshold = 0.02f;

	private float _lingerTime = 1.5f;

	private float _lastMoveTime;

	private string _currentTargetName = "";

	private Vector3 _lastBallPos;

	private Vector3 _stableDirection = Vector3.back;

	private GUIStyle _labelStyle;

	private void Awake()
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		_labelStyle = new GUIStyle();
		_labelStyle.alignment = (TextAnchor)1;
		_labelStyle.normal.textColor = Color.white;
		_labelStyle.fontStyle = (FontStyle)1;
		_labelStyle.fontSize = 16;
	}

	private string GetSafeName(PlayerInfo info)
	{
		if ((Object)(object)info == (Object)null)
		{
			return "Unknown";
		}
		Type type = ((object)info).GetType();
		PropertyInfo property = type.GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);
		if (property != null)
		{
			return property.GetValue(info)?.ToString() ?? "Unknown";
		}
		FieldInfo field = type.GetField("playerName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field != null)
		{
			return field.GetValue(info)?.ToString() ?? "Unknown";
		}
		Component component = ((Component)info).GetComponent("PlayerId");
		if ((Object)(object)component != (Object)null)
		{
			Type type2 = ((object)component).GetType();
			string[] array = new string[3] { "_playerName", "networkedPlayerName", "playerName" };
			string[] array2 = array;
			foreach (string name in array2)
			{
				FieldInfo field2 = type2.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field2 != null)
				{
					return field2.GetValue(component)?.ToString() ?? "Unknown";
				}
				PropertyInfo property2 = type2.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
				if (property2 != null)
				{
					return property2.GetValue(component)?.ToString() ?? "Unknown";
				}
			}
		}
		return "Golfer";
	}

	private PlayerGolfer GetAsGolfer(PlayerInfo info)
	{
		if ((Object)(object)info == (Object)null)
		{
			return null;
		}
		object? obj = ((object)info).GetType().GetProperty("AsGolfer", BindingFlags.Instance | BindingFlags.Public)?.GetValue(info);
		return (PlayerGolfer)((obj is PlayerGolfer) ? obj : null);
	}

	private object GetAsSpectator(PlayerInfo info)
	{
		if ((Object)(object)info == (Object)null)
		{
			return null;
		}
		return ((object)info).GetType().GetProperty("AsSpectator", BindingFlags.Instance | BindingFlags.Public)?.GetValue(info);
	}

	private Vector3 GetBallVelocity(Rigidbody rb)
	{
		//IL_000c: 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_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)rb == (Object)null)
		{
			return Vector3.zero;
		}
		PropertyInfo propertyInfo = ((object)rb).GetType().GetProperty("linearVelocity") ?? ((object)rb).GetType().GetProperty("velocity");
		if (propertyInfo != null)
		{
			return (Vector3)propertyInfo.GetValue(rb);
		}
		return Vector3.zero;
	}

	private void Update()
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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)
		if ((Object)(object)Camera.main == (Object)null || (Object)(object)GameManager.LocalPlayerInfo == (Object)null)
		{
			ClosePip();
			return;
		}
		PlayerInfo actualSpectatorTarget = GetActualSpectatorTarget();
		PlayerGolfer asGolfer = GetAsGolfer(actualSpectatorTarget);
		if ((Object)(object)asGolfer != (Object)null && (Object)(object)asGolfer.OwnBall != (Object)null)
		{
			GolfBall ownBall = asGolfer.OwnBall;
			_currentTargetName = GetSafeName(actualSpectatorTarget);
			Vector3 ballVelocity = GetBallVelocity(ownBall.Rigidbody);
			float magnitude = ((Vector3)(ref ballVelocity)).magnitude;
			if (magnitude > _movementThreshold || Time.time - _lastMoveTime < _lingerTime)
			{
				if (magnitude > _movementThreshold)
				{
					_lastMoveTime = Time.time;
				}
				ShowPip(ownBall);
			}
			else
			{
				ClosePip();
				_lastBallPos = Vector3.zero;
			}
		}
		else
		{
			ClosePip();
		}
	}

	private void ShowPip(GolfBall ball)
	{
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: 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_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: 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_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: 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_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: 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_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: 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_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: 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_0089: 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_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: 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)
		if ((Object)(object)_ballCamObj == (Object)null)
		{
			CreateBallCamera();
		}
		if (!_ballCamObj.activeSelf)
		{
			_ballCamObj.SetActive(true);
		}
		if ((Object)(object)_ballCam != (Object)null)
		{
			((Behaviour)_ballCam).enabled = true;
		}
		Vector3 position = ((Component)ball).transform.position;
		if (_lastBallPos != Vector3.zero)
		{
			Vector3 val = position - _lastBallPos;
			Vector3 val2 = default(Vector3);
			((Vector3)(ref val2))..ctor(val.x, 0f, val.z);
			if (((Vector3)(ref val2)).magnitude > 0.01f)
			{
				_stableDirection = Vector3.Slerp(_stableDirection, ((Vector3)(ref val2)).normalized, Time.deltaTime * 5f);
			}
		}
		_lastBallPos = position;
		Vector3 val3 = position - _stableDirection * 3f + Vector3.up * 1.2f;
		((Component)_ballCam).transform.position = Vector3.Lerp(((Component)_ballCam).transform.position, val3, Time.deltaTime * _smoothSpeed);
		((Component)_ballCam).transform.LookAt(position + Vector3.up * 0.15f);
	}

	private PlayerInfo GetActualSpectatorTarget()
	{
		PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
		object asSpectator = GetAsSpectator(localPlayerInfo);
		if (asSpectator == null)
		{
			return localPlayerInfo;
		}
		try
		{
			if (!(bool)(asSpectator.GetType().GetProperty("IsSpectating")?.GetValue(asSpectator) ?? ((object)false)))
			{
				return localPlayerInfo;
			}
			FieldInfo field = asSpectator.GetType().GetField("TargetPlayer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				object? value = field.GetValue(asSpectator);
				PlayerInfo val = (PlayerInfo)((value is PlayerInfo) ? value : null);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
		}
		catch
		{
		}
		Text[] array = Object.FindObjectsByType<Text>((FindObjectsSortMode)0);
		Text[] array2 = array;
		foreach (Text val2 in array2)
		{
			if (((Object)((Component)val2).gameObject).name.Contains("Name") && !string.IsNullOrEmpty(val2.text))
			{
				string viewingName = val2.text;
				PlayerInfo val3 = ((IEnumerable<PlayerInfo>)Object.FindObjectsByType<PlayerInfo>((FindObjectsSortMode)0)).FirstOrDefault((Func<PlayerInfo, bool>)((PlayerInfo p) => GetSafeName(p) == viewingName));
				if ((Object)(object)val3 != (Object)null)
				{
					return val3;
				}
			}
		}
		return localPlayerInfo;
	}

	private void ClosePip()
	{
		if ((Object)(object)_ballCamObj != (Object)null)
		{
			_ballCamObj.SetActive(false);
			if ((Object)(object)_ballCam != (Object)null)
			{
				((Behaviour)_ballCam).enabled = false;
			}
		}
		_currentTargetName = "";
	}

	private void OnGUI()
	{
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Expected O, but got Unknown
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: 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_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_ballCamObj == (Object)null) && _ballCamObj.activeInHierarchy && !((Object)(object)_ballCam == (Object)null) && ((Behaviour)_ballCam).enabled && !string.IsNullOrEmpty(_currentTargetName) && _labelStyle != null)
		{
			Rect pixelRect = _ballCam.pixelRect;
			float x = ((Rect)(ref pixelRect)).x;
			float num = (float)Screen.height - ((Rect)(ref pixelRect)).yMax + 5f;
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(x, num, ((Rect)(ref pixelRect)).width, 5f);
			string text = "<b>" + _currentTargetName + "</b>";
			GUIStyle val2 = new GUIStyle(_labelStyle);
			val2.normal.textColor = Color.black;
			float num2 = 2f;
			GUI.Label(new Rect(((Rect)(ref val)).x - num2, ((Rect)(ref val)).y - num2, ((Rect)(ref val)).width, ((Rect)(ref val)).height), text, val2);
			GUI.Label(new Rect(((Rect)(ref val)).x + num2, ((Rect)(ref val)).y - num2, ((Rect)(ref val)).width, ((Rect)(ref val)).height), text, val2);
			GUI.Label(new Rect(((Rect)(ref val)).x - num2, ((Rect)(ref val)).y + num2, ((Rect)(ref val)).width, ((Rect)(ref val)).height), text, val2);
			GUI.Label(new Rect(((Rect)(ref val)).x + num2, ((Rect)(ref val)).y + num2, ((Rect)(ref val)).width, ((Rect)(ref val)).height), text, val2);
			GUI.Label(val, text, _labelStyle);
		}
	}

	private void CreateBallCamera()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		_ballCamObj = new GameObject("PiPBallCamera");
		_ballCam = _ballCamObj.AddComponent<Camera>();
		float num = 0.2f;
		float num2 = 0.2f;
		float num3 = 0.79f;
		float num4 = 0.25f;
		_ballCam.rect = new Rect(num3, num4, num, num2);
		_ballCam.depth = 999f;
		_ballCam.cullingMask = -33;
		Object.DontDestroyOnLoad((Object)(object)_ballCamObj);
	}
}
namespace PiPBallCam
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SBG-PiPBallCam";

		public const string PLUGIN_NAME = "Picture-in-Picture Ball Camera";

		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)
		{
		}
	}
}