Decompiled source of LookingGlassCommandFix v1.0.0

plugins/LookingGlassCommandFix.dll

Decompiled 13 hours 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 HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("LookingGlassCommandFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LookingGlassCommandFix")]
[assembly: AssemblyTitle("LookingGlassCommandFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 LookingGlassCommandFix
{
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
	[BepInPlugin("Bryndan.LookingGlassCommandFix", "LookingGlassCommandFix", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginAuthor = "Bryndan";

		public const string PluginName = "LookingGlassCommandFix";

		public const string PluginVersion = "1.0.0";

		public const string PluginGUID = "Bryndan.LookingGlassCommandFix";

		public void Awake()
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			try
			{
				Type type = AccessTools.TypeByName("LookingGlass.CommandItemCount.CommandItemCountClass");
				if (type == null)
				{
					Log.Error("Could not find LookingGlass.CommandItemCount.CommandItemCountClass. Is LookingGlass installed?");
					return;
				}
				MethodInfo methodInfo = AccessTools.Method(type, "SubmitChoice", new Type[3]
				{
					typeof(Action<PickupPickerController, int>),
					typeof(PickupPickerController),
					typeof(int)
				}, (Type[])null);
				if (methodInfo == null)
				{
					Log.Error("Could not find LookingGlass SubmitChoice method.");
					return;
				}
				Harmony val = new Harmony("Bryndan.LookingGlassCommandFix");
				HarmonyMethod val2 = new HarmonyMethod(typeof(Plugin).GetMethod("SubmitChoicePrefix", BindingFlags.Static | BindingFlags.NonPublic));
				val.Patch((MethodBase)methodInfo, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Log.Info("LookingGlass Command fix patch applied successfully.");
			}
			catch (Exception arg)
			{
				Log.Error($"Failed to apply LookingGlass Command fix: {arg}");
			}
		}

		private static bool SubmitChoicePrefix(object __instance, Action<PickupPickerController, int> orig, PickupPickerController self, int index)
		{
			if ((Object)(object)self == (Object)null || self.options == null)
			{
				return false;
			}
			int num = index;
			try
			{
				FieldInfo fieldInfo = AccessTools.Field(__instance.GetType(), "optionMap");
				if (fieldInfo != null && fieldInfo.GetValue(__instance) is List<int> list && list.Count > 0 && list[0] >= 0)
				{
					if (index >= 0 && index < list.Count)
					{
						int num2 = list[index];
						if (num2 >= 0 && num2 < self.options.Length)
						{
							num = num2;
						}
						else
						{
							Log.Warning($"Mapped LookingGlass option index {num2} was invalid for option count {self.options.Length}. Falling back to original index {index}.");
						}
					}
					else
					{
						Log.Warning($"LookingGlass optionMap index {index} was invalid for optionMap count {list.Count}. Falling back to original index.");
					}
				}
			}
			catch (Exception ex)
			{
				Log.Warning($"Failed reading LookingGlass optionMap, falling back to original index {index}: {ex.Message}");
				num = index;
			}
			if (num < 0 || num >= self.options.Length)
			{
				if (index < 0 || index >= self.options.Length)
				{
					Log.Warning($"Both mapped index {num} and original index {index} were invalid for option count {self.options.Length}. Command selection was skipped.");
					return false;
				}
				num = index;
			}
			orig(self, num);
			return false;
		}
	}
}