Decompiled source of DualWieldExclusionFix v1.0.0

plugins/DualWieldExclusionFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.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 DualWieldExclusionFix
{
	[BepInPlugin("local.dualwield.exclusionfix", "DualWield Exclusion Fix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "local.dualwield.exclusionfix";

		private Harmony harmony;

		private static ConfigEntry<string> exclusions;

		private static readonly ExclusionPolicy policy = new ExclusionPolicy();

		private void Awake()
		{
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			try
			{
				PluginInfo obj = Chainloader.PluginInfos["Ketanol.DualWieldCore"];
				if (obj.Metadata.Version != new Version(1, 2, 4))
				{
					throw new NotSupportedException("This fix targets DualWieldCore 1.2.4 only.");
				}
				Assembly assembly = ((object)obj.Instance).GetType().Assembly;
				MethodInfo methodInfo = AccessTools.Method(assembly.GetType("DualWieldCore.Equipment", throwOnError: true), "SupportedWeapon", new Type[1] { typeof(ItemData) }, (Type[])null);
				exclusions = AccessTools.Field(assembly.GetType("DualWieldCore.Plugin", throwOnError: true), "ExcludedPrefabs")?.GetValue(null) as ConfigEntry<string>;
				if (methodInfo == null || !methodInfo.IsStatic || methodInfo.ReturnType != typeof(bool) || exclusions == null)
				{
					throw new MissingMemberException("Unsupported DualWieldCore layout; no patches applied.");
				}
				harmony = new Harmony("local.dualwield.exclusionfix");
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "RespectExclusions", (Type[])null)
				{
					priority = 0
				}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"DualWield Exclusion Fix 1.0.0 active: ExcludedPrefabs now protects standalone attacks as well as dual pairs.");
			}
			catch (Exception ex)
			{
				Harmony obj2 = harmony;
				if (obj2 != null)
				{
					obj2.UnpatchSelf();
				}
				((BaseUnityPlugin)this).Logger.LogError((object)("Exclusion fix inactive: " + ex));
			}
		}

		private static void RespectExclusions(ItemData __0, ref bool __result)
		{
			if (__result && __0 != null && Object.op_Implicit((Object)(object)__0.m_dropPrefab) && exclusions != null && policy.Contains(exclusions.Value, ((Object)__0.m_dropPrefab).name))
			{
				__result = false;
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	public sealed class ExclusionPolicy
	{
		private string cached;

		private readonly HashSet<string> names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		public bool Contains(string configured, string prefab)
		{
			configured = configured ?? "";
			if (!string.Equals(cached, configured, StringComparison.Ordinal))
			{
				names.Clear();
				string[] array = configured.Split(',');
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i].Trim();
					if (text.Length > 0)
					{
						names.Add(text);
					}
				}
				cached = configured;
			}
			if (prefab != null)
			{
				return names.Contains(prefab);
			}
			return false;
		}
	}
}