Decompiled source of RegionSwitcher v1.0.0

RegionSwitcher.dll

Decompiled 6 months 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.Logging;
using ContentSettings.API;
using ContentSettings.API.Settings;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using Zorro.Settings;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("RegionSwitcher")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RegionSwitcher")]
[assembly: AssemblyTitle("RegionSwitcher")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace RegionSwitcher
{
	[ContentWarningPlugin("Region Switcher", "1.0", true)]
	[BepInPlugin("RegionSwitcher", "Region Switcher", "1.0")]
	public class RegionSwitcher : BaseUnityPlugin
	{
		public List<string> AvailableRegions = new List<string> { "us", "usw", "asia", "sa", "eu" };

		public string SelectedRegion = "";

		internal static ManualLogSource Logger { get; private set; }

		public static RegionSwitcher Instance { get; private set; }

		private void Awake()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			SettingsLoader.RegisterSetting("Region", (Setting)(object)new RegionSetting());
			new Harmony("RegionSwitcher").PatchAll(typeof(RegionSwitcherPatch));
		}
	}
	internal class RegionSetting : EnumSetting, ICustomSetting, IExposedSetting
	{
		public string GetDisplayName()
		{
			return "Server - Will take effect the next time you enter a lobby";
		}

		public override List<string> GetChoices()
		{
			if (PhotonNetwork.NetworkingClient.RegionHandler == null)
			{
				RegionSwitcher.Logger.LogWarning((object)"RegionHandler is null");
				return RegionSwitcher.Instance.AvailableRegions;
			}
			List<string> list = new List<string>(RegionSwitcher.Instance.AvailableRegions);
			foreach (Region region in PhotonNetwork.NetworkingClient.RegionHandler.EnabledRegions)
			{
				int num = list.FindIndex((string item) => item.Equals(region.Code));
				if (num > -1 && num < list.Count)
				{
					list[num] = $"{RegionSwitcher.Instance.AvailableRegions[num]}    ({region.Ping}ms)";
				}
			}
			return list;
		}

		public override void ApplyValue()
		{
			if (RegionSwitcher.Instance.AvailableRegions.Count >= 1)
			{
				RegionSwitcher.Logger.LogInfo((object)$"Applied Value: {((IntSetting)this).Value} - {RegionSwitcher.Instance.AvailableRegions[((IntSetting)this).Value]}");
				RegionSwitcher.Instance.SelectedRegion = RegionSwitcher.Instance.AvailableRegions[((IntSetting)this).Value];
			}
		}

		protected override int GetDefaultValue()
		{
			return RegionSwitcher.Instance.AvailableRegions.FindIndex((string region) => region.Equals(PhotonNetwork.NetworkingClient.RegionHandler.BestRegion.Code));
		}
	}
	internal class RegionSwitcherPatch
	{
		[HarmonyPatch(typeof(LoadBalancingClient), "ConnectToRegionMaster")]
		private static void Prefix(ref string region)
		{
			if (!string.IsNullOrEmpty(RegionSwitcher.Instance.SelectedRegion) && RegionSwitcher.Instance.AvailableRegions.Contains(RegionSwitcher.Instance.SelectedRegion))
			{
				region = RegionSwitcher.Instance.SelectedRegion;
			}
		}
	}
}