Decompiled source of NoGoalTimer v1.1.1

PostGoalTimerPlus.dll

Decompiled a month 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.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("PostGoalTimerPlus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PostGoalTimerPlus")]
[assembly: AssemblyTitle("PostGoalTimerPlus")]
[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 PostGoalTimerPlus
{
	[BepInPlugin("superbattlegolf.postgoaltimerplus", "PostGoalTimerPlus", "1.1.0")]
	public class PostGoalTimerPlusPlugin : BaseUnityPlugin
	{
		private enum CountdownOption
		{
			Disabled = 0,
			OneMinute = 60,
			TwoMinutes = 120,
			ThreeMinutes = 180,
			FourMinutes = 240,
			FiveMinutes = 300
		}

		private const int DisabledCountdownSeconds = 0;

		private Harmony _harmony;

		private static ConfigEntry<CountdownOption> _countdownOption;

		private void Awake()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			_countdownOption = ((BaseUnityPlugin)this).Config.Bind<CountdownOption>("General", "CountdownOption", CountdownOption.Disabled, "Post-score countdown behavior. Options: Disabled, OneMinute, TwoMinutes, ThreeMinutes, FourMinutes, FiveMinutes.");
			_harmony = new Harmony("superbattlegolf.postgoaltimerplus");
			TryApplyPatch();
		}

		private void TryApplyPatch()
		{
			foreach (var item in new List<(string, string, string, string)>
			{
				("CourseManager", "BeginCountdownToMatchEnd", "BeginCountdownPrefix", "countdown start gate"),
				("CourseManager", "InformPlayerScoredInternal", "InformPlayerScoredPostfix", "countdown duration override")
			})
			{
				MethodInfo methodInfo = FindTargetMethod(item.Item1, item.Item2);
				if (!(methodInfo == null))
				{
					ApplyPatch(methodInfo, item.Item3, item.Item4);
				}
			}
		}

		private void ApplyPatch(MethodInfo targetMethod, string patchMethodName, string description)
		{
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			if (targetMethod == null)
			{
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(typeof(PostGoalTimerPlusPlugin), patchMethodName, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Patch method '" + patchMethodName + "' was not found in PostGoalTimerPlusPlugin."));
				return;
			}
			if (methodInfo.ReturnType == typeof(bool))
			{
				_harmony.Patch((MethodBase)targetMethod, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			else
			{
				_harmony.Patch((MethodBase)targetMethod, (HarmonyMethod)null, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched " + targetMethod.DeclaringType?.FullName + "." + targetMethod.Name + " for " + description + "."));
		}

		private static MethodInfo FindTargetMethod(string typeName, string methodName)
		{
			Type type = AccessTools.TypeByName(typeName);
			if (type != null)
			{
				return AccessTools.DeclaredMethod(type, methodName, (Type[])null, (Type[])null);
			}
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				Type[] types;
				try
				{
					types = assembly.GetTypes();
				}
				catch
				{
					continue;
				}
				Type[] array = types;
				foreach (Type type2 in array)
				{
					if (string.Equals(type2.Name, typeName, StringComparison.Ordinal))
					{
						MethodInfo methodInfo = AccessTools.DeclaredMethod(type2, methodName, (Type[])null, (Type[])null);
						if (methodInfo != null)
						{
							return methodInfo;
						}
					}
				}
			}
			return null;
		}

		private static bool BeginCountdownPrefix()
		{
			return GetConfiguredCountdownSeconds() > 0;
		}

		private static void InformPlayerScoredPostfix(object __instance)
		{
			int configuredCountdownSeconds = GetConfiguredCountdownSeconds();
			if (configuredCountdownSeconds > 0 && __instance != null)
			{
				Type type = __instance.GetType();
				FieldInfo fieldInfo = AccessTools.Field(type, "matchState");
				FieldInfo fieldInfo2 = AccessTools.Field(type, "countdownRemainingTime");
				if (!(fieldInfo == null) && !(fieldInfo2 == null) && string.Equals(fieldInfo.GetValue(__instance)?.ToString(), "CountingDownToEnd", StringComparison.Ordinal))
				{
					fieldInfo2.SetValue(__instance, (float)configuredCountdownSeconds);
				}
			}
		}

		private static int GetConfiguredCountdownSeconds()
		{
			if (_countdownOption == null)
			{
				return 0;
			}
			return (int)_countdownOption.Value;
		}
	}
}