Decompiled source of EventChatNotifications v0.3.0

EventChatNotifications.dll

Decompiled 3 hours 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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("EventChatNotifications")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EventChatNotifications")]
[assembly: AssemblyTitle("EventChatNotifications")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 EventChatNotifications
{
	[BepInPlugin("sbg.eventchatnotifications", "EventChatNotifications", "0.3.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "sbg.eventchatnotifications";

		public const string ModName = "EventChatNotifications";

		public const string ModVersion = "0.3.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("sbg.eventchatnotifications").PatchAll();
			StatPassTracker.Hook();
			Log.LogInfo((object)"EventChatNotifications v0.3.0 loaded.");
		}

		private void OnDestroy()
		{
			StatPassTracker.Unhook();
		}
	}
	internal static class StatPassTracker
	{
		private enum Metric
		{
			Score,
			Knockouts,
			Strokes
		}

		private static readonly Metric[] AllMetrics = new Metric[3]
		{
			Metric.Score,
			Metric.Knockouts,
			Metric.Strokes
		};

		private static readonly Dictionary<long, sbyte> PreviousSign = new Dictionary<long, sbyte>();

		private static bool subscribed;

		internal static void Hook()
		{
			if (!subscribed)
			{
				CourseManager.PlayerStatesChanged += OnPlayerStatesChanged;
				subscribed = true;
			}
		}

		internal static void Unhook()
		{
			if (subscribed)
			{
				CourseManager.PlayerStatesChanged -= OnPlayerStatesChanged;
				PreviousSign.Clear();
				subscribed = false;
			}
		}

		private static void OnPlayerStatesChanged(Operation<PlayerState> op, int index, PlayerState changed)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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_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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			SyncList<PlayerState> playerStates = CourseManager.PlayerStates;
			if (playerStates == null || playerStates.Count < 2)
			{
				return;
			}
			int count = playerStates.Count;
			for (int i = 0; i < count; i++)
			{
				PlayerState val = playerStates[i];
				if (val.isSpectator)
				{
					continue;
				}
				for (int j = 0; j < count; j++)
				{
					if (i == j)
					{
						continue;
					}
					PlayerState val2 = playerStates[j];
					if (val2.isSpectator)
					{
						continue;
					}
					Metric[] allMetrics = AllMetrics;
					foreach (Metric m in allMetrics)
					{
						int a = ReadMetric(val, m);
						int b = ReadMetric(val2, m);
						sbyte b2 = CompareForBetter(a, b, m);
						long key = MakeKey(val.playerGuid, val2.playerGuid, m);
						sbyte value;
						int num = (PreviousSign.TryGetValue(key, out value) ? value : 0);
						PreviousSign[key] = b2;
						if (num == -1 && b2 >= 0)
						{
							string text = Narrator.PlayerNameFromGuid(val.playerGuid);
							string text2 = Narrator.PlayerNameFromGuid(val2.playerGuid);
							Narrator.Post(text + " passed " + text2 + " on " + MetricLabel(m) + ".");
						}
					}
				}
			}
		}

		private static int ReadMetric(PlayerState s, Metric m)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			return m switch
			{
				Metric.Score => s.matchScore, 
				Metric.Knockouts => s.matchKnockouts, 
				Metric.Strokes => s.matchStrokes, 
				_ => 0, 
			};
		}

		private static sbyte CompareForBetter(int a, int b, Metric m)
		{
			int num = ((m == Metric.Strokes) ? (b - a) : (a - b));
			if (num > 0)
			{
				return 1;
			}
			if (num < 0)
			{
				return -1;
			}
			return 0;
		}

		private static long MakeKey(ulong a, ulong b, Metric m)
		{
			return (long)(((a * 1099511628211L) ^ b) << 2) | (long)m;
		}

		private static string MetricLabel(Metric m)
		{
			return m switch
			{
				Metric.Score => "score", 
				Metric.Knockouts => "knockouts", 
				Metric.Strokes => "putts", 
				_ => "stat", 
			};
		}
	}
	internal static class Narrator
	{
		private const string Prefix = "<color=#9aa6ff><b>[Match]</b></color> ";

		internal static void Post(string message)
		{
			if (string.IsNullOrEmpty(message))
			{
				return;
			}
			try
			{
				TextChatUi.ShowMessage("<color=#9aa6ff><b>[Match]</b></color> " + message);
			}
			catch
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogInfo((object)("[chat-fallback] " + message));
				}
			}
		}

		internal static string PlayerNameFromGuid(ulong guid)
		{
			PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
			if ((Object)(object)localPlayerInfo != (Object)null && (Object)(object)localPlayerInfo.PlayerId != (Object)null && localPlayerInfo.PlayerId.Guid == guid)
			{
				return localPlayerInfo.PlayerId.PlayerNameNoRichText;
			}
			if (GameManager.RemotePlayers != null)
			{
				foreach (PlayerInfo remotePlayer in GameManager.RemotePlayers)
				{
					if (!((Object)(object)remotePlayer == (Object)null) && !((Object)(object)remotePlayer.PlayerId == (Object)null) && remotePlayer.PlayerId.Guid == guid)
					{
						return remotePlayer.PlayerId.PlayerNameNoRichText;
					}
				}
			}
			return "Someone";
		}

		internal static string DescribeStrokesUnderPar(StrokesUnderParType type)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected I4, but got Unknown
			return (type - 2) switch
			{
				4 => "scored a hole in one!", 
				2 => "landed an albatross!", 
				1 => "landed an eagle!", 
				0 => "landed a birdie!", 
				3 => "pulled off a condor!", 
				_ => null, 
			};
		}
	}
	internal static class NiceShotChatHook
	{
		private const float DuplicateDebounceSeconds = 0.5f;

		private const float ProximityMatchSquared = 81f;

		private static float lastPostTime;

		private static Vector3 lastPostPosition;

		internal static void OnSwingNiceShot(Vector3 position)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			float unscaledTime = Time.unscaledTime;
			if (unscaledTime - lastPostTime < 0.5f)
			{
				Vector3 val = position - lastPostPosition;
				if (((Vector3)(ref val)).sqrMagnitude < 1f)
				{
					return;
				}
			}
			lastPostTime = unscaledTime;
			lastPostPosition = position;
			Narrator.Post(ResolveNearestPlayerName(position) + " hit a perfect drive!");
		}

		private static string ResolveNearestPlayerName(Vector3 position)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			PlayerInfo val = null;
			float num = 81f;
			PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
			Vector3 val2;
			if ((Object)(object)localPlayerInfo != (Object)null && (Object)(object)((Component)localPlayerInfo).transform != (Object)null)
			{
				val2 = ((Component)localPlayerInfo).transform.position - position;
				float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude;
				if (sqrMagnitude <= num)
				{
					val = localPlayerInfo;
					num = sqrMagnitude;
				}
			}
			if (GameManager.RemotePlayers != null)
			{
				foreach (PlayerInfo remotePlayer in GameManager.RemotePlayers)
				{
					if (!((Object)(object)remotePlayer == (Object)null) && !((Object)(object)((Component)remotePlayer).transform == (Object)null))
					{
						val2 = ((Component)remotePlayer).transform.position - position;
						float sqrMagnitude2 = ((Vector3)(ref val2)).sqrMagnitude;
						if (sqrMagnitude2 < num)
						{
							val = remotePlayer;
							num = sqrMagnitude2;
						}
					}
				}
			}
			if ((Object)(object)val == (Object)null || (Object)(object)val.PlayerId == (Object)null)
			{
				return "Someone";
			}
			return val.PlayerId.PlayerNameNoRichText;
		}
	}
	[HarmonyPatch(typeof(VfxManager), "PlayPooledVfxLocalOnlyInternal", new Type[]
	{
		typeof(VfxType),
		typeof(Vector3),
		typeof(Quaternion),
		typeof(Vector3),
		typeof(uint),
		typeof(bool),
		typeof(float),
		typeof(Action<PoolableParticleSystem>)
	})]
	internal static class Patch_VfxManager_NiceShot
	{
		private static void Postfix(VfxType vfxType, Vector3 position)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			if ((int)vfxType == 69)
			{
				NiceShotChatHook.OnSwingNiceShot(position);
			}
		}
	}
	[HarmonyPatch(typeof(InfoFeed), "UserCode_RpcShowMessage__StrokesMessageData")]
	internal static class Patch_StrokesMessage
	{
		private static void Postfix(StrokesMessageData messageData)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: 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)
			string text = Narrator.DescribeStrokesUnderPar(messageData.strokesUnderParType);
			if (text != null)
			{
				Narrator.Post(Narrator.PlayerNameFromGuid(messageData.playerGuid) + " " + text);
			}
		}
	}
	[HarmonyPatch(typeof(InfoFeed), "UserCode_RpcShowMessage__ChipInMessageData")]
	internal static class Patch_ChipInMessage
	{
		private static void Postfix(ChipInMessageData messageData)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			string arg = Narrator.PlayerNameFromGuid(messageData.playerGuid);
			Narrator.Post($"{arg} chipped in from {Mathf.RoundToInt(messageData.distance)}m!");
		}
	}
	[HarmonyPatch(typeof(InfoFeed), "UserCode_RpcShowMessage__SpeedrunMessageData")]
	internal static class Patch_SpeedrunMessage
	{
		private static void Postfix(SpeedrunMessageData messageData)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			Narrator.Post(Narrator.PlayerNameFromGuid(messageData.playerGuid) + " sprinted through the hole — that was fast!");
		}
	}
	[HarmonyPatch(typeof(InfoFeed), "UserCode_RpcShowMessage__KnockoutMessageData")]
	internal static class Patch_KnockoutMessage
	{
		private static void Postfix(KnockoutMessageData messageData)
		{
			//IL_0000: 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)
			string text = Narrator.PlayerNameFromGuid(messageData.responsiblePlayer);
			string text2 = Narrator.PlayerNameFromGuid(messageData.knockedOutPlayer);
			if (!(text == text2))
			{
				Narrator.Post(text + " knocked out " + text2 + ".");
			}
		}
	}
}