Decompiled source of AutoRestart v1.0.0

AutoRestart.dll

Decompiled 4 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AutoRestart")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AutoRestart")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8c384c1d-02fd-4861-b553-17ba4f00de70")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.morsecodeguy.autorestart", "AutoRestart", "1.0.0")]
public class RestartOnDeath : BaseUnityPlugin
{
	private ConfigEntry<float> checkIntervalConfig;

	private ConfigEntry<float> restartDelayConfig;

	private ConfigEntry<bool> autoRestartEnabledConfig;

	private ConfigEntry<bool> verboseLoggingConfig;

	private ConfigEntry<int> minPlayersToRestartConfig;

	private ConfigEntry<KeyCode> toggleMenuKeyConfig;

	private Coroutine restartCoroutine = null;

	private bool isRestarting = false;

	private bool isCheckingPlayers = false;

	private float sceneLoadDelay = 2f;

	private int previousDeadPlayersCount = -1;

	private int previousAlivePlayersCount = -1;

	private bool isMenuVisible = false;

	private void Awake()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Expected O, but got Unknown
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Expected O, but got Unknown
		((Object)Chainloader.ManagerObject).hideFlags = (HideFlags)61;
		autoRestartEnabledConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoRestartEnabled", false, "Enable or disable auto restart");
		restartDelayConfig = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RestartDelay", 5f, new ConfigDescription("Delay before restarting the game (in seconds)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 30f), Array.Empty<object>()));
		checkIntervalConfig = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CheckInterval", 1f, new ConfigDescription("How frequently to check player status (in seconds)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 5f), Array.Empty<object>()));
		verboseLoggingConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "VerboseLogging", false, "Enable or disable verbose logging");
		minPlayersToRestartConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MinPlayersToRestart", 1, new ConfigDescription("Minimum number of players alive before auto restart triggers", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
		toggleMenuKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ToggleMenuKey", (KeyCode)289, "Key to toggle the menu visibility");
		((MonoBehaviour)this).StartCoroutine(SceneWatcherCoroutine());
	}

	private void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(toggleMenuKeyConfig.Value))
		{
			isMenuVisible = !isMenuVisible;
		}
	}

	private IEnumerator SceneWatcherCoroutine()
	{
		while (true)
		{
			Scene activeScene = SceneManager.GetActiveScene();
			if (((Scene)(ref activeScene)).name.StartsWith("map_"))
			{
				break;
			}
			yield return (object)new WaitForSeconds(checkIntervalConfig.Value);
		}
		if (verboseLoggingConfig.Value)
		{
			Debug.Log((object)"Target scene loaded. Waiting for scene to initialize...");
		}
		yield return (object)new WaitForSeconds(sceneLoadDelay);
		isRestarting = false;
		isCheckingPlayers = true;
		if (verboseLoggingConfig.Value)
		{
			Debug.Log((object)"Scene initialized. Starting player checks.");
		}
		((MonoBehaviour)this).StartCoroutine(CheckPlayersAliveCoroutine());
	}

	private IEnumerator CheckPlayersAliveCoroutine()
	{
		while (isCheckingPlayers)
		{
			Scene activeScene = SceneManager.GetActiveScene();
			if (!((Scene)(ref activeScene)).name.StartsWith("map_"))
			{
				if (verboseLoggingConfig.Value)
				{
					Debug.Log((object)"Scene changed, stopping player checks.");
				}
				isCheckingPlayers = false;
				((MonoBehaviour)this).StartCoroutine(SceneWatcherCoroutine());
				break;
			}
			PlayerHealth[] players = Object.FindObjectsOfType<PlayerHealth>();
			int deadPlayersCount = players.Count((PlayerHealth player) => !player.alive);
			int alivePlayersCount = players.Count((PlayerHealth player) => player.alive);
			if (deadPlayersCount != previousDeadPlayersCount || alivePlayersCount != previousAlivePlayersCount)
			{
				if (verboseLoggingConfig.Value)
				{
					Debug.LogError((object)("Dead: " + deadPlayersCount + " Alive: " + alivePlayersCount));
				}
				previousDeadPlayersCount = deadPlayersCount;
				previousAlivePlayersCount = alivePlayersCount;
			}
			if (alivePlayersCount <= minPlayersToRestartConfig.Value && !isRestarting && autoRestartEnabledConfig.Value)
			{
				isRestarting = true;
				isCheckingPlayers = false;
				if (verboseLoggingConfig.Value)
				{
					Debug.LogError((object)"Restarting match.");
				}
				restartCoroutine = ((MonoBehaviour)this).StartCoroutine(RestartGameAfterDelay());
				break;
			}
			yield return (object)new WaitForSeconds(checkIntervalConfig.Value);
		}
	}

	private IEnumerator RestartGameAfterDelay()
	{
		float elapsed = 0f;
		while (elapsed < restartDelayConfig.Value)
		{
			if (!autoRestartEnabledConfig.Value)
			{
				if (verboseLoggingConfig.Value)
				{
					Debug.Log((object)"Auto-restart disabled during delay. Cancelling restart.");
				}
				isRestarting = false;
				isCheckingPlayers = true;
				((MonoBehaviour)this).StartCoroutine(CheckPlayersAliveCoroutine());
				yield break;
			}
			elapsed += Time.deltaTime;
			yield return null;
		}
		Scene activeScene = SceneManager.GetActiveScene();
		if (((Scene)(ref activeScene)).name.StartsWith("map_"))
		{
			GameMenu gameMenu = Object.FindObjectOfType<GameMenu>();
			if ((Object)(object)gameMenu != (Object)null)
			{
				gameMenu.RestartGame();
				if (verboseLoggingConfig.Value)
				{
					Debug.LogError((object)"Restarted.");
				}
			}
			else if (verboseLoggingConfig.Value)
			{
				Debug.LogError((object)"GameMenu component not found in the scene.");
			}
		}
		((MonoBehaviour)this).StartCoroutine(SceneWatcherCoroutine());
	}

	private void OnGUI()
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: 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_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Expected O, but got Unknown
		//IL_04cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d2: Unknown result type (might be due to invalid IL or missing references)
		if (!isMenuVisible)
		{
			return;
		}
		float num = 400f;
		float num2 = 320f;
		float num3 = ((float)Screen.width - num) / 2f;
		float num4 = 10f;
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(num3, num4, num, num2);
		GUILayout.BeginArea(val, GUI.skin.box);
		GUILayout.Label("<b><size=15>Restart Options</size></b>", new GUIStyle(GUI.skin.label)
		{
			alignment = (TextAnchor)4,
			richText = true
		}, Array.Empty<GUILayoutOption>());
		GUILayout.Space(10f);
		GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
		bool flag = GUILayout.Toggle(autoRestartEnabledConfig.Value, " Auto Restart", Array.Empty<GUILayoutOption>());
		if (flag != autoRestartEnabledConfig.Value)
		{
			autoRestartEnabledConfig.Value = flag;
			((BaseUnityPlugin)this).Config.Save();
			if (!autoRestartEnabledConfig.Value && restartCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(restartCoroutine);
				restartCoroutine = null;
				isRestarting = false;
				if (!isCheckingPlayers)
				{
					isCheckingPlayers = true;
					((MonoBehaviour)this).StartCoroutine(CheckPlayersAliveCoroutine());
				}
			}
		}
		GUILayout.EndHorizontal();
		GUILayout.Label("Restart Delay (seconds)", Array.Empty<GUILayoutOption>());
		GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
		string text = restartDelayConfig.Value.ToString("F1");
		string text2 = GUILayout.TextField(text, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(50f) });
		if (text2 != text && float.TryParse(text2, out var result))
		{
			restartDelayConfig.Value = Mathf.Clamp(result, 1f, 30f);
			((BaseUnityPlugin)this).Config.Save();
		}
		float num5 = GUILayout.HorizontalSlider(restartDelayConfig.Value, 1f, 30f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(300f) });
		if (Mathf.Abs(num5 - restartDelayConfig.Value) > Mathf.Epsilon)
		{
			restartDelayConfig.Value = num5;
			((BaseUnityPlugin)this).Config.Save();
		}
		GUILayout.EndHorizontal();
		GUILayout.Label("Minimum Players to Restart", Array.Empty<GUILayoutOption>());
		GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
		string text3 = minPlayersToRestartConfig.Value.ToString();
		string text4 = GUILayout.TextField(text3, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(50f) });
		if (text4 != text3 && int.TryParse(text4, out var result2))
		{
			minPlayersToRestartConfig.Value = Mathf.Clamp(result2, 0, 10);
			((BaseUnityPlugin)this).Config.Save();
		}
		int num6 = Mathf.RoundToInt(GUILayout.HorizontalSlider((float)minPlayersToRestartConfig.Value, 0f, 10f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(300f) }));
		if (num6 != minPlayersToRestartConfig.Value)
		{
			minPlayersToRestartConfig.Value = num6;
			((BaseUnityPlugin)this).Config.Save();
		}
		GUILayout.EndHorizontal();
		GUILayout.Label("Check Interval (seconds)", Array.Empty<GUILayoutOption>());
		GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
		string text5 = checkIntervalConfig.Value.ToString("F1");
		string text6 = GUILayout.TextField(text5, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(50f) });
		if (text6 != text5 && float.TryParse(text6, out var result3))
		{
			checkIntervalConfig.Value = Mathf.Clamp(result3, 0.1f, 5f);
			((BaseUnityPlugin)this).Config.Save();
		}
		float num7 = GUILayout.HorizontalSlider(checkIntervalConfig.Value, 0.1f, 5f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(300f) });
		if (Mathf.Abs(num7 - checkIntervalConfig.Value) > Mathf.Epsilon)
		{
			checkIntervalConfig.Value = num7;
			((BaseUnityPlugin)this).Config.Save();
		}
		GUILayout.EndHorizontal();
		bool flag2 = GUILayout.Toggle(verboseLoggingConfig.Value, " Verbose Logging", Array.Empty<GUILayoutOption>());
		if (flag2 != verboseLoggingConfig.Value)
		{
			verboseLoggingConfig.Value = flag2;
			((BaseUnityPlugin)this).Config.Save();
		}
		GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
		KeyCode value = toggleMenuKeyConfig.Value;
		GUILayout.Label("Toggle Menu Key: " + ((object)(KeyCode)(ref value)).ToString(), Array.Empty<GUILayoutOption>());
		if (GUILayout.Button("Change Key", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(100f) }))
		{
			((MonoBehaviour)this).StartCoroutine(WaitForKeyPress());
		}
		GUILayout.EndHorizontal();
		if (GUILayout.Button("Restart Game Now", Array.Empty<GUILayoutOption>()))
		{
			RestartGameNow();
		}
		if (GUILayout.Button("Reset to Default Settings", Array.Empty<GUILayoutOption>()))
		{
			ResetToDefaultSettings();
		}
		GUILayout.EndArea();
	}

	private IEnumerator WaitForKeyPress()
	{
		bool keySet = false;
		GUILayout.Label("Press any key to set as new toggle key...", Array.Empty<GUILayoutOption>());
		while (!keySet)
		{
			Event e = Event.current;
			if (e != null && e.isKey)
			{
				toggleMenuKeyConfig.Value = e.keyCode;
				((BaseUnityPlugin)this).Config.Save();
				keySet = true;
			}
			yield return null;
		}
	}

	private void RestartGameNow()
	{
		//IL_0001: 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)
		Scene activeScene = SceneManager.GetActiveScene();
		if (!((Scene)(ref activeScene)).name.StartsWith("map_"))
		{
			return;
		}
		GameMenu val = Object.FindObjectOfType<GameMenu>();
		if ((Object)(object)val != (Object)null)
		{
			val.RestartGame();
			if (verboseLoggingConfig.Value)
			{
				Debug.Log((object)"Game restarted manually.");
			}
		}
		else if (verboseLoggingConfig.Value)
		{
			Debug.LogError((object)"GameMenu component not found in the scene.");
		}
	}

	private void ResetToDefaultSettings()
	{
		autoRestartEnabledConfig.Value = false;
		restartDelayConfig.Value = 5f;
		checkIntervalConfig.Value = 1f;
		verboseLoggingConfig.Value = false;
		minPlayersToRestartConfig.Value = 1;
		toggleMenuKeyConfig.Value = (KeyCode)289;
		((BaseUnityPlugin)this).Config.Save();
		if (verboseLoggingConfig.Value)
		{
			Debug.Log((object)"Settings have been reset to default.");
		}
	}
}