Decompiled source of BetterSpectate v1.0.1

BepInEx/plugins/BetterSpectate/BetterSpectate.dll

Decompiled 7 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using BetterSpectate.Patches;
using BetterSpectate.SteamHelper;
using HarmonyLib;
using Photon.Realtime;
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: AssemblyTitle("BetterSpectate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterSpectate")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1d358671-9b8d-4212-904f-a592756f9482")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BetterSpectate
{
	[ContentWarningPlugin("funfoxrr.BetterSpectate", "1.0.0", false)]
	[BepInPlugin("funfoxrr.BetterSpectate", "BetterSpectate", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "funfoxrr.BetterSpectate";

		public const string NAME = "BetterSpectate";

		public const string VER = "1.0.0";

		private readonly Harmony harmony = new Harmony("funfoxrr.BetterSpectate");

		public static Plugin Instance;

		public static ManualLogSource Log;

		public static string assemblyLocation;

		public static AssetBundle assets;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Log = Logger.CreateLogSource("funfoxrr.BetterSpectate");
			assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			assets = AssetBundle.LoadFromFile(Path.Combine(assemblyLocation, "funfoxrr_betterspectate"));
			if ((Object)(object)assets == (Object)null)
			{
				Log.LogError((object)"Failed to load custom assets.");
				return;
			}
			Log.LogInfo((object)"Hey wsg bbgirl");
			Patch();
		}

		private void Patch()
		{
			harmony.PatchAll(typeof(SpectatePatch));
		}
	}
}
namespace BetterSpectate.SteamHelper
{
	internal class Utils
	{
		public static Sprite GetAvatar(Player plr)
		{
			Sprite result = default(Sprite);
			SteamAvatarHandler.TryGetAvatarForPlayer(plr, ref result);
			return result;
		}
	}
}
namespace BetterSpectate.Patches
{
	[HarmonyPatch(typeof(Spectate))]
	internal class SpectatePatch
	{
		private static GameObject panel;

		private static void AddPlayerToDeadPanel(Player player)
		{
			Transform val = panel.transform.Find("allDead");
			Transform val2 = val.Find("template");
			Transform val3 = Object.Instantiate<Transform>(val2, val);
			((Component)val3).gameObject.SetActive(true);
			((Component)val3).GetComponent<Image>().sprite = Utils.GetAvatar(player.refs.view.Owner);
			if ((double)player.data.microphoneValue >= 0.275)
			{
				Transform val4 = val3.Find("speak");
				((Component)val4).gameObject.SetActive(true);
			}
		}

		private static List<Player> getDeadPlayers()
		{
			List<Player> list = new List<Player>();
			List<Player> players = PlayerHandler.instance.players;
			for (int i = 0; i < players.Count; i++)
			{
				Player val = players[i];
				if (val.data.dead)
				{
					list.Add(val);
				}
			}
			return list;
		}

		private static void RefreshPlrList()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			Transform val = panel.transform.Find("allDead");
			foreach (Transform item in ((Component)val).transform)
			{
				Transform val2 = item;
				if (!(((Object)((Component)val2).gameObject).name == "template"))
				{
					Object.Destroy((Object)(object)((Component)val2).gameObject);
				}
			}
			for (int i = 0; i < getDeadPlayers().Count; i++)
			{
				Player player = getDeadPlayers()[i];
				AddPlayerToDeadPanel(player);
			}
		}

		[HarmonyPatch("StartSpectate")]
		[HarmonyPostfix]
		private static void StartPatch()
		{
			GameObject val = Plugin.assets.LoadAsset<GameObject>("SpectateUI");
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogFatal((object)"SpectateUI Panel is non-existant.");
				return;
			}
			panel = Object.Instantiate<GameObject>(val, GameObject.Find("GAME").transform);
			panel = ((Component)panel.transform.Find("Panel")).gameObject;
			for (int i = 0; i < getDeadPlayers().Count; i++)
			{
				Player player = getDeadPlayers()[i];
				AddPlayerToDeadPanel(player);
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePatch(Spectate __instance)
		{
			//IL_0050: 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)
			if (!((Object)(object)panel == (Object)null) && Spectate.spectating)
			{
				RefreshPlrList();
				Camera val = ((Component)__instance).gameObject.GetComponent<Camera>();
				if (!Object.op_Implicit((Object)(object)val))
				{
					val = ((Component)__instance).gameObject.GetComponentInChildren<Camera>();
				}
				if (Input.mouseScrollDelta.y > 0f)
				{
					Camera obj = val;
					Camera obj2 = val;
					float num2 = (obj2.fieldOfView -= 5f);
					obj.fieldOfView = Mathf.Clamp(num2, 25f, 180f);
				}
				if (Input.mouseScrollDelta.y < 0f)
				{
					Camera obj3 = val;
					Camera obj4 = val;
					float num2 = (obj4.fieldOfView += 5f);
					obj3.fieldOfView = Mathf.Clamp(num2, 25f, 180f);
				}
			}
		}
	}
}