Decompiled source of TeaTableMash v1.0.1

plugins/TeaTableMash.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Character;
using Core;
using FishNet.Object;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ThrownItems;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TeaTableMash")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("TeaTableMash")]
[assembly: AssemblyTitle("TeaTableMash")]
[assembly: AssemblyVersion("1.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 TeaTableMash
{
	[BepInPlugin("teatablemash", "TeaTableMash", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "teatablemash";

		public const string PluginName = "TeaTableMash";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<int> MashPresses;

		internal static ConfigEntry<float> LeapLockout;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Allow players to get up from a Tea Table by mashing Leap. Only the host's setting matters.");
			MashPresses = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MashPresses", 12, new ConfigDescription("How many Leap presses it takes to get up from a Tea Table.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			LeapLockout = ((BaseUnityPlugin)this).Config.Bind<float>("General", "LeapLockoutSeconds", 0.75f, new ConfigDescription("After mashing out, Leap is blocked for this many seconds so leftover mash presses don't launch you into the table (a leaping ragdoll breaks it). 0 disables.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 5f), Array.Empty<object>()));
			_harmony = new Harmony("teatablemash");
			_harmony.PatchAll(typeof(AddStatusEffectPatch));
			_harmony.PatchAll(typeof(RemoveStatusEffectPatch));
			MethodInfo methodInfo = AccessTools.GetDeclaredMethods(typeof(CoreCharacterController)).FirstOrDefault((MethodInfo m) => m.Name.StartsWith("RpcLogic___HandleLeapNoControl"));
			if (methodInfo == null)
			{
				Log.LogError((object)"Could not find HandleLeapNoControl RPC logic; mashing out of tea tables will not work.");
			}
			else
			{
				_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(HandleLeapPatch), "Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Log.LogInfo((object)"TeaTableMash 1.0.1 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		internal static float PerPressAmount(CoreCharacterController character)
		{
			if (!((Object)(object)character != (Object)null) || !(character.timeMashReduction > 0f))
			{
				return 1f;
			}
			return character.timeMashReduction;
		}
	}
	[HarmonyPatch(typeof(StatusEffectController), "AddStatusEffect", new Type[]
	{
		typeof(int),
		typeof(StatusEffectData)
	})]
	internal static class AddStatusEffectPatch
	{
		private static void Prefix(StatusEffectController __instance, StatusEffectData data)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			if (Plugin.Enabled.Value && data != null && (int)data.Effect == 9 && data.Owner is TeaTableSeat)
			{
				CoreCharacterController component = ((Component)__instance).GetComponent<CoreCharacterController>();
				data.AllowMashing = true;
				data.MashAmount = 0f;
				data.MaxMashAmount = (float)Plugin.MashPresses.Value * Plugin.PerPressAmount(component);
			}
		}
	}
	[HarmonyPatch(typeof(StatusEffectController), "RemoveStatusEffect")]
	internal static class RemoveStatusEffectPatch
	{
		private static void Prefix(StatusEffectController __instance, int id, out bool __state)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Invalid comparison between Unknown and I4
			__state = false;
			if (!Plugin.Enabled.Value || Plugin.LeapLockout.Value <= 0f || !((NetworkBehaviour)__instance).IsServerInitialized)
			{
				return;
			}
			foreach (StatusEffectData item in __instance.StatusEffects.Collection)
			{
				if (item.Id == id)
				{
					__state = (int)item.Effect == 9 && item.Owner is TeaTableSeat && item.AllowMashing && item.MaxMashAmount > 0f && item.MashAmount >= item.MaxMashAmount - 0.1f;
					break;
				}
			}
		}

		private static void Postfix(StatusEffectController __instance, bool __state)
		{
			//IL_001c: 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_0024: 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_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			if (__state)
			{
				CoreCharacterController component = ((Component)__instance).GetComponent<CoreCharacterController>();
				if (!((Object)(object)component == (Object)null))
				{
					__instance.AddStatusEffect(__instance.GetNextStatusEffectId(), new StatusEffectData
					{
						Effect = (StatusEffect)24,
						Owner = (NetworkBehaviour)(object)component,
						RemoveAfterTime = true,
						EndTime = RoundManager.GetRoundTime() + Plugin.LeapLockout.Value
					});
				}
			}
		}
	}
	internal static class HandleLeapPatch
	{
		private static readonly MethodInfo PlayMashReductionEffects = AccessTools.Method(typeof(CoreCharacterController), "PlayMashReductionEffects", (Type[])null, (Type[])null);

		internal static bool Prefix(CoreCharacterController __instance)
		{
			if (!Plugin.Enabled.Value || __instance.TimeFrozen)
			{
				return true;
			}
			StatusEffectController statusEffectController = __instance.statusEffectController;
			StatusEffectData currentNoControl = statusEffectController.GetCurrentNoControl();
			if (currentNoControl == null || !(currentNoControl.Owner is TeaTableSeat))
			{
				return true;
			}
			if (currentNoControl.AllowMashing && __instance.IsAlive)
			{
				PlayMashReductionEffects?.Invoke(__instance, null);
				currentNoControl.MashAmount += Plugin.PerPressAmount(__instance);
				if (currentNoControl.MashAmount > currentNoControl.MaxMashAmount)
				{
					currentNoControl.MashAmount = currentNoControl.MaxMashAmount;
				}
				statusEffectController.StatusEffects.Dirty(currentNoControl);
			}
			return false;
		}
	}
}