Decompiled source of AutoHost v0.1.3

AutoHost.dll

Decompiled a day 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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
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("AutoHost")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyInformationalVersion("0.1.3+abef7a64602de390a0f8bdbccca84fdf15dced25")]
[assembly: AssemblyProduct("AutoHost")]
[assembly: AssemblyTitle("AutoHost")]
[assembly: AssemblyVersion("0.1.3.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 AutoHost
{
	public class AutoHostBehavior : MonoBehaviour
	{
		private bool wasInMainMenu;

		private float menuEnterTime;

		private float cooldownUntil;

		private bool roomNamePending;

		private string roomNameWanted = "";

		private float roomNameDeadline;

		private bool startPending;

		private float lobbyReadyTime = -1f;

		private float startTimeout;

		private void Update()
		{
			if (ConfigSettings.Enabled != null && ConfigSettings.Enabled.Value)
			{
				TryAutoHost();
			}
			if (roomNamePending)
			{
				PumpRoomName();
			}
			if (roomNamePending || startPending)
			{
				PumpPassword();
			}
			if (startPending)
			{
				PumpStart();
			}
		}

		private void TryAutoHost()
		{
			bool flag;
			try
			{
				flag = SemiFunc.IsMainMenu();
			}
			catch
			{
				return;
			}
			if (!flag)
			{
				wasInMainMenu = false;
			}
			else
			{
				if (Time.time < cooldownUntil)
				{
					return;
				}
				if (!wasInMainMenu)
				{
					wasInMainMenu = true;
					menuEnterTime = Time.time;
					return;
				}
				float num = 5f;
				try
				{
					num = ((ConfigSettings.MenuDelay != null) ? ConfigSettings.MenuDelay.Value : 5f);
				}
				catch
				{
					num = 5f;
				}
				if (Time.time - menuEnterTime < num)
				{
					return;
				}
				wasInMainMenu = false;
				cooldownUntil = Time.time + 60f;
				if (AccessTools.Field(typeof(MainMenuOpen), "instance")?.GetValue(null) != null && !((Object)(object)RunManager.instance == (Object)null) && !((Object)(object)DataDirector.instance == (Object)null))
				{
					Traverse.Create((object)DataDirector.instance).Field("networkPassword").SetValue((object)"");
					SemiFunc.MainMenuSetMultiplayer();
					SemiFunc.MenuActionHostGame((string)null, (List<string>)null);
					roomNameWanted = "autohost-" + Guid.NewGuid().ToString("N").Substring(0, 12);
					roomNamePending = true;
					roomNameDeadline = Time.time + 60f;
					startPending = true;
					lobbyReadyTime = -1f;
					startTimeout = Time.time + 180f;
					string text;
					try
					{
						text = Traverse.Create((object)DataDirector.instance).Field("networkPassword").GetValue<string>();
					}
					catch
					{
						text = "?";
					}
					Plugin.Log("AutoHost: hosting fresh private lobby (password='" + text + "'), room " + roomNameWanted);
				}
			}
		}

		private void PumpRoomName()
		{
			if (Time.time > roomNameDeadline)
			{
				roomNamePending = false;
				return;
			}
			NetworkConnect[] array;
			try
			{
				array = Object.FindObjectsOfType<NetworkConnect>();
			}
			catch
			{
				return;
			}
			if (array == null || array.Length == 0)
			{
				return;
			}
			bool flag = true;
			NetworkConnect[] array2 = array;
			foreach (NetworkConnect val in array2)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Traverse val2 = Traverse.Create((object)val);
				bool value;
				try
				{
					value = val2.Field("joinedRoom").GetValue<bool>();
				}
				catch
				{
					continue;
				}
				if (value)
				{
					continue;
				}
				flag = false;
				try
				{
					if (val2.Field("RoomName").GetValue<string>() != roomNameWanted)
					{
						val2.Field("RoomName").SetValue((object)roomNameWanted);
					}
				}
				catch (Exception ex)
				{
					Plugin.Log("AutoHost: RoomName override failed (" + ex.Message + ")");
					roomNamePending = false;
					return;
				}
			}
			if (flag)
			{
				roomNamePending = false;
			}
		}

		private void PumpStart()
		{
			if (Time.time > startTimeout)
			{
				startPending = false;
				Plugin.Log("AutoHost: start window expired, leaving lobby up");
				return;
			}
			try
			{
				if (SemiFunc.RunIsLevel() || SemiFunc.RunIsShop())
				{
					startPending = false;
					Plugin.Log("AutoHost: run started manually, standing down");
					return;
				}
			}
			catch
			{
			}
			MenuPageLobby val = null;
			try
			{
				MenuPageLobby[] array = Object.FindObjectsOfType<MenuPageLobby>();
				foreach (MenuPageLobby val2 in array)
				{
					if ((Object)(object)val2 != (Object)null && ((Component)val2).gameObject.activeInHierarchy && ((Behaviour)val2).enabled)
					{
						val = val2;
						break;
					}
				}
			}
			catch
			{
				return;
			}
			if ((Object)(object)val == (Object)null)
			{
				lobbyReadyTime = -1f;
				return;
			}
			try
			{
				if (!PhotonNetwork.InRoom || !PhotonNetwork.IsMasterClient)
				{
					lobbyReadyTime = -1f;
					return;
				}
			}
			catch
			{
				return;
			}
			bool value;
			try
			{
				value = Traverse.Create((object)val).Field("joiningPlayer").GetValue<bool>();
			}
			catch
			{
				return;
			}
			if (value)
			{
				return;
			}
			float num = 5f;
			try
			{
				num = ((ConfigSettings.StartDelay != null) ? ConfigSettings.StartDelay.Value : 5f);
			}
			catch
			{
				num = 5f;
			}
			if (lobbyReadyTime < 0f)
			{
				lobbyReadyTime = Time.time;
				Plugin.Log("AutoHost: lobby ready, pressing Start in " + num.ToString("F0") + "s");
			}
			else if (!(num - (Time.time - lobbyReadyTime) > 0f))
			{
				startPending = false;
				try
				{
					Traverse.Create((object)val).Method("ButtonStart", Array.Empty<object>()).GetValue();
					Plugin.Log("AutoHost: Start pressed");
				}
				catch (Exception ex)
				{
					Plugin.Log("AutoHost: Start failed (" + ex.Message + ")");
				}
			}
		}

		private void PumpPassword()
		{
			MenuPagePassword val = null;
			try
			{
				MenuPagePassword[] array = Object.FindObjectsOfType<MenuPagePassword>();
				foreach (MenuPagePassword val2 in array)
				{
					if ((Object)(object)val2 != (Object)null && ((Component)val2).gameObject.activeInHierarchy && ((Behaviour)val2).enabled)
					{
						val = val2;
						break;
					}
				}
			}
			catch
			{
				return;
			}
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			try
			{
				Traverse.Create((object)val).Method("ConfirmButton", Array.Empty<object>()).GetValue();
				Plugin.Log("AutoHost: password page skipped (no password)");
			}
			catch (Exception ex)
			{
				Plugin.Log("AutoHost: password skip failed (" + ex.Message + ")");
			}
		}
	}
	public static class ConfigSettings
	{
		public static ConfigEntry<bool> Enabled { get; private set; }

		public static ConfigEntry<float> MenuDelay { get; private set; }

		public static ConfigEntry<float> StartDelay { get; private set; }

		public static void Bind(ConfigFile config)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			Enabled = config.Bind<bool>("AutoHost", "Enabled", true, "While active, host a fresh private lobby without password on reaching the main menu. Disable the plugin (or this) to use the menu normally.");
			MenuDelay = config.Bind<float>("AutoHost", "MenuDelay", 5f, new ConfigDescription("Seconds spent in the main menu before auto-hosting. Gives Steam invites a chance to pull you elsewhere first.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 30f), Array.Empty<object>()));
			StartDelay = config.Bind<float>("AutoHost", "StartDelay", 5f, new ConfigDescription("Seconds after the auto-hosted lobby is ready (room joined, lobby page up) before Start is pressed automatically. Window for Steam invites to land.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 120f), Array.Empty<object>()));
		}
	}
	[BepInPlugin("yo.AutoHost", "AutoHost", "0.1.3")]
	[BepInProcess("REPO.exe")]
	public class Plugin : BaseUnityPlugin
	{
		private static ManualLogSource logSource;

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			ConfigSettings.Bind(((BaseUnityPlugin)this).Config);
			logSource = Logger.CreateLogSource("AutoHost");
			new Harmony("yo.AutoHost").PatchAll();
			GameObject val = new GameObject("AutoHost");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<AutoHostBehavior>();
			Log("AutoHost v0.1.3 loaded | Enabled=" + ConfigSettings.Enabled.Value);
		}

		public static void Log(string message)
		{
			logSource.LogInfo((object)message);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "yo.AutoHost";

		public const string PLUGIN_NAME = "AutoHost";

		public const string PLUGIN_VERSION = "0.1.3";
	}
}