Decompiled source of SwmarlyValheimBetterFistsWeapons v1.0.1

BepInEx/plugins/SwmarlyValheimBetterFistsWeapons/SwmarlyValheimBetterFistsWeapons.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Swmarly Valheim Better Fists Weapons")]
[assembly: AssemblyProduct("SwmarlyValheimBetterFistsWeapons")]
[assembly: AssemblyCompany("Swmarly")]
[assembly: AssemblyDescription("Gives Unarmed/Fists weapons the vanilla knife secondary attack.")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 SwmarlyValheimBetterFistsWeapons
{
	[BepInPlugin("com.swmarly.valheimbetterfistsweapons", "Swmarly Valheim Better Fists Weapons", "1.0.1")]
	[BepInProcess("valheim.exe")]
	[BepInProcess("valheim_server.exe")]
	[BepInProcess("valheim_server.x86_64")]
	public sealed class BetterFistsWeaponsPlugin : BaseUnityPlugin
	{
		internal const string Guid = "com.swmarly.valheimbetterfistsweapons";

		internal const string Name = "Swmarly Valheim Better Fists Weapons";

		internal const string Version = "1.0.1";

		internal static ManualLogSource Log;

		private Harmony harmony;

		private 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.swmarly.valheimbetterfistsweapons");
			try
			{
				harmony.PatchAll(typeof(BetterFistsWeaponsPlugin).Assembly);
				Log.LogInfo((object)"Swmarly Valheim Better Fists Weapons 1.0.1 loaded.");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to install the ObjectDB patch. The mod will remain disabled: " + ex));
			}
		}

		private void OnDestroy()
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
				harmony = null;
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	internal static class ObjectDbAwakePatch
	{
		private static void Postfix(ObjectDB __instance)
		{
			try
			{
				FistWeaponAttackReplacer.Apply(__instance);
			}
			catch (Exception ex)
			{
				BetterFistsWeaponsPlugin.Log.LogError((object)("Failed to replace fist weapon secondary attacks: " + ex));
			}
		}
	}
	internal static class FistWeaponAttackReplacer
	{
		private const string PreferredKnifePrefab = "KnifeFlint";

		private const string BareHandsPrefab = "PlayerUnarmed";

		internal static void Apply(ObjectDB objectDb)
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Invalid comparison between Unknown and I4
			if ((Object)(object)objectDb == (Object)null || objectDb.m_items == null || objectDb.m_items.Count == 0)
			{
				return;
			}
			Attack val = FindKnifeSecondaryAttack(objectDb);
			if (val == null)
			{
				BetterFistsWeaponsPlugin.Log.LogWarning((object)"No vanilla knife with a secondary attack was found; fist weapons were left unchanged.");
				return;
			}
			int num = 0;
			foreach (GameObject item in objectDb.m_items)
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				ItemDrop component = item.GetComponent<ItemDrop>();
				if (!((Object)(object)component == (Object)null) && component.m_itemData != null && component.m_itemData.m_shared != null && !(((Object)item).name == "PlayerUnarmed"))
				{
					SharedData shared = component.m_itemData.m_shared;
					if ((int)shared.m_skillType == 11)
					{
						Attack source = shared.m_attack ?? shared.m_secondaryAttack;
						Attack val2 = val.Clone();
						CopyAttackEffectLists(source, val2);
						shared.m_secondaryAttack = val2;
						num++;
						BetterFistsWeaponsPlugin.Log.LogDebug((object)("Applied the vanilla knife secondary attack to " + ((Object)item).name + "."));
					}
				}
			}
			BetterFistsWeaponsPlugin.Log.LogInfo((object)("Applied the vanilla knife secondary attack to " + num + " Unarmed/Fists item(s)."));
		}

		private static Attack FindKnifeSecondaryAttack(ObjectDB objectDb)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Invalid comparison between Unknown and I4
			Attack result = null;
			foreach (GameObject item in objectDb.m_items)
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				ItemDrop component = item.GetComponent<ItemDrop>();
				if ((Object)(object)component == (Object)null || component.m_itemData == null || component.m_itemData.m_shared == null)
				{
					continue;
				}
				SharedData shared = component.m_itemData.m_shared;
				if ((int)shared.m_skillType == 2 && shared.m_secondaryAttack != null && !string.IsNullOrEmpty(shared.m_secondaryAttack.m_attackAnimation))
				{
					if (((Object)item).name == "KnifeFlint")
					{
						return shared.m_secondaryAttack;
					}
					result = shared.m_secondaryAttack;
				}
			}
			return result;
		}

		private static void CopyAttackEffectLists(Attack source, Attack target)
		{
			if (source == null || target == null)
			{
				return;
			}
			FieldInfo[] fields = typeof(Attack).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.FieldType == typeof(EffectList))
				{
					fieldInfo.SetValue(target, fieldInfo.GetValue(source));
				}
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}