Decompiled source of AutoRegion v1.3.1

AutoRegion.dll

Decompiled 4 days ago
using System;
using System.Collections;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[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("AutoRegion")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyInformationalVersion("1.3.1")]
[assembly: AssemblyProduct("AutoRegion")]
[assembly: AssemblyTitle("AutoRegion")]
[assembly: AssemblyVersion("1.3.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 AutoRegion
{
	[BepInPlugin("purtzel_hd.autoregion", "AutoRegion", "1.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> AutoSelect;

		internal static ConfigEntry<string> PreferredRegionCode;

		internal static ConfigEntry<bool> JumpStraightToServerList;

		internal static ConfigEntry<bool> AutoAcceptWarning;

		internal static readonly string[] ValidCodes = new string[16]
		{
			"asia", "au", "cae", "cn", "eu", "hk", "in", "jp", "kr", "sa",
			"tr", "uae", "za", "us", "usw", "ussc"
		};

		private void Awake()
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Expected O, but got Unknown
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Expected O, but got Unknown
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Expected O, but got Unknown
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Expected O, but got Unknown
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Expected O, but got Unknown
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			AutoSelect = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoSelectRegion", true, "Enable auto-selecting your preferred region.");
			PreferredRegionCode = ((BaseUnityPlugin)this).Config.Bind<string>("General", "PreferredRegionCode", "", "Saved region code (set automatically on first pick). Valid: " + string.Join(", ", ValidCodes));
			JumpStraightToServerList = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "JumpStraightToServerList", true, "Public flow: if true, pressing Public goes straight to Server List; otherwise to Public Game Choice.");
			AutoAcceptWarning = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoAcceptWarning", true, "Public flow: if true, the warning about random players is accepted automatically (I understand).");
			try
			{
				Harmony val = new Harmony("purtzel_hd.autoregion");
				Type type = AccessTools.TypeByName("MenuPageMain");
				Type type2 = AccessTools.TypeByName("MenuPageRegions");
				val.Patch((MethodBase)AccessTools.Method(type, "ButtonEventHostGame", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Patches), "FromMainMenu", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.Method(type, "ButtonEventPlayRandom", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Patches), "FromMainMenu", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.Method(AccessTools.TypeByName("MenuButton"), "OnSelect", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Patches), "ButtonSelectPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.Method(AccessTools.TypeByName("MenuPageServerList"), "ExitPage", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Patches), "ServerListExitPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.Method(type2, "Start", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Patches), "RegionsStartPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.Method(type2, "PickRegion", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Patches), "PickRegionPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Patching failed: {arg}");
			}
			Log.LogInfo((object)("AutoRegion loaded. Preferred region: '" + PreferredRegionCode.Value + "'"));
		}

		internal static string ValidCode(string code)
		{
			code = (code ?? "").Trim().ToLowerInvariant();
			if (!ValidCodes.Contains(code))
			{
				return null;
			}
			return code;
		}
	}
	internal static class Patches
	{
		private static bool _fromMainMenu;

		private const float ServerListDelay = 0.2f;

		public static void FromMainMenu()
		{
			_fromMainMenu = true;
		}

		public static bool ButtonSelectPrefix(object __instance)
		{
			try
			{
				if (!Plugin.AutoAcceptWarning.Value || __instance == null)
				{
					return true;
				}
				object? obj = AccessTools.Field(__instance.GetType(), "menuButtonPopUp")?.GetValue(__instance);
				Object val = (Object)((obj is Object) ? obj : null);
				if (val == (Object)null)
				{
					return true;
				}
				object? obj2 = AccessTools.Field(((object)val).GetType(), "option1Event")?.GetValue(val);
				UnityEvent val2 = (UnityEvent)((obj2 is UnityEvent) ? obj2 : null);
				object? obj3 = AccessTools.Field(__instance.GetType(), "button")?.GetValue(__instance);
				Button val3 = (Button)((obj3 is Button) ? obj3 : null);
				if (val2 == null)
				{
					return true;
				}
				if (!Calls((UnityEventBase)(object)val2, "ButtonEventPlayRandom") && (!((Object)(object)val3 != (Object)null) || !Calls((UnityEventBase)(object)val3.onClick, "ButtonEventPlayRandom")))
				{
					return true;
				}
				Plugin.Log.LogInfo((object)"Public warning accepted automatically.");
				val2.Invoke();
				return false;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
				return true;
			}
		}

		public static bool ServerListExitPrefix()
		{
			try
			{
				if (!Plugin.JumpStraightToServerList.Value)
				{
					return true;
				}
				Type type = AccessTools.TypeByName("MenuManager");
				object obj = AccessTools.Field(type, "instance")?.GetValue(null);
				if (obj == null)
				{
					return true;
				}
				object obj2 = Enum.Parse(AccessTools.TypeByName("MenuPageIndex"), "Main");
				AccessTools.Method(type, "PageCloseAll", (Type[])null, (Type[])null)?.Invoke(obj, null);
				object obj3 = AccessTools.Method(type, "PageOpen", (Type[])null, (Type[])null)?.Invoke(obj, new object[2] { obj2, false });
				if (obj3 != null)
				{
					AccessTools.Method(type, "PageSelectButtonByEvent", (Type[])null, (Type[])null)?.Invoke(obj, new object[2] { obj3, "ButtonEventPlayRandom" });
				}
				return false;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
				return true;
			}
		}

		private static bool Calls(UnityEventBase ev, string method)
		{
			for (int i = 0; i < ev.GetPersistentEventCount(); i++)
			{
				if (ev.GetPersistentMethodName(i) == method)
				{
					return true;
				}
			}
			return false;
		}

		public static void RegionsStartPostfix(MonoBehaviour __instance)
		{
			bool fromMainMenu = _fromMainMenu;
			_fromMainMenu = false;
			if (!fromMainMenu || !Plugin.AutoSelect.Value || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			string text = Plugin.ValidCode(Plugin.PreferredRegionCode.Value);
			if (text == null)
			{
				if (!string.IsNullOrWhiteSpace(Plugin.PreferredRegionCode.Value))
				{
					Plugin.Log.LogWarning((object)("Invalid region code '" + Plugin.PreferredRegionCode.Value + "', please pick a region manually."));
				}
			}
			else
			{
				__instance.StartCoroutine(PickLater(__instance, text));
			}
		}

		private static IEnumerator PickLater(MonoBehaviour page, string code)
		{
			yield return null;
			yield return null;
			if (!((Object)(object)page == (Object)null) && ((Behaviour)page).isActiveAndEnabled)
			{
				Plugin.Log.LogInfo((object)("Auto-selecting region '" + code + "'."));
				AccessTools.Method(((object)page).GetType(), "PickRegion", (Type[])null, (Type[])null)?.Invoke(page, new object[1] { code });
			}
		}

		public static void PickRegionPostfix(object __instance, string __0)
		{
			try
			{
				string text = Plugin.ValidCode(__0);
				if (text != null && Plugin.ValidCode(Plugin.PreferredRegionCode.Value) == null)
				{
					Plugin.PreferredRegionCode.Value = text;
					Plugin.Log.LogInfo((object)("Preferred region saved: '" + text + "'."));
				}
				object obj = AccessTools.Field(__instance.GetType(), "type")?.GetValue(__instance);
				if (obj != null && Convert.ToInt32(obj) == 1 && Plugin.JumpStraightToServerList.Value)
				{
					((MonoBehaviour)Runner.Instance).StartCoroutine(OpenServerListWhenReady((Object)((__instance is Object) ? __instance : null)));
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
			}
		}

		private static IEnumerator OpenServerListWhenReady(Object regionsPage)
		{
			float waited = 0f;
			while (waited < 0.2f)
			{
				waited += Time.unscaledDeltaTime;
				yield return null;
			}
			Type type = AccessTools.TypeByName("MenuManager");
			object obj = AccessTools.Field(type, "instance")?.GetValue(null);
			if (obj != null)
			{
				object obj2 = AccessTools.Field(type, "currentMenuPageIndex")?.GetValue(obj);
				if (obj2 == null || !(obj2.ToString() != "PublicGameChoice") || !(obj2.ToString() != "Regions"))
				{
					object obj3 = Enum.Parse(AccessTools.TypeByName("MenuPageIndex"), "ServerList");
					AccessTools.Method(type, "PageCloseAll", (Type[])null, (Type[])null)?.Invoke(obj, null);
					AccessTools.Method(type, "PageOpen", (Type[])null, (Type[])null)?.Invoke(obj, new object[2] { obj3, false });
					Plugin.Log.LogInfo((object)$"Opened server list ({waited:0.00} s after region pick).");
				}
			}
		}
	}
	internal class Runner : MonoBehaviour
	{
		private static Runner _instance;

		public static Runner Instance
		{
			get
			{
				//IL_0018: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Expected O, but got Unknown
				//IL_0023: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)_instance != (Object)null)
				{
					return _instance;
				}
				GameObject val = new GameObject("AutoRegionRunner");
				Object.DontDestroyOnLoad((Object)val);
				((Object)val).hideFlags = (HideFlags)61;
				_instance = val.AddComponent<Runner>();
				return _instance;
			}
		}
	}
}