Decompiled source of SBG PiPBallCam v1.1.7

SBG-PiPBallCam.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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.1.6.0")]
[assembly: AssemblyInformationalVersion("1.1.6+5a04ee1b548d7c42494e5d0a53c844603b9b04f1")]
[assembly: AssemblyProduct("Picture-in-Picture Ball Camera")]
[assembly: AssemblyTitle("SBG-PiPBallCam")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.6.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.6")]
public class BallCamMod : BaseUnityPlugin
{
	private Camera _ballCam;

	private GameObject _ballCamObj;

	private const float PipWidth = 0.2f;

	private const float PipHeight = 0.2f;

	private ConfigEntry<float> _pipPosX;

	private ConfigEntry<float> _pipPosY;

	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_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		_pipPosX = ((BaseUnityPlugin)this).Config.Bind<float>("PiP", "PositionX", 0.79f, new ConfigDescription("PiP X position (0.0 to 1.0). Values are clamped to keep PiP on screen.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		_pipPosY = ((BaseUnityPlugin)this).Config.Bind<float>("PiP", "PositionY", 0.25f, new ConfigDescription("PiP Y position (0.0 to 1.0). Values are clamped to keep PiP on screen.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		_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();
		string[] array = new string[3] { "Username", "Name", "playerName" };
		string[] array2 = array;
		foreach (string name in array2)
		{
			PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null)
			{
				return property.GetValue(info)?.ToString() ?? "Unknown";
			}
			FieldInfo field = type.GetField(name, 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[] array3 = new string[5] { "PlayerName", "Username", "_playerName", "networkedPlayerName", "playerName" };
			string[] array4 = array3;
			foreach (string name2 in array4)
			{
				PropertyInfo property2 = type2.GetProperty(name2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (property2 != null)
				{
					return property2.GetValue(component)?.ToString() ?? "Unknown";
				}
				FieldInfo field2 = type2.GetField(name2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field2 != null)
				{
					return field2.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_0047: 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)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: 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;
		}
		GolfBall val = DetermineTargetBall();
		if ((Object)(object)val != (Object)null)
		{
			Vector3 ballVelocity = GetBallVelocity(val.Rigidbody);
			float magnitude = ((Vector3)(ref ballVelocity)).magnitude;
			if (magnitude > _movementThreshold || Time.time - _lastMoveTime < _lingerTime)
			{
				if (magnitude > _movementThreshold)
				{
					_lastMoveTime = Time.time;
				}
				ShowPip(val);
			}
			else
			{
				ClosePip();
				_lastBallPos = Vector3.zero;
			}
		}
		else
		{
			ClosePip();
		}
	}

	private GolfBall DetermineTargetBall()
	{
		PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
		if ((Object)(object)localPlayerInfo == (Object)null)
		{
			return null;
		}
		PlayerSpectator component = ((Component)localPlayerInfo).GetComponent<PlayerSpectator>();
		if ((Object)(object)component != (Object)null && component.IsSpectating)
		{
			if ((Object)(object)component.TargetBall != (Object)null)
			{
				_currentTargetName = "Spectated Ball";
				return component.TargetBall;
			}
			if ((Object)(object)component.TargetPlayer != (Object)null)
			{
				_currentTargetName = GetSafeName(component.TargetPlayer);
				PlayerGolfer asGolfer = GetAsGolfer(component.TargetPlayer);
				return (asGolfer != null) ? asGolfer.OwnBall : null;
			}
		}
		_currentTargetName = GetSafeName(localPlayerInfo);
		PlayerGolfer asGolfer2 = GetAsGolfer(localPlayerInfo);
		return (asGolfer2 != null) ? asGolfer2.OwnBall : null;
	}

	private void ShowPip(GolfBall ball)
	{
		//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_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: 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_00e9: 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_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: 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_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: 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_0153: 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_0162: 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_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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_009c: 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_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: 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;
		}
		ApplyClampedPipRect();
		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 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
		_ballCamObj = new GameObject("PiPBallCamera");
		_ballCam = _ballCamObj.AddComponent<Camera>();
		ApplyClampedPipRect();
		_ballCam.depth = 999f;
		_ballCam.cullingMask = -33;
		Object.DontDestroyOnLoad((Object)(object)_ballCamObj);
	}

	private void ApplyClampedPipRect()
	{
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_ballCam == (Object)null))
		{
			float num = Mathf.Max(Screen.width, 1);
			float num2 = Mathf.Max(Screen.height, 1);
			float num3 = num * 0.2f;
			float num4 = num2 * 0.2f;
			float num5 = (_pipPosX?.Value ?? 0.79f) * num;
			float num6 = (_pipPosY?.Value ?? 0.25f) * num2;
			float num7 = Mathf.Clamp(num5, 0f, Mathf.Max(0f, num - num3));
			float num8 = Mathf.Clamp(num6, 0f, Mathf.Max(0f, num2 - num4));
			float num9 = num7 / num;
			float num10 = num8 / num2;
			if (_pipPosX != null && !Mathf.Approximately(_pipPosX.Value, num9))
			{
				_pipPosX.Value = num9;
			}
			if (_pipPosY != null && !Mathf.Approximately(_pipPosY.Value, num10))
			{
				_pipPosY.Value = num10;
			}
			_ballCam.rect = new Rect(num9, num10, 0.2f, 0.2f);
		}
	}
}
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.1.6";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}