Decompiled source of NowWatchThisDrive v0.4.0

NowWatchThisDrive.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using FMOD;
using FMODUnity;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("NowWatchThisDrive")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NowWatchThisDrive")]
[assembly: AssemblyTitle("NowWatchThisDrive")]
[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 NowWatchThisDrive
{
	[BepInPlugin("sbg.nowwatchthisdrive", "NowWatchThisDrive", "0.4.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(CourseManager), "PlayAnnouncerLineLocalOnly")]
		internal static class Patch_CourseManager_PlayAnnouncerLineLocalOnly
		{
			private static bool Prefix(AnnouncerLine line)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0002: Invalid comparison between Unknown and I4
				if ((int)line != 5 || !_soundReady)
				{
					return true;
				}
				PlayLocal2D();
				return false;
			}
		}

		[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_PlayPooledVfxLocalOnlyInternal
		{
			private static void Prefix(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_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Unknown result type (might be due to invalid IL or missing references)
				//IL_0021: Unknown result type (might be due to invalid IL or missing references)
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				if ((int)vfxType == 69 && _soundReady && !IsLocalPlayerSwingPosition(position))
				{
					PlayRemote3D(position + Vector3.up * 0.9f);
				}
			}
		}

		public const string ModGuid = "sbg.nowwatchthisdrive";

		public const string ModName = "NowWatchThisDrive";

		public const string ModVersion = "0.4.0";

		private const string AudioFileName = "NowWatchThisDrive.wav";

		private const float Audio3DMinDistance = 8f;

		private const float Audio3DMaxDistance = 60f;

		private const float AudioHeightOffset = 0.9f;

		private const float LocalSwingProximitySquared = 36f;

		private const float DuplicateWindowSeconds = 0.25f;

		private const float DuplicateDistanceSquared = 0.25f;

		internal static ManualLogSource Log;

		private static Sound _sound2D;

		private static Sound _sound3D;

		private static bool _soundReady;

		private static float _last3DPlayTime;

		private static Vector3 _last3DPlayPosition;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("sbg.nowwatchthisdrive").PatchAll();
			Log.LogInfo((object)"NowWatchThisDrive v0.4.0 loaded (sound loads in Start).");
		}

		private void Start()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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_008b: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "NowWatchThisDrive.wav");
				if (!File.Exists(text))
				{
					Log.LogError((object)("Audio file missing: " + text));
					return;
				}
				System coreSystem = RuntimeManager.CoreSystem;
				RESULT val = ((System)(ref coreSystem)).createSound(text, (MODE)256, ref _sound2D);
				if ((int)val != 0)
				{
					Log.LogError((object)$"FMOD createSound (2D) failed: {val}");
					return;
				}
				RESULT val2 = ((System)(ref coreSystem)).createSound(text, (MODE)2097424, ref _sound3D);
				if ((int)val2 != 0)
				{
					Log.LogError((object)$"FMOD createSound (3D) failed: {val2}");
					return;
				}
				((Sound)(ref _sound3D)).set3DMinMaxDistance(8f, 60f);
				uint num = default(uint);
				((Sound)(ref _sound2D)).getLength(ref num, (TIMEUNIT)1);
				_soundReady = true;
				Log.LogInfo((object)$"FMOD sounds loaded ({num} ms): {text}");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Start failed: {arg}");
			}
		}

		private static void PlayLocal2D()
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: 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)
			//IL_0027: 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_003b: Unknown result type (might be due to invalid IL or missing references)
			if (!_soundReady)
			{
				return;
			}
			try
			{
				System coreSystem = RuntimeManager.CoreSystem;
				Channel val = default(Channel);
				RESULT val2 = ((System)(ref coreSystem)).playSound(_sound2D, default(ChannelGroup), false, ref val);
				if ((int)val2 != 0)
				{
					ManualLogSource log = Log;
					if (log != null)
					{
						log.LogWarning((object)$"playSound 2D: {val2}");
					}
				}
			}
			catch (Exception arg)
			{
				ManualLogSource log2 = Log;
				if (log2 != null)
				{
					log2.LogError((object)$"PlayLocal2D failed: {arg}");
				}
			}
		}

		private static void PlayRemote3D(Vector3 worldPosition)
		{
			//IL_001a: 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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: 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)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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_0094: 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_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			if (!_soundReady)
			{
				return;
			}
			if (Time.unscaledTime - _last3DPlayTime < 0.25f)
			{
				Vector3 val = worldPosition - _last3DPlayPosition;
				if (((Vector3)(ref val)).sqrMagnitude < 0.25f)
				{
					return;
				}
			}
			try
			{
				System coreSystem = RuntimeManager.CoreSystem;
				Channel val2 = default(Channel);
				RESULT val3 = ((System)(ref coreSystem)).playSound(_sound3D, default(ChannelGroup), true, ref val2);
				if ((int)val3 != 0)
				{
					ManualLogSource log = Log;
					if (log != null)
					{
						log.LogWarning((object)$"playSound 3D: {val3}");
					}
					return;
				}
				((Channel)(ref val2)).setMode((MODE)2097168);
				((Channel)(ref val2)).set3DMinMaxDistance(8f, 60f);
				VECTOR val4 = RuntimeUtils.ToFMODVector(worldPosition);
				VECTOR val5 = RuntimeUtils.ToFMODVector(Vector3.zero);
				((Channel)(ref val2)).set3DAttributes(ref val4, ref val5);
				((Channel)(ref val2)).setPaused(false);
				_last3DPlayTime = Time.unscaledTime;
				_last3DPlayPosition = worldPosition;
			}
			catch (Exception arg)
			{
				ManualLogSource log2 = Log;
				if (log2 != null)
				{
					log2.LogError((object)$"PlayRemote3D failed: {arg}");
				}
			}
		}

		private static bool IsLocalPlayerSwingPosition(Vector3 position)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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)
			PlayerGolfer localPlayerAsGolfer = GameManager.LocalPlayerAsGolfer;
			if ((Object)(object)localPlayerAsGolfer == (Object)null)
			{
				return false;
			}
			Vector3 val = ((Component)localPlayerAsGolfer).transform.position - position;
			return ((Vector3)(ref val)).sqrMagnitude <= 36f;
		}
	}
}