Decompiled source of GolfCartBoost v0.1.1

GolfCartBoost.dll

Decompiled an hour ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.UI;

[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("GolfCartBoost")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GolfCartBoost")]
[assembly: AssemblyTitle("GolfCartBoost")]
[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 GolfCartBoost
{
	internal static class InputBridge
	{
		internal static bool WasPressedThisFrame(KeyCode keyCode)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected I4, but got Unknown
			//IL_009f: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			switch (keyCode - 323)
			{
			case 0:
				if (Mouse.current != null)
				{
					return Mouse.current.leftButton.wasPressedThisFrame;
				}
				return false;
			case 1:
				if (Mouse.current != null)
				{
					return Mouse.current.rightButton.wasPressedThisFrame;
				}
				return false;
			case 2:
				if (Mouse.current != null)
				{
					return Mouse.current.middleButton.wasPressedThisFrame;
				}
				return false;
			case 3:
				if (Mouse.current != null)
				{
					return Mouse.current.backButton.wasPressedThisFrame;
				}
				return false;
			case 4:
				if (Mouse.current != null)
				{
					return Mouse.current.forwardButton.wasPressedThisFrame;
				}
				return false;
			default:
			{
				Key val = TranslateKey(keyCode);
				if ((int)val == 0 || Keyboard.current == null)
				{
					return false;
				}
				return ((ButtonControl)Keyboard.current[val]).wasPressedThisFrame;
			}
			}
		}

		internal static bool IsHeld(KeyCode keyCode)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected I4, but got Unknown
			//IL_009f: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			switch (keyCode - 323)
			{
			case 0:
				if (Mouse.current != null)
				{
					return Mouse.current.leftButton.isPressed;
				}
				return false;
			case 1:
				if (Mouse.current != null)
				{
					return Mouse.current.rightButton.isPressed;
				}
				return false;
			case 2:
				if (Mouse.current != null)
				{
					return Mouse.current.middleButton.isPressed;
				}
				return false;
			case 3:
				if (Mouse.current != null)
				{
					return Mouse.current.backButton.isPressed;
				}
				return false;
			case 4:
				if (Mouse.current != null)
				{
					return Mouse.current.forwardButton.isPressed;
				}
				return false;
			default:
			{
				Key val = TranslateKey(keyCode);
				if ((int)val == 0 || Keyboard.current == null)
				{
					return false;
				}
				return ((ButtonControl)Keyboard.current[val]).isPressed;
			}
			}
		}

		private static Key TranslateKey(KeyCode keyCode)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Invalid comparison between Unknown and I4
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//IL_0076: 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_00fb: Expected I4, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Invalid comparison between Unknown and I4
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Invalid comparison between Unknown and I4
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Invalid comparison between Unknown and I4
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Expected I4, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected I4, but got Unknown
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Invalid comparison between Unknown and I4
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Invalid comparison between Unknown and I4
			if ((int)keyCode <= 27)
			{
				if ((int)keyCode <= 9)
				{
					if ((int)keyCode == 8)
					{
						return (Key)65;
					}
					if ((int)keyCode == 9)
					{
						return (Key)3;
					}
				}
				else
				{
					if ((int)keyCode == 13)
					{
						return (Key)2;
					}
					if ((int)keyCode == 27)
					{
						return (Key)60;
					}
				}
			}
			else if ((int)keyCode <= 57)
			{
				if ((int)keyCode == 32)
				{
					return (Key)1;
				}
				switch (keyCode - 48)
				{
				case 0:
					return (Key)50;
				case 1:
					return (Key)41;
				case 2:
					return (Key)42;
				case 3:
					return (Key)43;
				case 4:
					return (Key)44;
				case 5:
					return (Key)45;
				case 6:
					return (Key)46;
				case 7:
					return (Key)47;
				case 8:
					return (Key)48;
				case 9:
					return (Key)49;
				}
			}
			else
			{
				switch (keyCode - 97)
				{
				default:
					switch (keyCode - 273)
					{
					case 9:
						return (Key)94;
					case 10:
						return (Key)95;
					case 11:
						return (Key)96;
					case 12:
						return (Key)97;
					case 13:
						return (Key)98;
					case 14:
						return (Key)99;
					case 15:
						return (Key)100;
					case 16:
						return (Key)101;
					case 17:
						return (Key)102;
					case 18:
						return (Key)103;
					case 19:
						return (Key)104;
					case 20:
						return (Key)105;
					case 31:
						return (Key)51;
					case 30:
						return (Key)52;
					case 33:
						return (Key)55;
					case 32:
						return (Key)56;
					case 35:
						return (Key)53;
					case 34:
						return (Key)54;
					case 0:
						return (Key)63;
					case 1:
						return (Key)64;
					case 3:
						return (Key)61;
					case 2:
						return (Key)62;
					}
					break;
				case 0:
					return (Key)15;
				case 1:
					return (Key)16;
				case 2:
					return (Key)17;
				case 3:
					return (Key)18;
				case 4:
					return (Key)19;
				case 5:
					return (Key)20;
				case 6:
					return (Key)21;
				case 7:
					return (Key)22;
				case 8:
					return (Key)23;
				case 9:
					return (Key)24;
				case 10:
					return (Key)25;
				case 11:
					return (Key)26;
				case 12:
					return (Key)27;
				case 13:
					return (Key)28;
				case 14:
					return (Key)29;
				case 15:
					return (Key)30;
				case 16:
					return (Key)31;
				case 17:
					return (Key)32;
				case 18:
					return (Key)33;
				case 19:
					return (Key)34;
				case 20:
					return (Key)35;
				case 21:
					return (Key)36;
				case 22:
					return (Key)37;
				case 23:
					return (Key)38;
				case 24:
					return (Key)39;
				case 25:
					return (Key)40;
				case 30:
					return (Key)71;
				case 26:
				case 27:
				case 28:
				case 29:
					break;
				}
			}
			return (Key)0;
		}
	}
	[BepInPlugin("sbg.golfcartboost", "GolfCartBoost", "0.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "sbg.golfcartboost";

		public const string ModName = "GolfCartBoost";

		public const string ModVersion = "0.1.0";

		internal static ManualLogSource Log;

		internal static Plugin Instance;

		internal ConfigEntry<KeyCode> boostKeyConfig;

		internal ConfigEntry<float> boostDurationConfig;

		internal ConfigEntry<float> boostCooldownConfig;

		internal ConfigEntry<bool> uiPromptEnabledConfig;

		private static MethodInfo addSpeedBoostMethod;

		private float lastBoostTime = float.NegativeInfinity;

		private BoostPromptUi promptUi;

		private void Awake()
		{
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			boostKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Boost", "BoostKey", (KeyCode)324, "Key/button to trigger the cart boost. Defaults to right click (Mouse1). Any UnityEngine.KeyCode value works.");
			boostDurationConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Boost", "DurationSeconds", 1.5f, "Duration of each boost in seconds. Reuses the SpeedBoost status's natural tick-down.");
			boostCooldownConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Boost", "CooldownSeconds", 4f, "Minimum seconds between boost activations.");
			uiPromptEnabledConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("Ui", "PromptEnabled", true, "Show the on-screen \"Click [button] to boost!\" prompt while driving.");
			addSpeedBoostMethod = AccessTools.Method(typeof(PlayerMovement), "AddSpeedBoost", (Type[])null, (Type[])null);
			if (addSpeedBoostMethod == null)
			{
				Log.LogWarning((object)"PlayerMovement.AddSpeedBoost not found via reflection; boost will fall back to InformDrankCoffee with the coffee duration.");
			}
			new Harmony("sbg.golfcartboost").PatchAll();
			Log.LogInfo((object)"GolfCartBoost v0.1.0 loaded.");
		}

		private void Update()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			EnsurePromptUi();
			PlayerInfo localPlayerInfo = GameManager.LocalPlayerInfo;
			bool flag = IsLocalPlayerDriving(localPlayerInfo);
			float time = Time.time;
			bool flag2 = time - lastBoostTime >= boostCooldownConfig.Value;
			if (flag && flag2 && InputBridge.WasPressedThisFrame(boostKeyConfig.Value))
			{
				TriggerBoost(localPlayerInfo);
				lastBoostTime = time;
			}
			if ((Object)(object)promptUi != (Object)null)
			{
				promptUi.UpdateState(flag, flag2, boostKeyConfig.Value, uiPromptEnabledConfig.Value);
			}
		}

		private static bool IsLocalPlayerDriving(PlayerInfo info)
		{
			//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)
			if ((Object)(object)info == (Object)null)
			{
				return false;
			}
			GolfCartSeat activeGolfCartSeat = info.ActiveGolfCartSeat;
			if (!((GolfCartSeat)(ref activeGolfCartSeat)).IsValid())
			{
				return false;
			}
			return ((GolfCartSeat)(ref activeGolfCartSeat)).IsDriver();
		}

		private void TriggerBoost(PlayerInfo info)
		{
			if (!((Object)(object)info == (Object)null) && !((Object)(object)info.Movement == (Object)null))
			{
				if (addSpeedBoostMethod != null)
				{
					addSpeedBoostMethod.Invoke(info.Movement, new object[1] { boostDurationConfig.Value });
				}
				else
				{
					info.Movement.InformDrankCoffee();
				}
			}
		}

		private void EnsurePromptUi()
		{
			if (!((Object)(object)promptUi != (Object)null))
			{
				promptUi = BoostPromptUi.Create();
			}
		}
	}
	internal sealed class BoostPromptUi : MonoBehaviour
	{
		private CanvasGroup group;

		private TextMeshProUGUI label;

		private float currentAlpha;

		public static BoostPromptUi Create()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_004c: 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_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: 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_00d4: 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_00fd: 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_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("GolfCartBoost-Canvas");
			Object.DontDestroyOnLoad((Object)(object)val);
			((Object)val).hideFlags = (HideFlags)61;
			Canvas obj = val.AddComponent<Canvas>();
			obj.renderMode = (RenderMode)0;
			obj.sortingOrder = 1000;
			val.AddComponent<CanvasScaler>().uiScaleMode = (ScaleMode)1;
			val.GetComponent<CanvasScaler>().referenceResolution = new Vector2(1920f, 1080f);
			val.AddComponent<GraphicRaycaster>();
			CanvasGroup val2 = val.AddComponent<CanvasGroup>();
			val2.alpha = 0f;
			val2.blocksRaycasts = false;
			val2.interactable = false;
			GameObject val3 = new GameObject("Label");
			val3.transform.SetParent(val.transform, false);
			RectTransform obj2 = val3.AddComponent<RectTransform>();
			obj2.anchorMin = new Vector2(0.5f, 0f);
			obj2.anchorMax = new Vector2(0.5f, 0f);
			obj2.pivot = new Vector2(0.5f, 0f);
			obj2.anchoredPosition = new Vector2(0f, 220f);
			obj2.sizeDelta = new Vector2(640f, 60f);
			TextMeshProUGUI val4 = val3.AddComponent<TextMeshProUGUI>();
			((TMP_Text)val4).alignment = (TextAlignmentOptions)514;
			((TMP_Text)val4).fontSize = 36f;
			((TMP_Text)val4).fontStyle = (FontStyles)1;
			((Graphic)val4).color = new Color(1f, 0.94f, 0.55f, 1f);
			((TMP_Text)val4).outlineColor = Color32.op_Implicit(Color.black);
			((TMP_Text)val4).outlineWidth = 0.2f;
			((TMP_Text)val4).text = string.Empty;
			BoostPromptUi boostPromptUi = val.AddComponent<BoostPromptUi>();
			boostPromptUi.group = val2;
			boostPromptUi.label = val4;
			return boostPromptUi;
		}

		public void UpdateState(bool driving, bool offCooldown, KeyCode key, bool enabledByConfig)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			float num = ((driving && offCooldown && enabledByConfig) ? 1f : 0f);
			float num2 = ((num > currentAlpha) ? 6f : 2f);
			currentAlpha = Mathf.MoveTowards(currentAlpha, num, num2 * Time.deltaTime);
			group.alpha = currentAlpha;
			if (currentAlpha > 0.01f && driving)
			{
				string text = FormatKey(key);
				((TMP_Text)label).text = "Press <b>" + text + "</b> to boost!";
			}
		}

		private static string FormatKey(KeyCode key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected I4, but got Unknown
			return (key - 323) switch
			{
				0 => "Left Click", 
				1 => "Right Click", 
				2 => "Middle Click", 
				_ => ((object)(KeyCode)(ref key)).ToString(), 
			};
		}
	}
}