Decompiled source of RunicInventoryFix v1.0.0

plugins/RunicInventoryFix/RunicInventoryFix.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("RunicInventoryFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RunicInventoryFix")]
[assembly: AssemblyTitle("RunicInventoryFix")]
[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 RunicInventoryFix
{
	[BepInPlugin("s6652289.RunicInventoryFix", "RunicInventoryFix", "1.0.0")]
	public class FixPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource L;

		internal static ConfigEntry<bool> SuppressMouseGesture;

		internal static ConfigEntry<bool> Verbose;

		private const string TargetTypeName = "RunicInventory.Integration.InventoryRuntime";

		private const string TargetMethodName = "TryTogglePointerLock";

		private Harmony _harmony;

		private bool _installed;

		private void Awake()
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			L = ((BaseUnityPlugin)this).Logger;
			SuppressMouseGesture = ((BaseUnityPlugin)this).Config.Bind<bool>("Interaction", "SuppressMouseLockGesture", true, "关掉 RunicInventory 的鼠标锁格手势(硬编码的左Alt+右键),让 Stackmaster 的 Alt+左键/右键恢复可用。RunicInventory 的键盘锁格键(默认 L+左Alt)与其它功能不受影响。");
			Verbose = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "Verbose", true, "打印补丁安装与命中细节(确认没问题后可关)。");
			L.LogInfo((object)"=============== RunicInventoryFix 1.0.0 启动 ===============");
			L.LogInfo((object)"作用:关掉 RunicInventory 的『左Alt+右键』鼠标锁格手势,把 Alt+鼠标 让给 Stackmaster。");
			L.LogInfo((object)"保留:RunicInventory 键盘锁格键(默认 L+LeftAlt)、额外行、整理、拾取过滤、UI —— 全部不动;");
			L.LogInfo((object)"      也就是说 RunicInventory 的『键盘』能力完好,只是不再抢鼠标手势。");
			if (!SuppressMouseGesture.Value)
			{
				L.LogWarning((object)"[Fix] SuppressMouseLockGesture = false ⇒ 不做任何事(两个模组的 Alt+鼠标 冲突会照旧)。");
				return;
			}
			_harmony = new Harmony("s6652289.RunicInventoryFix");
			((MonoBehaviour)this).StartCoroutine(InstallWhenReady());
		}

		private IEnumerator InstallWhenReady()
		{
			for (int attempt = 0; attempt < 180; attempt++)
			{
				if (_installed)
				{
					break;
				}
				try
				{
					TryInstall();
				}
				catch (Exception ex)
				{
					L.LogError((object)("[Fix] 安装异常:" + ex));
				}
				if (_installed)
				{
					yield break;
				}
				yield return (object)new WaitForSecondsRealtime(1f);
			}
			if (!_installed)
			{
				L.LogWarning((object)"[Fix] 180 秒内没等到 RunicInventory.Integration.InventoryRuntime —— RunicInventory 可能没装或版本不匹配,本模组未做任何改动。");
			}
		}

		private void TryInstall()
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			Type type = FindType("RunicInventory.Integration.InventoryRuntime");
			if (type == null)
			{
				return;
			}
			MethodInfo methodInfo = FindMethod(type, "TryTogglePointerLock");
			if (methodInfo == null)
			{
				L.LogError((object)"[Fix] 找到类型但没有 TryTogglePointerLock —— RunicInventory 版本不匹配,拒绝打补丁(本模组不做任何改动)。");
				_installed = true;
				return;
			}
			ParameterInfo[] parameters = methodInfo.GetParameters();
			if (methodInfo.ReturnType != typeof(bool) || methodInfo.IsStatic || parameters.Length != 2)
			{
				L.LogError((object)string.Format("[Fix] 目标方法签名不符(期望 实例方法 bool {0}(2 参数)):实际 {1} {2}({3} 参数) —— 拒绝打补丁。", "TryTogglePointerLock", methodInfo.ReturnType.Name, methodInfo.IsStatic ? "static" : "instance", parameters.Length));
				_installed = true;
				return;
			}
			try
			{
				HarmonyMethod val = new HarmonyMethod(AccessTools.Method(typeof(FixPlugin), "SkipMouseGesture", (Type[])null, (Type[])null));
				_harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				_installed = true;
				L.LogInfo((object)"[Fix] 已打补丁:RunicInventory.Integration.InventoryRuntime.TryTogglePointerLock → Prefix 恒返回 false(跳过原方法)");
				L.LogInfo((object)"[Fix] 效果:RunicInventory 的『左Alt+右键』不再拦截 InventoryGrid.OnRightDown,");
				L.LogInfo((object)"[Fix]       Stackmaster 的 Alt+左键 / Alt+右键 应恢复正常。");
				if (Verbose.Value)
				{
					L.LogInfo((object)("[Fix] 目标方法签名确认:" + methodInfo.ReturnType.Name + " TryTogglePointerLock(" + parameters[0].ParameterType.Name + ", " + parameters[1].ParameterType.Name + ")"));
				}
				L.LogInfo((object)"[Fix] 验证方法:打开背包 → Alt+左键点物品,看是否出现 Stackmaster 的 『Stackmaster: item protected./item unprotected.』提示。");
			}
			catch (Exception ex)
			{
				L.LogError((object)("[Fix] 打补丁失败:" + ex));
				_installed = true;
			}
		}

		private static bool SkipMouseGesture(ref bool __result)
		{
			__result = false;
			return false;
		}

		private static Type FindType(string fullName)
		{
			Type type = AccessTools.TypeByName(fullName);
			if (type != null)
			{
				return type;
			}
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				try
				{
					if (assembly.GetName().Name.IndexOf("RunicInventory", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						type = assembly.GetType(fullName, throwOnError: false);
						if (type != null)
						{
							return type;
						}
					}
				}
				catch
				{
				}
			}
			return null;
		}

		private static MethodInfo FindMethod(Type type, string name)
		{
			MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.Name == name)
				{
					return methodInfo;
				}
			}
			return null;
		}
	}
}