Decompiled source of N2NDirectConnect v1.0.0

BepInEx/plugins/N2NDirectConnect/BigWalkN2n.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("BigWalkN2n")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BigWalkN2n")]
[assembly: AssemblyTitle("BigWalkN2n")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BigWalkN2n;

[BepInPlugin("lajilaji.bigwalk.n2n-direct-connect", "N2N Direct Connect", "1.0.0")]
public sealed class Plugin : BasePlugin
{
	public const string PluginGuid = "lajilaji.bigwalk.n2n-direct-connect";

	public const string PluginName = "N2N Direct Connect";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Logger;

	public override void Load()
	{
		//IL_0010: 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_002f: Expected O, but got Unknown
		Logger = ((BasePlugin)this).Log;
		new Harmony("lajilaji.bigwalk.n2n-direct-connect").PatchAll();
		ClassInjector.RegisterTypeInIl2Cpp<DirectConnectController>();
		GameObject val = new GameObject("N2NDirectConnectController");
		Object.DontDestroyOnLoad((Object)val);
		val.AddComponent<DirectConnectController>();
		Logger.LogInfo((object)"Loaded. The Join Game page now connects directly by IPv4 and password.");
	}
}
internal static class DirectConnectState
{
	internal const string PasswordFieldName = "N2NDirectConnectPassword";

	internal static TMP_InputField PasswordField;

	internal static string PendingPassword = string.Empty;

	internal static bool WaitingForPassword;

	internal static void ConfigureJoinPage(JoinMenu menu)
	{
		if ((Object)(object)menu.addressField == (Object)null)
		{
			throw new InvalidOperationException("BIG WALK JoinMenu.addressField is missing.");
		}
		Hide((Component)(object)menu.friendsSection);
		Hide((Component)(object)menu.cardParent);
		Hide((Component)(object)menu.noneFoundCard);
		Hide((Component)(object)menu.warningDisplay);
		menu.RequiresAuth = false;
		((Component)menu.addressField).gameObject.SetActive(true);
		SetPlaceholder(menu.addressField, "IP 地址,例如 10.151.133.54");
		PasswordField = FindOrCreatePasswordField(menu);
		((Component)PasswordField).gameObject.SetActive(true);
		PasswordField.contentType = (ContentType)7;
		PasswordField.characterLimit = 32;
		SetPlaceholder(PasswordField, "密码");
		PasswordField.ForceLabelUpdate();
		SetText(((Component)menu.addressField).transform.parent, "Subheading", "通过 IP 加入:");
		SetText(((Component)menu.addressField).transform.parent, "PlayButton/Text (TMP)", "确定");
		Plugin.Logger.LogInfo((object)"Configured Join Game page with IPv4 and password fields only.");
	}

	internal static bool TryBeginConnection(JoinMenu menu)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Expected O, but got Unknown
		string text = menu.addressField.text.Trim();
		bool flag = default(bool);
		if (!IPAddress.TryParse(text, out IPAddress address) || address.AddressFamily != AddressFamily.InterNetwork)
		{
			menu.addressField.text = string.Empty;
			SetPlaceholder(menu.addressField, "Invalid IPv4 address");
			((Selectable)menu.addressField).Select();
			ManualLogSource logger = Plugin.Logger;
			BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(31, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Rejected invalid IPv4 address: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
			}
			logger.LogWarning(val);
			return false;
		}
		if ((Object)(object)PasswordField == (Object)null)
		{
			throw new InvalidOperationException("The direct-connect password field is missing.");
		}
		PendingPassword = PasswordField.text;
		WaitingForPassword = true;
		menu.GoToConnectingMenu();
		NetworkMinder.SetTransportAndConnect(text);
		ManualLogSource logger2 = Plugin.Logger;
		BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(29, 1, ref flag);
		if (flag)
		{
			((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Connecting directly to ");
			((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text);
			((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(":7777.");
		}
		logger2.LogInfo(val2);
		return true;
	}

	internal static void SubmitPendingPassword(PasswordMenu passwordMenu)
	{
		if (WaitingForPassword)
		{
			if ((Object)(object)passwordMenu.passwordField == (Object)null)
			{
				throw new InvalidOperationException("BIG WALK password menu input field is missing.");
			}
			passwordMenu.passwordField.text = PendingPassword;
			WaitingForPassword = false;
			passwordMenu.SubmitPassword();
			Plugin.Logger.LogInfo((object)"Submitted the password requested by the host.");
		}
	}

	private static TMP_InputField FindOrCreatePasswordField(JoinMenu menu)
	{
		Transform parent = ((Component)menu.addressField).transform.parent;
		if ((Object)(object)parent == (Object)null)
		{
			throw new InvalidOperationException("BIG WALK address field has no parent transform.");
		}
		Transform val = parent.Find("N2NDirectConnectPassword");
		if ((Object)(object)val != (Object)null)
		{
			TMP_InputField component = ((Component)val).GetComponent<TMP_InputField>();
			if ((Object)(object)component == (Object)null)
			{
				throw new InvalidOperationException("N2NDirectConnectPassword exists without TMP_InputField.");
			}
			return component;
		}
		MainMenuManager instance = MainMenuManager.instance;
		object obj;
		if (instance == null)
		{
			obj = null;
		}
		else
		{
			PasswordMenu passwordMenu = instance.passwordMenu;
			obj = ((passwordMenu != null) ? passwordMenu.passwordField : null);
		}
		if ((Object)obj == (Object)null)
		{
			throw new InvalidOperationException("BIG WALK password input field is missing.");
		}
		GameObject obj2 = Object.Instantiate<GameObject>(((Component)obj).gameObject, parent);
		((Object)obj2).name = "N2NDirectConnectPassword";
		TMP_InputField component2 = obj2.GetComponent<TMP_InputField>();
		if ((Object)(object)component2 == (Object)null)
		{
			throw new InvalidOperationException("Cloned password object has no TMP_InputField.");
		}
		((UnityEventBase)component2.onValueChanged).RemoveAllListeners();
		((UnityEventBase)component2.onEndEdit).RemoveAllListeners();
		((UnityEventBase)component2.onSubmit).RemoveAllListeners();
		PositionBelowAddressField(menu.addressField, component2);
		return component2;
	}

	private static void PositionBelowAddressField(TMP_InputField addressField, TMP_InputField passwordField)
	{
		//IL_002d: 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_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		RectTransform component = ((Component)addressField).GetComponent<RectTransform>();
		RectTransform component2 = ((Component)passwordField).GetComponent<RectTransform>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
		{
			throw new InvalidOperationException("Direct-connect input fields require RectTransform.");
		}
		component2.anchorMin = component.anchorMin;
		component2.anchorMax = component.anchorMax;
		component2.pivot = component.pivot;
		component2.sizeDelta = component.sizeDelta;
		Vector2 anchoredPosition = component.anchoredPosition;
		Rect rect = component.rect;
		component2.anchoredPosition = anchoredPosition + new Vector2(0f, 0f - Math.Max(72f, ((Rect)(ref rect)).height * 1.35f));
		((Transform)component2).localScale = ((Transform)component).localScale;
	}

	private static void SetPlaceholder(TMP_InputField field, string value)
	{
		Graphic placeholder = field.placeholder;
		TMP_Text obj = ((placeholder != null) ? ((Component)placeholder).GetComponent<TMP_Text>() : null);
		if ((Object)(object)obj == (Object)null)
		{
			throw new InvalidOperationException("Input field " + ((Object)field).name + " has no TMP_Text placeholder.");
		}
		obj.text = value;
	}

	private static void Hide(Component component)
	{
		if ((Object)(object)component != (Object)null)
		{
			component.gameObject.SetActive(false);
		}
	}

	private static void SetText(Transform parent, string relativePath, string value)
	{
		Transform obj = parent.Find(relativePath);
		TMP_Text obj2 = ((obj != null) ? ((Component)obj).GetComponent<TMP_Text>() : null);
		if ((Object)(object)obj2 == (Object)null)
		{
			throw new InvalidOperationException("Join page text is missing: " + relativePath);
		}
		obj2.text = value;
	}
}
public sealed class DirectConnectController : MonoBehaviour
{
	public DirectConnectController(IntPtr pointer)
		: base(pointer)
	{
	}

	private void Update()
	{
		if (DirectConnectState.WaitingForPassword)
		{
			MainMenuManager instance = MainMenuManager.instance;
			PasswordMenu val = ((instance != null) ? instance.passwordMenu : null);
			if ((Object)(object)val != (Object)null && ((Component)val).gameObject.activeInHierarchy)
			{
				DirectConnectState.SubmitPendingPassword(val);
			}
		}
	}
}
[HarmonyPatch(typeof(JoinMenu), "OnEnable")]
internal static class JoinMenuOnEnablePatch
{
	private static void Postfix(JoinMenu __instance)
	{
		DirectConnectState.ConfigureJoinPage(__instance);
	}
}
[HarmonyPatch(typeof(JoinMenu), "Connect")]
internal static class JoinMenuConnectPatch
{
	private static bool Prefix(JoinMenu __instance)
	{
		DirectConnectState.TryBeginConnection(__instance);
		return false;
	}
}