Decompiled source of GameModePickerGamepadFix v1.0.0

GameModePickerGamepadFix.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2.UI;
using UnityEngine;
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("GameModePickerGamepadFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GameModePickerGamepadFix")]
[assembly: AssemblyTitle("GameModePickerGamepadFix")]
[assembly: AssemblyVersion("1.0.0.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 GameModePickerGamepadFix
{
	[BepInPlugin("com.syntaxspecter.GameModePickerGamepadFix", "GameModePickerGamepadFix", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.syntaxspecter.GameModePickerGamepadFix";

		public const string PluginName = "GameModePickerGamepadFix";

		public const string PluginVersion = "1.0.0";

		private const string GameModeSettingName = "gamemode";

		internal static ManualLogSource Log;

		private Harmony harmony;

		public void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("com.syntaxspecter.GameModePickerGamepadFix");
			TryPatch(AccessTools.Method(typeof(CarouselController), "Initialize", (Type[])null, (Type[])null), "OnCarouselReady");
			TryPatch(AccessTools.Method(typeof(BaseSettingsControl), "OnEnable", (Type[])null, (Type[])null), "OnCarouselReady");
		}

		private void TryPatch(MethodBase original, string postfixName)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			string text = ((original != null) ? (original.DeclaringType?.Name + "." + original.Name) : postfixName);
			try
			{
				if (original == null)
				{
					throw new MissingMethodException("Target method not found (the game may have changed)");
				}
				MethodInfo method = typeof(Plugin).GetMethod(postfixName, BindingFlags.Static | BindingFlags.NonPublic);
				harmony.Patch(original, (HarmonyMethod)null, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Could not patch '" + text + "', game mode picker gamepad fix may not apply: " + ex.Message));
			}
		}

		private static void OnCarouselReady(BaseSettingsControl __instance)
		{
			try
			{
				if (__instance is CarouselController && __instance.settingName == "gamemode")
				{
					MakeReachable(((Component)__instance).GetComponent<Selectable>());
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Failed to fix game mode picker navigation: {arg}");
			}
		}

		private static void MakeReachable(Selectable picker)
		{
			//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_017e: 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_003e: 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_0047: Invalid comparison between Unknown and I4
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: 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_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Invalid comparison between Unknown and I4
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)picker == (Object)null || (Object)(object)((Component)picker).transform.parent == (Object)null)
			{
				return;
			}
			Navigation navigation = picker.navigation;
			Selectable val = FindNavigableSibling(picker, -1);
			if ((Object)(object)val != (Object)null)
			{
				Navigation navigation2 = val.navigation;
				if ((int)((Navigation)(ref navigation2)).mode == 4)
				{
					navigation2 = val.navigation;
					if ((Object)(object)((Navigation)(ref navigation2)).selectOnDown != (Object)(object)picker)
					{
						Navigation navigation3 = val.navigation;
						Selectable val2 = (((Object)(object)((Navigation)(ref navigation3)).selectOnDown != (Object)null) ? ((Navigation)(ref navigation3)).selectOnDown : FindNavigableSibling(picker, 1));
						((Navigation)(ref navigation)).mode = (Mode)4;
						((Navigation)(ref navigation)).selectOnUp = val;
						((Navigation)(ref navigation)).selectOnDown = val2;
						((Navigation)(ref navigation)).selectOnLeft = null;
						((Navigation)(ref navigation)).selectOnRight = null;
						picker.navigation = navigation;
						((Navigation)(ref navigation3)).selectOnDown = picker;
						val.navigation = navigation3;
						if ((Object)(object)val2 != (Object)null)
						{
							navigation2 = val2.navigation;
							if ((int)((Navigation)(ref navigation2)).mode == 4)
							{
								navigation2 = val2.navigation;
								if ((Object)(object)((Navigation)(ref navigation2)).selectOnUp == (Object)(object)val)
								{
									Navigation navigation4 = val2.navigation;
									((Navigation)(ref navigation4)).selectOnUp = picker;
									val2.navigation = navigation4;
								}
							}
						}
						Log.LogInfo((object)("Linked game mode picker '" + ((Object)picker).name + "' between '" + ((Object)val).name + "' and '" + (((Object)(object)val2 != (Object)null) ? ((Object)val2).name : "nothing") + "'"));
						return;
					}
				}
			}
			if ((int)((Navigation)(ref navigation)).mode == 0)
			{
				((Navigation)(ref navigation)).mode = (Mode)2;
				picker.navigation = navigation;
				Log.LogInfo((object)("Enabled vertical navigation on game mode picker '" + ((Object)picker).name + "'"));
			}
		}

		private static Selectable FindNavigableSibling(Selectable picker, int step)
		{
			//IL_0040: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = ((Component)picker).transform.parent;
			for (int i = ((Component)picker).transform.GetSiblingIndex() + step; i >= 0 && i < parent.childCount; i += step)
			{
				Selectable component = ((Component)parent.GetChild(i)).GetComponent<Selectable>();
				if ((Object)(object)component != (Object)null && ((Component)component).gameObject.activeSelf)
				{
					Navigation navigation = component.navigation;
					if ((int)((Navigation)(ref navigation)).mode != 0)
					{
						return component;
					}
				}
			}
			return null;
		}
	}
}