Decompiled source of ArenaMode v1.0.1

ArenaMode.dll

Decompiled 8 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
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: AssemblyVersion("0.0.0.0")]
namespace ArenaMode;

[BepInPlugin("lyj_0322.ArenaMode", "Arena Mode", "1.0.1")]
public class ArenaPlugin : BaseUnityPlugin
{
	internal const string Guid = "lyj_0322.ArenaMode";

	internal const string DisplayName = "Arena Mode";

	internal const string Version = "1.0.1";

	internal static ManualLogSource Log;

	internal static bool DumpRequested;

	internal static bool FlowRequested;

	private void Awake()
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			string[] commandLineArgs = Environment.GetCommandLineArgs();
			for (int i = 0; i < commandLineArgs.Length; i++)
			{
				if (string.Equals(commandLineArgs[i], "--arena-dump", StringComparison.OrdinalIgnoreCase))
				{
					DumpRequested = true;
				}
				if (string.Equals(commandLineArgs[i], "--arena-flow", StringComparison.OrdinalIgnoreCase))
				{
					FlowRequested = true;
				}
			}
		}
		catch
		{
		}
		try
		{
			Harmony val = new Harmony("lyj_0322.ArenaMode");
			val.PatchAll(typeof(Patch_MenuPageMain_Start));
			val.PatchAll(typeof(Patch_MenuPageLobby_Start));
			val.PatchAll(typeof(Patch_MenuPageRegions_PickRegion));
			val.PatchAll(typeof(Patch_MenuPageLobby_ButtonStart));
			Logx.Msg("补丁已挂上:" + val.GetPatchedMethods().Count() + " 个方法");
		}
		catch (Exception ex)
		{
			Logx.Err("挂补丁失败:" + ex);
		}
		if (DumpRequested || FlowRequested)
		{
			SelfTest.Install(FlowRequested);
		}
		Logx.Msg("Arena Mode v1.0.1 已加载");
	}
}
internal static class Logx
{
	internal static ManualLogSource Raw => ArenaPlugin.Log;

	internal static void Msg(string s)
	{
		if (ArenaPlugin.Log != null)
		{
			ArenaPlugin.Log.LogInfo((object)s);
		}
	}

	internal static void Warn(string s)
	{
		if (ArenaPlugin.Log != null)
		{
			ArenaPlugin.Log.LogWarning((object)s);
		}
	}

	internal static void Err(string s)
	{
		if (ArenaPlugin.Log != null)
		{
			ArenaPlugin.Log.LogError((object)s);
		}
	}
}
[HarmonyPatch(typeof(MenuPageMain), "Start")]
internal static class Patch_MenuPageMain_Start
{
	private static void Postfix(MenuPageMain __instance)
	{
		try
		{
			ModeState.Active = false;
			ModeState.Mode = FightMode.Arena;
			UiInject.AddMainButton(__instance);
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 主菜单注入失败: " + ex);
		}
	}
}
[HarmonyPatch(typeof(MenuPageLobby), "Start")]
internal static class Patch_MenuPageLobby_Start
{
	private static void Postfix(MenuPageLobby __instance)
	{
		try
		{
			UiInject.AddLobbySelector(__instance);
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 大厅页注入失败: " + ex);
		}
	}
}
[HarmonyPatch(typeof(MenuPageRegions), "PickRegion")]
internal static class Patch_MenuPageRegions_PickRegion
{
	private static void Postfix()
	{
		if (!ModeState.Active)
		{
			return;
		}
		try
		{
			Logx.Msg("[竞技] 跳过选存档,直接建房");
			MenuManager.instance.PageCloseAll();
			SemiFunc.MenuActionHostGame((string)null, (List<string>)null);
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 直接建房失败: " + ex);
		}
	}
}
[HarmonyPatch(typeof(MenuPageLobby), "ButtonStart")]
internal static class Patch_MenuPageLobby_ButtonStart
{
	private static void Prefix()
	{
		try
		{
			ModeState.Apply();
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 开始游戏前设置关卡失败: " + ex);
		}
	}

	private static void Postfix()
	{
		ModeState.ClearDebugLevel();
	}
}
internal enum FightMode
{
	Arena,
	Race
}
internal static class ModeState
{
	internal static bool Active;

	internal static FightMode Mode;

	private static bool _applied;

	private const string ArenaName = "Arena Fight";

	private const string RaceName = "Arena Race";

	private static FieldInfo _debugLevel;

	internal static Level PickLevel(RunManager rm)
	{
		if ((Object)(object)rm == (Object)null || rm.levelArena == null || rm.levelArena.Count == 0)
		{
			return null;
		}
		string value = ((Mode == FightMode.Race) ? "Arena Race" : "Arena Fight");
		Level val = null;
		for (int i = 0; i < rm.levelArena.Count; i++)
		{
			Level val2 = rm.levelArena[i];
			if (!((Object)(object)val2 == (Object)null))
			{
				string text = ((Object)val2).name ?? string.Empty;
				if (text.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return val2;
				}
				if (Mode == FightMode.Arena && text.IndexOf("Race", StringComparison.OrdinalIgnoreCase) < 0 && (Object)(object)val == (Object)null)
				{
					val = val2;
				}
			}
		}
		return val;
	}

	internal static void Apply()
	{
		if (!Active)
		{
			return;
		}
		try
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				Logx.Msg("[竞技] 不是房主,关卡由房主那边同步");
				return;
			}
			RunManager instance = RunManager.instance;
			Level val = PickLevel(instance);
			if ((Object)(object)val == (Object)null)
			{
				Logx.Warn("[竞技] 找不到对应关卡,按原版流程走");
				return;
			}
			SetDebugLevel(instance, val);
			_applied = true;
			Logx.Msg("[竞技] 本局进入 " + ((Object)val).name + "(模式 " + Mode.ToString() + ")");
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 设置关卡失败: " + ex);
		}
	}

	internal static void ClearDebugLevel()
	{
		if (!_applied)
		{
			return;
		}
		_applied = false;
		try
		{
			RunManager instance = RunManager.instance;
			if ((Object)(object)instance != (Object)null && (Object)(object)GetDebugLevel(instance) != (Object)null)
			{
				SetDebugLevel(instance, null);
			}
		}
		catch
		{
		}
	}

	private static FieldInfo DebugLevelField()
	{
		if (_debugLevel == null)
		{
			_debugLevel = typeof(RunManager).GetField("debugLevel", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (_debugLevel == null)
			{
				Logx.Err("[竞技] 找不到 RunManager.debugLevel,功能会失效");
			}
		}
		return _debugLevel;
	}

	private static Level GetDebugLevel(RunManager rm)
	{
		FieldInfo fieldInfo = DebugLevelField();
		if (!(fieldInfo != null))
		{
			return null;
		}
		object? value = fieldInfo.GetValue(rm);
		return (Level)((value is Level) ? value : null);
	}

	internal static Level PeekDebugLevel()
	{
		try
		{
			return GetDebugLevel(RunManager.instance);
		}
		catch
		{
			return null;
		}
	}

	private static void SetDebugLevel(RunManager rm, Level lv)
	{
		FieldInfo fieldInfo = DebugLevelField();
		if (fieldInfo != null)
		{
			fieldInfo.SetValue(rm, lv);
		}
	}

	internal static bool UseChinese()
	{
		try
		{
			LocalizationManager instance = LocalizationManager.instance;
			if ((Object)(object)instance != (Object)null && (Object)(object)instance.ActiveLocale != (Object)null)
			{
				string text = instance.ActiveLocale.NativeName ?? string.Empty;
				for (int i = 0; i < text.Length; i++)
				{
					if (text[i] >= '一' && text[i] <= '鿿')
					{
						return true;
					}
				}
			}
		}
		catch
		{
		}
		return false;
	}

	internal static string Label(FightMode m)
	{
		bool flag = UseChinese();
		if (m == FightMode.Race)
		{
			if (!flag)
			{
				return "Racing";
			}
			return "赛车";
		}
		if (!flag)
		{
			return "Arena";
		}
		return "角斗场";
	}

	internal static string EntryLabel()
	{
		if (!UseChinese())
		{
			return "Arena Mode";
		}
		return "竞技模式";
	}
}
internal static class SelfTest
{
	private sealed class Runner : MonoBehaviour
	{
		internal bool FlowMode;

		private void Start()
		{
			((MonoBehaviour)this).StartCoroutine(FlowMode ? FullFlow() : Flow());
		}

		private IEnumerator FullFlow()
		{
			yield return (object)new WaitForSecondsRealtime(14f);
			float waited = 0f;
			while (((Object)(object)MenuPageMain.instance == (Object)null || !((Component)MenuPageMain.instance).gameObject.activeInHierarchy) && waited < 25f)
			{
				waited += Time.unscaledDeltaTime;
				yield return null;
			}
			yield return (object)new WaitForSecondsRealtime(2f);
			Transform val = FindDeep(((Component)MenuPageMain.instance).transform, "Menu Button - Arena Mode");
			Button val2 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<Button>() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				Logx.Err("[flow] 找不到竞技模式按钮");
				Application.Quit();
				yield break;
			}
			((UnityEvent)val2.onClick).Invoke();
			Logx.Msg("[flow] 已点击竞技模式");
			yield return (object)new WaitForSecondsRealtime(6f);
			GameObject val3 = GameObject.Find("Menu Page Regions(Clone)");
			if ((Object)(object)val3 == (Object)null)
			{
				Logx.Err("[flow] 区域页没打开");
				Application.Quit();
				yield break;
			}
			Logx.Msg("[flow] 区域页已打开,准备选区域");
			val3.GetComponent<MenuPageRegions>().PickRegion("asia");
			Logx.Msg("[flow] 已选区域 asia,等建房");
			float t = 0f;
			bool pwdDone = false;
			while (t < 70f && (Object)(object)MenuPageLobby.instance == (Object)null)
			{
				if (!pwdDone)
				{
					MenuPagePassword val4 = Object.FindFirstObjectByType<MenuPagePassword>();
					if ((Object)(object)val4 != (Object)null)
					{
						Logx.Msg("[flow] 有密码页,点跳过");
						val4.ConfirmButton();
						pwdDone = true;
					}
				}
				t += Time.unscaledDeltaTime;
				yield return null;
			}
			Logx.Msg("[flow] 当前关卡 = " + (((Object)(object)RunManager.instance.levelCurrent != (Object)null) ? ((Object)RunManager.instance.levelCurrent).name : "?") + ",等了 " + t.ToString("0") + "s");
			Logx.Msg("[flow] 大厅页 " + (((Object)(object)MenuPageLobby.instance != (Object)null) ? "已打开" : "没出现"));
			yield return Shot("flow_lobby.png");
			if ((Object)(object)MenuPageLobby.instance != (Object)null)
			{
				ModeState.Active = true;
				ModeState.Mode = FightMode.Arena;
				Logx.Msg("[flow] 按下开始游戏(角斗场)");
				MenuPageLobby.instance.ButtonStart();
				yield return (object)new WaitForSecondsRealtime(30f);
				Logx.Msg("[flow] 开始后关卡 = " + (((Object)(object)RunManager.instance.levelCurrent != (Object)null) ? ((Object)RunManager.instance.levelCurrent).name : "?"));
				Logx.Msg("[flow] RunIsArena = " + SemiFunc.RunIsArena());
				yield return Shot("flow_arena.png");
			}
			yield return (object)new WaitForSecondsRealtime(0.6f);
			Application.Quit();
		}

		private IEnumerator Flow()
		{
			yield return (object)new WaitForSecondsRealtime(14f);
			float waited = 0f;
			while (((Object)(object)MenuPageMain.instance == (Object)null || !((Component)MenuPageMain.instance).gameObject.activeInHierarchy) && waited < 25f)
			{
				waited += Time.unscaledDeltaTime;
				yield return null;
			}
			yield return (object)new WaitForSecondsRealtime(2f);
			string error = null;
			try
			{
				DumpLevels();
				DumpPage("主菜单(已注入)", ((Component)MenuPageMain.instance).gameObject);
			}
			catch (Exception ex)
			{
				error = ex.ToString();
			}
			yield return Shot("arena_menu.png");
			try
			{
				Transform val = FindDeep(((Component)MenuPageMain.instance).transform, "Menu Button - Arena Mode");
				Button val2 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<Button>() : null);
				if ((Object)(object)val2 != (Object)null)
				{
					((UnityEvent)val2.onClick).Invoke();
					Logx.Msg("[dump] 已点击竞技模式按钮");
				}
				else
				{
					Logx.Warn("[dump] 找不到竞技模式按钮");
				}
			}
			catch (Exception ex2)
			{
				error = error + "\n" + ex2;
			}
			yield return (object)new WaitForSecondsRealtime(2.5f);
			try
			{
				GameObject val3 = GameObject.Find("Menu Page Regions(Clone)");
				Logx.Msg("[dump] 区域选择页" + (((Object)(object)val3 != (Object)null) ? "已打开(原版开房流程)" : "没有出现"));
			}
			catch (Exception ex3)
			{
				Logx.Warn("[dump] 检查区域页失败: " + ex3.Message);
			}
			yield return Shot("arena_host.png");
			MenuPage lobby = null;
			MenuManager.instance.PageCloseAll();
			yield return (object)new WaitForSecondsRealtime(0.8f);
			try
			{
				lobby = MenuManager.instance.PageOpen((MenuPageIndex)8, false);
				if ((Object)(object)lobby != (Object)null)
				{
					DumpPage("大厅页(未注入)", ((Component)lobby).gameObject);
					ModeState.Active = true;
					ModeState.Mode = FightMode.Arena;
					UiInject.AddLobbySelector(((Component)lobby).GetComponent<MenuPageLobby>());
					DumpPage("大厅页(注入后)", ((Component)lobby).gameObject);
					VerifyLevelPick(FightMode.Arena);
					VerifyLevelPick(FightMode.Race);
				}
			}
			catch (Exception ex4)
			{
				error = error + "\n" + ex4;
			}
			yield return (object)new WaitForSecondsRealtime(1f);
			if ((Object)(object)lobby != (Object)null)
			{
				yield return Shot("arena_lobby.png");
			}
			if (error != null)
			{
				Logx.Err("[dump] 出错: " + error);
			}
			yield return (object)new WaitForSecondsRealtime(0.6f);
			Application.Quit();
		}

		private IEnumerator Shot(string file)
		{
			string fullPath = Path.GetFullPath(Path.Combine(Application.dataPath, "..", file));
			ScreenCapture.CaptureScreenshot(fullPath);
			Logx.Msg("[dump] 截图 " + fullPath);
			yield return (object)new WaitForSecondsRealtime(0.8f);
		}
	}

	internal static void Install(bool flow)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		GameObject val = new GameObject("ArenaModeSelfTest");
		Object.DontDestroyOnLoad((Object)val);
		val.AddComponent<Runner>().FlowMode = flow;
	}

	private static Transform FindDeep(Transform root, string name)
	{
		if (((Object)root).name == name)
		{
			return root;
		}
		for (int i = 0; i < root.childCount; i++)
		{
			Transform val = FindDeep(root.GetChild(i), name);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
		}
		return null;
	}

	private static void VerifyLevelPick(FightMode mode)
	{
		ModeState.Active = true;
		ModeState.Mode = mode;
		ModeState.Apply();
		Level val = ModeState.PeekDebugLevel();
		Logx.Msg("[dump] 模式 " + mode.ToString() + " → debugLevel=" + (((Object)(object)val != (Object)null) ? ((Object)val).name : "null"));
		ModeState.ClearDebugLevel();
		Logx.Msg("[dump] 清空后 debugLevel=" + (((Object)(object)ModeState.PeekDebugLevel() != (Object)null) ? "还在" : "null"));
	}

	private static void DumpLevels()
	{
		RunManager instance = RunManager.instance;
		if ((Object)(object)instance == (Object)null)
		{
			Logx.Msg("[dump] RunManager 为空");
			return;
		}
		Logx.Msg("[dump] levelArena 共 " + (instance.levelArena?.Count ?? 0) + " 个:");
		if (instance.levelArena == null)
		{
			return;
		}
		foreach (Level item in instance.levelArena)
		{
			if ((Object)(object)item == (Object)null)
			{
				Logx.Msg("  (null)");
			}
			else
			{
				Logx.Msg("  name='" + ((Object)item).name + "' status=" + ((object)Unsafe.As<Status, Status>(ref item.status)/*cast due to .constrained prefix*/).ToString());
			}
		}
	}

	private static void DumpPage(string title, GameObject root)
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("[dump] ").Append(title).Append(":");
		Walk(root.transform, 0, stringBuilder);
		Logx.Msg(stringBuilder.ToString());
	}

	private static void Walk(Transform t, int depth, StringBuilder sb)
	{
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		RectTransform val = (RectTransform)(object)((t is RectTransform) ? t : null);
		sb.Append('\n').Append(new string(' ', depth * 2)).Append(((Object)t).name)
			.Append(((Component)t).gameObject.activeSelf ? "" : "(停用)");
		string[] array = (from c in ((Component)t).GetComponents<Component>()
			where (Object)(object)c != (Object)null
			select ((object)c).GetType().Name into n
			where n != "Transform" && n != "RectTransform" && n != "CanvasRenderer"
			select n).ToArray();
		if (array.Length != 0)
		{
			sb.Append(" {").Append(string.Join(",", array)).Append('}');
		}
		if ((Object)(object)val != (Object)null)
		{
			sb.Append(" pos=").Append(((Transform)val).localPosition.x.ToString("0.#")).Append(',')
				.Append(((Transform)val).localPosition.y.ToString("0.#"))
				.Append(" size=")
				.Append(val.sizeDelta.x.ToString("0.#"))
				.Append('x')
				.Append(val.sizeDelta.y.ToString("0.#"));
		}
		TMP_Text component = ((Component)t).GetComponent<TMP_Text>();
		if ((Object)(object)component != (Object)null)
		{
			sb.Append(" text='").Append(component.text).Append('\'');
		}
		Button component2 = ((Component)t).GetComponent<Button>();
		if ((Object)(object)component2 != (Object)null)
		{
			sb.Append(" onClick=[");
			for (int num = 0; num < ((UnityEventBase)component2.onClick).GetPersistentEventCount(); num++)
			{
				if (num > 0)
				{
					sb.Append(',');
				}
				Object persistentTarget = ((UnityEventBase)component2.onClick).GetPersistentTarget(num);
				sb.Append((persistentTarget != (Object)null) ? (((object)persistentTarget).GetType().Name + ".") : "?.");
				sb.Append(((UnityEventBase)component2.onClick).GetPersistentMethodName(num));
			}
			sb.Append(']');
		}
		MenuButton component3 = ((Component)t).GetComponent<MenuButton>();
		if ((Object)(object)component3 != (Object)null)
		{
			sb.Append(" [MenuButton '").Append(component3.buttonTextString).Append("' id='")
				.Append(component3.MenuID)
				.Append("']");
		}
		LayoutGroup component4 = ((Component)t).GetComponent<LayoutGroup>();
		if ((Object)(object)component4 != (Object)null)
		{
			sb.Append(" <").Append(((object)component4).GetType().Name).Append('>');
		}
		for (int num2 = 0; num2 < t.childCount; num2++)
		{
			Walk(t.GetChild(num2), depth + 1, sb);
		}
	}
}
internal static class UiInject
{
	private sealed class AlignLater : MonoBehaviour
	{
		private const int StartFrame = 2;

		private const int StopFrame = 45;

		public MenuPageLobby page;

		public RectTransform holder;

		public RectTransform customize;

		public RectTransform settings;

		public RectTransform start;

		private int _frames;

		private void LateUpdate()
		{
			_frames++;
			if (_frames < 2)
			{
				return;
			}
			if ((Object)(object)holder == (Object)null || (Object)(object)page == (Object)null)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			Transform transform = ((Component)page).transform;
			RectTransform val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			if ((Object)(object)val != (Object)null)
			{
				LayoutRebuilder.ForceRebuildLayoutImmediate(val);
			}
			AlignToColumns(page, holder, customize, settings, start, _frames >= 45);
			if (_frames >= 45)
			{
				Object.Destroy((Object)(object)this);
			}
		}
	}

	private const string MainButtonName = "Menu Button - Arena Mode";

	private const string SelectorName = "Arena Mode Selector";

	private static readonly Vector2 MainButtonPos = new Vector2(0f, 260f);

	private static readonly Vector2 SelectorPos = new Vector2(180f, 128f);

	internal static void AddMainButton(MenuPageMain page)
	{
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)page == (Object)null)
		{
			return;
		}
		Transform val = ((Component)page).transform.Find("LeftTransform/Buttons");
		if ((Object)(object)val == (Object)null)
		{
			Logx.Warn("[竞技] 找不到主菜单按钮组");
		}
		else
		{
			if ((Object)(object)val.Find("Menu Button - Arena Mode") != (Object)null)
			{
				return;
			}
			Transform obj = val.Find("Menu Button - singleplayer");
			RectTransform val2 = (RectTransform)(object)((obj is RectTransform) ? obj : null);
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = FirstButton(val);
			}
			if ((Object)(object)val2 == (Object)null)
			{
				Logx.Warn("[竞技] 找不到可克隆的主菜单按钮");
				return;
			}
			MenuButton val3 = CloneButton(val2, val, "Menu Button - Arena Mode", ModeState.EntryLabel(), OnMainButton);
			if (!((Object)(object)val3 == (Object)null))
			{
				Transform transform = ((Component)val3).transform;
				Transform obj2 = ((transform is RectTransform) ? transform : null);
				obj2.SetSiblingIndex(0);
				obj2.localPosition = Vector2.op_Implicit(MainButtonPos);
				val3.ResizeToText();
				obj2.localPosition = Vector2.op_Implicit(MainButtonPos);
				bool flag = (Object)(object)((Component)val).GetComponent<LayoutGroup>() != (Object)null;
				Logx.Msg("[竞技] 主菜单入口已加入(" + (flag ? "按钮组带布局,排在第一位" : "按坐标摆在顶部") + "),文字 '" + ModeState.EntryLabel() + "'");
			}
		}
	}

	private static void OnMainButton()
	{
		try
		{
			ModeState.Active = true;
			ModeState.Mode = FightMode.Arena;
			Logx.Msg("[竞技] 进入竞技模式开房流程");
			MenuPageMain instance = MenuPageMain.instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.ButtonEventHostGame();
			}
			else
			{
				Logx.Warn("[竞技] 主菜单页不见了,没法开房");
			}
		}
		catch (Exception ex)
		{
			Logx.Err("[竞技] 打开开房流程失败: " + ex);
		}
	}

	internal static void AddLobbySelector(MenuPageLobby page)
	{
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Expected O, but got Unknown
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_020f: Unknown result type (might be due to invalid IL or missing references)
		//IL_022f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)page == (Object)null || !ModeState.Active)
		{
			return;
		}
		Transform transform = ((Component)page).transform;
		if ((Object)(object)transform.Find("Arena Mode Selector") != (Object)null)
		{
			return;
		}
		MenuButton startButton = page.startButton;
		if (!((Object)(object)startButton == (Object)null) && !((Component)startButton).gameObject.activeSelf)
		{
			Logx.Msg("[竞技] 非房主,不显示模式选择");
			return;
		}
		RectTransform val = FindButton(transform, "Menu Button - Settings");
		if ((Object)(object)val == (Object)null)
		{
			Logx.Warn("[竞技] 找不到可克隆的大厅按钮");
			return;
		}
		RectTransform customize = FindButton(transform, "Menu Button - Customize");
		GameObject val2 = new GameObject("Arena Mode Selector", new Type[1] { typeof(RectTransform) });
		RectTransform val3 = (RectTransform)val2.transform;
		((Transform)val3).SetParent(transform, false);
		Vector2 anchorMin = (val3.anchorMax = Vector2.zero);
		val3.anchorMin = anchorMin;
		val3.pivot = new Vector2(0.5f, 0.5f);
		val3.anchoredPosition = SelectorPos;
		val3.sizeDelta = new Vector2(240f, 40f);
		MenuButton obj = CloneButton(val, (Transform)(object)val3, "Mode - Arena", ModeState.Label(FightMode.Arena), delegate
		{
			ChooseMode(FightMode.Arena);
		});
		MenuButton val4 = CloneButton(val, (Transform)(object)val3, "Mode - Race", ModeState.Label(FightMode.Race), delegate
		{
			ChooseMode(FightMode.Race);
		});
		if (!((Object)(object)obj == (Object)null) && !((Object)(object)val4 == (Object)null))
		{
			Transform transform2 = ((Component)startButton).transform;
			AlignToColumns(page, val3, customize, val, (RectTransform)(object)((transform2 is RectTransform) ? transform2 : null), log: false);
			AlignLater alignLater = val2.AddComponent<AlignLater>();
			alignLater.page = page;
			alignLater.holder = val3;
			alignLater.customize = customize;
			alignLater.settings = val;
			ref RectTransform start = ref alignLater.start;
			Transform transform3 = ((Component)startButton).transform;
			start = (RectTransform)(object)((transform3 is RectTransform) ? transform3 : null);
			RefreshSelection(val3);
			Logx.Msg("[竞技] 大厅模式选择已加入,当前位置 " + val3.anchoredPosition.x.ToString("0.#") + "," + val3.anchoredPosition.y.ToString("0.#") + ",宽度 " + val3.sizeDelta.x.ToString("0.#"));
		}
	}

	private static void AlignToColumns(MenuPageLobby page, RectTransform holder, RectTransform customize, RectTransform settings, RectTransform start, bool log = true)
	{
		//IL_0059: 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_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_0195: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		Transform transform = ((Component)page).transform;
		RectTransform val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		if ((Object)(object)customize == (Object)null)
		{
			Logx.Warn("[竞技] 拿不到「外观」的位置,模式选择沿用默认坐标");
			return;
		}
		TMP_Text componentInChildren = ((Component)customize).GetComponentInChildren<TMP_Text>(true);
		Rect val2;
		float y;
		if (!((Object)(object)componentInChildren != (Object)null))
		{
			val2 = LocalRect(val, customize);
			y = ((Rect)(ref val2)).center.y;
		}
		else
		{
			y = ((Transform)val).InverseTransformPoint(componentInChildren.transform.position).y;
		}
		float num = y;
		holder.anchoredPosition = new Vector2(0f, num);
		holder.sizeDelta = new Vector2(0f, 40f);
		if (((Transform)holder).childCount >= 2)
		{
			Transform child = ((Transform)holder).GetChild(0);
			RectTransform val3 = (RectTransform)(object)((child is RectTransform) ? child : null);
			Transform child2 = ((Transform)holder).GetChild(1);
			RectTransform val4 = (RectTransform)(object)((child2 is RectTransform) ? child2 : null);
			Rect val5 = (((Object)(object)settings != (Object)null) ? LocalRect(val, settings) : LocalRect(val, customize));
			Rect val6 = (((Object)(object)start != (Object)null) ? LocalRect(val, start) : val5);
			PlaceAtPageX(val3, val, ((Rect)(ref val5)).center.x);
			PlaceAtPageX(val4, val, ((Rect)(ref val6)).center.x);
			if (log)
			{
				string[] obj = new string[10]
				{
					"[竞技] 对位:设置中心x=",
					((Rect)(ref val5)).center.x.ToString("0.#"),
					" 开始游戏中心x=",
					((Rect)(ref val6)).center.x.ToString("0.#"),
					" 行高y=",
					num.ToString("0.#"),
					" → 角斗场 ",
					null,
					null,
					null
				};
				val2 = LocalRect(val, val3);
				obj[7] = ((Rect)(ref val2)).center.x.ToString("0.#");
				obj[8] = " / 赛车 ";
				val2 = LocalRect(val, val4);
				obj[9] = ((Rect)(ref val2)).center.x.ToString("0.#");
				Logx.Msg(string.Concat(obj));
			}
		}
	}

	private static void PlaceAtPageX(RectTransform child, RectTransform root, float pageX)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)child == (Object)null))
		{
			((Transform)child).localPosition = new Vector3(pageX, 0f, 0f);
			Rect val = LocalRect(root, child);
			((Transform)child).localPosition = ((Transform)child).localPosition + new Vector3(pageX - ((Rect)(ref val)).center.x, 0f, 0f);
		}
	}

	private static Rect LocalRect(RectTransform root, RectTransform rt)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: 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_002a: 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_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		rt.GetWorldCorners(array);
		Vector3 val = ((Transform)root).InverseTransformPoint(array[0]);
		Vector3 val2 = ((Transform)root).InverseTransformPoint(array[2]);
		return Rect.MinMaxRect(val.x, val.y, val2.x, val2.y);
	}

	private static void ChooseMode(FightMode mode)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected O, but got Unknown
		ModeState.Mode = mode;
		MenuPageLobby instance = MenuPageLobby.instance;
		Transform val = (((Object)(object)instance != (Object)null) ? ((Component)instance).transform.Find("Arena Mode Selector") : null);
		if ((Object)(object)val != (Object)null)
		{
			RefreshSelection((RectTransform)val);
		}
		Logx.Msg("[竞技] 模式切换为 " + mode);
	}

	private static void RefreshSelection(RectTransform holder)
	{
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < ((Transform)holder).childCount; i++)
		{
			Transform child = ((Transform)holder).GetChild(i);
			MenuButton component = ((Component)child).GetComponent<MenuButton>();
			if (!((Object)(object)component == (Object)null))
			{
				bool flag = ((Object)child).name == "Mode - " + ModeState.Mode;
				component.colorNormal = (Color)(flag ? new Color(1f, 0.85f, 0.05f) : Color.gray);
				component.colorHover = Color.white;
				component.colorClick = new Color(1f, 0.55f, 0f);
			}
		}
	}

	private static MenuButton CloneButton(RectTransform src, Transform parent, string name, string label, Action onClick)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Expected O, but got Unknown
		if ((Object)(object)src == (Object)null || (Object)(object)parent == (Object)null)
		{
			return null;
		}
		GameObject obj = Object.Instantiate<GameObject>(((Component)src).gameObject, parent, false);
		((Object)obj).name = name;
		obj.SetActive(true);
		MenuButtonPopUp component = obj.GetComponent<MenuButtonPopUp>();
		if ((Object)(object)component != (Object)null)
		{
			Object.DestroyImmediate((Object)(object)component);
		}
		Button component2 = obj.GetComponent<Button>();
		if ((Object)(object)component2 != (Object)null)
		{
			component2.onClick = new ButtonClickedEvent();
			((UnityEvent)component2.onClick).AddListener((UnityAction)delegate
			{
				onClick();
			});
		}
		SetLabel(obj, label);
		MenuButton component3 = obj.GetComponent<MenuButton>();
		if ((Object)(object)component3 != (Object)null)
		{
			component3.buttonTextString = label;
			component3.ResizeToText();
		}
		return component3;
	}

	private static void SetLabel(GameObject go, string text)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = Vector2.op_Implicit(go.transform.localPosition);
		TMP_Text[] componentsInChildren = go.GetComponentsInChildren<TMP_Text>(true);
		foreach (TMP_Text val2 in componentsInChildren)
		{
			MonoBehaviour[] components = ((Component)val2).GetComponents<MonoBehaviour>();
			foreach (MonoBehaviour val3 in components)
			{
				if (!((Object)(object)val3 == (Object)null))
				{
					string name = ((object)val3).GetType().Name;
					if (name == "LocalizeChangedUI" || name == "LocalizationChangedEvent")
					{
						Object.DestroyImmediate((Object)(object)val3);
					}
				}
			}
			val2.text = text;
		}
		go.transform.localPosition = Vector2.op_Implicit(val);
	}

	private static RectTransform FirstButton(Transform parent)
	{
		MenuButton componentInChildren = ((Component)parent).GetComponentInChildren<MenuButton>(true);
		if (!((Object)(object)componentInChildren != (Object)null))
		{
			return null;
		}
		Transform transform = ((Component)componentInChildren).transform;
		return (RectTransform)(object)((transform is RectTransform) ? transform : null);
	}

	private static RectTransform FindButton(Transform root, string name)
	{
		Transform obj = root.Find(name);
		RectTransform val = (RectTransform)(object)((obj is RectTransform) ? obj : null);
		if ((Object)(object)val != (Object)null)
		{
			return val;
		}
		MenuButton[] componentsInChildren = ((Component)root).GetComponentsInChildren<MenuButton>(true);
		foreach (MenuButton val2 in componentsInChildren)
		{
			if ((Object)(object)val2 != (Object)null && ((Object)((Component)val2).gameObject).name == name)
			{
				Transform transform = ((Component)val2).transform;
				return (RectTransform)(object)((transform is RectTransform) ? transform : null);
			}
		}
		return null;
	}
}