Decompiled source of Solo Pause v1.0.0

SoloPause.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ProjectM.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : System.Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(/*Could not decode attribute arguments.*/)]
	internal sealed class RefSafetyRulesAttribute : System.Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SoloPause
{
	[BepInPlugin("com.zeeCameLsnake.SoloPause", "SoloPause", "1.0.0")]
	public class SoloPausePlugin : BasePlugin
	{
		[HarmonyPatch]
		internal static class EscapeMenuViewPatches
		{
			[HarmonyPatch(typeof(PauseInfoSystem), "OnStartRunning")]
			[HarmonyPostfix]
			private static void OnPauseStartPostfix()
			{
				_isGamePaused = true;
			}

			[HarmonyPatch(typeof(PauseInfoSystem), "OnStopRunning")]
			[HarmonyPostfix]
			private static void OnPauseStopPostfix()
			{
				_isGamePaused = false;
			}

			[HarmonyPatch(typeof(EscapeMenuView), "OnEnable")]
			[HarmonyPostfix]
			private static void OnEnablePostfix(EscapeMenuView __instance)
			{
				_activeMenu = __instance;
				if (_isMenuOpen)
				{
					return;
				}
				_isMenuOpen = true;
				if (ConfigEnabled.Value)
				{
					if (ConfigOnlyWhenHost.Value && !__instance.IsServerHost())
					{
						Log.LogDebug((object)"Client is not server host; skipping auto-pause.");
					}
					else if (!_isGamePaused)
					{
						_pendingAutoPause = true;
					}
				}
			}

			[HarmonyPatch(typeof(EscapeMenuView), "Update")]
			[HarmonyPrefix]
			private static void UpdatePrefix(EscapeMenuView __instance)
			{
				if (_pendingAutoPause)
				{
					_pendingAutoPause = false;
					if (!_isGamePaused)
					{
						_wasAutoPaused = true;
						Log.LogInfo((object)"Escape menu initialized: auto-pausing game...");
						SafeTogglePause(__instance, "Auto-pausing on first menu update");
					}
				}
			}

			[HarmonyPatch(typeof(FullscreenMenuMapper), "OnStopRunning")]
			[HarmonyPrefix]
			private static void FullscreenMenuStopPrefix()
			{
				if (_isMenuOpen)
				{
					_isMenuOpen = false;
					_pendingAutoPause = false;
					TryAutoResume(_activeMenu, "FullscreenMenuMapper.OnStopRunning");
				}
			}

			[HarmonyPatch(typeof(EscapeMenuView), "OnButtonClick_Pause")]
			[HarmonyPrefix]
			private static void OnButtonClick_PausePrefix()
			{
				//IL_0020: Unknown result type (might be due to invalid IL or missing references)
				//IL_0026: Expected O, but got Unknown
				if (!_isProgrammaticToggle && _isMenuOpen)
				{
					ManualLogSource log = Log;
					bool flag = default(bool);
					BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(73, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("User manually clicked pause button in menu. Clearing auto-resume (was: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(_wasAutoPaused);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(").");
					}
					log.LogInfo(val);
					_wasAutoPaused = false;
				}
			}

			[HarmonyPatch(typeof(EscapeMenuView), "OnDestroy")]
			[HarmonyPrefix]
			private static void OnDestroyPrefix(EscapeMenuView __instance)
			{
				_isMenuOpen = false;
				_pendingAutoPause = false;
				_wasAutoPaused = false;
				_isGamePaused = false;
				_activeMenu = null;
			}
		}

		public const string PluginGUID = "com.zeeCameLsnake.SoloPause";

		public const string PluginName = "SoloPause";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		public static ConfigEntry<bool> ConfigEnabled;

		public static ConfigEntry<bool> ConfigOnlyWhenHost;

		internal static bool _isMenuOpen;

		internal static bool _wasAutoPaused;

		internal static bool _isGamePaused;

		internal static bool _isProgrammaticToggle;

		internal static bool _pendingAutoPause;

		internal static EscapeMenuView _activeMenu;

		public override void Load()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			ConfigEnabled = ((BasePlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable auto-pause when opening the Escape menu.");
			ConfigOnlyWhenHost = ((BasePlugin)this).Config.Bind<bool>("General", "OnlyWhenHost", true, "Only auto-pause if the player is hosting the game (singleplayer / local server).");
			_harmony = new Harmony("com.zeeCameLsnake.SoloPause");
			_harmony.PatchAll();
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(23, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("SoloPause");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.0.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded successfully.");
			}
			log.LogInfo(val);
		}

		public override bool Unload()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return true;
		}

		internal static void SafeTogglePause(EscapeMenuView instance, string reason)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(24, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Executing pause toggle: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(reason);
			}
			log.LogInfo(val);
			_isProgrammaticToggle = true;
			try
			{
				instance.OnButtonClick_Pause();
			}
			finally
			{
				_isProgrammaticToggle = false;
			}
		}

		internal static void TryAutoResume(EscapeMenuView instance, string trigger)
		{
			if (_wasAutoPaused)
			{
				_wasAutoPaused = false;
				if (_isGamePaused && (Object)(object)instance != (Object)null)
				{
					SafeTogglePause(instance, "Auto-resuming game (" + trigger + ")");
				}
			}
		}
	}
}