Decompiled source of SimpleLateJoin v0.1.0

plugins/SimpleLateJoin.dll

Decompiled 4 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
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: AssemblyCompany("SimpleLateJoin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("SimpleLateJoin")]
[assembly: AssemblyTitle("SimpleLateJoin")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 SimpleLateJoin
{
	internal sealed class LateJoinButton : MonoBehaviour
	{
		private static readonly Color OffColor = new Color(0.72f, 0.76f, 0.81f);

		private static readonly Color OnColor = new Color(0.65f, 1f, 0.79f);

		private GameObject entry;

		private MenuButton menuButton;

		private MenuSelectableElement selection;

		private MenuSelectableElement? quitSelection;

		private TextMeshProUGUI label;

		private Button button;

		internal void Build(MenuPageEsc page)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_007d: 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_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Expected O, but got Unknown
			//IL_0182: 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_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI val = (Object.op_Implicit((Object)(object)page.quitButton) ? ((Component)page.quitButton).GetComponentInChildren<TextMeshProUGUI>(true) : null);
			entry = new GameObject("Simple Late Join", new Type[1] { typeof(RectTransform) });
			entry.SetActive(false);
			RectTransform val2 = (RectTransform)entry.transform;
			((Transform)val2).SetParent(((Component)page).transform, false);
			Vector2 val3 = default(Vector2);
			((Vector2)(ref val3))..ctor(0.5f, 0f);
			val2.anchorMax = val3;
			val2.anchorMin = val3;
			val2.anchoredPosition = new Vector2(0f, 37f);
			val2.sizeDelta = new Vector2(194f, 34f);
			Image val4 = entry.AddComponent<Image>();
			((Graphic)val4).raycastTarget = false;
			button = entry.AddComponent<Button>();
			((Selectable)button).targetGraphic = (Graphic)(object)val4;
			((Selectable)button).transition = (Transition)0;
			Button obj = button;
			Navigation navigation = default(Navigation);
			((Navigation)(ref navigation)).mode = (Mode)0;
			((Selectable)obj).navigation = navigation;
			((UnityEvent)button.onClick).AddListener(new UnityAction(Toggle));
			RectTransform val5 = AddChild(val2, "Select Late Join");
			selection = ((Component)val5).gameObject.AddComponent<MenuSelectableElement>();
			if (Object.op_Implicit((Object)(object)page.quitButton) && Object.op_Implicit((Object)(object)page.quitButton.rectTransformSelection))
			{
				quitSelection = ((Component)page.quitButton.rectTransformSelection).GetComponent<MenuSelectableElement>();
				Navigation navigation2 = ((Selectable)selection).navigation;
				((Navigation)(ref navigation2)).selectOnUp = (Selectable)(object)quitSelection;
				((Selectable)selection).navigation = navigation2;
			}
			label = ((Component)AddChild(val2, "Late Join status")).gameObject.AddComponent<TextMeshProUGUI>();
			if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)((TMP_Text)val).font))
			{
				((TMP_Text)label).font = ((TMP_Text)val).font;
				((TMP_Text)label).fontSharedMaterial = ((TMP_Text)val).fontSharedMaterial;
			}
			((Graphic)label).raycastTarget = false;
			((TMP_Text)label).fontSize = (Object.op_Implicit((Object)(object)val) ? ((TMP_Text)val).fontSize : 24f);
			((TMP_Text)label).alignment = (TextAlignmentOptions)4098;
			((TMP_Text)label).text = "Late Join: OFF";
			menuButton = entry.AddComponent<MenuButton>();
			menuButton.rectTransformSelection = val5;
			menuButton.resizeButton = false;
			menuButton.customHoverArea = true;
			menuButton.customColors = true;
			menuButton.colorNormal = OffColor;
			menuButton.colorHover = Color.white;
			menuButton.colorClick = new Color(1f, 0.72f, 0.3f);
			Update();
		}

		private static RectTransform AddChild(RectTransform parent, string name)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			RectTransform val = (RectTransform)new GameObject(name, new Type[1] { typeof(RectTransform) }).transform;
			((Transform)val).SetParent((Transform)(object)parent, false);
			val.anchorMin = Vector2.zero;
			val.anchorMax = Vector2.one;
			Vector2 offsetMin = (val.offsetMax = Vector2.zero);
			val.offsetMin = offsetMin;
			return val;
		}

		private void Update()
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)entry))
			{
				return;
			}
			bool flag = SimpleLateJoinPlugin.IsHostInRun();
			SimpleLateJoinPlugin instance = SimpleLateJoinPlugin.Instance;
			bool flag2 = flag && (instance?.Available ?? false);
			if (entry.activeSelf != flag)
			{
				entry.SetActive(flag);
			}
			if (Object.op_Implicit((Object)(object)quitSelection))
			{
				Navigation navigation = ((Selectable)quitSelection).navigation;
				Selectable val = (Selectable)(flag2 ? ((object)quitSelection) : ((object)selection));
				if ((Object)(object)((Navigation)(ref navigation)).selectOnDown == (Object)(object)val)
				{
					((Navigation)(ref navigation)).selectOnDown = (Selectable)(flag2 ? ((object)selection) : ((object)quitSelection));
					((Selectable)quitSelection).navigation = navigation;
				}
			}
			if (flag)
			{
				bool flag3 = instance?.Allowed ?? false;
				Button obj = button;
				bool interactable = (((Selectable)selection).interactable = flag2);
				((Selectable)obj).interactable = interactable;
				((TMP_Text)label).text = ((!flag2) ? "Late Join unavailable" : (flag3 ? "Late Join: ON" : "Late Join: OFF"));
				menuButton.colorNormal = (flag3 ? OnColor : OffColor);
			}
		}

		private void Toggle()
		{
			SimpleLateJoinPlugin.Instance?.Toggle();
			Update();
		}
	}
	[BepInPlugin("petop.repo.simplelatejoin", "Simple Late Join", "0.1.0")]
	[BepInDependency("zichen.latejoinnow", "1.1.2")]
	public sealed class SimpleLateJoinPlugin : BaseUnityPlugin
	{
		internal const string PluginId = "petop.repo.simplelatejoin";

		internal const string BackendId = "zichen.latejoinnow";

		private Harmony? harmony;

		private Func<bool>? backendActive;

		private Action? refreshBackend;

		private Room? room;

		private bool allowed;

		internal static SimpleLateJoinPlugin? Instance { get; private set; }

		internal bool Available => backendActive?.Invoke() ?? false;

		internal bool Allowed
		{
			get
			{
				if (PhotonNetwork.CurrentRoom != null && room == PhotonNetwork.CurrentRoom)
				{
					return allowed;
				}
				return false;
			}
		}

		internal static bool IsRunPhase()
		{
			if (Object.op_Implicit((Object)(object)RunManager.instance) && Object.op_Implicit((Object)(object)RunManager.instance.levelCurrent))
			{
				if (!SemiFunc.RunIsLobby() && !SemiFunc.RunIsShop())
				{
					return SemiFunc.RunIsLevel();
				}
				return true;
			}
			return false;
		}

		internal static bool IsHostInRun()
		{
			if (PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient && Object.op_Implicit((Object)(object)GameManager.instance) && GameManager.Multiplayer())
			{
				return IsRunPhase();
			}
			return false;
		}

		private void Awake()
		{
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Expected O, but got Unknown
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Expected O, but got Unknown
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Expected O, but got Unknown
			Instance = this;
			if (!Chainloader.PluginInfos.TryGetValue("zichen.latejoinnow", out var value) || !Object.op_Implicit((Object)(object)value.Instance))
			{
				return;
			}
			Type type = ((object)value.Instance).GetType();
			MethodInfo methodInfo = AccessTools.DeclaredMethod(type, "IsCurrentSceneAllowed", Type.EmptyTypes, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.DeclaredMethod(type, "IsHookActive", Type.EmptyTypes, (Type[])null);
			MethodInfo methodInfo3 = AccessTools.DeclaredMethod(type, "EnforceLobbyState", Type.EmptyTypes, (Type[])null);
			if (methodInfo?.ReturnType != typeof(bool) || methodInfo2?.ReturnType != typeof(bool) || methodInfo3?.ReturnType != typeof(void) || !methodInfo.IsStatic || !methodInfo2.IsStatic || !methodInfo3.IsStatic)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Simple Late Join could not find the admission methods in LateJoinNow 1.1.2.");
				return;
			}
			try
			{
				Harmony val = new Harmony("petop.repo.simplelatejoin");
				val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(SimpleLateJoinPlugin), "RequireHostPermission", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				val.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(MenuPageEsc), "Start", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(SimpleLateJoinPlugin), "AddButton", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				harmony = val;
				backendActive = AccessTools.MethodDelegate<Func<bool>>(methodInfo2, (object)null, true);
				refreshBackend = AccessTools.MethodDelegate<Action>(methodInfo3, (object)null, true);
			}
			catch (Exception ex)
			{
				new Harmony("petop.repo.simplelatejoin").UnpatchSelf();
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private void Update()
		{
			Room currentRoom = PhotonNetwork.CurrentRoom;
			if (!PhotonNetwork.IsMasterClient || room != currentRoom)
			{
				room = (PhotonNetwork.IsMasterClient ? currentRoom : null);
				allowed = false;
			}
		}

		internal void Toggle()
		{
			if (IsHostInRun() && Available)
			{
				bool flag = !Allowed;
				room = PhotonNetwork.CurrentRoom;
				allowed = flag;
				refreshBackend?.Invoke();
			}
		}

		private static void RequireHostPermission(ref bool __result)
		{
			if (__result && IsHostInRun())
			{
				__result = Instance?.Allowed ?? false;
			}
		}

		private static void AddButton(MenuPageEsc __instance)
		{
			if (Object.op_Implicit((Object)(object)__instance) && !Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<LateJoinButton>()))
			{
				((Component)__instance).gameObject.AddComponent<LateJoinButton>().Build(__instance);
			}
		}

		private void OnDestroy()
		{
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
		}
	}
}