Decompiled source of ShardExclusivity v1.0.0

ShardExclusivity.dll

Decompiled a day 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.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ShardExclusivity")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ShardExclusivity")]
[assembly: AssemblyTitle("ShardExclusivity")]
[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 ShardExclusivity
{
	[BepInPlugin("pieter.valheim.shardexclusivity", "Shard Exclusivity", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "pieter.valheim.shardexclusivity";

		public const string Name = "Shard Exclusivity";

		public const string Version = "1.0.0";

		private const string EpicLootGuid = "randyknapp.mods.epicloot";

		private static ConfigEntry<bool> _enabled;

		private static MethodInfo _resolve;

		private static FieldInfo _exclusive;

		private static object _runeColor;

		private static int _shardDepth;

		private void Awake()
		{
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Expected O, but got Unknown
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ignore effect exclusions for shards", true, "Shardstones may be socketed even when their effect is normally excluded by an effect already on the item (e.g. Add Poison Damage on a weapon with Elemental Damage Dealt). Enchanting and loot keep the normal rules.");
			Type type = AccessTools.TypeByName("EpicLoot.ShardStones.ShardSocketManager");
			Type type2 = AccessTools.TypeByName("EpicLoot.MagicItemEffectRequirements");
			Type type3 = AccessTools.TypeByName("EpicLoot.ShardStones.ShardType") ?? AccessTools.TypeByName("EpicLoot.ShardType");
			_resolve = ((type == null) ? null : AccessTools.Method(type, "ResolveSocketedEffect", (Type[])null, (Type[])null));
			_exclusive = ((type2 == null) ? null : AccessTools.Field(type2, "ExclusiveEffectTypes"));
			_runeColor = ((type3 == null) ? null : Enum.Parse(type3, "None"));
			MethodInfo methodInfo = ((type2 == null) ? null : AccessTools.Method(type2, "CheckRequirements", new Type[9]
			{
				typeof(ItemData),
				AccessTools.TypeByName("EpicLoot.MagicItem"),
				AccessTools.TypeByName("EpicLoot.RequirementFailure").MakeByRefType(),
				typeof(string).MakeByRefType(),
				typeof(string),
				typeof(bool),
				typeof(bool),
				typeof(bool),
				typeof(bool)
			}, (Type[])null));
			if (_resolve == null || _exclusive == null || _runeColor == null || methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Disabled, Epic Loot's shard socket internals were not found.");
				return;
			}
			Harmony val = new Harmony("pieter.valheim.shardexclusivity");
			string[] array = new string[3] { "CanSocket", "CanCoexist", "AddShard" };
			foreach (string text in array)
			{
				MethodInfo methodInfo2 = AccessTools.Method(type, text, (Type[])null, (Type[])null);
				if (!(methodInfo2 == null))
				{
					val.Patch((MethodBase)methodInfo2, Hook("SocketPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("SocketFinalizer"), (HarmonyMethod)null);
				}
			}
			val.Patch((MethodBase)methodInfo, Hook("CheckPrefix"), (HarmonyMethod)null, (HarmonyMethod)null, Hook("CheckFinalizer"), (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Shardstones ignore effect exclusions when socketed.");
		}

		private static HarmonyMethod Hook(string name)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			return new HarmonyMethod(typeof(Plugin).GetMethod(name, BindingFlags.Static | BindingFlags.NonPublic));
		}

		private static void SocketPrefix(ItemData equipment, ItemData input, out bool __state)
		{
			__state = false;
			if (!_enabled.Value || equipment == null || input == null)
			{
				return;
			}
			object[] array = new object[5] { equipment, input, null, null, null };
			try
			{
				if (!(bool)_resolve.Invoke(null, array) || object.Equals(array[3], _runeColor))
				{
					return;
				}
			}
			catch
			{
				return;
			}
			_shardDepth++;
			__state = true;
		}

		private static Exception SocketFinalizer(Exception __exception, bool __state)
		{
			if (__state)
			{
				_shardDepth--;
			}
			return __exception;
		}

		private static void CheckPrefix(object __instance, out List<string> __state)
		{
			__state = null;
			if (_shardDepth > 0 && _exclusive.GetValue(__instance) is List<string> { Count: not 0 } list)
			{
				__state = list;
				_exclusive.SetValue(__instance, new List<string>());
			}
		}

		private static Exception CheckFinalizer(Exception __exception, object __instance, List<string> __state)
		{
			if (__state != null)
			{
				_exclusive.SetValue(__instance, __state);
			}
			return __exception;
		}
	}
}